2019-09-17 18:11:19 +03:00
|
|
|
package htlcswitch
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/sha256"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/lightningnetwork/lnd/lntypes"
|
|
|
|
"github.com/lightningnetwork/lnd/lnwallet"
|
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
|
|
|
)
|
|
|
|
|
2019-09-17 18:41:22 +03:00
|
|
|
type linkTestContext struct {
|
|
|
|
t *testing.T
|
|
|
|
|
|
|
|
aliceLink ChannelLink
|
|
|
|
bobChannel *lnwallet.LightningChannel
|
|
|
|
aliceMsgs <-chan lnwire.Message
|
|
|
|
}
|
|
|
|
|
2019-09-17 18:11:19 +03:00
|
|
|
// sendHtlcBobToAlice sends an HTLC from Bob to Alice, that pays to a preimage
|
|
|
|
// already in Alice's registry.
|
2019-09-17 18:41:22 +03:00
|
|
|
func (l *linkTestContext) sendHtlcBobToAlice(htlc *lnwire.UpdateAddHTLC) {
|
|
|
|
l.t.Helper()
|
2019-09-17 18:11:19 +03:00
|
|
|
|
2019-09-17 18:41:22 +03:00
|
|
|
_, err := l.bobChannel.AddHTLC(htlc, nil)
|
2019-09-17 18:11:19 +03:00
|
|
|
if err != nil {
|
2019-09-17 18:41:22 +03:00
|
|
|
l.t.Fatalf("bob failed adding htlc: %v", err)
|
2019-09-17 18:11:19 +03:00
|
|
|
}
|
|
|
|
|
2019-09-17 18:41:22 +03:00
|
|
|
l.aliceLink.HandleChannelUpdate(htlc)
|
2019-09-17 18:11:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// sendHtlcAliceToBob sends an HTLC from Alice to Bob, by first committing the
|
|
|
|
// HTLC in the circuit map, then delivering the outgoing packet to Alice's link.
|
|
|
|
// The HTLC will be sent to Bob via Alice's message stream.
|
2019-09-17 18:41:22 +03:00
|
|
|
func (l *linkTestContext) sendHtlcAliceToBob(htlcID int,
|
2019-09-17 18:11:19 +03:00
|
|
|
htlc *lnwire.UpdateAddHTLC) {
|
|
|
|
|
2019-09-17 18:41:22 +03:00
|
|
|
l.t.Helper()
|
2019-09-17 18:11:19 +03:00
|
|
|
|
2019-09-17 18:41:22 +03:00
|
|
|
circuitMap := l.aliceLink.(*channelLink).cfg.Switch.circuits
|
2019-09-17 18:11:19 +03:00
|
|
|
fwdActions, err := circuitMap.CommitCircuits(
|
|
|
|
&PaymentCircuit{
|
|
|
|
Incoming: CircuitKey{
|
|
|
|
HtlcID: uint64(htlcID),
|
|
|
|
},
|
|
|
|
PaymentHash: htlc.PaymentHash,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
2019-09-17 18:41:22 +03:00
|
|
|
l.t.Fatalf("unable to commit circuit: %v", err)
|
2019-09-17 18:11:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(fwdActions.Adds) != 1 {
|
2019-09-17 18:41:22 +03:00
|
|
|
l.t.Fatalf("expected 1 adds, found %d", len(fwdActions.Adds))
|
2019-09-17 18:11:19 +03:00
|
|
|
}
|
|
|
|
|
2019-09-17 18:41:22 +03:00
|
|
|
err = l.aliceLink.HandleSwitchPacket(&htlcPacket{
|
2019-09-17 18:11:19 +03:00
|
|
|
incomingHTLCID: uint64(htlcID),
|
|
|
|
htlc: htlc,
|
|
|
|
})
|
2019-09-17 18:41:22 +03:00
|
|
|
if err != nil {
|
|
|
|
l.t.Fatal(err)
|
|
|
|
}
|
2019-09-17 18:11:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// receiveHtlcAliceToBob pulls the next message from Alice's message stream,
|
|
|
|
// asserts that it is an UpdateAddHTLC, then applies it to Bob's state machine.
|
2019-09-17 18:41:22 +03:00
|
|
|
func (l *linkTestContext) receiveHtlcAliceToBob() {
|
|
|
|
l.t.Helper()
|
2019-09-17 18:11:19 +03:00
|
|
|
|
|
|
|
var msg lnwire.Message
|
|
|
|
select {
|
2019-09-17 18:41:22 +03:00
|
|
|
case msg = <-l.aliceMsgs:
|
2019-09-17 18:11:19 +03:00
|
|
|
case <-time.After(15 * time.Second):
|
2019-09-17 18:41:22 +03:00
|
|
|
l.t.Fatalf("did not received htlc from alice")
|
2019-09-17 18:11:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
htlcAdd, ok := msg.(*lnwire.UpdateAddHTLC)
|
|
|
|
if !ok {
|
2019-09-17 18:41:22 +03:00
|
|
|
l.t.Fatalf("expected UpdateAddHTLC, got %T", msg)
|
2019-09-17 18:11:19 +03:00
|
|
|
}
|
|
|
|
|
2019-09-17 18:41:22 +03:00
|
|
|
_, err := l.bobChannel.ReceiveHTLC(htlcAdd)
|
2019-09-17 18:11:19 +03:00
|
|
|
if err != nil {
|
2019-09-17 18:41:22 +03:00
|
|
|
l.t.Fatalf("bob failed receiving htlc: %v", err)
|
2019-09-17 18:11:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// sendCommitSigBobToAlice makes Bob sign a new commitment and send it to
|
|
|
|
// Alice, asserting that it signs expHtlcs number of HTLCs.
|
2019-09-17 18:41:22 +03:00
|
|
|
func (l *linkTestContext) sendCommitSigBobToAlice(expHtlcs int) {
|
|
|
|
l.t.Helper()
|
2019-09-17 18:11:19 +03:00
|
|
|
|
2019-09-17 18:41:22 +03:00
|
|
|
sig, htlcSigs, _, err := l.bobChannel.SignNextCommitment()
|
2019-09-17 18:11:19 +03:00
|
|
|
if err != nil {
|
2019-09-17 18:41:22 +03:00
|
|
|
l.t.Fatalf("error signing commitment: %v", err)
|
2019-09-17 18:11:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
commitSig := &lnwire.CommitSig{
|
|
|
|
CommitSig: sig,
|
|
|
|
HtlcSigs: htlcSigs,
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(commitSig.HtlcSigs) != expHtlcs {
|
2019-09-17 18:41:22 +03:00
|
|
|
l.t.Fatalf("Expected %d htlc sigs, got %d", expHtlcs,
|
2019-09-17 18:11:19 +03:00
|
|
|
len(commitSig.HtlcSigs))
|
|
|
|
}
|
|
|
|
|
2019-09-17 18:41:22 +03:00
|
|
|
l.aliceLink.HandleChannelUpdate(commitSig)
|
2019-09-17 18:11:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// receiveRevAndAckAliceToBob waits for Alice to send a RevAndAck to Bob, then
|
|
|
|
// hands this to Bob.
|
2019-09-17 18:41:22 +03:00
|
|
|
func (l *linkTestContext) receiveRevAndAckAliceToBob() {
|
|
|
|
l.t.Helper()
|
2019-09-17 18:11:19 +03:00
|
|
|
|
|
|
|
var msg lnwire.Message
|
|
|
|
select {
|
2019-09-17 18:41:22 +03:00
|
|
|
case msg = <-l.aliceMsgs:
|
2019-09-17 18:11:19 +03:00
|
|
|
case <-time.After(15 * time.Second):
|
2019-09-17 18:41:22 +03:00
|
|
|
l.t.Fatalf("did not receive message")
|
2019-09-17 18:11:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
rev, ok := msg.(*lnwire.RevokeAndAck)
|
|
|
|
if !ok {
|
2019-09-17 18:41:22 +03:00
|
|
|
l.t.Fatalf("expected RevokeAndAck, got %T", msg)
|
2019-09-17 18:11:19 +03:00
|
|
|
}
|
|
|
|
|
2019-09-17 18:41:22 +03:00
|
|
|
_, _, _, _, err := l.bobChannel.ReceiveRevocation(rev)
|
2019-09-17 18:11:19 +03:00
|
|
|
if err != nil {
|
2019-09-17 18:41:22 +03:00
|
|
|
l.t.Fatalf("bob failed receiving revocation: %v", err)
|
2019-09-17 18:11:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// receiveCommitSigAliceToBob waits for Alice to send a CommitSig to Bob,
|
|
|
|
// signing expHtlcs numbers of HTLCs, then hands this to Bob.
|
2019-09-17 18:41:22 +03:00
|
|
|
func (l *linkTestContext) receiveCommitSigAliceToBob(expHtlcs int) {
|
|
|
|
l.t.Helper()
|
2019-09-17 18:11:19 +03:00
|
|
|
|
|
|
|
var msg lnwire.Message
|
|
|
|
select {
|
2019-09-17 18:41:22 +03:00
|
|
|
case msg = <-l.aliceMsgs:
|
2019-09-17 18:11:19 +03:00
|
|
|
case <-time.After(15 * time.Second):
|
2019-09-17 18:41:22 +03:00
|
|
|
l.t.Fatalf("did not receive message")
|
2019-09-17 18:11:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
comSig, ok := msg.(*lnwire.CommitSig)
|
|
|
|
if !ok {
|
2019-09-17 18:41:22 +03:00
|
|
|
l.t.Fatalf("expected CommitSig, got %T", msg)
|
2019-09-17 18:11:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(comSig.HtlcSigs) != expHtlcs {
|
2019-09-17 18:41:22 +03:00
|
|
|
l.t.Fatalf("expected %d htlc sigs, got %d", expHtlcs,
|
2019-09-17 18:11:19 +03:00
|
|
|
len(comSig.HtlcSigs))
|
|
|
|
}
|
2019-09-17 18:41:22 +03:00
|
|
|
err := l.bobChannel.ReceiveNewCommitment(comSig.CommitSig,
|
2019-09-17 18:11:19 +03:00
|
|
|
comSig.HtlcSigs)
|
|
|
|
if err != nil {
|
2019-09-17 18:41:22 +03:00
|
|
|
l.t.Fatalf("bob failed receiving commitment: %v", err)
|
2019-09-17 18:11:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// sendRevAndAckBobToAlice make Bob revoke his current commitment, then hand
|
|
|
|
// the RevokeAndAck to Alice.
|
2019-09-17 18:41:22 +03:00
|
|
|
func (l *linkTestContext) sendRevAndAckBobToAlice() {
|
|
|
|
l.t.Helper()
|
2019-09-17 18:11:19 +03:00
|
|
|
|
2019-09-17 18:41:22 +03:00
|
|
|
rev, _, err := l.bobChannel.RevokeCurrentCommitment()
|
2019-09-17 18:11:19 +03:00
|
|
|
if err != nil {
|
2019-09-17 18:41:22 +03:00
|
|
|
l.t.Fatalf("unable to revoke commitment: %v", err)
|
2019-09-17 18:11:19 +03:00
|
|
|
}
|
|
|
|
|
2019-09-17 18:41:22 +03:00
|
|
|
l.aliceLink.HandleChannelUpdate(rev)
|
2019-09-17 18:11:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// receiveSettleAliceToBob waits for Alice to send a HTLC settle message to
|
|
|
|
// Bob, then hands this to Bob.
|
2019-09-17 18:41:22 +03:00
|
|
|
func (l *linkTestContext) receiveSettleAliceToBob() {
|
|
|
|
l.t.Helper()
|
2019-09-17 18:11:19 +03:00
|
|
|
|
|
|
|
var msg lnwire.Message
|
|
|
|
select {
|
2019-09-17 18:41:22 +03:00
|
|
|
case msg = <-l.aliceMsgs:
|
2019-09-17 18:11:19 +03:00
|
|
|
case <-time.After(15 * time.Second):
|
2019-09-17 18:41:22 +03:00
|
|
|
l.t.Fatalf("did not receive message")
|
2019-09-17 18:11:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
settleMsg, ok := msg.(*lnwire.UpdateFulfillHTLC)
|
|
|
|
if !ok {
|
2019-09-17 18:41:22 +03:00
|
|
|
l.t.Fatalf("expected UpdateFulfillHTLC, got %T", msg)
|
2019-09-17 18:11:19 +03:00
|
|
|
}
|
|
|
|
|
2019-09-17 18:41:22 +03:00
|
|
|
err := l.bobChannel.ReceiveHTLCSettle(settleMsg.PaymentPreimage,
|
2019-09-17 18:11:19 +03:00
|
|
|
settleMsg.ID)
|
|
|
|
if err != nil {
|
2019-09-17 18:41:22 +03:00
|
|
|
l.t.Fatalf("failed settling htlc: %v", err)
|
2019-09-17 18:11:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// sendSettleBobToAlice settles an HTLC on Bob's state machine, then sends an
|
|
|
|
// UpdateFulfillHTLC message to Alice's upstream inbox.
|
2019-09-17 18:41:22 +03:00
|
|
|
func (l *linkTestContext) sendSettleBobToAlice(htlcID uint64,
|
2019-09-17 18:11:19 +03:00
|
|
|
preimage lntypes.Preimage) {
|
|
|
|
|
2019-09-17 18:41:22 +03:00
|
|
|
l.t.Helper()
|
2019-09-17 18:11:19 +03:00
|
|
|
|
2019-09-17 18:41:22 +03:00
|
|
|
err := l.bobChannel.SettleHTLC(preimage, htlcID, nil, nil, nil)
|
2019-09-17 18:11:19 +03:00
|
|
|
if err != nil {
|
2019-09-17 18:41:22 +03:00
|
|
|
l.t.Fatalf("alice failed settling htlc id=%d hash=%x",
|
2019-09-17 18:11:19 +03:00
|
|
|
htlcID, sha256.Sum256(preimage[:]))
|
|
|
|
}
|
|
|
|
|
|
|
|
settle := &lnwire.UpdateFulfillHTLC{
|
|
|
|
ID: htlcID,
|
|
|
|
PaymentPreimage: preimage,
|
|
|
|
}
|
|
|
|
|
2019-09-17 18:41:22 +03:00
|
|
|
l.aliceLink.HandleChannelUpdate(settle)
|
2019-09-17 18:11:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// receiveSettleAliceToBob waits for Alice to send a HTLC settle message to
|
|
|
|
// Bob, then hands this to Bob.
|
2019-09-17 18:41:22 +03:00
|
|
|
func (l *linkTestContext) receiveFailAliceToBob() {
|
|
|
|
l.t.Helper()
|
2019-09-17 18:11:19 +03:00
|
|
|
|
|
|
|
var msg lnwire.Message
|
|
|
|
select {
|
2019-09-17 18:41:22 +03:00
|
|
|
case msg = <-l.aliceMsgs:
|
2019-09-17 18:11:19 +03:00
|
|
|
case <-time.After(15 * time.Second):
|
2019-09-17 18:41:22 +03:00
|
|
|
l.t.Fatalf("did not receive message")
|
2019-09-17 18:11:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
failMsg, ok := msg.(*lnwire.UpdateFailHTLC)
|
|
|
|
if !ok {
|
2019-09-17 18:41:22 +03:00
|
|
|
l.t.Fatalf("expected UpdateFailHTLC, got %T", msg)
|
2019-09-17 18:11:19 +03:00
|
|
|
}
|
|
|
|
|
2019-09-17 18:41:22 +03:00
|
|
|
err := l.bobChannel.ReceiveFailHTLC(failMsg.ID, failMsg.Reason)
|
2019-09-17 18:11:19 +03:00
|
|
|
if err != nil {
|
2019-09-17 18:41:22 +03:00
|
|
|
l.t.Fatalf("unable to apply received fail htlc: %v", err)
|
2019-09-17 18:11:19 +03:00
|
|
|
}
|
|
|
|
}
|