Skip to content

Commit

Permalink
rpcserver: include fee calc. for psbt flow.
Browse files Browse the repository at this point in the history
Include the fee calculaltion for the psbt flow. Moreover include
fee rate testing in the itest environment. Additionally add an
itest for the OpenChannelSync rpc endpoint.
  • Loading branch information
ziggie1984 committed Jul 11, 2024
1 parent e2e761b commit e1e145b
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 21 deletions.
4 changes: 4 additions & 0 deletions itest/list_on_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ var allTestCases = []*lntest.TestCase{
Name: "wallet rescan address detection",
TestFunc: testRescanAddressDetection,
},
{
Name: "open channel sync funding",
TestFunc: testChannelOpeningSync,
},
{
Name: "basic funding flow with all input types",
TestFunc: testChannelFundingInputTypes,
Expand Down
61 changes: 59 additions & 2 deletions itest/lnd_funding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/lightningnetwork/lnd/lnrpc/signrpc"
"github.com/lightningnetwork/lnd/lntest"
"github.com/lightningnetwork/lnd/lntest/node"
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
"github.com/lightningnetwork/lnd/lnwire"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -955,11 +956,16 @@ func testBatchChanFunding(ht *lntest.HarnessTest) {
ht.EnsureConnected(alice, dave)
ht.EnsureConnected(alice, eve)

expectedFeeRate := chainfee.SatPerKWeight(2500)

// We verify that the channel opening uses the correct fee rate.
ht.SetFeeEstimateWithConf(expectedFeeRate, 3)

// Let's create our batch TX request. This first one should fail as we
// open a channel to Carol that is too small for her min chan size.
batchReq := &lnrpc.BatchOpenChannelRequest{
SatPerVbyte: 12,
MinConfs: 1,
TargetConf: 3,
MinConfs: 1,
Channels: []*lnrpc.BatchOpenChannel{{
NodePubkey: bob.PubKey[:],
LocalFundingAmount: 100_000,
Expand Down Expand Up @@ -1056,6 +1062,12 @@ func testBatchChanFunding(ht *lntest.HarnessTest) {
rawTx := ht.Miner.GetRawTransaction(txHash)
require.Len(ht, rawTx.MsgTx().TxOut, 5)

// Check the fee rate of the batch-opening transaction. We expect slight
// inaccuracies because of the DER signature fee estimation.
openingFeeRate := ht.CalculateTxFeeRate(rawTx.MsgTx())
require.InEpsilonf(ht, uint64(expectedFeeRate), uint64(openingFeeRate),
0.01, "want %v, got %v", expectedFeeRate, openingFeeRate)

// For calculating the change output index we use the formula for the
// sum of consecutive of integers (n(n+1)/2). All the channel point
// indexes are known, so we just calculate the difference to get the
Expand Down Expand Up @@ -1459,3 +1471,48 @@ func testChannelFundingWithUnstableUtxos(ht *lntest.HarnessTest) {
ht.CloseChannel(carol, chanPoint1)
ht.CloseChannel(carol, chanPoint4)
}

// testChannelOpeningSync tests that a channel is opened using the
// `OpenChannelSync` rpc endpoint. Most of the logic is similar to the
// `OpenChannel` rpc endpoint therefore we only test the basic channel opening.
func testChannelOpeningSync(ht *lntest.HarnessTest) {
alice, bob := ht.Alice, ht.Bob

chanAmt := funding.MaxBtcFundingAmount
expectedFeeRate := chainfee.SatPerKWeight(2500)

// We verify that the channel opening uses the correct fee rate.
ht.SetFeeEstimateWithConf(expectedFeeRate, 3)

// Open a pending channel between alice and bob. After this call the
// channel is still not confirmed but pending in the mempool.
chanPoint := ht.OpenChannelSync(alice, bob, lntest.OpenChannelParams{
Amt: chanAmt,
ConfTarget: fn.Some(int32(3)),
})

hash := chanPoint.GetFundingTxidBytes()

fundingTxID, err := chainhash.NewHash(hash)
require.NoError(ht, err)

// Mine 1 block to confirm the funding transaction.
block := ht.MineBlocksAndAssertNumTxes(1, 1)[0]

ht.Miner.AssertTxInBlock(block, fundingTxID)

tx := ht.Miner.GetRawTransaction(fundingTxID)
require.NotNil(ht, tx)

feerate := ht.CalculateTxFeeRate(tx.MsgTx())

// There are minor differences in the fee estimation because of the
// different sizes of the DER signature.
require.InEpsilonf(ht, uint64(expectedFeeRate), uint64(feerate),
0.01, "want %v, got %v", expectedFeeRate, feerate)

// Finally, immediately close the channel. This function will
// also block until the channel is closed and will additionally
// assert the relevant channel closing post conditions.
ht.CloseChannel(alice, chanPoint)
}
34 changes: 15 additions & 19 deletions rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -2172,28 +2172,24 @@ func (r *rpcServer) parseOpenChannelReq(in *lnrpc.OpenChannelRequest,

var feeRate chainfee.SatPerKWeight

// Skip estimating fee rate for PSBT funding.
if in.FundingShim == nil || in.FundingShim.GetPsbtShim() == nil {
// Keep the old behavior prior to 0.18.0 - when the user
// doesn't set fee rate or conf target, the default conf target
// of 6 is used.
targetConf := maybeUseDefaultConf(
in.SatPerByte, in.SatPerVbyte, uint32(in.TargetConf),
)

// Calculate an appropriate fee rate for this transaction.
feeRate, err = lnrpc.CalculateFeeRate(
uint64(in.SatPerByte), in.SatPerVbyte,
targetConf, r.server.cc.FeeEstimator,
)
if err != nil {
return nil, err
}
// NOTE: We also need to do the fee rate calculation for the psbt
// funding flow because the `batchfund` depends on it.
targetConf := maybeUseDefaultConf(
in.SatPerByte, in.SatPerVbyte, uint32(in.TargetConf),
)

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

rpcsLog.Debugf("[openchannel]: using fee of %v sat/kw for "+
"funding tx", int64(feeRate))

script, err := chancloser.ParseUpfrontShutdownAddress(
in.CloseAddress, r.cfg.ActiveNetParams.Params,
)
Expand Down
4 changes: 4 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4562,6 +4562,10 @@ func (s *server) OpenChannel(
req.Err <- err
return req.Updates, req.Err
}

srvrLog.Infof("Funding fee rate was not set, using defaut "+
"6 conf target: %v", feeRate)

req.FundingFeePerKw = feeRate
}

Expand Down

0 comments on commit e1e145b

Please sign in to comment.