itest: use require library

We rewrite the test to use the require library to make it a
bit more condensed.
This commit is contained in:
Oliver Gugger 2020-10-01 16:21:49 +02:00
parent c206d062d5
commit f114fb3c8d
No known key found for this signature in database
GPG Key ID: 8E4256593F177720

View File

@ -25,15 +25,11 @@ func testPsbtChanFunding(net *lntest.NetworkHarness, t *harnessTest) {
// First, we'll create two new nodes that we'll use to open channel
// between for this test.
carol, err := net.NewNode("carol", nil)
if err != nil {
t.Fatalf("unable to start new node: %v", err)
}
require.NoError(t.t, err)
defer shutdownAndAssert(net, t, carol)
dave, err := net.NewNode("dave", nil)
if err != nil {
t.Fatalf("unable to start new node: %v", err)
}
require.NoError(t.t, err)
defer shutdownAndAssert(net, t, dave)
// Before we start the test, we'll ensure both sides are connected so
@ -41,27 +37,21 @@ func testPsbtChanFunding(net *lntest.NetworkHarness, t *harnessTest) {
ctxt, cancel := context.WithTimeout(ctxb, defaultTimeout)
defer cancel()
err = net.EnsureConnected(ctxt, carol, dave)
if err != nil {
t.Fatalf("unable to connect peers: %v", err)
}
require.NoError(t.t, err)
err = net.EnsureConnected(ctxt, carol, net.Alice)
if err != nil {
t.Fatalf("unable to connect peers: %v", err)
}
require.NoError(t.t, err)
// At this point, we can begin our PSBT channel funding workflow. We'll
// start by generating a pending channel ID externally that will be used
// to track this new funding type.
var pendingChanID [32]byte
if _, err := rand.Read(pendingChanID[:]); err != nil {
t.Fatalf("unable to gen pending chan ID: %v", err)
}
_, err = rand.Read(pendingChanID[:])
require.NoError(t.t, err)
// We'll also test batch funding of two channels so we need another ID.
var pendingChanID2 [32]byte
if _, err := rand.Read(pendingChanID2[:]); err != nil {
t.Fatalf("unable to gen pending chan ID: %v", err)
}
_, err = rand.Read(pendingChanID2[:])
require.NoError(t.t, err)
// Now that we have the pending channel ID, Carol will open the channel
// by specifying a PSBT shim. We use the NoPublish flag here to avoid
@ -81,13 +71,9 @@ func testPsbtChanFunding(net *lntest.NetworkHarness, t *harnessTest) {
},
},
)
if err != nil {
t.Fatalf("unable to open channel to dave: %v", err)
}
require.NoError(t.t, err)
packet, err := psbt.NewFromRawBytes(bytes.NewReader(psbtBytes), false)
if err != nil {
t.Fatalf("unable to parse returned PSBT: %v", err)
}
require.NoError(t.t, err)
// Let's add a second channel to the batch. This time between carol and
// alice. We will the batch TX once this channel funding is complete.
@ -106,30 +92,21 @@ func testPsbtChanFunding(net *lntest.NetworkHarness, t *harnessTest) {
},
},
)
if err != nil {
t.Fatalf("unable to open channel to alice: %v", err)
}
require.NoError(t.t, err)
packet2, err := psbt.NewFromRawBytes(bytes.NewReader(psbtBytes2), false)
if err != nil {
t.Fatalf("unable to parse returned PSBT: %v", err)
}
require.NoError(t.t, err)
// We'll now create a fully signed transaction that sends to the outputs
// encoded in the PSBT. We'll let the miner do it and convert the final
// TX into a PSBT, that's way easier than assembling a PSBT manually.
allOuts := append(packet.UnsignedTx.TxOut, packet2.UnsignedTx.TxOut...)
finalTx, err := net.Miner.CreateTransaction(allOuts, 5, true)
if err != nil {
t.Fatalf("unable to create funding transaction: %v", err)
}
require.NoError(t.t, err)
// The helper function splits the final TX into the non-witness data
// encoded in a PSBT and the witness data returned separately.
unsignedPsbt, scripts, witnesses, err := createPsbtFromSignedTx(finalTx)
if err != nil {
t.Fatalf("unable to convert funding transaction into PSBT: %v",
err)
}
require.NoError(t.t, err)
// The PSBT will also be checked if there are large enough inputs
// present. We need to add some fake UTXO information to the PSBT to
@ -151,9 +128,7 @@ func testPsbtChanFunding(net *lntest.NetworkHarness, t *harnessTest) {
// Serialize the PSBT with the faked UTXO information.
var buf bytes.Buffer
err = unsignedPsbt.Serialize(&buf)
if err != nil {
t.Fatalf("error serializing PSBT: %v", err)
}
require.NoError(t.t, err)
// We have a PSBT that has no witness data yet, which is exactly what we
// need for the next step: Verify the PSBT with the funding intents.
@ -165,9 +140,7 @@ func testPsbtChanFunding(net *lntest.NetworkHarness, t *harnessTest) {
},
},
})
if err != nil {
t.Fatalf("error verifying PSBT with funding intent: %v", err)
}
require.NoError(t.t, err)
_, err = carol.FundingStateStep(ctxb, &lnrpc.FundingTransitionMsg{
Trigger: &lnrpc.FundingTransitionMsg_PsbtVerify{
PsbtVerify: &lnrpc.FundingPsbtVerify{
@ -176,27 +149,21 @@ func testPsbtChanFunding(net *lntest.NetworkHarness, t *harnessTest) {
},
},
})
if err != nil {
t.Fatalf("error verifying PSBT with funding intent 2: %v", err)
}
require.NoError(t.t, err)
// Now we'll add the witness data back into the PSBT to make it a
// complete and signed transaction that can be finalized. We'll trick
// a bit by putting the script sig back directly, because we know we
// will only get non-witness outputs from the miner wallet.
for idx := range finalTx.TxIn {
if len(witnesses[idx]) > 0 {
t.Fatalf("unexpected witness inputs in wallet TX")
}
require.Greater(t.t, len(witnesses[idx]), 0)
unsignedPsbt.Inputs[idx].FinalScriptSig = scripts[idx]
}
// We've signed our PSBT now, let's pass it to the intent again.
buf.Reset()
err = unsignedPsbt.Serialize(&buf)
if err != nil {
t.Fatalf("error serializing PSBT: %v", err)
}
require.NoError(t.t, err)
_, err = carol.FundingStateStep(ctxb, &lnrpc.FundingTransitionMsg{
Trigger: &lnrpc.FundingTransitionMsg_PsbtFinalize{
PsbtFinalize: &lnrpc.FundingPsbtFinalize{
@ -205,23 +172,16 @@ func testPsbtChanFunding(net *lntest.NetworkHarness, t *harnessTest) {
},
},
})
if err != nil {
t.Fatalf("error finalizing PSBT with funding intent: %v", err)
}
require.NoError(t.t, err)
// Consume the "channel pending" update. This waits until the funding
// transaction was fully compiled.
ctxt, cancel = context.WithTimeout(ctxb, defaultTimeout)
defer cancel()
updateResp, err := receiveChanUpdate(ctxt, chanUpdates)
if err != nil {
t.Fatalf("unable to consume channel update message: %v", err)
}
require.NoError(t.t, err)
upd, ok := updateResp.Update.(*lnrpc.OpenStatusUpdate_ChanPending)
if !ok {
t.Fatalf("expected PSBT funding update, instead got %v",
updateResp)
}
require.True(t.t, ok)
chanPoint := &lnrpc.ChannelPoint{
FundingTxid: &lnrpc.ChannelPoint_FundingTxidBytes{
FundingTxidBytes: upd.ChanPending.Txid,
@ -231,12 +191,8 @@ func testPsbtChanFunding(net *lntest.NetworkHarness, t *harnessTest) {
// No transaction should have been published yet.
mempool, err := net.Miner.Node.GetRawMempool()
if err != nil {
t.Fatalf("error querying mempool: %v", err)
}
if len(mempool) != 0 {
t.Fatalf("unexpected txes in mempool: %v", mempool)
}
require.NoError(t.t, err)
require.Equal(t.t, 0, len(mempool))
// Let's progress the second channel now. This time we'll use the raw
// wire format transaction directly.
@ -251,9 +207,7 @@ func testPsbtChanFunding(net *lntest.NetworkHarness, t *harnessTest) {
},
},
})
if err != nil {
t.Fatalf("error finalizing PSBT with funding intent 2: %v", err)
}
require.NoError(t.t, err)
// Consume the "channel pending" update for the second channel. This
// waits until the funding transaction was fully compiled and in this
@ -261,14 +215,9 @@ func testPsbtChanFunding(net *lntest.NetworkHarness, t *harnessTest) {
ctxt, cancel = context.WithTimeout(ctxb, defaultTimeout)
defer cancel()
updateResp2, err := receiveChanUpdate(ctxt, chanUpdates2)
if err != nil {
t.Fatalf("unable to consume channel update message: %v", err)
}
require.NoError(t.t, err)
upd2, ok := updateResp2.Update.(*lnrpc.OpenStatusUpdate_ChanPending)
if !ok {
t.Fatalf("expected PSBT funding update, instead got %v",
updateResp2)
}
require.True(t.t, ok)
chanPoint2 := &lnrpc.ChannelPoint{
FundingTxid: &lnrpc.ChannelPoint_FundingTxidBytes{
FundingTxidBytes: upd2.ChanPending.Txid,
@ -284,13 +233,9 @@ func testPsbtChanFunding(net *lntest.NetworkHarness, t *harnessTest) {
ctxt, cancel = context.WithTimeout(ctxb, defaultTimeout)
defer cancel()
err = carol.WaitForNetworkChannelOpen(ctxt, chanPoint)
if err != nil {
t.Fatalf("carol didn't report channel: %v", err)
}
require.NoError(t.t, err)
err = carol.WaitForNetworkChannelOpen(ctxt, chanPoint2)
if err != nil {
t.Fatalf("carol didn't report channel 2: %v", err)
}
require.NoError(t.t, err)
// With the channel open, ensure that it is counted towards Carol's
// total channel balance.
@ -298,12 +243,8 @@ func testPsbtChanFunding(net *lntest.NetworkHarness, t *harnessTest) {
ctxt, cancel = context.WithTimeout(ctxb, defaultTimeout)
defer cancel()
balRes, err := carol.ChannelBalance(ctxt, balReq)
if err != nil {
t.Fatalf("unable to get carol's balance: %v", err)
}
if balRes.LocalBalance.Sat == 0 {
t.Fatalf("carol has an empty channel balance")
}
require.NoError(t.t, err)
require.NotEqual(t.t, int64(0), balRes.LocalBalance.Sat)
// Next, to make sure the channel functions as normal, we'll make some
// payments within the channel.
@ -314,17 +255,13 @@ func testPsbtChanFunding(net *lntest.NetworkHarness, t *harnessTest) {
}
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
resp, err := dave.AddInvoice(ctxt, invoice)
if err != nil {
t.Fatalf("unable to add invoice: %v", err)
}
require.NoError(t.t, err)
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
err = completePaymentRequests(
ctxt, carol, carol.RouterClient, []string{resp.PaymentRequest},
true,
)
if err != nil {
t.Fatalf("unable to make payments between Carol and Dave")
}
require.NoError(t.t, err)
// To conclude, we'll close the newly created channel between Carol and
// Dave. This function will also block until the channel is closed and