Skip to content

Commit fbc1baa

Browse files
authored
Merge branch 'master' into feat/removeLegacyERpc
2 parents 2ae3d35 + 17d6218 commit fbc1baa

File tree

8 files changed

+421
-7
lines changed

8 files changed

+421
-7
lines changed

Cargo.lock

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

bins/conflux/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ parity-version = { workspace = true }
4040
tokio = { workspace = true, features = ["rt"] }
4141
bls-signatures = { workspace = true }
4242
cfx-executor = { workspace = true }
43+
cfx-execute-helper = { workspace = true }
4344

4445
[target.'cfg(not(target_env = "msvc"))'.dependencies.jemallocator]
4546
version = "0.3.2"
@@ -54,7 +55,7 @@ u64-mpt-db-key = ["client/u64_mpt_db_key"]
5455
# it will be enabled across all paths depending on that package.
5556
# (https://doc.rust-lang.org/cargo/reference/features.html#feature-unification)
5657
blst-portable = ["bls-signatures/blst-portable"]
57-
align_evm = ["cfx-executor/align_evm"]
58+
align_evm = ["cfx-executor/align_evm", "cfx-execute-helper/align_evm"]
5859

5960
[dev-dependencies]
6061
tokio = { workspace = true, features = ["full"] }

crates/cfx_types/src/space.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ impl FromStr for Space {
4848
match s {
4949
"native" => Ok(Space::Native),
5050
"evm" => Ok(Space::Ethereum),
51-
_ => Err("Invalid space str".to_owned()),
51+
_ => Err(format!("Unrecognized space: {}", s)),
5252
}
5353
}
5454
}

