Skip to content

Commit

Permalink
test: Add comments for cases
Browse files Browse the repository at this point in the history
  • Loading branch information
keroro520 committed Sep 29, 2019
1 parent a474b67 commit 6caf7d3
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 37 deletions.
51 changes: 24 additions & 27 deletions test/src/specs/mining/fee.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::utils::generate_utxo_set;
use crate::{Net, Node, Spec};
use ckb_types::core::{BlockNumber, BlockView, Capacity, TransactionView};
use ckb_types::core::{BlockView, Capacity, TransactionView};
use ckb_types::packed::{Byte32, OutPoint};
use ckb_types::prelude::Entity;
use ckb_types::prelude::*;
Expand Down Expand Up @@ -32,9 +32,11 @@ impl Spec for FeeOfTransaction {
let number_to_propose = number_to_submit + 1;
let number_to_commit = number_to_propose + closest;
node.generate_blocks(2 * finalization_delay_length as usize);
assert_block(node, number_to_propose, &txs, &[]);
assert_block(node, number_to_commit, &[], &txs);
assert_chain(node, &txs);
assert_proposals(&node.get_block_by_number(number_to_propose), &txs);
assert_committed(&node.get_block_by_number(number_to_commit), &txs);

assert_transactions_committed(node, &txs);
assert_chain_rewards(node);
}
}

Expand All @@ -61,8 +63,10 @@ impl Spec for FeeOfMaxBlockProposalsLimit {
let number_to_submit = node.get_tip_block_number();
let number_to_propose = number_to_submit + 1;
node.generate_blocks(2 * finalization_delay_length as usize);
assert_block(node, number_to_propose, &txs, &[]);
assert_chain(node, &txs);
assert_proposals(&node.get_block_by_number(number_to_propose), &txs);

assert_transactions_committed(node, &txs);
assert_chain_rewards(node);
}
}

Expand Down Expand Up @@ -98,7 +102,8 @@ impl Spec for FeeOfMultipleMaxBlockProposalsLimit {
});

node.generate_blocks(2 * finalization_delay_length as usize);
assert_chain(node, &txs);
assert_transactions_committed(node, &txs);
assert_chain_rewards(node);
}
}

Expand All @@ -114,7 +119,10 @@ impl Spec for ProposeButNotCommit {
let target_node = &net.nodes[0];
let feed_node = &net.nodes[1];

// Construct a chain which proposed the target transaction in the tip block
// We use `feed_node` to construct a chain proposed `txs` in the tip block.
//
// The returned `feed_blocks`, which represents the main fork of
// `feed_node`, only proposes `txs` in the last block and never commit
let feed_blocks: Vec<_> = {
let txs = generate_utxo_set(feed_node, 1)
.bang_random_fee(vec![feed_node.always_success_cell_dep()]);
Expand All @@ -126,14 +134,17 @@ impl Spec for ProposeButNotCommit {
.collect()
};

// `target_node` propose `tx`
feed_blocks.iter().for_each(|block| {
target_node.submit_block(&block.data());
});

// `target_node` keeps growing, but it will never commit `tx` since its transactions_pool
// have not `tx`.
let finalization_delay_length = feed_node.consensus().finalization_delay_length();
target_node.generate_blocks(2 * finalization_delay_length as usize);
assert_block(target_node, target_node.get_tip_block_number(), &[], &[]);
assert_chain(target_node, &[]);

assert_chain_rewards(target_node);
}
}

Expand Down Expand Up @@ -178,7 +189,9 @@ impl Spec for ProposeDuplicated {

let finalization_delay_length = node.consensus().finalization_delay_length();
node.generate_blocks(2 * finalization_delay_length as usize);
assert_chain(node, &txs);

assert_transactions_committed(node, &txs);
assert_chain_rewards(node);
}
}

Expand Down Expand Up @@ -233,22 +246,6 @@ fn assert_chain_rewards(node: &Node) {
}
}

fn assert_chain(node: &Node, transactions: &[TransactionView]) {
assert_transactions_committed(node, transactions);
assert_chain_rewards(node);
}

fn assert_block(
node: &Node,
block_number: BlockNumber,
proposals: &[TransactionView],
committed: &[TransactionView],
) {
let block = node.get_block_by_number(block_number);
assert_proposals(&block, proposals);
assert_committed(&block, committed);
}

fn assert_proposals(block: &BlockView, expected: &[TransactionView]) {
let mut actual_proposals: Vec<_> = block.union_proposal_ids_iter().collect();
let mut expected_proposals: Vec<_> = expected.iter().map(|tx| tx.proposal_short_id()).collect();
Expand Down
32 changes: 22 additions & 10 deletions test/src/specs/sync/chain_forks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -570,41 +570,53 @@ impl Spec for ForkedTransaction {
let fixed_point = node0.get_tip_block_number();
let tx = node1.new_transaction_spend_tip_cellbase();

// node0 doesn't have `tx` => TxStatus: None
// node1 have `tx` on main-fork => TxStatus: Some(Committed)
// `node0` doesn't have `tx` => TxStatus: None
{
node0.generate_blocks(1 + 2 * finalization_delay_length as usize);
node1.submit_transaction(&tx);
node1.generate_blocks(2 * finalization_delay_length as usize);

let tx_status = node0.rpc_client().get_transaction(tx.hash());
assert!(tx_status.is_none(), "node0 maintains tx in unverified fork");
}

// `node1` have `tx` on main-fork => TxStatus: Some(Committed)
{
node1.submit_transaction(&tx);
node1.generate_blocks(2 * finalization_delay_length as usize);
let tx_status = node1.rpc_client().get_transaction(tx.hash()).unwrap();
is_committed(&tx_status);
}

// node0 have `tx` on unverified-fork => TxStatus: None
// node1 have `tx` on verified-fork => TxStatus: Some(Pending)
// `node0` have `tx` on unverified-fork only => TxStatus: None
//
// We submit the main-fork of `node1` to `node0`, that will be persisted as an
// unverified-fork inside `node0`.
{
(fixed_point..=node1.get_tip_block_number()).for_each(|number| {
let block = node1.get_block_by_number(number);
node0.submit_block(&block.data());
});
let tx_status = node0.rpc_client().get_transaction(tx.hash());
assert!(tx_status.is_none(), "node0 maintains tx in unverified fork");
}

// node1 have `tx` on verified-fork => TxStatus: Some(Pending)
//
// We submit the main-fork of `node0` to `node1`, that will trigger switching forks. Then
// the original main-fork of `node0` will become side verified-fork. And `tx` will be moved
// to gap-transactions-pool during switching forks
{
(fixed_point..=node0.get_tip_block_number()).for_each(|number| {
let block = node0.get_block_by_number(number);
node1.submit_block(&block.data());
});

let tx_status = node0.rpc_client().get_transaction(tx.hash());
assert!(tx_status.is_none(), "node0 maintains tx in unverified fork");
let is_pending = |tx_status: &TransactionWithStatus| {
let pending_status = TxStatus::pending();
tx_status.tx_status.status == pending_status.status
};
let tx_status = node1.rpc_client().get_transaction(tx.hash()).unwrap();
assert!(
is_pending(&tx_status),
"node1 maintains tx in verified fork"
"node1 maintains tx in verified fork."
);
}
}
Expand Down

0 comments on commit 6caf7d3

Please sign in to comment.