github.com/decred/dcrlnd@v0.7.6/htlcswitch/link_isolated_test.go (about)

     1  package htlcswitch
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/decred/dcrlnd/lntypes"
     8  	"github.com/decred/dcrlnd/lnwallet"
     9  	"github.com/decred/dcrlnd/lnwire"
    10  )
    11  
    12  type linkTestContext struct {
    13  	t *testing.T
    14  
    15  	aliceLink  ChannelLink
    16  	bobChannel *lnwallet.LightningChannel
    17  	aliceMsgs  <-chan lnwire.Message
    18  }
    19  
    20  // sendHtlcBobToAlice sends an HTLC from Bob to Alice, that pays to a preimage
    21  // already in Alice's registry.
    22  func (l *linkTestContext) sendHtlcBobToAlice(htlc *lnwire.UpdateAddHTLC) {
    23  	l.t.Helper()
    24  
    25  	_, err := l.bobChannel.AddHTLC(htlc, nil)
    26  	if err != nil {
    27  		l.t.Fatalf("bob failed adding htlc: %v", err)
    28  	}
    29  
    30  	l.aliceLink.HandleChannelUpdate(htlc)
    31  }
    32  
    33  // sendHtlcAliceToBob sends an HTLC from Alice to Bob, by first committing the
    34  // HTLC in the circuit map, then delivering the outgoing packet to Alice's link.
    35  // The HTLC will be sent to Bob via Alice's message stream.
    36  func (l *linkTestContext) sendHtlcAliceToBob(htlcID int,
    37  	htlc *lnwire.UpdateAddHTLC) {
    38  
    39  	l.t.Helper()
    40  
    41  	circuitMap := l.aliceLink.(*channelLink).cfg.Switch.circuits
    42  	fwdActions, err := circuitMap.CommitCircuits(
    43  		&PaymentCircuit{
    44  			Incoming: CircuitKey{
    45  				HtlcID: uint64(htlcID),
    46  			},
    47  			PaymentHash: htlc.PaymentHash,
    48  		},
    49  	)
    50  	if err != nil {
    51  		l.t.Fatalf("unable to commit circuit: %v", err)
    52  	}
    53  
    54  	if len(fwdActions.Adds) != 1 {
    55  		l.t.Fatalf("expected 1 adds, found %d", len(fwdActions.Adds))
    56  	}
    57  
    58  	err = l.aliceLink.handleSwitchPacket(&htlcPacket{
    59  		incomingHTLCID: uint64(htlcID),
    60  		htlc:           htlc,
    61  	})
    62  	if err != nil {
    63  		l.t.Fatal(err)
    64  	}
    65  }
    66  
    67  // receiveHtlcAliceToBob pulls the next message from Alice's message stream,
    68  // asserts that it is an UpdateAddHTLC, then applies it to Bob's state machine.
    69  func (l *linkTestContext) receiveHtlcAliceToBob() {
    70  	l.t.Helper()
    71  
    72  	var msg lnwire.Message
    73  	select {
    74  	case msg = <-l.aliceMsgs:
    75  	case <-time.After(15 * time.Second):
    76  		l.t.Fatalf("did not received htlc from alice")
    77  	}
    78  
    79  	htlcAdd, ok := msg.(*lnwire.UpdateAddHTLC)
    80  	if !ok {
    81  		l.t.Fatalf("expected UpdateAddHTLC, got %T", msg)
    82  	}
    83  
    84  	_, err := l.bobChannel.ReceiveHTLC(htlcAdd)
    85  	if err != nil {
    86  		l.t.Fatalf("bob failed receiving htlc: %v", err)
    87  	}
    88  }
    89  
    90  // sendCommitSigBobToAlice makes Bob sign a new commitment and send it to
    91  // Alice, asserting that it signs expHtlcs number of HTLCs.
    92  func (l *linkTestContext) sendCommitSigBobToAlice(expHtlcs int) {
    93  	l.t.Helper()
    94  
    95  	sig, htlcSigs, _, err := l.bobChannel.SignNextCommitment()
    96  	if err != nil {
    97  		l.t.Fatalf("error signing commitment: %v", err)
    98  	}
    99  
   100  	commitSig := &lnwire.CommitSig{
   101  		CommitSig: sig,
   102  		HtlcSigs:  htlcSigs,
   103  	}
   104  
   105  	if len(commitSig.HtlcSigs) != expHtlcs {
   106  		l.t.Fatalf("Expected %d htlc sigs, got %d", expHtlcs,
   107  			len(commitSig.HtlcSigs))
   108  	}
   109  
   110  	l.aliceLink.HandleChannelUpdate(commitSig)
   111  }
   112  
   113  // receiveRevAndAckAliceToBob waits for Alice to send a RevAndAck to Bob, then
   114  // hands this to Bob.
   115  func (l *linkTestContext) receiveRevAndAckAliceToBob() {
   116  	l.t.Helper()
   117  
   118  	var msg lnwire.Message
   119  	select {
   120  	case msg = <-l.aliceMsgs:
   121  	case <-time.After(15 * time.Second):
   122  		l.t.Fatalf("did not receive message")
   123  	}
   124  
   125  	rev, ok := msg.(*lnwire.RevokeAndAck)
   126  	if !ok {
   127  		l.t.Fatalf("expected RevokeAndAck, got %T", msg)
   128  	}
   129  
   130  	_, _, _, _, err := l.bobChannel.ReceiveRevocation(rev)
   131  	if err != nil {
   132  		l.t.Fatalf("bob failed receiving revocation: %v", err)
   133  	}
   134  }
   135  
   136  // receiveCommitSigAliceToBob waits for Alice to send a CommitSig to Bob,
   137  // signing expHtlcs numbers of HTLCs, then hands this to Bob.
   138  func (l *linkTestContext) receiveCommitSigAliceToBob(expHtlcs int) {
   139  	l.t.Helper()
   140  
   141  	comSig := l.receiveCommitSigAlice(expHtlcs)
   142  
   143  	err := l.bobChannel.ReceiveNewCommitment(
   144  		comSig.CommitSig, comSig.HtlcSigs,
   145  	)
   146  	if err != nil {
   147  		l.t.Fatalf("bob failed receiving commitment: %v", err)
   148  	}
   149  }
   150  
   151  // receiveCommitSigAlice waits for Alice to send a CommitSig, signing expHtlcs
   152  // numbers of HTLCs.
   153  func (l *linkTestContext) receiveCommitSigAlice(expHtlcs int) *lnwire.CommitSig {
   154  	l.t.Helper()
   155  
   156  	var msg lnwire.Message
   157  	select {
   158  	case msg = <-l.aliceMsgs:
   159  	case <-time.After(15 * time.Second):
   160  		l.t.Fatalf("did not receive message")
   161  	}
   162  
   163  	comSig, ok := msg.(*lnwire.CommitSig)
   164  	if !ok {
   165  		l.t.Fatalf("expected CommitSig, got %T", msg)
   166  	}
   167  
   168  	if len(comSig.HtlcSigs) != expHtlcs {
   169  		l.t.Fatalf("expected %d htlc sigs, got %d", expHtlcs,
   170  			len(comSig.HtlcSigs))
   171  	}
   172  
   173  	return comSig
   174  }
   175  
   176  // sendRevAndAckBobToAlice make Bob revoke his current commitment, then hand
   177  // the RevokeAndAck to Alice.
   178  func (l *linkTestContext) sendRevAndAckBobToAlice() {
   179  	l.t.Helper()
   180  
   181  	rev, _, err := l.bobChannel.RevokeCurrentCommitment()
   182  	if err != nil {
   183  		l.t.Fatalf("unable to revoke commitment: %v", err)
   184  	}
   185  
   186  	l.aliceLink.HandleChannelUpdate(rev)
   187  }
   188  
   189  // receiveSettleAliceToBob waits for Alice to send a HTLC settle message to
   190  // Bob, then hands this to Bob.
   191  func (l *linkTestContext) receiveSettleAliceToBob() {
   192  	l.t.Helper()
   193  
   194  	var msg lnwire.Message
   195  	select {
   196  	case msg = <-l.aliceMsgs:
   197  	case <-time.After(15 * time.Second):
   198  		l.t.Fatalf("did not receive message")
   199  	}
   200  
   201  	settleMsg, ok := msg.(*lnwire.UpdateFulfillHTLC)
   202  	if !ok {
   203  		l.t.Fatalf("expected UpdateFulfillHTLC, got %T", msg)
   204  	}
   205  
   206  	err := l.bobChannel.ReceiveHTLCSettle(settleMsg.PaymentPreimage,
   207  		settleMsg.ID)
   208  	if err != nil {
   209  		l.t.Fatalf("failed settling htlc: %v", err)
   210  	}
   211  }
   212  
   213  // sendSettleBobToAlice settles an HTLC on Bob's state machine, then sends an
   214  // UpdateFulfillHTLC message to Alice's upstream inbox.
   215  func (l *linkTestContext) sendSettleBobToAlice(htlcID uint64,
   216  	preimage lntypes.Preimage) {
   217  
   218  	l.t.Helper()
   219  
   220  	err := l.bobChannel.SettleHTLC(preimage, htlcID, nil, nil, nil)
   221  	if err != nil {
   222  		l.t.Fatalf("alice failed settling htlc id=%d hash=%x",
   223  			htlcID, preimage.Hash())
   224  	}
   225  
   226  	settle := &lnwire.UpdateFulfillHTLC{
   227  		ID:              htlcID,
   228  		PaymentPreimage: preimage,
   229  	}
   230  
   231  	l.aliceLink.HandleChannelUpdate(settle)
   232  }
   233  
   234  // receiveSettleAliceToBob waits for Alice to send a HTLC settle message to
   235  // Bob, then hands this to Bob.
   236  func (l *linkTestContext) receiveFailAliceToBob() {
   237  	l.t.Helper()
   238  
   239  	var msg lnwire.Message
   240  	select {
   241  	case msg = <-l.aliceMsgs:
   242  	case <-time.After(15 * time.Second):
   243  		l.t.Fatalf("did not receive message")
   244  	}
   245  
   246  	failMsg, ok := msg.(*lnwire.UpdateFailHTLC)
   247  	if !ok {
   248  		l.t.Fatalf("expected UpdateFailHTLC, got %T", msg)
   249  	}
   250  
   251  	err := l.bobChannel.ReceiveFailHTLC(failMsg.ID, failMsg.Reason)
   252  	if err != nil {
   253  		l.t.Fatalf("unable to apply received fail htlc: %v", err)
   254  	}
   255  }
   256  
   257  // assertNoMsgFromAlice asserts that Alice hasn't sent a message. Before
   258  // calling, make sure that Alice has had the opportunity to send the message.
   259  func (l *linkTestContext) assertNoMsgFromAlice(timeout time.Duration) {
   260  	l.t.Helper()
   261  
   262  	select {
   263  	case msg := <-l.aliceMsgs:
   264  		l.t.Fatalf("unexpected message from Alice: %v", msg)
   265  	case <-time.After(timeout):
   266  	}
   267  }