switch+router+server: move NextPaymentID to router

This commit moves the responsibility of generating a unique payment ID
from the switch to the router. This will make it easier for the router
to keep track of which HTLCs were successfully forwarded onto the
network, as it can query the switch for existing HTLCs as long as the
paymentIDs are kept.

The router is expected to maintain a map from paymentHash->paymentID,
such that they can be replayed on restart. This also lets the router
check the status of a sent payment after a restart, by querying the
switch for the paymentID in question.
This commit is contained in:
Johan T. Halseth
2019-05-16 15:27:28 +02:00
parent f1cb54f943
commit c9e8ff6a34
8 changed files with 84 additions and 45 deletions

View File

@@ -1100,15 +1100,16 @@ func TestChannelLinkMultiHopUnknownPaymentHash(t *testing.T) {
// Generate payment invoice and htlc, but don't add this invoice to the
// receiver registry. This should trigger an unknown payment hash
// failure.
_, htlc, err := generatePayment(amount, htlcAmt, totalTimelock,
blob)
_, htlc, pid, err := generatePayment(
amount, htlcAmt, totalTimelock, blob,
)
if err != nil {
t.Fatal(err)
}
// Send payment and expose err channel.
_, err = n.aliceServer.htlcSwitch.SendHTLC(
n.firstBobChannelLink.ShortChanID(), htlc,
n.firstBobChannelLink.ShortChanID(), pid, htlc,
newMockDeobfuscator(),
)
if !strings.Contains(err.Error(), lnwire.CodeUnknownPaymentHash.String()) {
@@ -1909,7 +1910,9 @@ func TestChannelLinkBandwidthConsistency(t *testing.T) {
// a switch initiated payment. The resulting bandwidth should
// now be decremented to reflect the new HTLC.
htlcAmt := lnwire.NewMSatFromSatoshis(btcutil.SatoshiPerBitcoin)
invoice, htlc, err := generatePayment(htlcAmt, htlcAmt, 5, mockBlob)
invoice, htlc, _, err := generatePayment(
htlcAmt, htlcAmt, 5, mockBlob,
)
if err != nil {
t.Fatalf("unable to create payment: %v", err)
}
@@ -1989,7 +1992,7 @@ func TestChannelLinkBandwidthConsistency(t *testing.T) {
// Next, we'll add another HTLC initiated by the switch (of the same
// amount as the prior one).
invoice, htlc, err = generatePayment(htlcAmt, htlcAmt, 5, mockBlob)
invoice, htlc, _, err = generatePayment(htlcAmt, htlcAmt, 5, mockBlob)
if err != nil {
t.Fatalf("unable to create payment: %v", err)
}
@@ -2075,8 +2078,9 @@ func TestChannelLinkBandwidthConsistency(t *testing.T) {
if err != nil {
t.Fatalf("unable to gen route: %v", err)
}
invoice, htlc, err = generatePayment(htlcAmt, htlcAmt,
totalTimelock, blob)
invoice, htlc, _, err = generatePayment(
htlcAmt, htlcAmt, totalTimelock, blob,
)
if err != nil {
t.Fatalf("unable to create payment: %v", err)
}
@@ -2183,7 +2187,9 @@ func TestChannelLinkBandwidthConsistency(t *testing.T) {
if err != nil {
t.Fatalf("unable to gen route: %v", err)
}
invoice, htlc, err = generatePayment(htlcAmt, htlcAmt, totalTimelock, blob)
invoice, htlc, _, err = generatePayment(
htlcAmt, htlcAmt, totalTimelock, blob,
)
if err != nil {
t.Fatalf("unable to create payment: %v", err)
}
@@ -2314,7 +2320,9 @@ func TestChannelLinkBandwidthConsistencyOverflow(t *testing.T) {
var htlcID uint64
addLinkHTLC := func(id uint64, amt lnwire.MilliSatoshi) [32]byte {
invoice, htlc, err := generatePayment(amt, amt, 5, mockBlob)
invoice, htlc, _, err := generatePayment(
amt, amt, 5, mockBlob,
)
if err != nil {
t.Fatalf("unable to create payment: %v", err)
}
@@ -2580,7 +2588,7 @@ func TestChannelLinkTrimCircuitsPending(t *testing.T) {
// message for the test.
var mockBlob [lnwire.OnionPacketSize]byte
htlcAmt := lnwire.NewMSatFromSatoshis(btcutil.SatoshiPerBitcoin)
_, htlc, err := generatePayment(htlcAmt, htlcAmt, 5, mockBlob)
_, htlc, _, err := generatePayment(htlcAmt, htlcAmt, 5, mockBlob)
if err != nil {
t.Fatalf("unable to create payment: %v", err)
}
@@ -2860,7 +2868,7 @@ func TestChannelLinkTrimCircuitsNoCommit(t *testing.T) {
// message for the test.
var mockBlob [lnwire.OnionPacketSize]byte
htlcAmt := lnwire.NewMSatFromSatoshis(btcutil.SatoshiPerBitcoin)
_, htlc, err := generatePayment(htlcAmt, htlcAmt, 5, mockBlob)
_, htlc, _, err := generatePayment(htlcAmt, htlcAmt, 5, mockBlob)
if err != nil {
t.Fatalf("unable to create payment: %v", err)
}
@@ -3113,7 +3121,7 @@ func TestChannelLinkBandwidthChanReserve(t *testing.T) {
// a switch initiated payment. The resulting bandwidth should
// now be decremented to reflect the new HTLC.
htlcAmt := lnwire.NewMSatFromSatoshis(3 * btcutil.SatoshiPerBitcoin)
invoice, htlc, err := generatePayment(htlcAmt, htlcAmt, 5, mockBlob)
invoice, htlc, _, err := generatePayment(htlcAmt, htlcAmt, 5, mockBlob)
if err != nil {
t.Fatalf("unable to create payment: %v", err)
}
@@ -3844,8 +3852,9 @@ func TestChannelLinkAcceptDuplicatePayment(t *testing.T) {
if err != nil {
t.Fatal(err)
}
invoice, htlc, err := generatePayment(amount, htlcAmt, totalTimelock,
blob)
invoice, htlc, pid, err := generatePayment(
amount, htlcAmt, totalTimelock, blob,
)
if err != nil {
t.Fatal(err)
}
@@ -3859,7 +3868,7 @@ func TestChannelLinkAcceptDuplicatePayment(t *testing.T) {
// payment. It should succeed w/o any issues as it has been crafted
// properly.
_, err = n.aliceServer.htlcSwitch.SendHTLC(
n.firstBobChannelLink.ShortChanID(), htlc,
n.firstBobChannelLink.ShortChanID(), pid, htlc,
newMockDeobfuscator(),
)
if err != nil {
@@ -3869,7 +3878,7 @@ func TestChannelLinkAcceptDuplicatePayment(t *testing.T) {
// Now, if we attempt to send the payment *again* it should be rejected
// as it's a duplicate request.
_, err = n.aliceServer.htlcSwitch.SendHTLC(
n.firstBobChannelLink.ShortChanID(), htlc,
n.firstBobChannelLink.ShortChanID(), pid, htlc,
newMockDeobfuscator(),
)
if err != ErrAlreadyPaid {
@@ -4255,7 +4264,7 @@ func generateHtlcAndInvoice(t *testing.T,
t.Fatalf("unable to generate route: %v", err)
}
invoice, htlc, err := generatePayment(
invoice, htlc, _, err := generatePayment(
htlcAmt, htlcAmt, uint32(htlcExpiry), blob,
)
if err != nil {