mirror of
https://github.com/lightningnetwork/lnd.git
synced 2024-11-19 09:53:54 +01:00
777ed104a3
In this commit, we create a new chainfee package, that houses all fee related functionality used within the codebase. The creation of this new package furthers our long-term goal of extracting functionality from the bloated `lnwallet` package into new distinct packages. Additionally, this new packages resolves a class of import cycle that could arise if a new package that was imported by something in `lnwallet` wanted to use the existing fee related functions in the prior `lnwallet` package.
79 lines
1.6 KiB
Go
79 lines
1.6 KiB
Go
package sweep
|
|
|
|
import (
|
|
"sync"
|
|
|
|
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
|
|
)
|
|
|
|
// mockFeeEstimator implements a mock fee estimator. It closely resembles
|
|
// lnwallet.StaticFeeEstimator with the addition that fees can be changed for
|
|
// testing purposes in a thread safe manner.
|
|
type mockFeeEstimator struct {
|
|
feePerKW chainfee.SatPerKWeight
|
|
|
|
relayFee chainfee.SatPerKWeight
|
|
|
|
blocksToFee map[uint32]chainfee.SatPerKWeight
|
|
|
|
// A closure that when set is used instead of the
|
|
// mockFeeEstimator.EstimateFeePerKW method.
|
|
estimateFeePerKW func(numBlocks uint32) (chainfee.SatPerKWeight, error)
|
|
|
|
lock sync.Mutex
|
|
}
|
|
|
|
func newMockFeeEstimator(feePerKW,
|
|
relayFee chainfee.SatPerKWeight) *mockFeeEstimator {
|
|
|
|
return &mockFeeEstimator{
|
|
feePerKW: feePerKW,
|
|
relayFee: relayFee,
|
|
blocksToFee: make(map[uint32]chainfee.SatPerKWeight),
|
|
}
|
|
}
|
|
|
|
func (e *mockFeeEstimator) updateFees(feePerKW,
|
|
relayFee chainfee.SatPerKWeight) {
|
|
|
|
e.lock.Lock()
|
|
defer e.lock.Unlock()
|
|
|
|
e.feePerKW = feePerKW
|
|
e.relayFee = relayFee
|
|
}
|
|
|
|
func (e *mockFeeEstimator) EstimateFeePerKW(numBlocks uint32) (
|
|
chainfee.SatPerKWeight, error) {
|
|
|
|
e.lock.Lock()
|
|
defer e.lock.Unlock()
|
|
|
|
if e.estimateFeePerKW != nil {
|
|
return e.estimateFeePerKW(numBlocks)
|
|
}
|
|
|
|
if fee, ok := e.blocksToFee[numBlocks]; ok {
|
|
return fee, nil
|
|
}
|
|
|
|
return e.feePerKW, nil
|
|
}
|
|
|
|
func (e *mockFeeEstimator) RelayFeePerKW() chainfee.SatPerKWeight {
|
|
e.lock.Lock()
|
|
defer e.lock.Unlock()
|
|
|
|
return e.relayFee
|
|
}
|
|
|
|
func (e *mockFeeEstimator) Start() error {
|
|
return nil
|
|
}
|
|
|
|
func (e *mockFeeEstimator) Stop() error {
|
|
return nil
|
|
}
|
|
|
|
var _ chainfee.Estimator = (*mockFeeEstimator)(nil)
|