htlcswitch: refactor TestChannelLinkUpdateCommitFee common code into closure

This commit is contained in:
Wilmer Paulino 2019-08-23 16:04:43 -07:00
parent 59d19e0ca9
commit 047d5b173c
No known key found for this signature in database
GPG Key ID: 6DF57B9F9514972F

@ -3775,62 +3775,66 @@ func TestChannelLinkUpdateCommitFee(t *testing.T) {
defer n.stop() defer n.stop()
defer n.feeEstimator.Stop() defer n.feeEstimator.Stop()
// For the sake of this test, we'll reset the timer to fire in a second
// so that Alice's link queries for a new network fee.
n.aliceChannelLink.updateFeeTimer.Reset(time.Millisecond)
startingFeeRate := channels.aliceToBob.CommitFeeRate() startingFeeRate := channels.aliceToBob.CommitFeeRate()
// Next, we'll send the first fee rate response to Alice. // triggerFeeUpdate is a helper closure to determine whether a fee
select { // update was triggered and completed properly.
case n.feeEstimator.byteFeeIn <- startingFeeRate: triggerFeeUpdate := func(newFeeRate lnwallet.SatPerKWeight,
case <-time.After(time.Second * 5): shouldUpdate bool) {
t.Fatalf("alice didn't query for the new network fee")
}
time.Sleep(time.Second) t.Helper()
// The fee rate on the alice <-> bob channel should still be the same // Record the fee rates before the links process the fee update
// on both sides. // to test the case where a fee update isn't triggered.
aliceFeeRate := channels.aliceToBob.CommitFeeRate() aliceBefore := channels.aliceToBob.CommitFeeRate()
bobFeeRate := channels.bobToAlice.CommitFeeRate() bobBefore := channels.bobToAlice.CommitFeeRate()
if aliceFeeRate != startingFeeRate {
t.Fatalf("alice's fee rate shouldn't have changed: "+
"expected %v, got %v", aliceFeeRate, startingFeeRate)
}
if bobFeeRate != startingFeeRate {
t.Fatalf("bob's fee rate shouldn't have changed: "+
"expected %v, got %v", bobFeeRate, startingFeeRate)
}
// We'll reset the timer once again to ensure Alice's link queries for a // For the sake of this test, we'll reset the timer so that
// new network fee. // Alice's link queries for a new network fee.
n.aliceChannelLink.updateFeeTimer.Reset(time.Millisecond) n.aliceChannelLink.updateFeeTimer.Reset(time.Millisecond)
// Next, we'll set up a deliver a fee rate that's triple the current // Next, we'll send the first fee rate response to Alice.
// fee rate. This should cause the Alice (the initiator) to trigger a
// fee update.
newFeeRate := startingFeeRate * 3
select { select {
case n.feeEstimator.byteFeeIn <- newFeeRate: case n.feeEstimator.byteFeeIn <- newFeeRate:
case <-time.After(time.Second * 5): case <-time.After(time.Second * 5):
t.Fatalf("alice didn't query for the new network fee") t.Fatalf("alice didn't query for the new network fee")
} }
// Give the links some time to process the fee update.
time.Sleep(time.Second) time.Sleep(time.Second)
// At this point, Alice should've triggered a new fee update that // Record the fee rates after the links have processed the fee
// increased the fee rate to match the new rate. // update and ensure they are correct based on whether a fee
aliceFeeRate = channels.aliceToBob.CommitFeeRate() // update should have been triggered.
bobFeeRate = channels.bobToAlice.CommitFeeRate() aliceAfter := channels.aliceToBob.CommitFeeRate()
if aliceFeeRate != newFeeRate { bobAfter := channels.bobToAlice.CommitFeeRate()
t.Fatalf("alice's fee rate didn't change: expected %v, got %v",
newFeeRate, aliceFeeRate) switch {
case shouldUpdate && aliceAfter != newFeeRate:
t.Fatalf("alice's fee rate didn't change: expected %v, "+
"got %v", newFeeRate, aliceAfter)
case shouldUpdate && bobAfter != newFeeRate:
t.Fatalf("bob's fee rate didn't change: expected %v, "+
"got %v", newFeeRate, bobAfter)
case !shouldUpdate && aliceAfter != aliceBefore:
t.Fatalf("alice's fee rate shouldn't have changed: "+
"expected %v, got %v", aliceAfter, aliceAfter)
case !shouldUpdate && bobAfter != bobBefore:
t.Fatalf("bob's fee rate shouldn't have changed: "+
"expected %v, got %v", bobBefore, bobAfter)
} }
if bobFeeRate != newFeeRate {
t.Fatalf("bob's fee rate didn't change: expected %v, got %v",
newFeeRate, aliceFeeRate)
} }
// Triggering the link to update the fee of the channel with the same
// fee rate should not send a fee update.
triggerFeeUpdate(startingFeeRate, false)
// Triggering the link to update the fee of the channel with a much
// larger fee rate _should_ send a fee update.
triggerFeeUpdate(startingFeeRate*3, true)
} }
// TestChannelLinkAcceptDuplicatePayment tests that if a link receives an // TestChannelLinkAcceptDuplicatePayment tests that if a link receives an