crates/cfxcore/core/src/consensus/consensus_inner/consensus_executor/mod.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,13 @@ impl ConsensusExecutor {
505505
// the lock, there might be a checkpoint coming in to break
506506
// index
507507
for state_block_hash in waiting_blocks {
508-
self.wait_for_result(state_block_hash)?;
508+
let commitment = self.wait_for_result(state_block_hash)?;
509+
self.handler.data_man.insert_epoch_execution_commitment(
510+
state_block_hash,
511+
commitment.state_root_with_aux_info,
512+
commitment.receipts_root,
513+
commitment.logs_bloom_hash,
514+
);
509515
}
510516
// Now we need to wait for the execution information of all missing
511517
// blocks to come back

crates/cfxcore/core/src/transaction_pool/deferred_pool/tests.rs

Lines changed: 306 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
11
use crate::transaction_pool::TransactionPoolError;
22

33
use super::{DeferredPool, InsertResult, TxWithReadyInfo};
4-
use crate::keylib::{Generator, KeyPair, Random};
5-
use cfx_types::{Address, AddressSpaceUtil, Space, U256};
4+
use crate::{
5+
keylib::{Generator, KeyPair, Random},
6+
verification::PackingCheckResult,
7+
};
8+
use cfx_types::{Address, AddressSpaceUtil, AddressWithSpace, Space, U256};
9+
use cfxkey::Secret;
610
use primitives::{
7-
transaction::{native_transaction::NativeTransaction, Eip155Transaction},
11+
transaction::{
12+
native_transaction::NativeTransaction, Eip155Transaction,
13+
TypedNativeTransaction,
14+
},
815
Action, SignedTransaction, Transaction,
916
};
10-
use std::sync::Arc;
17+
use std::{str::FromStr, sync::Arc};
18+
19+
const PRIVATE_KEY: &str =
20+
"74806d258099decd5f5bd500f5b318aaaa0a8a289f8dcb10a9609966d8a0e442";
1121

1222
fn new_test_tx(
1323
sender: &KeyPair, nonce: usize, gas_price: usize, gas: usize, value: usize,
@@ -267,3 +277,295 @@ fn test_deferred_pool_recalculate_readiness() {
267277
None
268278
);
269279
}
280+
281+
pub fn const_account_with_native_space() -> AddressWithSpace {
282+
let secret = Secret::from_str(&PRIVATE_KEY).unwrap();
283+
let key_pair = KeyPair::from_secret(secret).unwrap();
284+
// return a Native Space address
285+
key_pair.address().with_native_space()
286+
}
287+
288+
pub fn create_signed_transaction(
289+
nonce: U256, gas_limit: U256, gas_price: u64, sender: AddressWithSpace,
290+
private_key: &str,
291+
) -> Result<SignedTransaction, Box<dyn std::error::Error>> {
292+
let secret = Secret::from_str(&private_key).unwrap();
293+
294+
let receiver = &sender;
295+
// create a tx
296+
let typetx = TypedNativeTransaction::Cip155(NativeTransaction {
297+
nonce,
298+
gas_price: U256::from(gas_price),
299+
gas: U256::from(gas_limit),
300+
action: Action::Call(receiver.address),
301+
value: U256::from(1_000_000u64),
302+
storage_limit: 0_u64,
303+
epoch_height: 0,
304+
chain_id: 1, // Testnet chain ID
305+
data: vec![],
306+
});
307+
308+
let transaction = Transaction::Native(typetx);
309+
// sign the tx
310+
let signed_tx = transaction.sign(&secret);
311+
312+
Ok(signed_tx)
313+
}
314+
315+
fn create_tx_with_ready_info(
316+
nonce: U256, gas_limit: U256, gas_price: u64, sender: AddressWithSpace,
317+
private_key: &str,
318+
) -> TxWithReadyInfo {
319+
// 1.create signed_tx
320+
let signed_tx = create_signed_transaction(
321+
nonce,
322+
gas_limit,
323+
gas_price,
324+
sender,
325+
private_key,
326+
)
327+
.unwrap();
328+
329+
// 2. create TxWithReadyInfo
330+
let transaction = Arc::new(signed_tx);
331+
let packed = false;
332+
let sponsored_gas = U256::from(0);
333+
let sponsored_storage = 0_u64;
334+
let tx_with_ready_info = TxWithReadyInfo::new(
335+
transaction,
336+
packed,
337+
sponsored_gas,
338+
sponsored_storage,
339+
);
340+
tx_with_ready_info
341+
}
342+
343+
#[test]
344+
fn test_packing_sampler_valid_transaction() {
345+
let mut dpool = DeferredPool::new_for_test();
346+
let addr = const_account_with_native_space();
347+
let tx1 = create_tx_with_ready_info(
348+
U256::from(0),
349+
U256::from(21000),
350+
10000,
351+
addr,
352+
PRIVATE_KEY,
353+
);
354+
let tx_clone = tx1.clone();
355+
dpool.insert(tx1, false);
356+
357+
dpool.recalculate_readiness_with_local_info(
358+
&addr,
359+
U256::from(0),
360+
U256::from(1_000_000_000_000_000u64),
361+
);
362+
assert!(dpool.has_ready_tx(&addr));
363+
let validity = |_: &SignedTransaction| PackingCheckResult::Pack;
364+
let (txs, gas_used, size_used) = dpool.packing_sampler(
365+
Space::Native,
366+
U256::from(15000000),
367+
40000,
368+
10,
369+
U256::from(20),
370+
validity,
371+
);
372+
373+
assert_eq!(txs.len(), 1);
374+
assert_eq!(gas_used, U256::from(21000));
375+
assert!(size_used <= 15000000);
376+
assert_eq!(txs[0], tx_clone.transaction);
377+
}
378+
379+
#[test]
380+
fn test_insert_new_transaction() {
381+
let mut dpool = DeferredPool::new_for_test();
382+
let addr = const_account_with_native_space();
383+
let tx = create_tx_with_ready_info(
384+
U256::from(0),
385+
U256::from(21000),
386+
100,
387+
addr,
388+
PRIVATE_KEY,
389+
);
390+
391+
let result = dpool.insert(tx, false);
392+
assert!(matches!(result, InsertResult::NewAdded));
393+
assert!(dpool.contain_address(&addr));
394+
assert!(dpool.check_sender_and_nonce_exists(&addr, &U256::from(0)));
395+
}
396+
397+
#[test]
398+
fn test_insert_replace_transaction() {
399+
let mut dpool = DeferredPool::new_for_test();
400+
let addr = const_account_with_native_space();
401+
let tx1 = create_tx_with_ready_info(
402+
U256::from(0),
403+
U256::from(21000),
404+
100,
405+
addr,
406+
PRIVATE_KEY,
407+
);
408+
let tx2 = create_tx_with_ready_info(
409+
U256::from(0),
410+
U256::from(21000),
411+
200,
412+
addr,
413+
PRIVATE_KEY,
414+
); // Higher gas price
415+
416+
dpool.insert(tx1, false);
417+
let result = dpool.insert(tx2, false);
418+
assert!(matches!(result, InsertResult::Updated(_)));
419+
assert_eq!(dpool.count_less(&addr, &U256::from(1)), 1);
420+
}
421+
422+
#[test]
423+
fn test_packing_sampler_empty_limits() {
424+
let mut dpool = DeferredPool::new_for_test();
425+
let validity = |_: &SignedTransaction| PackingCheckResult::Pack;
426+
let (txs, gas_used, size_used) = dpool.packing_sampler(
427+
Space::Native,
428+
U256::from(0),
429+
0,
430+
0,
431+
U256::from(50),
432+
validity,
433+
);
434+
435+
assert!(txs.is_empty());
436+
assert_eq!(gas_used, U256::from(0));
437+
assert_eq!(size_used, 0);
438+
}
439+
440+
#[test]
441+
fn test_estimate_packing_gas_limit() {
442+
let dpool = DeferredPool::new_for_test();
443+
let (gas_limit, price_limit) = dpool.estimate_packing_gas_limit(
444+
Space::Native,
445+
U256::from(100_000),
446+
U256::from(100),
447+
U256::from(50),
448+
);
449+
450+
assert!(gas_limit <= U256::from(200_000)); // gas_target * 2
451+
assert!(price_limit >= U256::from(50)); // At least min_base_price
452+
}
453+
454+
#[test]
455+
fn test_mark_packed() {
456+
let mut dpool = DeferredPool::new_for_test();
457+
let addr = const_account_with_native_space();
458+
let tx = create_tx_with_ready_info(
459+
U256::from(0),
460+
U256::from(21000),
461+
100,
462+
addr,
463+
PRIVATE_KEY,
464+
);
465+
466+
dpool.insert(tx, false);
467+
let result = dpool.mark_packed(addr, &U256::from(0), true);
468+
assert!(result);
469+
470+
let result = dpool.check_tx_packed(addr, U256::from(0));
471+
assert!(result);
472+
}
473+
474+
#[test]
475+
fn test_remove_lowest_nonce() {
476+
let mut dpool = DeferredPool::new_for_test();
477+
let addr = const_account_with_native_space();
478+
let tx1 = create_tx_with_ready_info(
479+
U256::from(0),
480+
U256::from(21000),
481+
100,
482+
addr,
483+
PRIVATE_KEY,
484+
);
485+
let tx2 = create_tx_with_ready_info(
486+
U256::from(1),
487+
U256::from(21000),
488+
100,
489+
addr,
490+
PRIVATE_KEY,
491+
);
492+
493+
dpool.insert(tx1.clone(), false);
494+
dpool.insert(tx2, false);
495+
496+
let removed = dpool.remove_lowest_nonce(&addr).unwrap();
497+
assert_eq!(removed.nonce(), &U256::from(0));
498+
assert_eq!(dpool.count_less(&addr, &U256::from(2)), 1);
499+
}
500+
501+
#[test]
502+
fn test_recalculate_readiness_with_local_info() {
503+
let mut dpool = DeferredPool::new_for_test();
504+
let addr = const_account_with_native_space();
505+
let tx1 = create_tx_with_ready_info(
506+
U256::from(0),
507+
U256::from(21000),
508+
100,
509+
addr,
510+
PRIVATE_KEY,
511+
);
512+
let tx2 = create_tx_with_ready_info(
513+
U256::from(1),
514+
U256::from(21000),
515+
100,
516+
addr,
517+
PRIVATE_KEY,
518+
);
519+
520+
dpool.insert(tx1, false);
521+
dpool.insert(tx2, false);
522+
523+
let result = dpool.recalculate_readiness_with_local_info(
524+
&addr,
525+
U256::from(0),
526+
U256::from(1_000_000_000_000_000u64),
527+
);
528+
assert!(result.is_some());
529+
assert!(dpool.has_ready_tx(&addr));
530+
}
531+
532+
#[test]
533+
fn test_get_bucket() {
534+
let mut dpool = DeferredPool::new_for_test();
535+
let addr = const_account_with_native_space();
536+
let tx = create_tx_with_ready_info(
537+
U256::from(0),
538+
U256::from(21000),
539+
100,
540+
addr,
541+
PRIVATE_KEY,
542+
);
543+
dpool.insert(tx, false);
544+
let result = dpool.get_bucket(&addr).unwrap();
545+
let gas_limit = result.get_lowest_nonce_tx().unwrap().gas_limit();
546+
assert_eq!(*gas_limit, U256::from(21000));
547+
}
548+
549+
#[test]
550+
fn test_clear_bucket() {
551+
let mut dpool = DeferredPool::new_for_test();
552+
let addr = const_account_with_native_space();
553+
let tx = create_tx_with_ready_info(
554+
U256::from(0),
555+
U256::from(21000),
556+
100,
557+
addr,
558+
PRIVATE_KEY,
559+
);
560+
dpool.insert(tx, false);
561+
dpool.recalculate_readiness_with_local_info(
562+
&addr,
563+
U256::from(0),
564+
U256::from(1_000_000_000_000_000u64),
565+
);
566+
assert!(!dpool.buckets.is_empty());
567+
assert!(dpool.packing_pool.in_space(Space::Native).len() == 1);
568+
dpool.clear();
569+
assert!(dpool.buckets.is_empty());
570+
assert!(dpool.packing_pool.in_space(Space::Native).len() == 0);
571+
}

crates/execution/execute-helper/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,6 @@ typemap = { workspace = true }
2121
alloy-rpc-types-trace = { workspace = true }
2222
geth-tracer = { workspace = true }
2323
cfx-parity-trace-types = { workspace = true }
24+
25+
[features]
26+
align_evm = ["cfx-vm-types/align_evm", "cfx-executor/align_evm"]

crates/execution/execute-helper/src/estimation.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ impl<'a> EstimationContext<'a> {
133133
pub fn transact_virtual(
134134
&mut self, mut tx: SignedTransaction, request: EstimateRequest,
135135
) -> DbResult<(ExecutionOutcome, EstimateExt)> {
136+
#[cfg(not(feature = "align_evm"))]
136137
if let Some(outcome) = self.check_cip130(&tx, &request) {
137138
return Ok(outcome);
138139
}
@@ -400,7 +401,11 @@ impl<'a> EstimationContext<'a> {
400401
}
401402

402403
fn estimated_gas_limit(executed: &Executed, tx: &SignedTransaction) -> U256 {
404+
#[cfg(not(feature = "align_evm"))]
403405
let cip130_min_gas_limit = U256::from(tx.data().len() * 100);
406+
#[cfg(feature = "align_evm")]
407+
let cip130_min_gas_limit = U256::zero();
408+
404409
let eip7623_gas_limit = 21000
405410
+ tx.data()
406411
.iter()

0 commit comments

Comments
 (0)