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