lnwallet: add FeeEstimator interface, StaticFeeEstimator implementation
This commit adds the FeeEstimator interface, which can be used for future fee calculation implementations. Currently, there is only the StaticFeeEstimator implementation, which returns the same fee rate for any transaction.
This commit is contained in:
1 parent
320bed7e6b
commit
abe2e502d5
11 files changed
+100
-25
No files matched your search
+7
-1
@@ -571,6 +571,10 @@ type LightningChannel struct {
|
||||
|
||||
status channelState
|
||||
|
||||
// feeEstimator is used to calculate the fee rate for the channel's
|
||||
// commitment and cooperative close transactions.
|
||||
feeEstimator FeeEstimator
|
||||
|
||||
// Capcity is the total capacity of this channel.
|
||||
Capacity btcutil.Amount
|
||||
|
||||
@@ -679,11 +683,13 @@ type LightningChannel struct {
|
||||
// automatically persist pertinent state to the database in an efficient
|
||||
// manner.
|
||||
func NewLightningChannel(signer Signer, events chainntnfs.ChainNotifier,
|
||||
state *channeldb.OpenChannel) (*LightningChannel, error) {
|
||||
fe FeeEstimator, state *channeldb.OpenChannel) (*LightningChannel,
|
||||
error) {
|
||||
|
||||
lc := &LightningChannel{
|
||||
signer: signer,
|
||||
channelEvents: events,
|
||||
feeEstimator: fe,
|
||||
currentHeight: state.NumUpdates,
|
||||
remoteCommitChain: newCommitmentChain(state.NumUpdates),
|
||||
localCommitChain: newCommitmentChain(state.NumUpdates),
|
||||
|
||||
@@ -306,12 +306,15 @@ func createTestChannels(revocationWindow int) (*LightningChannel, *LightningChan
|
||||
bobSigner := &mockSigner{bobKeyPriv}
|
||||
|
||||
notifier := &mockNotfier{}
|
||||
estimator := &StaticFeeEstimator{50, 6}
|
||||
|
||||
channelAlice, err := NewLightningChannel(aliceSigner, notifier, aliceChannelState)
|
||||
channelAlice, err := NewLightningChannel(aliceSigner, notifier,
|
||||
estimator, aliceChannelState)
|
||||
if err != nil {
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
channelBob, err := NewLightningChannel(bobSigner, notifier, bobChannelState)
|
||||
channelBob, err := NewLightningChannel(bobSigner, notifier,
|
||||
estimator, bobChannelState)
|
||||
if err != nil {
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
@@ -1362,12 +1365,12 @@ func TestStateUpdatePersistence(t *testing.T) {
|
||||
}
|
||||
notifier := aliceChannel.channelEvents
|
||||
aliceChannelNew, err := NewLightningChannel(aliceChannel.signer,
|
||||
notifier, aliceChannels[0])
|
||||
notifier, aliceChannel.feeEstimator, aliceChannels[0])
|
||||
if err != nil {
|
||||
t.Fatalf("unable to create new channel: %v", err)
|
||||
}
|
||||
bobChannelNew, err := NewLightningChannel(bobChannel.signer, notifier,
|
||||
bobChannels[0])
|
||||
bobChannel.feeEstimator, bobChannels[0])
|
||||
if err != nil {
|
||||
t.Fatalf("unable to create new channel: %v", err)
|
||||
}
|
||||
|
||||
@@ -302,6 +302,26 @@ type MessageSigner interface {
|
||||
SignMessage(pubKey *btcec.PublicKey, msg []byte) (*btcec.Signature, error)
|
||||
}
|
||||
|
||||
// FeeEstimator provides the ability to estimate on-chain transaction fees for
|
||||
// various combinations of transaction sizes and desired confirmation time
|
||||
// (measured by number of blocks).
|
||||
type FeeEstimator interface {
|
||||
// EstimateFeePerByte takes in a target for the number of blocks until
|
||||
// an initial confirmation and returns the estimated fee expressed in
|
||||
// satoshis/byte.
|
||||
EstimateFeePerByte(numBlocks uint32) uint64
|
||||
|
||||
// EstimateFeePerWeight takes in a target for the number of blocks until
|
||||
// an initial confirmation and returns the estimated fee expressed in
|
||||
// satoshis/weight.
|
||||
EstimateFeePerWeight(numBlocks uint32) uint64
|
||||
|
||||
// EstimateConfirmation will return the number of blocks expected for a
|
||||
// transaction to be confirmed given a fee rate in satoshis per
|
||||
// byte.
|
||||
EstimateConfirmation(satPerByte int64) uint32
|
||||
}
|
||||
|
||||
// WalletDriver represents a "driver" for a particular concrete
|
||||
// WalletController implementation. A driver is identified by a globally unique
|
||||
// string identifier along with a 'New()' method which is responsible for
|
||||
|
||||
@@ -339,8 +339,9 @@ func createTestWallet(tempTestDir string, miningNode *rpctest.Harness,
|
||||
return nil, err
|
||||
}
|
||||
|
||||
estimator := lnwallet.StaticFeeEstimator{FeeRate: 250}
|
||||
wallet, err := lnwallet.NewLightningWallet(cdb, notifier, wc, signer,
|
||||
bio, netParams)
|
||||
bio, estimator, netParams)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
+33
-1
@@ -273,6 +273,10 @@ type LightningWallet struct {
|
||||
// update the commitment state.
|
||||
Signer Signer
|
||||
|
||||
// FeeEstimator is the implementation that the wallet will use for the
|
||||
// calculation of on-chain transaction fees.
|
||||
FeeEstimator FeeEstimator
|
||||
|
||||
// ChainIO is an instance of the BlockChainIO interface. ChainIO is
|
||||
// used to lookup the existence of outputs within the UTXO set.
|
||||
ChainIO BlockChainIO
|
||||
@@ -320,12 +324,13 @@ type LightningWallet struct {
|
||||
// initialized/started before being passed as a function arugment.
|
||||
func NewLightningWallet(cdb *channeldb.DB, notifier chainntnfs.ChainNotifier,
|
||||
wallet WalletController, signer Signer, bio BlockChainIO,
|
||||
netParams *chaincfg.Params) (*LightningWallet, error) {
|
||||
fe FeeEstimator, netParams *chaincfg.Params) (*LightningWallet, error) {
|
||||
|
||||
return &LightningWallet{
|
||||
chainNotifier: notifier,
|
||||
Signer: signer,
|
||||
WalletController: wallet,
|
||||
FeeEstimator: fe,
|
||||
ChainIO: bio,
|
||||
ChannelDB: cdb,
|
||||
msgChan: make(chan interface{}, msgBufferSize),
|
||||
@@ -490,6 +495,7 @@ func (l *LightningWallet) InitChannelReservation(capacity,
|
||||
|
||||
errChan := make(chan error, 1)
|
||||
respChan := make(chan *ChannelReservation, 1)
|
||||
minFeeRate := btcutil.Amount(l.FeeEstimator.EstimateFeePerWeight(1))
|
||||
|
||||
l.msgChan <- &initFundingReserveMsg{
|
||||
capacity: capacity,
|
||||
@@ -497,6 +503,7 @@ func (l *LightningWallet) InitChannelReservation(capacity,
|
||||
fundingAmount: ourFundAmt,
|
||||
csvDelay: csvDelay,
|
||||
ourDustLimit: ourDustLimit,
|
||||
minFeeRate: minFeeRate,
|
||||
pushSat: pushSat,
|
||||
nodeID: theirID,
|
||||
nodeAddr: theirAddr,
|
||||
@@ -1434,3 +1441,28 @@ func coinSelect(feeRate uint64, amt btcutil.Amount,
|
||||
return selectedUtxos, changeAmt, nil
|
||||
}
|
||||
}
|
||||
|
||||
// StaticFeeEstimator will return a static value for all fee calculation
|
||||
// requests. It is designed to be replaced by a proper fee calcuation
|
||||
// implementation.
|
||||
type StaticFeeEstimator struct {
|
||||
FeeRate uint64
|
||||
Confirmation uint32
|
||||
}
|
||||
|
||||
// EstimateFeePerByte will return a static value for fee calculations.
|
||||
func (e StaticFeeEstimator) EstimateFeePerByte(numBlocks uint32) uint64 {
|
||||
return e.FeeRate
|
||||
}
|
||||
|
||||
// EstimateFeePerWeight will return a static value for fee calculations.
|
||||
func (e StaticFeeEstimator) EstimateFeePerWeight(numBlocks uint32) uint64 {
|
||||
return e.FeeRate * 4
|
||||
}
|
||||
|
||||
// EstimateConfirmation will return a static value representing the estimated
|
||||
// number of blocks that will be required to confirm a transaction for the
|
||||
// given fee rate.
|
||||
func (e StaticFeeEstimator) EstimateConfirmation(satPerByte int64) uint32 {
|
||||
return e.Confirmation
|
||||
}
|
||||
Reference in new issue
Block a user