2018-12-07 09:06:36 +01:00
|
|
|
package sweep
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
2018-12-24 22:29:55 +01:00
|
|
|
|
2019-10-31 03:43:05 +01:00
|
|
|
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
|
2018-12-07 09:06:36 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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 {
|
2019-10-31 03:43:05 +01:00
|
|
|
feePerKW chainfee.SatPerKWeight
|
2018-12-07 09:06:36 +01:00
|
|
|
|
2019-10-31 03:43:05 +01:00
|
|
|
relayFee chainfee.SatPerKWeight
|
2018-12-07 09:06:36 +01:00
|
|
|
|
2019-10-31 03:43:05 +01:00
|
|
|
blocksToFee map[uint32]chainfee.SatPerKWeight
|
2018-12-24 22:29:55 +01:00
|
|
|
|
2019-05-02 01:06:19 +02:00
|
|
|
// A closure that when set is used instead of the
|
|
|
|
// mockFeeEstimator.EstimateFeePerKW method.
|
2019-10-31 03:43:05 +01:00
|
|
|
estimateFeePerKW func(numBlocks uint32) (chainfee.SatPerKWeight, error)
|
2019-05-02 01:06:19 +02:00
|
|
|
|
2018-12-07 09:06:36 +01:00
|
|
|
lock sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func newMockFeeEstimator(feePerKW,
|
2019-10-31 03:43:05 +01:00
|
|
|
relayFee chainfee.SatPerKWeight) *mockFeeEstimator {
|
2018-12-07 09:06:36 +01:00
|
|
|
|
|
|
|
return &mockFeeEstimator{
|
2018-12-24 22:29:55 +01:00
|
|
|
feePerKW: feePerKW,
|
|
|
|
relayFee: relayFee,
|
2019-10-31 03:43:05 +01:00
|
|
|
blocksToFee: make(map[uint32]chainfee.SatPerKWeight),
|
2018-12-07 09:06:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *mockFeeEstimator) updateFees(feePerKW,
|
2019-10-31 03:43:05 +01:00
|
|
|
relayFee chainfee.SatPerKWeight) {
|
2018-12-07 09:06:36 +01:00
|
|
|
|
|
|
|
e.lock.Lock()
|
|
|
|
defer e.lock.Unlock()
|
|
|
|
|
|
|
|
e.feePerKW = feePerKW
|
|
|
|
e.relayFee = relayFee
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *mockFeeEstimator) EstimateFeePerKW(numBlocks uint32) (
|
2019-10-31 03:43:05 +01:00
|
|
|
chainfee.SatPerKWeight, error) {
|
2018-12-07 09:06:36 +01:00
|
|
|
|
|
|
|
e.lock.Lock()
|
|
|
|
defer e.lock.Unlock()
|
|
|
|
|
2019-05-02 01:06:19 +02:00
|
|
|
if e.estimateFeePerKW != nil {
|
|
|
|
return e.estimateFeePerKW(numBlocks)
|
|
|
|
}
|
|
|
|
|
2018-12-24 22:29:55 +01:00
|
|
|
if fee, ok := e.blocksToFee[numBlocks]; ok {
|
|
|
|
return fee, nil
|
|
|
|
}
|
|
|
|
|
2018-12-07 09:06:36 +01:00
|
|
|
return e.feePerKW, nil
|
|
|
|
}
|
|
|
|
|
2019-10-31 03:43:05 +01:00
|
|
|
func (e *mockFeeEstimator) RelayFeePerKW() chainfee.SatPerKWeight {
|
2018-12-07 09:06:36 +01:00
|
|
|
e.lock.Lock()
|
|
|
|
defer e.lock.Unlock()
|
|
|
|
|
|
|
|
return e.relayFee
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *mockFeeEstimator) Start() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *mockFeeEstimator) Stop() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-10-31 03:43:05 +01:00
|
|
|
var _ chainfee.Estimator = (*mockFeeEstimator)(nil)
|