Skip to content

Commit b7c59b3

Browse files
authored
Merge pull request #8896 from ziggie1984/batchopen-feerate-fix
Fix batchopen fee calculation
2 parents 8c0d786 + eb7818a commit b7c59b3

File tree

4 files changed

+41
-32
lines changed

4 files changed

+41
-32
lines changed

docs/release-notes/release-notes-0.18.3.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
* [Avoids duplicate wallet addresses being
3333
created](https://github.com/lightningnetwork/lnd/pull/8921) when multiple RPC
3434
calls are made concurrently.
35+
36+
* [Fixed a bug](https://github.com/lightningnetwork/lnd/pull/8896) that caused
37+
LND to use a default fee rate for the batch channel opening flow.
3538

3639
# New Features
3740
## Functional Enhancements
@@ -148,3 +151,4 @@
148151
* Oliver Gugger
149152
* Slyghtning
150153
* Yong Yu
154+
* Ziggie

itest/lnd_funding_test.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/lightningnetwork/lnd/lnrpc/signrpc"
2020
"github.com/lightningnetwork/lnd/lntest"
2121
"github.com/lightningnetwork/lnd/lntest/node"
22+
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
2223
"github.com/lightningnetwork/lnd/lnwire"
2324
"github.com/stretchr/testify/require"
2425
)
@@ -968,11 +969,16 @@ func testBatchChanFunding(ht *lntest.HarnessTest) {
968969
ht.EnsureConnected(alice, dave)
969970
ht.EnsureConnected(alice, eve)
970971

972+
expectedFeeRate := chainfee.SatPerKWeight(2500)
973+
974+
// We verify that the channel opening uses the correct fee rate.
975+
ht.SetFeeEstimateWithConf(expectedFeeRate, 3)
976+
971977
// Let's create our batch TX request. This first one should fail as we
972978
// open a channel to Carol that is too small for her min chan size.
973979
batchReq := &lnrpc.BatchOpenChannelRequest{
974-
SatPerVbyte: 12,
975-
MinConfs: 1,
980+
TargetConf: 3,
981+
MinConfs: 1,
976982
Channels: []*lnrpc.BatchOpenChannel{{
977983
NodePubkey: bob.PubKey[:],
978984
LocalFundingAmount: 100_000,
@@ -1069,6 +1075,12 @@ func testBatchChanFunding(ht *lntest.HarnessTest) {
10691075
rawTx := ht.GetRawTransaction(txHash)
10701076
require.Len(ht, rawTx.MsgTx().TxOut, 5)
10711077

1078+
// Check the fee rate of the batch-opening transaction. We expect slight
1079+
// inaccuracies because of the DER signature fee estimation.
1080+
openingFeeRate := ht.CalculateTxFeeRate(rawTx.MsgTx())
1081+
require.InEpsilonf(ht, uint64(expectedFeeRate), uint64(openingFeeRate),
1082+
0.01, "want %v, got %v", expectedFeeRate, openingFeeRate)
1083+
10721084
// For calculating the change output index we use the formula for the
10731085
// sum of consecutive of integers (n(n+1)/2). All the channel point
10741086
// indexes are known, so we just calculate the difference to get the

rpcserver.go

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2159,30 +2159,24 @@ func (r *rpcServer) parseOpenChannelReq(in *lnrpc.OpenChannelRequest,
21592159
return nil, fmt.Errorf("cannot open channel to self")
21602160
}
21612161

2162-
var feeRate chainfee.SatPerKWeight
2163-
2164-
// Skip estimating fee rate for PSBT funding.
2165-
if in.FundingShim == nil || in.FundingShim.GetPsbtShim() == nil {
2166-
// Keep the old behavior prior to 0.18.0 - when the user
2167-
// doesn't set fee rate or conf target, the default conf target
2168-
// of 6 is used.
2169-
targetConf := maybeUseDefaultConf(
2170-
in.SatPerByte, in.SatPerVbyte, uint32(in.TargetConf),
2171-
)
2172-
2173-
// Calculate an appropriate fee rate for this transaction.
2174-
feeRate, err = lnrpc.CalculateFeeRate(
2175-
uint64(in.SatPerByte), in.SatPerVbyte,
2176-
targetConf, r.server.cc.FeeEstimator,
2177-
)
2178-
if err != nil {
2179-
return nil, err
2180-
}
2162+
// NOTE: We also need to do the fee rate calculation for the psbt
2163+
// funding flow because the `batchfund` depends on it.
2164+
targetConf := maybeUseDefaultConf(
2165+
in.SatPerByte, in.SatPerVbyte, uint32(in.TargetConf),
2166+
)
21812167

2182-
rpcsLog.Debugf("[openchannel]: using fee of %v sat/kw for "+
2183-
"funding tx", int64(feeRate))
2168+
// Calculate an appropriate fee rate for this transaction.
2169+
feeRate, err := lnrpc.CalculateFeeRate(
2170+
uint64(in.SatPerByte), in.SatPerVbyte,
2171+
targetConf, r.server.cc.FeeEstimator,
2172+
)
2173+
if err != nil {
2174+
return nil, err
21842175
}
21852176

2177+
rpcsLog.Debugf("[openchannel]: using fee of %v sat/kw for "+
2178+
"funding tx", int64(feeRate))
2179+
21862180
script, err := chancloser.ParseUpfrontShutdownAddress(
21872181
in.CloseAddress, r.cfg.ActiveNetParams.Params,
21882182
)

server.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4578,16 +4578,15 @@ func (s *server) OpenChannel(
45784578
return req.Updates, req.Err
45794579
}
45804580

4581-
// If the fee rate wasn't specified, then we'll use a default
4582-
// confirmation target.
4581+
// If the fee rate wasn't specified at this point we fail the funding
4582+
// because of the missing fee rate information. The caller of the
4583+
// `OpenChannel` method needs to make sure that default values for the
4584+
// fee rate are set beforehand.
45834585
if req.FundingFeePerKw == 0 {
4584-
estimator := s.cc.FeeEstimator
4585-
feeRate, err := estimator.EstimateFeePerKW(6)
4586-
if err != nil {
4587-
req.Err <- err
4588-
return req.Updates, req.Err
4589-
}
4590-
req.FundingFeePerKw = feeRate
4586+
req.Err <- fmt.Errorf("no FundingFeePerKw specified for " +
4587+
"the channel opening transaction")
4588+
4589+
return req.Updates, req.Err
45914590
}
45924591

45934592
// Spawn a goroutine to send the funding workflow request to the funding

0 commit comments

Comments
 (0)