Skip to content

Commit

Permalink
test: add reorg tests fn extend
Browse files Browse the repository at this point in the history
  • Loading branch information
rustaceanrob committed Jun 4, 2024
1 parent 24df264 commit fce625e
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 4 deletions.
108 changes: 106 additions & 2 deletions src/chain/header_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ impl HeaderChain {
.expect("cannot extend from empty batch")
.prev_blockhash)
{
reorged.push(DisconnectedHeader::new(*height, header.block_hash()));
reorged.push(DisconnectedHeader::new(*height, *header));
} else {
break;
}
Expand Down Expand Up @@ -299,5 +299,109 @@ mod tests {
}

#[test]
fn test_reorganized_chain() {}
fn test_depth_one_reorg() {
let block_8: Header = deserialize(&hex::decode("0000002016fe292517eecbbd63227d126a6b1db30ebc5262c61f8f3a4a529206388fc262dfd043cef8454f71f30b5bbb9eb1a4c9aea87390f429721e435cf3f8aa6e2a9171375166ffff7f2000000000").unwrap()).unwrap();
let block_9: Header = deserialize(&hex::decode("000000205708a90197d93475975545816b2229401ccff7567cb23900f14f2bd46732c605fd8de19615a1d687e89db365503cdf58cb649b8e935a1d3518fa79b0d408704e71375166ffff7f2000000000").unwrap()).unwrap();
let block_10: Header = deserialize(&hex::decode("000000201d062f2162835787db536c55317e08df17c58078c7610328bdced198574093790c9f554a7780a6043a19619d2a4697364bb62abf6336c0568c31f1eedca3c3e171375166ffff7f2000000000").unwrap()).unwrap();
let batch_1 = vec![block_8, block_9, block_10];
let new_block_10: Header = deserialize(&hex::decode("000000201d062f2162835787db536c55317e08df17c58078c7610328bdced198574093792151c0e9ce4e4c789ca98427d7740cc7acf30d2ca0c08baef266bf152289d814567e5e66ffff7f2001000000").unwrap()).unwrap();
let block_11: Header = deserialize(&hex::decode("00000020efcf8b12221fccc735b9b0b657ce15b31b9c50aff530ce96a5b4cfe02d8c0068496c1b8a89cf5dec22e46c35ea1035f80f5b666a1b3aa7f3d6f0880d0061adcc567e5e66ffff7f2001000000").unwrap()).unwrap();
let fork = vec![new_block_10, block_11];
let mut chain = HeaderChain::new(
HeaderCheckpoint::new(
7,
BlockHash::from_str(
"62c28f380692524a3a8f1fc66252bc0eb31d6b6a127d2263bdcbee172529fe16",
)
.unwrap(),
),
BTreeMap::new(),
);
let reorg = chain.extend(&batch_1);
assert!(reorg.is_empty());
assert_eq!(chain.height(), 10);
assert_eq!(
chain.chainwork_after_height(7),
block_8.work() + block_9.work() + block_10.work()
);
assert_eq!(
chain.chainwork_after_height(8),
block_9.work() + block_10.work()
);
let reorg = chain.extend(&fork);
assert_eq!(reorg.len(), 1);
assert_eq!(reorg.first().unwrap().header, block_10);
assert_eq!(
vec![block_8, block_9, new_block_10, block_11],
chain.values()
);
assert_eq!(chain.header_at_height(12), None);
assert_eq!(chain.header_at_height(10), Some(&new_block_10));
}

