Skip to content

Commit

Permalink
fix: mining blocking until specified tx confirm
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangsoledad committed May 10, 2022
1 parent bef4e5f commit 1ca9a32
Show file tree
Hide file tree
Showing 12 changed files with 40 additions and 16 deletions.
2 changes: 1 addition & 1 deletion test/src/specs/dao/satoshi_dao_occupied.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ impl Spec for SpendSatoshiCell {
node0
.rpc_client()
.send_transaction(transaction.data().into());
node0.mine_until_transactions_confirm();
node0.mine_until_transaction_confirm(&tx_hash);
// cellbase occupied capacity minus satoshi cell
let cellbase_used_capacity = Capacity::bytes(CELLBASE_USED_BYTES).unwrap();
assert!(is_transaction_committed(node0, &transaction));
Expand Down
5 changes: 3 additions & 2 deletions test/src/specs/dao/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ use ckb_types::{core::TransactionView, packed::OutPoint};
/// Send the given transaction and make it committed
pub(crate) fn ensure_committed(node: &Node, transaction: &TransactionView) -> OutPoint {
let closest = node.consensus().tx_proposal_window().closest();
let tx_hash = transaction.hash();
node.rpc_client()
.send_transaction(transaction.data().into());
node.mine_until_transactions_confirm_with_windows(closest);
node.mine_until_transaction_confirm_with_windows(&tx_hash, closest);
assert!(is_transaction_committed(node, transaction));
OutPoint::new(transaction.hash(), 0)
OutPoint::new(tx_hash, 0)
}

/// A helper function keep the node growing until into the target EpochNumberWithFraction.
Expand Down
2 changes: 1 addition & 1 deletion test/src/specs/hardfork/v2021/cell_deps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ impl<'a> CheckCellDepsTestRunner<'a> {

fn submit_transaction_until_committed_to(node: &Node, tx: &TransactionView) {
node.submit_transaction(tx);
node.mine_until_transactions_confirm();
node.mine_until_transaction_confirm(&tx.hash());
}

fn submit_transaction_until_committed(&self, tx: &TransactionView) {
Expand Down
2 changes: 1 addition & 1 deletion test/src/specs/mining/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl Spec for MiningBasic {
let cells = gen_spendable(node, 1);
let transaction = always_success_transaction(node, &cells[0]);
node.submit_transaction(&transaction);
node.mine_until_transactions_confirm();
node.mine_until_transaction_confirm(&transaction.hash());

let block3 = node.get_tip_block();
assert_eq!(block3.get_commit_tx_ids(), transaction.get_commit_tx_ids());
Expand Down
3 changes: 2 additions & 1 deletion test/src/specs/mining/fee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ impl Spec for FeeOfTransaction {
let cells = gen_spendable(node, 1);
let transaction = always_success_transaction(node, &cells[0]);
node.submit_transaction(&transaction);
let tx_hash = transaction.hash();

let txs = vec![transaction];
let closest = DEFAULT_TX_PROPOSAL_WINDOW.0;
let number_to_propose = node.get_tip_block_number() + 1;
let number_to_commit = number_to_propose + closest;
node.mine_until_transactions_confirm();
node.mine_until_transaction_confirm(&tx_hash);
node.mine(2 * FINALIZATION_DELAY_LENGTH);

assert_eq!(
Expand Down
2 changes: 1 addition & 1 deletion test/src/specs/relay/get_block_proposal_process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl Spec for ProposalRespondSizelimit {

proposal_ids.push(transaction.proposal_short_id());

node0.mine_until_transactions_confirm();
node0.mine_until_transaction_confirm(&transaction.hash());

// spend all new cell
cells = get_spendable(node0);
Expand Down
6 changes: 3 additions & 3 deletions test/src/specs/rpc/transaction_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ impl Spec for RpcTransactionProof {
let node0 = &nodes[0];
node0.mine(DEFAULT_TX_PROPOSAL_WINDOW.1 + 2);

let tx_hash = node0.generate_transaction().unpack();
let tx_hashes = vec![tx_hash];
node0.mine_until_transactions_confirm();
let tx_hash = node0.generate_transaction();
node0.mine_until_transaction_confirm(&tx_hash);
let tx_hashes = vec![tx_hash.unpack()];
let proof = node0
.rpc_client()
.inner()
Expand Down
4 changes: 2 additions & 2 deletions test/src/specs/rpc/truncate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl Spec for RpcTruncate {
let to_truncate = node.get_block_by_number(truncate_number).hash();

node.submit_transaction(tx1);
node.mine_until_transactions_confirm();
node.mine_until_transaction_confirm(&tx1.hash());
node.submit_transaction(tx2);

// tx1 is already committed on chain, tx2 is still in tx-pool.
Expand Down Expand Up @@ -70,7 +70,7 @@ impl Spec for RpcTruncate {
// The chain can generate new blocks
node.mine(3);
node.submit_transaction(tx1);
node.mine_until_transactions_confirm();
node.mine_until_transaction_confirm(&tx1.hash());
let cell1 = node
.rpc_client()
.get_live_cell(tx1.inputs().get(0).unwrap().previous_output().into(), false);
Expand Down
2 changes: 1 addition & 1 deletion test/src/specs/tx_pool/collision.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ impl Spec for SubmitConflict {

let (txa, txb) = conflict_transactions(node);
node.submit_transaction(&txa);
node.mine_until_transactions_confirm();
node.mine_until_transaction_confirm(&txa.hash());
assert!(is_transaction_committed(node, &txa));
assert_send_transaction_fail(
node,
Expand Down
2 changes: 1 addition & 1 deletion test/src/specs/tx_pool/pool_reconcile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ impl Spec for PoolReconcile {
let hash = node0.generate_transaction();

info!("Generate 3 more blocks on node0");
node0.mine_until_transactions_confirm();
node0.mine_until_transaction_confirm(&hash);

info!("Pool should be empty");
assert!(node0
Expand Down
2 changes: 1 addition & 1 deletion test/src/specs/tx_pool/send_large_cycles_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl Spec for SendLargeCyclesTxInBlock {
ret.is_some() && matches!(ret.unwrap().tx_status.status, Status::Pending)
});
assert!(result, "large cycles tx rejected by node0");
node0.mine_until_transactions_confirm();
node0.mine_until_transaction_confirm(&tx.hash());
let block: BlockView = node0.get_tip_block();
assert_eq!(block.transactions()[1], tx);
node0.connect(node1);
Expand Down
24 changes: 23 additions & 1 deletion test/src/util/mining.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use crate::util::chain::forward_main_blocks;
use crate::Node;
use crate::DEFAULT_TX_PROPOSAL_WINDOW;
use ckb_jsonrpc_types::BlockTemplate;
use ckb_jsonrpc_types::{BlockTemplate, ProposalShortId};
use ckb_types::{
core::{BlockBuilder, BlockView, EpochNumberWithFraction, HeaderView},
packed,
prelude::*,
};
use std::{thread::sleep, time::Duration};

Expand Down Expand Up @@ -136,6 +137,27 @@ impl Node {
number
}

pub fn mine_until_transaction_confirm_with_windows(
&self,
tx_hash: &packed::Byte32,
closest: u64,
) {
let target: ProposalShortId = packed::ProposalShortId::from_tx_hash(tx_hash).into();
let last =
self.mine_with_blocking(|template| !template.proposals.iter().any(|id| id == &target));
self.mine_with_blocking(|template| template.number.value() != (last + closest - 1));
self.mine_with_blocking(|template| {
!template
.transactions
.iter()
.any(|tx| tx.hash == tx_hash.unpack())
});
}

pub fn mine_until_transaction_confirm(&self, tx_hash: &packed::Byte32) {
self.mine_until_transaction_confirm_with_windows(tx_hash, DEFAULT_TX_PROPOSAL_WINDOW.0)
}

pub fn mine_until_transactions_confirm_with_windows(&self, closest: u64) {
let last = self.mine_with_blocking(|template| template.proposals.is_empty());
self.mine_with_blocking(|template| template.number.value() != (last + closest - 1));
Expand Down

0 comments on commit 1ca9a32

Please sign in to comment.