Skip to content

Commit a46c71a

Browse files
authored
Merge pull request #3256 from sky4009/dev
1. unit test code for deferred_pool 2. unit code code for solidity-abi basic
2 parents 7c5979d + ae0054a commit a46c71a

File tree

2 files changed

+402
-4
lines changed
  • crates
    • cfxcore/core/src/transaction_pool/deferred_pool
    • execution/solidity-abi/src

2 files changed

+402
-4
lines changed

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+
}

0 commit comments

Comments
 (0)