2017-05-02 02:29:30 +03:00
|
|
|
package htlcswitch
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2017-09-25 22:50:00 +03:00
|
|
|
"runtime"
|
2017-10-03 08:24:49 +03:00
|
|
|
"strings"
|
2017-09-25 22:50:00 +03:00
|
|
|
"sync"
|
2017-05-02 02:29:30 +03:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"io"
|
|
|
|
|
2017-06-25 20:27:36 +03:00
|
|
|
"math"
|
|
|
|
|
2017-05-02 02:29:30 +03:00
|
|
|
"github.com/davecgh/go-spew/spew"
|
2017-07-09 02:04:49 +03:00
|
|
|
"github.com/go-errors/errors"
|
2017-09-25 22:50:00 +03:00
|
|
|
"github.com/lightningnetwork/lnd/chainntnfs"
|
2017-07-09 02:30:20 +03:00
|
|
|
"github.com/lightningnetwork/lnd/channeldb"
|
2018-01-17 07:18:53 +03:00
|
|
|
"github.com/lightningnetwork/lnd/contractcourt"
|
2017-05-02 02:29:30 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnwallet"
|
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
|
|
|
"github.com/roasbeef/btcd/chaincfg/chainhash"
|
2017-11-23 10:15:21 +03:00
|
|
|
"github.com/roasbeef/btcd/wire"
|
2017-05-02 02:29:30 +03:00
|
|
|
"github.com/roasbeef/btcutil"
|
|
|
|
)
|
|
|
|
|
2017-08-03 07:11:31 +03:00
|
|
|
const (
|
|
|
|
testStartingHeight = 100
|
|
|
|
)
|
|
|
|
|
2017-11-12 02:05:09 +03:00
|
|
|
// concurrentTester is a thread-safe wrapper around the Fatalf method of a
|
|
|
|
// *testing.T instance. With this wrapper multiple goroutines can safely
|
|
|
|
// attempt to fail a test concurrently.
|
|
|
|
type concurrentTester struct {
|
|
|
|
mtx sync.Mutex
|
|
|
|
*testing.T
|
|
|
|
}
|
|
|
|
|
|
|
|
func newConcurrentTester(t *testing.T) *concurrentTester {
|
|
|
|
return &concurrentTester{
|
|
|
|
T: t,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *concurrentTester) Fatalf(format string, args ...interface{}) {
|
|
|
|
c.mtx.Lock()
|
|
|
|
defer c.mtx.Unlock()
|
|
|
|
|
|
|
|
c.T.Fatalf(format, args)
|
|
|
|
}
|
|
|
|
|
2017-07-31 00:10:27 +03:00
|
|
|
// messageToString is used to produce less spammy log messages in trace mode by
|
|
|
|
// setting the 'Curve" parameter to nil. Doing this avoids printing out each of
|
|
|
|
// the field elements in the curve parameters for secp256k1.
|
2017-05-02 02:29:30 +03:00
|
|
|
func messageToString(msg lnwire.Message) string {
|
|
|
|
switch m := msg.(type) {
|
|
|
|
case *lnwire.RevokeAndAck:
|
|
|
|
m.NextRevocationKey.Curve = nil
|
|
|
|
case *lnwire.NodeAnnouncement:
|
|
|
|
m.NodeID.Curve = nil
|
|
|
|
case *lnwire.ChannelAnnouncement:
|
|
|
|
m.NodeID1.Curve = nil
|
|
|
|
m.NodeID2.Curve = nil
|
|
|
|
m.BitcoinKey1.Curve = nil
|
|
|
|
m.BitcoinKey2.Curve = nil
|
2017-07-31 00:10:27 +03:00
|
|
|
case *lnwire.AcceptChannel:
|
|
|
|
m.FundingKey.Curve = nil
|
|
|
|
m.RevocationPoint.Curve = nil
|
|
|
|
m.PaymentPoint.Curve = nil
|
|
|
|
m.DelayedPaymentPoint.Curve = nil
|
|
|
|
m.FirstCommitmentPoint.Curve = nil
|
|
|
|
case *lnwire.OpenChannel:
|
|
|
|
m.FundingKey.Curve = nil
|
|
|
|
m.RevocationPoint.Curve = nil
|
|
|
|
m.PaymentPoint.Curve = nil
|
|
|
|
m.DelayedPaymentPoint.Curve = nil
|
|
|
|
m.FirstCommitmentPoint.Curve = nil
|
|
|
|
case *lnwire.FundingLocked:
|
|
|
|
m.NextPerCommitmentPoint.Curve = nil
|
2017-05-02 02:29:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return spew.Sdump(msg)
|
|
|
|
}
|
|
|
|
|
2017-11-11 02:09:19 +03:00
|
|
|
// expectedMessage struct holds the message which travels from one peer to
|
|
|
|
// another, and additional information like, should this message we skipped for
|
|
|
|
// handling.
|
2017-07-09 01:46:27 +03:00
|
|
|
type expectedMessage struct {
|
|
|
|
from string
|
|
|
|
to string
|
|
|
|
message lnwire.Message
|
|
|
|
skip bool
|
|
|
|
}
|
|
|
|
|
2017-05-02 02:29:30 +03:00
|
|
|
// createLogFunc is a helper function which returns the function which will be
|
|
|
|
// used for logging message are received from another peer.
|
|
|
|
func createLogFunc(name string, channelID lnwire.ChannelID) messageInterceptor {
|
2017-07-09 01:46:27 +03:00
|
|
|
return func(m lnwire.Message) (bool, error) {
|
|
|
|
chanID, err := getChanID(m)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if chanID == channelID {
|
2017-05-02 02:29:30 +03:00
|
|
|
fmt.Printf("---------------------- \n %v received: "+
|
|
|
|
"%v", name, messageToString(m))
|
|
|
|
}
|
2017-07-09 01:46:27 +03:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// createInterceptorFunc creates the function by the given set of messages
|
|
|
|
// which, checks the order of the messages and skip the ones which were
|
|
|
|
// indicated to be intercepted.
|
2017-07-09 02:30:20 +03:00
|
|
|
func createInterceptorFunc(prefix, receiver string, messages []expectedMessage,
|
2017-07-09 01:46:27 +03:00
|
|
|
chanID lnwire.ChannelID, debug bool) messageInterceptor {
|
|
|
|
|
|
|
|
// Filter message which should be received with given peer name.
|
|
|
|
var expectToReceive []expectedMessage
|
|
|
|
for _, message := range messages {
|
2017-07-09 02:30:20 +03:00
|
|
|
if message.to == receiver {
|
2017-07-09 01:46:27 +03:00
|
|
|
expectToReceive = append(expectToReceive, message)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return function which checks the message order and skip the
|
|
|
|
// messages.
|
|
|
|
return func(m lnwire.Message) (bool, error) {
|
|
|
|
messageChanID, err := getChanID(m)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if messageChanID == chanID {
|
|
|
|
if len(expectToReceive) == 0 {
|
2017-11-11 02:09:19 +03:00
|
|
|
return false, errors.Errorf("%v received "+
|
|
|
|
"unexpected message out of range: %v",
|
|
|
|
receiver, m.MsgType())
|
2017-07-09 01:46:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
expectedMessage := expectToReceive[0]
|
|
|
|
expectToReceive = expectToReceive[1:]
|
|
|
|
|
|
|
|
if expectedMessage.message.MsgType() != m.MsgType() {
|
|
|
|
return false, errors.Errorf("%v received wrong message: \n"+
|
2017-07-09 02:30:20 +03:00
|
|
|
"real: %v\nexpected: %v", receiver, m.MsgType(),
|
2017-07-09 01:46:27 +03:00
|
|
|
expectedMessage.message.MsgType())
|
|
|
|
}
|
|
|
|
|
|
|
|
if debug {
|
2017-07-09 02:30:20 +03:00
|
|
|
var postfix string
|
|
|
|
if revocation, ok := m.(*lnwire.RevokeAndAck); ok {
|
|
|
|
var zeroHash chainhash.Hash
|
|
|
|
if bytes.Equal(zeroHash[:], revocation.Revocation[:]) {
|
|
|
|
postfix = "- empty revocation"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-09 01:46:27 +03:00
|
|
|
if expectedMessage.skip {
|
2017-07-09 02:30:20 +03:00
|
|
|
fmt.Printf("skipped: %v: %v %v \n", prefix,
|
|
|
|
m.MsgType(), postfix)
|
2017-07-09 01:46:27 +03:00
|
|
|
} else {
|
2017-07-09 02:30:20 +03:00
|
|
|
fmt.Printf("%v: %v %v \n", prefix, m.MsgType(), postfix)
|
2017-07-09 01:46:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return expectedMessage.skip, nil
|
|
|
|
}
|
|
|
|
return false, nil
|
2017-05-02 02:29:30 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestChannelLinkSingleHopPayment in this test we checks the interaction
|
|
|
|
// between Alice and Bob within scope of one channel.
|
|
|
|
func TestChannelLinkSingleHopPayment(t *testing.T) {
|
2017-06-17 01:59:20 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2017-07-09 02:30:20 +03:00
|
|
|
channels, cleanUp, _, err := createClusterChannels(
|
2017-05-02 02:29:30 +03:00
|
|
|
btcutil.SatoshiPerBitcoin*3,
|
2017-07-09 02:30:20 +03:00
|
|
|
btcutil.SatoshiPerBitcoin*5)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create channel: %v", err)
|
|
|
|
}
|
|
|
|
defer cleanUp()
|
|
|
|
|
|
|
|
n := newThreeHopNetwork(t, channels.aliceToBob, channels.bobToAlice,
|
2017-11-11 02:09:19 +03:00
|
|
|
channels.bobToCarol, channels.carolToBob, testStartingHeight)
|
2017-05-02 02:29:30 +03:00
|
|
|
if err := n.start(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer n.stop()
|
|
|
|
|
|
|
|
aliceBandwidthBefore := n.aliceChannelLink.Bandwidth()
|
2017-11-11 02:09:19 +03:00
|
|
|
bobBandwidthBefore := n.firstBobChannelLink.Bandwidth()
|
2017-05-02 02:29:30 +03:00
|
|
|
|
|
|
|
debug := false
|
|
|
|
if debug {
|
|
|
|
// Log message that alice receives.
|
2017-07-09 01:46:27 +03:00
|
|
|
n.aliceServer.intersect(createLogFunc("alice",
|
2017-05-02 02:29:30 +03:00
|
|
|
n.aliceChannelLink.ChanID()))
|
|
|
|
|
|
|
|
// Log message that bob receives.
|
2017-07-09 01:46:27 +03:00
|
|
|
n.bobServer.intersect(createLogFunc("bob",
|
2017-05-02 02:29:30 +03:00
|
|
|
n.firstBobChannelLink.ChanID()))
|
|
|
|
}
|
|
|
|
|
2017-08-22 11:05:32 +03:00
|
|
|
amount := lnwire.NewMSatFromSatoshis(btcutil.SatoshiPerBitcoin)
|
2017-08-03 07:11:31 +03:00
|
|
|
htlcAmt, totalTimelock, hops := generateHops(amount, testStartingHeight,
|
|
|
|
n.firstBobChannelLink)
|
2017-06-17 01:08:19 +03:00
|
|
|
|
2017-05-02 02:29:30 +03:00
|
|
|
// Wait for:
|
|
|
|
// * HTLC add request to be sent to bob.
|
|
|
|
// * alice<->bob commitment state to be updated.
|
|
|
|
// * settle request to be sent back from bob to alice.
|
|
|
|
// * alice<->bob commitment state to be updated.
|
|
|
|
// * user notification to be sent.
|
2017-07-09 02:30:20 +03:00
|
|
|
receiver := n.bobServer
|
|
|
|
rhash, err := n.makePayment(n.aliceServer, receiver,
|
2017-07-09 02:04:49 +03:00
|
|
|
n.bobServer.PubKey(), hops, amount, htlcAmt,
|
2017-11-12 02:06:53 +03:00
|
|
|
totalTimelock).Wait(30 * time.Second)
|
2017-05-02 02:29:30 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to make the payment: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for Bob to receive the revocation.
|
2017-06-17 01:08:19 +03:00
|
|
|
//
|
|
|
|
// TODO(roasbef); replace with select over returned err chan
|
2017-05-02 02:29:30 +03:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
|
|
|
|
// Check that alice invoice was settled and bandwidth of HTLC
|
|
|
|
// links was changed.
|
2017-07-09 02:30:20 +03:00
|
|
|
invoice, err := receiver.registry.LookupInvoice(rhash)
|
|
|
|
if err != nil {
|
2017-11-11 02:09:19 +03:00
|
|
|
t.Fatalf("unable to get invoice: %v", err)
|
2017-07-09 02:30:20 +03:00
|
|
|
}
|
2017-05-02 02:29:30 +03:00
|
|
|
if !invoice.Terms.Settled {
|
2017-07-09 02:30:20 +03:00
|
|
|
t.Fatal("alice invoice wasn't settled")
|
2017-05-02 02:29:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if aliceBandwidthBefore-amount != n.aliceChannelLink.Bandwidth() {
|
2017-09-25 22:50:00 +03:00
|
|
|
t.Fatal("alice bandwidth should have decrease on payment " +
|
2017-05-02 02:29:30 +03:00
|
|
|
"amount")
|
|
|
|
}
|
|
|
|
|
|
|
|
if bobBandwidthBefore+amount != n.firstBobChannelLink.Bandwidth() {
|
2017-11-11 02:09:19 +03:00
|
|
|
t.Fatalf("bob bandwidth isn't match: expected %v, got %v",
|
|
|
|
bobBandwidthBefore+amount,
|
|
|
|
n.firstBobChannelLink.Bandwidth())
|
2017-05-02 02:29:30 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-24 18:27:39 +03:00
|
|
|
// TestChannelLinkBidirectionalOneHopPayments tests the ability of channel
|
|
|
|
// link to cope with bigger number of payment updates that commitment
|
|
|
|
// transaction may consist.
|
|
|
|
func TestChannelLinkBidirectionalOneHopPayments(t *testing.T) {
|
2017-06-17 01:59:20 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2017-07-09 02:30:20 +03:00
|
|
|
channels, cleanUp, _, err := createClusterChannels(
|
2017-05-24 18:27:39 +03:00
|
|
|
btcutil.SatoshiPerBitcoin*3,
|
2017-07-09 02:30:20 +03:00
|
|
|
btcutil.SatoshiPerBitcoin*5)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create channel: %v", err)
|
|
|
|
}
|
|
|
|
defer cleanUp()
|
|
|
|
|
|
|
|
n := newThreeHopNetwork(t, channels.aliceToBob, channels.bobToAlice,
|
2017-11-11 02:09:19 +03:00
|
|
|
channels.bobToCarol, channels.carolToBob, testStartingHeight)
|
2017-05-24 18:27:39 +03:00
|
|
|
if err := n.start(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer n.stop()
|
|
|
|
bobBandwidthBefore := n.firstBobChannelLink.Bandwidth()
|
|
|
|
aliceBandwidthBefore := n.aliceChannelLink.Bandwidth()
|
|
|
|
|
|
|
|
debug := false
|
|
|
|
if debug {
|
|
|
|
// Log message that alice receives.
|
2017-07-09 01:46:27 +03:00
|
|
|
n.aliceServer.intersect(createLogFunc("alice",
|
2017-05-24 18:27:39 +03:00
|
|
|
n.aliceChannelLink.ChanID()))
|
|
|
|
|
|
|
|
// Log message that bob receives.
|
2017-07-09 01:46:27 +03:00
|
|
|
n.bobServer.intersect(createLogFunc("bob",
|
2017-05-24 18:27:39 +03:00
|
|
|
n.firstBobChannelLink.ChanID()))
|
|
|
|
}
|
|
|
|
|
2017-08-22 09:36:43 +03:00
|
|
|
amt := lnwire.NewMSatFromSatoshis(20000)
|
2017-06-17 01:08:19 +03:00
|
|
|
|
|
|
|
htlcAmt, totalTimelock, hopsForwards := generateHops(amt,
|
2017-08-03 07:11:31 +03:00
|
|
|
testStartingHeight, n.firstBobChannelLink)
|
|
|
|
_, _, hopsBackwards := generateHops(amt,
|
|
|
|
testStartingHeight, n.aliceChannelLink)
|
2017-06-17 01:08:19 +03:00
|
|
|
|
2017-06-25 20:27:36 +03:00
|
|
|
type result struct {
|
|
|
|
err error
|
|
|
|
start time.Time
|
|
|
|
number int
|
|
|
|
sender string
|
|
|
|
}
|
|
|
|
|
2017-05-24 18:27:39 +03:00
|
|
|
// Send max available payment number in both sides, thereby testing
|
|
|
|
// the property of channel link to cope with overflowing.
|
|
|
|
count := 2 * lnwallet.MaxHTLCNumber
|
2017-08-01 22:53:07 +03:00
|
|
|
resultChan := make(chan *result, count)
|
2017-05-24 18:27:39 +03:00
|
|
|
for i := 0; i < count/2; i++ {
|
2017-06-25 20:27:36 +03:00
|
|
|
go func(i int) {
|
|
|
|
r := &result{
|
|
|
|
start: time.Now(),
|
|
|
|
number: i,
|
|
|
|
sender: "alice",
|
|
|
|
}
|
|
|
|
|
|
|
|
_, r.err = n.makePayment(n.aliceServer, n.bobServer,
|
2017-06-17 01:08:19 +03:00
|
|
|
n.bobServer.PubKey(), hopsForwards, amt, htlcAmt,
|
2017-11-12 02:06:53 +03:00
|
|
|
totalTimelock).Wait(5 * time.Minute)
|
2017-06-25 20:27:36 +03:00
|
|
|
resultChan <- r
|
|
|
|
}(i)
|
2017-05-24 18:27:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < count/2; i++ {
|
2017-06-25 20:27:36 +03:00
|
|
|
go func(i int) {
|
|
|
|
r := &result{
|
|
|
|
start: time.Now(),
|
|
|
|
number: i,
|
|
|
|
sender: "bob",
|
|
|
|
}
|
|
|
|
|
|
|
|
_, r.err = n.makePayment(n.bobServer, n.aliceServer,
|
2017-06-17 01:08:19 +03:00
|
|
|
n.aliceServer.PubKey(), hopsBackwards, amt, htlcAmt,
|
2017-11-12 02:06:53 +03:00
|
|
|
totalTimelock).Wait(5 * time.Minute)
|
2017-06-25 20:27:36 +03:00
|
|
|
resultChan <- r
|
|
|
|
}(i)
|
2017-05-24 18:27:39 +03:00
|
|
|
}
|
|
|
|
|
2017-06-25 20:27:36 +03:00
|
|
|
maxDelay := time.Duration(0)
|
|
|
|
minDelay := time.Duration(math.MaxInt64)
|
|
|
|
averageDelay := time.Duration(0)
|
|
|
|
|
2017-05-24 18:27:39 +03:00
|
|
|
// Check that alice invoice was settled and bandwidth of HTLC
|
|
|
|
// links was changed.
|
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
select {
|
2017-06-25 20:27:36 +03:00
|
|
|
case r := <-resultChan:
|
|
|
|
if r.err != nil {
|
2017-11-12 02:06:53 +03:00
|
|
|
t.Fatalf("unable to make payment: %v", r.err)
|
2017-05-24 18:27:39 +03:00
|
|
|
}
|
2017-06-25 20:27:36 +03:00
|
|
|
|
|
|
|
delay := time.Since(r.start)
|
|
|
|
if delay > maxDelay {
|
|
|
|
maxDelay = delay
|
|
|
|
}
|
|
|
|
|
|
|
|
if delay < minDelay {
|
|
|
|
minDelay = delay
|
|
|
|
}
|
|
|
|
averageDelay += delay
|
|
|
|
|
2017-08-01 22:53:07 +03:00
|
|
|
case <-time.After(5 * time.Minute):
|
2017-05-24 18:27:39 +03:00
|
|
|
t.Fatalf("timeout: (%v/%v)", i+1, count)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-14 09:51:16 +03:00
|
|
|
// TODO(roasbeef): should instead consume async notifications from both
|
|
|
|
// links
|
2017-11-16 05:24:52 +03:00
|
|
|
time.Sleep(time.Second * 2)
|
|
|
|
|
2017-05-24 18:27:39 +03:00
|
|
|
// At the end Bob and Alice balances should be the same as previous,
|
|
|
|
// because they sent the equal amount of money to each other.
|
|
|
|
if aliceBandwidthBefore != n.aliceChannelLink.Bandwidth() {
|
2017-11-14 09:51:16 +03:00
|
|
|
t.Fatalf("alice bandwidth shouldn't have changed: expected %v, got %x",
|
|
|
|
aliceBandwidthBefore, n.aliceChannelLink.Bandwidth())
|
2017-05-24 18:27:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if bobBandwidthBefore != n.firstBobChannelLink.Bandwidth() {
|
2017-11-14 09:51:16 +03:00
|
|
|
t.Fatalf("bob bandwidth shouldn't have changed: expected %v, got %v",
|
|
|
|
bobBandwidthBefore, n.firstBobChannelLink.Bandwidth())
|
2017-05-24 18:27:39 +03:00
|
|
|
}
|
2017-06-25 20:27:36 +03:00
|
|
|
|
|
|
|
t.Logf("Max waiting: %v", maxDelay)
|
|
|
|
t.Logf("Min waiting: %v", minDelay)
|
|
|
|
t.Logf("Average waiting: %v", time.Duration(int(averageDelay)/count))
|
2017-05-24 18:27:39 +03:00
|
|
|
}
|
|
|
|
|
2017-05-02 02:29:30 +03:00
|
|
|
// TestChannelLinkMultiHopPayment checks the ability to send payment over two
|
2017-07-14 21:40:42 +03:00
|
|
|
// hops. In this test we send the payment from Carol to Alice over Bob peer.
|
2017-05-02 02:29:30 +03:00
|
|
|
// (Carol -> Bob -> Alice) and checking that HTLC was settled properly and
|
|
|
|
// balances were changed in two channels.
|
|
|
|
func TestChannelLinkMultiHopPayment(t *testing.T) {
|
2017-06-17 01:59:20 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2017-07-09 02:30:20 +03:00
|
|
|
channels, cleanUp, _, err := createClusterChannels(
|
2017-05-02 02:29:30 +03:00
|
|
|
btcutil.SatoshiPerBitcoin*3,
|
2017-07-09 02:30:20 +03:00
|
|
|
btcutil.SatoshiPerBitcoin*5)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create channel: %v", err)
|
|
|
|
}
|
|
|
|
defer cleanUp()
|
|
|
|
|
|
|
|
n := newThreeHopNetwork(t, channels.aliceToBob, channels.bobToAlice,
|
2017-11-11 02:09:19 +03:00
|
|
|
channels.bobToCarol, channels.carolToBob, testStartingHeight)
|
2017-05-02 02:29:30 +03:00
|
|
|
if err := n.start(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer n.stop()
|
|
|
|
|
|
|
|
carolBandwidthBefore := n.carolChannelLink.Bandwidth()
|
|
|
|
firstBobBandwidthBefore := n.firstBobChannelLink.Bandwidth()
|
|
|
|
secondBobBandwidthBefore := n.secondBobChannelLink.Bandwidth()
|
|
|
|
aliceBandwidthBefore := n.aliceChannelLink.Bandwidth()
|
|
|
|
|
|
|
|
debug := false
|
|
|
|
if debug {
|
|
|
|
// Log messages that alice receives from bob.
|
2017-07-09 01:46:27 +03:00
|
|
|
n.aliceServer.intersect(createLogFunc("[alice]<-bob<-carol: ",
|
2017-05-02 02:29:30 +03:00
|
|
|
n.aliceChannelLink.ChanID()))
|
|
|
|
|
|
|
|
// Log messages that bob receives from alice.
|
2017-07-09 01:46:27 +03:00
|
|
|
n.bobServer.intersect(createLogFunc("alice->[bob]->carol: ",
|
2017-05-02 02:29:30 +03:00
|
|
|
n.firstBobChannelLink.ChanID()))
|
|
|
|
|
|
|
|
// Log messages that bob receives from carol.
|
2017-07-09 01:46:27 +03:00
|
|
|
n.bobServer.intersect(createLogFunc("alice<-[bob]<-carol: ",
|
2017-05-02 02:29:30 +03:00
|
|
|
n.secondBobChannelLink.ChanID()))
|
|
|
|
|
|
|
|
// Log messages that carol receives from bob.
|
2017-07-09 01:46:27 +03:00
|
|
|
n.carolServer.intersect(createLogFunc("alice->bob->[carol]",
|
2017-05-02 02:29:30 +03:00
|
|
|
n.carolChannelLink.ChanID()))
|
|
|
|
}
|
|
|
|
|
2017-08-22 09:36:43 +03:00
|
|
|
amount := lnwire.NewMSatFromSatoshis(btcutil.SatoshiPerBitcoin)
|
2017-06-17 01:08:19 +03:00
|
|
|
htlcAmt, totalTimelock, hops := generateHops(amount,
|
2017-08-03 07:11:31 +03:00
|
|
|
testStartingHeight,
|
2017-06-17 01:08:19 +03:00
|
|
|
n.firstBobChannelLink, n.carolChannelLink)
|
|
|
|
|
2017-05-02 02:29:30 +03:00
|
|
|
// Wait for:
|
|
|
|
// * HTLC add request to be sent from Alice to Bob.
|
|
|
|
// * Alice<->Bob commitment states to be updated.
|
|
|
|
// * HTLC add request to be propagated to Carol.
|
|
|
|
// * Bob<->Carol commitment state to be updated.
|
|
|
|
// * settle request to be sent back from Carol to Bob.
|
|
|
|
// * Alice<->Bob commitment state to be updated.
|
|
|
|
// * settle request to be sent back from Bob to Alice.
|
|
|
|
// * Alice<->Bob commitment states to be updated.
|
|
|
|
// * user notification to be sent.
|
2017-07-09 02:30:20 +03:00
|
|
|
receiver := n.carolServer
|
|
|
|
rhash, err := n.makePayment(n.aliceServer, n.carolServer,
|
2017-06-17 01:08:19 +03:00
|
|
|
n.bobServer.PubKey(), hops, amount, htlcAmt,
|
2017-11-12 02:06:53 +03:00
|
|
|
totalTimelock).Wait(30 * time.Second)
|
2017-05-02 02:29:30 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send payment: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for Bob to receive the revocation.
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
|
|
|
|
// Check that Carol invoice was settled and bandwidth of HTLC
|
|
|
|
// links were changed.
|
2017-07-09 02:30:20 +03:00
|
|
|
invoice, err := receiver.registry.LookupInvoice(rhash)
|
|
|
|
if err != nil {
|
2018-01-04 23:23:31 +03:00
|
|
|
t.Fatalf("unable to get invoice: %v", err)
|
2017-07-09 02:30:20 +03:00
|
|
|
}
|
2017-05-02 02:29:30 +03:00
|
|
|
if !invoice.Terms.Settled {
|
2017-07-09 02:30:20 +03:00
|
|
|
t.Fatal("carol invoice haven't been settled")
|
2017-05-02 02:29:30 +03:00
|
|
|
}
|
|
|
|
|
2017-06-17 01:08:19 +03:00
|
|
|
expectedAliceBandwidth := aliceBandwidthBefore - htlcAmt
|
|
|
|
if expectedAliceBandwidth != n.aliceChannelLink.Bandwidth() {
|
|
|
|
t.Fatalf("channel bandwidth incorrect: expected %v, got %v",
|
|
|
|
expectedAliceBandwidth, n.aliceChannelLink.Bandwidth())
|
2017-05-02 02:29:30 +03:00
|
|
|
}
|
|
|
|
|
2017-06-17 01:08:19 +03:00
|
|
|
expectedBobBandwidth1 := firstBobBandwidthBefore + htlcAmt
|
|
|
|
if expectedBobBandwidth1 != n.firstBobChannelLink.Bandwidth() {
|
|
|
|
t.Fatalf("channel bandwidth incorrect: expected %v, got %v",
|
|
|
|
expectedBobBandwidth1, n.firstBobChannelLink.Bandwidth())
|
2017-05-02 02:29:30 +03:00
|
|
|
}
|
|
|
|
|
2017-06-17 01:08:19 +03:00
|
|
|
expectedBobBandwidth2 := secondBobBandwidthBefore - amount
|
|
|
|
if expectedBobBandwidth2 != n.secondBobChannelLink.Bandwidth() {
|
|
|
|
t.Fatalf("channel bandwidth incorrect: expected %v, got %v",
|
|
|
|
expectedBobBandwidth2, n.secondBobChannelLink.Bandwidth())
|
2017-05-02 02:29:30 +03:00
|
|
|
}
|
|
|
|
|
2017-06-17 01:08:19 +03:00
|
|
|
expectedCarolBandwidth := carolBandwidthBefore + amount
|
|
|
|
if expectedCarolBandwidth != n.carolChannelLink.Bandwidth() {
|
|
|
|
t.Fatalf("channel bandwidth incorrect: expected %v, got %v",
|
|
|
|
expectedCarolBandwidth, n.carolChannelLink.Bandwidth())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-17 01:09:47 +03:00
|
|
|
// TestExitNodeTimelockPayloadMismatch tests that when an exit node receives an
|
|
|
|
// incoming HTLC, if the time lock encoded in the payload of the forwarded HTLC
|
|
|
|
// doesn't match the expected payment value, then the HTLC will be rejected
|
|
|
|
// with the appropriate error.
|
|
|
|
func TestExitNodeTimelockPayloadMismatch(t *testing.T) {
|
2017-06-17 01:59:20 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2017-07-09 02:30:20 +03:00
|
|
|
channels, cleanUp, _, err := createClusterChannels(
|
2017-06-17 01:09:47 +03:00
|
|
|
btcutil.SatoshiPerBitcoin*5,
|
2017-07-09 02:30:20 +03:00
|
|
|
btcutil.SatoshiPerBitcoin*5)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create channel: %v", err)
|
|
|
|
}
|
|
|
|
defer cleanUp()
|
|
|
|
|
|
|
|
n := newThreeHopNetwork(t, channels.aliceToBob, channels.bobToAlice,
|
2017-11-11 02:09:19 +03:00
|
|
|
channels.bobToCarol, channels.carolToBob, testStartingHeight)
|
2017-06-17 01:09:47 +03:00
|
|
|
if err := n.start(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer n.stop()
|
|
|
|
|
|
|
|
const amount = btcutil.SatoshiPerBitcoin
|
|
|
|
htlcAmt, htlcExpiry, hops := generateHops(amount,
|
2017-08-03 07:11:31 +03:00
|
|
|
testStartingHeight, n.firstBobChannelLink)
|
2017-06-17 01:09:47 +03:00
|
|
|
|
|
|
|
// In order to exercise this case, we'll now _manually_ modify the
|
|
|
|
// per-hop payload for outgoing time lock to be the incorrect value.
|
|
|
|
// The proper value of the outgoing CLTV should be the policy set by
|
|
|
|
// the receiving node, instead we set it to be a random value.
|
|
|
|
hops[0].OutgoingCTLV = 500
|
|
|
|
|
2017-07-09 02:30:20 +03:00
|
|
|
_, err = n.makePayment(n.aliceServer, n.bobServer,
|
2017-07-09 02:04:49 +03:00
|
|
|
n.bobServer.PubKey(), hops, amount, htlcAmt,
|
2017-11-12 02:06:53 +03:00
|
|
|
htlcExpiry).Wait(30 * time.Second)
|
2017-06-17 01:09:47 +03:00
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("payment should have failed but didn't")
|
2017-10-03 08:10:25 +03:00
|
|
|
}
|
|
|
|
|
2017-10-11 05:37:28 +03:00
|
|
|
ferr, ok := err.(*ForwardingError)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("expected a ForwardingError, instead got: %T", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ferr.FailureMessage.(type) {
|
2017-10-03 08:10:25 +03:00
|
|
|
case *lnwire.FailFinalIncorrectCltvExpiry:
|
|
|
|
default:
|
2017-06-29 16:40:45 +03:00
|
|
|
t.Fatalf("incorrect error, expected incorrect cltv expiry, "+
|
2017-06-17 01:09:47 +03:00
|
|
|
"instead have: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestExitNodeAmountPayloadMismatch tests that when an exit node receives an
|
|
|
|
// incoming HTLC, if the amount encoded in the onion payload of the forwarded
|
|
|
|
// HTLC doesn't match the expected payment value, then the HTLC will be
|
|
|
|
// rejected.
|
|
|
|
func TestExitNodeAmountPayloadMismatch(t *testing.T) {
|
2017-06-17 01:59:20 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2017-07-09 02:30:20 +03:00
|
|
|
channels, cleanUp, _, err := createClusterChannels(
|
2017-06-17 01:09:47 +03:00
|
|
|
btcutil.SatoshiPerBitcoin*5,
|
2017-07-09 02:30:20 +03:00
|
|
|
btcutil.SatoshiPerBitcoin*5)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create channel: %v", err)
|
|
|
|
}
|
|
|
|
defer cleanUp()
|
|
|
|
|
|
|
|
n := newThreeHopNetwork(t, channels.aliceToBob, channels.bobToAlice,
|
2017-11-11 02:09:19 +03:00
|
|
|
channels.bobToCarol, channels.carolToBob, testStartingHeight)
|
2017-06-17 01:09:47 +03:00
|
|
|
if err := n.start(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer n.stop()
|
|
|
|
|
|
|
|
const amount = btcutil.SatoshiPerBitcoin
|
2017-08-03 07:11:31 +03:00
|
|
|
htlcAmt, htlcExpiry, hops := generateHops(amount, testStartingHeight,
|
|
|
|
n.firstBobChannelLink)
|
2017-06-17 01:09:47 +03:00
|
|
|
|
|
|
|
// In order to exercise this case, we'll now _manually_ modify the
|
|
|
|
// per-hop payload for amount to be the incorrect value. The proper
|
|
|
|
// value of the amount to forward should be the amount that the
|
|
|
|
// receiving node expects to receive.
|
|
|
|
hops[0].AmountToForward = 1
|
|
|
|
|
2017-07-09 02:30:20 +03:00
|
|
|
_, err = n.makePayment(n.aliceServer, n.bobServer,
|
2017-07-09 02:04:49 +03:00
|
|
|
n.bobServer.PubKey(), hops, amount, htlcAmt,
|
2017-11-12 02:06:53 +03:00
|
|
|
htlcExpiry).Wait(30 * time.Second)
|
2017-06-17 01:09:47 +03:00
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("payment should have failed but didn't")
|
2017-06-29 16:40:45 +03:00
|
|
|
} else if err.Error() != lnwire.CodeIncorrectPaymentAmount.String() {
|
2017-06-17 01:09:47 +03:00
|
|
|
// TODO(roasbeef): use proper error after error propagation is
|
|
|
|
// in
|
|
|
|
t.Fatalf("incorrect error, expected insufficient value, "+
|
|
|
|
"instead have: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-11 02:18:07 +03:00
|
|
|
// TestLinkForwardTimelockPolicyMismatch tests that if a node is an
|
|
|
|
// intermediate node in a multi-hop payment, and receives an HTLC which
|
|
|
|
// violates its specified multi-hop policy, then the HTLC is rejected.
|
2017-06-17 01:09:47 +03:00
|
|
|
func TestLinkForwardTimelockPolicyMismatch(t *testing.T) {
|
2017-06-17 01:59:20 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2017-07-09 02:30:20 +03:00
|
|
|
channels, cleanUp, _, err := createClusterChannels(
|
2017-06-17 01:09:47 +03:00
|
|
|
btcutil.SatoshiPerBitcoin*5,
|
2017-07-09 02:30:20 +03:00
|
|
|
btcutil.SatoshiPerBitcoin*5)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create channel: %v", err)
|
|
|
|
}
|
|
|
|
defer cleanUp()
|
|
|
|
|
|
|
|
n := newThreeHopNetwork(t, channels.aliceToBob, channels.bobToAlice,
|
2017-11-11 02:09:19 +03:00
|
|
|
channels.bobToCarol, channels.carolToBob, testStartingHeight)
|
2017-06-17 01:09:47 +03:00
|
|
|
if err := n.start(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer n.stop()
|
|
|
|
|
|
|
|
// We'll be sending 1 BTC over a 2-hop (3 vertex) route.
|
2017-08-22 09:36:43 +03:00
|
|
|
amount := lnwire.NewMSatFromSatoshis(btcutil.SatoshiPerBitcoin)
|
2017-06-17 01:09:47 +03:00
|
|
|
|
|
|
|
// Generate the route over two hops, ignoring the total time lock that
|
|
|
|
// we'll need to use for the first HTLC in order to have a sufficient
|
|
|
|
// time-lock value to account for the decrements over the entire route.
|
2017-08-03 07:11:31 +03:00
|
|
|
htlcAmt, htlcExpiry, hops := generateHops(amount, testStartingHeight,
|
|
|
|
n.firstBobChannelLink, n.carolChannelLink)
|
2017-10-19 08:21:52 +03:00
|
|
|
htlcExpiry -= 2
|
2017-06-17 01:09:47 +03:00
|
|
|
|
|
|
|
// Next, we'll make the payment which'll send an HTLC with our
|
|
|
|
// specified parameters to the first hop in the route.
|
2017-07-09 02:30:20 +03:00
|
|
|
_, err = n.makePayment(n.aliceServer, n.carolServer,
|
2017-07-09 02:04:49 +03:00
|
|
|
n.bobServer.PubKey(), hops, amount, htlcAmt,
|
2017-11-12 02:06:53 +03:00
|
|
|
htlcExpiry).Wait(30 * time.Second)
|
2017-06-17 01:09:47 +03:00
|
|
|
// We should get an error, and that error should indicate that the HTLC
|
|
|
|
// should be rejected due to a policy violation.
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("payment should have failed but didn't")
|
2017-10-03 08:10:25 +03:00
|
|
|
}
|
|
|
|
|
2017-10-11 05:37:28 +03:00
|
|
|
ferr, ok := err.(*ForwardingError)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("expected a ForwardingError, instead got: %T", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ferr.FailureMessage.(type) {
|
2017-10-03 08:10:25 +03:00
|
|
|
case *lnwire.FailIncorrectCltvExpiry:
|
|
|
|
default:
|
2017-06-29 16:40:45 +03:00
|
|
|
t.Fatalf("incorrect error, expected incorrect cltv expiry, "+
|
2017-06-17 01:09:47 +03:00
|
|
|
"instead have: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestLinkForwardTimelockPolicyMismatch tests that if a node is an
|
|
|
|
// intermediate node in a multi-hop payment and receives an HTLC that violates
|
|
|
|
// its current fee policy, then the HTLC is rejected with the proper error.
|
|
|
|
func TestLinkForwardFeePolicyMismatch(t *testing.T) {
|
2017-06-17 01:59:20 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2017-07-09 02:30:20 +03:00
|
|
|
channels, cleanUp, _, err := createClusterChannels(
|
|
|
|
btcutil.SatoshiPerBitcoin*3,
|
|
|
|
btcutil.SatoshiPerBitcoin*5)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create channel: %v", err)
|
|
|
|
}
|
|
|
|
defer cleanUp()
|
|
|
|
|
|
|
|
n := newThreeHopNetwork(t, channels.aliceToBob, channels.bobToAlice,
|
2017-11-11 02:09:19 +03:00
|
|
|
channels.bobToCarol, channels.carolToBob, testStartingHeight)
|
2017-06-17 01:09:47 +03:00
|
|
|
if err := n.start(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer n.stop()
|
|
|
|
|
|
|
|
// We'll be sending 1 BTC over a 2-hop (3 vertex) route. Given the
|
|
|
|
// current default fee of 1 SAT, if we just send a single BTC over in
|
|
|
|
// an HTLC, it should be rejected.
|
2017-08-22 09:36:43 +03:00
|
|
|
amountNoFee := lnwire.NewMSatFromSatoshis(btcutil.SatoshiPerBitcoin)
|
2017-06-17 01:09:47 +03:00
|
|
|
|
|
|
|
// Generate the route over two hops, ignoring the amount we _should_
|
|
|
|
// actually send in order to be able to cover fees.
|
2017-08-03 07:11:31 +03:00
|
|
|
_, htlcExpiry, hops := generateHops(amountNoFee, testStartingHeight,
|
|
|
|
n.firstBobChannelLink, n.carolChannelLink)
|
2017-06-17 01:09:47 +03:00
|
|
|
|
|
|
|
// Next, we'll make the payment which'll send an HTLC with our
|
|
|
|
// specified parameters to the first hop in the route.
|
2017-07-09 02:30:20 +03:00
|
|
|
_, err = n.makePayment(n.aliceServer, n.bobServer,
|
2017-06-17 01:09:47 +03:00
|
|
|
n.bobServer.PubKey(), hops, amountNoFee, amountNoFee,
|
2017-11-12 02:06:53 +03:00
|
|
|
htlcExpiry).Wait(30 * time.Second)
|
2017-06-17 01:09:47 +03:00
|
|
|
|
|
|
|
// We should get an error, and that error should indicate that the HTLC
|
|
|
|
// should be rejected due to a policy violation.
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("payment should have failed but didn't")
|
2017-10-03 08:10:25 +03:00
|
|
|
}
|
|
|
|
|
2017-10-11 05:37:28 +03:00
|
|
|
ferr, ok := err.(*ForwardingError)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("expected a ForwardingError, instead got: %T", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ferr.FailureMessage.(type) {
|
2017-10-03 08:10:25 +03:00
|
|
|
case *lnwire.FailFeeInsufficient:
|
|
|
|
default:
|
2017-06-29 16:40:45 +03:00
|
|
|
t.Fatalf("incorrect error, expected fee insufficient, "+
|
2017-10-03 08:10:25 +03:00
|
|
|
"instead have: %T", err)
|
2017-06-17 01:09:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestLinkForwardFeePolicyMismatch tests that if a node is an intermediate
|
|
|
|
// node and receives an HTLC which is _below_ its min HTLC policy, then the
|
|
|
|
// HTLC will be rejected.
|
|
|
|
func TestLinkForwardMinHTLCPolicyMismatch(t *testing.T) {
|
2017-06-17 01:59:20 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2017-07-09 02:30:20 +03:00
|
|
|
channels, cleanUp, _, err := createClusterChannels(
|
2017-06-17 01:09:47 +03:00
|
|
|
btcutil.SatoshiPerBitcoin*5,
|
2017-07-09 02:30:20 +03:00
|
|
|
btcutil.SatoshiPerBitcoin*5)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create channel: %v", err)
|
|
|
|
}
|
|
|
|
defer cleanUp()
|
|
|
|
|
|
|
|
n := newThreeHopNetwork(t, channels.aliceToBob, channels.bobToAlice,
|
2017-11-11 02:09:19 +03:00
|
|
|
channels.bobToCarol, channels.carolToBob, testStartingHeight)
|
2017-06-17 01:09:47 +03:00
|
|
|
if err := n.start(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer n.stop()
|
|
|
|
|
|
|
|
// The current default global min HTLC policy set in the default config
|
|
|
|
// for the three-hop-network is 5 SAT. So in order to trigger this
|
|
|
|
// failure mode, we'll create an HTLC with 1 satoshi.
|
2017-08-22 09:36:43 +03:00
|
|
|
amountNoFee := lnwire.NewMSatFromSatoshis(1)
|
2017-06-17 01:09:47 +03:00
|
|
|
|
|
|
|
// With the amount set, we'll generate a route over 2 hops within the
|
|
|
|
// network that attempts to pay out our specified amount.
|
2017-08-03 07:11:31 +03:00
|
|
|
htlcAmt, htlcExpiry, hops := generateHops(amountNoFee, testStartingHeight,
|
2017-06-17 01:09:47 +03:00
|
|
|
n.firstBobChannelLink, n.carolChannelLink)
|
|
|
|
|
|
|
|
// Next, we'll make the payment which'll send an HTLC with our
|
|
|
|
// specified parameters to the first hop in the route.
|
2017-07-09 02:30:20 +03:00
|
|
|
_, err = n.makePayment(n.aliceServer, n.bobServer,
|
2017-06-17 01:09:47 +03:00
|
|
|
n.bobServer.PubKey(), hops, amountNoFee, htlcAmt,
|
2017-11-12 02:06:53 +03:00
|
|
|
htlcExpiry).Wait(30 * time.Second)
|
2017-06-17 01:09:47 +03:00
|
|
|
|
|
|
|
// We should get an error, and that error should indicate that the HTLC
|
|
|
|
// should be rejected due to a policy violation (below min HTLC).
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("payment should have failed but didn't")
|
2017-10-03 08:10:25 +03:00
|
|
|
}
|
|
|
|
|
2017-10-11 05:37:28 +03:00
|
|
|
ferr, ok := err.(*ForwardingError)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("expected a ForwardingError, instead got: %T", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ferr.FailureMessage.(type) {
|
2017-10-03 08:10:25 +03:00
|
|
|
case *lnwire.FailAmountBelowMinimum:
|
|
|
|
default:
|
2017-06-29 16:40:45 +03:00
|
|
|
t.Fatalf("incorrect error, expected amount below minimum, "+
|
2017-06-17 01:09:47 +03:00
|
|
|
"instead have: %v", err)
|
2017-05-02 02:29:30 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-17 01:09:47 +03:00
|
|
|
// TestUpdateForwardingPolicy tests that the forwarding policy for a link is
|
|
|
|
// able to be updated properly. We'll first create an HTLC that meets the
|
|
|
|
// specified policy, assert that it succeeds, update the policy (to invalidate
|
|
|
|
// the prior HTLC), and then ensure that the HTLC is rejected.
|
|
|
|
func TestUpdateForwardingPolicy(t *testing.T) {
|
2017-06-17 01:59:20 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2017-07-09 02:30:20 +03:00
|
|
|
channels, cleanUp, _, err := createClusterChannels(
|
2017-06-17 01:09:47 +03:00
|
|
|
btcutil.SatoshiPerBitcoin*5,
|
2017-07-09 02:30:20 +03:00
|
|
|
btcutil.SatoshiPerBitcoin*5)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create channel: %v", err)
|
|
|
|
}
|
|
|
|
defer cleanUp()
|
|
|
|
|
|
|
|
n := newThreeHopNetwork(t, channels.aliceToBob, channels.bobToAlice,
|
2017-11-11 02:09:19 +03:00
|
|
|
channels.bobToCarol, channels.carolToBob, testStartingHeight)
|
2017-06-17 01:09:47 +03:00
|
|
|
if err := n.start(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer n.stop()
|
|
|
|
|
|
|
|
carolBandwidthBefore := n.carolChannelLink.Bandwidth()
|
|
|
|
firstBobBandwidthBefore := n.firstBobChannelLink.Bandwidth()
|
|
|
|
secondBobBandwidthBefore := n.secondBobChannelLink.Bandwidth()
|
|
|
|
aliceBandwidthBefore := n.aliceChannelLink.Bandwidth()
|
|
|
|
|
2017-08-22 09:36:43 +03:00
|
|
|
amountNoFee := lnwire.NewMSatFromSatoshis(10)
|
2017-06-17 01:09:47 +03:00
|
|
|
htlcAmt, htlcExpiry, hops := generateHops(amountNoFee,
|
2017-08-03 07:11:31 +03:00
|
|
|
testStartingHeight,
|
2017-06-17 01:09:47 +03:00
|
|
|
n.firstBobChannelLink, n.carolChannelLink)
|
|
|
|
|
|
|
|
// First, send this 1 BTC payment over the three hops, the payment
|
2017-10-25 04:31:39 +03:00
|
|
|
// should succeed, and all balances should be updated accordingly.
|
2017-11-11 02:18:36 +03:00
|
|
|
payResp, err := n.makePayment(n.aliceServer, n.carolServer,
|
2017-06-17 01:09:47 +03:00
|
|
|
n.bobServer.PubKey(), hops, amountNoFee, htlcAmt,
|
2017-11-12 02:06:53 +03:00
|
|
|
htlcExpiry).Wait(30 * time.Second)
|
2017-06-17 01:09:47 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send payment: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Carol's invoice should now be shown as settled as the payment
|
|
|
|
// succeeded.
|
2017-11-11 02:18:36 +03:00
|
|
|
invoice, err := n.carolServer.registry.LookupInvoice(payResp)
|
2017-07-09 02:30:20 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get invoice: %v", err)
|
|
|
|
}
|
2017-06-17 01:09:47 +03:00
|
|
|
if !invoice.Terms.Settled {
|
2017-07-09 02:30:20 +03:00
|
|
|
t.Fatal("carol invoice haven't been settled")
|
2017-06-17 01:09:47 +03:00
|
|
|
}
|
2017-07-09 02:30:20 +03:00
|
|
|
|
2017-06-17 01:09:47 +03:00
|
|
|
expectedAliceBandwidth := aliceBandwidthBefore - htlcAmt
|
|
|
|
if expectedAliceBandwidth != n.aliceChannelLink.Bandwidth() {
|
|
|
|
t.Fatalf("channel bandwidth incorrect: expected %v, got %v",
|
|
|
|
expectedAliceBandwidth, n.aliceChannelLink.Bandwidth())
|
|
|
|
}
|
|
|
|
expectedBobBandwidth1 := firstBobBandwidthBefore + htlcAmt
|
|
|
|
if expectedBobBandwidth1 != n.firstBobChannelLink.Bandwidth() {
|
|
|
|
t.Fatalf("channel bandwidth incorrect: expected %v, got %v",
|
|
|
|
expectedBobBandwidth1, n.firstBobChannelLink.Bandwidth())
|
|
|
|
}
|
|
|
|
expectedBobBandwidth2 := secondBobBandwidthBefore - amountNoFee
|
|
|
|
if expectedBobBandwidth2 != n.secondBobChannelLink.Bandwidth() {
|
|
|
|
t.Fatalf("channel bandwidth incorrect: expected %v, got %v",
|
|
|
|
expectedBobBandwidth2, n.secondBobChannelLink.Bandwidth())
|
|
|
|
}
|
|
|
|
expectedCarolBandwidth := carolBandwidthBefore + amountNoFee
|
|
|
|
if expectedCarolBandwidth != n.carolChannelLink.Bandwidth() {
|
|
|
|
t.Fatalf("channel bandwidth incorrect: expected %v, got %v",
|
|
|
|
expectedCarolBandwidth, n.carolChannelLink.Bandwidth())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now we'll update Bob's policy to jack up his free rate to an extent
|
|
|
|
// that'll cause him to reject the same HTLC that we just sent.
|
|
|
|
//
|
|
|
|
// TODO(roasbeef): should implement grace period within link policy
|
|
|
|
// update logic
|
|
|
|
newPolicy := n.globalPolicy
|
2017-08-22 09:36:43 +03:00
|
|
|
newPolicy.BaseFee = lnwire.NewMSatFromSatoshis(1000)
|
2017-06-17 01:09:47 +03:00
|
|
|
n.firstBobChannelLink.UpdateForwardingPolicy(newPolicy)
|
2017-08-22 09:36:43 +03:00
|
|
|
|
2017-10-25 04:31:39 +03:00
|
|
|
// Next, we'll send the payment again, using the exact same per-hop
|
|
|
|
// payload for each node. This payment should fail as it wont' factor
|
|
|
|
// in Bob's new fee policy.
|
|
|
|
_, err = n.makePayment(n.aliceServer, n.carolServer,
|
|
|
|
n.bobServer.PubKey(), hops, amountNoFee, htlcAmt,
|
2017-11-12 02:06:53 +03:00
|
|
|
htlcExpiry).Wait(30 * time.Second)
|
2017-10-25 04:31:39 +03:00
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("payment should've been rejected")
|
|
|
|
}
|
|
|
|
|
|
|
|
ferr, ok := err.(*ForwardingError)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("expected a ForwardingError, instead got: %T", err)
|
|
|
|
}
|
|
|
|
switch ferr.FailureMessage.(type) {
|
|
|
|
case *lnwire.FailFeeInsufficient:
|
|
|
|
default:
|
|
|
|
t.Fatalf("expected FailFeeInsufficient instead got: %v", err)
|
|
|
|
}
|
2017-06-17 01:09:47 +03:00
|
|
|
}
|
|
|
|
|
2017-05-02 02:29:30 +03:00
|
|
|
// TestChannelLinkMultiHopInsufficientPayment checks that we receive error if
|
|
|
|
// bob<->alice channel has insufficient BTC capacity/bandwidth. In this test we
|
|
|
|
// send the payment from Carol to Alice over Bob peer. (Carol -> Bob -> Alice)
|
|
|
|
func TestChannelLinkMultiHopInsufficientPayment(t *testing.T) {
|
2017-06-17 01:59:20 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2017-07-09 02:30:20 +03:00
|
|
|
channels, cleanUp, _, err := createClusterChannels(
|
2017-11-11 02:18:36 +03:00
|
|
|
btcutil.SatoshiPerBitcoin*3,
|
|
|
|
btcutil.SatoshiPerBitcoin*5)
|
2017-07-09 02:30:20 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create channel: %v", err)
|
|
|
|
}
|
|
|
|
defer cleanUp()
|
|
|
|
|
|
|
|
n := newThreeHopNetwork(t, channels.aliceToBob, channels.bobToAlice,
|
2017-11-11 02:09:19 +03:00
|
|
|
channels.bobToCarol, channels.carolToBob, testStartingHeight)
|
2017-05-02 02:29:30 +03:00
|
|
|
if err := n.start(); err != nil {
|
2017-06-29 16:40:45 +03:00
|
|
|
t.Fatalf("unable to start three hop network: %v", err)
|
2017-05-02 02:29:30 +03:00
|
|
|
}
|
|
|
|
defer n.stop()
|
|
|
|
|
|
|
|
carolBandwidthBefore := n.carolChannelLink.Bandwidth()
|
|
|
|
firstBobBandwidthBefore := n.firstBobChannelLink.Bandwidth()
|
|
|
|
secondBobBandwidthBefore := n.secondBobChannelLink.Bandwidth()
|
|
|
|
aliceBandwidthBefore := n.aliceChannelLink.Bandwidth()
|
|
|
|
|
2017-11-11 02:18:36 +03:00
|
|
|
// We'll attempt to send 4 BTC although the alice-to-bob channel only
|
|
|
|
// has 3 BTC total capacity. As a result, this payment should be
|
|
|
|
// rejected.
|
2017-08-22 09:36:43 +03:00
|
|
|
amount := lnwire.NewMSatFromSatoshis(4 * btcutil.SatoshiPerBitcoin)
|
2017-08-03 07:11:31 +03:00
|
|
|
htlcAmt, totalTimelock, hops := generateHops(amount, testStartingHeight,
|
2017-06-17 01:08:19 +03:00
|
|
|
n.firstBobChannelLink, n.carolChannelLink)
|
|
|
|
|
2017-05-02 02:29:30 +03:00
|
|
|
// Wait for:
|
|
|
|
// * HTLC add request to be sent to from Alice to Bob.
|
|
|
|
// * Alice<->Bob commitment states to be updated.
|
|
|
|
// * Bob trying to add HTLC add request in Bob<->Carol channel.
|
|
|
|
// * Cancel HTLC request to be sent back from Bob to Alice.
|
|
|
|
// * user notification to be sent.
|
2017-07-09 02:30:20 +03:00
|
|
|
|
|
|
|
receiver := n.carolServer
|
|
|
|
rhash, err := n.makePayment(n.aliceServer, n.carolServer,
|
2017-07-09 02:04:49 +03:00
|
|
|
n.bobServer.PubKey(), hops, amount, htlcAmt,
|
2017-11-12 02:06:53 +03:00
|
|
|
totalTimelock).Wait(30 * time.Second)
|
2017-05-02 02:29:30 +03:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("error haven't been received")
|
2017-10-03 08:24:49 +03:00
|
|
|
} else if !strings.Contains(err.Error(), "insufficient capacity") {
|
|
|
|
t.Fatalf("wrong error has been received: %v", err)
|
2017-05-02 02:29:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for Alice to receive the revocation.
|
2017-06-17 01:09:47 +03:00
|
|
|
//
|
|
|
|
// TODO(roasbeef): add in ntfn hook for state transition completion
|
2017-05-02 02:29:30 +03:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
|
|
|
|
// Check that alice invoice wasn't settled and bandwidth of htlc
|
|
|
|
// links hasn't been changed.
|
2017-07-09 02:30:20 +03:00
|
|
|
invoice, err := receiver.registry.LookupInvoice(rhash)
|
|
|
|
if err != nil {
|
2018-01-04 23:23:31 +03:00
|
|
|
t.Fatalf("unable to get invoice: %v", err)
|
2017-07-09 02:30:20 +03:00
|
|
|
}
|
2017-05-02 02:29:30 +03:00
|
|
|
if invoice.Terms.Settled {
|
2017-07-09 02:30:20 +03:00
|
|
|
t.Fatal("carol invoice have been settled")
|
2017-05-02 02:29:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if n.aliceChannelLink.Bandwidth() != aliceBandwidthBefore {
|
|
|
|
t.Fatal("the bandwidth of alice channel link which handles " +
|
|
|
|
"alice->bob channel should be the same")
|
|
|
|
}
|
|
|
|
|
|
|
|
if n.firstBobChannelLink.Bandwidth() != firstBobBandwidthBefore {
|
|
|
|
t.Fatal("the bandwidth of bob channel link which handles " +
|
|
|
|
"alice->bob channel should be the same")
|
|
|
|
}
|
|
|
|
|
|
|
|
if n.secondBobChannelLink.Bandwidth() != secondBobBandwidthBefore {
|
|
|
|
t.Fatal("the bandwidth of bob channel link which handles " +
|
|
|
|
"bob->carol channel should be the same")
|
|
|
|
}
|
|
|
|
|
|
|
|
if n.carolChannelLink.Bandwidth() != carolBandwidthBefore {
|
|
|
|
t.Fatal("the bandwidth of carol channel link which handles " +
|
|
|
|
"bob->carol channel should be the same")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestChannelLinkMultiHopUnknownPaymentHash checks that we receive remote error
|
|
|
|
// from Alice if she received not suitable payment hash for htlc.
|
|
|
|
func TestChannelLinkMultiHopUnknownPaymentHash(t *testing.T) {
|
2017-06-17 01:59:20 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2017-07-09 02:30:20 +03:00
|
|
|
channels, cleanUp, _, err := createClusterChannels(
|
2017-05-02 02:29:30 +03:00
|
|
|
btcutil.SatoshiPerBitcoin*5,
|
2017-07-09 02:30:20 +03:00
|
|
|
btcutil.SatoshiPerBitcoin*5)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create channel: %v", err)
|
|
|
|
}
|
|
|
|
defer cleanUp()
|
|
|
|
|
|
|
|
n := newThreeHopNetwork(t, channels.aliceToBob, channels.bobToAlice,
|
2017-11-11 02:09:19 +03:00
|
|
|
channels.bobToCarol, channels.carolToBob, testStartingHeight)
|
2017-05-02 02:29:30 +03:00
|
|
|
if err := n.start(); err != nil {
|
2017-06-29 16:40:45 +03:00
|
|
|
t.Fatalf("unable to start three hop network: %v", err)
|
2017-05-02 02:29:30 +03:00
|
|
|
}
|
|
|
|
defer n.stop()
|
|
|
|
|
|
|
|
carolBandwidthBefore := n.carolChannelLink.Bandwidth()
|
|
|
|
firstBobBandwidthBefore := n.firstBobChannelLink.Bandwidth()
|
|
|
|
secondBobBandwidthBefore := n.secondBobChannelLink.Bandwidth()
|
|
|
|
aliceBandwidthBefore := n.aliceChannelLink.Bandwidth()
|
|
|
|
|
2017-08-22 09:36:43 +03:00
|
|
|
amount := lnwire.NewMSatFromSatoshis(btcutil.SatoshiPerBitcoin)
|
2017-05-02 02:29:30 +03:00
|
|
|
|
2017-08-03 07:11:31 +03:00
|
|
|
htlcAmt, totalTimelock, hops := generateHops(amount, testStartingHeight,
|
2017-06-17 01:08:19 +03:00
|
|
|
n.firstBobChannelLink, n.carolChannelLink)
|
|
|
|
blob, err := generateRoute(hops...)
|
2017-05-02 02:29:30 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate payment: invoice and htlc.
|
2017-06-17 01:08:19 +03:00
|
|
|
invoice, htlc, err := generatePayment(amount, htlcAmt, totalTimelock,
|
|
|
|
blob)
|
2017-05-02 02:29:30 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We need to have wrong rhash for that reason we should change the
|
|
|
|
// preimage. Inverse first byte by xoring with 0xff.
|
|
|
|
invoice.Terms.PaymentPreimage[0] ^= byte(255)
|
|
|
|
|
|
|
|
// Check who is last in the route and add invoice to server registry.
|
2017-11-12 03:09:14 +03:00
|
|
|
if err := n.carolServer.registry.AddInvoice(*invoice); err != nil {
|
2017-06-29 16:40:45 +03:00
|
|
|
t.Fatalf("unable to add invoice in carol registry: %v", err)
|
2017-05-02 02:29:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Send payment and expose err channel.
|
2017-06-29 16:40:45 +03:00
|
|
|
_, err = n.aliceServer.htlcSwitch.SendHTLC(n.bobServer.PubKey(), htlc,
|
|
|
|
newMockDeobfuscator())
|
|
|
|
if err.Error() != lnwire.CodeUnknownPaymentHash.String() {
|
|
|
|
t.Fatal("error haven't been received")
|
2017-05-02 02:29:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for Alice to receive the revocation.
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
|
|
|
|
// Check that alice invoice wasn't settled and bandwidth of htlc
|
|
|
|
// links hasn't been changed.
|
|
|
|
if invoice.Terms.Settled {
|
|
|
|
t.Fatal("alice invoice was settled")
|
|
|
|
}
|
|
|
|
|
|
|
|
if n.aliceChannelLink.Bandwidth() != aliceBandwidthBefore {
|
|
|
|
t.Fatal("the bandwidth of alice channel link which handles " +
|
|
|
|
"alice->bob channel should be the same")
|
|
|
|
}
|
|
|
|
|
|
|
|
if n.firstBobChannelLink.Bandwidth() != firstBobBandwidthBefore {
|
|
|
|
t.Fatal("the bandwidth of bob channel link which handles " +
|
|
|
|
"alice->bob channel should be the same")
|
|
|
|
}
|
|
|
|
|
|
|
|
if n.secondBobChannelLink.Bandwidth() != secondBobBandwidthBefore {
|
|
|
|
t.Fatal("the bandwidth of bob channel link which handles " +
|
|
|
|
"bob->carol channel should be the same")
|
|
|
|
}
|
|
|
|
|
|
|
|
if n.carolChannelLink.Bandwidth() != carolBandwidthBefore {
|
|
|
|
t.Fatal("the bandwidth of carol channel link which handles " +
|
|
|
|
"bob->carol channel should be the same")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestChannelLinkMultiHopUnknownNextHop construct the chain of hops
|
|
|
|
// Carol<->Bob<->Alice and checks that we receive remote error from Bob if he
|
|
|
|
// has no idea about next hop (hop might goes down and routing info not updated
|
2017-06-17 01:08:19 +03:00
|
|
|
// yet).
|
2017-05-02 02:29:30 +03:00
|
|
|
func TestChannelLinkMultiHopUnknownNextHop(t *testing.T) {
|
2017-06-17 01:59:20 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2017-07-09 02:30:20 +03:00
|
|
|
channels, cleanUp, _, err := createClusterChannels(
|
2017-05-02 02:29:30 +03:00
|
|
|
btcutil.SatoshiPerBitcoin*5,
|
2017-07-09 02:30:20 +03:00
|
|
|
btcutil.SatoshiPerBitcoin*5)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create channel: %v", err)
|
|
|
|
}
|
|
|
|
defer cleanUp()
|
|
|
|
|
|
|
|
n := newThreeHopNetwork(t, channels.aliceToBob, channels.bobToAlice,
|
2017-11-11 02:09:19 +03:00
|
|
|
channels.bobToCarol, channels.carolToBob, testStartingHeight)
|
2017-05-02 02:29:30 +03:00
|
|
|
if err := n.start(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer n.stop()
|
|
|
|
|
|
|
|
carolBandwidthBefore := n.carolChannelLink.Bandwidth()
|
|
|
|
firstBobBandwidthBefore := n.firstBobChannelLink.Bandwidth()
|
|
|
|
secondBobBandwidthBefore := n.secondBobChannelLink.Bandwidth()
|
|
|
|
aliceBandwidthBefore := n.aliceChannelLink.Bandwidth()
|
|
|
|
|
2017-08-22 09:36:43 +03:00
|
|
|
amount := lnwire.NewMSatFromSatoshis(btcutil.SatoshiPerBitcoin)
|
2017-08-03 07:11:31 +03:00
|
|
|
htlcAmt, totalTimelock, hops := generateHops(amount, testStartingHeight,
|
2017-06-17 01:08:19 +03:00
|
|
|
n.firstBobChannelLink, n.carolChannelLink)
|
2017-05-02 02:29:30 +03:00
|
|
|
|
2017-11-11 02:09:19 +03:00
|
|
|
davePub := newMockServer(t, "dave").PubKey()
|
2017-07-09 02:30:20 +03:00
|
|
|
receiver := n.bobServer
|
|
|
|
rhash, err := n.makePayment(n.aliceServer, n.bobServer, davePub, hops,
|
2017-11-12 02:06:53 +03:00
|
|
|
amount, htlcAmt, totalTimelock).Wait(30 * time.Second)
|
2017-05-02 02:29:30 +03:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("error haven't been received")
|
2017-06-29 16:40:45 +03:00
|
|
|
} else if err.Error() != lnwire.CodeUnknownNextPeer.String() {
|
2017-05-02 02:29:30 +03:00
|
|
|
t.Fatalf("wrong error have been received: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for Alice to receive the revocation.
|
2017-06-17 01:08:19 +03:00
|
|
|
//
|
|
|
|
// TODO(roasbeef): add in ntfn hook for state transition completion
|
2017-05-02 02:29:30 +03:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
|
|
|
|
// Check that alice invoice wasn't settled and bandwidth of htlc
|
|
|
|
// links hasn't been changed.
|
2017-07-09 02:30:20 +03:00
|
|
|
invoice, err := receiver.registry.LookupInvoice(rhash)
|
|
|
|
if err != nil {
|
2018-01-04 23:23:31 +03:00
|
|
|
t.Fatalf("unable to get invoice: %v", err)
|
2017-07-09 02:30:20 +03:00
|
|
|
}
|
2017-05-02 02:29:30 +03:00
|
|
|
if invoice.Terms.Settled {
|
2017-07-09 02:30:20 +03:00
|
|
|
t.Fatal("carol invoice have been settled")
|
2017-05-02 02:29:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if n.aliceChannelLink.Bandwidth() != aliceBandwidthBefore {
|
|
|
|
t.Fatal("the bandwidth of alice channel link which handles " +
|
|
|
|
"alice->bob channel should be the same")
|
|
|
|
}
|
|
|
|
|
|
|
|
if n.firstBobChannelLink.Bandwidth() != firstBobBandwidthBefore {
|
|
|
|
t.Fatal("the bandwidth of bob channel link which handles " +
|
|
|
|
"alice->bob channel should be the same")
|
|
|
|
}
|
|
|
|
|
|
|
|
if n.secondBobChannelLink.Bandwidth() != secondBobBandwidthBefore {
|
|
|
|
t.Fatal("the bandwidth of bob channel link which handles " +
|
|
|
|
"bob->carol channel should be the same")
|
|
|
|
}
|
|
|
|
|
|
|
|
if n.carolChannelLink.Bandwidth() != carolBandwidthBefore {
|
|
|
|
t.Fatal("the bandwidth of carol channel link which handles " +
|
|
|
|
"bob->carol channel should be the same")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestChannelLinkMultiHopDecodeError checks that we send HTLC cancel if
|
|
|
|
// decoding of onion blob failed.
|
|
|
|
func TestChannelLinkMultiHopDecodeError(t *testing.T) {
|
2017-06-17 01:59:20 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2017-07-09 02:30:20 +03:00
|
|
|
channels, cleanUp, _, err := createClusterChannels(
|
2017-05-02 02:29:30 +03:00
|
|
|
btcutil.SatoshiPerBitcoin*3,
|
2017-07-09 02:30:20 +03:00
|
|
|
btcutil.SatoshiPerBitcoin*5)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create channel: %v", err)
|
|
|
|
}
|
|
|
|
defer cleanUp()
|
|
|
|
|
|
|
|
n := newThreeHopNetwork(t, channels.aliceToBob, channels.bobToAlice,
|
2017-11-11 02:09:19 +03:00
|
|
|
channels.bobToCarol, channels.carolToBob, testStartingHeight)
|
2017-05-02 02:29:30 +03:00
|
|
|
if err := n.start(); err != nil {
|
2017-06-29 16:40:45 +03:00
|
|
|
t.Fatalf("unable to start three hop network: %v", err)
|
2017-05-02 02:29:30 +03:00
|
|
|
}
|
|
|
|
defer n.stop()
|
|
|
|
|
|
|
|
// Replace decode function with another which throws an error.
|
2017-06-29 16:40:45 +03:00
|
|
|
n.carolChannelLink.cfg.DecodeOnionObfuscator = func(
|
2017-10-11 05:37:28 +03:00
|
|
|
r io.Reader) (ErrorEncrypter, lnwire.FailCode) {
|
2017-06-29 16:40:45 +03:00
|
|
|
return nil, lnwire.CodeInvalidOnionVersion
|
2017-05-02 02:29:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
carolBandwidthBefore := n.carolChannelLink.Bandwidth()
|
|
|
|
firstBobBandwidthBefore := n.firstBobChannelLink.Bandwidth()
|
|
|
|
secondBobBandwidthBefore := n.secondBobChannelLink.Bandwidth()
|
|
|
|
aliceBandwidthBefore := n.aliceChannelLink.Bandwidth()
|
|
|
|
|
2017-08-22 09:36:43 +03:00
|
|
|
amount := lnwire.NewMSatFromSatoshis(btcutil.SatoshiPerBitcoin)
|
2017-08-03 07:11:31 +03:00
|
|
|
htlcAmt, totalTimelock, hops := generateHops(amount, testStartingHeight,
|
2017-06-17 01:08:19 +03:00
|
|
|
n.firstBobChannelLink, n.carolChannelLink)
|
|
|
|
|
2017-07-09 02:30:20 +03:00
|
|
|
receiver := n.carolServer
|
|
|
|
rhash, err := n.makePayment(n.aliceServer, n.carolServer,
|
2017-07-09 02:04:49 +03:00
|
|
|
n.bobServer.PubKey(), hops, amount, htlcAmt,
|
2017-11-12 02:06:53 +03:00
|
|
|
totalTimelock).Wait(30 * time.Second)
|
2017-05-02 02:29:30 +03:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("error haven't been received")
|
2017-10-03 08:10:25 +03:00
|
|
|
}
|
2017-10-11 05:37:28 +03:00
|
|
|
|
|
|
|
ferr, ok := err.(*ForwardingError)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("expected a ForwardingError, instead got: %T", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ferr.FailureMessage.(type) {
|
2017-10-03 08:10:25 +03:00
|
|
|
case *lnwire.FailInvalidOnionVersion:
|
|
|
|
default:
|
2017-05-02 02:29:30 +03:00
|
|
|
t.Fatalf("wrong error have been received: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for Bob to receive the revocation.
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
|
|
|
|
// Check that alice invoice wasn't settled and bandwidth of htlc
|
|
|
|
// links hasn't been changed.
|
2017-07-09 02:30:20 +03:00
|
|
|
invoice, err := receiver.registry.LookupInvoice(rhash)
|
|
|
|
if err != nil {
|
2018-01-04 23:23:31 +03:00
|
|
|
t.Fatalf("unable to get invoice: %v", err)
|
2017-07-09 02:30:20 +03:00
|
|
|
}
|
2017-05-02 02:29:30 +03:00
|
|
|
if invoice.Terms.Settled {
|
2017-07-09 02:30:20 +03:00
|
|
|
t.Fatal("carol invoice have been settled")
|
2017-05-02 02:29:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if n.aliceChannelLink.Bandwidth() != aliceBandwidthBefore {
|
|
|
|
t.Fatal("the bandwidth of alice channel link which handles " +
|
|
|
|
"alice->bob channel should be the same")
|
|
|
|
}
|
|
|
|
|
|
|
|
if n.firstBobChannelLink.Bandwidth() != firstBobBandwidthBefore {
|
|
|
|
t.Fatal("the bandwidth of bob channel link which handles " +
|
|
|
|
"alice->bob channel should be the same")
|
|
|
|
}
|
|
|
|
|
|
|
|
if n.secondBobChannelLink.Bandwidth() != secondBobBandwidthBefore {
|
|
|
|
t.Fatal("the bandwidth of bob channel link which handles " +
|
|
|
|
"bob->carol channel should be the same")
|
|
|
|
}
|
|
|
|
|
|
|
|
if n.carolChannelLink.Bandwidth() != carolBandwidthBefore {
|
|
|
|
t.Fatal("the bandwidth of carol channel link which handles " +
|
|
|
|
"bob->carol channel should be the same")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-03 07:13:09 +03:00
|
|
|
// TestChannelLinkExpiryTooSoonExitNode tests that if we send an HTLC to a node
|
|
|
|
// with an expiry that is already expired, or too close to the current block
|
|
|
|
// height, then it will cancel the HTLC.
|
|
|
|
func TestChannelLinkExpiryTooSoonExitNode(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
// The starting height for this test will be 200. So we'll base all
|
|
|
|
// HTLC starting points off of that.
|
2017-07-09 02:30:20 +03:00
|
|
|
channels, cleanUp, _, err := createClusterChannels(
|
2017-08-03 07:13:09 +03:00
|
|
|
btcutil.SatoshiPerBitcoin*3,
|
2017-07-09 02:30:20 +03:00
|
|
|
btcutil.SatoshiPerBitcoin*5)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create channel: %v", err)
|
|
|
|
}
|
|
|
|
defer cleanUp()
|
|
|
|
|
|
|
|
const startingHeight = 200
|
|
|
|
n := newThreeHopNetwork(t, channels.aliceToBob, channels.bobToAlice,
|
2017-11-11 02:09:19 +03:00
|
|
|
channels.bobToCarol, channels.carolToBob, startingHeight)
|
2017-08-03 07:13:09 +03:00
|
|
|
if err := n.start(); err != nil {
|
|
|
|
t.Fatalf("unable to start three hop network: %v", err)
|
|
|
|
}
|
|
|
|
defer n.stop()
|
|
|
|
|
2017-08-22 09:36:43 +03:00
|
|
|
amount := lnwire.NewMSatFromSatoshis(btcutil.SatoshiPerBitcoin)
|
2017-08-03 07:13:09 +03:00
|
|
|
|
|
|
|
// We'll craft an HTLC packet, but set the starting height to 10 blocks
|
|
|
|
// before the current true height.
|
|
|
|
htlcAmt, totalTimelock, hops := generateHops(amount,
|
|
|
|
startingHeight-10, n.firstBobChannelLink)
|
|
|
|
|
|
|
|
// Now we'll send out the payment from Alice to Bob.
|
2017-07-09 02:30:20 +03:00
|
|
|
_, err = n.makePayment(n.aliceServer, n.bobServer,
|
|
|
|
n.bobServer.PubKey(), hops, amount, htlcAmt,
|
2017-11-12 02:06:53 +03:00
|
|
|
totalTimelock).Wait(30 * time.Second)
|
2017-08-03 07:13:09 +03:00
|
|
|
|
|
|
|
// The payment should've failed as the time lock value was in the
|
|
|
|
// _past_.
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("payment should have failed due to a too early " +
|
|
|
|
"time lock value")
|
2017-10-03 08:10:25 +03:00
|
|
|
}
|
|
|
|
|
2017-10-11 05:37:28 +03:00
|
|
|
ferr, ok := err.(*ForwardingError)
|
|
|
|
if !ok {
|
2017-11-12 02:06:53 +03:00
|
|
|
t.Fatalf("expected a ForwardingError, instead got: %T %v",
|
|
|
|
err, err)
|
2017-10-11 05:37:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
switch ferr.FailureMessage.(type) {
|
2017-10-03 08:10:25 +03:00
|
|
|
case *lnwire.FailFinalIncorrectCltvExpiry:
|
|
|
|
default:
|
2017-08-03 07:13:09 +03:00
|
|
|
t.Fatalf("incorrect error, expected final time lock too "+
|
|
|
|
"early, instead have: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestChannelLinkExpiryTooSoonExitNode tests that if we send a multi-hop HTLC,
|
|
|
|
// and the time lock is too early for an intermediate node, then they cancel
|
|
|
|
// the HTLC back to the sender.
|
|
|
|
func TestChannelLinkExpiryTooSoonMidNode(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
// The starting height for this test will be 200. So we'll base all
|
|
|
|
// HTLC starting points off of that.
|
2017-07-09 02:30:20 +03:00
|
|
|
channels, cleanUp, _, err := createClusterChannels(
|
2017-08-03 07:13:09 +03:00
|
|
|
btcutil.SatoshiPerBitcoin*3,
|
2017-07-09 02:30:20 +03:00
|
|
|
btcutil.SatoshiPerBitcoin*5)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create channel: %v", err)
|
|
|
|
}
|
|
|
|
defer cleanUp()
|
|
|
|
|
|
|
|
const startingHeight = 200
|
|
|
|
n := newThreeHopNetwork(t, channels.aliceToBob, channels.bobToAlice,
|
2017-11-11 02:09:19 +03:00
|
|
|
channels.bobToCarol, channels.carolToBob, startingHeight)
|
2017-08-03 07:13:09 +03:00
|
|
|
if err := n.start(); err != nil {
|
|
|
|
t.Fatalf("unable to start three hop network: %v", err)
|
|
|
|
}
|
|
|
|
defer n.stop()
|
|
|
|
|
2017-08-22 09:36:43 +03:00
|
|
|
amount := lnwire.NewMSatFromSatoshis(btcutil.SatoshiPerBitcoin)
|
2017-08-03 07:13:09 +03:00
|
|
|
|
|
|
|
// We'll craft an HTLC packet, but set the starting height to 10 blocks
|
|
|
|
// before the current true height. The final route will be three hops,
|
|
|
|
// so the middle hop should detect the issue.
|
|
|
|
htlcAmt, totalTimelock, hops := generateHops(amount,
|
|
|
|
startingHeight-10, n.firstBobChannelLink, n.carolChannelLink)
|
|
|
|
|
|
|
|
// Now we'll send out the payment from Alice to Bob.
|
2017-07-09 02:30:20 +03:00
|
|
|
_, err = n.makePayment(n.aliceServer, n.bobServer,
|
|
|
|
n.bobServer.PubKey(), hops, amount, htlcAmt,
|
2017-11-12 02:06:53 +03:00
|
|
|
totalTimelock).Wait(30 * time.Second)
|
2017-08-03 07:13:09 +03:00
|
|
|
|
|
|
|
// The payment should've failed as the time lock value was in the
|
|
|
|
// _past_.
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("payment should have failed due to a too early " +
|
|
|
|
"time lock value")
|
2017-10-03 08:10:25 +03:00
|
|
|
}
|
|
|
|
|
2017-10-11 05:37:28 +03:00
|
|
|
ferr, ok := err.(*ForwardingError)
|
|
|
|
if !ok {
|
2017-11-12 02:06:53 +03:00
|
|
|
t.Fatalf("expected a ForwardingError, instead got: %T: %v", err, err)
|
2017-10-11 05:37:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
switch ferr.FailureMessage.(type) {
|
2017-10-03 08:10:25 +03:00
|
|
|
case *lnwire.FailExpiryTooSoon:
|
|
|
|
default:
|
2017-08-03 07:13:09 +03:00
|
|
|
t.Fatalf("incorrect error, expected final time lock too "+
|
|
|
|
"early, instead have: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-02 02:29:30 +03:00
|
|
|
// TestChannelLinkSingleHopMessageOrdering test checks ordering of message which
|
|
|
|
// flying around between Alice and Bob are correct when Bob sends payments to
|
|
|
|
// Alice.
|
|
|
|
func TestChannelLinkSingleHopMessageOrdering(t *testing.T) {
|
2017-06-17 01:59:20 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2017-07-09 02:30:20 +03:00
|
|
|
channels, cleanUp, _, err := createClusterChannels(
|
2017-05-02 02:29:30 +03:00
|
|
|
btcutil.SatoshiPerBitcoin*3,
|
2017-07-09 02:30:20 +03:00
|
|
|
btcutil.SatoshiPerBitcoin*5)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create channel: %v", err)
|
|
|
|
}
|
|
|
|
defer cleanUp()
|
|
|
|
|
|
|
|
n := newThreeHopNetwork(t, channels.aliceToBob, channels.bobToAlice,
|
2017-11-11 02:09:19 +03:00
|
|
|
channels.bobToCarol, channels.carolToBob, testStartingHeight)
|
2017-05-02 02:29:30 +03:00
|
|
|
|
2017-07-09 01:46:27 +03:00
|
|
|
chanID := n.aliceChannelLink.ChanID()
|
|
|
|
|
|
|
|
messages := []expectedMessage{
|
2017-07-09 02:30:20 +03:00
|
|
|
{"alice", "bob", &lnwire.ChannelReestablish{}, false},
|
|
|
|
{"bob", "alice", &lnwire.ChannelReestablish{}, false},
|
|
|
|
|
2017-11-11 02:18:54 +03:00
|
|
|
{"alice", "bob", &lnwire.FundingLocked{}, false},
|
|
|
|
{"bob", "alice", &lnwire.FundingLocked{}, false},
|
|
|
|
|
2017-07-09 01:46:27 +03:00
|
|
|
{"alice", "bob", &lnwire.UpdateAddHTLC{}, false},
|
|
|
|
{"alice", "bob", &lnwire.CommitSig{}, false},
|
|
|
|
{"bob", "alice", &lnwire.RevokeAndAck{}, false},
|
|
|
|
{"bob", "alice", &lnwire.CommitSig{}, false},
|
|
|
|
{"alice", "bob", &lnwire.RevokeAndAck{}, false},
|
|
|
|
|
|
|
|
{"bob", "alice", &lnwire.UpdateFufillHTLC{}, false},
|
|
|
|
{"bob", "alice", &lnwire.CommitSig{}, false},
|
|
|
|
{"alice", "bob", &lnwire.RevokeAndAck{}, false},
|
|
|
|
{"alice", "bob", &lnwire.CommitSig{}, false},
|
|
|
|
{"bob", "alice", &lnwire.RevokeAndAck{}, false},
|
|
|
|
}
|
2017-05-02 02:29:30 +03:00
|
|
|
|
|
|
|
debug := false
|
|
|
|
if debug {
|
|
|
|
// Log message that alice receives.
|
2017-07-09 01:46:27 +03:00
|
|
|
n.aliceServer.intersect(createLogFunc("alice",
|
2017-05-02 02:29:30 +03:00
|
|
|
n.aliceChannelLink.ChanID()))
|
|
|
|
|
|
|
|
// Log message that bob receives.
|
2017-07-09 01:46:27 +03:00
|
|
|
n.bobServer.intersect(createLogFunc("bob",
|
2017-05-02 02:29:30 +03:00
|
|
|
n.firstBobChannelLink.ChanID()))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that alice receives messages in right order.
|
2017-07-09 02:30:20 +03:00
|
|
|
n.aliceServer.intersect(createInterceptorFunc("[alice] <-- [bob]",
|
|
|
|
"alice", messages, chanID, false))
|
2017-05-02 02:29:30 +03:00
|
|
|
|
|
|
|
// Check that bob receives messages in right order.
|
2017-07-09 02:30:20 +03:00
|
|
|
n.bobServer.intersect(createInterceptorFunc("[alice] --> [bob]",
|
|
|
|
"bob", messages, chanID, false))
|
2017-05-02 02:29:30 +03:00
|
|
|
|
|
|
|
if err := n.start(); err != nil {
|
2017-06-29 16:40:45 +03:00
|
|
|
t.Fatalf("unable to start three hop network: %v", err)
|
2017-05-02 02:29:30 +03:00
|
|
|
}
|
|
|
|
defer n.stop()
|
|
|
|
|
2017-08-22 09:36:43 +03:00
|
|
|
amount := lnwire.NewMSatFromSatoshis(btcutil.SatoshiPerBitcoin)
|
2017-08-03 07:11:31 +03:00
|
|
|
htlcAmt, totalTimelock, hops := generateHops(amount, testStartingHeight,
|
|
|
|
n.firstBobChannelLink)
|
2017-06-17 01:08:19 +03:00
|
|
|
|
2017-05-02 02:29:30 +03:00
|
|
|
// Wait for:
|
2017-07-09 02:04:49 +03:00
|
|
|
// * HTLC add request to be sent to bob.
|
|
|
|
// * alice<->bob commitment state to be updated.
|
|
|
|
// * settle request to be sent back from bob to alice.
|
|
|
|
// * alice<->bob commitment state to be updated.
|
|
|
|
// * user notification to be sent.
|
2017-11-11 02:09:19 +03:00
|
|
|
_, err = n.makePayment(n.aliceServer, n.bobServer,
|
2017-07-09 02:04:49 +03:00
|
|
|
n.bobServer.PubKey(), hops, amount, htlcAmt,
|
2017-11-12 02:06:53 +03:00
|
|
|
totalTimelock).Wait(30 * time.Second)
|
2017-06-17 01:08:19 +03:00
|
|
|
if err != nil {
|
2017-05-02 02:29:30 +03:00
|
|
|
t.Fatalf("unable to make the payment: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2017-09-25 22:50:00 +03:00
|
|
|
|
|
|
|
type mockPeer struct {
|
|
|
|
sync.Mutex
|
|
|
|
sentMsgs []lnwire.Message
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockPeer) SendMessage(msg lnwire.Message) error {
|
|
|
|
m.Lock()
|
|
|
|
m.sentMsgs = append(m.sentMsgs, msg)
|
|
|
|
m.Unlock()
|
|
|
|
return nil
|
|
|
|
}
|
2017-11-23 10:15:21 +03:00
|
|
|
func (m *mockPeer) WipeChannel(*wire.OutPoint) error {
|
2017-09-25 22:50:00 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (m *mockPeer) PubKey() [33]byte {
|
|
|
|
return [33]byte{}
|
|
|
|
}
|
|
|
|
func (m *mockPeer) Disconnect(reason error) {
|
|
|
|
}
|
|
|
|
func (m *mockPeer) popSentMsg() lnwire.Message {
|
|
|
|
m.Lock()
|
|
|
|
msg := m.sentMsgs[0]
|
|
|
|
m.sentMsgs[0] = nil
|
|
|
|
m.sentMsgs = m.sentMsgs[1:]
|
|
|
|
m.Unlock()
|
|
|
|
|
|
|
|
return msg
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ Peer = (*mockPeer)(nil)
|
|
|
|
|
|
|
|
func newSingleLinkTestHarness(chanAmt btcutil.Amount) (ChannelLink, func(), error) {
|
|
|
|
globalEpoch := &chainntnfs.BlockEpochEvent{
|
|
|
|
Epochs: make(chan *chainntnfs.BlockEpoch),
|
|
|
|
Cancel: func() {
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
chanID := lnwire.NewShortChanIDFromInt(4)
|
2017-11-11 02:09:19 +03:00
|
|
|
aliceChannel, _, fCleanUp, _, err := createTestChannel(
|
2017-09-25 22:50:00 +03:00
|
|
|
alicePrivKey, bobPrivKey, chanAmt, chanAmt, chanID,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
invoiveRegistry = newMockRegistry()
|
|
|
|
decoder = &mockIteratorDecoder{}
|
|
|
|
obfuscator = newMockObfuscator()
|
|
|
|
alicePeer mockPeer
|
|
|
|
|
|
|
|
globalPolicy = ForwardingPolicy{
|
|
|
|
MinHTLC: lnwire.NewMSatFromSatoshis(5),
|
|
|
|
BaseFee: lnwire.NewMSatFromSatoshis(1),
|
|
|
|
TimeLockDelta: 6,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2018-01-17 07:18:53 +03:00
|
|
|
pCache := &mockPreimageCache{
|
|
|
|
// hash -> preimage
|
|
|
|
preimageMap: make(map[[32]byte][]byte),
|
|
|
|
}
|
|
|
|
|
2017-09-25 22:50:00 +03:00
|
|
|
aliceCfg := ChannelLinkConfig{
|
|
|
|
FwrdingPolicy: globalPolicy,
|
|
|
|
Peer: &alicePeer,
|
2017-10-30 22:57:32 +03:00
|
|
|
Switch: New(Config{}),
|
2017-09-25 22:50:00 +03:00
|
|
|
DecodeHopIterator: decoder.DecodeHopIterator,
|
2017-10-11 05:37:28 +03:00
|
|
|
DecodeOnionObfuscator: func(io.Reader) (ErrorEncrypter, lnwire.FailCode) {
|
2017-09-25 22:50:00 +03:00
|
|
|
return obfuscator, lnwire.CodeNone
|
|
|
|
},
|
|
|
|
GetLastChannelUpdate: mockGetChanUpdateMessage,
|
2018-01-17 07:18:53 +03:00
|
|
|
PreimageCache: pCache,
|
|
|
|
UpdateContractSignals: func(*contractcourt.ContractSignals) error {
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
Registry: invoiveRegistry,
|
|
|
|
BlockEpochs: globalEpoch,
|
2017-09-25 22:50:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const startingHeight = 100
|
|
|
|
aliceLink := NewChannelLink(aliceCfg, aliceChannel, startingHeight)
|
|
|
|
if err := aliceLink.Start(); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanUp := func() {
|
|
|
|
defer fCleanUp()
|
|
|
|
defer aliceLink.Stop()
|
|
|
|
}
|
|
|
|
|
|
|
|
return aliceLink, cleanUp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func assertLinkBandwidth(t *testing.T, link ChannelLink,
|
|
|
|
expected lnwire.MilliSatoshi) {
|
|
|
|
|
|
|
|
currentBandwidth := link.Bandwidth()
|
|
|
|
_, _, line, _ := runtime.Caller(1)
|
|
|
|
if currentBandwidth != expected {
|
|
|
|
t.Fatalf("line %v: alice's link bandwidth is incorrect: "+
|
|
|
|
"expected %v, got %v", line, expected, currentBandwidth)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestChannelLinkBandwidthConsistency ensures that the reported bandwidth of a
|
|
|
|
// given ChannelLink is properly updated in response to downstream messages
|
|
|
|
// from the switch, and upstream messages from its channel peer.
|
|
|
|
//
|
|
|
|
// TODO(roasbeef): add sync hook into packet processing so can eliminate all
|
|
|
|
// sleep in this test and the one below
|
|
|
|
func TestChannelLinkBandwidthConsistency(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2017-11-11 02:19:26 +03:00
|
|
|
// TODO(roasbeef): replace manual bit twiddling with concept of
|
|
|
|
// resource cost for packets?
|
|
|
|
// * or also able to consult link
|
|
|
|
|
2017-09-25 22:50:00 +03:00
|
|
|
// We'll start the test by creating a single instance of
|
|
|
|
const chanAmt = btcutil.SatoshiPerBitcoin * 5
|
|
|
|
aliceLink, cleanUp, err := newSingleLinkTestHarness(chanAmt)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create link: %v", err)
|
|
|
|
}
|
|
|
|
defer cleanUp()
|
|
|
|
|
|
|
|
var (
|
|
|
|
mockBlob [lnwire.OnionPacketSize]byte
|
|
|
|
coreChan = aliceLink.(*channelLink).channel
|
|
|
|
defaultCommitFee = coreChan.StateSnapshot().CommitFee
|
|
|
|
aliceStartingBandwidth = aliceLink.Bandwidth()
|
|
|
|
)
|
|
|
|
|
2017-11-11 02:19:26 +03:00
|
|
|
estimator := &lnwallet.StaticFeeEstimator{
|
2017-11-23 10:12:53 +03:00
|
|
|
FeeRate: 24,
|
2017-11-11 02:19:26 +03:00
|
|
|
}
|
2017-11-23 10:12:53 +03:00
|
|
|
feePerWeight, err := estimator.EstimateFeePerWeight(1)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to query fee estimator: %v", err)
|
|
|
|
}
|
|
|
|
feePerKw := feePerWeight * 1000
|
2017-11-11 02:19:26 +03:00
|
|
|
htlcFee := lnwire.NewMSatFromSatoshis(
|
|
|
|
btcutil.Amount((int64(feePerKw) * lnwallet.HtlcWeight) / 1000),
|
|
|
|
)
|
|
|
|
|
2017-09-25 22:50:00 +03:00
|
|
|
// The starting bandwidth of the channel should be exactly the amount
|
|
|
|
// that we created the channel between her and Bob.
|
|
|
|
expectedBandwidth := lnwire.NewMSatFromSatoshis(chanAmt - defaultCommitFee)
|
|
|
|
assertLinkBandwidth(t, aliceLink, expectedBandwidth)
|
|
|
|
|
|
|
|
// Next, we'll create an HTLC worth 1 BTC, and send it into the link as
|
|
|
|
// 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)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create payment: %v", err)
|
|
|
|
}
|
|
|
|
addPkt := htlcPacket{
|
|
|
|
htlc: htlc,
|
|
|
|
}
|
|
|
|
aliceLink.HandleSwitchPacket(&addPkt)
|
2017-12-21 13:47:35 +03:00
|
|
|
time.Sleep(time.Millisecond * 500)
|
2017-11-11 02:19:26 +03:00
|
|
|
assertLinkBandwidth(t, aliceLink, aliceStartingBandwidth-htlcAmt-htlcFee)
|
2017-09-25 22:50:00 +03:00
|
|
|
|
|
|
|
// If we now send in a valid HTLC settle for the prior HTLC we added,
|
|
|
|
// then the bandwidth should remain unchanged as the remote party will
|
|
|
|
// gain additional channel balance.
|
|
|
|
htlcSettle := &lnwire.UpdateFufillHTLC{
|
|
|
|
ID: 0,
|
|
|
|
PaymentPreimage: invoice.Terms.PaymentPreimage,
|
|
|
|
}
|
|
|
|
aliceLink.HandleChannelUpdate(htlcSettle)
|
2017-12-21 13:47:35 +03:00
|
|
|
time.Sleep(time.Millisecond * 500)
|
2017-09-25 22:50:00 +03:00
|
|
|
assertLinkBandwidth(t, aliceLink, aliceStartingBandwidth-htlcAmt)
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create payment: %v", err)
|
|
|
|
}
|
|
|
|
addPkt = htlcPacket{
|
|
|
|
htlc: htlc,
|
|
|
|
}
|
|
|
|
aliceLink.HandleSwitchPacket(&addPkt)
|
2017-12-21 13:47:35 +03:00
|
|
|
time.Sleep(time.Millisecond * 500)
|
2017-11-11 02:19:26 +03:00
|
|
|
assertLinkBandwidth(t, aliceLink, aliceStartingBandwidth-htlcAmt*2-htlcFee)
|
2017-09-25 22:50:00 +03:00
|
|
|
|
|
|
|
// With that processed, we'll now generate an HTLC fail (sent by the
|
|
|
|
// remote peer) to cancel the HTLC we just added. This should return us
|
|
|
|
// back to the bandwidth of the link right before the HTLC was sent.
|
|
|
|
failMsg := &lnwire.UpdateFailHTLC{
|
|
|
|
ID: 1, // As this is the second HTLC.
|
|
|
|
Reason: lnwire.OpaqueReason([]byte("nop")),
|
|
|
|
}
|
|
|
|
aliceLink.HandleChannelUpdate(failMsg)
|
2017-12-21 13:47:35 +03:00
|
|
|
time.Sleep(time.Millisecond * 500)
|
2017-09-25 22:50:00 +03:00
|
|
|
assertLinkBandwidth(t, aliceLink, aliceStartingBandwidth-htlcAmt)
|
|
|
|
|
|
|
|
// Moving along, we'll now receive a new HTLC from the remote peer,
|
|
|
|
// with an ID of 0 as this is their first HTLC. The bandwidth should
|
2017-11-11 02:19:26 +03:00
|
|
|
// remain unchanged (but Alice will need to pay the fee for the extra
|
|
|
|
// HTLC).
|
2017-09-25 22:50:00 +03:00
|
|
|
updateMsg := &lnwire.UpdateAddHTLC{
|
2017-10-24 10:48:52 +03:00
|
|
|
ID: 0,
|
2017-09-25 22:50:00 +03:00
|
|
|
Amount: htlcAmt,
|
|
|
|
Expiry: 9,
|
|
|
|
PaymentHash: htlc.PaymentHash, // Re-using the same payment hash.
|
|
|
|
}
|
|
|
|
aliceLink.HandleChannelUpdate(updateMsg)
|
2017-12-21 13:47:35 +03:00
|
|
|
time.Sleep(time.Millisecond * 500)
|
2017-11-11 02:19:26 +03:00
|
|
|
assertLinkBandwidth(t, aliceLink, aliceStartingBandwidth-htlcAmt-htlcFee)
|
2017-09-25 22:50:00 +03:00
|
|
|
|
|
|
|
// Next, we'll settle the HTLC with our knowledge of the pre-image that
|
|
|
|
// we eventually learn (simulating a multi-hop payment). The bandwidth
|
|
|
|
// of the channel should now be re-balanced to the starting point.
|
|
|
|
settlePkt := htlcPacket{
|
|
|
|
htlc: &lnwire.UpdateFufillHTLC{
|
|
|
|
ID: 2,
|
|
|
|
PaymentPreimage: invoice.Terms.PaymentPreimage,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
aliceLink.HandleSwitchPacket(&settlePkt)
|
2017-12-21 13:47:35 +03:00
|
|
|
time.Sleep(time.Millisecond * 500)
|
2017-09-25 22:50:00 +03:00
|
|
|
assertLinkBandwidth(t, aliceLink, aliceStartingBandwidth)
|
|
|
|
|
|
|
|
// Finally, we'll test the scenario of failing an HTLC received by the
|
|
|
|
// remote node. This should result in no perceived bandwidth changes.
|
|
|
|
htlcAdd := &lnwire.UpdateAddHTLC{
|
2017-10-24 10:48:52 +03:00
|
|
|
ID: 1,
|
2017-09-25 22:50:00 +03:00
|
|
|
Amount: htlcAmt,
|
|
|
|
Expiry: 9,
|
|
|
|
PaymentHash: htlc.PaymentHash,
|
|
|
|
}
|
|
|
|
aliceLink.HandleChannelUpdate(htlcAdd)
|
2017-12-21 13:47:35 +03:00
|
|
|
time.Sleep(time.Millisecond * 500)
|
2017-11-11 02:19:26 +03:00
|
|
|
assertLinkBandwidth(t, aliceLink, aliceStartingBandwidth-htlcFee)
|
2017-09-25 22:50:00 +03:00
|
|
|
failPkt := htlcPacket{
|
|
|
|
htlc: &lnwire.UpdateFailHTLC{
|
|
|
|
ID: 3,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
aliceLink.HandleSwitchPacket(&failPkt)
|
2017-12-21 13:47:35 +03:00
|
|
|
time.Sleep(time.Millisecond * 500)
|
2017-09-25 22:50:00 +03:00
|
|
|
assertLinkBandwidth(t, aliceLink, aliceStartingBandwidth)
|
|
|
|
}
|
2017-09-25 23:01:17 +03:00
|
|
|
|
|
|
|
// TestChannelLinkBandwidthConsistencyOverflow tests that in the case of a
|
|
|
|
// commitment overflow (no more space for new HTLC's), the bandwidth is updated
|
|
|
|
// properly as items are being added and removed from the overflow queue.
|
|
|
|
func TestChannelLinkBandwidthConsistencyOverflow(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
var mockBlob [lnwire.OnionPacketSize]byte
|
|
|
|
|
|
|
|
const chanAmt = btcutil.SatoshiPerBitcoin * 5
|
|
|
|
aliceLink, cleanUp, err := newSingleLinkTestHarness(chanAmt)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create link: %v", err)
|
|
|
|
}
|
|
|
|
defer cleanUp()
|
|
|
|
|
|
|
|
var (
|
|
|
|
coreLink = aliceLink.(*channelLink)
|
2017-11-11 02:19:47 +03:00
|
|
|
defaultCommitFee = coreLink.channel.StateSnapshot().CommitFee
|
2017-09-25 23:01:17 +03:00
|
|
|
aliceStartingBandwidth = aliceLink.Bandwidth()
|
|
|
|
)
|
|
|
|
|
2017-11-11 02:19:47 +03:00
|
|
|
estimator := &lnwallet.StaticFeeEstimator{
|
2017-11-23 10:12:53 +03:00
|
|
|
FeeRate: 24,
|
|
|
|
}
|
|
|
|
feePerWeight, err := estimator.EstimateFeePerWeight(1)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to query fee estimator: %v", err)
|
2017-11-11 02:19:47 +03:00
|
|
|
}
|
2017-11-23 10:12:53 +03:00
|
|
|
feePerKw := feePerWeight * 1000
|
2017-11-11 02:19:47 +03:00
|
|
|
|
2017-09-25 23:01:17 +03:00
|
|
|
addLinkHTLC := func(amt lnwire.MilliSatoshi) [32]byte {
|
|
|
|
invoice, htlc, err := generatePayment(amt, amt, 5, mockBlob)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create payment: %v", err)
|
|
|
|
}
|
2017-10-30 22:21:07 +03:00
|
|
|
aliceLink.HandleSwitchPacket(&htlcPacket{
|
2017-11-11 02:19:47 +03:00
|
|
|
htlc: htlc,
|
|
|
|
amount: amt,
|
2017-10-30 22:21:07 +03:00
|
|
|
})
|
2017-09-25 23:01:17 +03:00
|
|
|
|
|
|
|
return invoice.Terms.PaymentPreimage
|
|
|
|
}
|
|
|
|
|
|
|
|
// We'll first start by adding enough HTLC's to overflow the commitment
|
|
|
|
// transaction, checking the reported link bandwidth for proper
|
|
|
|
// consistency along the way
|
|
|
|
htlcAmt := lnwire.NewMSatFromSatoshis(100000)
|
|
|
|
totalHtlcAmt := lnwire.MilliSatoshi(0)
|
|
|
|
const numHTLCs = lnwallet.MaxHTLCNumber / 2
|
|
|
|
var preImages [][32]byte
|
|
|
|
for i := 0; i < numHTLCs; i++ {
|
|
|
|
preImage := addLinkHTLC(htlcAmt)
|
|
|
|
preImages = append(preImages, preImage)
|
|
|
|
|
|
|
|
totalHtlcAmt += htlcAmt
|
|
|
|
}
|
|
|
|
|
2017-12-21 13:47:35 +03:00
|
|
|
// TODO(roasbeef): increase sleep
|
2017-11-11 02:19:47 +03:00
|
|
|
time.Sleep(time.Second * 1)
|
|
|
|
commitWeight := lnwallet.CommitWeight + lnwallet.HtlcWeight*numHTLCs
|
|
|
|
htlcFee := lnwire.NewMSatFromSatoshis(
|
|
|
|
btcutil.Amount((int64(feePerKw) * commitWeight) / 1000),
|
|
|
|
)
|
|
|
|
expectedBandwidth := aliceStartingBandwidth - totalHtlcAmt - htlcFee
|
|
|
|
expectedBandwidth += lnwire.NewMSatFromSatoshis(defaultCommitFee)
|
2017-09-25 23:01:17 +03:00
|
|
|
assertLinkBandwidth(t, aliceLink, expectedBandwidth)
|
|
|
|
|
|
|
|
// The overflow queue should be empty at this point, as the commitment
|
|
|
|
// transaction should be full, but not yet overflown.
|
|
|
|
if coreLink.overflowQueue.Length() != 0 {
|
|
|
|
t.Fatalf("wrong overflow queue length: expected %v, got %v", 0,
|
|
|
|
coreLink.overflowQueue.Length())
|
|
|
|
}
|
|
|
|
|
|
|
|
// At this point, the commitment transaction should now be fully
|
|
|
|
// saturated. We'll continue adding HTLC's, and asserting that the
|
2017-11-11 02:19:47 +03:00
|
|
|
// bandwidth accounting is done properly.
|
2017-09-25 23:01:17 +03:00
|
|
|
const numOverFlowHTLCs = 20
|
|
|
|
for i := 0; i < numOverFlowHTLCs; i++ {
|
|
|
|
preImage := addLinkHTLC(htlcAmt)
|
|
|
|
preImages = append(preImages, preImage)
|
|
|
|
|
|
|
|
totalHtlcAmt += htlcAmt
|
|
|
|
}
|
|
|
|
|
2017-11-11 02:19:47 +03:00
|
|
|
time.Sleep(time.Second * 2)
|
|
|
|
expectedBandwidth -= (numOverFlowHTLCs * htlcAmt)
|
2017-09-25 23:01:17 +03:00
|
|
|
assertLinkBandwidth(t, aliceLink, expectedBandwidth)
|
|
|
|
|
|
|
|
// With the extra HTLC's added, the overflow queue should now be
|
|
|
|
// populated with our 10 additional HTLC's.
|
|
|
|
if coreLink.overflowQueue.Length() != numOverFlowHTLCs {
|
|
|
|
t.Fatalf("wrong overflow queue length: expected %v, got %v",
|
|
|
|
numOverFlowHTLCs,
|
|
|
|
coreLink.overflowQueue.Length())
|
|
|
|
}
|
|
|
|
|
|
|
|
// At this point, we'll now settle one of the HTLC's that were added.
|
|
|
|
// The resulting bandwidth change should be non-existent as this will
|
|
|
|
// simply transfer over funds to the remote party. However, the size of
|
|
|
|
// the overflow queue should be decreasing
|
|
|
|
for i := 0; i < numOverFlowHTLCs; i++ {
|
|
|
|
htlcSettle := &lnwire.UpdateFufillHTLC{
|
|
|
|
ID: uint64(i),
|
|
|
|
PaymentPreimage: preImages[i],
|
|
|
|
}
|
|
|
|
|
|
|
|
aliceLink.HandleChannelUpdate(htlcSettle)
|
|
|
|
time.Sleep(time.Millisecond * 50)
|
|
|
|
|
|
|
|
// As we're not actually initiating a full state update, we'll
|
|
|
|
// trigger a free-slot signal manually here.
|
|
|
|
coreLink.overflowQueue.SignalFreeSlot()
|
|
|
|
}
|
|
|
|
|
2017-12-21 13:47:35 +03:00
|
|
|
time.Sleep(time.Millisecond * 500)
|
2017-11-11 02:19:47 +03:00
|
|
|
assertLinkBandwidth(t, aliceLink, expectedBandwidth)
|
|
|
|
|
2017-09-25 23:01:17 +03:00
|
|
|
// Finally, at this point, the queue itself should be fully empty. As
|
|
|
|
// enough slots have been drained from the commitment transaction to
|
|
|
|
// allocate the queue items to.
|
|
|
|
time.Sleep(time.Millisecond * 100)
|
|
|
|
if coreLink.overflowQueue.Length() != 0 {
|
|
|
|
t.Fatalf("wrong overflow queue length: expected %v, got %v", 0,
|
|
|
|
coreLink.overflowQueue.Length())
|
|
|
|
}
|
|
|
|
}
|
2017-07-09 02:30:20 +03:00
|
|
|
|
|
|
|
// TestChannelRetransmission tests the ability of the channel links to
|
|
|
|
// synchronize theirs states after abrupt disconnect.
|
|
|
|
func TestChannelRetransmission(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2017-11-11 02:20:27 +03:00
|
|
|
retransmissionTests := []struct {
|
|
|
|
name string
|
|
|
|
messages []expectedMessage
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
// Tests the ability of the channel links states to be
|
|
|
|
// synchronized after remote node haven't receive
|
|
|
|
// revoke and ack message.
|
|
|
|
name: "intercept last alice revoke_and_ack",
|
|
|
|
messages: []expectedMessage{
|
|
|
|
// First initialization of the channel.
|
|
|
|
{"alice", "bob", &lnwire.ChannelReestablish{}, false},
|
|
|
|
{"bob", "alice", &lnwire.ChannelReestablish{}, false},
|
|
|
|
|
|
|
|
{"alice", "bob", &lnwire.FundingLocked{}, false},
|
|
|
|
{"bob", "alice", &lnwire.FundingLocked{}, false},
|
|
|
|
|
|
|
|
// Send payment from Alice to Bob and intercept
|
|
|
|
// the last revocation message, in this case
|
|
|
|
// Bob should not proceed the payment farther.
|
|
|
|
{"alice", "bob", &lnwire.UpdateAddHTLC{}, false},
|
|
|
|
{"alice", "bob", &lnwire.CommitSig{}, false},
|
|
|
|
{"bob", "alice", &lnwire.RevokeAndAck{}, false},
|
|
|
|
{"bob", "alice", &lnwire.CommitSig{}, false},
|
|
|
|
{"alice", "bob", &lnwire.RevokeAndAck{}, true},
|
|
|
|
|
|
|
|
// Reestablish messages exchange on nodes restart.
|
|
|
|
{"alice", "bob", &lnwire.ChannelReestablish{}, false},
|
|
|
|
{"bob", "alice", &lnwire.ChannelReestablish{}, false},
|
|
|
|
|
|
|
|
// Alice should resend the revoke_and_ack
|
|
|
|
// message to Bob because Bob claimed it in the
|
|
|
|
// reestbalish message.
|
|
|
|
{"alice", "bob", &lnwire.RevokeAndAck{}, false},
|
|
|
|
|
|
|
|
// Proceed the payment farther by sending the
|
|
|
|
// fulfilment message and trigger the state
|
|
|
|
// update.
|
|
|
|
{"bob", "alice", &lnwire.UpdateFufillHTLC{}, false},
|
|
|
|
{"bob", "alice", &lnwire.CommitSig{}, false},
|
|
|
|
{"alice", "bob", &lnwire.RevokeAndAck{}, false},
|
|
|
|
{"alice", "bob", &lnwire.CommitSig{}, false},
|
|
|
|
{"bob", "alice", &lnwire.RevokeAndAck{}, false},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Tests the ability of the channel links states to be
|
|
|
|
// synchronized after remote node haven't receive
|
|
|
|
// revoke and ack message.
|
|
|
|
name: "intercept bob revoke_and_ack commit_sig messages",
|
|
|
|
messages: []expectedMessage{
|
|
|
|
{"alice", "bob", &lnwire.ChannelReestablish{}, false},
|
|
|
|
{"bob", "alice", &lnwire.ChannelReestablish{}, false},
|
|
|
|
|
|
|
|
{"alice", "bob", &lnwire.FundingLocked{}, false},
|
|
|
|
{"bob", "alice", &lnwire.FundingLocked{}, false},
|
|
|
|
|
|
|
|
// Send payment from Alice to Bob and intercept
|
|
|
|
// the last revocation message, in this case
|
|
|
|
// Bob should not proceed the payment farther.
|
|
|
|
{"alice", "bob", &lnwire.UpdateAddHTLC{}, false},
|
|
|
|
{"alice", "bob", &lnwire.CommitSig{}, false},
|
|
|
|
|
|
|
|
// Intercept bob commit sig and revoke and ack
|
|
|
|
// messages.
|
|
|
|
{"bob", "alice", &lnwire.RevokeAndAck{}, true},
|
|
|
|
{"bob", "alice", &lnwire.CommitSig{}, true},
|
|
|
|
|
|
|
|
// Reestablish messages exchange on nodes restart.
|
|
|
|
{"alice", "bob", &lnwire.ChannelReestablish{}, false},
|
|
|
|
{"bob", "alice", &lnwire.ChannelReestablish{}, false},
|
|
|
|
|
|
|
|
// Bob should resend previously intercepted messages.
|
|
|
|
{"bob", "alice", &lnwire.RevokeAndAck{}, false},
|
|
|
|
{"bob", "alice", &lnwire.CommitSig{}, false},
|
|
|
|
|
|
|
|
// Proceed the payment farther by sending the
|
|
|
|
// fulfilment message and trigger the state
|
|
|
|
// update.
|
|
|
|
{"alice", "bob", &lnwire.RevokeAndAck{}, false},
|
|
|
|
{"bob", "alice", &lnwire.UpdateFufillHTLC{}, false},
|
|
|
|
{"bob", "alice", &lnwire.CommitSig{}, false},
|
|
|
|
{"alice", "bob", &lnwire.RevokeAndAck{}, false},
|
|
|
|
{"alice", "bob", &lnwire.CommitSig{}, false},
|
|
|
|
{"bob", "alice", &lnwire.RevokeAndAck{}, false},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Tests the ability of the channel links states to be
|
|
|
|
// synchronized after remote node haven't receive
|
|
|
|
// update and commit sig messages.
|
|
|
|
name: "intercept update add htlc and commit sig messages",
|
|
|
|
messages: []expectedMessage{
|
|
|
|
{"alice", "bob", &lnwire.ChannelReestablish{}, false},
|
|
|
|
{"bob", "alice", &lnwire.ChannelReestablish{}, false},
|
|
|
|
|
|
|
|
{"alice", "bob", &lnwire.FundingLocked{}, false},
|
|
|
|
{"bob", "alice", &lnwire.FundingLocked{}, false},
|
|
|
|
|
|
|
|
// Attempt make a payment from Alice to Bob,
|
|
|
|
// which is intercepted, emulating the Bob
|
|
|
|
// server abrupt stop.
|
|
|
|
{"alice", "bob", &lnwire.UpdateAddHTLC{}, true},
|
|
|
|
{"alice", "bob", &lnwire.CommitSig{}, true},
|
|
|
|
|
|
|
|
// Restart of the nodes, and after that nodes
|
|
|
|
// should exchange the reestablish messages.
|
|
|
|
{"alice", "bob", &lnwire.ChannelReestablish{}, false},
|
|
|
|
{"bob", "alice", &lnwire.ChannelReestablish{}, false},
|
|
|
|
|
|
|
|
{"alice", "bob", &lnwire.FundingLocked{}, false},
|
|
|
|
{"bob", "alice", &lnwire.FundingLocked{}, false},
|
|
|
|
|
|
|
|
// After Bob has notified Alice that he didn't
|
|
|
|
// receive updates Alice should re-send them.
|
|
|
|
{"alice", "bob", &lnwire.UpdateAddHTLC{}, false},
|
|
|
|
{"alice", "bob", &lnwire.CommitSig{}, false},
|
|
|
|
|
|
|
|
{"bob", "alice", &lnwire.RevokeAndAck{}, false},
|
|
|
|
{"bob", "alice", &lnwire.CommitSig{}, false},
|
|
|
|
{"alice", "bob", &lnwire.RevokeAndAck{}, false},
|
|
|
|
|
|
|
|
{"bob", "alice", &lnwire.UpdateFufillHTLC{}, false},
|
|
|
|
{"bob", "alice", &lnwire.CommitSig{}, false},
|
|
|
|
{"alice", "bob", &lnwire.RevokeAndAck{}, false},
|
|
|
|
{"alice", "bob", &lnwire.CommitSig{}, false},
|
|
|
|
{"bob", "alice", &lnwire.RevokeAndAck{}, false},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2017-07-09 02:30:20 +03:00
|
|
|
paymentWithRestart := func(t *testing.T, messages []expectedMessage) {
|
|
|
|
channels, cleanUp, restoreChannelsFromDb, err := createClusterChannels(
|
|
|
|
btcutil.SatoshiPerBitcoin*5,
|
|
|
|
btcutil.SatoshiPerBitcoin*5)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create channel: %v", err)
|
|
|
|
}
|
|
|
|
defer cleanUp()
|
|
|
|
|
|
|
|
chanID := lnwire.NewChanIDFromOutPoint(channels.aliceToBob.ChannelPoint())
|
|
|
|
serverErr := make(chan error, 4)
|
|
|
|
|
|
|
|
aliceInterceptor := createInterceptorFunc("[alice] <-- [bob]",
|
2017-11-11 02:20:44 +03:00
|
|
|
"alice", messages, chanID, false)
|
2017-07-09 02:30:20 +03:00
|
|
|
bobInterceptor := createInterceptorFunc("[alice] --> [bob]",
|
2017-11-11 02:20:44 +03:00
|
|
|
"bob", messages, chanID, false)
|
2017-07-09 02:30:20 +03:00
|
|
|
|
2017-11-12 02:05:09 +03:00
|
|
|
ct := newConcurrentTester(t)
|
|
|
|
|
2017-11-12 02:06:53 +03:00
|
|
|
// Add interceptor to check the order of Bob and Alice
|
|
|
|
// messages.
|
|
|
|
n := newThreeHopNetwork(ct,
|
2017-11-11 02:09:19 +03:00
|
|
|
channels.aliceToBob, channels.bobToAlice,
|
|
|
|
channels.bobToCarol, channels.carolToBob,
|
|
|
|
testStartingHeight,
|
|
|
|
)
|
2017-07-09 02:30:20 +03:00
|
|
|
n.aliceServer.intersect(aliceInterceptor)
|
|
|
|
n.bobServer.intersect(bobInterceptor)
|
|
|
|
if err := n.start(); err != nil {
|
2017-11-12 02:06:53 +03:00
|
|
|
ct.Fatalf("unable to start three hop network: %v", err)
|
2017-07-09 02:30:20 +03:00
|
|
|
}
|
|
|
|
defer n.stop()
|
|
|
|
|
|
|
|
bobBandwidthBefore := n.firstBobChannelLink.Bandwidth()
|
|
|
|
aliceBandwidthBefore := n.aliceChannelLink.Bandwidth()
|
|
|
|
|
|
|
|
amount := lnwire.NewMSatFromSatoshis(btcutil.SatoshiPerBitcoin)
|
|
|
|
htlcAmt, totalTimelock, hops := generateHops(amount, testStartingHeight,
|
|
|
|
n.firstBobChannelLink)
|
|
|
|
|
2017-11-11 02:21:11 +03:00
|
|
|
// Send payment which should fail because we intercept the
|
|
|
|
// update and commit messages.
|
2017-11-12 02:06:53 +03:00
|
|
|
//
|
|
|
|
// TODO(roasbeef); increase timeout?
|
2017-07-09 02:30:20 +03:00
|
|
|
receiver := n.bobServer
|
|
|
|
rhash, err := n.makePayment(n.aliceServer, receiver,
|
|
|
|
n.bobServer.PubKey(), hops, amount, htlcAmt,
|
2017-11-12 02:06:53 +03:00
|
|
|
totalTimelock).Wait(time.Second * 5)
|
2017-07-09 02:30:20 +03:00
|
|
|
if err == nil {
|
2017-11-12 02:06:53 +03:00
|
|
|
ct.Fatalf("payment shouldn't haven been finished")
|
2017-07-09 02:30:20 +03:00
|
|
|
}
|
|
|
|
|
2017-11-11 02:21:11 +03:00
|
|
|
// Stop network cluster and create new one, with the old
|
|
|
|
// channels states. Also do the *hack* - save the payment
|
|
|
|
// receiver to pass it in new channel link, otherwise payment
|
|
|
|
// will be failed because of the unknown payment hash. Hack
|
|
|
|
// will be removed with sphinx payment.
|
2017-07-09 02:30:20 +03:00
|
|
|
bobRegistry := n.bobServer.registry
|
|
|
|
n.stop()
|
|
|
|
|
|
|
|
channels, err = restoreChannelsFromDb()
|
|
|
|
if err != nil {
|
2017-11-12 02:06:53 +03:00
|
|
|
ct.Fatalf("unable to restore channels from database: %v", err)
|
2017-07-09 02:30:20 +03:00
|
|
|
}
|
|
|
|
|
2017-11-12 02:06:53 +03:00
|
|
|
n = newThreeHopNetwork(ct, channels.aliceToBob, channels.bobToAlice,
|
2017-11-11 02:09:19 +03:00
|
|
|
channels.bobToCarol, channels.carolToBob, testStartingHeight)
|
2017-07-09 02:30:20 +03:00
|
|
|
n.firstBobChannelLink.cfg.Registry = bobRegistry
|
|
|
|
n.aliceServer.intersect(aliceInterceptor)
|
|
|
|
n.bobServer.intersect(bobInterceptor)
|
|
|
|
|
|
|
|
if err := n.start(); err != nil {
|
2017-11-12 02:06:53 +03:00
|
|
|
ct.Fatalf("unable to start three hop network: %v", err)
|
2017-07-09 02:30:20 +03:00
|
|
|
}
|
|
|
|
defer n.stop()
|
|
|
|
|
|
|
|
// Wait for reestablishment to be proceeded and invoice to be settled.
|
|
|
|
// TODO(andrew.shvv) Will be removed if we move the notification center
|
|
|
|
// to the channel link itself.
|
|
|
|
|
2017-11-12 03:09:14 +03:00
|
|
|
var invoice channeldb.Invoice
|
2017-07-09 02:30:20 +03:00
|
|
|
for i := 0; i < 20; i++ {
|
|
|
|
select {
|
|
|
|
case <-time.After(time.Millisecond * 200):
|
|
|
|
case serverErr := <-serverErr:
|
2017-11-12 02:06:53 +03:00
|
|
|
ct.Fatalf("server error: %v", serverErr)
|
2017-07-09 02:30:20 +03:00
|
|
|
}
|
|
|
|
|
2017-11-11 02:09:19 +03:00
|
|
|
// Check that alice invoice wasn't settled and
|
|
|
|
// bandwidth of htlc links hasn't been changed.
|
2017-07-09 02:30:20 +03:00
|
|
|
invoice, err = receiver.registry.LookupInvoice(rhash)
|
|
|
|
if err != nil {
|
|
|
|
err = errors.Errorf("unable to get invoice: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !invoice.Terms.Settled {
|
|
|
|
err = errors.Errorf("alice invoice haven't been settled")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-11-11 02:21:11 +03:00
|
|
|
aliceExpectedBandwidth := aliceBandwidthBefore - htlcAmt
|
|
|
|
if aliceExpectedBandwidth != n.aliceChannelLink.Bandwidth() {
|
|
|
|
err = errors.Errorf("expected alice to have %v, instead has %v",
|
|
|
|
aliceExpectedBandwidth, n.aliceChannelLink.Bandwidth())
|
2017-07-09 02:30:20 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-11-11 02:21:11 +03:00
|
|
|
bobExpectedBandwidth := bobBandwidthBefore + htlcAmt
|
2017-11-12 02:06:53 +03:00
|
|
|
if bobExpectedBandwidth != n.firstBobChannelLink.Bandwidth() {
|
2017-11-11 02:21:11 +03:00
|
|
|
err = errors.Errorf("expected bob to have %v, instead has %v",
|
|
|
|
bobExpectedBandwidth, n.firstBobChannelLink.Bandwidth())
|
2017-07-09 02:30:20 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2017-11-12 02:06:53 +03:00
|
|
|
ct.Fatal(err)
|
2017-07-09 02:30:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range retransmissionTests {
|
2017-11-12 02:06:20 +03:00
|
|
|
passed := t.Run(test.name, func(t *testing.T) {
|
2017-07-09 02:30:20 +03:00
|
|
|
paymentWithRestart(t, test.messages)
|
|
|
|
})
|
2017-11-12 02:06:20 +03:00
|
|
|
|
|
|
|
if !passed {
|
|
|
|
break
|
|
|
|
}
|
2017-07-09 02:30:20 +03:00
|
|
|
}
|
2017-11-12 02:06:20 +03:00
|
|
|
|
2017-07-09 02:30:20 +03:00
|
|
|
}
|
2017-11-24 07:21:46 +03:00
|
|
|
|
|
|
|
// TestShouldAdjustCommitFee tests the shouldAdjustCommitFee pivot function to
|
|
|
|
// ensure that ie behaves properly. We should only update the fee if it
|
|
|
|
// deviates from our current fee by more 10% or more.
|
|
|
|
func TestShouldAdjustCommitFee(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
netFee btcutil.Amount
|
|
|
|
chanFee btcutil.Amount
|
|
|
|
shouldAdjust bool
|
|
|
|
}{
|
|
|
|
|
|
|
|
// The network fee is 3x lower than the current commitment
|
|
|
|
// transaction. As a result, we should adjust our fee to match
|
|
|
|
// it.
|
|
|
|
{
|
|
|
|
netFee: 100,
|
|
|
|
chanFee: 3000,
|
|
|
|
shouldAdjust: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
// The network fee is lower than the current commitment fee,
|
|
|
|
// but only slightly so, so we won't update the commitment fee.
|
|
|
|
{
|
|
|
|
netFee: 2999,
|
|
|
|
chanFee: 3000,
|
|
|
|
shouldAdjust: false,
|
|
|
|
},
|
|
|
|
|
|
|
|
// The network fee is lower than the commitment fee, but only
|
|
|
|
// right before it crosses our current threshold.
|
|
|
|
{
|
|
|
|
netFee: 1000,
|
|
|
|
chanFee: 1099,
|
|
|
|
shouldAdjust: false,
|
|
|
|
},
|
|
|
|
|
|
|
|
// The network fee is lower than the commitment fee, and within
|
|
|
|
// our range of adjustment, so we should adjust.
|
|
|
|
{
|
|
|
|
netFee: 1000,
|
|
|
|
chanFee: 1100,
|
|
|
|
shouldAdjust: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
// The network fee is 2x higher than our commitment fee, so we
|
|
|
|
// should adjust upwards.
|
|
|
|
{
|
|
|
|
netFee: 2000,
|
|
|
|
chanFee: 1000,
|
|
|
|
shouldAdjust: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
// The network fee is higher than our commitment fee, but only
|
|
|
|
// slightly so, so we won't update.
|
|
|
|
{
|
|
|
|
netFee: 1001,
|
|
|
|
chanFee: 1000,
|
|
|
|
shouldAdjust: false,
|
|
|
|
},
|
|
|
|
|
|
|
|
// The network fee is higher than our commitment fee, but
|
|
|
|
// hasn't yet crossed our activation threshold.
|
|
|
|
{
|
|
|
|
netFee: 1100,
|
|
|
|
chanFee: 1099,
|
|
|
|
shouldAdjust: false,
|
|
|
|
},
|
|
|
|
|
|
|
|
// The network fee is higher than our commitment fee, and
|
|
|
|
// within our activation threshold, so we should update our
|
|
|
|
// fee.
|
|
|
|
{
|
|
|
|
netFee: 1100,
|
|
|
|
chanFee: 1000,
|
|
|
|
shouldAdjust: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
// Our fees match exactly, so we shouldn't update it at all.
|
|
|
|
{
|
|
|
|
netFee: 1000,
|
|
|
|
chanFee: 1000,
|
|
|
|
shouldAdjust: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
|
|
|
adjustedFee := shouldAdjustCommitFee(
|
|
|
|
test.netFee, test.chanFee,
|
|
|
|
)
|
|
|
|
|
|
|
|
if adjustedFee && !test.shouldAdjust {
|
|
|
|
t.Fatalf("test #%v failed: net_fee=%v, "+
|
|
|
|
"chan_fee=%v, adjust_expect=%v, adjust_returned=%v",
|
|
|
|
i, test.netFee, test.chanFee, test.shouldAdjust,
|
|
|
|
adjustedFee)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-11-24 07:31:45 +03:00
|
|
|
|
|
|
|
// TestChannelLinkUpdateCommitFee tests that when a new block comes in, the
|
|
|
|
// channel link properly checks to see if it should update the commitment fee.
|
|
|
|
func TestChannelLinkUpdateCommitFee(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
// First, we'll create our traditional three hop network. We'll only be
|
|
|
|
// interacting with and asserting the state of two of the end points
|
|
|
|
// for this test.
|
|
|
|
channels, cleanUp, _, err := createClusterChannels(
|
|
|
|
btcutil.SatoshiPerBitcoin*3,
|
|
|
|
btcutil.SatoshiPerBitcoin*5)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create channel: %v", err)
|
|
|
|
}
|
|
|
|
defer cleanUp()
|
|
|
|
|
|
|
|
n := newThreeHopNetwork(t, channels.aliceToBob, channels.bobToAlice,
|
|
|
|
channels.bobToCarol, channels.carolToBob, testStartingHeight)
|
|
|
|
|
|
|
|
// First, we'll set up some message interceptors to ensure that the
|
|
|
|
// proper messages are sent when updating fees.
|
|
|
|
chanID := n.aliceChannelLink.ChanID()
|
|
|
|
messages := []expectedMessage{
|
|
|
|
{"alice", "bob", &lnwire.ChannelReestablish{}, false},
|
|
|
|
{"bob", "alice", &lnwire.ChannelReestablish{}, false},
|
|
|
|
|
|
|
|
{"alice", "bob", &lnwire.FundingLocked{}, false},
|
|
|
|
{"bob", "alice", &lnwire.FundingLocked{}, false},
|
|
|
|
|
|
|
|
{"alice", "bob", &lnwire.UpdateFee{}, false},
|
|
|
|
|
|
|
|
{"alice", "bob", &lnwire.CommitSig{}, false},
|
|
|
|
{"bob", "alice", &lnwire.RevokeAndAck{}, false},
|
|
|
|
{"bob", "alice", &lnwire.CommitSig{}, false},
|
|
|
|
{"alice", "bob", &lnwire.RevokeAndAck{}, false},
|
|
|
|
}
|
|
|
|
n.aliceServer.intersect(createInterceptorFunc("[alice] <-- [bob]",
|
|
|
|
"alice", messages, chanID, false))
|
|
|
|
n.bobServer.intersect(createInterceptorFunc("[alice] --> [bob]",
|
|
|
|
"bob", messages, chanID, false))
|
|
|
|
|
|
|
|
if err := n.start(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer n.stop()
|
|
|
|
defer n.feeEstimator.Stop()
|
|
|
|
|
|
|
|
// First, we'll start off all channels at "height" 9000 by sending a
|
|
|
|
// new epoch to all the clients.
|
|
|
|
select {
|
|
|
|
case n.aliceBlockEpoch <- &chainntnfs.BlockEpoch{
|
|
|
|
Height: 9000,
|
|
|
|
}:
|
|
|
|
case <-time.After(time.Second * 5):
|
|
|
|
t.Fatalf("link didn't read block epoch")
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case n.bobFirstBlockEpoch <- &chainntnfs.BlockEpoch{
|
|
|
|
Height: 9000,
|
|
|
|
}:
|
|
|
|
case <-time.After(time.Second * 5):
|
|
|
|
t.Fatalf("link didn't read block epoch")
|
|
|
|
}
|
|
|
|
|
|
|
|
startingFeeRate := channels.aliceToBob.CommitFeeRate()
|
|
|
|
|
|
|
|
// Next, we'll send the first fee rate response to Alice.
|
|
|
|
select {
|
|
|
|
case n.feeEstimator.weightFeeIn <- startingFeeRate / 1000:
|
|
|
|
case <-time.After(time.Second * 5):
|
|
|
|
t.Fatalf("alice didn't query for the new " +
|
|
|
|
"network fee")
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(time.Millisecond * 500)
|
|
|
|
|
|
|
|
// The fee rate on the alice <-> bob channel should still be the same
|
|
|
|
// on both sides.
|
|
|
|
aliceFeeRate := channels.aliceToBob.CommitFeeRate()
|
|
|
|
bobFeeRate := channels.bobToAlice.CommitFeeRate()
|
|
|
|
if aliceFeeRate != bobFeeRate {
|
|
|
|
t.Fatalf("fee rates don't match: expected %v got %v",
|
|
|
|
aliceFeeRate, bobFeeRate)
|
|
|
|
}
|
|
|
|
if aliceFeeRate != startingFeeRate {
|
|
|
|
t.Fatalf("alice's fee rate shouldn't have changed: "+
|
|
|
|
"expected %v, got %v", aliceFeeRate, startingFeeRate)
|
|
|
|
}
|
|
|
|
if bobFeeRate != startingFeeRate {
|
|
|
|
t.Fatalf("bob's fee rate shouldn't have changed: "+
|
|
|
|
"expected %v, got %v", bobFeeRate, startingFeeRate)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now we'll send a new block update to all end points, with a new
|
|
|
|
// height THAT'S OVER 9000!!!
|
|
|
|
select {
|
|
|
|
case n.aliceBlockEpoch <- &chainntnfs.BlockEpoch{
|
|
|
|
Height: 9001,
|
|
|
|
}:
|
|
|
|
case <-time.After(time.Second * 5):
|
|
|
|
t.Fatalf("link didn't read block epoch")
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case n.bobFirstBlockEpoch <- &chainntnfs.BlockEpoch{
|
|
|
|
Height: 9001,
|
|
|
|
}:
|
|
|
|
case <-time.After(time.Second * 5):
|
|
|
|
t.Fatalf("link didn't read block epoch")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next, we'll set up a deliver a fee rate that's triple the current
|
|
|
|
// fee rate. This should cause the Alice (the initiator) to trigger a
|
|
|
|
// fee update.
|
|
|
|
newFeeRate := startingFeeRate * 3
|
|
|
|
select {
|
|
|
|
case n.feeEstimator.weightFeeIn <- newFeeRate:
|
|
|
|
case <-time.After(time.Second * 5):
|
|
|
|
t.Fatalf("alice didn't query for the new " +
|
|
|
|
"network fee")
|
|
|
|
}
|
|
|
|
|
2017-12-22 21:29:12 +03:00
|
|
|
time.Sleep(time.Second * 2)
|
2017-11-24 07:31:45 +03:00
|
|
|
|
|
|
|
// At this point, Alice should've triggered a new fee update that
|
|
|
|
// increased the fee rate to match the new rate.
|
|
|
|
//
|
|
|
|
// We'll scale the new fee rate by 100 as we deal with units of fee
|
|
|
|
// per-kw.
|
|
|
|
expectedFeeRate := newFeeRate * 1000
|
|
|
|
aliceFeeRate = channels.aliceToBob.CommitFeeRate()
|
|
|
|
bobFeeRate = channels.bobToAlice.CommitFeeRate()
|
|
|
|
if aliceFeeRate != expectedFeeRate {
|
|
|
|
t.Fatalf("alice's fee rate didn't change: expected %v, got %v",
|
|
|
|
expectedFeeRate, aliceFeeRate)
|
|
|
|
}
|
|
|
|
if bobFeeRate != expectedFeeRate {
|
|
|
|
t.Fatalf("bob's fee rate didn't change: expected %v, got %v",
|
|
|
|
expectedFeeRate, aliceFeeRate)
|
|
|
|
}
|
|
|
|
if aliceFeeRate != bobFeeRate {
|
|
|
|
t.Fatalf("fee rates don't match: expected %v got %v",
|
|
|
|
aliceFeeRate, bobFeeRate)
|
|
|
|
}
|
|
|
|
}
|
2018-01-04 23:23:31 +03:00
|
|
|
|
|
|
|
// TestChannelLinkRejectDuplicatePayment tests that if a link receives an
|
|
|
|
// incoming HTLC for a payment we have already settled, then it rejects the
|
|
|
|
// HTLC. We do this as we want to enforce the fact that invoices are only to be
|
|
|
|
// used _once.
|
|
|
|
func TestChannelLinkRejectDuplicatePayment(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
// First, we'll create our traditional three hop network. We'll only be
|
|
|
|
// interacting with and asserting the state of two of the end points
|
|
|
|
// for this test.
|
|
|
|
channels, cleanUp, _, err := createClusterChannels(
|
|
|
|
btcutil.SatoshiPerBitcoin*3,
|
|
|
|
btcutil.SatoshiPerBitcoin*5)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create channel: %v", err)
|
|
|
|
}
|
|
|
|
defer cleanUp()
|
|
|
|
|
|
|
|
n := newThreeHopNetwork(t, channels.aliceToBob, channels.bobToAlice,
|
|
|
|
channels.bobToCarol, channels.carolToBob, testStartingHeight)
|
|
|
|
if err := n.start(); err != nil {
|
|
|
|
t.Fatalf("unable to start three hop network: %v", err)
|
|
|
|
}
|
|
|
|
defer n.stop()
|
|
|
|
|
|
|
|
amount := lnwire.NewMSatFromSatoshis(btcutil.SatoshiPerBitcoin)
|
|
|
|
|
|
|
|
// We'll start off by making a payment from Alice to Carol. We'll
|
|
|
|
// manually generate this request so we can control all the parameters.
|
|
|
|
htlcAmt, totalTimelock, hops := generateHops(amount, testStartingHeight,
|
|
|
|
n.firstBobChannelLink, n.carolChannelLink)
|
|
|
|
blob, err := generateRoute(hops...)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
invoice, htlc, err := generatePayment(amount, htlcAmt, totalTimelock,
|
|
|
|
blob)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := n.carolServer.registry.AddInvoice(*invoice); err != nil {
|
|
|
|
t.Fatalf("unable to add invoice in carol registry: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// With the invoice now added to Carol's registry, we'll send the
|
|
|
|
// payment. It should succeed w/o any issues as it has been crafted
|
|
|
|
// properly.
|
|
|
|
_, err = n.aliceServer.htlcSwitch.SendHTLC(n.bobServer.PubKey(), htlc,
|
|
|
|
newMockDeobfuscator())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send payment to carol: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.bobServer.PubKey(), htlc,
|
|
|
|
newMockDeobfuscator())
|
|
|
|
if err.Error() != lnwire.CodeUnknownPaymentHash.String() {
|
|
|
|
t.Fatal("error haven't been received")
|
|
|
|
}
|
|
|
|
}
|
2018-01-17 07:18:53 +03:00
|
|
|
|
|
|
|
// TODO(roasbeef): add test for re-sending after hodl mode, to settle any lingering
|