lnwallet: prevent static fee estimator fees from being modified

Modifying the static fees is not thread safe. In this commit the fees
are made immutable.
This commit is contained in:
Joost Jager committed 2018-12-18 10:50:05 +01:00
1 parent 423dd8ab9b
commit 91f3df07e4
12 files changed
+41 -32

No files matched your search

+1 -1
View File
@@ -1131,7 +1131,7 @@ func TestHTLCSigNumber(t *testing.T) {
}
// Calculate two values that will be below and above Bob's dust limit.
estimator := &StaticFeeEstimator{FeePerKW: 6000}
estimator := NewStaticFeeEstimator(6000, 0)
feePerKw, err := estimator.EstimateFeePerKW(1)
if err != nil {
t.Fatalf("unable to get fee: %v", err)
+18 -7
View File
@@ -68,22 +68,33 @@ type FeeEstimator interface {
// StaticFeeEstimator will return a static value for all fee calculation
// requests. It is designed to be replaced by a proper fee calculation
// implementation.
// implementation. The fees are not accessible directly, because changing them
// would not be thread safe.
type StaticFeeEstimator struct {
// FeePerKW is the static fee rate in satoshis-per-vbyte that will be
// feePerKW is the static fee rate in satoshis-per-vbyte that will be
// returned by this fee estimator.
FeePerKW SatPerKWeight
feePerKW SatPerKWeight
// RelayFee is the minimum fee rate required for transactions to be
// relayFee is the minimum fee rate required for transactions to be
// relayed.
RelayFee SatPerKWeight
relayFee SatPerKWeight
}
// NewStaticFeeEstimator returns a new static fee estimator instance.
func NewStaticFeeEstimator(feePerKW,
relayFee SatPerKWeight) *StaticFeeEstimator {
return &StaticFeeEstimator{
feePerKW: feePerKW,
relayFee: relayFee,
}
}
// EstimateFeePerKW will return a static value for fee calculations.
//
// NOTE: This method is part of the FeeEstimator interface.
func (e StaticFeeEstimator) EstimateFeePerKW(numBlocks uint32) (SatPerKWeight, error) {
return e.FeePerKW, nil
return e.feePerKW, nil
}
// RelayFeePerKW returns the minimum fee rate required for transactions to be
@@ -91,7 +102,7 @@ func (e StaticFeeEstimator) EstimateFeePerKW(numBlocks uint32) (SatPerKWeight, e
//
// NOTE: This method is part of the FeeEstimator interface.
func (e StaticFeeEstimator) RelayFeePerKW() SatPerKWeight {
return e.RelayFee
return e.relayFee
}
// Start signals the FeeEstimator to start any processes or goroutines
+1 -3
View File
@@ -74,9 +74,7 @@ func TestStaticFeeEstimator(t *testing.T) {
const feePerKw = lnwallet.FeePerKwFloor
feeEstimator := &lnwallet.StaticFeeEstimator{
FeePerKW: feePerKw,
}
feeEstimator := lnwallet.NewStaticFeeEstimator(feePerKw, 0)
if err := feeEstimator.Start(); err != nil {
t.Fatalf("unable to start fee estimator: %v", err)
}
+2 -2
View File
@@ -368,7 +368,7 @@ func createTestWallet(tempTestDir string, miningNode *rpctest.Harness,
WalletController: wc,
Signer: signer,
ChainIO: bio,
FeeEstimator: lnwallet.StaticFeeEstimator{FeePerKW: 2500},
FeeEstimator: lnwallet.NewStaticFeeEstimator(2500, 0),
DefaultConstraints: channeldb.ChannelConstraints{
DustLimit: 500,
MaxPendingAmount: lnwire.NewMSatFromSatoshis(btcutil.SatoshiPerBitcoin) * 100,
@@ -2440,7 +2440,7 @@ func runTests(t *testing.T, walletDriver *lnwallet.WalletDriver,
}
case "neutrino":
feeEstimator = lnwallet.StaticFeeEstimator{FeePerKW: 62500}
feeEstimator = lnwallet.NewStaticFeeEstimator(62500, 0)
// Set some package-level variable to speed up
// operation for tests.
+1 -1
View File
@@ -229,7 +229,7 @@ func CreateTestChannels() (*LightningChannel, *LightningChannel, func(), error)
return nil, nil, nil, err
}
estimator := &StaticFeeEstimator{FeePerKW: 6000}
estimator := NewStaticFeeEstimator(6000, 0)
feePerKw, err := estimator.EstimateFeePerKW(1)
if err != nil {
return nil, nil, nil, err