itest: break down basic funding flow tests

This commit is contained in:
yyforyongyu 2024-11-09 14:56:57 +08:00
parent c58fa01a66
commit c029f0a84f
No known key found for this signature in database
GPG Key ID: 9BCD95C4FF296868
2 changed files with 215 additions and 150 deletions

View File

@ -11,10 +11,6 @@ var allTestCases = []*lntest.TestCase{
Name: "update channel status", Name: "update channel status",
TestFunc: testUpdateChanStatus, TestFunc: testUpdateChanStatus,
}, },
{
Name: "basic funding flow",
TestFunc: testBasicChannelFunding,
},
{ {
Name: "external channel funding", Name: "external channel funding",
TestFunc: testExternalFundingChanPoint, TestFunc: testExternalFundingChanPoint,
@ -681,4 +677,5 @@ func init() {
allTestCases = append(allTestCases, zeroConfPolicyTestCases...) allTestCases = append(allTestCases, zeroConfPolicyTestCases...)
allTestCases = append(allTestCases, channelFeePolicyTestCases...) allTestCases = append(allTestCases, channelFeePolicyTestCases...)
allTestCases = append(allTestCases, walletImportAccountTestCases...) allTestCases = append(allTestCases, walletImportAccountTestCases...)
allTestCases = append(allTestCases, basicFundingTestCases...)
} }

View File

@ -20,40 +20,137 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
// testBasicChannelFunding performs a test exercising expected behavior from a // basicFundingTestCases defines the test cases for the basic funding test.
// basic funding workflow. The test creates a new channel between Alice and var basicFundingTestCases = []*lntest.TestCase{
// Bob, then immediately closes the channel after asserting some expected post {
// conditions. Finally, the chain itself is checked to ensure the closing Name: "basic funding flow static key remote",
// transaction was mined. TestFunc: testBasicChannelFundingStaticRemote,
func testBasicChannelFunding(ht *lntest.HarnessTest) { },
// Run through the test with combinations of all the different {
// commitment types. Name: "basic funding flow anchor",
allTypes := []lnrpc.CommitmentType{ TestFunc: testBasicChannelFundingAnchor,
},
{
Name: "basic funding flow simple taproot",
TestFunc: testBasicChannelFundingSimpleTaproot,
},
}
// allFundingTypes defines the channel types to test for the basic funding
// test.
var allFundingTypes = []lnrpc.CommitmentType{
lnrpc.CommitmentType_STATIC_REMOTE_KEY, lnrpc.CommitmentType_STATIC_REMOTE_KEY,
lnrpc.CommitmentType_ANCHORS, lnrpc.CommitmentType_ANCHORS,
lnrpc.CommitmentType_SIMPLE_TAPROOT, lnrpc.CommitmentType_SIMPLE_TAPROOT,
} }
// testFunding is a function closure that takes Carol and Dave's // testBasicChannelFundingStaticRemote performs a test exercising expected
// commitment types and test the funding flow. // behavior from a basic funding workflow. The test creates a new channel
testFunding := func(ht *lntest.HarnessTest, carolCommitType, // between Carol and Dave, with Carol using the static remote key commitment
// type, and Dave using allFundingTypes.
func testBasicChannelFundingStaticRemote(ht *lntest.HarnessTest) {
carolCommitType := lnrpc.CommitmentType_STATIC_REMOTE_KEY
// We'll test all possible combinations of the feature bit presence
// that both nodes can signal for this new channel type. We'll make a
// new Carol+Dave for each test instance as well.
for _, daveCommitType := range allFundingTypes {
cc := carolCommitType
dc := daveCommitType
testName := fmt.Sprintf(
"carol_commit=%v,dave_commit=%v", cc, dc,
)
success := ht.Run(testName, func(t *testing.T) {
st := ht.Subtest(t)
runBasicFundingTest(st, cc, dc)
})
if !success {
break
}
}
}
// testBasicChannelFundingAnchor performs a test exercising expected behavior
// from a basic funding workflow. The test creates a new channel between Carol
// and Dave, with Carol using the anchor commitment type, and Dave using
// allFundingTypes.
func testBasicChannelFundingAnchor(ht *lntest.HarnessTest) {
carolCommitType := lnrpc.CommitmentType_ANCHORS
// We'll test all possible combinations of the feature bit presence
// that both nodes can signal for this new channel type. We'll make a
// new Carol+Dave for each test instance as well.
for _, daveCommitType := range allFundingTypes {
cc := carolCommitType
dc := daveCommitType
testName := fmt.Sprintf(
"carol_commit=%v,dave_commit=%v", cc, dc,
)
success := ht.Run(testName, func(t *testing.T) {
st := ht.Subtest(t)
runBasicFundingTest(st, cc, dc)
})
if !success {
break
}
}
}
// testBasicChannelFundingSimpleTaproot performs a test exercising expected
// behavior from a basic funding workflow. The test creates a new channel
// between Carol and Dave, with Carol using the simple taproot commitment type,
// and Dave using allFundingTypes.
func testBasicChannelFundingSimpleTaproot(ht *lntest.HarnessTest) {
carolCommitType := lnrpc.CommitmentType_SIMPLE_TAPROOT
// We'll test all possible combinations of the feature bit presence
// that both nodes can signal for this new channel type. We'll make a
// new Carol+Dave for each test instance as well.
for _, daveCommitType := range allFundingTypes {
cc := carolCommitType
dc := daveCommitType
testName := fmt.Sprintf(
"carol_commit=%v,dave_commit=%v", cc, dc,
)
success := ht.Run(testName, func(t *testing.T) {
st := ht.Subtest(t)
runBasicFundingTest(st, cc, dc)
})
if !success {
break
}
}
}
// runBasicFundingTest is a helper function that takes Carol and Dave's
// commitment types and test the funding flow.
func runBasicFundingTest(ht *lntest.HarnessTest, carolCommitType,
daveCommitType lnrpc.CommitmentType) { daveCommitType lnrpc.CommitmentType) {
// Based on the current tweak variable for Carol, we'll // Based on the current tweak variable for Carol, we'll preferentially
// preferentially signal the legacy commitment format. We do // signal the legacy commitment format. We do the same for Dave
// the same for Dave shortly below. // shortly below.
carolArgs := lntest.NodeArgsForCommitType(carolCommitType) carolArgs := lntest.NodeArgsForCommitType(carolCommitType)
carol := ht.NewNode("Carol", carolArgs) carol := ht.NewNode("Carol", carolArgs)
// Each time, we'll send Carol a new set of coins in order to // Each time, we'll send Carol a new set of coins in order to fund the
// fund the channel. // channel.
ht.FundCoins(btcutil.SatoshiPerBitcoin, carol) ht.FundCoins(btcutil.SatoshiPerBitcoin, carol)
daveArgs := lntest.NodeArgsForCommitType(daveCommitType) daveArgs := lntest.NodeArgsForCommitType(daveCommitType)
dave := ht.NewNode("Dave", daveArgs) dave := ht.NewNode("Dave", daveArgs)
// Before we start the test, we'll ensure both sides are // Before we start the test, we'll ensure both sides are connected to
// connected to the funding flow can properly be executed. // the funding flow can properly be executed.
ht.EnsureConnected(carol, dave) ht.EnsureConnected(carol, dave)
var privateChan bool var privateChan bool
@ -66,9 +163,8 @@ func testBasicChannelFunding(ht *lntest.HarnessTest) {
privateChan = true privateChan = true
} }
// If carol wants taproot, but dave wants something // If carol wants taproot, but dave wants something else, then we'll
// else, then we'll assert that the channel negotiation // assert that the channel negotiation attempt fails.
// attempt fails.
if carolCommitType == lnrpc.CommitmentType_SIMPLE_TAPROOT && if carolCommitType == lnrpc.CommitmentType_SIMPLE_TAPROOT &&
daveCommitType != lnrpc.CommitmentType_SIMPLE_TAPROOT { daveCommitType != lnrpc.CommitmentType_SIMPLE_TAPROOT {
@ -90,15 +186,14 @@ func testBasicChannelFunding(ht *lntest.HarnessTest) {
ht, carol, dave, nil, privateChan, &carolCommitType, ht, carol, dave, nil, privateChan, &carolCommitType,
) )
// Both nodes should report the same commitment // Both nodes should report the same commitment type.
// type.
chansCommitType := carolChan.CommitmentType chansCommitType := carolChan.CommitmentType
require.Equal(ht, chansCommitType, daveChan.CommitmentType, require.Equal(ht, chansCommitType, daveChan.CommitmentType,
"commit types don't match") "commit types don't match")
// Now check that the commitment type reported by both nodes is // Now check that the commitment type reported by both nodes is what we
// what we expect. It will be the minimum of the two nodes' // expect. It will be the minimum of the two nodes' preference, in the
// preference, in the order Legacy, Tweakless, Anchors. // order Legacy, Tweakless, Anchors.
expType := carolCommitType expType := carolCommitType
switch daveCommitType { switch daveCommitType {
@ -107,15 +202,14 @@ func testBasicChannelFunding(ht *lntest.HarnessTest) {
// Dave supports anchors, type will be what Carol supports. // Dave supports anchors, type will be what Carol supports.
case lnrpc.CommitmentType_ANCHORS: case lnrpc.CommitmentType_ANCHORS:
// However if Alice wants taproot chans, then we // However if Alice wants taproot chans, then we downgrade to
// downgrade to anchors as this is still using implicit // anchors as this is still using implicit negotiation.
// negotiation.
if expType == lnrpc.CommitmentType_SIMPLE_TAPROOT { if expType == lnrpc.CommitmentType_SIMPLE_TAPROOT {
expType = lnrpc.CommitmentType_ANCHORS expType = lnrpc.CommitmentType_ANCHORS
} }
// Dave only supports tweakless, channel will be downgraded to // Dave only supports tweakless, channel will be downgraded to this
// this type if Carol supports anchors. // type if Carol supports anchors.
case lnrpc.CommitmentType_STATIC_REMOTE_KEY: case lnrpc.CommitmentType_STATIC_REMOTE_KEY:
switch expType { switch expType {
case lnrpc.CommitmentType_ANCHORS: case lnrpc.CommitmentType_ANCHORS:
@ -124,8 +218,8 @@ func testBasicChannelFunding(ht *lntest.HarnessTest) {
expType = lnrpc.CommitmentType_STATIC_REMOTE_KEY expType = lnrpc.CommitmentType_STATIC_REMOTE_KEY
} }
// Dave only supports legacy type, channel will be downgraded // Dave only supports legacy type, channel will be downgraded to this
// to this type. // type.
case lnrpc.CommitmentType_LEGACY: case lnrpc.CommitmentType_LEGACY:
expType = lnrpc.CommitmentType_LEGACY expType = lnrpc.CommitmentType_LEGACY
@ -139,7 +233,7 @@ func testBasicChannelFunding(ht *lntest.HarnessTest) {
chansCommitType == lnrpc.CommitmentType_ANCHORS: chansCommitType == lnrpc.CommitmentType_ANCHORS:
case expType == lnrpc.CommitmentType_STATIC_REMOTE_KEY && case expType == lnrpc.CommitmentType_STATIC_REMOTE_KEY &&
chansCommitType == lnrpc.CommitmentType_STATIC_REMOTE_KEY: //nolint:ll chansCommitType == lnrpc.CommitmentType_STATIC_REMOTE_KEY:
case expType == lnrpc.CommitmentType_LEGACY && case expType == lnrpc.CommitmentType_LEGACY &&
chansCommitType == lnrpc.CommitmentType_LEGACY: chansCommitType == lnrpc.CommitmentType_LEGACY:
@ -148,34 +242,8 @@ func testBasicChannelFunding(ht *lntest.HarnessTest) {
chansCommitType == lnrpc.CommitmentType_SIMPLE_TAPROOT: chansCommitType == lnrpc.CommitmentType_SIMPLE_TAPROOT:
default: default:
ht.Fatalf("expected nodes to signal "+ ht.Fatalf("expected nodes to signal commit type %v, instead "+
"commit type %v, instead got "+ "got %v", expType, chansCommitType)
"%v", expType, chansCommitType)
}
}
test:
// We'll test all possible combinations of the feature bit presence
// that both nodes can signal for this new channel type. We'll make a
// new Carol+Dave for each test instance as well.
for _, carolCommitType := range allTypes {
for _, daveCommitType := range allTypes {
cc := carolCommitType
dc := daveCommitType
testName := fmt.Sprintf(
"carol_commit=%v,dave_commit=%v", cc, dc,
)
success := ht.Run(testName, func(t *testing.T) {
st := ht.Subtest(t)
testFunding(st, cc, dc)
})
if !success {
break test
}
}
} }
} }