Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Txpool will evict tx when resolve error in block assembler #4220

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions test/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,7 @@ fn all_specs() -> Vec<Box<dyn Spec>> {
Box::new(RelayWithWrongTx::new()),
Box::new(TxsRelayOrder),
Box::new(SendTxChain),
Box::new(SendTxChainRevOrder),
Box::new(DifferentTxsWithSameInputWithOutRBF),
Box::new(RbfEnable),
Box::new(RbfBasic),
Expand Down
42 changes: 42 additions & 0 deletions test/src/specs/tx_pool/send_tx_chain.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use crate::specs::tx_pool::utils::prepare_tx_family;
use crate::utils::blank;
use crate::utils::propose;
use crate::{Node, Spec};
use ckb_jsonrpc_types::TxStatus;
use ckb_logger::info;
use ckb_types::core::FeeRate;
use ckb_types::{
Expand Down Expand Up @@ -93,3 +97,41 @@ impl Spec for SendTxChain {
config.tx_pool.max_ancestors_count = MAX_ANCESTORS_COUNT;
}
}

pub struct SendTxChainRevOrder;

impl Spec for SendTxChainRevOrder {
crate::setup!(num_nodes: 1);

// Case: Check txpool will evict tx when tx check failed during block_assembler
// avoid to stay for a long time in the pool
fn run(&self, nodes: &mut Vec<Node>) {
let node_a = &nodes[0];
let window = node_a.consensus().tx_proposal_window();

node_a.mine_until_out_bootstrap_period();
let family = prepare_tx_family(node_a);

node_a.submit_transaction(family.a());
node_a.submit_transaction(family.b());

let tx_pool_info = node_a.rpc_client().tx_pool_info();
assert_eq!(tx_pool_info.pending.value(), 2);

// send the child tx firstly
node_a.submit_block(&propose(node_a, &[family.b()]));

assert!(node_a.get_transaction(family.a().hash()) == TxStatus::pending());
assert!(node_a.get_transaction(family.b().hash()) == TxStatus::pending());
(0..window.closest()).for_each(|_| {
node_a.submit_block(&blank(node_a));
});

assert!(node_a.get_transaction(family.a().hash()) == TxStatus::pending());

// tx_b is removed by txpool, don't stay in the pool
assert!(node_a.get_transaction(family.b().hash()) == TxStatus::unknown());
let tx_pool_info = node_a.rpc_client().tx_pool_info();
assert_eq!(tx_pool_info.pending.value(), 1);
}
}
20 changes: 14 additions & 6 deletions tx-pool/src/block_assembler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ impl BlockAssembler {
let basic_block_size =
Self::basic_block_size(cellbase.data(), &[], iter::empty(), extension.clone());

let (dao, _checked_txs) =
let (dao, _checked_txs, _failed_txs) =
Self::calc_dao(&snapshot, &current_epoch, cellbase.clone(), vec![])
.expect("calc_dao for BlockAssembler initial");

Expand Down Expand Up @@ -197,12 +197,18 @@ impl BlockAssembler {
};

let proposals_size = proposals.len() * ProposalShortId::serialized_size();
let (dao, checked_txs) = Self::calc_dao(
let (dao, checked_txs, failed_txs) = Self::calc_dao(
&current.snapshot,
&current.epoch,
current_template.cellbase.clone(),
txs,
)?;
if !failed_txs.is_empty() {
let mut tx_pool_writer = tx_pool.write().await;
for id in failed_txs {
tx_pool_writer.remove_tx(&id);
}
}

let txs_size = checked_txs.iter().map(|tx| tx.size).sum();
let total_size = basic_size + txs_size;
Expand Down Expand Up @@ -251,7 +257,7 @@ impl BlockAssembler {
let basic_block_size =
Self::basic_block_size(cellbase.data(), &uncles, iter::empty(), extension.clone());

let (dao, _checked_txs) =
let (dao, _checked_txs, _failed_txs) =
Self::calc_dao(&snapshot, &current_epoch, cellbase.clone(), vec![])?;

builder
Expand Down Expand Up @@ -407,7 +413,7 @@ impl BlockAssembler {
txs
};

if let Ok((dao, checked_txs)) = Self::calc_dao(
if let Ok((dao, checked_txs, _failed_txs)) = Self::calc_dao(
&current.snapshot,
&current.epoch,
current_template.cellbase.clone(),
Expand Down Expand Up @@ -577,12 +583,13 @@ impl BlockAssembler {
current_epoch: &EpochExt,
cellbase: TransactionView,
entries: Vec<TxEntry>,
) -> Result<(Byte32, Vec<TxEntry>), AnyError> {
) -> Result<(Byte32, Vec<TxEntry>, Vec<ProposalShortId>), AnyError> {
let tip_header = snapshot.tip_header();
let consensus = snapshot.consensus();
let mut seen_inputs = HashSet::new();
let mut transactions_checker = TransactionsChecker::new(iter::once(&cellbase));

let mut checked_failed_txs = vec![];
let checked_entries: Vec<_> = block_in_place(|| {
entries
.into_iter()
Expand All @@ -602,6 +609,7 @@ impl BlockAssembler {
entry.transaction().hash(),
err
);
checked_failed_txs.push(entry.proposal_short_id());
None
} else {
transactions_checker.insert(entry.transaction());
Expand All @@ -620,7 +628,7 @@ impl BlockAssembler {
let dao = DaoCalculator::new(consensus, &snapshot.borrow_as_data_loader())
.dao_field_with_current_epoch(entries_iter, tip_header, current_epoch)?;

Ok((dao, checked_entries))
Ok((dao, checked_entries, checked_failed_txs))
}

pub(crate) async fn notify(&self) {
Expand Down
3 changes: 1 addition & 2 deletions tx-pool/src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,7 @@ impl TxPoolService {
) -> (Result<(), Reject>, Arc<Snapshot>) {
let (ret, snapshot) = self
.with_tx_pool_write_lock(move |tx_pool, snapshot| {
// if snapshot changed by context switch
// we need redo time_relative verify
// if snapshot changed by context switch we need redo time_relative verify
let tip_hash = snapshot.tip_hash();
if pre_resolve_tip != tip_hash {
debug!(
Expand Down
Loading