lnwallet: add no cache option to web api estimator

This commit is contained in:
Joost Jager 2020-09-10 09:05:54 +02:00
parent 23cd2f40eb
commit 166be979dd
No known key found for this signature in database
GPG Key ID: A61B9D4C393C59C7
3 changed files with 23 additions and 2 deletions

View File

@ -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 {

View File

@ -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")

View File

@ -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)