Skip to content

Commit

Permalink
fix: proposed pool remove committed
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangsoledad committed Jan 10, 2022
1 parent d57ab2d commit aa4e359
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 31 deletions.
6 changes: 4 additions & 2 deletions test/src/specs/tx_pool/orphan_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use ckb_jsonrpc_types::Status;
use ckb_network::SupportProtocols;

const ALWAYS_SUCCESS_SCRIPT_CYCLE: u64 = 537;
// always_failure, as the name implies, so it doesn't matter what the cycles are
const ALWAYS_FAILURE_SCRIPT_CYCLE: u64 = 1000;

pub struct OrphanTxAccepted;

Expand Down Expand Up @@ -67,7 +69,7 @@ impl Spec for OrphanTxRejected {
let child_tx = node0.new_always_failure_transaction(parent_tx.hash());
let child_hash = child_tx.hash();

relay_tx(&net, node0, child_tx, ALWAYS_SUCCESS_SCRIPT_CYCLE);
relay_tx(&net, node0, child_tx, ALWAYS_FAILURE_SCRIPT_CYCLE);
let result = wait_until(5, || {
let tx_pool_info = node0.get_tip_tx_pool_info();
tx_pool_info.orphan.value() == 1 && tx_pool_info.pending.value() == 0
Expand All @@ -77,7 +79,7 @@ impl Spec for OrphanTxRejected {
"Send child tx first, it will be added to orphan tx pool"
);

relay_tx(&net, node0, parent_tx, ALWAYS_SUCCESS_SCRIPT_CYCLE);
relay_tx(&net, node0, parent_tx, ALWAYS_FAILURE_SCRIPT_CYCLE);
let result = wait_until(5, || {
let tx_pool_info = node0.get_tip_tx_pool_info();
tx_pool_info.orphan.value() == 0 && tx_pool_info.pending.value() == 1
Expand Down
47 changes: 22 additions & 25 deletions tx-pool/src/component/proposed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl Edges {
self.inputs.get(out_point)
}

pub(crate) fn get_output_mut_ref(
pub(crate) fn get_mut_output(
&mut self,
out_point: &OutPoint,
) -> Option<&mut Option<ProposalShortId>> {
Expand Down Expand Up @@ -110,19 +110,19 @@ impl CellProvider for ProposedPool {
if let Some(x) = self.edges.get_output_ref(out_point) {
// output consumed
if x.is_some() {
CellStatus::Dead
return CellStatus::Dead;
} else {
let (output, data) = self.get_output_with_data(out_point).expect("output");
let cell_meta = CellMetaBuilder::from_cell_output(output, data)
.out_point(out_point.to_owned())
.build();
CellStatus::live_cell(cell_meta)
return CellStatus::live_cell(cell_meta);
}
} else if self.edges.get_input_ref(out_point).is_some() {
CellStatus::Dead
} else {
CellStatus::Unknown
}
if self.edges.get_input_ref(out_point).is_some() {
return CellStatus::Dead;
}
CellStatus::Unknown
}
}

Expand All @@ -131,15 +131,15 @@ impl CellChecker for ProposedPool {
if let Some(x) = self.edges.get_output_ref(out_point) {
// output consumed
if x.is_some() {
Some(false)
return Some(false);
} else {
Some(true)
return Some(true);
}
} else if self.edges.get_input_ref(out_point).is_some() {
Some(false)
} else {
None
}
if self.edges.get_input_ref(out_point).is_some() {
return Some(false);
}
None
}
}

Expand Down Expand Up @@ -189,8 +189,9 @@ impl ProposedPool {
let inputs = tx.input_pts_iter();
let outputs = tx.output_pts();
for i in inputs {
if self.edges.outputs.remove(&i).is_none() {
self.edges.inputs.remove(&i);
self.edges.inputs.remove(&i);
if let Some(id) = self.edges.get_mut_output(&i) {
*id = None;
}
}

Expand All @@ -200,7 +201,6 @@ impl ProposedPool {

for o in outputs {
self.edges.remove_output(&o);
// self.edges.remove_deps(&o);
}

self.edges.header_deps.remove(&entry.proposal_short_id());
Expand All @@ -215,17 +215,15 @@ impl ProposedPool {

if let Some(entry) = self.inner.remove_entry(&id) {
for o in outputs {
// notice: cause tx removed by committed,
// remove output, but if this output consumed by other in-pool tx,
// we need record it to inputs' map
if let Some(cid) = self.edges.remove_output(&o) {
self.edges.insert_input(o.clone(), cid);
}
self.edges.remove_output(&o);
}

for i in inputs {
// release input record
self.edges.remove_input(&i);
if let Some(id) = self.edges.get_mut_output(&i) {
*id = None;
}
}

for d in entry.related_dep_out_points().cloned() {
Expand All @@ -252,11 +250,10 @@ impl ProposedPool {
// if input reference a in-pool output, connect it
// otherwise, record input for conflict check
for i in inputs {
if let Some(id) = self.edges.get_output_mut_ref(&i) {
if let Some(id) = self.edges.get_mut_output(&i) {
*id = Some(tx_short_id.clone());
} else {
self.edges.insert_input(i.to_owned(), tx_short_id.clone());
}
self.edges.insert_input(i.to_owned(), tx_short_id.clone());
}

// record dep-txid
Expand Down
8 changes: 4 additions & 4 deletions tx-pool/src/component/tests/proposed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ fn test_add_entry() {

assert_eq!(pool.size(), 2);
assert_eq!(pool.edges.outputs_len(), 2);
assert_eq!(pool.edges.inputs_len(), 2);
assert_eq!(pool.edges.inputs_len(), 3);

pool.remove_committed_tx(&tx1);
assert_eq!(pool.edges.outputs_len(), 1);
Expand Down Expand Up @@ -161,12 +161,12 @@ fn test_add_no_roots() {
.unwrap();

assert_eq!(pool.edges.outputs_len(), 13);
assert_eq!(pool.edges.inputs_len(), 2);
assert_eq!(pool.edges.inputs_len(), 7);

pool.remove_committed_tx(&tx1);

assert_eq!(pool.edges.outputs_len(), 10);
assert_eq!(pool.edges.inputs_len(), 4);
assert_eq!(pool.edges.inputs_len(), 6);
}

#[test]
Expand Down Expand Up @@ -521,7 +521,7 @@ fn test_disordered_remove_committed_tx() {
pool.add_entry(entry2).unwrap();

assert_eq!(pool.edges.outputs_len(), 2);
assert_eq!(pool.edges.inputs_len(), 1);
assert_eq!(pool.edges.inputs_len(), 2);

pool.remove_committed_tx(&tx2);
pool.remove_committed_tx(&tx1);
Expand Down

0 comments on commit aa4e359

Please sign in to comment.