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