lntemp+itest: refactor testUpdateChannelPolicyForPrivateChannel

This commit is contained in:
yyforyongyu 2022-08-04 06:10:44 +08:00
parent b579ed72d9
commit cd7f02c866
No known key found for this signature in database
GPG Key ID: 9BCD95C4FF296868
5 changed files with 74 additions and 85 deletions

View File

@ -1261,21 +1261,20 @@ func (h *HarnessTest) AssertNumHTLCsAndStage(hn *node.HarnessNode,
// findPayment queries the payment from the node's ListPayments which matches // findPayment queries the payment from the node's ListPayments which matches
// the specified preimage hash. // the specified preimage hash.
func (h *HarnessTest) findPayment(hn *node.HarnessNode, func (h *HarnessTest) findPayment(hn *node.HarnessNode,
preimage lntypes.Preimage) *lnrpc.Payment { paymentHash string) *lnrpc.Payment {
req := &lnrpc.ListPaymentsRequest{IncludeIncomplete: true} req := &lnrpc.ListPaymentsRequest{IncludeIncomplete: true}
paymentsResp := hn.RPC.ListPayments(req) paymentsResp := hn.RPC.ListPayments(req)
payHash := preimage.Hash()
for _, p := range paymentsResp.Payments { for _, p := range paymentsResp.Payments {
if p.PaymentHash != payHash.String() { if p.PaymentHash != paymentHash {
continue continue
} }
return p return p
} }
require.Fail(h, "payment: %v not found", payHash) require.Fail(h, "payment: %v not found", paymentHash)
return nil return nil
} }
@ -1291,7 +1290,7 @@ func (h *HarnessTest) AssertPaymentStatus(hn *node.HarnessNode,
var target *lnrpc.Payment var target *lnrpc.Payment
err := wait.NoError(func() error { err := wait.NoError(func() error {
p := h.findPayment(hn, preimage) p := h.findPayment(hn, preimage.Hash().String())
if status == p.Status { if status == p.Status {
target = p target = p
return nil return nil
@ -1596,3 +1595,32 @@ func (h *HarnessTest) AssertNumPolicyUpdates(hn *node.HarnessNode,
require.NoError(h, err, "%s: timeout waiting for num of policy updates", require.NoError(h, err, "%s: timeout waiting for num of policy updates",
hn.Name()) hn.Name())
} }
// AssertNumPayments asserts that the number of payments made within the test
// scope is as expected, including the incomplete ones.
func (h *HarnessTest) AssertNumPayments(hn *node.HarnessNode,
num int) []*lnrpc.Payment {
// Get the number of payments we already have from the previous test.
have := hn.State.Payment.Total
req := &lnrpc.ListPaymentsRequest{
IncludeIncomplete: true,
}
var payments []*lnrpc.Payment
err := wait.NoError(func() error {
resp := hn.RPC.ListPayments(req)
payments = resp.Payments
if len(payments) == num {
return nil
}
return errNumNotMatched(hn.Name(), "num of payments",
num, len(payments), have+len(payments), have)
}, DefaultTimeout)
require.NoError(h, err, "timeout checking num of payments")
return payments
}

View File

@ -166,6 +166,7 @@ func (cfg *BaseNodeConfig) GenArgs() []string {
"--debuglevel=debug", "--debuglevel=debug",
"--bitcoin.defaultchanconfs=1", "--bitcoin.defaultchanconfs=1",
"--accept-keysend", "--accept-keysend",
"--keep-failed-payment-attempts",
fmt.Sprintf("--db.batch-commit-interval=%v", commitInterval), fmt.Sprintf("--db.batch-commit-interval=%v", commitInterval),
fmt.Sprintf("--bitcoin.defaultremotedelay=%v", fmt.Sprintf("--bitcoin.defaultremotedelay=%v",
lntest.DefaultCSV), lntest.DefaultCSV),

View File

@ -143,4 +143,8 @@ var allTestCasesTemp = []*lntemp.TestCase{
Name: "send update disable channel", Name: "send update disable channel",
TestFunc: testSendUpdateDisableChannel, TestFunc: testSendUpdateDisableChannel,
}, },
{
Name: "private channel update policy",
TestFunc: testUpdateChannelPolicyForPrivateChannel,
},
} }

View File

@ -7,7 +7,6 @@ import (
"time" "time"
"github.com/btcsuite/btcd/btcutil" "github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/wire"
"github.com/lightningnetwork/lnd/chainreg" "github.com/lightningnetwork/lnd/chainreg"
"github.com/lightningnetwork/lnd/funding" "github.com/lightningnetwork/lnd/funding"
"github.com/lightningnetwork/lnd/lnrpc" "github.com/lightningnetwork/lnd/lnrpc"
@ -672,80 +671,53 @@ func testSendUpdateDisableChannel(ht *lntemp.HarnessTest) {
// Bob will update the base fee via UpdateChannelPolicy, we will test that // Bob will update the base fee via UpdateChannelPolicy, we will test that
// Alice will not fail the payment and send it using the updated channel // Alice will not fail the payment and send it using the updated channel
// policy. // policy.
func testUpdateChannelPolicyForPrivateChannel(net *lntest.NetworkHarness, func testUpdateChannelPolicyForPrivateChannel(ht *lntemp.HarnessTest) {
t *harnessTest) { const (
chanAmt = btcutil.Amount(100000)
ctxb := context.Background() paymentAmt = 20000
defer ctxb.Done() baseFeeMSat = 33000
)
// We'll create the following topology first, // We'll create the following topology first,
// Alice <--public:100k--> Bob <--private:100k--> Carol // Alice <--public:100k--> Bob <--private:100k--> Carol
const chanAmt = btcutil.Amount(100000) alice, bob := ht.Alice, ht.Bob
// Open a channel with 100k satoshis between Alice and Bob. // Open a channel with 100k satoshis between Alice and Bob.
chanPointAliceBob := openChannelAndAssert( chanPointAliceBob := ht.OpenChannel(
t, net, net.Alice, net.Bob, alice, bob, lntemp.OpenChannelParams{
lntest.OpenChannelParams{
Amt: chanAmt, Amt: chanAmt,
}, },
) )
defer closeChannelAndAssert(t, net, net.Alice, chanPointAliceBob, false)
// Get Alice's funding point.
aliceChanTXID, err := lnrpc.GetChanPointFundingTxid(chanPointAliceBob)
require.NoError(t.t, err, "unable to get txid")
aliceFundPoint := wire.OutPoint{
Hash: *aliceChanTXID,
Index: chanPointAliceBob.OutputIndex,
}
// Create a new node Carol. // Create a new node Carol.
carol := net.NewNode(t.t, "Carol", nil) carol := ht.NewNode("Carol", nil)
defer shutdownAndAssert(net, t, carol)
// Connect Carol to Bob. // Connect Carol to Bob.
net.ConnectNodes(t.t, carol, net.Bob) ht.ConnectNodes(carol, bob)
// Open a channel with 100k satoshis between Bob and Carol. // Open a channel with 100k satoshis between Bob and Carol.
chanPointBobCarol := openChannelAndAssert( chanPointBobCarol := ht.OpenChannel(
t, net, net.Bob, carol, bob, carol, lntemp.OpenChannelParams{
lntest.OpenChannelParams{
Amt: chanAmt, Amt: chanAmt,
Private: true, Private: true,
}, },
) )
defer closeChannelAndAssert(t, net, net.Bob, chanPointBobCarol, false)
// Carol should be aware of the channel between Alice and Bob. // Carol should be aware of the channel between Alice and Bob.
err = carol.WaitForNetworkChannelOpen(chanPointAliceBob) ht.AssertTopologyChannelOpen(carol, chanPointAliceBob)
require.NoError(t.t, err)
// Get Bob's funding point.
bobChanTXID, err := lnrpc.GetChanPointFundingTxid(chanPointBobCarol)
require.NoError(t.t, err, "unable to get txid")
bobFundPoint := wire.OutPoint{
Hash: *bobChanTXID,
Index: chanPointBobCarol.OutputIndex,
}
// We should have the following topology now, // We should have the following topology now,
// Alice <--public:100k--> Bob <--private:100k--> Carol // Alice <--public:100k--> Bob <--private:100k--> Carol
// //
// Now we will create an invoice for Carol. // Now we will create an invoice for Carol.
const paymentAmt = 20000
invoice := &lnrpc.Invoice{ invoice := &lnrpc.Invoice{
Memo: "routing hints", Memo: "routing hints",
Value: paymentAmt, Value: paymentAmt,
Private: true, Private: true,
} }
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout) resp := carol.RPC.AddInvoice(invoice)
resp, err := carol.AddInvoice(ctxt, invoice)
require.NoError(t.t, err, "unable to create invoice for carol")
// Bob now updates the channel edge policy for the private channel. // Bob now updates the channel edge policy for the private channel.
const (
baseFeeMSat = 33000
)
timeLockDelta := uint32(chainreg.DefaultBitcoinTimeLockDelta) timeLockDelta := uint32(chainreg.DefaultBitcoinTimeLockDelta)
updateFeeReq := &lnrpc.PolicyUpdateRequest{ updateFeeReq := &lnrpc.PolicyUpdateRequest{
BaseFeeMsat: baseFeeMSat, BaseFeeMsat: baseFeeMSat,
@ -754,58 +726,46 @@ func testUpdateChannelPolicyForPrivateChannel(net *lntest.NetworkHarness,
ChanPoint: chanPointBobCarol, ChanPoint: chanPointBobCarol,
}, },
} }
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout) bob.RPC.UpdateChannelPolicy(updateFeeReq)
_, err = net.Bob.UpdateChannelPolicy(ctxt, updateFeeReq)
require.NoError(t.t, err, "unable to update chan policy")
// Alice pays the invoices. She will use the updated baseFeeMSat in the // Alice pays the invoices. She will use the updated baseFeeMSat in the
// payment // payment
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
payReqs := []string{resp.PaymentRequest} payReqs := []string{resp.PaymentRequest}
require.NoError(t.t, ht.CompletePaymentRequests(alice, payReqs)
completePaymentRequests(
net.Alice, net.Alice.RouterClient, payReqs, true,
), "unable to send payment",
)
// Check that Alice did make the payment with two HTLCs, one failed and // Check that Alice did make the payment with two HTLCs, one failed and
// one succeeded. // one succeeded.
ctxt, _ = context.WithTimeout(ctxt, defaultTimeout) payment := ht.AssertNumPayments(alice, 1)[0]
paymentsResp, err := net.Alice.ListPayments(
ctxt, &lnrpc.ListPaymentsRequest{},
)
require.NoError(t.t, err, "failed to obtain payments for Alice")
require.Equal(t.t, 1, len(paymentsResp.Payments), "expected 1 payment")
htlcs := paymentsResp.Payments[0].Htlcs htlcs := payment.Htlcs
require.Equal(t.t, 2, len(htlcs), "expected to have 2 HTLCs") require.Equal(ht, 2, len(htlcs), "expected to have 2 HTLCs")
require.Equal( require.Equal(ht, lnrpc.HTLCAttempt_FAILED, htlcs[0].Status,
t.t, lnrpc.HTLCAttempt_FAILED, htlcs[0].Status, "the first HTLC attempt should fail")
"the first HTLC attempt should fail", require.Equal(ht, lnrpc.HTLCAttempt_SUCCEEDED, htlcs[1].Status,
) "the second HTLC attempt should succeed")
require.Equal(
t.t, lnrpc.HTLCAttempt_SUCCEEDED, htlcs[1].Status,
"the second HTLC attempt should succeed",
)
// Carol should have received 20k satoshis from Bob. // Carol should have received 20k satoshis from Bob.
assertAmountPaid(t, "Carol(remote) [<=private] Bob(local)", ht.AssertAmountPaid("Carol(remote) [<=private] Bob(local)",
carol, bobFundPoint, 0, paymentAmt) carol, chanPointBobCarol, 0, paymentAmt)
// Bob should have sent 20k satoshis to Carol. // Bob should have sent 20k satoshis to Carol.
assertAmountPaid(t, "Bob(local) [private=>] Carol(remote)", ht.AssertAmountPaid("Bob(local) [private=>] Carol(remote)",
net.Bob, bobFundPoint, paymentAmt, 0) bob, chanPointBobCarol, paymentAmt, 0)
// Calculate the amount in satoshis. // Calculate the amount in satoshis.
amtExpected := int64(paymentAmt + baseFeeMSat/1000) amtExpected := int64(paymentAmt + baseFeeMSat/1000)
// Bob should have received 20k satoshis + fee from Alice. // Bob should have received 20k satoshis + fee from Alice.
assertAmountPaid(t, "Bob(remote) <= Alice(local)", ht.AssertAmountPaid("Bob(remote) <= Alice(local)",
net.Bob, aliceFundPoint, 0, amtExpected) bob, chanPointAliceBob, 0, amtExpected)
// Alice should have sent 20k satoshis + fee to Bob. // Alice should have sent 20k satoshis + fee to Bob.
assertAmountPaid(t, "Alice(local) => Bob(remote)", ht.AssertAmountPaid("Alice(local) => Bob(remote)",
net.Alice, aliceFundPoint, amtExpected, 0) alice, chanPointAliceBob, amtExpected, 0)
// Finally, close the channels.
ht.CloseChannel(alice, chanPointAliceBob)
ht.CloseChannel(bob, chanPointBobCarol)
} }
// testUpdateChannelPolicyFeeRateAccuracy tests that updating the channel policy // testUpdateChannelPolicyFeeRateAccuracy tests that updating the channel policy

View File

@ -68,10 +68,6 @@ var allTestCases = []*testCase{
name: "private channels", name: "private channels",
test: testPrivateChannels, test: testPrivateChannels,
}, },
{
name: "private channel update policy",
test: testUpdateChannelPolicyForPrivateChannel,
},
{ {
name: "invoice routing hints", name: "invoice routing hints",
test: testInvoiceRoutingHints, test: testInvoiceRoutingHints,