itest: break down channel backup restore tests

This commit is contained in:
yyforyongyu 2024-11-09 01:12:49 +08:00
parent 7b1427a565
commit 31aada65a4
No known key found for this signature in database
GPG Key ID: 9BCD95C4FF296868
2 changed files with 173 additions and 192 deletions

View File

@ -19,10 +19,6 @@ var allTestCases = []*lntest.TestCase{
Name: "external channel funding", Name: "external channel funding",
TestFunc: testExternalFundingChanPoint, TestFunc: testExternalFundingChanPoint,
}, },
{
Name: "channel backup restore basic",
TestFunc: testChannelBackupRestoreBasic,
},
{ {
Name: "channel backup restore unconfirmed", Name: "channel backup restore unconfirmed",
TestFunc: testChannelBackupRestoreUnconfirmed, TestFunc: testChannelBackupRestoreUnconfirmed,

View File

@ -85,6 +85,26 @@ var channelRestoreTestCases = []*lntest.TestCase{
) )
}, },
}, },
{
Name: "channel backup restore from rpc",
TestFunc: testChannelBackupRestoreFromRPC,
},
{
Name: "channel backup restore from file",
TestFunc: testChannelBackupRestoreFromFile,
},
{
Name: "channel backup restore during creation",
TestFunc: testChannelBackupRestoreDuringCreation,
},
{
Name: "channel backup restore during unlock",
TestFunc: testChannelBackupRestoreDuringUnlock,
},
{
Name: "channel backup restore twice",
TestFunc: testChannelBackupRestoreTwice,
},
} }
type ( type (
@ -298,89 +318,76 @@ func (c *chanRestoreScenario) testScenario(ht *lntest.HarnessTest,
) )
} }
// testChannelBackupRestore tests that we're able to recover from, and initiate // testChannelBackupRestoreFromRPC tests that we're able to recover from, and
// the DLP protocol via: the RPC restore command, restoring on unlock, and // initiate the DLP protocol via the RPC restore command.
// restoring from initial wallet creation. We'll also alternate between func testChannelBackupRestoreFromRPC(ht *lntest.HarnessTest) {
// restoring form the on disk file, and restoring from the exported RPC command // Restore from backups obtained via the RPC interface. Dave was the
// as well. // initiator, of the non-advertised channel.
func testChannelBackupRestoreBasic(ht *lntest.HarnessTest) { restoreMethod := func(st *lntest.HarnessTest, oldNode *node.HarnessNode,
var testCases = []struct { backupFilePath string, password []byte,
name string
restoreMethod restoreMethodType
}{
// Restore from backups obtained via the RPC interface. Dave
// was the initiator, of the non-advertised channel.
{
name: "restore from RPC backup",
restoreMethod: func(st *lntest.HarnessTest,
oldNode *node.HarnessNode,
backupFilePath string,
password []byte,
mnemonic []string) nodeRestorer { mnemonic []string) nodeRestorer {
// For this restoration method, we'll grab the // For this restoration method, we'll grab the current
// current multi-channel backup from the old // multi-channel backup from the old node, and use it to
// node, and use it to restore a new node // restore a new node within the closure.
// within the closure.
chanBackup := oldNode.RPC.ExportAllChanBackups() chanBackup := oldNode.RPC.ExportAllChanBackups()
multi := chanBackup.MultiChanBackup. multi := chanBackup.MultiChanBackup.
MultiChanBackup MultiChanBackup
// In our nodeRestorer function, we'll restore // In our nodeRestorer function, we'll restore the node from
// the node from seed, then manually recover // seed, then manually recover the channel backup.
// the channel backup.
return chanRestoreViaRPC( return chanRestoreViaRPC(
st, password, mnemonic, multi, st, password, mnemonic, multi,
) )
}, }
},
// Restore the backup from the on-disk file, using the RPC runChanRestoreScenarioBasic(ht, restoreMethod)
// interface. }
{
name: "restore from backup file", // testChannelBackupRestoreFromFile tests that we're able to recover from, and
restoreMethod: func(st *lntest.HarnessTest, // initiate the DLP protocol via the backup file.
oldNode *node.HarnessNode, func testChannelBackupRestoreFromFile(ht *lntest.HarnessTest) {
backupFilePath string, // Restore the backup from the on-disk file, using the RPC interface.
password []byte, restoreMethod := func(st *lntest.HarnessTest, oldNode *node.HarnessNode,
backupFilePath string, password []byte,
mnemonic []string) nodeRestorer { mnemonic []string) nodeRestorer {
// Read the entire Multi backup stored within // Read the entire Multi backup stored within this node's
// this node's channel.backup file. // channel.backup file.
multi, err := os.ReadFile(backupFilePath) multi, err := os.ReadFile(backupFilePath)
require.NoError(st, err) require.NoError(st, err)
// Now that we have Dave's backup file, we'll // Now that we have Dave's backup file, we'll create a new
// create a new nodeRestorer that will restore // nodeRestorer that will restore using the on-disk
// using the on-disk channel.backup. // channel.backup.
return chanRestoreViaRPC( return chanRestoreViaRPC(
st, password, mnemonic, multi, st, password, mnemonic, multi,
) )
}, }
},
// Restore the backup as part of node initialization with the runChanRestoreScenarioBasic(ht, restoreMethod)
// prior mnemonic and new backup seed. }
{
name: "restore during creation", // testChannelBackupRestoreFromFile tests that we're able to recover from, and
restoreMethod: func(st *lntest.HarnessTest, // initiate the DLP protocol via restoring from initial wallet creation.
oldNode *node.HarnessNode, func testChannelBackupRestoreDuringCreation(ht *lntest.HarnessTest) {
backupFilePath string, // Restore the backup as part of node initialization with the prior
password []byte, // mnemonic and new backup seed.
restoreMethod := func(st *lntest.HarnessTest, oldNode *node.HarnessNode,
backupFilePath string, password []byte,
mnemonic []string) nodeRestorer { mnemonic []string) nodeRestorer {
// First, fetch the current backup state as is, // First, fetch the current backup state as is, to obtain our
// to obtain our latest Multi. // latest Multi.
chanBackup := oldNode.RPC.ExportAllChanBackups() chanBackup := oldNode.RPC.ExportAllChanBackups()
backupSnapshot := &lnrpc.ChanBackupSnapshot{ backupSnapshot := &lnrpc.ChanBackupSnapshot{
MultiChanBackup: chanBackup. MultiChanBackup: chanBackup.
MultiChanBackup, MultiChanBackup,
} }
// Create a new nodeRestorer that will restore // Create a new nodeRestorer that will restore the node using
// the node using the Multi backup we just // the Multi backup we just obtained above.
// obtained above.
return func() *node.HarnessNode { return func() *node.HarnessNode {
return st.RestoreNodeWithSeed( return st.RestoreNodeWithSeed(
"dave", nil, password, mnemonic, "dave", nil, password, mnemonic,
@ -388,31 +395,31 @@ func testChannelBackupRestoreBasic(ht *lntest.HarnessTest) {
backupSnapshot, backupSnapshot,
) )
} }
}, }
},
// Restore the backup once the node has already been runChanRestoreScenarioBasic(ht, restoreMethod)
// re-created, using the Unlock call. }
{
name: "restore during unlock", // testChannelBackupRestoreFromFile tests that we're able to recover from, and
restoreMethod: func(st *lntest.HarnessTest, // initiate the DLP protocol via restoring on unlock.
oldNode *node.HarnessNode, func testChannelBackupRestoreDuringUnlock(ht *lntest.HarnessTest) {
backupFilePath string, // Restore the backup once the node has already been re-created, using
password []byte, // the Unlock call.
restoreMethod := func(st *lntest.HarnessTest, oldNode *node.HarnessNode,
backupFilePath string, password []byte,
mnemonic []string) nodeRestorer { mnemonic []string) nodeRestorer {
// First, fetch the current backup state as is, // First, fetch the current backup state as is, to obtain our
// to obtain our latest Multi. // latest Multi.
chanBackup := oldNode.RPC.ExportAllChanBackups() chanBackup := oldNode.RPC.ExportAllChanBackups()
backupSnapshot := &lnrpc.ChanBackupSnapshot{ backupSnapshot := &lnrpc.ChanBackupSnapshot{
MultiChanBackup: chanBackup. MultiChanBackup: chanBackup.
MultiChanBackup, MultiChanBackup,
} }
// Create a new nodeRestorer that will restore // Create a new nodeRestorer that will restore the node with
// the node with its seed, but no channel // its seed, but no channel backup, shutdown this initialized
// backup, shutdown this initialized node, then // node, then restart it again using Unlock.
// restart it again using Unlock.
return func() *node.HarnessNode { return func() *node.HarnessNode {
newNode := st.RestoreNodeWithSeed( newNode := st.RestoreNodeWithSeed(
"dave", nil, password, mnemonic, "dave", nil, password, mnemonic,
@ -424,29 +431,29 @@ func testChannelBackupRestoreBasic(ht *lntest.HarnessTest) {
return newNode return newNode
} }
}, }
},
// Restore the backup from the on-disk file a second time to runChanRestoreScenarioBasic(ht, restoreMethod)
// make sure imports can be canceled and later resumed. }
{
name: "restore from backup file twice", // testChannelBackupRestoreTwice tests that we're able to recover from, and
restoreMethod: func(st *lntest.HarnessTest, // initiate the DLP protocol twice by alternating between restoring form the on
oldNode *node.HarnessNode, // disk file, and restoring from the exported RPC command.
backupFilePath string, func testChannelBackupRestoreTwice(ht *lntest.HarnessTest) {
password []byte, // Restore the backup from the on-disk file a second time to make sure
// imports can be canceled and later resumed.
restoreMethod := func(st *lntest.HarnessTest, oldNode *node.HarnessNode,
backupFilePath string, password []byte,
mnemonic []string) nodeRestorer { mnemonic []string) nodeRestorer {
// Read the entire Multi backup stored within // Read the entire Multi backup stored within this node's
// this node's channel.backup file. // channel.backup file.
multi, err := os.ReadFile(backupFilePath) multi, err := os.ReadFile(backupFilePath)
require.NoError(st, err) require.NoError(st, err)
// Now that we have Dave's backup file, we'll // Now that we have Dave's backup file, we'll create a new
// create a new nodeRestorer that will restore // nodeRestorer that will restore using the on-disk
// using the on-disk channel.backup. // channel.backup.
//
//nolint:ll
backup := &lnrpc.RestoreChanBackupRequest_MultiChanBackup{ backup := &lnrpc.RestoreChanBackupRequest_MultiChanBackup{
MultiChanBackup: multi, MultiChanBackup: multi,
} }
@ -460,40 +467,18 @@ func testChannelBackupRestoreBasic(ht *lntest.HarnessTest) {
req := &lnrpc.RestoreChanBackupRequest{ req := &lnrpc.RestoreChanBackupRequest{
Backup: backup, Backup: backup,
} }
res := newNode.RPC.RestoreChanBackups( newNode.RPC.RestoreChanBackups(req)
req,
)
require.EqualValues(
st, 1, res.NumRestored,
)
req = &lnrpc.RestoreChanBackupRequest{ req = &lnrpc.RestoreChanBackupRequest{
Backup: backup, Backup: backup,
} }
res = newNode.RPC.RestoreChanBackups( newNode.RPC.RestoreChanBackups(req)
req,
)
require.EqualValues(
st, 0, res.NumRestored,
)
return newNode return newNode
} }
},
},
} }
for _, testCase := range testCases { runChanRestoreScenarioBasic(ht, restoreMethod)
tc := testCase
success := ht.Run(tc.name, func(t *testing.T) {
h := ht.Subtest(t)
runChanRestoreScenarioBasic(h, tc.restoreMethod)
})
if !success {
break
}
}
} }
// runChanRestoreScenarioBasic executes a given test case from end to end, // runChanRestoreScenarioBasic executes a given test case from end to end,