11use crate :: helpers:: { FeeHistoryCache , MAX_FEE_HISTORY_CACHE_BLOCK_COUNT } ;
22use async_trait:: async_trait;
3+ use blockgen:: BlockGenerator ;
34use cfx_execute_helper:: estimation:: EstimateRequest ;
45use cfx_executor:: executive:: {
56 Executed , ExecutionError , ExecutionOutcome , TxDropError ,
@@ -32,6 +33,7 @@ use cfx_types::{
3233use cfx_util_macros:: bail;
3334use cfx_vm_types:: Error as VmError ;
3435use cfxcore:: {
36+ consensus_parameters:: DEFERRED_STATE_EPOCH_COUNT ,
3537 errors:: { Error as CoreError , Result as CoreResult } ,
3638 ConsensusGraph , SharedConsensusGraph , SharedSynchronizationService ,
3739 SharedTransactionPool ,
@@ -45,7 +47,9 @@ use primitives::{
4547} ;
4648use rustc_hex:: ToHex ;
4749use solidity_abi:: string_revert_reason_decode;
48- use std:: { collections:: HashMap , future:: Future } ;
50+ use std:: {
51+ collections:: HashMap , future:: Future , sync:: Arc , thread, time:: Duration ,
52+ } ;
4953
5054type BlockNumber = BlockId ;
5155type BlockNumberOrTag = BlockId ;
@@ -59,6 +63,7 @@ pub struct EthApi {
5963 consensus : SharedConsensusGraph ,
6064 sync : SharedSynchronizationService ,
6165 tx_pool : SharedTransactionPool ,
66+ block_gen : Arc < BlockGenerator > ,
6267 fee_history_cache : FeeHistoryCache ,
6368 executor : TaskExecutor ,
6469}
@@ -67,13 +72,14 @@ impl EthApi {
6772 pub fn new (
6873 config : RpcImplConfiguration , consensus : SharedConsensusGraph ,
6974 sync : SharedSynchronizationService , tx_pool : SharedTransactionPool ,
70- executor : TaskExecutor ,
75+ block_gen : Arc < BlockGenerator > , executor : TaskExecutor ,
7176 ) -> Self {
7277 EthApi {
7378 config,
7479 consensus,
7580 sync,
7681 tx_pool,
82+ block_gen,
7783 fee_history_cache : FeeHistoryCache :: new ( ) ,
7884 executor,
7985 }
@@ -83,6 +89,17 @@ impl EthApi {
8389
8490 pub fn tx_pool ( & self ) -> & SharedTransactionPool { & self . tx_pool }
8591
92+ fn generate_one_block (
93+ & self , num_txs : usize , block_size_limit : usize ,
94+ ) -> CoreResult < H256 > {
95+ let hash = self . block_gen . test_api ( ) . generate_block (
96+ num_txs,
97+ block_size_limit,
98+ vec ! [ ] ,
99+ ) ;
100+ Ok ( hash)
101+ }
102+
86103 pub fn fetch_block_by_height (
87104 & self , height : u64 ,
88105 ) -> Result < PhantomBlock , ProviderBlockError > {
@@ -1491,6 +1508,28 @@ impl EthApiServer for EthApi {
14911508 }
14921509
14931510 let r = self . send_transaction_with_signature ( tx) ?;
1511+ if self . config . dev_pack_tx_immediately {
1512+ // Try to pack and execute this new tx.
1513+ for _ in 0 ..DEFERRED_STATE_EPOCH_COUNT {
1514+ let generated = self . generate_one_block (
1515+ 1 , /* num_txs */
1516+ self . sync
1517+ . get_synchronization_graph ( )
1518+ . verification_config
1519+ . max_block_size_in_bytes ,
1520+ ) ?;
1521+ loop {
1522+ // Wait for the new block to be fully processed, so all
1523+ // generated blocks form a chain for
1524+ // `tx` to be executed.
1525+ if self . consensus . best_block_hash ( ) == generated {
1526+ break ;
1527+ } else {
1528+ thread:: sleep ( Duration :: from_millis ( 10 ) ) ;
1529+ }
1530+ }
1531+ }
1532+ }
14941533 Ok ( r)
14951534 }
14961535
0 commit comments