lnwallet: add no cache option to web api estimator
This commit is contained in:
parent
23cd2f40eb
commit
166be979dd
@ -257,6 +257,7 @@ func newChainControlFromConfig(cfg *Config, localDB, remoteDB *channeldb.DB,
|
||||
chainfee.SparseConfFeeSource{
|
||||
URL: cfg.NeutrinoMode.FeeURL,
|
||||
},
|
||||
false,
|
||||
)
|
||||
|
||||
if err := estimator.Start(); err != nil {
|
||||
|
@ -540,16 +540,21 @@ type WebAPIEstimator struct {
|
||||
feesMtx sync.Mutex
|
||||
feeByBlockTarget map[uint32]uint32
|
||||
|
||||
// noCache determines whether the web estimator should cache fee
|
||||
// estimates.
|
||||
noCache bool
|
||||
|
||||
quit chan struct{}
|
||||
wg sync.WaitGroup
|
||||
}
|
||||
|
||||
// NewWebAPIEstimator creates a new WebAPIEstimator from a given URL and a
|
||||
// fallback default fee. The fees are updated whenever a new block is mined.
|
||||
func NewWebAPIEstimator(api WebAPIFeeSource) *WebAPIEstimator {
|
||||
func NewWebAPIEstimator(api WebAPIFeeSource, noCache bool) *WebAPIEstimator {
|
||||
return &WebAPIEstimator{
|
||||
apiSource: api,
|
||||
feeByBlockTarget: make(map[uint32]uint32),
|
||||
noCache: noCache,
|
||||
quit: make(chan struct{}),
|
||||
}
|
||||
}
|
||||
@ -566,6 +571,11 @@ func (w *WebAPIEstimator) EstimateFeePerKW(numBlocks uint32) (SatPerKWeight, err
|
||||
"accepted is %v", numBlocks, minBlockTarget)
|
||||
}
|
||||
|
||||
// Get fee estimates now if we don't refresh periodically.
|
||||
if w.noCache {
|
||||
w.updateFeeEstimates()
|
||||
}
|
||||
|
||||
feePerKb, err := w.getCachedFee(numBlocks)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
@ -589,6 +599,11 @@ func (w *WebAPIEstimator) EstimateFeePerKW(numBlocks uint32) (SatPerKWeight, err
|
||||
//
|
||||
// NOTE: This method is part of the Estimator interface.
|
||||
func (w *WebAPIEstimator) Start() error {
|
||||
// No update loop is needed when we don't cache.
|
||||
if w.noCache {
|
||||
return nil
|
||||
}
|
||||
|
||||
var err error
|
||||
w.started.Do(func() {
|
||||
log.Infof("Starting web API fee estimator")
|
||||
@ -608,6 +623,11 @@ func (w *WebAPIEstimator) Start() error {
|
||||
//
|
||||
// NOTE: This method is part of the Estimator interface.
|
||||
func (w *WebAPIEstimator) Stop() error {
|
||||
// Update loop is not running when we don't cache.
|
||||
if w.noCache {
|
||||
return nil
|
||||
}
|
||||
|
||||
w.stopped.Do(func() {
|
||||
log.Infof("Stopping web API fee estimator")
|
||||
|
||||
|
@ -192,7 +192,7 @@ func TestWebAPIFeeEstimator(t *testing.T) {
|
||||
fees: testFees,
|
||||
}
|
||||
|
||||
estimator := NewWebAPIEstimator(feeSource)
|
||||
estimator := NewWebAPIEstimator(feeSource, false)
|
||||
|
||||
// Test that requesting a fee when no fees have been cached fails.
|
||||
_, err := estimator.EstimateFeePerKW(5)
|
||||
|
Loading…
Reference in New Issue
Block a user