Skip to content

Commit d0a7765

Browse files
committed
rpcserver: include fee calc. for psbt flow.
Include the fee calculaltion for the psbt flow. Moreover include fee rate testing in the itest environment.
1 parent 6f37db3 commit d0a7765

File tree

2 files changed

+29
-23
lines changed

2 files changed

+29
-23
lines changed

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
@@ -2158,30 +2158,24 @@ func (r *rpcServer) parseOpenChannelReq(in *lnrpc.OpenChannelRequest,
21582158
return nil, fmt.Errorf("cannot open channel to self")
21592159
}
21602160

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

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

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

0 commit comments

Comments
 (0)