itest: refactor testExportChannelBackup
This commit is contained in:
3 files changed
+30
-73
No files matched your search
@@ -195,4 +195,8 @@ var allTestCasesTemp = []*lntemp.TestCase{
|
|||||||
Name: "streaming channel backup update",
|
Name: "streaming channel backup update",
|
||||||
TestFunc: testChannelBackupUpdates,
|
TestFunc: testChannelBackupUpdates,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
Name: "export channel backup",
|
||||||
|
TestFunc: testExportChannelBackup,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
@@ -20,7 +20,6 @@ import (
|
|||||||
"github.com/lightningnetwork/lnd/lnrpc/walletrpc"
|
"github.com/lightningnetwork/lnd/lnrpc/walletrpc"
|
||||||
"github.com/lightningnetwork/lnd/lntemp"
|
"github.com/lightningnetwork/lnd/lntemp"
|
||||||
"github.com/lightningnetwork/lnd/lntemp/node"
|
"github.com/lightningnetwork/lnd/lntemp/node"
|
||||||
"github.com/lightningnetwork/lnd/lntest"
|
|
||||||
"github.com/lightningnetwork/lnd/lntest/wait"
|
"github.com/lightningnetwork/lnd/lntest/wait"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
@@ -952,73 +951,49 @@ func testChannelBackupUpdates(ht *lntemp.HarnessTest) {
|
|||||||
// testExportChannelBackup tests that we're able to properly export either a
|
// testExportChannelBackup tests that we're able to properly export either a
|
||||||
// targeted channel's backup, or export backups of all the currents open
|
// targeted channel's backup, or export backups of all the currents open
|
||||||
// channels.
|
// channels.
|
||||||
func testExportChannelBackup(net *lntest.NetworkHarness, t *harnessTest) {
|
func testExportChannelBackup(ht *lntemp.HarnessTest) {
|
||||||
ctxb := context.Background()
|
|
||||||
|
|
||||||
// First, we'll create our primary test node: Carol. We'll use Carol to
|
// First, we'll create our primary test node: Carol. We'll use Carol to
|
||||||
// open channels and also export backups that we'll examine throughout
|
// open channels and also export backups that we'll examine throughout
|
||||||
// the test.
|
// the test.
|
||||||
carol := net.NewNode(t.t, "carol", nil)
|
carol := ht.NewNode("carol", nil)
|
||||||
defer shutdownAndAssert(net, t, carol)
|
|
||||||
|
|
||||||
// With Carol up, we'll now connect her to Alice, and open a channel
|
// With Carol up, we'll now connect her to Alice, and open a channel
|
||||||
// between them.
|
// between them.
|
||||||
net.ConnectNodes(t.t, carol, net.Alice)
|
alice := ht.Alice
|
||||||
|
ht.ConnectNodes(carol, alice)
|
||||||
|
|
||||||
// Next, we'll open two channels between Alice and Carol back to back.
|
// Next, we'll open two channels between Alice and Carol back to back.
|
||||||
var chanPoints []*lnrpc.ChannelPoint
|
var chanPoints []*lnrpc.ChannelPoint
|
||||||
numChans := 2
|
numChans := 2
|
||||||
chanAmt := btcutil.Amount(1000000)
|
chanAmt := btcutil.Amount(1000000)
|
||||||
for i := 0; i < numChans; i++ {
|
for i := 0; i < numChans; i++ {
|
||||||
chanPoint := openChannelAndAssert(
|
chanPoint := ht.OpenChannel(
|
||||||
t, net, net.Alice, carol,
|
alice, carol, lntemp.OpenChannelParams{Amt: chanAmt},
|
||||||
lntest.OpenChannelParams{Amt: chanAmt},
|
|
||||||
)
|
)
|
||||||
|
|
||||||
chanPoints = append(chanPoints, chanPoint)
|
chanPoints = append(chanPoints, chanPoint)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Now that the channels are open, we should be able to fetch the
|
// Now that the channels are open, we should be able to fetch the
|
||||||
// backups of each of the channels.
|
// backups of each of the channels.
|
||||||
for _, chanPoint := range chanPoints {
|
for _, chanPoint := range chanPoints {
|
||||||
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
chanBackup := carol.RPC.ExportChanBackup(chanPoint)
|
||||||
req := &lnrpc.ExportChannelBackupRequest{
|
|
||||||
ChanPoint: chanPoint,
|
|
||||||
}
|
|
||||||
chanBackup, err := carol.ExportChannelBackup(ctxt, req)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("unable to fetch backup for channel %v: %v",
|
|
||||||
chanPoint, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// The returned backup should be full populated. Since it's
|
// The returned backup should be full populated. Since it's
|
||||||
// encrypted, we can't assert any more than that atm.
|
// encrypted, we can't assert any more than that atm.
|
||||||
if len(chanBackup.ChanBackup) == 0 {
|
require.NotEmptyf(ht, chanBackup.ChanBackup,
|
||||||
t.Fatalf("obtained empty backup for channel: %v", chanPoint)
|
"obtained empty backup for channel: %v", chanPoint)
|
||||||
}
|
|
||||||
|
|
||||||
// The specified chanPoint in the response should match our
|
// The specified chanPoint in the response should match our
|
||||||
// requested chanPoint.
|
// requested chanPoint.
|
||||||
if chanBackup.ChanPoint.String() != chanPoint.String() {
|
require.Equal(ht, chanBackup.ChanPoint.String(),
|
||||||
t.Fatalf("chanPoint mismatched: expected %v, got %v",
|
chanPoint.String())
|
||||||
chanPoint.String(),
|
|
||||||
chanBackup.ChanPoint.String())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Before we proceed, we'll make two utility methods we'll use below
|
// Before we proceed, we'll make two utility methods we'll use below
|
||||||
// for our primary assertions.
|
// for our primary assertions.
|
||||||
assertNumSingleBackups := func(numSingles int) {
|
assertNumSingleBackups := func(numSingles int) {
|
||||||
err := wait.NoError(func() error {
|
err := wait.NoError(func() error {
|
||||||
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
chanSnapshot := carol.RPC.ExportAllChanBackups()
|
||||||
req := &lnrpc.ChanBackupExportRequest{}
|
|
||||||
chanSnapshot, err := carol.ExportAllChannelBackups(
|
|
||||||
ctxt, req,
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("unable to export channel "+
|
|
||||||
"backup: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if chanSnapshot.SingleChanBackups == nil {
|
if chanSnapshot.SingleChanBackups == nil {
|
||||||
return fmt.Errorf("single chan backups not " +
|
return fmt.Errorf("single chan backups not " +
|
||||||
@@ -1033,29 +1008,23 @@ func testExportChannelBackup(net *lntest.NetworkHarness, t *harnessTest) {
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}, defaultTimeout)
|
}, defaultTimeout)
|
||||||
if err != nil {
|
require.NoError(ht, err, "timeout checking num single backup")
|
||||||
t.Fatalf(err.Error())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
assertMultiBackupFound := func() func(bool, map[wire.OutPoint]struct{}) {
|
assertMultiBackupFound := func() func(bool, map[wire.OutPoint]struct{}) {
|
||||||
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
chanSnapshot := carol.RPC.ExportAllChanBackups()
|
||||||
req := &lnrpc.ChanBackupExportRequest{}
|
|
||||||
chanSnapshot, err := carol.ExportAllChannelBackups(ctxt, req)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("unable to export channel backup: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return func(found bool, chanPoints map[wire.OutPoint]struct{}) {
|
return func(found bool, chanPoints map[wire.OutPoint]struct{}) {
|
||||||
switch {
|
switch {
|
||||||
case found && chanSnapshot.MultiChanBackup == nil:
|
case found && chanSnapshot.MultiChanBackup == nil:
|
||||||
t.Fatalf("multi-backup not present")
|
require.Fail(ht, "multi-backup not present")
|
||||||
|
|
||||||
case !found && chanSnapshot.MultiChanBackup != nil &&
|
case !found && chanSnapshot.MultiChanBackup != nil &&
|
||||||
(len(chanSnapshot.MultiChanBackup.MultiChanBackup) !=
|
(len(chanSnapshot.MultiChanBackup.MultiChanBackup) !=
|
||||||
chanbackup.NilMultiSizePacked):
|
chanbackup.NilMultiSizePacked):
|
||||||
|
|
||||||
t.Fatalf("found multi-backup when non should " +
|
require.Fail(ht, "found multi-backup when "+
|
||||||
"be found")
|
"non should be found")
|
||||||
}
|
}
|
||||||
|
|
||||||
if !found {
|
if !found {
|
||||||
@@ -1063,23 +1032,20 @@ func testExportChannelBackup(net *lntest.NetworkHarness, t *harnessTest) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
backedUpChans := chanSnapshot.MultiChanBackup.ChanPoints
|
backedUpChans := chanSnapshot.MultiChanBackup.ChanPoints
|
||||||
if len(chanPoints) != len(backedUpChans) {
|
require.Len(ht, backedUpChans, len(chanPoints))
|
||||||
t.Fatalf("expected %v chans got %v", len(chanPoints),
|
|
||||||
len(backedUpChans))
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, chanPoint := range backedUpChans {
|
for _, chanPoint := range backedUpChans {
|
||||||
wirePoint := rpcPointToWirePoint(t, chanPoint)
|
wp := ht.OutPointFromChannelPoint(chanPoint)
|
||||||
if _, ok := chanPoints[wirePoint]; !ok {
|
_, ok := chanPoints[wp]
|
||||||
t.Fatalf("unexpected backup: %v", wirePoint)
|
require.True(ht, ok, "unexpected "+
|
||||||
}
|
"backup: %v", wp)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
chans := make(map[wire.OutPoint]struct{})
|
chans := make(map[wire.OutPoint]struct{})
|
||||||
for _, chanPoint := range chanPoints {
|
for _, chanPoint := range chanPoints {
|
||||||
chans[rpcPointToWirePoint(t, chanPoint)] = struct{}{}
|
chans[ht.OutPointFromChannelPoint(chanPoint)] = struct{}{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// We should have exactly two single channel backups contained, and we
|
// We should have exactly two single channel backups contained, and we
|
||||||
@@ -1091,11 +1057,11 @@ func testExportChannelBackup(net *lntest.NetworkHarness, t *harnessTest) {
|
|||||||
// shouldn't be able to find that channel as a backup still. We should
|
// shouldn't be able to find that channel as a backup still. We should
|
||||||
// also have one less single written to disk.
|
// also have one less single written to disk.
|
||||||
for i, chanPoint := range chanPoints {
|
for i, chanPoint := range chanPoints {
|
||||||
closeChannelAndAssert(t, net, net.Alice, chanPoint, false)
|
ht.CloseChannel(alice, chanPoint)
|
||||||
|
|
||||||
assertNumSingleBackups(len(chanPoints) - i - 1)
|
assertNumSingleBackups(len(chanPoints) - i - 1)
|
||||||
|
|
||||||
delete(chans, rpcPointToWirePoint(t, chanPoint))
|
delete(chans, ht.OutPointFromChannelPoint(chanPoint))
|
||||||
assertMultiBackupFound()(true, chans)
|
assertMultiBackupFound()(true, chans)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1399,15 +1365,6 @@ func copyPorts(oldNode *node.HarnessNode) node.Option {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func rpcPointToWirePoint(t *harnessTest,
|
|
||||||
chanPoint *lnrpc.ChannelPoint) wire.OutPoint {
|
|
||||||
|
|
||||||
op, err := lntest.MakeOutpoint(chanPoint)
|
|
||||||
require.NoError(t.t, err, "unable to get txid")
|
|
||||||
|
|
||||||
return op
|
|
||||||
}
|
|
||||||
|
|
||||||
// assertTimeLockSwept when dave's outputs matures, he should claim them. This
|
// assertTimeLockSwept when dave's outputs matures, he should claim them. This
|
||||||
// function will advance 2 blocks such that all the pending closing
|
// function will advance 2 blocks such that all the pending closing
|
||||||
// transactions would be swept in the end.
|
// transactions would be swept in the end.
|
||||||
|
|||||||
@@ -126,10 +126,6 @@ var allTestCases = []*testCase{
|
|||||||
name: "route fee cutoff",
|
name: "route fee cutoff",
|
||||||
test: testRouteFeeCutoff,
|
test: testRouteFeeCutoff,
|
||||||
},
|
},
|
||||||
{
|
|
||||||
name: "export channel backup",
|
|
||||||
test: testExportChannelBackup,
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
name: "hold invoice sender persistence",
|
name: "hold invoice sender persistence",
|
||||||
test: testHoldInvoicePersistence,
|
test: testHoldInvoicePersistence,
|
||||||
|
|||||||
Reference in new issue
Block a user