Skip to content

Commit

Permalink
multi: update to latest fee estimation interface
Browse files Browse the repository at this point in the history
  • Loading branch information
wpaulino committed Aug 10, 2018
1 parent a63677a commit 9d2eeb6
Show file tree
Hide file tree
Showing 21 changed files with 150 additions and 204 deletions.
7 changes: 3 additions & 4 deletions breacharbiter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1351,7 +1351,7 @@ func createTestArbiter(t *testing.T, contractBreaches chan *ContractBreachEvent,
ba := newBreachArbiter(&BreachConfig{
CloseLink: func(_ *wire.OutPoint, _ htlcswitch.ChannelCloseType) {},
DB: db,
Estimator: &lnwallet.StaticFeeEstimator{FeeRate: 50},
Estimator: &lnwallet.StaticFeeEstimator{FeePerKW: 12500},
GenSweepScript: func() ([]byte, error) { return nil, nil },
ContractBreaches: contractBreaches,
Signer: signer,
Expand Down Expand Up @@ -1491,12 +1491,11 @@ func createInitChannels(revocationWindow int) (*lnwallet.LightningChannel, *lnwa
return nil, nil, nil, err
}

estimator := &lnwallet.StaticFeeEstimator{FeeRate: 50}
feePerVSize, err := estimator.EstimateFeePerVSize(1)
estimator := &lnwallet.StaticFeeEstimator{FeePerKW: 12500}
feePerKw, err := estimator.EstimateFeePerKW(1)
if err != nil {
return nil, nil, nil, err
}
feePerKw := feePerVSize.FeePerKWeight()

// TODO(roasbeef): need to factor in commit fee?
aliceCommit := channeldb.ChannelCommitment{
Expand Down
26 changes: 16 additions & 10 deletions chainregistry.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,21 @@ const (
defaultBitcoinBaseFeeMSat = lnwire.MilliSatoshi(1000)
defaultBitcoinFeeRate = lnwire.MilliSatoshi(1)
defaultBitcoinTimeLockDelta = 144
defaultBitcoinStaticFeeRate = lnwallet.SatPerVByte(50)

defaultLitecoinMinHTLCMSat = lnwire.MilliSatoshi(1000)
defaultLitecoinBaseFeeMSat = lnwire.MilliSatoshi(1000)
defaultLitecoinFeeRate = lnwire.MilliSatoshi(1)
defaultLitecoinTimeLockDelta = 576
defaultLitecoinStaticFeeRate = lnwallet.SatPerVByte(200)
defaultLitecoinDustLimit = btcutil.Amount(54600)

// defaultBitcoinStaticFeePerKW is the fee rate of 50 sat/vbyte
// expressed in sat/kw.
defaultBitcoinStaticFeePerKW = lnwallet.SatPerKWeight(12500)

// defaultLitecoinStaticFeePerKW is the fee rate of 200 sat/vbyte
// expressed in sat/kw.
defaultLitecoinStaticFeePerKW = lnwallet.SatPerKWeight(50000)

// btcToLtcConversionRate is a fixed ratio used in order to scale up
// payments when running on the Litecoin chain.
btcToLtcConversionRate = 60
Expand Down Expand Up @@ -141,7 +147,7 @@ func newChainControlFromConfig(cfg *config, chanDB *channeldb.DB,
TimeLockDelta: cfg.Bitcoin.TimeLockDelta,
}
cc.feeEstimator = lnwallet.StaticFeeEstimator{
FeeRate: defaultBitcoinStaticFeeRate,
FeePerKW: defaultBitcoinStaticFeePerKW,
}
case litecoinChain:
cc.routingPolicy = htlcswitch.ForwardingPolicy{
Expand All @@ -151,7 +157,7 @@ func newChainControlFromConfig(cfg *config, chanDB *channeldb.DB,
TimeLockDelta: cfg.Litecoin.TimeLockDelta,
}
cc.feeEstimator = lnwallet.StaticFeeEstimator{
FeeRate: defaultLitecoinStaticFeeRate,
FeePerKW: defaultLitecoinStaticFeePerKW,
}
default:
return nil, nil, fmt.Errorf("Default routing policy for "+
Expand Down Expand Up @@ -337,9 +343,9 @@ func newChainControlFromConfig(cfg *config, chanDB *channeldb.DB,
// if we're using bitcoind as a backend, then we can
// use live fee estimates, rather than a statically
// coded value.
fallBackFeeRate := lnwallet.SatPerVByte(25)
fallBackFeeRate := lnwallet.SatPerKVByte(25 * 1000)
cc.feeEstimator, err = lnwallet.NewBitcoindFeeEstimator(
*rpcConfig, fallBackFeeRate,
*rpcConfig, fallBackFeeRate.FeePerKWeight(),
)
if err != nil {
return nil, nil, err
Expand All @@ -354,9 +360,9 @@ func newChainControlFromConfig(cfg *config, chanDB *channeldb.DB,
// if we're using litecoind as a backend, then we can
// use live fee estimates, rather than a statically
// coded value.
fallBackFeeRate := lnwallet.SatPerVByte(25)
fallBackFeeRate := lnwallet.SatPerKVByte(25 * 1000)
cc.feeEstimator, err = lnwallet.NewBitcoindFeeEstimator(
*rpcConfig, fallBackFeeRate,
*rpcConfig, fallBackFeeRate.FeePerKWeight(),
)
if err != nil {
return nil, nil, err
Expand Down Expand Up @@ -457,9 +463,9 @@ func newChainControlFromConfig(cfg *config, chanDB *channeldb.DB,
// if we're using btcd as a backend, then we can use
// live fee estimates, rather than a statically coded
// value.
fallBackFeeRate := lnwallet.SatPerVByte(25)
fallBackFeeRate := lnwallet.SatPerKVByte(25 * 1000)
cc.feeEstimator, err = lnwallet.NewBtcdFeeEstimator(
*rpcConfig, fallBackFeeRate,
*rpcConfig, fallBackFeeRate.FeePerKWeight(),
)
if err != nil {
return nil, nil, err
Expand Down
30 changes: 6 additions & 24 deletions fundingmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ import (
"sync/atomic"
"time"

"google.golang.org/grpc"

"golang.org/x/crypto/salsa20"

"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire"
Expand All @@ -28,6 +24,8 @@ import (
"github.com/lightningnetwork/lnd/lnwallet"
"github.com/lightningnetwork/lnd/lnwire"
"github.com/lightningnetwork/lnd/routing"
"golang.org/x/crypto/salsa20"
"google.golang.org/grpc"
)

const (
Expand Down Expand Up @@ -68,11 +66,6 @@ const (
// currently accepted on the Litecoin chain within the Lightning
// Protocol.
maxLtcFundingAmount = maxBtcFundingAmount * btcToLtcConversionRate

// minCommitFeePerKw is the smallest fee rate that we should propose
// for a new fee update. We'll use this as a fee floor when proposing
// and accepting updates.
minCommitFeePerKw = 253
)

var (
Expand Down Expand Up @@ -1028,8 +1021,8 @@ func (f *fundingManager) handleFundingOpen(fmsg *fundingOpenMsg) {
reservation, err := f.cfg.Wallet.InitChannelReservation(
amt, 0, msg.PushAmount,
lnwallet.SatPerKWeight(msg.FeePerKiloWeight), 0,
fmsg.peer.IdentityKey(), fmsg.peer.Address(),
&chainHash, msg.ChannelFlags,
fmsg.peer.IdentityKey(), fmsg.peer.Address(), &chainHash,
msg.ChannelFlags,
)
if err != nil {
fndgLog.Errorf("Unable to initialize reservation: %v", err)
Expand Down Expand Up @@ -2543,23 +2536,12 @@ func (f *fundingManager) handleInitFundingMsg(msg *initFundingMsg) {
// commitment transaction confirmed by the next few blocks (conf target
// of 3). We target the near blocks here to ensure that we'll be able
// to execute a timely unilateral channel closure if needed.
feePerVSize, err := f.cfg.FeeEstimator.EstimateFeePerVSize(3)
commitFeePerKw, err := f.cfg.FeeEstimator.EstimateFeePerKW(3)
if err != nil {
msg.err <- err
return
}

// If the converted fee-per-kw is below the current widely used policy
// floor, then we'll use the floor instead.
commitFeePerKw := feePerVSize.FeePerKWeight()
if commitFeePerKw < minCommitFeePerKw {
fndgLog.Infof("Proposed fee rate of %v sat/kw is below min "+
"of %v sat/kw, using fee floor", int64(commitFeePerKw),
int64(minCommitFeePerKw))

commitFeePerKw = minCommitFeePerKw
}

// We set the channel flags to indicate whether we want this channel to
// be announced to the network.
var channelFlags lnwire.FundingFlag
Expand All @@ -2573,7 +2555,7 @@ func (f *fundingManager) handleInitFundingMsg(msg *initFundingMsg) {
// request will fail, and be aborted.
reservation, err := f.cfg.Wallet.InitChannelReservation(
capacity, localAmt, msg.pushAmt, commitFeePerKw,
msg.fundingFeePerVSize, peerKey, msg.peer.Address(),
msg.fundingFeePerKw, peerKey, msg.peer.Address(),
&msg.chainHash, channelFlags,
)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion fundingmanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ func createTestFundingManager(t *testing.T, privKey *btcec.PrivateKey,
addr *lnwire.NetAddress, tempTestDir string) (*testNode, error) {

netParams := activeNetParams.Params
estimator := lnwallet.StaticFeeEstimator{FeeRate: 250}
estimator := lnwallet.StaticFeeEstimator{FeePerKW: 62500}

chainNotifier := &mockNotifier{
oneConfChannel: make(chan *chainntnfs.TxConfirmation, 1),
Expand Down
24 changes: 3 additions & 21 deletions htlcswitch/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@ const (
// TODO(roasbeef): must be < default delta
expiryGraceDelta = 2

// minCommitFeePerKw is the smallest fee rate that we should propose
// for a new fee update. We'll use this as a fee floor when proposing
// and accepting updates.
minCommitFeePerKw = 253

// DefaultMinLinkFeeUpdateTimeout represents the minimum interval in
// which a link should propose to update its commitment fee rate.
DefaultMinLinkFeeUpdateTimeout = 10 * time.Minute
Expand Down Expand Up @@ -495,26 +490,13 @@ func (l *channelLink) EligibleToForward() bool {
// this is the native rate used when computing the fee for commitment
// transactions, and the second-level HTLC transactions.
func (l *channelLink) sampleNetworkFee() (lnwallet.SatPerKWeight, error) {
// We'll first query for the sat/vbyte recommended to be confirmed
// within 3 blocks.
feePerVSize, err := l.cfg.FeeEstimator.EstimateFeePerVSize(3)
// We'll first query for the sat/kw recommended to be confirmed within 3
// blocks.
feePerKw, err := l.cfg.FeeEstimator.EstimateFeePerKW(3)
if err != nil {
return 0, err
}

// Once we have this fee rate, we'll convert to sat-per-kw.
feePerKw := feePerVSize.FeePerKWeight()

// If the returned feePerKw is less than the current widely used
// policy, then we'll use that instead as a floor.
if feePerKw < minCommitFeePerKw {
log.Debugf("Proposed fee rate of %v sat/kw is below min "+
"of %v sat/kw, using fee floor", int64(feePerKw),
int64(minCommitFeePerKw))

feePerKw = minCommitFeePerKw
}

log.Debugf("ChannelLink(%v): sampled fee rate for 3 block conf: %v "+
"sat/kw", l, int64(feePerKw))

Expand Down
47 changes: 14 additions & 33 deletions htlcswitch/link_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1726,14 +1726,11 @@ func TestChannelLinkBandwidthConsistency(t *testing.T) {
coreLink.cfg.HodlMask = hodl.MaskFromFlags(hodl.ExitSettle)
coreLink.cfg.DebugHTLC = true

estimator := &lnwallet.StaticFeeEstimator{
FeeRate: 24,
}
feeRate, err := estimator.EstimateFeePerVSize(1)
estimator := &lnwallet.StaticFeeEstimator{FeePerKW: 6000}
feePerKw, err := estimator.EstimateFeePerKW(1)
if err != nil {
t.Fatalf("unable to query fee estimator: %v", err)
}
feePerKw := feeRate.FeePerKWeight()
htlcFee := lnwire.NewMSatFromSatoshis(
feePerKw.FeeForWeight(lnwallet.HtlcWeight),
)
Expand Down Expand Up @@ -2140,14 +2137,11 @@ func TestChannelLinkBandwidthConsistencyOverflow(t *testing.T) {
aliceMsgs = coreLink.cfg.Peer.(*mockPeer).sentMsgs
)

estimator := &lnwallet.StaticFeeEstimator{
FeeRate: 24,
}
feeRate, err := estimator.EstimateFeePerVSize(1)
estimator := &lnwallet.StaticFeeEstimator{FeePerKW: 6000}
feePerKw, err := estimator.EstimateFeePerKW(1)
if err != nil {
t.Fatalf("unable to query fee estimator: %v", err)
}
feePerKw := feeRate.FeePerKWeight()

var htlcID uint64
addLinkHTLC := func(id uint64, amt lnwire.MilliSatoshi) [32]byte {
Expand Down Expand Up @@ -2390,17 +2384,15 @@ func TestChannelLinkTrimCircuitsPending(t *testing.T) {

// Compute the static fees that will be used to determine the
// correctness of Alice's bandwidth when forwarding HTLCs.
estimator := &lnwallet.StaticFeeEstimator{
FeeRate: 24,
}
feeRate, err := estimator.EstimateFeePerVSize(1)
estimator := &lnwallet.StaticFeeEstimator{FeePerKW: 6000}
feePerKw, err := estimator.EstimateFeePerKW(1)
if err != nil {
t.Fatalf("unable to query fee estimator: %v", err)
}

defaultCommitFee := alice.channel.StateSnapshot().CommitFee
htlcFee := lnwire.NewMSatFromSatoshis(
feeRate.FeePerKWeight().FeeForWeight(lnwallet.HtlcWeight),
feePerKw.FeeForWeight(lnwallet.HtlcWeight),
)

// The starting bandwidth of the channel should be exactly the amount
Expand Down Expand Up @@ -2666,17 +2658,15 @@ func TestChannelLinkTrimCircuitsNoCommit(t *testing.T) {

// Compute the static fees that will be used to determine the
// correctness of Alice's bandwidth when forwarding HTLCs.
estimator := &lnwallet.StaticFeeEstimator{
FeeRate: 24,
}
feeRate, err := estimator.EstimateFeePerVSize(1)
estimator := &lnwallet.StaticFeeEstimator{FeePerKW: 6000}
feePerKw, err := estimator.EstimateFeePerKW(1)
if err != nil {
t.Fatalf("unable to query fee estimator: %v", err)
}

defaultCommitFee := alice.channel.StateSnapshot().CommitFee
htlcFee := lnwire.NewMSatFromSatoshis(
feeRate.FeePerKWeight().FeeForWeight(lnwallet.HtlcWeight),
feePerKw.FeeForWeight(lnwallet.HtlcWeight),
)

// The starting bandwidth of the channel should be exactly the amount
Expand Down Expand Up @@ -2926,14 +2916,11 @@ func TestChannelLinkBandwidthChanReserve(t *testing.T) {
aliceMsgs = coreLink.cfg.Peer.(*mockPeer).sentMsgs
)

estimator := &lnwallet.StaticFeeEstimator{
FeeRate: 24,
}
feeRate, err := estimator.EstimateFeePerVSize(1)
estimator := &lnwallet.StaticFeeEstimator{FeePerKW: 6000}
feePerKw, err := estimator.EstimateFeePerKW(1)
if err != nil {
t.Fatalf("unable to query fee estimator: %v", err)
}
feePerKw := feeRate.FeePerKWeight()
htlcFee := lnwire.NewMSatFromSatoshis(
feePerKw.FeeForWeight(lnwallet.HtlcWeight),
)
Expand Down Expand Up @@ -3460,15 +3447,9 @@ 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.byteFeeIn <- startingFeeRateSatPerVByte:
case n.feeEstimator.byteFeeIn <- startingFeeRate:
case <-time.After(time.Second * 5):
t.Fatalf("alice didn't query for the new network fee")
}
Expand Down Expand Up @@ -3497,7 +3478,7 @@ func TestChannelLinkUpdateCommitFee(t *testing.T) {
// fee update.
newFeeRate := startingFeeRate * 3
select {
case n.feeEstimator.byteFeeIn <- startingFeeRateSatPerVByte * 3:
case n.feeEstimator.byteFeeIn <- newFeeRate:
case <-time.After(time.Second * 5):
t.Fatalf("alice didn't query for the new network fee")
}
Expand Down
6 changes: 4 additions & 2 deletions htlcswitch/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,14 @@ func (m *mockPreimageCache) SubscribeUpdates() *contractcourt.WitnessSubscriptio
}

type mockFeeEstimator struct {
byteFeeIn chan lnwallet.SatPerVByte
byteFeeIn chan lnwallet.SatPerKWeight

quit chan struct{}
}

func (m *mockFeeEstimator) EstimateFeePerVSize(numBlocks uint32) (lnwallet.SatPerVByte, error) {
func (m *mockFeeEstimator) EstimateFeePerKW(
numBlocks uint32) (lnwallet.SatPerKWeight, error) {

select {
case feeRate := <-m.byteFeeIn:
return feeRate, nil
Expand Down
9 changes: 3 additions & 6 deletions htlcswitch/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,14 +271,11 @@ func createTestChannel(alicePrivKey, bobPrivKey []byte,
return nil, nil, nil, nil, err
}

estimator := &lnwallet.StaticFeeEstimator{
FeeRate: 24,
}
feePerVSize, err := estimator.EstimateFeePerVSize(1)
estimator := &lnwallet.StaticFeeEstimator{FeePerKW: 6000}
feePerKw, err := estimator.EstimateFeePerKW(1)
if err != nil {
return nil, nil, nil, nil, err
}
feePerKw := feePerVSize.FeePerKWeight()
commitFee := feePerKw.FeeForWeight(724)

const broadcastHeight = 1
Expand Down Expand Up @@ -873,7 +870,7 @@ func newThreeHopNetwork(t testing.TB, aliceChannel, firstBobChannel,
carolDecoder := newMockIteratorDecoder()

feeEstimator := &mockFeeEstimator{
byteFeeIn: make(chan lnwallet.SatPerVByte),
byteFeeIn: make(chan lnwallet.SatPerKWeight),
quit: make(chan struct{}),
}

Expand Down
Loading

0 comments on commit 9d2eeb6

Please sign in to comment.