#[test]
fn test_depth_two_reorg() {
let block_1: Header = deserialize(&hex::decode("0000002006226e46111a0b59caaf126043eb5bbf28c34f3a5e332a1fc7b2b73cf188910f047eb4d0fe76345e307d0e020a079cedfa37101ee7ac84575cf829a611b0f84bc4805e66ffff7f2001000000").unwrap()).unwrap();
let block_2: Header = deserialize(&hex::decode("00000020299e41732deb76d869fcdb5f72518d3784e99482f572afb73068d52134f1f75e1f20f5da8d18661d0f13aa3db8fff0f53598f7d61f56988a6d66573394b2c6ffc5805e66ffff7f2001000000").unwrap()).unwrap();
let block_3: Header = deserialize(&hex::decode("00000020b96feaa82716f11befeb608724acee4743e0920639a70f35f1637a88b8b6ea3471f1dbedc283ce6a43a87ed3c8e6326dae8d3dbacce1b2daba08e508054ffdb697815e66ffff7f2001000000").unwrap()).unwrap();
let block_4: Header = deserialize(&hex::decode("0000002052ff614fa461ff38b4a5c101d04fdcac2f34722e60bd43d12c8de0a394fe0c60444fb24b7e9885f60fed9927d27f23854ecfab29287163ef2b868d5d626f82ed97815e66ffff7f2002000000").unwrap()).unwrap();

let batch_1 = vec![block_1, block_2, block_3, block_4];
let new_block_3: Header = deserialize(&hex::decode("00000020b96feaa82716f11befeb608724acee4743e0920639a70f35f1637a88b8b6ea349c6240c5d0521966771808950f796c9c04088bc9551a828b64f1cf06831705dfbc835e66ffff7f2000000000").unwrap()).unwrap();
let new_block_4: Header = deserialize(&hex::decode("00000020d2a1c6ba2be393f405fe2f4574565f9ee38ac68d264872fcd82b030970d0232ce882eb47c3dd138587120f1ad97dd0e73d1e30b79559ad516cb131f83dcb87e9bc835e66ffff7f2002000000").unwrap()).unwrap();
let fork = vec![new_block_3, new_block_4];
let mut chain = HeaderChain::new(
HeaderCheckpoint::new(
0,
BlockHash::from_str(
"0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206",
)
.unwrap(),
),
BTreeMap::new(),
);
chain.extend(&batch_1);
let reorged = chain.extend(&fork);
assert_eq!(reorged.len(), 2);
assert_eq!(
vec![block_1, block_2, new_block_3, new_block_4],
chain.values()
);
}

#[tokio::test]
async fn reorg_is_stump() {
let block_1: Header = deserialize(&hex::decode("0000002006226e46111a0b59caaf126043eb5bbf28c34f3a5e332a1fc7b2b73cf188910f575b313ad3ef825cfc204c34da8f3c1fd1784e2553accfa38001010587cb57241f855e66ffff7f2000000000").unwrap()).unwrap();
let block_2: Header = deserialize(&hex::decode("00000020c81cedd6a989939936f31448e49d010a13c2e750acf02d3fa73c9c7ecfb9476e798da2e5565335929ad303fc746acabc812ee8b06139bcf2a4c0eb533c21b8c420855e66ffff7f2000000000").unwrap()).unwrap();
let batch_1 = vec![block_1, block_2];
let new_block_1: Header = deserialize(&hex::decode("0000002006226e46111a0b59caaf126043eb5bbf28c34f3a5e332a1fc7b2b73cf188910f575b313ad3ef825cfc204c34da8f3c1fd1784e2553accfa38001010587cb5724d5855e66ffff7f2004000000").unwrap()).unwrap();
let new_block_2: Header = deserialize(&hex::decode("00000020d1d80f53343a084bd0da6d6ab846f9fe4a133de051ea00e7cae16ed19f601065798da2e5565335929ad303fc746acabc812ee8b06139bcf2a4c0eb533c21b8c4d6855e66ffff7f2000000000").unwrap()).unwrap();
let fork = vec![new_block_1, new_block_2];
let block_3: Header = deserialize(&hex::decode("0000002080f38c14e898d6646dd426428472888966e0d279d86453f42edc56fdb143241aa66c8fa8837d95be3f85d53f22e86a0d6d456b1ab348e073da4d42a39f50637423865e66ffff7f2000000000").unwrap()).unwrap();
let block_4: Header = deserialize(&hex::decode("000000204877fed370af64c0a1f7a76f6944e1127aad965b1865f99ecfdf8fa72ae23377f51921d01ff1131bd589500a8ca142884297ceeb1aa762ad727249e9a23f2cb023865e66ffff7f2000000000").unwrap()).unwrap();
let batch_2 = vec![block_3, block_4];
let mut chain = HeaderChain::new(
HeaderCheckpoint::new(
0,
BlockHash::from_str(
"0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206",
)
.unwrap(),
),
BTreeMap::new(),
);
chain.extend(&batch_1);
let reorged = chain.extend(&fork);
assert_eq!(reorged.len(), 2);
assert_eq!(vec![new_block_1, new_block_2], chain.values());
let no_org = chain.extend(&batch_2);
assert_eq!(no_org.len(), 0);
assert_eq!(
vec![new_block_1, new_block_2, block_3, block_4],
chain.values()
);
assert_eq!(chain.header_at_height(3), Some(&block_3));
let want = chain.height_of_hash(new_block_2.block_hash()).await;
assert_eq!(Some(2), want);
}
}
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ pub struct DisconnectedHeader {
/// The height where this header used to be in the chain.
pub height: u32,
/// The reorganized header.
pub header: BlockHash,
pub header: Header,
}

impl DisconnectedHeader {
pub(crate) fn new(height: u32, header: BlockHash) -> Self {
pub(crate) fn new(height: u32, header: Header) -> Self {
Self { height, header }
}
}
Expand Down

0 comments on commit fce625e

Please sign in to comment.