htlcswitch tests: update tests to new FeeEstimator and fee rate types

This commit is contained in:
Johan T. Halseth 2018-02-13 14:57:47 +01:00
parent 80277c0517
commit 4d2a36dce8
No known key found for this signature in database
GPG Key ID: 15BAADA29DA20D26
3 changed files with 32 additions and 42 deletions

View File

@ -1683,13 +1683,13 @@ func TestChannelLinkBandwidthConsistency(t *testing.T) {
estimator := &lnwallet.StaticFeeEstimator{ estimator := &lnwallet.StaticFeeEstimator{
FeeRate: 24, FeeRate: 24,
} }
feePerWeight, err := estimator.EstimateFeePerWeight(1) feeRate, err := estimator.EstimateFeePerVSize(1)
if err != nil { if err != nil {
t.Fatalf("unable to query fee estimator: %v", err) t.Fatalf("unable to query fee estimator: %v", err)
} }
feePerKw := feePerWeight * 1000 feePerKw := feeRate.FeePerKWeight()
htlcFee := lnwire.NewMSatFromSatoshis( 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 // The starting bandwidth of the channel should be exactly the amount
@ -2010,11 +2010,11 @@ func TestChannelLinkBandwidthConsistencyOverflow(t *testing.T) {
estimator := &lnwallet.StaticFeeEstimator{ estimator := &lnwallet.StaticFeeEstimator{
FeeRate: 24, FeeRate: 24,
} }
feePerWeight, err := estimator.EstimateFeePerWeight(1) feeRate, err := estimator.EstimateFeePerVSize(1)
if err != nil { if err != nil {
t.Fatalf("unable to query fee estimator: %v", err) 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 // The starting bandwidth of the channel should be exactly the amount
// that we created the channel between her and Bob. // that we created the channel between her and Bob.
@ -2077,7 +2077,7 @@ func TestChannelLinkBandwidthConsistencyOverflow(t *testing.T) {
time.Sleep(time.Second * 1) time.Sleep(time.Second * 1)
commitWeight := lnwallet.CommitWeight + lnwallet.HtlcWeight*numHTLCs commitWeight := lnwallet.CommitWeight + lnwallet.HtlcWeight*numHTLCs
htlcFee := lnwire.NewMSatFromSatoshis( htlcFee := lnwire.NewMSatFromSatoshis(
btcutil.Amount((int64(feePerKw) * commitWeight) / 1000), feePerKw.FeeForWeight(commitWeight),
) )
expectedBandwidth = aliceStartingBandwidth - totalHtlcAmt - htlcFee expectedBandwidth = aliceStartingBandwidth - totalHtlcAmt - htlcFee
expectedBandwidth += lnwire.NewMSatFromSatoshis(defaultCommitFee) expectedBandwidth += lnwire.NewMSatFromSatoshis(defaultCommitFee)
@ -2222,13 +2222,13 @@ func TestChannelLinkBandwidthChanReserve(t *testing.T) {
estimator := &lnwallet.StaticFeeEstimator{ estimator := &lnwallet.StaticFeeEstimator{
FeeRate: 24, FeeRate: 24,
} }
feePerWeight, err := estimator.EstimateFeePerWeight(1) feeRate, err := estimator.EstimateFeePerVSize(1)
if err != nil { if err != nil {
t.Fatalf("unable to query fee estimator: %v", err) t.Fatalf("unable to query fee estimator: %v", err)
} }
feePerKw := feePerWeight * 1000 feePerKw := feeRate.FeePerKWeight()
htlcFee := lnwire.NewMSatFromSatoshis( 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 // 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. // deviates from our current fee by more 10% or more.
func TestShouldAdjustCommitFee(t *testing.T) { func TestShouldAdjustCommitFee(t *testing.T) {
tests := []struct { tests := []struct {
netFee btcutil.Amount netFee lnwallet.SatPerKWeight
chanFee btcutil.Amount chanFee lnwallet.SatPerKWeight
shouldAdjust bool shouldAdjust bool
}{ }{
@ -2754,9 +2754,15 @@ func TestChannelLinkUpdateCommitFee(t *testing.T) {
startingFeeRate := channels.aliceToBob.CommitFeeRate() 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. // Next, we'll send the first fee rate response to Alice.
select { select {
case n.feeEstimator.weightFeeIn <- startingFeeRate / 1000: case n.feeEstimator.byteFeeIn <- startingFeeRateSatPerVByte:
case <-time.After(time.Second * 5): case <-time.After(time.Second * 5):
t.Fatalf("alice didn't query for the new " + t.Fatalf("alice didn't query for the new " +
"network fee") "network fee")
@ -2803,7 +2809,7 @@ func TestChannelLinkUpdateCommitFee(t *testing.T) {
// fee update. // fee update.
newFeeRate := startingFeeRate * 3 newFeeRate := startingFeeRate * 3
select { select {
case n.feeEstimator.weightFeeIn <- newFeeRate: case n.feeEstimator.byteFeeIn <- startingFeeRateSatPerVByte * 3:
case <-time.After(time.Second * 5): case <-time.After(time.Second * 5):
t.Fatalf("alice didn't query for the new " + t.Fatalf("alice didn't query for the new " +
"network fee") "network fee")
@ -2813,19 +2819,15 @@ func TestChannelLinkUpdateCommitFee(t *testing.T) {
// At this point, Alice should've triggered a new fee update that // At this point, Alice should've triggered a new fee update that
// increased the fee rate to match the new rate. // 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() aliceFeeRate = channels.aliceToBob.CommitFeeRate()
bobFeeRate = channels.bobToAlice.CommitFeeRate() bobFeeRate = channels.bobToAlice.CommitFeeRate()
if aliceFeeRate != expectedFeeRate { if aliceFeeRate != newFeeRate {
t.Fatalf("alice's fee rate didn't change: expected %v, got %v", 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", t.Fatalf("bob's fee rate didn't change: expected %v, got %v",
expectedFeeRate, aliceFeeRate) newFeeRate, aliceFeeRate)
} }
if aliceFeeRate != bobFeeRate { if aliceFeeRate != bobFeeRate {
t.Fatalf("fee rates don't match: expected %v got %v", t.Fatalf("fee rates don't match: expected %v got %v",

View File

@ -24,7 +24,6 @@ import (
"github.com/roasbeef/btcd/chaincfg/chainhash" "github.com/roasbeef/btcd/chaincfg/chainhash"
"github.com/roasbeef/btcd/txscript" "github.com/roasbeef/btcd/txscript"
"github.com/roasbeef/btcd/wire" "github.com/roasbeef/btcd/wire"
"github.com/roasbeef/btcutil"
) )
type mockPreimageCache struct { type mockPreimageCache struct {
@ -57,13 +56,12 @@ func (m *mockPreimageCache) SubscribeUpdates() *contractcourt.WitnessSubscriptio
} }
type mockFeeEstimator struct { type mockFeeEstimator struct {
byteFeeIn chan btcutil.Amount byteFeeIn chan lnwallet.SatPerVByte
weightFeeIn chan btcutil.Amount
quit chan struct{} quit chan struct{}
} }
func (m *mockFeeEstimator) EstimateFeePerByte(numBlocks uint32) (btcutil.Amount, error) { func (m *mockFeeEstimator) EstimateFeePerVSize(numBlocks uint32) (lnwallet.SatPerVByte, error) {
select { select {
case feeRate := <-m.byteFeeIn: case feeRate := <-m.byteFeeIn:
return feeRate, nil 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 { func (m *mockFeeEstimator) Start() error {
return nil return nil
} }

View File

@ -188,12 +188,12 @@ func createTestChannel(alicePrivKey, bobPrivKey []byte,
estimator := &lnwallet.StaticFeeEstimator{ estimator := &lnwallet.StaticFeeEstimator{
FeeRate: 24, FeeRate: 24,
} }
feePerWeight, err := estimator.EstimateFeePerWeight(1) feePerVSize, err := estimator.EstimateFeePerVSize(1)
if err != nil { if err != nil {
return nil, nil, nil, nil, err return nil, nil, nil, nil, err
} }
feePerKw := btcutil.Amount(feePerWeight * 1000) feePerKw := feePerVSize.FeePerKWeight()
commitFee := (feePerKw * btcutil.Amount(724)) / 1000 commitFee := feePerKw.FeeForWeight(724)
const broadcastHeight = 1 const broadcastHeight = 1
bobAddr := &net.TCPAddr{ bobAddr := &net.TCPAddr{
@ -211,7 +211,7 @@ func createTestChannel(alicePrivKey, bobPrivKey []byte,
LocalBalance: lnwire.NewMSatFromSatoshis(aliceAmount - commitFee), LocalBalance: lnwire.NewMSatFromSatoshis(aliceAmount - commitFee),
RemoteBalance: lnwire.NewMSatFromSatoshis(bobAmount), RemoteBalance: lnwire.NewMSatFromSatoshis(bobAmount),
CommitFee: commitFee, CommitFee: commitFee,
FeePerKw: feePerKw, FeePerKw: btcutil.Amount(feePerKw),
CommitTx: aliceCommitTx, CommitTx: aliceCommitTx,
CommitSig: bytes.Repeat([]byte{1}, 71), CommitSig: bytes.Repeat([]byte{1}, 71),
} }
@ -220,7 +220,7 @@ func createTestChannel(alicePrivKey, bobPrivKey []byte,
LocalBalance: lnwire.NewMSatFromSatoshis(bobAmount), LocalBalance: lnwire.NewMSatFromSatoshis(bobAmount),
RemoteBalance: lnwire.NewMSatFromSatoshis(aliceAmount - commitFee), RemoteBalance: lnwire.NewMSatFromSatoshis(aliceAmount - commitFee),
CommitFee: commitFee, CommitFee: commitFee,
FeePerKw: feePerKw, FeePerKw: btcutil.Amount(feePerKw),
CommitTx: bobCommitTx, CommitTx: bobCommitTx,
CommitSig: bytes.Repeat([]byte{1}, 71), CommitSig: bytes.Repeat([]byte{1}, 71),
} }
@ -744,9 +744,8 @@ func newThreeHopNetwork(t testing.TB, aliceChannel, firstBobChannel,
decoder := &mockIteratorDecoder{} decoder := &mockIteratorDecoder{}
feeEstimator := &mockFeeEstimator{ feeEstimator := &mockFeeEstimator{
byteFeeIn: make(chan btcutil.Amount), byteFeeIn: make(chan lnwallet.SatPerVByte),
weightFeeIn: make(chan btcutil.Amount), quit: make(chan struct{}),
quit: make(chan struct{}),
} }
pCache := &mockPreimageCache{ pCache := &mockPreimageCache{