github.com/decred/dcrlnd@v0.7.6/sweep/fee_estimator_mock_test.go (about)

     1  package sweep
     2  
     3  import (
     4  	"sync"
     5  
     6  	"github.com/decred/dcrlnd/lnwallet/chainfee"
     7  )
     8  
     9  // mockFeeEstimator implements a mock fee estimator. It closely resembles
    10  // lnwallet.StaticFeeEstimator with the addition that fees can be changed for
    11  // testing purposes in a thread safe manner.
    12  type mockFeeEstimator struct {
    13  	feePerKB chainfee.AtomPerKByte
    14  
    15  	relayFee chainfee.AtomPerKByte
    16  
    17  	blocksToFee map[uint32]chainfee.AtomPerKByte
    18  
    19  	// A closure that when set is used instead of the
    20  	// mockFeeEstimator.EstimateFeePerKW method.
    21  	estimateFeePerKW func(numBlocks uint32) (chainfee.AtomPerKByte, error)
    22  
    23  	lock sync.Mutex
    24  }
    25  
    26  func newMockFeeEstimator(feePerKB,
    27  	relayFee chainfee.AtomPerKByte) *mockFeeEstimator {
    28  
    29  	return &mockFeeEstimator{
    30  		feePerKB:    feePerKB,
    31  		relayFee:    relayFee,
    32  		blocksToFee: make(map[uint32]chainfee.AtomPerKByte),
    33  	}
    34  }
    35  
    36  func (e *mockFeeEstimator) updateFees(feePerKB,
    37  	relayFee chainfee.AtomPerKByte) {
    38  
    39  	e.lock.Lock()
    40  	defer e.lock.Unlock()
    41  
    42  	e.feePerKB = feePerKB
    43  	e.relayFee = relayFee
    44  }
    45  
    46  func (e *mockFeeEstimator) EstimateFeePerKB(numBlocks uint32) (
    47  	chainfee.AtomPerKByte, error) {
    48  
    49  	e.lock.Lock()
    50  	defer e.lock.Unlock()
    51  
    52  	if e.estimateFeePerKW != nil {
    53  		return e.estimateFeePerKW(numBlocks)
    54  	}
    55  
    56  	if fee, ok := e.blocksToFee[numBlocks]; ok {
    57  		return fee, nil
    58  	}
    59  
    60  	return e.feePerKB, nil
    61  }
    62  
    63  func (e *mockFeeEstimator) RelayFeePerKB() chainfee.AtomPerKByte {
    64  	e.lock.Lock()
    65  	defer e.lock.Unlock()
    66  
    67  	return e.relayFee
    68  }
    69  
    70  func (e *mockFeeEstimator) Start() error {
    71  	return nil
    72  }
    73  
    74  func (e *mockFeeEstimator) Stop() error {
    75  	return nil
    76  }
    77  
    78  var _ chainfee.Estimator = (*mockFeeEstimator)(nil)