From 166be979dd96d51cc0bcfa79a5c61ca8ae2924e7 Mon Sep 17 00:00:00 2001 From: Joost Jager Date: Thu, 10 Sep 2020 09:05:54 +0200 Subject: [PATCH] lnwallet: add no cache option to web api estimator --- chainregistry.go | 1 + lnwallet/chainfee/estimator.go | 22 +++++++++++++++++++++- lnwallet/chainfee/estimator_test.go | 2 +- 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/chainregistry.go b/chainregistry.go index 23a2f0f4..15b421be 100644 --- a/chainregistry.go +++ b/chainregistry.go @@ -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 { diff --git a/lnwallet/chainfee/estimator.go b/lnwallet/chainfee/estimator.go index 13a04d76..341ee919 100644 --- a/lnwallet/chainfee/estimator.go +++ b/lnwallet/chainfee/estimator.go @@ -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") diff --git a/lnwallet/chainfee/estimator_test.go b/lnwallet/chainfee/estimator_test.go index d99424bc..f98ae80c 100644 --- a/lnwallet/chainfee/estimator_test.go +++ b/lnwallet/chainfee/estimator_test.go @@ -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)