Skip to content

Commit

Permalink
lntest+itest: export DeriveFundingShim
Browse files Browse the repository at this point in the history
  • Loading branch information
yyforyongyu committed Dec 20, 2024
1 parent 7d6b125 commit 5851746
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 144 deletions.
4 changes: 2 additions & 2 deletions itest/lnd_channel_backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -637,8 +637,8 @@ func runChanRestoreScenarioCommitTypes(ht *lntest.HarnessTest,
minerHeight := ht.CurrentHeight()
thawHeight := minerHeight + thawHeightDelta

fundingShim, _ = deriveFundingShim(
ht, dave, carol, crs.params.Amt, thawHeight, true, ct,
fundingShim, _ = ht.DeriveFundingShim(
dave, carol, crs.params.Amt, thawHeight, true, ct,
)
crs.params.FundingShim = fundingShim
}
Expand Down
137 changes: 8 additions & 129 deletions itest/lnd_funding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,13 @@ import (
"testing"
"time"

"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcd/wire"
"github.com/lightningnetwork/lnd/chainreg"
"github.com/lightningnetwork/lnd/fn/v2"
"github.com/lightningnetwork/lnd/funding"
"github.com/lightningnetwork/lnd/input"
"github.com/lightningnetwork/lnd/labels"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/lnrpc/signrpc"
"github.com/lightningnetwork/lnd/lntest"
"github.com/lightningnetwork/lnd/lntest/node"
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
Expand Down Expand Up @@ -583,8 +578,8 @@ func runExternalFundingScriptEnforced(ht *lntest.HarnessTest) {
// a transaction that will never be published.
const thawHeight uint32 = 10
const chanSize = funding.MaxBtcFundingAmount
fundingShim1, chanPoint1 := deriveFundingShim(
ht, carol, dave, chanSize, thawHeight, false, commitmentType,
fundingShim1, chanPoint1 := ht.DeriveFundingShim(
carol, dave, chanSize, thawHeight, false, commitmentType,
)
ht.OpenChannelAssertPending(
carol, dave, lntest.OpenChannelParams{
Expand All @@ -599,8 +594,8 @@ func runExternalFundingScriptEnforced(ht *lntest.HarnessTest) {
// externally funded, we should still be able to open another one. Let's
// do exactly that now. For this one we publish the transaction so we
// can mine it later.
fundingShim2, chanPoint2 := deriveFundingShim(
ht, carol, dave, chanSize, thawHeight, true, commitmentType,
fundingShim2, chanPoint2 := ht.DeriveFundingShim(
carol, dave, chanSize, thawHeight, true, commitmentType,
)

// At this point, we'll now carry out the normal basic channel funding
Expand Down Expand Up @@ -699,8 +694,8 @@ func runExternalFundingTaproot(ht *lntest.HarnessTest) {
// a transaction that will never be published.
const thawHeight uint32 = 10
const chanSize = funding.MaxBtcFundingAmount
fundingShim1, chanPoint1 := deriveFundingShim(
ht, carol, dave, chanSize, thawHeight, false, commitmentType,
fundingShim1, chanPoint1 := ht.DeriveFundingShim(
carol, dave, chanSize, thawHeight, false, commitmentType,
)
ht.OpenChannelAssertPending(carol, dave, lntest.OpenChannelParams{
Amt: chanSize,
Expand All @@ -715,8 +710,8 @@ func runExternalFundingTaproot(ht *lntest.HarnessTest) {
// externally funded, we should still be able to open another one. Let's
// do exactly that now. For this one we publish the transaction so we
// can mine it later.
fundingShim2, chanPoint2 := deriveFundingShim(
ht, carol, dave, chanSize, thawHeight, true, commitmentType,
fundingShim2, chanPoint2 := ht.DeriveFundingShim(
carol, dave, chanSize, thawHeight, true, commitmentType,
)

// At this point, we'll now carry out the normal basic channel funding
Expand Down Expand Up @@ -1163,122 +1158,6 @@ func ensurePolicy(ht *lntest.HarnessTest, alice, peer *node.HarnessNode,
require.EqualValues(ht, expectedFeeRate, alicePolicy.FeeRateMilliMsat)
}

// deriveFundingShim creates a channel funding shim by deriving the necessary
// keys on both sides.
func deriveFundingShim(ht *lntest.HarnessTest, carol, dave *node.HarnessNode,
chanSize btcutil.Amount, thawHeight uint32, publish bool,
commitType lnrpc.CommitmentType) (*lnrpc.FundingShim,
*lnrpc.ChannelPoint) {

keyLoc := &signrpc.KeyLocator{KeyFamily: 9999}
carolFundingKey := carol.RPC.DeriveKey(keyLoc)
daveFundingKey := dave.RPC.DeriveKey(keyLoc)

// Now that we have the multi-sig keys for each party, we can manually
// construct the funding transaction. We'll instruct the backend to
// immediately create and broadcast a transaction paying out an exact
// amount. Normally this would reside in the mempool, but we just
// confirm it now for simplicity.
var (
fundingOutput *wire.TxOut
musig2 bool
err error
)
if commitType == lnrpc.CommitmentType_SIMPLE_TAPROOT {
var carolKey, daveKey *btcec.PublicKey
carolKey, err = btcec.ParsePubKey(carolFundingKey.RawKeyBytes)
require.NoError(ht, err)
daveKey, err = btcec.ParsePubKey(daveFundingKey.RawKeyBytes)
require.NoError(ht, err)

_, fundingOutput, err = input.GenTaprootFundingScript(
carolKey, daveKey, int64(chanSize),
fn.None[chainhash.Hash](),
)
require.NoError(ht, err)

musig2 = true
} else {
_, fundingOutput, err = input.GenFundingPkScript(
carolFundingKey.RawKeyBytes, daveFundingKey.RawKeyBytes,
int64(chanSize),
)
require.NoError(ht, err)
}

var txid *chainhash.Hash
targetOutputs := []*wire.TxOut{fundingOutput}
if publish {
txid = ht.SendOutputsWithoutChange(targetOutputs, 5)
} else {
tx := ht.CreateTransaction(targetOutputs, 5)

txHash := tx.TxHash()
txid = &txHash
}

// At this point, we can being our external channel funding workflow.
// We'll start by generating a pending channel ID externally that will
// be used to track this new funding type.
pendingChanID := ht.Random32Bytes()

// Now that we have the pending channel ID, Dave (our responder) will
// register the intent to receive a new channel funding workflow using
// the pending channel ID.
chanPoint := &lnrpc.ChannelPoint{
FundingTxid: &lnrpc.ChannelPoint_FundingTxidBytes{
FundingTxidBytes: txid[:],
},
}
chanPointShim := &lnrpc.ChanPointShim{
Amt: int64(chanSize),
ChanPoint: chanPoint,
LocalKey: &lnrpc.KeyDescriptor{
RawKeyBytes: daveFundingKey.RawKeyBytes,
KeyLoc: &lnrpc.KeyLocator{
KeyFamily: daveFundingKey.KeyLoc.KeyFamily,
KeyIndex: daveFundingKey.KeyLoc.KeyIndex,
},
},
RemoteKey: carolFundingKey.RawKeyBytes,
PendingChanId: pendingChanID,
ThawHeight: thawHeight,
Musig2: musig2,
}
fundingShim := &lnrpc.FundingShim{
Shim: &lnrpc.FundingShim_ChanPointShim{
ChanPointShim: chanPointShim,
},
}
dave.RPC.FundingStateStep(&lnrpc.FundingTransitionMsg{
Trigger: &lnrpc.FundingTransitionMsg_ShimRegister{
ShimRegister: fundingShim,
},
})

// If we attempt to register the same shim (has the same pending chan
// ID), then we should get an error.
dave.RPC.FundingStateStepAssertErr(&lnrpc.FundingTransitionMsg{
Trigger: &lnrpc.FundingTransitionMsg_ShimRegister{
ShimRegister: fundingShim,
},
})

// We'll take the chan point shim we just registered for Dave (the
// responder), and swap the local/remote keys before we feed it in as
// Carol's funding shim as the initiator.
fundingShim.GetChanPointShim().LocalKey = &lnrpc.KeyDescriptor{
RawKeyBytes: carolFundingKey.RawKeyBytes,
KeyLoc: &lnrpc.KeyLocator{
KeyFamily: carolFundingKey.KeyLoc.KeyFamily,
KeyIndex: carolFundingKey.KeyLoc.KeyIndex,
},
}
fundingShim.GetChanPointShim().RemoteKey = daveFundingKey.RawKeyBytes

return fundingShim, chanPoint
}

// testChannelFundingWithUnstableUtxos tests channel openings with restricted
// utxo selection. Internal wallet utxos might be restricted due to another
// subsystems still using it therefore it would be unsecure to use them for
Expand Down
22 changes: 12 additions & 10 deletions itest/lnd_sweep_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1018,6 +1018,7 @@ func testSweepHTLCs(ht *lntest.HarnessTest) {
if len(closeTx.TxIn) != 1 {
closeTx = txns[2]
}
require.Len(ht, closeTx.TxIn, 1)

// We don't care the behavior of the anchor sweep in this test, so we
// mine the force close tx to trigger Bob's contractcourt to offer his
Expand Down Expand Up @@ -1215,18 +1216,19 @@ func testSweepCommitOutputAndAnchor(ht *lntest.HarnessTest) {
// config.
deadline := uint32(1000)

// For Alice, since her commit output is offered to the sweeper at
// CSV-1. With a deadline of 1000, her actual width of her fee func is
// CSV+1000-1.
// deadlineA is the deadline used for Alice, since her commit output is
// offered to the sweeper at CSV-1. With a deadline of 1000, her actual
// width of her fee func is CSV+1000-1. Given we are using a CSV of 2
// here, her fee func deadline then becomes 1001.
deadlineA := deadline + 1

// For Bob, the actual deadline used by the fee function will be one
// block off from the deadline configured as we require one block to be
// mined to trigger the sweep. In addition, when sweeping his to_local
// output from Alice's commit tx, because of CSV of 2, the starting
// height will be "force_close_height+2", which means when the sweep
// request is received by the sweeper, the actual deadline delta is
// "deadline+1".
// deadlineB is the deadline used for for Bob, the actual deadline used
// by the fee function will be one block off from the deadline
// configured as we require one block to be mined to trigger the sweep.
// In addition, when sweeping his to_local output from Alice's commit
// tx, because of CSV of 2, the starting height will be
// "force_close_height+2", which means when the sweep request is
// received by the sweeper, the actual deadline delta is "deadline+1".
deadlineB := deadline + 1

// startFeeRate is returned by the fee estimator in sat/kw. This
Expand Down
6 changes: 3 additions & 3 deletions lntest/harness.go
Original file line number Diff line number Diff line change
Expand Up @@ -2348,7 +2348,7 @@ func (h *HarnessTest) openChannelsForNodes(nodes []*node.HarnessNode,
// open channel params and return it.
minerHeight := h.CurrentHeight()
thawHeight := minerHeight + thawHeightDelta
fundingShim, _ := h.deriveFundingShim(
fundingShim, _ := h.DeriveFundingShim(
nodeA, nodeB, p.Amt, thawHeight, true, leasedType,
)

Expand Down Expand Up @@ -2417,9 +2417,9 @@ func (h *HarnessTest) openZeroConfChannelsForNodes(nodes []*node.HarnessNode,
return resp
}

// deriveFundingShim creates a channel funding shim by deriving the necessary
// DeriveFundingShim creates a channel funding shim by deriving the necessary
// keys on both sides.
func (h *HarnessTest) deriveFundingShim(alice, bob *node.HarnessNode,
func (h *HarnessTest) DeriveFundingShim(alice, bob *node.HarnessNode,
chanSize btcutil.Amount, thawHeight uint32, publish bool,
commitType lnrpc.CommitmentType) (*lnrpc.FundingShim,
*lnrpc.ChannelPoint) {
Expand Down

0 comments on commit 5851746

Please sign in to comment.