Skip to content

Commit c5050bd

Browse files
committed
feat: support dev_pack_tx_immediately in espace
1 parent 0dcec5e commit c5050bd

File tree

7 files changed

+65
-7
lines changed

7 files changed

+65
-7
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/client/src/common/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,7 @@ pub fn initialize_not_light_node_modules(
725725
consensus.clone(),
726726
sync.clone(),
727727
txpool.clone(),
728+
blockgen.clone(),
728729
notifications.clone(),
729730
task_executor.clone(),
730731
conf,

crates/client/src/rpc/mod.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Conflux is free software and distributed under GNU General Public License.
33
// See http://www.gnu.org/licenses/
44

5+
use blockgen::BlockGenerator;
56
use cfx_rpc_builder::{
67
RpcModuleBuilder, RpcServerConfig, RpcServerHandle,
78
TransportRpcModuleConfig,
@@ -406,8 +407,9 @@ where
406407
// start espace rpc server v2(async)
407408
pub async fn launch_async_rpc_servers(
408409
consensus: SharedConsensusGraph, sync: SharedSynchronizationService,
409-
tx_pool: SharedTransactionPool, notifications: Arc<Notifications>,
410-
executor: TaskExecutor, conf: &Configuration,
410+
tx_pool: SharedTransactionPool, block_gen: Arc<BlockGenerator>,
411+
notifications: Arc<Notifications>, executor: TaskExecutor,
412+
conf: &Configuration,
411413
) -> Result<Option<RpcServerHandle>, String> {
412414
let http_config = conf.eth_http_config();
413415
let ws_config = conf.eth_ws_config();
@@ -455,6 +457,7 @@ pub async fn launch_async_rpc_servers(
455457
consensus,
456458
sync,
457459
tx_pool,
460+
block_gen,
458461
executor,
459462
notifications,
460463
);

crates/rpc/rpc-builder/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,6 @@ log = { workspace = true }
3131
cfx-rpc-middlewares = { workspace = true }
3232
cfx-tasks = { workspace = true }
3333
tokio = { workspace = true }
34+
blockgen = { workspace = true }
3435

3536
[dev-dependencies]

crates/rpc/rpc-builder/src/lib.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ pub use id_provider::EthSubscriptionIdProvider;
3737
use log::debug;
3838
pub use module::{EthRpcModule, RpcModuleSelection};
3939

40+
use blockgen::BlockGenerator;
4041
use cfx_rpc::{helpers::ChainInfo, *};
4142
use cfx_rpc_cfx_types::RpcImplConfiguration;
4243
use cfx_rpc_eth_api::*;
@@ -75,6 +76,7 @@ pub struct RpcModuleBuilder {
7576
consensus: SharedConsensusGraph,
7677
sync: SharedSynchronizationService,
7778
tx_pool: SharedTransactionPool,
79+
block_gen: Arc<BlockGenerator>,
7880
executor: TaskExecutor,
7981
notifications: Arc<Notifications>,
8082
}
@@ -83,13 +85,15 @@ impl RpcModuleBuilder {
8385
pub fn new(
8486
config: RpcImplConfiguration, consensus: SharedConsensusGraph,
8587
sync: SharedSynchronizationService, tx_pool: SharedTransactionPool,
86-
executor: TaskExecutor, notifications: Arc<Notifications>,
88+
block_gen: Arc<BlockGenerator>, executor: TaskExecutor,
89+
notifications: Arc<Notifications>,
8790
) -> Self {
8891
Self {
8992
config,
9093
consensus,
9194
sync,
9295
tx_pool,
96+
block_gen,
9397
executor,
9498
notifications,
9599
}
@@ -111,6 +115,7 @@ impl RpcModuleBuilder {
111115
consensus,
112116
sync,
113117
tx_pool,
118+
block_gen,
114119
executor,
115120
notifications,
116121
} = self;
@@ -120,6 +125,7 @@ impl RpcModuleBuilder {
120125
consensus,
121126
sync,
122127
tx_pool,
128+
block_gen,
123129
executor,
124130
notifications,
125131
);
@@ -140,6 +146,7 @@ pub struct RpcRegistryInner {
140146
config: RpcImplConfiguration,
141147
sync: SharedSynchronizationService,
142148
tx_pool: SharedTransactionPool,
149+
block_gen: Arc<BlockGenerator>,
143150
modules: HashMap<EthRpcModule, Methods>,
144151
executor: TaskExecutor,
145152
notifications: Arc<Notifications>,
@@ -149,13 +156,15 @@ impl RpcRegistryInner {
149156
pub fn new(
150157
config: RpcImplConfiguration, consensus: SharedConsensusGraph,
151158
sync: SharedSynchronizationService, tx_pool: SharedTransactionPool,
152-
executor: TaskExecutor, notifications: Arc<Notifications>,
159+
block_gen: Arc<BlockGenerator>, executor: TaskExecutor,
160+
notifications: Arc<Notifications>,
153161
) -> Self {
154162
Self {
155163
consensus,
156164
config,
157165
sync,
158166
tx_pool,
167+
block_gen,
159168
modules: Default::default(),
160169
executor,
161170
notifications,
@@ -248,6 +257,7 @@ impl RpcRegistryInner {
248257
self.consensus.clone(),
249258
self.sync.clone(),
250259
self.tx_pool.clone(),
260+
self.block_gen.clone(),
251261
self.executor.clone(),
252262
)
253263
.into_rpc();
@@ -286,6 +296,7 @@ impl RpcRegistryInner {
286296
self.consensus.clone(),
287297
self.sync.clone(),
288298
self.tx_pool.clone(),
299+
self.block_gen.clone(),
289300
self.executor.clone(),
290301
);
291302
ParityApi::new(eth_api).into_rpc().into()

crates/rpc/rpc-eth-impl/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ cfx-types = { workspace = true }
1919
cfx-rpc-eth-types = { workspace = true }
2020
cfx-rpc-primitives = { workspace = true }
2121
cfx-rpc-cfx-impl = { workspace = true }
22+
blockgen = { workspace = true }
2223
async-trait = { workspace = true }
2324
keccak-hash = { workspace = true }
2425
alloy-rpc-types-trace = { workspace = true }
@@ -49,4 +50,4 @@ solidity-abi = { workspace = true }
4950
cfx-rpc-common-impl = { workspace = true }
5051
cfx-tasks = { workspace = true }
5152
cfx-parity-trace-types = { workspace = true }
52-
cfxcore-errors = { workspace = true }
53+
cfxcore-errors = { workspace = true }

crates/rpc/rpc-eth-impl/src/eth.rs

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::helpers::{FeeHistoryCache, MAX_FEE_HISTORY_CACHE_BLOCK_COUNT};
22
use async_trait::async_trait;
3+
use blockgen::BlockGenerator;
34
use cfx_execute_helper::estimation::EstimateRequest;
45
use cfx_executor::executive::{
56
Executed, ExecutionError, ExecutionOutcome, TxDropError,
@@ -32,6 +33,7 @@ use cfx_types::{
3233
use cfx_util_macros::bail;
3334
use cfx_vm_types::Error as VmError;
3435
use 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
};
4648
use rustc_hex::ToHex;
4749
use 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

5054
type BlockNumber = BlockId;
5155
type 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

Comments
 (0)