-
Notifications
You must be signed in to change notification settings - Fork 114
Add reorg test #604
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add reorg test #604
Conversation
|
👋 Thanks for assigning @tnull as a reviewer! |
a9a00e4 to
c28fa3c
Compare
tnull
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool, thanks for looking into this! At a high-level this looks pretty much good to me, but I'll need to take a closer look at the test logic still.
I hope the base PR (#602) get's merged soon to fix builds on main. Mind adding a more descriptive commit message in the meantime?
c28fa3c to
6d9a167
Compare
|
Thanks! I was going to make the commit message more descriptive, but got caught up looking into the CI failure. Since PR (#602) fixed it, I forgot to update the message. I initially opened this as a draft to address the issue before marking it ready. |
|
This needs a rebase now. |
6d9a167 to
78a73de
Compare
tnull
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Basically LGTM, just some minor comments/nits.
tests/reorg_test.rs
Outdated
| macro_rules! reorg { | ||
| ($reorg_depth: expr) => {{ | ||
| invalidate_blocks(bitcoind, $reorg_depth); | ||
| generate_blocks_and_wait(bitcoind, electrs, $reorg_depth); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I want to note that operating on the same chain and generating/invalidating blocks is only safe/working because proptests are run sequentially as it seems. Fine for this case, but maybe we should inline init_setup_test_reorg to avoid confusion in the future if other tests would try to reuse the same helper?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You’re right. I initially created init_setup_test_reorg when building the test, thinking I’d need many proptest rounds. Since reorgs don’t require that many cases, the performance impact is negligible now, so I’ve removed the helper.
tests/reorg_test.rs
Outdated
| force_close in prop::bool::ANY, | ||
| ) { | ||
|
|
||
| let (bitcoind, electrsd) = init_setup_test_reorg(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Seems all of this method needs one more indentation to the right. rustfmt didn't catch this as it doesn't operate on macro code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done!
tests/reorg_test.rs
Outdated
| assert!(node.list_balances().pending_balances_from_channel_closures.len() > 0); | ||
| match node.list_balances().pending_balances_from_channel_closures[0] { | ||
| PendingSweepBalance::BroadcastAwaitingConfirmation { .. } => {}, | ||
| _ => println!("Unexpected balance state!"), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we panic here and below rather than just printing that we reached an unexpected state?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, had to remove it for tests, but added it back now.
tests/reorg_test.rs
Outdated
|
|
||
| // Check balance after close channel | ||
| nodes.iter().for_each(|node| { | ||
| assert!(node.list_balances().spendable_onchain_balance_sats > amount - 7000); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where is this 7000 number coming from? Mind introducing an appropriately named variable for it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done! It’s the approximate tx fee to prevent amount check failures.
tests/reorg_test.rs
Outdated
| } | ||
|
|
||
| let mut node_channels_id = HashMap::new(); | ||
| for index in 0..nodes.len() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Rather than iterating over the index, can we use iterators? (here and elswhere)
| for index in 0..nodes.len() { | |
| for node in nodes.iter() { |
They are usually a bit easier to read.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I replaced some for index loops with clearer iterator expressions.
78a73de to
a4d755f
Compare
tnull
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! One last comment, should otherwise be good to go.
tests/reorg_test.rs
Outdated
| }}; | ||
| } | ||
|
|
||
| let amount = 2_100_000; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For numerical amounts and fees please always append a _sat/_msat/_btc suffix to make it explicit which unit we're dealing in (here and everywhere else).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
- Introduced a property test to verify reorg handling during both channel opening and closing (normal and force close). - Added `invalidate_block` helper to roll back the chain and regenerate blocks to the previous height.
a4d755f to
e57927a
Compare
tnull
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK, will merge if CI passes.
This PR introduces functional tests to validate node behavior under blockchain reorganization scenarios, specifically when opening and closing channels.
Additionally, the premine_and_distribute_funds helper was optimized. Previously, when many addresses were funded, transactions were sent sequentially, waiting for each to appear in the mempool before sending the next. This was slow. The implementation now uses sendmany from Bitcoin Core to send all outputs in a single transaction, improving speed for all tests relying on this function.
Close #575