2017-05-02 02:29:30 +03:00
|
|
|
package htlcswitch
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2017-08-01 22:53:07 +03:00
|
|
|
"crypto/rand"
|
2017-05-02 02:29:30 +03:00
|
|
|
"crypto/sha256"
|
2017-11-11 02:15:19 +03:00
|
|
|
"fmt"
|
2017-05-02 02:29:30 +03:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
|
2017-06-29 16:40:45 +03:00
|
|
|
"io"
|
|
|
|
|
|
|
|
"math/big"
|
|
|
|
|
2017-07-09 02:30:20 +03:00
|
|
|
"net"
|
|
|
|
|
2017-05-02 02:29:30 +03:00
|
|
|
"github.com/btcsuite/fastsha256"
|
|
|
|
"github.com/go-errors/errors"
|
2017-08-03 07:11:31 +03:00
|
|
|
"github.com/lightningnetwork/lnd/chainntnfs"
|
2017-05-02 02:29:30 +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/lightningnetwork/lnd/shachain"
|
|
|
|
"github.com/roasbeef/btcd/btcec"
|
|
|
|
"github.com/roasbeef/btcd/chaincfg/chainhash"
|
|
|
|
"github.com/roasbeef/btcd/wire"
|
|
|
|
"github.com/roasbeef/btcutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
alicePrivKey = []byte("alice priv key")
|
|
|
|
bobPrivKey = []byte("bob priv key")
|
|
|
|
carolPrivKey = []byte("carol priv key")
|
2017-06-29 16:40:45 +03:00
|
|
|
|
|
|
|
testPrivKey = []byte{
|
|
|
|
0x81, 0xb6, 0x37, 0xd8, 0xfc, 0xd2, 0xc6, 0xda,
|
|
|
|
0x63, 0x59, 0xe6, 0x96, 0x31, 0x13, 0xa1, 0x17,
|
|
|
|
0xd, 0xe7, 0x95, 0xe4, 0xb7, 0x25, 0xb8, 0x4d,
|
|
|
|
0x1e, 0xb, 0x4c, 0xfd, 0x9e, 0xc5, 0x8c, 0xe9,
|
|
|
|
}
|
|
|
|
|
|
|
|
_, testPubKey = btcec.PrivKeyFromBytes(btcec.S256(), testPrivKey)
|
|
|
|
testSig = &btcec.Signature{
|
|
|
|
R: new(big.Int),
|
|
|
|
S: new(big.Int),
|
|
|
|
}
|
|
|
|
|
|
|
|
_, _ = testSig.R.SetString("6372440660162918006277497454296753625158993"+
|
|
|
|
"5445068131219452686511677818569431", 10)
|
|
|
|
_, _ = testSig.S.SetString("1880105606924982582529128710493133386286603"+
|
|
|
|
"3135609736119018462340006816851118", 10)
|
2017-05-02 02:29:30 +03:00
|
|
|
)
|
|
|
|
|
2017-06-29 16:40:45 +03:00
|
|
|
// mockGetChanUpdateMessage helper function which returns topology update
|
|
|
|
// of the channel
|
|
|
|
func mockGetChanUpdateMessage() (*lnwire.ChannelUpdate, error) {
|
|
|
|
return &lnwire.ChannelUpdate{
|
|
|
|
Signature: testSig,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2017-05-02 02:29:30 +03:00
|
|
|
// generateRandomBytes returns securely generated random bytes.
|
|
|
|
// It will return an error if the system's secure random
|
|
|
|
// number generator fails to function correctly, in which
|
|
|
|
// case the caller should not continue.
|
|
|
|
func generateRandomBytes(n int) ([]byte, error) {
|
|
|
|
b := make([]byte, n)
|
|
|
|
|
2017-08-01 22:53:07 +03:00
|
|
|
// TODO(roasbeef): should use counter in tests (atomic) rather than
|
|
|
|
// this
|
|
|
|
|
2017-05-02 02:29:30 +03:00
|
|
|
_, err := rand.Read(b[:])
|
|
|
|
// Note that Err == nil only if we read len(b) bytes.
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// createTestChannel creates the channel and returns our and remote channels
|
|
|
|
// representations.
|
2017-07-31 00:09:10 +03:00
|
|
|
//
|
|
|
|
// TODO(roasbeef): need to factor out, similar func re-used in many parts of codebase
|
|
|
|
func createTestChannel(alicePrivKey, bobPrivKey []byte,
|
|
|
|
aliceAmount, bobAmount btcutil.Amount,
|
2017-07-09 02:30:20 +03:00
|
|
|
chanID lnwire.ShortChannelID) (*lnwallet.LightningChannel, *lnwallet.LightningChannel, func(),
|
|
|
|
func() (*lnwallet.LightningChannel, *lnwallet.LightningChannel,
|
|
|
|
error), error) {
|
2017-05-02 02:29:30 +03:00
|
|
|
|
|
|
|
aliceKeyPriv, aliceKeyPub := btcec.PrivKeyFromBytes(btcec.S256(), alicePrivKey)
|
|
|
|
bobKeyPriv, bobKeyPub := btcec.PrivKeyFromBytes(btcec.S256(), bobPrivKey)
|
|
|
|
|
|
|
|
channelCapacity := aliceAmount + bobAmount
|
|
|
|
aliceDustLimit := btcutil.Amount(200)
|
|
|
|
bobDustLimit := btcutil.Amount(800)
|
|
|
|
csvTimeoutAlice := uint32(5)
|
|
|
|
csvTimeoutBob := uint32(4)
|
|
|
|
|
|
|
|
var hash [sha256.Size]byte
|
|
|
|
randomSeed, err := generateRandomBytes(sha256.Size)
|
|
|
|
if err != nil {
|
2017-07-09 02:30:20 +03:00
|
|
|
return nil, nil, nil, nil, err
|
2017-05-02 02:29:30 +03:00
|
|
|
}
|
|
|
|
copy(hash[:], randomSeed)
|
|
|
|
|
|
|
|
prevOut := &wire.OutPoint{
|
|
|
|
Hash: chainhash.Hash(hash),
|
|
|
|
Index: 0,
|
|
|
|
}
|
|
|
|
fundingTxIn := wire.NewTxIn(prevOut, nil, nil)
|
|
|
|
|
2017-07-31 00:09:10 +03:00
|
|
|
aliceCfg := channeldb.ChannelConfig{
|
|
|
|
ChannelConstraints: channeldb.ChannelConstraints{
|
|
|
|
DustLimit: aliceDustLimit,
|
|
|
|
},
|
|
|
|
CsvDelay: uint16(csvTimeoutAlice),
|
|
|
|
MultiSigKey: aliceKeyPub,
|
|
|
|
RevocationBasePoint: aliceKeyPub,
|
|
|
|
PaymentBasePoint: aliceKeyPub,
|
|
|
|
DelayBasePoint: aliceKeyPub,
|
2017-11-17 06:48:55 +03:00
|
|
|
HtlcBasePoint: aliceKeyPub,
|
2017-07-31 00:09:10 +03:00
|
|
|
}
|
|
|
|
bobCfg := channeldb.ChannelConfig{
|
|
|
|
ChannelConstraints: channeldb.ChannelConstraints{
|
|
|
|
DustLimit: bobDustLimit,
|
|
|
|
},
|
|
|
|
CsvDelay: uint16(csvTimeoutBob),
|
|
|
|
MultiSigKey: bobKeyPub,
|
|
|
|
RevocationBasePoint: bobKeyPub,
|
|
|
|
PaymentBasePoint: bobKeyPub,
|
|
|
|
DelayBasePoint: bobKeyPub,
|
2017-11-17 06:48:55 +03:00
|
|
|
HtlcBasePoint: bobKeyPub,
|
2017-07-31 00:09:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bobRoot := lnwallet.DeriveRevocationRoot(bobKeyPriv, hash, aliceKeyPub)
|
|
|
|
bobPreimageProducer := shachain.NewRevocationProducer(bobRoot)
|
2017-05-02 02:29:30 +03:00
|
|
|
bobFirstRevoke, err := bobPreimageProducer.AtIndex(0)
|
|
|
|
if err != nil {
|
2017-07-09 02:30:20 +03:00
|
|
|
return nil, nil, nil, nil, err
|
2017-05-02 02:29:30 +03:00
|
|
|
}
|
2017-07-31 00:09:10 +03:00
|
|
|
bobCommitPoint := lnwallet.ComputeCommitmentPoint(bobFirstRevoke[:])
|
2017-05-02 02:29:30 +03:00
|
|
|
|
2017-07-31 00:09:10 +03:00
|
|
|
aliceRoot := lnwallet.DeriveRevocationRoot(aliceKeyPriv, hash, bobKeyPub)
|
|
|
|
alicePreimageProducer := shachain.NewRevocationProducer(aliceRoot)
|
2017-05-02 02:29:30 +03:00
|
|
|
aliceFirstRevoke, err := alicePreimageProducer.AtIndex(0)
|
|
|
|
if err != nil {
|
2017-07-09 02:30:20 +03:00
|
|
|
return nil, nil, nil, nil, err
|
2017-05-02 02:29:30 +03:00
|
|
|
}
|
2017-07-31 00:09:10 +03:00
|
|
|
aliceCommitPoint := lnwallet.ComputeCommitmentPoint(aliceFirstRevoke[:])
|
|
|
|
|
|
|
|
aliceCommitTx, bobCommitTx, err := lnwallet.CreateCommitmentTxns(aliceAmount,
|
|
|
|
bobAmount, &aliceCfg, &bobCfg, aliceCommitPoint, bobCommitPoint,
|
2017-12-22 21:26:16 +03:00
|
|
|
*fundingTxIn)
|
2017-05-02 02:29:30 +03:00
|
|
|
if err != nil {
|
2017-07-09 02:30:20 +03:00
|
|
|
return nil, nil, nil, nil, err
|
2017-05-02 02:29:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
alicePath, err := ioutil.TempDir("", "alicedb")
|
|
|
|
dbAlice, err := channeldb.Open(alicePath)
|
|
|
|
if err != nil {
|
2017-07-09 02:30:20 +03:00
|
|
|
return nil, nil, nil, nil, err
|
2017-05-02 02:29:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bobPath, err := ioutil.TempDir("", "bobdb")
|
|
|
|
dbBob, err := channeldb.Open(bobPath)
|
|
|
|
if err != nil {
|
2017-07-09 02:30:20 +03:00
|
|
|
return nil, nil, nil, nil, err
|
2017-05-02 02:29:30 +03:00
|
|
|
}
|
|
|
|
|
2017-07-31 04:22:38 +03:00
|
|
|
estimator := &lnwallet.StaticFeeEstimator{
|
2017-11-23 10:12:53 +03:00
|
|
|
FeeRate: 24,
|
2017-07-31 04:22:38 +03:00
|
|
|
}
|
2017-11-23 10:12:53 +03:00
|
|
|
feePerWeight, err := estimator.EstimateFeePerWeight(1)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, nil, err
|
|
|
|
}
|
|
|
|
feePerKw := btcutil.Amount(feePerWeight * 1000)
|
2017-07-31 00:09:10 +03:00
|
|
|
commitFee := (feePerKw * btcutil.Amount(724)) / 1000
|
|
|
|
|
2017-07-09 02:30:20 +03:00
|
|
|
const broadcastHeight = 1
|
|
|
|
bobAddr := &net.TCPAddr{
|
|
|
|
IP: net.ParseIP("127.0.0.1"),
|
|
|
|
Port: 18555,
|
|
|
|
}
|
|
|
|
|
|
|
|
aliceAddr := &net.TCPAddr{
|
|
|
|
IP: net.ParseIP("127.0.0.1"),
|
|
|
|
Port: 18556,
|
|
|
|
}
|
|
|
|
|
2017-11-11 02:15:19 +03:00
|
|
|
aliceCommit := channeldb.ChannelCommitment{
|
|
|
|
CommitHeight: 0,
|
|
|
|
LocalBalance: lnwire.NewMSatFromSatoshis(aliceAmount - commitFee),
|
|
|
|
RemoteBalance: lnwire.NewMSatFromSatoshis(bobAmount),
|
|
|
|
CommitFee: commitFee,
|
|
|
|
FeePerKw: feePerKw,
|
|
|
|
CommitTx: aliceCommitTx,
|
|
|
|
CommitSig: bytes.Repeat([]byte{1}, 71),
|
|
|
|
}
|
|
|
|
bobCommit := channeldb.ChannelCommitment{
|
|
|
|
CommitHeight: 0,
|
|
|
|
LocalBalance: lnwire.NewMSatFromSatoshis(bobAmount),
|
|
|
|
RemoteBalance: lnwire.NewMSatFromSatoshis(aliceAmount - commitFee),
|
|
|
|
CommitFee: commitFee,
|
|
|
|
FeePerKw: feePerKw,
|
|
|
|
CommitTx: bobCommitTx,
|
|
|
|
CommitSig: bytes.Repeat([]byte{1}, 71),
|
|
|
|
}
|
|
|
|
|
2017-05-02 02:29:30 +03:00
|
|
|
aliceChannelState := &channeldb.OpenChannel{
|
2017-07-31 00:09:10 +03:00
|
|
|
LocalChanCfg: aliceCfg,
|
|
|
|
RemoteChanCfg: bobCfg,
|
|
|
|
IdentityPub: aliceKeyPub,
|
|
|
|
FundingOutpoint: *prevOut,
|
|
|
|
ChanType: channeldb.SingleFunder,
|
|
|
|
IsInitiator: true,
|
|
|
|
Capacity: channelCapacity,
|
|
|
|
RemoteCurrentRevocation: bobCommitPoint,
|
|
|
|
RevocationProducer: alicePreimageProducer,
|
|
|
|
RevocationStore: shachain.NewRevocationStore(),
|
2017-11-11 02:15:19 +03:00
|
|
|
LocalCommitment: aliceCommit,
|
|
|
|
RemoteCommitment: aliceCommit,
|
2017-07-31 00:09:10 +03:00
|
|
|
ShortChanID: chanID,
|
|
|
|
Db: dbAlice,
|
2017-05-02 02:29:30 +03:00
|
|
|
}
|
2017-07-09 02:30:20 +03:00
|
|
|
|
2017-05-02 02:29:30 +03:00
|
|
|
bobChannelState := &channeldb.OpenChannel{
|
2017-07-31 00:09:10 +03:00
|
|
|
LocalChanCfg: bobCfg,
|
|
|
|
RemoteChanCfg: aliceCfg,
|
|
|
|
IdentityPub: bobKeyPub,
|
|
|
|
FundingOutpoint: *prevOut,
|
|
|
|
ChanType: channeldb.SingleFunder,
|
|
|
|
IsInitiator: false,
|
|
|
|
Capacity: channelCapacity,
|
|
|
|
RemoteCurrentRevocation: aliceCommitPoint,
|
|
|
|
RevocationProducer: bobPreimageProducer,
|
|
|
|
RevocationStore: shachain.NewRevocationStore(),
|
2017-11-11 02:15:19 +03:00
|
|
|
LocalCommitment: bobCommit,
|
|
|
|
RemoteCommitment: bobCommit,
|
2017-07-31 00:09:10 +03:00
|
|
|
ShortChanID: chanID,
|
|
|
|
Db: dbBob,
|
2017-05-02 02:29:30 +03:00
|
|
|
}
|
|
|
|
|
2017-11-11 02:15:19 +03:00
|
|
|
if err := aliceChannelState.SyncPending(bobAddr, broadcastHeight); err != nil {
|
|
|
|
return nil, nil, nil, nil, err
|
|
|
|
}
|
|
|
|
|
2017-07-09 02:30:20 +03:00
|
|
|
if err := bobChannelState.SyncPending(aliceAddr, broadcastHeight); err != nil {
|
|
|
|
return nil, nil, nil, nil, err
|
|
|
|
}
|
|
|
|
|
2017-05-02 02:29:30 +03:00
|
|
|
cleanUpFunc := func() {
|
|
|
|
os.RemoveAll(bobPath)
|
|
|
|
os.RemoveAll(alicePath)
|
|
|
|
}
|
|
|
|
|
|
|
|
aliceSigner := &mockSigner{aliceKeyPriv}
|
|
|
|
bobSigner := &mockSigner{bobKeyPriv}
|
|
|
|
|
2018-01-17 07:18:53 +03:00
|
|
|
pCache := &mockPreimageCache{
|
|
|
|
// hash -> preimage
|
|
|
|
preimageMap: make(map[[32]byte][]byte),
|
|
|
|
}
|
|
|
|
|
|
|
|
channelAlice, err := lnwallet.NewLightningChannel(
|
|
|
|
aliceSigner, nil, pCache, aliceChannelState,
|
|
|
|
)
|
2017-05-02 02:29:30 +03:00
|
|
|
if err != nil {
|
2017-07-09 02:30:20 +03:00
|
|
|
return nil, nil, nil, nil, err
|
2017-05-02 02:29:30 +03:00
|
|
|
}
|
2018-01-17 07:18:53 +03:00
|
|
|
channelBob, err := lnwallet.NewLightningChannel(
|
|
|
|
bobSigner, nil, pCache, bobChannelState,
|
|
|
|
)
|
2017-05-02 02:29:30 +03:00
|
|
|
if err != nil {
|
2017-07-09 02:30:20 +03:00
|
|
|
return nil, nil, nil, nil, err
|
2017-05-02 02:29:30 +03:00
|
|
|
}
|
|
|
|
|
2017-07-31 00:09:10 +03:00
|
|
|
// Now that the channel are open, simulate the start of a session by
|
|
|
|
// having Alice and Bob extend their revocation windows to each other.
|
2017-07-31 04:22:38 +03:00
|
|
|
aliceNextRevoke, err := channelAlice.NextRevocationKey()
|
2017-07-31 00:09:10 +03:00
|
|
|
if err != nil {
|
2017-07-09 02:30:20 +03:00
|
|
|
return nil, nil, nil, nil, err
|
2017-07-31 00:09:10 +03:00
|
|
|
}
|
|
|
|
if err := channelBob.InitNextRevocation(aliceNextRevoke); err != nil {
|
2017-07-09 02:30:20 +03:00
|
|
|
return nil, nil, nil, nil, err
|
2017-07-31 00:09:10 +03:00
|
|
|
}
|
|
|
|
|
2017-07-31 04:22:38 +03:00
|
|
|
bobNextRevoke, err := channelBob.NextRevocationKey()
|
2017-07-31 00:09:10 +03:00
|
|
|
if err != nil {
|
2017-07-09 02:30:20 +03:00
|
|
|
return nil, nil, nil, nil, err
|
2017-07-31 00:09:10 +03:00
|
|
|
}
|
|
|
|
if err := channelAlice.InitNextRevocation(bobNextRevoke); err != nil {
|
2017-07-09 02:30:20 +03:00
|
|
|
return nil, nil, nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
restore := func() (*lnwallet.LightningChannel, *lnwallet.LightningChannel,
|
|
|
|
error) {
|
|
|
|
aliceStoredChannels, err := dbAlice.FetchOpenChannels(aliceKeyPub)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, errors.Errorf("unable to fetch alice channel: "+
|
|
|
|
"%v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var aliceStoredChannel *channeldb.OpenChannel
|
|
|
|
for _, channel := range aliceStoredChannels {
|
|
|
|
if channel.FundingOutpoint.String() == prevOut.String() {
|
|
|
|
aliceStoredChannel = channel
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if aliceStoredChannel == nil {
|
|
|
|
return nil, nil, errors.New("unable to find stored alice channel")
|
|
|
|
}
|
|
|
|
|
|
|
|
newAliceChannel, err := lnwallet.NewLightningChannel(aliceSigner,
|
2018-01-17 07:18:53 +03:00
|
|
|
nil, nil, aliceStoredChannel)
|
2017-07-09 02:30:20 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, errors.Errorf("unable to create new channel: %v",
|
|
|
|
err)
|
|
|
|
}
|
|
|
|
|
|
|
|
bobStoredChannels, err := dbBob.FetchOpenChannels(bobKeyPub)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, errors.Errorf("unable to fetch bob channel: "+
|
|
|
|
"%v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var bobStoredChannel *channeldb.OpenChannel
|
|
|
|
for _, channel := range bobStoredChannels {
|
|
|
|
if channel.FundingOutpoint.String() == prevOut.String() {
|
|
|
|
bobStoredChannel = channel
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if bobStoredChannel == nil {
|
|
|
|
return nil, nil, errors.New("unable to find stored bob channel")
|
|
|
|
}
|
|
|
|
|
|
|
|
newBobChannel, err := lnwallet.NewLightningChannel(bobSigner, nil,
|
2018-01-17 07:18:53 +03:00
|
|
|
nil, bobStoredChannel)
|
2017-07-09 02:30:20 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, errors.Errorf("unable to create new channel: %v",
|
|
|
|
err)
|
|
|
|
}
|
|
|
|
return newAliceChannel, newBobChannel, nil
|
2017-07-31 00:09:10 +03:00
|
|
|
}
|
|
|
|
|
2017-07-09 02:30:20 +03:00
|
|
|
return channelAlice, channelBob, cleanUpFunc, restore, nil
|
2017-05-02 02:29:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// getChanID retrieves the channel point from nwire message.
|
2017-07-09 01:46:27 +03:00
|
|
|
func getChanID(msg lnwire.Message) (lnwire.ChannelID, error) {
|
|
|
|
var chanID lnwire.ChannelID
|
2017-05-02 02:29:30 +03:00
|
|
|
switch msg := msg.(type) {
|
|
|
|
case *lnwire.UpdateAddHTLC:
|
2017-07-09 01:46:27 +03:00
|
|
|
chanID = msg.ChanID
|
2017-05-02 02:29:30 +03:00
|
|
|
case *lnwire.UpdateFufillHTLC:
|
2017-07-09 01:46:27 +03:00
|
|
|
chanID = msg.ChanID
|
2017-05-02 02:29:30 +03:00
|
|
|
case *lnwire.UpdateFailHTLC:
|
2017-07-09 01:46:27 +03:00
|
|
|
chanID = msg.ChanID
|
2017-05-02 02:29:30 +03:00
|
|
|
case *lnwire.RevokeAndAck:
|
2017-07-09 01:46:27 +03:00
|
|
|
chanID = msg.ChanID
|
2017-05-02 02:29:30 +03:00
|
|
|
case *lnwire.CommitSig:
|
2017-07-09 01:46:27 +03:00
|
|
|
chanID = msg.ChanID
|
|
|
|
case *lnwire.ChannelReestablish:
|
|
|
|
chanID = msg.ChanID
|
2017-11-11 02:17:33 +03:00
|
|
|
case *lnwire.FundingLocked:
|
|
|
|
chanID = msg.ChanID
|
2017-11-24 07:31:45 +03:00
|
|
|
case *lnwire.UpdateFee:
|
|
|
|
chanID = msg.ChanID
|
2017-07-09 01:46:27 +03:00
|
|
|
default:
|
2017-11-11 02:17:33 +03:00
|
|
|
return chanID, fmt.Errorf("unknown type: %T", msg)
|
2017-05-02 02:29:30 +03:00
|
|
|
}
|
|
|
|
|
2017-07-09 01:46:27 +03:00
|
|
|
return chanID, nil
|
2017-05-02 02:29:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// generatePayment generates the htlc add request by given path blob and
|
|
|
|
// invoice which should be added by destination peer.
|
2017-08-22 09:36:43 +03:00
|
|
|
func generatePayment(invoiceAmt, htlcAmt lnwire.MilliSatoshi, timelock uint32,
|
2017-06-17 01:03:30 +03:00
|
|
|
blob [lnwire.OnionPacketSize]byte) (*channeldb.Invoice, *lnwire.UpdateAddHTLC, error) {
|
2017-05-02 02:29:30 +03:00
|
|
|
|
|
|
|
var preimage [sha256.Size]byte
|
|
|
|
r, err := generateRandomBytes(sha256.Size)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
copy(preimage[:], r)
|
2017-06-17 01:03:30 +03:00
|
|
|
|
2017-05-02 02:29:30 +03:00
|
|
|
rhash := fastsha256.Sum256(preimage[:])
|
|
|
|
|
2017-06-17 01:03:30 +03:00
|
|
|
invoice := &channeldb.Invoice{
|
|
|
|
CreationDate: time.Now(),
|
|
|
|
Terms: channeldb.ContractTerm{
|
|
|
|
Value: invoiceAmt,
|
|
|
|
PaymentPreimage: preimage,
|
2017-05-02 02:29:30 +03:00
|
|
|
},
|
2017-06-17 01:03:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
htlc := &lnwire.UpdateAddHTLC{
|
|
|
|
PaymentHash: rhash,
|
|
|
|
Amount: htlcAmt,
|
|
|
|
Expiry: timelock,
|
|
|
|
OnionBlob: blob,
|
|
|
|
}
|
|
|
|
|
|
|
|
return invoice, htlc, nil
|
2017-05-02 02:29:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// generateRoute generates the path blob by given array of peers.
|
2017-06-17 01:03:30 +03:00
|
|
|
func generateRoute(hops ...ForwardingInfo) ([lnwire.OnionPacketSize]byte, error) {
|
2017-05-02 02:29:30 +03:00
|
|
|
var blob [lnwire.OnionPacketSize]byte
|
2017-06-17 01:03:30 +03:00
|
|
|
if len(hops) == 0 {
|
|
|
|
return blob, errors.New("empty path")
|
2017-05-02 02:29:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
iterator := newMockHopIterator(hops...)
|
2017-06-17 01:03:30 +03:00
|
|
|
|
|
|
|
w := bytes.NewBuffer(blob[0:0])
|
|
|
|
if err := iterator.EncodeNextHop(w); err != nil {
|
|
|
|
return blob, err
|
2017-05-02 02:29:30 +03:00
|
|
|
}
|
|
|
|
|
2017-06-17 01:03:30 +03:00
|
|
|
return blob, nil
|
2017-05-02 02:29:30 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// threeHopNetwork is used for managing the created cluster of 3 hops.
|
|
|
|
type threeHopNetwork struct {
|
|
|
|
aliceServer *mockServer
|
|
|
|
aliceChannelLink *channelLink
|
2017-11-24 07:31:45 +03:00
|
|
|
aliceBlockEpoch chan *chainntnfs.BlockEpoch
|
|
|
|
|
|
|
|
firstBobChannelLink *channelLink
|
|
|
|
bobFirstBlockEpoch chan *chainntnfs.BlockEpoch
|
2017-05-02 02:29:30 +03:00
|
|
|
|
|
|
|
bobServer *mockServer
|
|
|
|
secondBobChannelLink *channelLink
|
2017-11-24 07:31:45 +03:00
|
|
|
bobSecondBlockEpoch chan *chainntnfs.BlockEpoch
|
2017-05-02 02:29:30 +03:00
|
|
|
|
|
|
|
carolChannelLink *channelLink
|
|
|
|
carolServer *mockServer
|
2017-11-24 07:31:45 +03:00
|
|
|
carolBlockEpoch chan *chainntnfs.BlockEpoch
|
|
|
|
|
|
|
|
feeEstimator *mockFeeEstimator
|
2017-05-02 02:29:30 +03:00
|
|
|
|
2017-06-17 01:03:30 +03:00
|
|
|
globalPolicy ForwardingPolicy
|
|
|
|
}
|
|
|
|
|
2017-06-17 01:41:42 +03:00
|
|
|
// generateHops creates the per hop payload, the total amount to be sent, and
|
|
|
|
// also the time lock value needed to route a HTLC with the target amount over
|
|
|
|
// the specified path.
|
2017-08-22 09:36:43 +03:00
|
|
|
func generateHops(payAmt lnwire.MilliSatoshi, startingHeight uint32,
|
|
|
|
path ...*channelLink) (lnwire.MilliSatoshi, uint32, []ForwardingInfo) {
|
2017-06-17 01:03:30 +03:00
|
|
|
|
|
|
|
lastHop := path[len(path)-1]
|
|
|
|
|
2017-08-03 07:11:31 +03:00
|
|
|
totalTimelock := startingHeight
|
2017-06-17 02:10:03 +03:00
|
|
|
runningAmt := payAmt
|
2017-06-17 01:03:30 +03:00
|
|
|
|
|
|
|
hops := make([]ForwardingInfo, len(path))
|
|
|
|
for i := len(path) - 1; i >= 0; i-- {
|
2017-06-17 01:41:42 +03:00
|
|
|
// If this is the last hop, then the next hop is the special
|
|
|
|
// "exit node". Otherwise, we look to the "prior" hop.
|
2017-06-17 01:03:30 +03:00
|
|
|
nextHop := exitHop
|
|
|
|
if i != len(path)-1 {
|
|
|
|
nextHop = path[i+1].channel.ShortChanID()
|
|
|
|
}
|
|
|
|
|
2017-06-17 01:41:42 +03:00
|
|
|
// If this is the last, hop, then the time lock will be their
|
2017-09-12 22:31:55 +03:00
|
|
|
// specified delta policy plus our starting height.
|
|
|
|
totalTimelock += lastHop.cfg.FwrdingPolicy.TimeLockDelta
|
|
|
|
timeLock := totalTimelock
|
2017-06-17 01:03:30 +03:00
|
|
|
|
2017-06-17 01:41:42 +03:00
|
|
|
// Otherwise, the outgoing time lock should be the incoming
|
|
|
|
// timelock minus their specified delta.
|
2017-06-17 01:03:30 +03:00
|
|
|
if i != len(path)-1 {
|
|
|
|
delta := path[i].cfg.FwrdingPolicy.TimeLockDelta
|
|
|
|
timeLock = totalTimelock - delta
|
|
|
|
}
|
|
|
|
|
2017-06-17 01:41:42 +03:00
|
|
|
// Finally, we'll need to calculate the amount to forward. For
|
|
|
|
// the last hop, it's just the payment amount.
|
2017-06-17 01:03:30 +03:00
|
|
|
amount := payAmt
|
|
|
|
if i != len(path)-1 {
|
|
|
|
prevHop := hops[i+1]
|
|
|
|
prevAmount := prevHop.AmountToForward
|
|
|
|
|
|
|
|
fee := ExpectedFee(path[i].cfg.FwrdingPolicy, prevAmount)
|
|
|
|
runningAmt += fee
|
|
|
|
|
2017-10-25 04:31:39 +03:00
|
|
|
// Otherwise, for a node to forward an HTLC, then
|
|
|
|
// following inequality most hold true:
|
|
|
|
// * amt_in - fee >= amt_to_forward
|
|
|
|
amount = runningAmt - fee
|
2017-06-17 01:03:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
hops[i] = ForwardingInfo{
|
|
|
|
Network: BitcoinHop,
|
|
|
|
NextHop: nextHop,
|
|
|
|
AmountToForward: amount,
|
|
|
|
OutgoingCTLV: timeLock,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return runningAmt, totalTimelock, hops
|
2017-05-02 02:29:30 +03:00
|
|
|
}
|
|
|
|
|
2017-07-09 02:04:49 +03:00
|
|
|
type paymentResponse struct {
|
2017-07-09 02:30:20 +03:00
|
|
|
rhash chainhash.Hash
|
|
|
|
err chan error
|
2017-07-09 02:04:49 +03:00
|
|
|
}
|
|
|
|
|
2017-07-09 02:30:20 +03:00
|
|
|
func (r *paymentResponse) Wait(d time.Duration) (chainhash.Hash, error) {
|
2017-07-09 02:04:49 +03:00
|
|
|
select {
|
|
|
|
case err := <-r.err:
|
|
|
|
close(r.err)
|
2017-07-09 02:30:20 +03:00
|
|
|
return r.rhash, err
|
2017-07-09 02:04:49 +03:00
|
|
|
case <-time.After(d):
|
2017-07-09 02:30:20 +03:00
|
|
|
return r.rhash, errors.New("htlc was no settled in time")
|
2017-07-09 02:04:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-02 02:29:30 +03:00
|
|
|
// makePayment takes the destination node and amount as input, sends the
|
|
|
|
// payment and returns the error channel to wait for error to be received and
|
|
|
|
// invoice in order to check its status after the payment finished.
|
|
|
|
//
|
|
|
|
// With this function you can send payments:
|
|
|
|
// * from Alice to Bob
|
|
|
|
// * from Alice to Carol through the Bob
|
|
|
|
// * from Alice to some another peer through the Bob
|
2017-06-17 01:03:30 +03:00
|
|
|
func (n *threeHopNetwork) makePayment(sendingPeer, receivingPeer Peer,
|
|
|
|
firstHopPub [33]byte, hops []ForwardingInfo,
|
2017-08-22 09:36:43 +03:00
|
|
|
invoiceAmt, htlcAmt lnwire.MilliSatoshi,
|
2017-07-09 02:04:49 +03:00
|
|
|
timelock uint32) *paymentResponse {
|
|
|
|
|
|
|
|
paymentErr := make(chan error, 1)
|
2017-05-02 02:29:30 +03:00
|
|
|
|
2017-07-09 02:30:20 +03:00
|
|
|
var rhash chainhash.Hash
|
|
|
|
|
2017-06-17 01:03:30 +03:00
|
|
|
sender := sendingPeer.(*mockServer)
|
|
|
|
receiver := receivingPeer.(*mockServer)
|
2017-05-02 02:29:30 +03:00
|
|
|
|
|
|
|
// Generate route convert it to blob, and return next destination for
|
|
|
|
// htlc add request.
|
2017-06-17 01:03:30 +03:00
|
|
|
blob, err := generateRoute(hops...)
|
2017-05-02 02:29:30 +03:00
|
|
|
if err != nil {
|
2017-07-09 02:04:49 +03:00
|
|
|
paymentErr <- err
|
|
|
|
return &paymentResponse{
|
2017-07-09 02:30:20 +03:00
|
|
|
rhash: rhash,
|
|
|
|
err: paymentErr,
|
2017-07-09 02:04:49 +03:00
|
|
|
}
|
2017-05-02 02:29:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Generate payment: invoice and htlc.
|
2017-07-09 02:30:20 +03:00
|
|
|
invoice, htlc, err := generatePayment(invoiceAmt, htlcAmt, timelock, blob)
|
2017-05-02 02:29:30 +03:00
|
|
|
if err != nil {
|
2017-07-09 02:04:49 +03:00
|
|
|
paymentErr <- err
|
|
|
|
return &paymentResponse{
|
2017-07-09 02:30:20 +03:00
|
|
|
rhash: rhash,
|
|
|
|
err: paymentErr,
|
2017-07-09 02:04:49 +03:00
|
|
|
}
|
2017-05-02 02:29:30 +03:00
|
|
|
}
|
2017-07-09 02:30:20 +03:00
|
|
|
rhash = fastsha256.Sum256(invoice.Terms.PaymentPreimage[:])
|
2017-05-02 02:29:30 +03:00
|
|
|
|
|
|
|
// Check who is last in the route and add invoice to server registry.
|
2017-11-12 03:09:14 +03:00
|
|
|
if err := receiver.registry.AddInvoice(*invoice); err != nil {
|
2017-07-09 02:04:49 +03:00
|
|
|
paymentErr <- err
|
|
|
|
return &paymentResponse{
|
2017-07-09 02:30:20 +03:00
|
|
|
rhash: rhash,
|
|
|
|
err: paymentErr,
|
2017-07-09 02:04:49 +03:00
|
|
|
}
|
2017-05-02 02:29:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Send payment and expose err channel.
|
|
|
|
go func() {
|
2017-06-29 16:40:45 +03:00
|
|
|
_, err := sender.htlcSwitch.SendHTLC(firstHopPub, htlc,
|
|
|
|
newMockDeobfuscator())
|
2017-07-09 02:30:20 +03:00
|
|
|
paymentErr <- err
|
2017-05-02 02:29:30 +03:00
|
|
|
}()
|
|
|
|
|
2017-07-09 02:04:49 +03:00
|
|
|
return &paymentResponse{
|
2017-07-09 02:30:20 +03:00
|
|
|
rhash: rhash,
|
|
|
|
err: paymentErr,
|
2017-05-02 02:29:30 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// start starts the three hop network alice,bob,carol servers.
|
|
|
|
func (n *threeHopNetwork) start() error {
|
|
|
|
if err := n.aliceServer.Start(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := n.bobServer.Start(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := n.carolServer.Start(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// stop stops nodes and cleanup its databases.
|
|
|
|
func (n *threeHopNetwork) stop() {
|
|
|
|
done := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
n.aliceServer.Stop()
|
|
|
|
done <- struct{}{}
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
n.bobServer.Stop()
|
|
|
|
done <- struct{}{}
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
n.carolServer.Stop()
|
|
|
|
done <- struct{}{}
|
|
|
|
}()
|
|
|
|
|
|
|
|
for i := 0; i < 3; i++ {
|
|
|
|
<-done
|
|
|
|
}
|
2017-07-09 02:30:20 +03:00
|
|
|
}
|
2017-05-02 02:29:30 +03:00
|
|
|
|
2017-07-09 02:30:20 +03:00
|
|
|
type clusterChannels struct {
|
|
|
|
aliceToBob *lnwallet.LightningChannel
|
|
|
|
bobToAlice *lnwallet.LightningChannel
|
|
|
|
bobToCarol *lnwallet.LightningChannel
|
|
|
|
carolToBob *lnwallet.LightningChannel
|
|
|
|
}
|
|
|
|
|
|
|
|
// createClusterChannels creates lightning channels which are needed for
|
|
|
|
// network cluster to be initialized.
|
|
|
|
func createClusterChannels(aliceToBob, bobToCarol btcutil.Amount) (
|
|
|
|
*clusterChannels, func(), func() (*clusterChannels, error), error) {
|
|
|
|
|
|
|
|
firstChanID := lnwire.NewShortChanIDFromInt(4)
|
|
|
|
secondChanID := lnwire.NewShortChanIDFromInt(5)
|
|
|
|
|
|
|
|
// Create lightning channels between Alice<->Bob and Bob<->Carol
|
|
|
|
aliceChannel, firstBobChannel, cleanAliceBob, restoreAliceBob, err := createTestChannel(
|
|
|
|
alicePrivKey, bobPrivKey, aliceToBob, aliceToBob, firstChanID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, errors.Errorf("unable to create "+
|
|
|
|
"alice<->bob channel: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
secondBobChannel, carolChannel, cleanBobCarol, restoreBobCarol, err := createTestChannel(
|
|
|
|
bobPrivKey, carolPrivKey, bobToCarol, bobToCarol, secondChanID)
|
|
|
|
if err != nil {
|
|
|
|
cleanAliceBob()
|
|
|
|
return nil, nil, nil, errors.Errorf("unable to create "+
|
|
|
|
"bob<->carol channel: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanUp := func() {
|
|
|
|
cleanAliceBob()
|
|
|
|
cleanBobCarol()
|
|
|
|
}
|
|
|
|
|
|
|
|
restoreFromDb := func() (*clusterChannels, error) {
|
|
|
|
a2b, b2a, err := restoreAliceBob()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
b2c, c2b, err := restoreBobCarol()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &clusterChannels{
|
|
|
|
aliceToBob: a2b,
|
|
|
|
bobToAlice: b2a,
|
|
|
|
bobToCarol: b2c,
|
|
|
|
carolToBob: c2b,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return &clusterChannels{
|
|
|
|
aliceToBob: aliceChannel,
|
|
|
|
bobToAlice: firstBobChannel,
|
|
|
|
bobToCarol: secondBobChannel,
|
|
|
|
carolToBob: carolChannel,
|
|
|
|
}, cleanUp, restoreFromDb, nil
|
2017-05-02 02:29:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// newThreeHopNetwork function creates the following topology and returns the
|
|
|
|
// control object to manage this cluster:
|
|
|
|
//
|
|
|
|
// alice bob carol
|
|
|
|
// server - <-connection-> - server - - <-connection-> - - - server
|
|
|
|
// | | |
|
|
|
|
// alice htlc bob htlc carol htlc
|
|
|
|
// switch switch \ switch
|
|
|
|
// | | \ |
|
|
|
|
// | | \ |
|
|
|
|
// alice first bob second bob carol
|
|
|
|
// channel link channel link channel link channel link
|
|
|
|
//
|
2017-11-12 02:05:09 +03:00
|
|
|
func newThreeHopNetwork(t testing.TB, aliceChannel, firstBobChannel,
|
2017-07-09 02:30:20 +03:00
|
|
|
secondBobChannel, carolChannel *lnwallet.LightningChannel,
|
2017-07-09 01:52:51 +03:00
|
|
|
startingHeight uint32) *threeHopNetwork {
|
2017-05-02 02:29:30 +03:00
|
|
|
|
|
|
|
// Create three peers/servers.
|
2017-11-11 02:09:19 +03:00
|
|
|
aliceServer := newMockServer(t, "alice")
|
|
|
|
bobServer := newMockServer(t, "bob")
|
|
|
|
carolServer := newMockServer(t, "carol")
|
2017-05-02 02:29:30 +03:00
|
|
|
|
2017-11-24 07:31:45 +03:00
|
|
|
// Create mock decoder instead of sphinx one in order to mock the route
|
|
|
|
// which htlc should follow.
|
2017-05-02 02:29:30 +03:00
|
|
|
decoder := &mockIteratorDecoder{}
|
|
|
|
|
2017-11-24 07:31:45 +03:00
|
|
|
feeEstimator := &mockFeeEstimator{
|
|
|
|
byteFeeIn: make(chan btcutil.Amount),
|
|
|
|
weightFeeIn: make(chan btcutil.Amount),
|
|
|
|
quit: make(chan struct{}),
|
2017-08-03 07:11:31 +03:00
|
|
|
}
|
2017-11-24 07:31:45 +03:00
|
|
|
|
2018-01-17 07:18:53 +03:00
|
|
|
pCache := &mockPreimageCache{
|
|
|
|
// hash -> preimage
|
|
|
|
preimageMap: make(map[[32]byte][]byte),
|
|
|
|
}
|
|
|
|
|
2017-06-17 01:03:30 +03:00
|
|
|
globalPolicy := ForwardingPolicy{
|
2017-08-22 09:36:43 +03:00
|
|
|
MinHTLC: lnwire.NewMSatFromSatoshis(5),
|
|
|
|
BaseFee: lnwire.NewMSatFromSatoshis(1),
|
2017-08-03 07:11:31 +03:00
|
|
|
TimeLockDelta: 6,
|
2017-06-17 01:03:30 +03:00
|
|
|
}
|
2017-06-29 16:40:45 +03:00
|
|
|
obfuscator := newMockObfuscator()
|
2017-11-24 07:31:45 +03:00
|
|
|
|
|
|
|
aliceEpochChan := make(chan *chainntnfs.BlockEpoch)
|
|
|
|
aliceEpoch := &chainntnfs.BlockEpochEvent{
|
|
|
|
Epochs: aliceEpochChan,
|
|
|
|
Cancel: func() {
|
|
|
|
},
|
|
|
|
}
|
2017-05-02 02:29:30 +03:00
|
|
|
aliceChannelLink := NewChannelLink(
|
2017-06-17 01:03:30 +03:00
|
|
|
ChannelLinkConfig{
|
2017-06-29 16:40:45 +03:00
|
|
|
FwrdingPolicy: globalPolicy,
|
|
|
|
Peer: bobServer,
|
|
|
|
Switch: aliceServer.htlcSwitch,
|
|
|
|
DecodeHopIterator: decoder.DecodeHopIterator,
|
2017-10-11 05:36:52 +03:00
|
|
|
DecodeOnionObfuscator: func(io.Reader) (ErrorEncrypter,
|
2017-06-29 16:40:45 +03:00
|
|
|
lnwire.FailCode) {
|
|
|
|
return obfuscator, lnwire.CodeNone
|
|
|
|
},
|
|
|
|
GetLastChannelUpdate: mockGetChanUpdateMessage,
|
|
|
|
Registry: aliceServer.registry,
|
2017-11-24 07:31:45 +03:00
|
|
|
BlockEpochs: aliceEpoch,
|
|
|
|
FeeEstimator: feeEstimator,
|
2018-01-17 07:18:53 +03:00
|
|
|
PreimageCache: pCache,
|
|
|
|
UpdateContractSignals: func(*contractcourt.ContractSignals) error {
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
SyncStates: true,
|
2017-06-17 01:03:30 +03:00
|
|
|
},
|
|
|
|
aliceChannel,
|
2017-08-03 07:11:31 +03:00
|
|
|
startingHeight,
|
2017-06-17 01:03:30 +03:00
|
|
|
)
|
2017-05-02 02:29:30 +03:00
|
|
|
if err := aliceServer.htlcSwitch.addLink(aliceChannelLink); err != nil {
|
|
|
|
t.Fatalf("unable to add alice channel link: %v", err)
|
|
|
|
}
|
2018-01-17 07:18:53 +03:00
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
<-aliceChannelLink.(*channelLink).htlcUpdates
|
|
|
|
}
|
|
|
|
}()
|
2017-05-02 02:29:30 +03:00
|
|
|
|
2017-11-24 07:31:45 +03:00
|
|
|
bobFirstEpochChan := make(chan *chainntnfs.BlockEpoch)
|
|
|
|
bobFirstEpoch := &chainntnfs.BlockEpochEvent{
|
|
|
|
Epochs: bobFirstEpochChan,
|
|
|
|
Cancel: func() {
|
|
|
|
},
|
|
|
|
}
|
2017-05-02 02:29:30 +03:00
|
|
|
firstBobChannelLink := NewChannelLink(
|
2017-06-17 01:03:30 +03:00
|
|
|
ChannelLinkConfig{
|
2017-06-29 16:40:45 +03:00
|
|
|
FwrdingPolicy: globalPolicy,
|
|
|
|
Peer: aliceServer,
|
|
|
|
Switch: bobServer.htlcSwitch,
|
|
|
|
DecodeHopIterator: decoder.DecodeHopIterator,
|
2017-10-11 05:36:52 +03:00
|
|
|
DecodeOnionObfuscator: func(io.Reader) (ErrorEncrypter,
|
2017-06-29 16:40:45 +03:00
|
|
|
lnwire.FailCode) {
|
|
|
|
return obfuscator, lnwire.CodeNone
|
|
|
|
},
|
|
|
|
GetLastChannelUpdate: mockGetChanUpdateMessage,
|
|
|
|
Registry: bobServer.registry,
|
2017-11-24 07:31:45 +03:00
|
|
|
BlockEpochs: bobFirstEpoch,
|
|
|
|
FeeEstimator: feeEstimator,
|
2018-01-17 07:18:53 +03:00
|
|
|
PreimageCache: pCache,
|
|
|
|
UpdateContractSignals: func(*contractcourt.ContractSignals) error {
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
SyncStates: true,
|
2017-06-17 01:03:30 +03:00
|
|
|
},
|
|
|
|
firstBobChannel,
|
2017-08-03 07:11:31 +03:00
|
|
|
startingHeight,
|
2017-06-17 01:03:30 +03:00
|
|
|
)
|
2017-05-02 02:29:30 +03:00
|
|
|
if err := bobServer.htlcSwitch.addLink(firstBobChannelLink); err != nil {
|
|
|
|
t.Fatalf("unable to add first bob channel link: %v", err)
|
|
|
|
}
|
2018-01-17 07:18:53 +03:00
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
<-firstBobChannelLink.(*channelLink).htlcUpdates
|
|
|
|
}
|
|
|
|
}()
|
2017-05-02 02:29:30 +03:00
|
|
|
|
2017-11-24 07:31:45 +03:00
|
|
|
bobSecondEpochChan := make(chan *chainntnfs.BlockEpoch)
|
|
|
|
bobSecondEpoch := &chainntnfs.BlockEpochEvent{
|
|
|
|
Epochs: bobSecondEpochChan,
|
|
|
|
Cancel: func() {
|
|
|
|
},
|
|
|
|
}
|
2017-05-02 02:29:30 +03:00
|
|
|
secondBobChannelLink := NewChannelLink(
|
2017-06-17 01:03:30 +03:00
|
|
|
ChannelLinkConfig{
|
2017-06-29 16:40:45 +03:00
|
|
|
FwrdingPolicy: globalPolicy,
|
|
|
|
Peer: carolServer,
|
|
|
|
Switch: bobServer.htlcSwitch,
|
|
|
|
DecodeHopIterator: decoder.DecodeHopIterator,
|
2017-10-11 05:36:52 +03:00
|
|
|
DecodeOnionObfuscator: func(io.Reader) (ErrorEncrypter,
|
2017-06-29 16:40:45 +03:00
|
|
|
lnwire.FailCode) {
|
|
|
|
return obfuscator, lnwire.CodeNone
|
|
|
|
},
|
|
|
|
GetLastChannelUpdate: mockGetChanUpdateMessage,
|
|
|
|
Registry: bobServer.registry,
|
2017-11-24 07:31:45 +03:00
|
|
|
BlockEpochs: bobSecondEpoch,
|
|
|
|
FeeEstimator: feeEstimator,
|
2018-01-17 07:18:53 +03:00
|
|
|
PreimageCache: pCache,
|
|
|
|
UpdateContractSignals: func(*contractcourt.ContractSignals) error {
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
SyncStates: true,
|
2017-06-17 01:03:30 +03:00
|
|
|
},
|
|
|
|
secondBobChannel,
|
2017-08-03 07:11:31 +03:00
|
|
|
startingHeight,
|
2017-06-17 01:03:30 +03:00
|
|
|
)
|
2017-05-02 02:29:30 +03:00
|
|
|
if err := bobServer.htlcSwitch.addLink(secondBobChannelLink); err != nil {
|
|
|
|
t.Fatalf("unable to add second bob channel link: %v", err)
|
|
|
|
}
|
2018-01-17 07:18:53 +03:00
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
<-secondBobChannelLink.(*channelLink).htlcUpdates
|
|
|
|
}
|
|
|
|
}()
|
2017-05-02 02:29:30 +03:00
|
|
|
|
2017-11-24 07:31:45 +03:00
|
|
|
carolBlockEpoch := make(chan *chainntnfs.BlockEpoch)
|
|
|
|
carolEpoch := &chainntnfs.BlockEpochEvent{
|
|
|
|
Epochs: bobSecondEpochChan,
|
|
|
|
Cancel: func() {
|
|
|
|
},
|
|
|
|
}
|
2017-05-02 02:29:30 +03:00
|
|
|
carolChannelLink := NewChannelLink(
|
2017-06-17 01:03:30 +03:00
|
|
|
ChannelLinkConfig{
|
2017-06-29 16:40:45 +03:00
|
|
|
FwrdingPolicy: globalPolicy,
|
|
|
|
Peer: bobServer,
|
|
|
|
Switch: carolServer.htlcSwitch,
|
|
|
|
DecodeHopIterator: decoder.DecodeHopIterator,
|
2017-10-11 05:36:52 +03:00
|
|
|
DecodeOnionObfuscator: func(io.Reader) (ErrorEncrypter,
|
2017-06-29 16:40:45 +03:00
|
|
|
lnwire.FailCode) {
|
|
|
|
return obfuscator, lnwire.CodeNone
|
|
|
|
},
|
|
|
|
GetLastChannelUpdate: mockGetChanUpdateMessage,
|
|
|
|
Registry: carolServer.registry,
|
2017-11-24 07:31:45 +03:00
|
|
|
BlockEpochs: carolEpoch,
|
|
|
|
FeeEstimator: feeEstimator,
|
2018-01-17 07:18:53 +03:00
|
|
|
PreimageCache: pCache,
|
|
|
|
UpdateContractSignals: func(*contractcourt.ContractSignals) error {
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
SyncStates: true,
|
2017-06-17 01:03:30 +03:00
|
|
|
},
|
|
|
|
carolChannel,
|
2017-08-03 07:11:31 +03:00
|
|
|
startingHeight,
|
2017-06-17 01:03:30 +03:00
|
|
|
)
|
2017-05-02 02:29:30 +03:00
|
|
|
if err := carolServer.htlcSwitch.addLink(carolChannelLink); err != nil {
|
|
|
|
t.Fatalf("unable to add carol channel link: %v", err)
|
|
|
|
}
|
2018-01-17 07:18:53 +03:00
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
<-carolChannelLink.(*channelLink).htlcUpdates
|
|
|
|
}
|
|
|
|
}()
|
2017-05-02 02:29:30 +03:00
|
|
|
|
|
|
|
return &threeHopNetwork{
|
2017-11-24 07:31:45 +03:00
|
|
|
aliceServer: aliceServer,
|
|
|
|
aliceChannelLink: aliceChannelLink.(*channelLink),
|
|
|
|
aliceBlockEpoch: aliceEpochChan,
|
|
|
|
|
|
|
|
firstBobChannelLink: firstBobChannelLink.(*channelLink),
|
|
|
|
bobFirstBlockEpoch: bobFirstEpochChan,
|
|
|
|
|
2017-05-02 02:29:30 +03:00
|
|
|
bobServer: bobServer,
|
|
|
|
secondBobChannelLink: secondBobChannelLink.(*channelLink),
|
2017-11-24 07:31:45 +03:00
|
|
|
bobSecondBlockEpoch: bobSecondEpochChan,
|
|
|
|
|
|
|
|
carolChannelLink: carolChannelLink.(*channelLink),
|
|
|
|
carolServer: carolServer,
|
|
|
|
carolBlockEpoch: carolBlockEpoch,
|
2017-05-02 02:29:30 +03:00
|
|
|
|
2017-11-24 07:31:45 +03:00
|
|
|
feeEstimator: feeEstimator,
|
2017-06-17 01:03:30 +03:00
|
|
|
globalPolicy: globalPolicy,
|
2017-05-02 02:29:30 +03:00
|
|
|
}
|
|
|
|
}
|