From 4d2a36dce89accd0446fce1361a54edbdda49cb5 Mon Sep 17 00:00:00 2001 From: "Johan T. Halseth" Date: Tue, 13 Feb 2018 14:57:47 +0100 Subject: [PATCH] htlcswitch tests: update tests to new FeeEstimator and fee rate types --- htlcswitch/link_test.go | 44 +++++++++++++++++++++------------------- htlcswitch/mock.go | 15 ++------------ htlcswitch/test_utils.go | 15 +++++++------- 3 files changed, 32 insertions(+), 42 deletions(-) diff --git a/htlcswitch/link_test.go b/htlcswitch/link_test.go index aa6ade23..8add4e84 100644 --- a/htlcswitch/link_test.go +++ b/htlcswitch/link_test.go @@ -1683,13 +1683,13 @@ func TestChannelLinkBandwidthConsistency(t *testing.T) { estimator := &lnwallet.StaticFeeEstimator{ FeeRate: 24, } - feePerWeight, err := estimator.EstimateFeePerWeight(1) + feeRate, err := estimator.EstimateFeePerVSize(1) if err != nil { t.Fatalf("unable to query fee estimator: %v", err) } - feePerKw := feePerWeight * 1000 + feePerKw := feeRate.FeePerKWeight() htlcFee := lnwire.NewMSatFromSatoshis( - btcutil.Amount((int64(feePerKw) * lnwallet.HtlcWeight) / 1000), + feePerKw.FeeForWeight(lnwallet.HtlcWeight), ) // The starting bandwidth of the channel should be exactly the amount @@ -2010,11 +2010,11 @@ func TestChannelLinkBandwidthConsistencyOverflow(t *testing.T) { estimator := &lnwallet.StaticFeeEstimator{ FeeRate: 24, } - feePerWeight, err := estimator.EstimateFeePerWeight(1) + feeRate, err := estimator.EstimateFeePerVSize(1) if err != nil { t.Fatalf("unable to query fee estimator: %v", err) } - feePerKw := feePerWeight * 1000 + feePerKw := feeRate.FeePerKWeight() // The starting bandwidth of the channel should be exactly the amount // that we created the channel between her and Bob. @@ -2077,7 +2077,7 @@ func TestChannelLinkBandwidthConsistencyOverflow(t *testing.T) { time.Sleep(time.Second * 1) commitWeight := lnwallet.CommitWeight + lnwallet.HtlcWeight*numHTLCs htlcFee := lnwire.NewMSatFromSatoshis( - btcutil.Amount((int64(feePerKw) * commitWeight) / 1000), + feePerKw.FeeForWeight(commitWeight), ) expectedBandwidth = aliceStartingBandwidth - totalHtlcAmt - htlcFee expectedBandwidth += lnwire.NewMSatFromSatoshis(defaultCommitFee) @@ -2222,13 +2222,13 @@ func TestChannelLinkBandwidthChanReserve(t *testing.T) { estimator := &lnwallet.StaticFeeEstimator{ FeeRate: 24, } - feePerWeight, err := estimator.EstimateFeePerWeight(1) + feeRate, err := estimator.EstimateFeePerVSize(1) if err != nil { t.Fatalf("unable to query fee estimator: %v", err) } - feePerKw := feePerWeight * 1000 + feePerKw := feeRate.FeePerKWeight() htlcFee := lnwire.NewMSatFromSatoshis( - btcutil.Amount((int64(feePerKw) * lnwallet.HtlcWeight) / 1000), + feePerKw.FeeForWeight(lnwallet.HtlcWeight), ) // The starting bandwidth of the channel should be exactly the amount @@ -2595,8 +2595,8 @@ func TestChannelRetransmission(t *testing.T) { // deviates from our current fee by more 10% or more. func TestShouldAdjustCommitFee(t *testing.T) { tests := []struct { - netFee btcutil.Amount - chanFee btcutil.Amount + netFee lnwallet.SatPerKWeight + chanFee lnwallet.SatPerKWeight shouldAdjust bool }{ @@ -2754,9 +2754,15 @@ func TestChannelLinkUpdateCommitFee(t *testing.T) { startingFeeRate := channels.aliceToBob.CommitFeeRate() + // Convert starting fee rate to sat/vbyte. This is usually a + // lossy conversion, but since the startingFeeRate is + // 6000 sat/kw in this case, we won't lose precision. + startingFeeRateSatPerVByte := lnwallet.SatPerVByte( + startingFeeRate * 4 / 1000) + // Next, we'll send the first fee rate response to Alice. select { - case n.feeEstimator.weightFeeIn <- startingFeeRate / 1000: + case n.feeEstimator.byteFeeIn <- startingFeeRateSatPerVByte: case <-time.After(time.Second * 5): t.Fatalf("alice didn't query for the new " + "network fee") @@ -2803,7 +2809,7 @@ func TestChannelLinkUpdateCommitFee(t *testing.T) { // fee update. newFeeRate := startingFeeRate * 3 select { - case n.feeEstimator.weightFeeIn <- newFeeRate: + case n.feeEstimator.byteFeeIn <- startingFeeRateSatPerVByte * 3: case <-time.After(time.Second * 5): t.Fatalf("alice didn't query for the new " + "network fee") @@ -2813,19 +2819,15 @@ func TestChannelLinkUpdateCommitFee(t *testing.T) { // At this point, Alice should've triggered a new fee update that // increased the fee rate to match the new rate. - // - // We'll scale the new fee rate by 100 as we deal with units of fee - // per-kw. - expectedFeeRate := newFeeRate * 1000 aliceFeeRate = channels.aliceToBob.CommitFeeRate() bobFeeRate = channels.bobToAlice.CommitFeeRate() - if aliceFeeRate != expectedFeeRate { + if aliceFeeRate != newFeeRate { t.Fatalf("alice's fee rate didn't change: expected %v, got %v", - expectedFeeRate, aliceFeeRate) + newFeeRate, aliceFeeRate) } - if bobFeeRate != expectedFeeRate { + if bobFeeRate != newFeeRate { t.Fatalf("bob's fee rate didn't change: expected %v, got %v", - expectedFeeRate, aliceFeeRate) + newFeeRate, aliceFeeRate) } if aliceFeeRate != bobFeeRate { t.Fatalf("fee rates don't match: expected %v got %v", diff --git a/htlcswitch/mock.go b/htlcswitch/mock.go index f5ba055b..55df1e49 100644 --- a/htlcswitch/mock.go +++ b/htlcswitch/mock.go @@ -24,7 +24,6 @@ import ( "github.com/roasbeef/btcd/chaincfg/chainhash" "github.com/roasbeef/btcd/txscript" "github.com/roasbeef/btcd/wire" - "github.com/roasbeef/btcutil" ) type mockPreimageCache struct { @@ -57,13 +56,12 @@ func (m *mockPreimageCache) SubscribeUpdates() *contractcourt.WitnessSubscriptio } type mockFeeEstimator struct { - byteFeeIn chan btcutil.Amount - weightFeeIn chan btcutil.Amount + byteFeeIn chan lnwallet.SatPerVByte quit chan struct{} } -func (m *mockFeeEstimator) EstimateFeePerByte(numBlocks uint32) (btcutil.Amount, error) { +func (m *mockFeeEstimator) EstimateFeePerVSize(numBlocks uint32) (lnwallet.SatPerVByte, error) { select { case feeRate := <-m.byteFeeIn: return feeRate, nil @@ -72,15 +70,6 @@ func (m *mockFeeEstimator) EstimateFeePerByte(numBlocks uint32) (btcutil.Amount, } } -func (m *mockFeeEstimator) EstimateFeePerWeight(numBlocks uint32) (btcutil.Amount, error) { - select { - case feeRate := <-m.weightFeeIn: - return feeRate, nil - case <-m.quit: - return 0, fmt.Errorf("exiting") - } -} - func (m *mockFeeEstimator) Start() error { return nil } diff --git a/htlcswitch/test_utils.go b/htlcswitch/test_utils.go index bc3b0191..7549568d 100644 --- a/htlcswitch/test_utils.go +++ b/htlcswitch/test_utils.go @@ -188,12 +188,12 @@ func createTestChannel(alicePrivKey, bobPrivKey []byte, estimator := &lnwallet.StaticFeeEstimator{ FeeRate: 24, } - feePerWeight, err := estimator.EstimateFeePerWeight(1) + feePerVSize, err := estimator.EstimateFeePerVSize(1) if err != nil { return nil, nil, nil, nil, err } - feePerKw := btcutil.Amount(feePerWeight * 1000) - commitFee := (feePerKw * btcutil.Amount(724)) / 1000 + feePerKw := feePerVSize.FeePerKWeight() + commitFee := feePerKw.FeeForWeight(724) const broadcastHeight = 1 bobAddr := &net.TCPAddr{ @@ -211,7 +211,7 @@ func createTestChannel(alicePrivKey, bobPrivKey []byte, LocalBalance: lnwire.NewMSatFromSatoshis(aliceAmount - commitFee), RemoteBalance: lnwire.NewMSatFromSatoshis(bobAmount), CommitFee: commitFee, - FeePerKw: feePerKw, + FeePerKw: btcutil.Amount(feePerKw), CommitTx: aliceCommitTx, CommitSig: bytes.Repeat([]byte{1}, 71), } @@ -220,7 +220,7 @@ func createTestChannel(alicePrivKey, bobPrivKey []byte, LocalBalance: lnwire.NewMSatFromSatoshis(bobAmount), RemoteBalance: lnwire.NewMSatFromSatoshis(aliceAmount - commitFee), CommitFee: commitFee, - FeePerKw: feePerKw, + FeePerKw: btcutil.Amount(feePerKw), CommitTx: bobCommitTx, CommitSig: bytes.Repeat([]byte{1}, 71), } @@ -744,9 +744,8 @@ func newThreeHopNetwork(t testing.TB, aliceChannel, firstBobChannel, decoder := &mockIteratorDecoder{} feeEstimator := &mockFeeEstimator{ - byteFeeIn: make(chan btcutil.Amount), - weightFeeIn: make(chan btcutil.Amount), - quit: make(chan struct{}), + byteFeeIn: make(chan lnwallet.SatPerVByte), + quit: make(chan struct{}), } pCache := &mockPreimageCache{