Skip to content

Commit

Permalink
add unit test for links
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyukang committed Jan 24, 2024
1 parent a0290f5 commit 4f661a7
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 7 deletions.
6 changes: 5 additions & 1 deletion tx-pool/src/component/links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,17 @@ impl TxLinksMap {
relation_ids.insert(id);
}
// for direct parents, we don't store children in links map
// so filter those not in links map now, they maybe removed from tx-pool now
// so filter those not in links map, they maybe removed from tx-pool now
if relation == Relation::DirectParents {
relation_ids.retain(|id| self.inner.contains_key(id));
}
relation_ids
}

pub fn add_link(&mut self, short_id: ProposalShortId, links: TxLinks) {
self.inner.insert(short_id, links);
}

pub fn calc_ancestors(&self, short_id: &ProposalShortId) -> HashSet<ProposalShortId> {
self.calc_relative_ids(short_id, Relation::Parents)
}
Expand Down
14 changes: 8 additions & 6 deletions tx-pool/src/component/pool_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -511,12 +511,14 @@ impl PoolMap {
self.links.add_child(parent, short_id.clone());
}

let links = TxLinks {
parents,
direct_parents,
children: Default::default(),
};
self.links.inner.insert(short_id, links);
self.links.add_link(
short_id,
TxLinks {
parents,
direct_parents,
children: Default::default(),
},
);

Ok(true)
}
Expand Down
32 changes: 32 additions & 0 deletions tx-pool/src/component/tests/links.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use crate::component::links::{Relation, TxLinks, TxLinksMap};
use ckb_types::packed::ProposalShortId;
use ckb_types::prelude::Entity;
use std::collections::HashSet;

#[test]
fn test_link_map() {
let mut map = TxLinksMap::default();
let id1 = ProposalShortId::from_slice(&[1; 10]).unwrap();
let id2 = ProposalShortId::from_slice(&[2; 10]).unwrap();
let id3 = ProposalShortId::from_slice(&[3; 10]).unwrap();
let id4 = ProposalShortId::from_slice(&[4; 10]).unwrap();

map.add_link(id1.clone(), TxLinks::default());
map.add_link(id2.clone(), TxLinks::default());
map.add_link(id3.clone(), TxLinks::default());
map.add_link(id4.clone(), TxLinks::default());

map.add_parent(&id1, id2.clone());
let expect: HashSet<ProposalShortId> = vec![id2.clone()].into_iter().collect();
assert_eq!(map.get_parents(&id1).unwrap(), &expect);

map.add_direct_parent(&id1, id2.clone());
map.add_direct_parent(&id2, id3.clone());
map.add_direct_parent(&id3, id4.clone());
let direct_parents = map.calc_relation_ids([id1.clone()].into(), Relation::DirectParents);
assert_eq!(direct_parents.len(), 4);

map.remove(&id3);
let direct_parents = map.calc_relation_ids([id1.clone()].into(), Relation::DirectParents);
assert_eq!(direct_parents.len(), 2);
}
1 change: 1 addition & 0 deletions tx-pool/src/component/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod chunk;
mod entry;
mod links;
mod orphan;
mod pending;
mod proposed;
Expand Down

0 comments on commit 4f661a7

Please sign in to comment.