Skip to content
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

sim_network: Add unit tests + squash bugs #175

Merged
merged 7 commits into from
Mar 22, 2024

Conversation

carlaKC
Copy link
Contributor

@carlaKC carlaKC commented Mar 14, 2024

This PR adds coverage for our simulated network and (unsurprisingly) squashes some bugs:

  • Do not double-return liquidity for failed HTLCs: previously we'd return liquidity to local balance on failures twice over
  • Off by one removal: if a HTLC failed at index 0, we would not remove the first HTLC because our loop was not running for =0.

Removal bugs not caught in original testing because we evenly distribute liquidity, so didn't run into any payment failure - going to set liquidity to zero and re-run for a while to see if anything falls apart.

@carlaKC carlaKC requested a review from enigbe March 14, 2024 15:46
@carlaKC carlaKC marked this pull request as ready for review March 14, 2024 16:53
@carlaKC carlaKC requested a review from sr-gi March 14, 2024 16:58
}

impl<'a> DispatchPaymentTestKit<'a> {
/// Creates a test graph with a set of nodes connected by channels three channels, with all the capacity of
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: "connected by three channels"

// Test insufficient fee.
let htlc_amount = 1000;
let htlc_fee = (channel_state.policy.base_fee as f64
+ ((channel_state.policy.fee_rate_prop as f64 * htlc_amount as f64) / 1000000.0))
Copy link
Contributor

@enigbe enigbe Mar 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would extracting this into a constant improve readability? I'd wondered why the division with 1,000,000.0 until I consulted the lightning textbook to review how the fee rate is calculated.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty standard to have this peppered around LN fee calculations :') changed to 1e6 to make a little more readable, but leaving as-is for now.

Copy link
Contributor

@enigbe enigbe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@carlaKC I think you've done a really good job with the tests. I have found them very helpful in navigating sim_node and better understanding aspects of the code I'd cursorily reviewed before.
I have a few comments/questions on flakiness, decimal precision, multiple assertions, and readability.

carlaKC added 6 commits March 20, 2024 10:18
We handle balances in settle_htlc, so do not need to shift local
balance in remove_outgoing_htlc.
With the previous code, our removal loop would not execute for a
resolution index of zero. To fix this, we make the loop inclusive and
provide the appropriate index for successful HTLCs (failing HTLCs
already use the correct failure index).
To allow more isolated testing of channel state, move balance updates
to function on the individual sides of the channel.
@carlaKC
Copy link
Contributor Author

carlaKC commented Mar 20, 2024

Thanks for the review @enigbe! Updated to address most feedback, left some of the test setup as-is for the sake of simplicity (can always come back and improve as is required).

@carlaKC carlaKC requested a review from enigbe March 21, 2024 12:15
Copy link
Contributor

@enigbe enigbe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for addressing all comments. I have no additional review on this.

@carlaKC carlaKC merged commit 028c647 into bitcoin-dev-project:main Mar 22, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants