2017-08-11 07:59:06 +03:00
|
|
|
// +build !rpctest
|
|
|
|
|
2019-01-24 16:28:25 +03:00
|
|
|
package lnd
|
2017-06-08 20:48:07 +03:00
|
|
|
|
|
|
|
import (
|
2019-05-31 00:37:30 +03:00
|
|
|
"bytes"
|
2018-07-06 05:32:22 +03:00
|
|
|
"errors"
|
2017-06-08 20:48:07 +03:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2018-01-31 07:30:00 +03:00
|
|
|
"math/big"
|
2017-06-08 20:48:07 +03:00
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2018-07-26 20:23:18 +03:00
|
|
|
"runtime"
|
2019-01-12 04:02:11 +03:00
|
|
|
"strings"
|
2017-06-08 20:48:07 +03:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2018-07-06 05:32:22 +03:00
|
|
|
"github.com/btcsuite/btcd/btcec"
|
|
|
|
"github.com/btcsuite/btcd/chaincfg"
|
|
|
|
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
|
|
|
"github.com/btcsuite/btcd/wire"
|
|
|
|
"github.com/btcsuite/btcutil"
|
2018-09-20 13:23:01 +03:00
|
|
|
|
2017-06-08 20:48:07 +03:00
|
|
|
"github.com/lightningnetwork/lnd/chainntnfs"
|
2019-08-08 05:18:23 +03:00
|
|
|
"github.com/lightningnetwork/lnd/chanacceptor"
|
2017-06-08 20:48:07 +03:00
|
|
|
"github.com/lightningnetwork/lnd/channeldb"
|
2019-04-17 23:26:11 +03:00
|
|
|
"github.com/lightningnetwork/lnd/discovery"
|
2018-04-05 20:53:06 +03:00
|
|
|
"github.com/lightningnetwork/lnd/htlcswitch"
|
2019-01-16 17:47:43 +03:00
|
|
|
"github.com/lightningnetwork/lnd/input"
|
2018-02-18 02:29:01 +03:00
|
|
|
"github.com/lightningnetwork/lnd/keychain"
|
2018-07-06 05:32:22 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnpeer"
|
2017-06-08 20:48:07 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnrpc"
|
|
|
|
"github.com/lightningnetwork/lnd/lnwallet"
|
2019-10-31 05:43:05 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
|
2017-06-08 20:48:07 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
|
|
|
)
|
|
|
|
|
2017-10-25 23:17:22 +03:00
|
|
|
const (
|
|
|
|
// testPollNumTries is the number of times we attempt to query
|
|
|
|
// for a certain expected database state before we give up and
|
|
|
|
// consider the test failed. Since it sometimes can take a
|
|
|
|
// while to update the database, we poll a certain amount of
|
|
|
|
// times, until it gets into the state we expect, or we are out
|
|
|
|
// of tries.
|
|
|
|
testPollNumTries = 10
|
|
|
|
|
|
|
|
// testPollSleepMs is the number of milliseconds to sleep between
|
|
|
|
// each attempt to access the database to check its state.
|
|
|
|
testPollSleepMs = 500
|
|
|
|
)
|
|
|
|
|
2017-06-08 20:48:07 +03:00
|
|
|
var (
|
|
|
|
// Use hard-coded keys for Alice and Bob, the two FundingManagers that
|
|
|
|
// we will test the interaction between.
|
|
|
|
alicePrivKeyBytes = [32]byte{
|
|
|
|
0xb7, 0x94, 0x38, 0x5f, 0x2d, 0x1e, 0xf7, 0xab,
|
|
|
|
0x4d, 0x92, 0x73, 0xd1, 0x90, 0x63, 0x81, 0xb4,
|
|
|
|
0x4f, 0x2f, 0x6f, 0x25, 0x88, 0xa3, 0xef, 0xb9,
|
|
|
|
0x6a, 0x49, 0x18, 0x83, 0x31, 0x98, 0x47, 0x53,
|
|
|
|
}
|
|
|
|
|
|
|
|
alicePrivKey, alicePubKey = btcec.PrivKeyFromBytes(btcec.S256(),
|
|
|
|
alicePrivKeyBytes[:])
|
|
|
|
|
|
|
|
aliceTCPAddr, _ = net.ResolveTCPAddr("tcp", "10.0.0.2:9001")
|
|
|
|
|
|
|
|
aliceAddr = &lnwire.NetAddress{
|
|
|
|
IdentityKey: alicePubKey,
|
|
|
|
Address: aliceTCPAddr,
|
|
|
|
}
|
|
|
|
|
|
|
|
bobPrivKeyBytes = [32]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,
|
|
|
|
}
|
|
|
|
|
|
|
|
bobPrivKey, bobPubKey = btcec.PrivKeyFromBytes(btcec.S256(),
|
|
|
|
bobPrivKeyBytes[:])
|
|
|
|
|
|
|
|
bobTCPAddr, _ = net.ResolveTCPAddr("tcp", "10.0.0.2:9000")
|
|
|
|
|
|
|
|
bobAddr = &lnwire.NetAddress{
|
|
|
|
IdentityKey: bobPubKey,
|
|
|
|
Address: bobTCPAddr,
|
|
|
|
}
|
2018-01-31 07:30:00 +03:00
|
|
|
|
|
|
|
testSig = &btcec.Signature{
|
|
|
|
R: new(big.Int),
|
|
|
|
S: new(big.Int),
|
|
|
|
}
|
|
|
|
_, _ = testSig.R.SetString("63724406601629180062774974542967536251589935445068131219452686511677818569431", 10)
|
|
|
|
_, _ = testSig.S.SetString("18801056069249825825291287104931333862866033135609736119018462340006816851118", 10)
|
2017-06-08 20:48:07 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type mockNotifier struct {
|
2017-11-14 04:32:27 +03:00
|
|
|
oneConfChannel chan *chainntnfs.TxConfirmation
|
|
|
|
sixConfChannel chan *chainntnfs.TxConfirmation
|
|
|
|
epochChan chan *chainntnfs.BlockEpoch
|
2017-06-08 20:48:07 +03:00
|
|
|
}
|
|
|
|
|
2018-05-31 08:16:34 +03:00
|
|
|
func (m *mockNotifier) RegisterConfirmationsNtfn(txid *chainhash.Hash,
|
|
|
|
_ []byte, numConfs, heightHint uint32) (*chainntnfs.ConfirmationEvent, error) {
|
|
|
|
|
2017-11-14 04:32:27 +03:00
|
|
|
if numConfs == 6 {
|
|
|
|
return &chainntnfs.ConfirmationEvent{
|
|
|
|
Confirmed: m.sixConfChannel,
|
|
|
|
}, nil
|
|
|
|
}
|
2017-06-08 20:48:07 +03:00
|
|
|
return &chainntnfs.ConfirmationEvent{
|
2017-11-14 04:32:27 +03:00
|
|
|
Confirmed: m.oneConfChannel,
|
2017-06-08 20:48:07 +03:00
|
|
|
}, nil
|
|
|
|
}
|
2018-03-13 05:11:40 +03:00
|
|
|
|
2018-08-09 10:05:27 +03:00
|
|
|
func (m *mockNotifier) RegisterBlockEpochNtfn(
|
|
|
|
bestBlock *chainntnfs.BlockEpoch) (*chainntnfs.BlockEpochEvent, error) {
|
2017-06-08 20:48:07 +03:00
|
|
|
return &chainntnfs.BlockEpochEvent{
|
|
|
|
Epochs: m.epochChan,
|
|
|
|
Cancel: func() {},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockNotifier) Start() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockNotifier) Stop() error {
|
|
|
|
return nil
|
|
|
|
}
|
2018-03-13 05:11:40 +03:00
|
|
|
|
2018-07-18 05:23:47 +03:00
|
|
|
func (m *mockNotifier) RegisterSpendNtfn(outpoint *wire.OutPoint, _ []byte,
|
2018-07-17 10:13:06 +03:00
|
|
|
heightHint uint32) (*chainntnfs.SpendEvent, error) {
|
2017-06-08 20:48:07 +03:00
|
|
|
return &chainntnfs.SpendEvent{
|
|
|
|
Spend: make(chan *chainntnfs.SpendDetail),
|
|
|
|
Cancel: func() {},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type testNode struct {
|
2017-09-01 12:42:40 +03:00
|
|
|
privKey *btcec.PrivateKey
|
2018-07-06 05:32:22 +03:00
|
|
|
addr *lnwire.NetAddress
|
2017-09-01 12:42:40 +03:00
|
|
|
msgChan chan lnwire.Message
|
|
|
|
announceChan chan lnwire.Message
|
|
|
|
publTxChan chan *wire.MsgTx
|
|
|
|
fundingMgr *fundingManager
|
2018-07-06 05:32:22 +03:00
|
|
|
newChannels chan *newChannelMsg
|
2017-09-01 12:42:40 +03:00
|
|
|
mockNotifier *mockNotifier
|
|
|
|
testDir string
|
|
|
|
shutdownChannel chan struct{}
|
2019-12-03 12:38:29 +03:00
|
|
|
remoteFeatures []lnwire.FeatureBit
|
2018-07-06 05:32:22 +03:00
|
|
|
|
|
|
|
remotePeer *testNode
|
|
|
|
sendMessage func(lnwire.Message) error
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ lnpeer.Peer = (*testNode)(nil)
|
|
|
|
|
|
|
|
func (n *testNode) IdentityKey() *btcec.PublicKey {
|
|
|
|
return n.addr.IdentityKey
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *testNode) Address() net.Addr {
|
|
|
|
return n.addr.Address
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *testNode) PubKey() [33]byte {
|
|
|
|
return newSerializedKey(n.addr.IdentityKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *testNode) SendMessage(_ bool, msg ...lnwire.Message) error {
|
|
|
|
return n.sendMessage(msg[0])
|
|
|
|
}
|
|
|
|
|
2019-03-06 04:08:22 +03:00
|
|
|
func (n *testNode) SendMessageLazy(sync bool, msgs ...lnwire.Message) error {
|
|
|
|
return n.SendMessage(sync, msgs...)
|
|
|
|
}
|
|
|
|
|
2018-07-06 05:32:22 +03:00
|
|
|
func (n *testNode) WipeChannel(_ *wire.OutPoint) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-08-26 03:11:40 +03:00
|
|
|
func (n *testNode) QuitSignal() <-chan struct{} {
|
|
|
|
return n.shutdownChannel
|
|
|
|
}
|
|
|
|
|
2019-11-08 16:31:47 +03:00
|
|
|
func (n *testNode) LocalFeatures() *lnwire.FeatureVector {
|
2019-09-11 15:41:08 +03:00
|
|
|
return lnwire.NewFeatureVector(nil, nil)
|
|
|
|
}
|
|
|
|
|
2019-11-08 16:31:47 +03:00
|
|
|
func (n *testNode) RemoteFeatures() *lnwire.FeatureVector {
|
2019-12-03 12:38:29 +03:00
|
|
|
return lnwire.NewFeatureVector(
|
|
|
|
lnwire.NewRawFeatureVector(n.remoteFeatures...), nil,
|
|
|
|
)
|
2019-09-11 15:41:08 +03:00
|
|
|
}
|
|
|
|
|
2018-09-26 12:12:57 +03:00
|
|
|
func (n *testNode) AddNewChannel(channel *channeldb.OpenChannel,
|
2018-07-06 05:32:22 +03:00
|
|
|
quit <-chan struct{}) error {
|
|
|
|
|
2018-09-26 12:12:57 +03:00
|
|
|
errChan := make(chan error)
|
2018-07-06 05:32:22 +03:00
|
|
|
msg := &newChannelMsg{
|
|
|
|
channel: channel,
|
2018-09-26 12:12:57 +03:00
|
|
|
err: errChan,
|
2018-07-06 05:32:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case n.newChannels <- msg:
|
|
|
|
case <-quit:
|
|
|
|
return ErrFundingManagerShuttingDown
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
2018-09-26 12:12:57 +03:00
|
|
|
case err := <-errChan:
|
|
|
|
return err
|
2018-07-06 05:32:22 +03:00
|
|
|
case <-quit:
|
|
|
|
return ErrFundingManagerShuttingDown
|
|
|
|
}
|
2017-06-08 20:48:07 +03:00
|
|
|
}
|
|
|
|
|
2017-09-01 12:42:40 +03:00
|
|
|
func createTestWallet(cdb *channeldb.DB, netParams *chaincfg.Params,
|
2017-06-08 20:48:07 +03:00
|
|
|
notifier chainntnfs.ChainNotifier, wc lnwallet.WalletController,
|
2019-01-16 17:47:43 +03:00
|
|
|
signer input.Signer, keyRing keychain.SecretKeyRing,
|
2018-02-18 02:29:01 +03:00
|
|
|
bio lnwallet.BlockChainIO,
|
2019-10-31 05:43:05 +03:00
|
|
|
estimator chainfee.Estimator) (*lnwallet.LightningWallet, error) {
|
2017-06-08 20:48:07 +03:00
|
|
|
|
|
|
|
wallet, err := lnwallet.NewLightningWallet(lnwallet.Config{
|
2017-11-29 16:39:28 +03:00
|
|
|
Database: cdb,
|
|
|
|
Notifier: notifier,
|
2018-02-18 02:29:01 +03:00
|
|
|
SecretKeyRing: keyRing,
|
2017-11-29 16:39:28 +03:00
|
|
|
WalletController: wc,
|
|
|
|
Signer: signer,
|
|
|
|
ChainIO: bio,
|
|
|
|
FeeEstimator: estimator,
|
|
|
|
NetParams: *netParams,
|
2018-03-14 12:43:13 +03:00
|
|
|
DefaultConstraints: defaultBtcChannelConstraints,
|
2017-06-08 20:48:07 +03:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := wallet.Startup(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return wallet, nil
|
|
|
|
}
|
|
|
|
|
2017-09-25 01:03:07 +03:00
|
|
|
func createTestFundingManager(t *testing.T, privKey *btcec.PrivateKey,
|
2019-07-11 14:14:35 +03:00
|
|
|
addr *lnwire.NetAddress, tempTestDir string,
|
|
|
|
options ...cfgOption) (*testNode, error) {
|
2017-09-25 01:03:07 +03:00
|
|
|
|
|
|
|
netParams := activeNetParams.Params
|
2019-10-31 05:43:05 +03:00
|
|
|
estimator := chainfee.NewStaticEstimator(62500, 0)
|
2017-09-25 01:03:07 +03:00
|
|
|
|
|
|
|
chainNotifier := &mockNotifier{
|
2017-11-14 04:32:27 +03:00
|
|
|
oneConfChannel: make(chan *chainntnfs.TxConfirmation, 1),
|
|
|
|
sixConfChannel: make(chan *chainntnfs.TxConfirmation, 1),
|
2018-09-16 11:40:10 +03:00
|
|
|
epochChan: make(chan *chainntnfs.BlockEpoch, 2),
|
2017-09-25 01:03:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
sentMessages := make(chan lnwire.Message)
|
|
|
|
sentAnnouncements := make(chan lnwire.Message)
|
|
|
|
publTxChan := make(chan *wire.MsgTx, 1)
|
|
|
|
shutdownChan := make(chan struct{})
|
2017-06-08 20:48:07 +03:00
|
|
|
|
|
|
|
wc := &mockWalletController{
|
2018-07-26 16:48:09 +03:00
|
|
|
rootKey: alicePrivKey,
|
2017-06-08 20:48:07 +03:00
|
|
|
}
|
|
|
|
signer := &mockSigner{
|
|
|
|
key: alicePrivKey,
|
|
|
|
}
|
2018-10-23 13:08:03 +03:00
|
|
|
bio := &mockChainIO{
|
|
|
|
bestHeight: fundingBroadcastHeight,
|
|
|
|
}
|
2017-06-08 20:48:07 +03:00
|
|
|
|
2017-09-01 12:42:40 +03:00
|
|
|
dbDir := filepath.Join(tempTestDir, "cdb")
|
|
|
|
cdb, err := channeldb.Open(dbDir)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-02-18 02:29:01 +03:00
|
|
|
keyRing := &mockSecretKeyRing{
|
|
|
|
rootKey: alicePrivKey,
|
|
|
|
}
|
|
|
|
|
|
|
|
lnw, err := createTestWallet(
|
|
|
|
cdb, netParams, chainNotifier, wc, signer, keyRing, bio,
|
|
|
|
estimator,
|
|
|
|
)
|
2017-06-08 20:48:07 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create test ln wallet: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var chanIDSeed [32]byte
|
|
|
|
|
2019-08-08 05:18:23 +03:00
|
|
|
chainedAcceptor := chanacceptor.NewChainedAcceptor()
|
|
|
|
|
2019-07-11 14:14:35 +03:00
|
|
|
fundingCfg := fundingConfig{
|
2017-09-25 01:03:07 +03:00
|
|
|
IDKey: privKey.PubKey(),
|
2017-06-08 20:48:07 +03:00
|
|
|
Wallet: lnw,
|
|
|
|
Notifier: chainNotifier,
|
|
|
|
FeeEstimator: estimator,
|
|
|
|
SignMessage: func(pubKey *btcec.PublicKey, msg []byte) (*btcec.Signature, error) {
|
2018-01-31 07:30:00 +03:00
|
|
|
return testSig, nil
|
2017-06-08 20:48:07 +03:00
|
|
|
},
|
2019-04-17 23:26:11 +03:00
|
|
|
SendAnnouncement: func(msg lnwire.Message,
|
|
|
|
_ ...discovery.OptionalMsgField) chan error {
|
|
|
|
|
2018-08-20 15:28:11 +03:00
|
|
|
errChan := make(chan error, 1)
|
2017-06-08 20:48:07 +03:00
|
|
|
select {
|
|
|
|
case sentAnnouncements <- msg:
|
2018-08-20 15:28:11 +03:00
|
|
|
errChan <- nil
|
2017-06-08 20:48:07 +03:00
|
|
|
case <-shutdownChan:
|
2018-08-20 15:28:11 +03:00
|
|
|
errChan <- fmt.Errorf("shutting down")
|
2017-06-08 20:48:07 +03:00
|
|
|
}
|
2018-08-20 15:28:11 +03:00
|
|
|
return errChan
|
2017-06-08 20:48:07 +03:00
|
|
|
},
|
|
|
|
CurrentNodeAnnouncement: func() (lnwire.NodeAnnouncement, error) {
|
|
|
|
return lnwire.NodeAnnouncement{}, nil
|
|
|
|
},
|
|
|
|
TempChanIDSeed: chanIDSeed,
|
2018-09-26 12:12:57 +03:00
|
|
|
FindChannel: func(chanID lnwire.ChannelID) (
|
|
|
|
*channeldb.OpenChannel, error) {
|
2017-09-01 12:42:40 +03:00
|
|
|
dbChannels, err := cdb.FetchAllChannels()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, channel := range dbChannels {
|
|
|
|
if chanID.IsChanPoint(&channel.FundingOutpoint) {
|
2018-09-26 12:12:57 +03:00
|
|
|
return channel, nil
|
2017-09-01 12:42:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("unable to find channel")
|
2017-06-08 20:48:07 +03:00
|
|
|
},
|
2018-04-05 20:53:06 +03:00
|
|
|
DefaultRoutingPolicy: htlcswitch.ForwardingPolicy{
|
|
|
|
MinHTLC: 5,
|
|
|
|
BaseFee: 100,
|
|
|
|
FeeRate: 1000,
|
|
|
|
TimeLockDelta: 10,
|
|
|
|
},
|
2017-08-22 09:25:41 +03:00
|
|
|
NumRequiredConfs: func(chanAmt btcutil.Amount,
|
|
|
|
pushAmt lnwire.MilliSatoshi) uint16 {
|
2017-12-19 19:29:45 +03:00
|
|
|
return 3
|
2017-06-08 20:48:07 +03:00
|
|
|
},
|
|
|
|
RequiredRemoteDelay: func(amt btcutil.Amount) uint16 {
|
|
|
|
return 4
|
|
|
|
},
|
2018-05-14 21:03:58 +03:00
|
|
|
RequiredRemoteChanReserve: func(chanAmt,
|
|
|
|
dustLimit btcutil.Amount) btcutil.Amount {
|
|
|
|
|
|
|
|
reserve := chanAmt / 100
|
|
|
|
if reserve < dustLimit {
|
|
|
|
reserve = dustLimit
|
|
|
|
}
|
|
|
|
|
|
|
|
return reserve
|
2017-11-29 15:59:27 +03:00
|
|
|
},
|
|
|
|
RequiredRemoteMaxValue: func(chanAmt btcutil.Amount) lnwire.MilliSatoshi {
|
|
|
|
reserve := lnwire.NewMSatFromSatoshis(chanAmt / 100)
|
|
|
|
return lnwire.NewMSatFromSatoshis(chanAmt) - reserve
|
|
|
|
},
|
|
|
|
RequiredRemoteMaxHTLCs: func(chanAmt btcutil.Amount) uint16 {
|
2019-01-16 17:47:43 +03:00
|
|
|
return uint16(input.MaxHTLCNumber / 2)
|
2017-11-29 15:59:27 +03:00
|
|
|
},
|
2018-07-06 05:32:22 +03:00
|
|
|
WatchNewChannel: func(*channeldb.OpenChannel, *btcec.PublicKey) error {
|
2018-01-17 07:05:07 +03:00
|
|
|
return nil
|
|
|
|
},
|
2018-05-08 07:21:01 +03:00
|
|
|
ReportShortChanID: func(wire.OutPoint) error {
|
2018-02-04 05:14:09 +03:00
|
|
|
return nil
|
|
|
|
},
|
2018-07-26 16:48:09 +03:00
|
|
|
PublishTransaction: func(txn *wire.MsgTx) error {
|
|
|
|
publTxChan <- txn
|
|
|
|
return nil
|
|
|
|
},
|
2019-01-17 01:35:30 +03:00
|
|
|
ZombieSweeperInterval: 1 * time.Hour,
|
|
|
|
ReservationTimeout: 1 * time.Nanosecond,
|
2019-07-11 14:14:35 +03:00
|
|
|
MaxPendingChannels: DefaultMaxPendingChannels,
|
2019-01-17 01:35:30 +03:00
|
|
|
NotifyOpenChannelEvent: func(wire.OutPoint) {},
|
2019-08-08 05:18:23 +03:00
|
|
|
OpenChannelPredicate: chainedAcceptor,
|
2019-07-11 14:14:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, op := range options {
|
|
|
|
op(&fundingCfg)
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := newFundingManager(fundingCfg)
|
2017-06-08 20:48:07 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed creating fundingManager: %v", err)
|
|
|
|
}
|
2017-09-25 01:03:07 +03:00
|
|
|
if err = f.Start(); err != nil {
|
|
|
|
t.Fatalf("failed starting fundingManager: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-07-06 05:32:22 +03:00
|
|
|
testNode := &testNode{
|
2017-09-25 01:03:07 +03:00
|
|
|
privKey: privKey,
|
|
|
|
msgChan: sentMessages,
|
2018-07-06 05:32:22 +03:00
|
|
|
newChannels: make(chan *newChannelMsg),
|
2017-09-25 01:03:07 +03:00
|
|
|
announceChan: sentAnnouncements,
|
|
|
|
publTxChan: publTxChan,
|
|
|
|
fundingMgr: f,
|
|
|
|
mockNotifier: chainNotifier,
|
|
|
|
testDir: tempTestDir,
|
|
|
|
shutdownChannel: shutdownChan,
|
2018-07-06 05:32:22 +03:00
|
|
|
addr: addr,
|
|
|
|
}
|
|
|
|
|
2019-05-31 00:37:30 +03:00
|
|
|
f.cfg.NotifyWhenOnline = func(peer [33]byte,
|
2018-07-06 05:32:22 +03:00
|
|
|
connectedChan chan<- lnpeer.Peer) {
|
|
|
|
|
2018-09-16 11:40:09 +03:00
|
|
|
connectedChan <- testNode.remotePeer
|
2018-07-06 05:32:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return testNode, nil
|
2017-06-08 20:48:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func recreateAliceFundingManager(t *testing.T, alice *testNode) {
|
|
|
|
// Stop the old fundingManager before creating a new one.
|
2017-09-01 12:42:40 +03:00
|
|
|
close(alice.shutdownChannel)
|
2017-06-08 20:48:07 +03:00
|
|
|
if err := alice.fundingMgr.Stop(); err != nil {
|
|
|
|
t.Fatalf("unable to stop old fundingManager: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
aliceMsgChan := make(chan lnwire.Message)
|
|
|
|
aliceAnnounceChan := make(chan lnwire.Message)
|
2017-09-01 12:42:40 +03:00
|
|
|
shutdownChan := make(chan struct{})
|
2018-03-11 04:09:03 +03:00
|
|
|
publishChan := make(chan *wire.MsgTx, 10)
|
2017-06-08 20:48:07 +03:00
|
|
|
|
|
|
|
oldCfg := alice.fundingMgr.cfg
|
|
|
|
|
2019-08-08 05:18:23 +03:00
|
|
|
chainedAcceptor := chanacceptor.NewChainedAcceptor()
|
|
|
|
|
2017-06-08 20:48:07 +03:00
|
|
|
f, err := newFundingManager(fundingConfig{
|
|
|
|
IDKey: oldCfg.IDKey,
|
|
|
|
Wallet: oldCfg.Wallet,
|
|
|
|
Notifier: oldCfg.Notifier,
|
|
|
|
FeeEstimator: oldCfg.FeeEstimator,
|
|
|
|
SignMessage: func(pubKey *btcec.PublicKey,
|
|
|
|
msg []byte) (*btcec.Signature, error) {
|
2018-01-31 07:30:00 +03:00
|
|
|
return testSig, nil
|
2017-06-08 20:48:07 +03:00
|
|
|
},
|
2019-04-17 23:26:11 +03:00
|
|
|
SendAnnouncement: func(msg lnwire.Message,
|
|
|
|
_ ...discovery.OptionalMsgField) chan error {
|
|
|
|
|
2018-08-20 15:28:11 +03:00
|
|
|
errChan := make(chan error, 1)
|
2017-09-01 12:42:40 +03:00
|
|
|
select {
|
|
|
|
case aliceAnnounceChan <- msg:
|
2018-08-20 15:28:11 +03:00
|
|
|
errChan <- nil
|
2017-09-01 12:42:40 +03:00
|
|
|
case <-shutdownChan:
|
2018-08-20 15:28:11 +03:00
|
|
|
errChan <- fmt.Errorf("shutting down")
|
2017-09-01 12:42:40 +03:00
|
|
|
}
|
2018-08-20 15:28:11 +03:00
|
|
|
return errChan
|
2017-06-08 20:48:07 +03:00
|
|
|
},
|
|
|
|
CurrentNodeAnnouncement: func() (lnwire.NodeAnnouncement, error) {
|
|
|
|
return lnwire.NodeAnnouncement{}, nil
|
|
|
|
},
|
2019-05-31 00:37:30 +03:00
|
|
|
NotifyWhenOnline: func(peer [33]byte,
|
2018-07-06 05:32:22 +03:00
|
|
|
connectedChan chan<- lnpeer.Peer) {
|
|
|
|
|
|
|
|
connectedChan <- alice.remotePeer
|
2017-09-25 01:03:07 +03:00
|
|
|
},
|
2017-06-08 20:48:07 +03:00
|
|
|
TempChanIDSeed: oldCfg.TempChanIDSeed,
|
|
|
|
FindChannel: oldCfg.FindChannel,
|
2018-08-22 10:32:43 +03:00
|
|
|
DefaultRoutingPolicy: htlcswitch.ForwardingPolicy{
|
|
|
|
MinHTLC: 5,
|
|
|
|
BaseFee: 100,
|
|
|
|
FeeRate: 1000,
|
|
|
|
TimeLockDelta: 10,
|
|
|
|
},
|
2019-01-12 20:59:46 +03:00
|
|
|
RequiredRemoteMaxValue: oldCfg.RequiredRemoteMaxValue,
|
2018-03-11 04:09:03 +03:00
|
|
|
PublishTransaction: func(txn *wire.MsgTx) error {
|
|
|
|
publishChan <- txn
|
|
|
|
return nil
|
|
|
|
},
|
2018-03-13 05:11:40 +03:00
|
|
|
ZombieSweeperInterval: oldCfg.ZombieSweeperInterval,
|
|
|
|
ReservationTimeout: oldCfg.ReservationTimeout,
|
2019-08-08 05:18:23 +03:00
|
|
|
OpenChannelPredicate: chainedAcceptor,
|
2017-06-08 20:48:07 +03:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed recreating aliceFundingManager: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
alice.fundingMgr = f
|
|
|
|
alice.msgChan = aliceMsgChan
|
|
|
|
alice.announceChan = aliceAnnounceChan
|
2018-03-11 04:09:03 +03:00
|
|
|
alice.publTxChan = publishChan
|
2017-09-01 12:42:40 +03:00
|
|
|
alice.shutdownChannel = shutdownChan
|
2017-06-08 20:48:07 +03:00
|
|
|
|
|
|
|
if err = f.Start(); err != nil {
|
|
|
|
t.Fatalf("failed starting fundingManager: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-11 14:14:35 +03:00
|
|
|
type cfgOption func(*fundingConfig)
|
|
|
|
|
|
|
|
func setupFundingManagers(t *testing.T,
|
|
|
|
options ...cfgOption) (*testNode, *testNode) {
|
2017-06-08 20:48:07 +03:00
|
|
|
|
|
|
|
aliceTestDir, err := ioutil.TempDir("", "alicelnwallet")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create temp directory: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-07-06 05:32:22 +03:00
|
|
|
alice, err := createTestFundingManager(
|
2019-07-11 14:14:35 +03:00
|
|
|
t, alicePrivKey, aliceAddr, aliceTestDir, options...,
|
2018-07-06 05:32:22 +03:00
|
|
|
)
|
2017-06-08 20:48:07 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed creating fundingManager: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
bobTestDir, err := ioutil.TempDir("", "boblnwallet")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create temp directory: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-07-11 14:14:35 +03:00
|
|
|
bob, err := createTestFundingManager(
|
|
|
|
t, bobPrivKey, bobAddr, bobTestDir, options...,
|
|
|
|
)
|
2017-06-08 20:48:07 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed creating fundingManager: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-07-06 05:32:22 +03:00
|
|
|
// With the funding manager's created, we'll now attempt to mimic a
|
|
|
|
// connection pipe between them. In order to intercept the messages
|
|
|
|
// within it, we'll redirect all messages back to the msgChan of the
|
|
|
|
// sender. Since the fundingManager now has a reference to peers itself,
|
|
|
|
// alice.sendMessage will be triggered when Bob's funding manager
|
|
|
|
// attempts to send a message to Alice and vice versa.
|
|
|
|
alice.remotePeer = bob
|
|
|
|
alice.sendMessage = func(msg lnwire.Message) error {
|
|
|
|
select {
|
|
|
|
case alice.remotePeer.msgChan <- msg:
|
|
|
|
case <-alice.shutdownChannel:
|
|
|
|
return errors.New("shutting down")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
bob.remotePeer = alice
|
|
|
|
bob.sendMessage = func(msg lnwire.Message) error {
|
|
|
|
select {
|
|
|
|
case bob.remotePeer.msgChan <- msg:
|
|
|
|
case <-bob.shutdownChannel:
|
|
|
|
return errors.New("shutting down")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-06-08 20:48:07 +03:00
|
|
|
return alice, bob
|
|
|
|
}
|
|
|
|
|
2017-09-01 12:42:40 +03:00
|
|
|
func tearDownFundingManagers(t *testing.T, a, b *testNode) {
|
|
|
|
close(a.shutdownChannel)
|
|
|
|
close(b.shutdownChannel)
|
2017-06-08 20:48:07 +03:00
|
|
|
|
|
|
|
if err := a.fundingMgr.Stop(); err != nil {
|
|
|
|
t.Fatalf("unable to stop fundingManager: %v", err)
|
|
|
|
}
|
|
|
|
if err := b.fundingMgr.Stop(); err != nil {
|
|
|
|
t.Fatalf("unable to stop fundingManager: %v", err)
|
|
|
|
}
|
|
|
|
os.RemoveAll(a.testDir)
|
|
|
|
os.RemoveAll(b.testDir)
|
|
|
|
}
|
|
|
|
|
|
|
|
// openChannel takes the funding process to the point where the funding
|
|
|
|
// transaction is confirmed on-chain. Returns the funding out point.
|
|
|
|
func openChannel(t *testing.T, alice, bob *testNode, localFundingAmt,
|
|
|
|
pushAmt btcutil.Amount, numConfs uint32,
|
2019-07-11 14:14:37 +03:00
|
|
|
updateChan chan *lnrpc.OpenStatusUpdate, announceChan bool) (
|
|
|
|
*wire.OutPoint, *wire.MsgTx) {
|
|
|
|
|
|
|
|
publ := fundChannel(
|
2019-07-11 14:14:38 +03:00
|
|
|
t, alice, bob, localFundingAmt, pushAmt, false, numConfs,
|
|
|
|
updateChan, announceChan,
|
2019-07-11 14:14:37 +03:00
|
|
|
)
|
|
|
|
fundingOutPoint := &wire.OutPoint{
|
|
|
|
Hash: publ.TxHash(),
|
|
|
|
Index: 0,
|
|
|
|
}
|
|
|
|
return fundingOutPoint, publ
|
|
|
|
}
|
|
|
|
|
|
|
|
// fundChannel takes the funding process to the point where the funding
|
|
|
|
// transaction is confirmed on-chain. Returns the funding tx.
|
|
|
|
func fundChannel(t *testing.T, alice, bob *testNode, localFundingAmt,
|
2019-07-11 14:14:38 +03:00
|
|
|
pushAmt btcutil.Amount, subtractFees bool, numConfs uint32,
|
2019-07-11 14:14:37 +03:00
|
|
|
updateChan chan *lnrpc.OpenStatusUpdate, announceChan bool) *wire.MsgTx {
|
|
|
|
|
2017-06-08 20:48:07 +03:00
|
|
|
// Create a funding request and start the workflow.
|
|
|
|
errChan := make(chan error, 1)
|
|
|
|
initReq := &openChanReq{
|
|
|
|
targetPubkey: bob.privKey.PubKey(),
|
|
|
|
chainHash: *activeNetParams.GenesisHash,
|
2019-07-11 14:14:38 +03:00
|
|
|
subtractFees: subtractFees,
|
2017-06-08 20:48:07 +03:00
|
|
|
localFundingAmt: localFundingAmt,
|
2017-08-22 09:25:41 +03:00
|
|
|
pushAmt: lnwire.NewMSatFromSatoshis(pushAmt),
|
2019-07-11 14:14:37 +03:00
|
|
|
fundingFeePerKw: 1000,
|
2017-11-14 04:32:27 +03:00
|
|
|
private: !announceChan,
|
2017-06-08 20:48:07 +03:00
|
|
|
updates: updateChan,
|
|
|
|
err: errChan,
|
|
|
|
}
|
|
|
|
|
2018-07-06 05:32:22 +03:00
|
|
|
alice.fundingMgr.initFundingWorkflow(bob, initReq)
|
2017-06-08 20:48:07 +03:00
|
|
|
|
|
|
|
// Alice should have sent the OpenChannel message to Bob.
|
|
|
|
var aliceMsg lnwire.Message
|
|
|
|
select {
|
|
|
|
case aliceMsg = <-alice.msgChan:
|
|
|
|
case err := <-initReq.err:
|
|
|
|
t.Fatalf("error init funding workflow: %v", err)
|
|
|
|
case <-time.After(time.Second * 5):
|
|
|
|
t.Fatalf("alice did not send OpenChannel message")
|
|
|
|
}
|
|
|
|
|
|
|
|
openChannelReq, ok := aliceMsg.(*lnwire.OpenChannel)
|
|
|
|
if !ok {
|
|
|
|
errorMsg, gotError := aliceMsg.(*lnwire.Error)
|
|
|
|
if gotError {
|
|
|
|
t.Fatalf("expected OpenChannel to be sent "+
|
2017-08-22 09:25:41 +03:00
|
|
|
"from bob, instead got error: %v",
|
2019-09-20 11:55:21 +03:00
|
|
|
errorMsg.Error())
|
2017-06-08 20:48:07 +03:00
|
|
|
}
|
|
|
|
t.Fatalf("expected OpenChannel to be sent from "+
|
|
|
|
"alice, instead got %T", aliceMsg)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Let Bob handle the init message.
|
2018-07-06 05:32:22 +03:00
|
|
|
bob.fundingMgr.processFundingOpen(openChannelReq, alice)
|
2017-06-08 20:48:07 +03:00
|
|
|
|
2018-03-13 05:11:40 +03:00
|
|
|
// Bob should answer with an AcceptChannel message.
|
|
|
|
acceptChannelResponse := assertFundingMsgSent(
|
|
|
|
t, bob.msgChan, "AcceptChannel",
|
|
|
|
).(*lnwire.AcceptChannel)
|
2017-06-08 20:48:07 +03:00
|
|
|
|
2018-07-26 16:48:09 +03:00
|
|
|
// They now should both have pending reservations for this channel
|
|
|
|
// active.
|
|
|
|
assertNumPendingReservations(t, alice, bobPubKey, 1)
|
|
|
|
assertNumPendingReservations(t, bob, alicePubKey, 1)
|
|
|
|
|
2017-06-08 20:48:07 +03:00
|
|
|
// Forward the response to Alice.
|
2018-07-06 05:32:22 +03:00
|
|
|
alice.fundingMgr.processFundingAccept(acceptChannelResponse, bob)
|
2017-06-08 20:48:07 +03:00
|
|
|
|
2018-03-13 05:11:40 +03:00
|
|
|
// Alice responds with a FundingCreated message.
|
|
|
|
fundingCreated := assertFundingMsgSent(
|
|
|
|
t, alice.msgChan, "FundingCreated",
|
|
|
|
).(*lnwire.FundingCreated)
|
2017-06-08 20:48:07 +03:00
|
|
|
|
|
|
|
// Give the message to Bob.
|
2018-07-06 05:32:22 +03:00
|
|
|
bob.fundingMgr.processFundingCreated(fundingCreated, alice)
|
2017-06-08 20:48:07 +03:00
|
|
|
|
|
|
|
// Finally, Bob should send the FundingSigned message.
|
2018-03-13 05:11:40 +03:00
|
|
|
fundingSigned := assertFundingMsgSent(
|
|
|
|
t, bob.msgChan, "FundingSigned",
|
|
|
|
).(*lnwire.FundingSigned)
|
2017-06-08 20:48:07 +03:00
|
|
|
|
|
|
|
// Forward the signature to Alice.
|
2018-07-06 05:32:22 +03:00
|
|
|
alice.fundingMgr.processFundingSigned(fundingSigned, bob)
|
2017-06-08 20:48:07 +03:00
|
|
|
|
|
|
|
// After Alice processes the singleFundingSignComplete message, she will
|
|
|
|
// broadcast the funding transaction to the network. We expect to get a
|
|
|
|
// channel update saying the channel is pending.
|
|
|
|
var pendingUpdate *lnrpc.OpenStatusUpdate
|
|
|
|
select {
|
|
|
|
case pendingUpdate = <-updateChan:
|
|
|
|
case <-time.After(time.Second * 5):
|
|
|
|
t.Fatalf("alice did not send OpenStatusUpdate_ChanPending")
|
|
|
|
}
|
|
|
|
|
|
|
|
_, ok = pendingUpdate.Update.(*lnrpc.OpenStatusUpdate_ChanPending)
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("OpenStatusUpdate was not OpenStatusUpdate_ChanPending")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get and return the transaction Alice published to the network.
|
|
|
|
var publ *wire.MsgTx
|
|
|
|
select {
|
|
|
|
case publ = <-alice.publTxChan:
|
|
|
|
case <-time.After(time.Second * 5):
|
|
|
|
t.Fatalf("alice did not publish funding tx")
|
|
|
|
}
|
|
|
|
|
2018-07-26 16:48:09 +03:00
|
|
|
// Finally, make sure neither have active reservation for the channel
|
|
|
|
// now pending open in the database.
|
|
|
|
assertNumPendingReservations(t, alice, bobPubKey, 0)
|
|
|
|
assertNumPendingReservations(t, bob, alicePubKey, 0)
|
|
|
|
|
2019-07-11 14:14:37 +03:00
|
|
|
return publ
|
2017-06-08 20:48:07 +03:00
|
|
|
}
|
|
|
|
|
2018-03-13 05:11:40 +03:00
|
|
|
func assertErrorNotSent(t *testing.T, msgChan chan lnwire.Message) {
|
2018-09-16 11:40:08 +03:00
|
|
|
t.Helper()
|
|
|
|
|
2018-03-13 05:11:40 +03:00
|
|
|
select {
|
|
|
|
case <-msgChan:
|
|
|
|
t.Fatalf("error sent unexpectedly")
|
2018-03-14 03:08:53 +03:00
|
|
|
case <-time.After(100 * time.Millisecond):
|
2018-03-13 05:11:40 +03:00
|
|
|
// Expected, return.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func assertErrorSent(t *testing.T, msgChan chan lnwire.Message) {
|
2018-09-16 11:40:08 +03:00
|
|
|
t.Helper()
|
|
|
|
|
2018-03-13 05:11:40 +03:00
|
|
|
var msg lnwire.Message
|
|
|
|
select {
|
|
|
|
case msg = <-msgChan:
|
|
|
|
case <-time.After(time.Second * 5):
|
|
|
|
t.Fatalf("node did not send Error message")
|
|
|
|
}
|
|
|
|
_, ok := msg.(*lnwire.Error)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("expected Error to be sent from "+
|
|
|
|
"node, instead got %T", msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func assertFundingMsgSent(t *testing.T, msgChan chan lnwire.Message,
|
|
|
|
msgType string) lnwire.Message {
|
2019-07-11 14:14:35 +03:00
|
|
|
t.Helper()
|
|
|
|
|
2018-03-13 05:11:40 +03:00
|
|
|
var msg lnwire.Message
|
|
|
|
select {
|
|
|
|
case msg = <-msgChan:
|
|
|
|
case <-time.After(time.Second * 5):
|
|
|
|
t.Fatalf("peer did not send %s message", msgType)
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
sentMsg lnwire.Message
|
2018-03-14 03:08:53 +03:00
|
|
|
ok bool
|
2018-03-13 05:11:40 +03:00
|
|
|
)
|
|
|
|
switch msgType {
|
|
|
|
case "AcceptChannel":
|
|
|
|
sentMsg, ok = msg.(*lnwire.AcceptChannel)
|
|
|
|
case "FundingCreated":
|
|
|
|
sentMsg, ok = msg.(*lnwire.FundingCreated)
|
|
|
|
case "FundingSigned":
|
|
|
|
sentMsg, ok = msg.(*lnwire.FundingSigned)
|
|
|
|
case "FundingLocked":
|
|
|
|
sentMsg, ok = msg.(*lnwire.FundingLocked)
|
2018-07-26 20:23:18 +03:00
|
|
|
case "Error":
|
|
|
|
sentMsg, ok = msg.(*lnwire.Error)
|
2018-03-13 05:11:40 +03:00
|
|
|
default:
|
|
|
|
t.Fatalf("unknown message type: %s", msgType)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
errorMsg, gotError := msg.(*lnwire.Error)
|
|
|
|
if gotError {
|
|
|
|
t.Fatalf("expected %s to be sent, instead got error: %v",
|
2019-09-20 11:55:21 +03:00
|
|
|
msgType, errorMsg.Error())
|
2018-03-13 05:11:40 +03:00
|
|
|
}
|
2018-07-26 20:23:18 +03:00
|
|
|
|
|
|
|
_, _, line, _ := runtime.Caller(1)
|
|
|
|
t.Fatalf("expected %s to be sent, instead got %T at %v",
|
|
|
|
msgType, msg, line)
|
2018-03-13 05:11:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return sentMsg
|
|
|
|
}
|
|
|
|
|
|
|
|
func assertNumPendingReservations(t *testing.T, node *testNode,
|
|
|
|
peerPubKey *btcec.PublicKey, expectedNum int) {
|
2018-09-16 11:40:08 +03:00
|
|
|
t.Helper()
|
|
|
|
|
2018-03-13 05:11:40 +03:00
|
|
|
serializedPubKey := newSerializedKey(peerPubKey)
|
|
|
|
actualNum := len(node.fundingMgr.activeReservations[serializedPubKey])
|
|
|
|
if actualNum == expectedNum {
|
|
|
|
// Success, return.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Fatalf("Expected node to have %d pending reservations, had %v",
|
|
|
|
expectedNum, actualNum)
|
|
|
|
}
|
|
|
|
|
2018-02-23 03:16:33 +03:00
|
|
|
func assertNumPendingChannelsBecomes(t *testing.T, node *testNode, expectedNum int) {
|
2018-09-16 11:40:08 +03:00
|
|
|
t.Helper()
|
|
|
|
|
2017-10-25 23:17:22 +03:00
|
|
|
var numPendingChans int
|
|
|
|
for i := 0; i < testPollNumTries; i++ {
|
|
|
|
// If this is not the first try, sleep before retrying.
|
|
|
|
if i > 0 {
|
|
|
|
time.Sleep(testPollSleepMs * time.Millisecond)
|
|
|
|
}
|
|
|
|
pendingChannels, err := node.fundingMgr.
|
|
|
|
cfg.Wallet.Cfg.Database.FetchPendingChannels()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to fetch pending channels: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
numPendingChans = len(pendingChannels)
|
|
|
|
if numPendingChans == expectedNum {
|
|
|
|
// Success, return.
|
|
|
|
return
|
|
|
|
}
|
2017-06-08 20:48:07 +03:00
|
|
|
}
|
|
|
|
|
2017-10-25 23:17:22 +03:00
|
|
|
t.Fatalf("Expected node to have %d pending channels, had %v",
|
|
|
|
expectedNum, numPendingChans)
|
|
|
|
}
|
|
|
|
|
2018-02-23 03:16:33 +03:00
|
|
|
func assertNumPendingChannelsRemains(t *testing.T, node *testNode, expectedNum int) {
|
2018-09-16 11:40:08 +03:00
|
|
|
t.Helper()
|
|
|
|
|
2018-02-23 03:16:33 +03:00
|
|
|
var numPendingChans int
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
// If this is not the first try, sleep before retrying.
|
|
|
|
if i > 0 {
|
|
|
|
time.Sleep(200 * time.Millisecond)
|
|
|
|
}
|
|
|
|
pendingChannels, err := node.fundingMgr.
|
|
|
|
cfg.Wallet.Cfg.Database.FetchPendingChannels()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to fetch pending channels: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
numPendingChans = len(pendingChannels)
|
|
|
|
if numPendingChans != expectedNum {
|
|
|
|
|
|
|
|
t.Fatalf("Expected node to have %d pending channels, had %v",
|
|
|
|
expectedNum, numPendingChans)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-25 23:17:22 +03:00
|
|
|
func assertDatabaseState(t *testing.T, node *testNode,
|
|
|
|
fundingOutPoint *wire.OutPoint, expectedState channelOpeningState) {
|
2018-09-16 11:40:08 +03:00
|
|
|
t.Helper()
|
2017-10-25 23:17:22 +03:00
|
|
|
|
|
|
|
var state channelOpeningState
|
|
|
|
var err error
|
|
|
|
for i := 0; i < testPollNumTries; i++ {
|
|
|
|
// If this is not the first try, sleep before retrying.
|
|
|
|
if i > 0 {
|
|
|
|
time.Sleep(testPollSleepMs * time.Millisecond)
|
|
|
|
}
|
|
|
|
state, _, err = node.fundingMgr.getChannelOpeningState(
|
|
|
|
fundingOutPoint)
|
|
|
|
if err != nil && err != ErrChannelNotFound {
|
|
|
|
t.Fatalf("unable to get channel state: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we found the channel, check if it had the expected state.
|
|
|
|
if err != ErrChannelNotFound && state == expectedState {
|
|
|
|
// Got expected state, return with success.
|
|
|
|
return
|
|
|
|
}
|
2017-06-08 20:48:07 +03:00
|
|
|
}
|
2017-10-25 23:17:22 +03:00
|
|
|
|
|
|
|
// 10 tries without success.
|
2017-06-08 20:48:07 +03:00
|
|
|
if err != nil {
|
2017-10-25 23:17:22 +03:00
|
|
|
t.Fatalf("error getting channelOpeningState: %v", err)
|
|
|
|
} else {
|
|
|
|
t.Fatalf("expected state to be %v, was %v", expectedState,
|
|
|
|
state)
|
2017-06-08 20:48:07 +03:00
|
|
|
}
|
2017-10-25 23:17:22 +03:00
|
|
|
}
|
2017-06-08 20:48:07 +03:00
|
|
|
|
2017-10-25 23:17:22 +03:00
|
|
|
func assertMarkedOpen(t *testing.T, alice, bob *testNode,
|
|
|
|
fundingOutPoint *wire.OutPoint) {
|
2018-09-16 11:40:08 +03:00
|
|
|
t.Helper()
|
|
|
|
|
2017-10-25 23:17:22 +03:00
|
|
|
assertDatabaseState(t, alice, fundingOutPoint, markedOpen)
|
|
|
|
assertDatabaseState(t, bob, fundingOutPoint, markedOpen)
|
2017-09-25 01:03:07 +03:00
|
|
|
}
|
2017-06-08 20:48:07 +03:00
|
|
|
|
2017-09-25 01:03:07 +03:00
|
|
|
func assertFundingLockedSent(t *testing.T, alice, bob *testNode,
|
|
|
|
fundingOutPoint *wire.OutPoint) {
|
2018-09-16 11:40:08 +03:00
|
|
|
t.Helper()
|
|
|
|
|
2017-10-25 23:17:22 +03:00
|
|
|
assertDatabaseState(t, alice, fundingOutPoint, fundingLockedSent)
|
|
|
|
assertDatabaseState(t, bob, fundingOutPoint, fundingLockedSent)
|
2017-09-25 01:03:07 +03:00
|
|
|
}
|
2017-06-08 20:48:07 +03:00
|
|
|
|
2017-11-14 04:32:27 +03:00
|
|
|
func assertAddedToRouterGraph(t *testing.T, alice, bob *testNode,
|
|
|
|
fundingOutPoint *wire.OutPoint) {
|
2018-09-16 11:40:08 +03:00
|
|
|
t.Helper()
|
|
|
|
|
2017-11-14 04:32:27 +03:00
|
|
|
assertDatabaseState(t, alice, fundingOutPoint, addedToRouterGraph)
|
|
|
|
assertDatabaseState(t, bob, fundingOutPoint, addedToRouterGraph)
|
|
|
|
}
|
|
|
|
|
2018-08-22 10:32:43 +03:00
|
|
|
// assertChannelAnnouncements checks that alice and bob both sends the expected
|
|
|
|
// announcements (ChannelAnnouncement, ChannelUpdate) after the funding tx has
|
|
|
|
// confirmed. The last arguments can be set if we expect the nodes to advertise
|
|
|
|
// custom min_htlc values as part of their ChannelUpdate. We expect Alice to
|
|
|
|
// advertise the value required by Bob and vice versa. If they are not set the
|
2018-09-06 11:48:46 +03:00
|
|
|
// advertised value will be checked against the other node's default min_htlc
|
2018-08-22 10:32:43 +03:00
|
|
|
// value.
|
|
|
|
func assertChannelAnnouncements(t *testing.T, alice, bob *testNode,
|
2019-01-12 20:59:46 +03:00
|
|
|
capacity btcutil.Amount, customMinHtlc ...lnwire.MilliSatoshi) {
|
|
|
|
t.Helper()
|
2018-08-22 10:32:43 +03:00
|
|
|
|
2017-11-14 02:59:17 +03:00
|
|
|
// After the FundingLocked message is sent, Alice and Bob will each
|
2017-11-14 04:32:27 +03:00
|
|
|
// send the following messages to their gossiper:
|
2017-06-08 20:48:07 +03:00
|
|
|
// 1) ChannelAnnouncement
|
|
|
|
// 2) ChannelUpdate
|
2017-11-14 04:32:27 +03:00
|
|
|
// The ChannelAnnouncement is kept locally, while the ChannelUpdate
|
|
|
|
// is sent directly to the other peer, so the edge policies are
|
|
|
|
// known to both peers.
|
2018-08-22 10:32:43 +03:00
|
|
|
nodes := []*testNode{alice, bob}
|
|
|
|
for j, node := range nodes {
|
2017-11-14 04:32:27 +03:00
|
|
|
announcements := make([]lnwire.Message, 2)
|
|
|
|
for i := 0; i < len(announcements); i++ {
|
|
|
|
select {
|
|
|
|
case announcements[i] = <-node.announceChan:
|
|
|
|
case <-time.After(time.Second * 5):
|
|
|
|
t.Fatalf("node did not send announcement: %v", i)
|
|
|
|
}
|
2017-11-14 02:59:17 +03:00
|
|
|
}
|
|
|
|
|
2017-11-14 04:32:27 +03:00
|
|
|
gotChannelAnnouncement := false
|
|
|
|
gotChannelUpdate := false
|
|
|
|
for _, msg := range announcements {
|
2018-08-22 10:32:43 +03:00
|
|
|
switch m := msg.(type) {
|
2017-11-14 04:32:27 +03:00
|
|
|
case *lnwire.ChannelAnnouncement:
|
|
|
|
gotChannelAnnouncement = true
|
|
|
|
case *lnwire.ChannelUpdate:
|
2018-08-22 10:32:43 +03:00
|
|
|
|
|
|
|
// The channel update sent by the node should
|
|
|
|
// advertise the MinHTLC value required by the
|
|
|
|
// _other_ node.
|
|
|
|
other := (j + 1) % 2
|
|
|
|
minHtlc := nodes[other].fundingMgr.cfg.
|
|
|
|
DefaultRoutingPolicy.MinHTLC
|
|
|
|
|
|
|
|
// We might expect a custom MinHTLC value.
|
|
|
|
if len(customMinHtlc) > 0 {
|
|
|
|
if len(customMinHtlc) != 2 {
|
|
|
|
t.Fatalf("only 0 or 2 custom " +
|
|
|
|
"min htlc values " +
|
|
|
|
"currently supported")
|
|
|
|
}
|
|
|
|
|
|
|
|
minHtlc = customMinHtlc[j]
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.HtlcMinimumMsat != minHtlc {
|
|
|
|
t.Fatalf("expected ChannelUpdate to "+
|
|
|
|
"advertise min HTLC %v, had %v",
|
|
|
|
minHtlc, m.HtlcMinimumMsat)
|
|
|
|
}
|
|
|
|
|
2019-01-12 20:59:46 +03:00
|
|
|
// The MaxHTLC value should at this point
|
|
|
|
// _always_ be the same as the
|
|
|
|
// maxValueInFlight capacity.
|
|
|
|
if m.MessageFlags != 1 {
|
|
|
|
t.Fatalf("expected message flags to "+
|
|
|
|
"be 1, was %v", m.MessageFlags)
|
|
|
|
}
|
|
|
|
|
|
|
|
maxPendingMsat := alice.fundingMgr.cfg.RequiredRemoteMaxValue(
|
|
|
|
capacity,
|
|
|
|
)
|
|
|
|
if maxPendingMsat != m.HtlcMaximumMsat {
|
|
|
|
t.Fatalf("expected ChannelUpdate to "+
|
|
|
|
"advertise max HTLC %v, had %v",
|
|
|
|
maxPendingMsat,
|
|
|
|
m.HtlcMaximumMsat)
|
|
|
|
}
|
|
|
|
|
2017-11-14 04:32:27 +03:00
|
|
|
gotChannelUpdate = true
|
|
|
|
}
|
2017-11-14 02:59:17 +03:00
|
|
|
}
|
|
|
|
|
2017-11-14 04:32:27 +03:00
|
|
|
if !gotChannelAnnouncement {
|
|
|
|
t.Fatalf("did not get ChannelAnnouncement from node %d",
|
|
|
|
j)
|
|
|
|
}
|
|
|
|
if !gotChannelUpdate {
|
|
|
|
t.Fatalf("did not get ChannelUpdate from node %d", j)
|
|
|
|
}
|
2017-11-14 02:59:17 +03:00
|
|
|
|
2017-11-14 04:32:27 +03:00
|
|
|
// Make sure no other message is sent.
|
|
|
|
select {
|
|
|
|
case <-node.announceChan:
|
|
|
|
t.Fatalf("received unexpected announcement")
|
|
|
|
case <-time.After(300 * time.Millisecond):
|
|
|
|
// Expected
|
|
|
|
}
|
2017-11-14 02:59:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-14 04:32:27 +03:00
|
|
|
func assertAnnouncementSignatures(t *testing.T, alice, bob *testNode) {
|
2018-09-16 11:40:08 +03:00
|
|
|
t.Helper()
|
|
|
|
|
2017-11-14 02:59:17 +03:00
|
|
|
// After the FundingLocked message is sent and six confirmations have
|
2017-11-14 04:32:27 +03:00
|
|
|
// been reached, the channel will be announced to the greater network
|
|
|
|
// by having the nodes exchange announcement signatures.
|
2017-11-14 02:59:17 +03:00
|
|
|
// Two distinct messages will be sent:
|
|
|
|
// 1) AnnouncementSignatures
|
|
|
|
// 2) NodeAnnouncement
|
|
|
|
// These may arrive in no particular order.
|
2017-11-14 04:32:27 +03:00
|
|
|
// Note that sending the NodeAnnouncement at this point is an
|
|
|
|
// implementation detail, and not something required by the LN spec.
|
|
|
|
for j, node := range []*testNode{alice, bob} {
|
|
|
|
announcements := make([]lnwire.Message, 2)
|
|
|
|
for i := 0; i < len(announcements); i++ {
|
|
|
|
select {
|
|
|
|
case announcements[i] = <-node.announceChan:
|
|
|
|
case <-time.After(time.Second * 5):
|
|
|
|
t.Fatalf("node did not send announcement %v", i)
|
|
|
|
}
|
2017-06-08 20:48:07 +03:00
|
|
|
}
|
|
|
|
|
2017-11-14 04:32:27 +03:00
|
|
|
gotAnnounceSignatures := false
|
|
|
|
gotNodeAnnouncement := false
|
|
|
|
for _, msg := range announcements {
|
|
|
|
switch msg.(type) {
|
|
|
|
case *lnwire.AnnounceSignatures:
|
|
|
|
gotAnnounceSignatures = true
|
|
|
|
case *lnwire.NodeAnnouncement:
|
|
|
|
gotNodeAnnouncement = true
|
|
|
|
}
|
2017-06-08 20:48:07 +03:00
|
|
|
}
|
|
|
|
|
2017-11-14 04:32:27 +03:00
|
|
|
if !gotAnnounceSignatures {
|
|
|
|
t.Fatalf("did not get AnnounceSignatures from node %d",
|
|
|
|
j)
|
2017-06-08 20:48:07 +03:00
|
|
|
}
|
2017-11-14 04:32:27 +03:00
|
|
|
if !gotNodeAnnouncement {
|
|
|
|
t.Fatalf("did not get NodeAnnouncement from node %d", j)
|
2017-06-08 20:48:07 +03:00
|
|
|
}
|
|
|
|
}
|
2017-09-25 01:03:07 +03:00
|
|
|
}
|
2017-06-08 20:48:07 +03:00
|
|
|
|
2017-09-25 01:03:07 +03:00
|
|
|
func waitForOpenUpdate(t *testing.T, updateChan chan *lnrpc.OpenStatusUpdate) {
|
2017-06-08 20:48:07 +03:00
|
|
|
var openUpdate *lnrpc.OpenStatusUpdate
|
|
|
|
select {
|
|
|
|
case openUpdate = <-updateChan:
|
|
|
|
case <-time.After(time.Second * 5):
|
|
|
|
t.Fatalf("alice did not send OpenStatusUpdate")
|
|
|
|
}
|
|
|
|
|
2017-09-25 01:03:07 +03:00
|
|
|
_, ok := openUpdate.Update.(*lnrpc.OpenStatusUpdate_ChanOpen)
|
2017-06-08 20:48:07 +03:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("OpenStatusUpdate was not OpenStatusUpdate_ChanOpen")
|
|
|
|
}
|
2017-09-25 01:03:07 +03:00
|
|
|
}
|
2017-06-08 20:48:07 +03:00
|
|
|
|
2017-09-25 01:03:07 +03:00
|
|
|
func assertNoChannelState(t *testing.T, alice, bob *testNode,
|
|
|
|
fundingOutPoint *wire.OutPoint) {
|
2018-09-16 11:40:08 +03:00
|
|
|
t.Helper()
|
2017-06-08 20:48:07 +03:00
|
|
|
|
2017-10-25 23:17:22 +03:00
|
|
|
assertErrChannelNotFound(t, alice, fundingOutPoint)
|
|
|
|
assertErrChannelNotFound(t, bob, fundingOutPoint)
|
|
|
|
}
|
|
|
|
|
|
|
|
func assertErrChannelNotFound(t *testing.T, node *testNode,
|
|
|
|
fundingOutPoint *wire.OutPoint) {
|
2018-09-16 11:40:08 +03:00
|
|
|
t.Helper()
|
2017-10-25 23:17:22 +03:00
|
|
|
|
|
|
|
var state channelOpeningState
|
|
|
|
var err error
|
|
|
|
for i := 0; i < testPollNumTries; i++ {
|
|
|
|
// If this is not the first try, sleep before retrying.
|
|
|
|
if i > 0 {
|
|
|
|
time.Sleep(testPollSleepMs * time.Millisecond)
|
|
|
|
}
|
|
|
|
state, _, err = node.fundingMgr.getChannelOpeningState(
|
|
|
|
fundingOutPoint)
|
|
|
|
if err == ErrChannelNotFound {
|
|
|
|
// Got expected state, return with success.
|
|
|
|
return
|
|
|
|
} else if err != nil {
|
|
|
|
t.Fatalf("unable to get channel state: %v", err)
|
|
|
|
}
|
2017-06-08 20:48:07 +03:00
|
|
|
}
|
|
|
|
|
2017-10-25 23:17:22 +03:00
|
|
|
// 10 tries without success.
|
|
|
|
t.Fatalf("expected to not find state, found state %v", state)
|
2017-09-25 01:03:07 +03:00
|
|
|
}
|
2017-09-01 12:42:40 +03:00
|
|
|
|
2017-09-25 01:03:07 +03:00
|
|
|
func assertHandleFundingLocked(t *testing.T, alice, bob *testNode) {
|
2018-09-16 11:40:08 +03:00
|
|
|
t.Helper()
|
|
|
|
|
2018-04-19 12:01:56 +03:00
|
|
|
// They should both send the new channel state to their peer.
|
2017-09-01 12:42:40 +03:00
|
|
|
select {
|
2018-07-06 05:32:22 +03:00
|
|
|
case c := <-alice.newChannels:
|
2018-09-26 12:12:57 +03:00
|
|
|
close(c.err)
|
2017-12-22 21:29:12 +03:00
|
|
|
case <-time.After(time.Second * 15):
|
2017-09-01 12:42:40 +03:00
|
|
|
t.Fatalf("alice did not send new channel to peer")
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
2018-07-06 05:32:22 +03:00
|
|
|
case c := <-bob.newChannels:
|
2018-09-26 12:12:57 +03:00
|
|
|
close(c.err)
|
2017-12-22 21:29:12 +03:00
|
|
|
case <-time.After(time.Second * 15):
|
2017-09-01 12:42:40 +03:00
|
|
|
t.Fatalf("bob did not send new channel to peer")
|
|
|
|
}
|
2017-09-25 01:03:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFundingManagerNormalWorkflow(t *testing.T) {
|
2019-07-11 14:14:35 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2019-07-11 14:14:35 +03:00
|
|
|
alice, bob := setupFundingManagers(t)
|
2017-09-25 01:03:07 +03:00
|
|
|
defer tearDownFundingManagers(t, alice, bob)
|
|
|
|
|
|
|
|
// We will consume the channel updates as we go, so no buffering is needed.
|
|
|
|
updateChan := make(chan *lnrpc.OpenStatusUpdate)
|
|
|
|
|
|
|
|
// Run through the process of opening the channel, up until the funding
|
|
|
|
// transaction is broadcasted.
|
2019-01-12 20:59:46 +03:00
|
|
|
localAmt := btcutil.Amount(500000)
|
|
|
|
pushAmt := btcutil.Amount(0)
|
|
|
|
capacity := localAmt + pushAmt
|
2019-07-11 14:14:37 +03:00
|
|
|
fundingOutPoint, fundingTx := openChannel(
|
|
|
|
t, alice, bob, localAmt, pushAmt, 1, updateChan, true,
|
|
|
|
)
|
2017-09-25 01:03:07 +03:00
|
|
|
|
2018-03-13 05:11:40 +03:00
|
|
|
// Check that neither Alice nor Bob sent an error message.
|
|
|
|
assertErrorNotSent(t, alice.msgChan)
|
|
|
|
assertErrorNotSent(t, bob.msgChan)
|
|
|
|
|
|
|
|
// Notify that transaction was mined.
|
2019-07-11 14:14:37 +03:00
|
|
|
alice.mockNotifier.oneConfChannel <- &chainntnfs.TxConfirmation{
|
|
|
|
Tx: fundingTx,
|
|
|
|
}
|
|
|
|
bob.mockNotifier.oneConfChannel <- &chainntnfs.TxConfirmation{
|
|
|
|
Tx: fundingTx,
|
|
|
|
}
|
2017-09-25 01:03:07 +03:00
|
|
|
|
|
|
|
// The funding transaction was mined, so assert that both funding
|
|
|
|
// managers now have the state of this channel 'markedOpen' in their
|
|
|
|
// internal state machine.
|
|
|
|
assertMarkedOpen(t, alice, bob, fundingOutPoint)
|
|
|
|
|
|
|
|
// After the funding transaction is mined, Alice will send
|
|
|
|
// fundingLocked to Bob.
|
2018-03-13 05:11:40 +03:00
|
|
|
fundingLockedAlice := assertFundingMsgSent(
|
|
|
|
t, alice.msgChan, "FundingLocked",
|
|
|
|
).(*lnwire.FundingLocked)
|
2017-09-25 01:03:07 +03:00
|
|
|
|
|
|
|
// And similarly Bob will send funding locked to Alice.
|
2018-03-13 05:11:40 +03:00
|
|
|
fundingLockedBob := assertFundingMsgSent(
|
|
|
|
t, bob.msgChan, "FundingLocked",
|
|
|
|
).(*lnwire.FundingLocked)
|
2017-09-25 01:03:07 +03:00
|
|
|
|
|
|
|
// Check that the state machine is updated accordingly
|
|
|
|
assertFundingLockedSent(t, alice, bob, fundingOutPoint)
|
|
|
|
|
2017-11-14 04:32:27 +03:00
|
|
|
// Make sure both fundingManagers send the expected channel
|
|
|
|
// announcements.
|
2019-01-12 20:59:46 +03:00
|
|
|
assertChannelAnnouncements(t, alice, bob, capacity)
|
2017-11-14 02:59:17 +03:00
|
|
|
|
|
|
|
// Check that the state machine is updated accordingly
|
|
|
|
assertAddedToRouterGraph(t, alice, bob, fundingOutPoint)
|
|
|
|
|
2017-11-14 04:32:27 +03:00
|
|
|
// The funding transaction is now confirmed, wait for the
|
2017-09-25 01:03:07 +03:00
|
|
|
// OpenStatusUpdate_ChanOpen update
|
|
|
|
waitForOpenUpdate(t, updateChan)
|
|
|
|
|
|
|
|
// Exchange the fundingLocked messages.
|
2018-07-06 05:32:22 +03:00
|
|
|
alice.fundingMgr.processFundingLocked(fundingLockedBob, bob)
|
|
|
|
bob.fundingMgr.processFundingLocked(fundingLockedAlice, alice)
|
2017-09-01 12:42:40 +03:00
|
|
|
|
2017-09-25 01:03:07 +03:00
|
|
|
// Check that they notify the breach arbiter and peer about the new
|
|
|
|
// channel.
|
|
|
|
assertHandleFundingLocked(t, alice, bob)
|
2017-11-14 04:32:27 +03:00
|
|
|
|
|
|
|
// Notify that six confirmations has been reached on funding transaction.
|
2019-07-11 14:14:37 +03:00
|
|
|
alice.mockNotifier.sixConfChannel <- &chainntnfs.TxConfirmation{
|
|
|
|
Tx: fundingTx,
|
|
|
|
}
|
|
|
|
bob.mockNotifier.sixConfChannel <- &chainntnfs.TxConfirmation{
|
|
|
|
Tx: fundingTx,
|
|
|
|
}
|
2017-11-14 04:32:27 +03:00
|
|
|
|
|
|
|
// Make sure the fundingManagers exchange announcement signatures.
|
|
|
|
assertAnnouncementSignatures(t, alice, bob)
|
|
|
|
|
|
|
|
// The internal state-machine should now have deleted the channelStates
|
|
|
|
// from the database, as the channel is announced.
|
|
|
|
assertNoChannelState(t, alice, bob, fundingOutPoint)
|
2017-06-08 20:48:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFundingManagerRestartBehavior(t *testing.T) {
|
2019-07-11 14:14:35 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2019-07-11 14:14:35 +03:00
|
|
|
alice, bob := setupFundingManagers(t)
|
2017-09-01 12:42:40 +03:00
|
|
|
defer tearDownFundingManagers(t, alice, bob)
|
2017-06-08 20:48:07 +03:00
|
|
|
|
|
|
|
// Run through the process of opening the channel, up until the funding
|
|
|
|
// transaction is broadcasted.
|
2019-01-12 20:59:46 +03:00
|
|
|
localAmt := btcutil.Amount(500000)
|
|
|
|
pushAmt := btcutil.Amount(0)
|
|
|
|
capacity := localAmt + pushAmt
|
2017-06-08 20:48:07 +03:00
|
|
|
updateChan := make(chan *lnrpc.OpenStatusUpdate)
|
2019-07-11 14:14:37 +03:00
|
|
|
fundingOutPoint, fundingTx := openChannel(
|
|
|
|
t, alice, bob, localAmt, pushAmt, 1, updateChan, true,
|
|
|
|
)
|
2017-06-08 20:48:07 +03:00
|
|
|
|
|
|
|
// After the funding transaction gets mined, both nodes will send the
|
|
|
|
// fundingLocked message to the other peer. If the funding node fails
|
|
|
|
// before this message has been successfully sent, it should retry
|
|
|
|
// sending it on restart. We mimic this behavior by letting the
|
|
|
|
// SendToPeer method return an error, as if the message was not
|
2017-09-25 01:03:07 +03:00
|
|
|
// successfully sent. We then recreate the fundingManager and make sure
|
2018-07-06 05:32:22 +03:00
|
|
|
// it continues the process as expected. We'll save the current
|
|
|
|
// implementation of sendMessage to restore the original behavior later
|
|
|
|
// on.
|
|
|
|
workingSendMessage := bob.sendMessage
|
|
|
|
bob.sendMessage = func(msg lnwire.Message) error {
|
2017-06-08 20:48:07 +03:00
|
|
|
return fmt.Errorf("intentional error in SendToPeer")
|
|
|
|
}
|
2019-05-31 00:37:30 +03:00
|
|
|
alice.fundingMgr.cfg.NotifyWhenOnline = func(peer [33]byte,
|
2018-07-06 05:32:22 +03:00
|
|
|
con chan<- lnpeer.Peer) {
|
2018-02-07 06:11:11 +03:00
|
|
|
// Intentionally empty.
|
2017-09-25 01:03:07 +03:00
|
|
|
}
|
2017-06-08 20:48:07 +03:00
|
|
|
|
|
|
|
// Notify that transaction was mined
|
2019-07-11 14:14:37 +03:00
|
|
|
alice.mockNotifier.oneConfChannel <- &chainntnfs.TxConfirmation{
|
|
|
|
Tx: fundingTx,
|
|
|
|
}
|
|
|
|
bob.mockNotifier.oneConfChannel <- &chainntnfs.TxConfirmation{
|
|
|
|
Tx: fundingTx,
|
|
|
|
}
|
2017-06-08 20:48:07 +03:00
|
|
|
|
|
|
|
// The funding transaction was mined, so assert that both funding
|
|
|
|
// managers now have the state of this channel 'markedOpen' in their
|
|
|
|
// internal state machine.
|
2017-09-25 01:03:07 +03:00
|
|
|
assertMarkedOpen(t, alice, bob, fundingOutPoint)
|
2017-06-08 20:48:07 +03:00
|
|
|
|
|
|
|
// After the funding transaction was mined, Bob should have successfully
|
|
|
|
// sent the fundingLocked message, while Alice failed sending it. In
|
|
|
|
// Alice's case this means that there should be no messages for Bob, and
|
2017-09-01 12:42:40 +03:00
|
|
|
// the channel should still be in state 'markedOpen'
|
2017-06-08 20:48:07 +03:00
|
|
|
select {
|
|
|
|
case msg := <-alice.msgChan:
|
|
|
|
t.Fatalf("did not expect any message from Alice: %v", msg)
|
|
|
|
default:
|
|
|
|
// Expected.
|
|
|
|
}
|
|
|
|
|
2017-09-25 01:03:07 +03:00
|
|
|
// Bob will send funding locked to Alice.
|
2018-03-13 05:11:40 +03:00
|
|
|
fundingLockedBob := assertFundingMsgSent(
|
|
|
|
t, bob.msgChan, "FundingLocked",
|
|
|
|
).(*lnwire.FundingLocked)
|
2017-06-08 20:48:07 +03:00
|
|
|
|
|
|
|
// Alice should still be markedOpen
|
2017-10-25 23:17:22 +03:00
|
|
|
assertDatabaseState(t, alice, fundingOutPoint, markedOpen)
|
2017-06-08 20:48:07 +03:00
|
|
|
|
|
|
|
// While Bob successfully sent fundingLocked.
|
2017-10-25 23:17:22 +03:00
|
|
|
assertDatabaseState(t, bob, fundingOutPoint, fundingLockedSent)
|
2017-06-08 20:48:07 +03:00
|
|
|
|
2018-07-06 05:32:22 +03:00
|
|
|
// We now recreate Alice's fundingManager with the correct sendMessage
|
|
|
|
// implementation, and expect it to retry sending the fundingLocked
|
|
|
|
// message. We'll explicitly shut down Alice's funding manager to
|
|
|
|
// prevent a race when overriding the sendMessage implementation.
|
|
|
|
if err := alice.fundingMgr.Stop(); err != nil {
|
|
|
|
t.Fatalf("unable to stop alice's funding manager: %v", err)
|
|
|
|
}
|
|
|
|
bob.sendMessage = workingSendMessage
|
2017-06-08 20:48:07 +03:00
|
|
|
recreateAliceFundingManager(t, alice)
|
|
|
|
|
2017-11-14 02:59:17 +03:00
|
|
|
// Intentionally make the channel announcements fail
|
2019-04-17 23:26:11 +03:00
|
|
|
alice.fundingMgr.cfg.SendAnnouncement = func(msg lnwire.Message,
|
|
|
|
_ ...discovery.OptionalMsgField) chan error {
|
|
|
|
|
|
|
|
errChan := make(chan error, 1)
|
|
|
|
errChan <- fmt.Errorf("intentional error in SendAnnouncement")
|
|
|
|
return errChan
|
|
|
|
}
|
2017-06-08 20:48:07 +03:00
|
|
|
|
2018-03-13 05:11:40 +03:00
|
|
|
fundingLockedAlice := assertFundingMsgSent(
|
|
|
|
t, alice.msgChan, "FundingLocked",
|
|
|
|
).(*lnwire.FundingLocked)
|
2017-06-08 20:48:07 +03:00
|
|
|
|
|
|
|
// The state should now be fundingLockedSent
|
2017-10-25 23:17:22 +03:00
|
|
|
assertDatabaseState(t, alice, fundingOutPoint, fundingLockedSent)
|
2017-06-08 20:48:07 +03:00
|
|
|
|
|
|
|
// Check that the channel announcements were never sent
|
|
|
|
select {
|
|
|
|
case ann := <-alice.announceChan:
|
2017-10-25 23:17:22 +03:00
|
|
|
t.Fatalf("unexpectedly got channel announcement message: %v",
|
|
|
|
ann)
|
2017-06-08 20:48:07 +03:00
|
|
|
default:
|
|
|
|
// Expected
|
|
|
|
}
|
|
|
|
|
2017-11-14 04:32:27 +03:00
|
|
|
// Exchange the fundingLocked messages.
|
2018-07-06 05:32:22 +03:00
|
|
|
alice.fundingMgr.processFundingLocked(fundingLockedBob, bob)
|
|
|
|
bob.fundingMgr.processFundingLocked(fundingLockedAlice, alice)
|
2017-11-14 02:59:17 +03:00
|
|
|
|
2017-11-14 04:32:27 +03:00
|
|
|
// Check that they notify the breach arbiter and peer about the new
|
|
|
|
// channel.
|
|
|
|
assertHandleFundingLocked(t, alice, bob)
|
2017-11-14 02:59:17 +03:00
|
|
|
|
2017-11-14 04:32:27 +03:00
|
|
|
// Next up, we check that Alice rebroadcasts the announcement
|
|
|
|
// messages on restart. Bob should as expected send announcements.
|
|
|
|
recreateAliceFundingManager(t, alice)
|
2017-11-14 02:59:17 +03:00
|
|
|
time.Sleep(300 * time.Millisecond)
|
|
|
|
|
2017-11-14 04:32:27 +03:00
|
|
|
// Make sure both fundingManagers send the expected channel
|
|
|
|
// announcements.
|
2019-01-12 20:59:46 +03:00
|
|
|
assertChannelAnnouncements(t, alice, bob, capacity)
|
2017-11-14 04:32:27 +03:00
|
|
|
|
2017-11-14 02:59:17 +03:00
|
|
|
// Check that the state machine is updated accordingly
|
|
|
|
assertAddedToRouterGraph(t, alice, bob, fundingOutPoint)
|
|
|
|
|
2018-02-07 06:11:11 +03:00
|
|
|
// Next, we check that Alice sends the announcement signatures
|
2017-11-14 04:32:27 +03:00
|
|
|
// on restart after six confirmations. Bob should as expected send
|
|
|
|
// them as well.
|
2017-11-14 02:59:17 +03:00
|
|
|
recreateAliceFundingManager(t, alice)
|
|
|
|
time.Sleep(300 * time.Millisecond)
|
|
|
|
|
|
|
|
// Notify that six confirmations has been reached on funding transaction.
|
2019-07-11 14:14:37 +03:00
|
|
|
alice.mockNotifier.sixConfChannel <- &chainntnfs.TxConfirmation{
|
|
|
|
Tx: fundingTx,
|
|
|
|
}
|
|
|
|
bob.mockNotifier.sixConfChannel <- &chainntnfs.TxConfirmation{
|
|
|
|
Tx: fundingTx,
|
|
|
|
}
|
2017-11-14 02:59:17 +03:00
|
|
|
|
2017-11-14 04:32:27 +03:00
|
|
|
// Make sure the fundingManagers exchange announcement signatures.
|
|
|
|
assertAnnouncementSignatures(t, alice, bob)
|
2017-06-08 20:48:07 +03:00
|
|
|
|
2017-09-25 01:03:07 +03:00
|
|
|
// The internal state-machine should now have deleted the channelStates
|
|
|
|
// from the database, as the channel is announced.
|
|
|
|
assertNoChannelState(t, alice, bob, fundingOutPoint)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestFundingManagerOfflinePeer checks that the fundingManager waits for the
|
|
|
|
// server to notify when the peer comes online, in case sending the
|
|
|
|
// fundingLocked message fails the first time.
|
|
|
|
func TestFundingManagerOfflinePeer(t *testing.T) {
|
2019-07-11 14:14:35 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2019-07-11 14:14:35 +03:00
|
|
|
alice, bob := setupFundingManagers(t)
|
2017-09-25 01:03:07 +03:00
|
|
|
defer tearDownFundingManagers(t, alice, bob)
|
|
|
|
|
|
|
|
// Run through the process of opening the channel, up until the funding
|
|
|
|
// transaction is broadcasted.
|
2019-01-12 20:59:46 +03:00
|
|
|
localAmt := btcutil.Amount(500000)
|
|
|
|
pushAmt := btcutil.Amount(0)
|
|
|
|
capacity := localAmt + pushAmt
|
2017-09-25 01:03:07 +03:00
|
|
|
updateChan := make(chan *lnrpc.OpenStatusUpdate)
|
2019-07-11 14:14:37 +03:00
|
|
|
fundingOutPoint, fundingTx := openChannel(
|
|
|
|
t, alice, bob, localAmt, pushAmt, 1, updateChan, true,
|
|
|
|
)
|
2017-09-25 01:03:07 +03:00
|
|
|
|
|
|
|
// After the funding transaction gets mined, both nodes will send the
|
|
|
|
// fundingLocked message to the other peer. If the funding node fails
|
|
|
|
// to send the fundingLocked message to the peer, it should wait for
|
|
|
|
// the server to notify it that the peer is back online, and try again.
|
2018-07-06 05:32:22 +03:00
|
|
|
// We'll save the current implementation of sendMessage to restore the
|
|
|
|
// original behavior later on.
|
|
|
|
workingSendMessage := bob.sendMessage
|
|
|
|
bob.sendMessage = func(msg lnwire.Message) error {
|
2017-09-25 01:03:07 +03:00
|
|
|
return fmt.Errorf("intentional error in SendToPeer")
|
2017-06-08 20:48:07 +03:00
|
|
|
}
|
2019-05-31 00:37:30 +03:00
|
|
|
peerChan := make(chan [33]byte, 1)
|
2018-07-06 05:32:22 +03:00
|
|
|
conChan := make(chan chan<- lnpeer.Peer, 1)
|
2019-05-31 00:37:30 +03:00
|
|
|
alice.fundingMgr.cfg.NotifyWhenOnline = func(peer [33]byte,
|
2018-07-06 05:32:22 +03:00
|
|
|
connected chan<- lnpeer.Peer) {
|
|
|
|
|
2017-09-25 01:03:07 +03:00
|
|
|
peerChan <- peer
|
|
|
|
conChan <- connected
|
2017-06-08 20:48:07 +03:00
|
|
|
}
|
2017-09-25 01:03:07 +03:00
|
|
|
|
|
|
|
// Notify that transaction was mined
|
2019-07-11 14:14:37 +03:00
|
|
|
alice.mockNotifier.oneConfChannel <- &chainntnfs.TxConfirmation{
|
|
|
|
Tx: fundingTx,
|
|
|
|
}
|
|
|
|
bob.mockNotifier.oneConfChannel <- &chainntnfs.TxConfirmation{
|
|
|
|
Tx: fundingTx,
|
|
|
|
}
|
2017-09-25 01:03:07 +03:00
|
|
|
|
|
|
|
// The funding transaction was mined, so assert that both funding
|
|
|
|
// managers now have the state of this channel 'markedOpen' in their
|
|
|
|
// internal state machine.
|
|
|
|
assertMarkedOpen(t, alice, bob, fundingOutPoint)
|
|
|
|
|
|
|
|
// After the funding transaction was mined, Bob should have successfully
|
|
|
|
// sent the fundingLocked message, while Alice failed sending it. In
|
|
|
|
// Alice's case this means that there should be no messages for Bob, and
|
|
|
|
// the channel should still be in state 'markedOpen'
|
|
|
|
select {
|
|
|
|
case msg := <-alice.msgChan:
|
|
|
|
t.Fatalf("did not expect any message from Alice: %v", msg)
|
|
|
|
default:
|
|
|
|
// Expected.
|
2017-06-08 20:48:07 +03:00
|
|
|
}
|
|
|
|
|
2017-09-25 01:03:07 +03:00
|
|
|
// Bob will send funding locked to Alice
|
2018-03-13 05:11:40 +03:00
|
|
|
fundingLockedBob := assertFundingMsgSent(
|
|
|
|
t, bob.msgChan, "FundingLocked",
|
|
|
|
).(*lnwire.FundingLocked)
|
2017-06-08 20:48:07 +03:00
|
|
|
|
2017-09-25 01:03:07 +03:00
|
|
|
// Alice should still be markedOpen
|
2017-10-25 23:17:22 +03:00
|
|
|
assertDatabaseState(t, alice, fundingOutPoint, markedOpen)
|
2017-09-25 01:03:07 +03:00
|
|
|
|
|
|
|
// While Bob successfully sent fundingLocked.
|
2017-10-25 23:17:22 +03:00
|
|
|
assertDatabaseState(t, bob, fundingOutPoint, fundingLockedSent)
|
2017-09-01 12:42:40 +03:00
|
|
|
|
2018-07-06 05:32:22 +03:00
|
|
|
// Alice should be waiting for the server to notify when Bob comes back
|
|
|
|
// online.
|
2019-05-31 00:37:30 +03:00
|
|
|
var peer [33]byte
|
2018-07-06 05:32:22 +03:00
|
|
|
var con chan<- lnpeer.Peer
|
2017-09-01 12:42:40 +03:00
|
|
|
select {
|
2017-09-25 01:03:07 +03:00
|
|
|
case peer = <-peerChan:
|
|
|
|
// Expected
|
|
|
|
case <-time.After(time.Second * 3):
|
|
|
|
t.Fatalf("alice did not register peer with server")
|
2017-09-01 12:42:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
2017-09-25 01:03:07 +03:00
|
|
|
case con = <-conChan:
|
|
|
|
// Expected
|
|
|
|
case <-time.After(time.Second * 3):
|
|
|
|
t.Fatalf("alice did not register connectedChan with server")
|
2017-09-01 12:42:40 +03:00
|
|
|
}
|
|
|
|
|
2019-05-31 00:37:30 +03:00
|
|
|
if !bytes.Equal(peer[:], bobPubKey.SerializeCompressed()) {
|
2017-09-25 01:03:07 +03:00
|
|
|
t.Fatalf("expected to receive Bob's pubkey (%v), instead got %v",
|
|
|
|
bobPubKey, peer)
|
2017-09-01 12:42:40 +03:00
|
|
|
}
|
|
|
|
|
2018-07-06 05:32:22 +03:00
|
|
|
// Restore the correct sendMessage implementation, and notify that Bob
|
|
|
|
// is back online.
|
|
|
|
bob.sendMessage = workingSendMessage
|
|
|
|
con <- bob
|
2017-09-25 01:03:07 +03:00
|
|
|
|
|
|
|
// This should make Alice send the fundingLocked.
|
2018-03-13 05:11:40 +03:00
|
|
|
fundingLockedAlice := assertFundingMsgSent(
|
|
|
|
t, alice.msgChan, "FundingLocked",
|
|
|
|
).(*lnwire.FundingLocked)
|
2017-09-25 01:03:07 +03:00
|
|
|
|
|
|
|
// The state should now be fundingLockedSent
|
2017-10-25 23:17:22 +03:00
|
|
|
assertDatabaseState(t, alice, fundingOutPoint, fundingLockedSent)
|
2017-09-01 12:42:40 +03:00
|
|
|
|
2017-10-25 23:17:22 +03:00
|
|
|
// Make sure both fundingManagers send the expected channel
|
|
|
|
// announcements.
|
2019-01-12 20:59:46 +03:00
|
|
|
assertChannelAnnouncements(t, alice, bob, capacity)
|
2017-11-14 02:59:17 +03:00
|
|
|
|
|
|
|
// Check that the state machine is updated accordingly
|
|
|
|
assertAddedToRouterGraph(t, alice, bob, fundingOutPoint)
|
|
|
|
|
2017-11-14 04:32:27 +03:00
|
|
|
// The funding transaction is now confirmed, wait for the
|
2017-09-25 01:03:07 +03:00
|
|
|
// OpenStatusUpdate_ChanOpen update
|
|
|
|
waitForOpenUpdate(t, updateChan)
|
|
|
|
|
|
|
|
// Exchange the fundingLocked messages.
|
2018-07-06 05:32:22 +03:00
|
|
|
alice.fundingMgr.processFundingLocked(fundingLockedBob, bob)
|
|
|
|
bob.fundingMgr.processFundingLocked(fundingLockedAlice, alice)
|
2017-09-25 01:03:07 +03:00
|
|
|
|
|
|
|
// Check that they notify the breach arbiter and peer about the new
|
|
|
|
// channel.
|
|
|
|
assertHandleFundingLocked(t, alice, bob)
|
|
|
|
|
2017-11-14 04:32:27 +03:00
|
|
|
// Notify that six confirmations has been reached on funding transaction.
|
2019-07-11 14:14:37 +03:00
|
|
|
alice.mockNotifier.sixConfChannel <- &chainntnfs.TxConfirmation{
|
|
|
|
Tx: fundingTx,
|
|
|
|
}
|
|
|
|
bob.mockNotifier.sixConfChannel <- &chainntnfs.TxConfirmation{
|
|
|
|
Tx: fundingTx,
|
|
|
|
}
|
2017-11-14 04:32:27 +03:00
|
|
|
|
|
|
|
// Make sure both fundingManagers send the expected announcement
|
|
|
|
// signatures.
|
|
|
|
assertAnnouncementSignatures(t, alice, bob)
|
|
|
|
|
|
|
|
// The internal state-machine should now have deleted the channelStates
|
|
|
|
// from the database, as the channel is announced.
|
|
|
|
assertNoChannelState(t, alice, bob, fundingOutPoint)
|
2017-06-08 20:48:07 +03:00
|
|
|
}
|
|
|
|
|
2018-03-13 05:11:40 +03:00
|
|
|
// TestFundingManagerPeerTimeoutAfterInitFunding checks that the zombie sweeper
|
|
|
|
// will properly clean up a zombie reservation that times out after the
|
|
|
|
// initFundingMsg has been handled.
|
|
|
|
func TestFundingManagerPeerTimeoutAfterInitFunding(t *testing.T) {
|
2019-07-11 14:14:35 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2019-07-11 14:14:35 +03:00
|
|
|
alice, bob := setupFundingManagers(t)
|
2018-03-13 05:11:40 +03:00
|
|
|
defer tearDownFundingManagers(t, alice, bob)
|
|
|
|
|
|
|
|
// We will consume the channel updates as we go, so no buffering is needed.
|
|
|
|
updateChan := make(chan *lnrpc.OpenStatusUpdate)
|
|
|
|
|
|
|
|
// Create a funding request and start the workflow.
|
|
|
|
errChan := make(chan error, 1)
|
|
|
|
initReq := &openChanReq{
|
|
|
|
targetPubkey: bob.privKey.PubKey(),
|
|
|
|
chainHash: *activeNetParams.GenesisHash,
|
|
|
|
localFundingAmt: 500000,
|
|
|
|
pushAmt: lnwire.NewMSatFromSatoshis(0),
|
|
|
|
private: false,
|
|
|
|
updates: updateChan,
|
|
|
|
err: errChan,
|
|
|
|
}
|
|
|
|
|
2018-07-06 05:32:22 +03:00
|
|
|
alice.fundingMgr.initFundingWorkflow(bob, initReq)
|
2018-03-13 05:11:40 +03:00
|
|
|
|
|
|
|
// Alice should have sent the OpenChannel message to Bob.
|
|
|
|
var aliceMsg lnwire.Message
|
|
|
|
select {
|
|
|
|
case aliceMsg = <-alice.msgChan:
|
|
|
|
case err := <-initReq.err:
|
|
|
|
t.Fatalf("error init funding workflow: %v", err)
|
|
|
|
case <-time.After(time.Second * 5):
|
|
|
|
t.Fatalf("alice did not send OpenChannel message")
|
|
|
|
}
|
|
|
|
|
|
|
|
_, ok := aliceMsg.(*lnwire.OpenChannel)
|
|
|
|
if !ok {
|
|
|
|
errorMsg, gotError := aliceMsg.(*lnwire.Error)
|
|
|
|
if gotError {
|
|
|
|
t.Fatalf("expected OpenChannel to be sent "+
|
|
|
|
"from bob, instead got error: %v",
|
2019-09-20 11:55:21 +03:00
|
|
|
errorMsg.Error())
|
2018-03-13 05:11:40 +03:00
|
|
|
}
|
|
|
|
t.Fatalf("expected OpenChannel to be sent from "+
|
|
|
|
"alice, instead got %T", aliceMsg)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Alice should have a new pending reservation.
|
|
|
|
assertNumPendingReservations(t, alice, bobPubKey, 1)
|
|
|
|
|
|
|
|
// Make sure Alice's reservation times out and then run her zombie sweeper.
|
|
|
|
time.Sleep(1 * time.Millisecond)
|
|
|
|
go alice.fundingMgr.pruneZombieReservations()
|
|
|
|
|
|
|
|
// Alice should have sent an Error message to Bob.
|
|
|
|
assertErrorSent(t, alice.msgChan)
|
|
|
|
|
|
|
|
// Alice's zombie reservation should have been pruned.
|
|
|
|
assertNumPendingReservations(t, alice, bobPubKey, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestFundingManagerPeerTimeoutAfterFundingOpen checks that the zombie sweeper
|
|
|
|
// will properly clean up a zombie reservation that times out after the
|
|
|
|
// fundingOpenMsg has been handled.
|
|
|
|
func TestFundingManagerPeerTimeoutAfterFundingOpen(t *testing.T) {
|
2019-07-11 14:14:35 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2019-07-11 14:14:35 +03:00
|
|
|
alice, bob := setupFundingManagers(t)
|
2018-03-13 05:11:40 +03:00
|
|
|
defer tearDownFundingManagers(t, alice, bob)
|
|
|
|
|
|
|
|
// We will consume the channel updates as we go, so no buffering is needed.
|
|
|
|
updateChan := make(chan *lnrpc.OpenStatusUpdate)
|
|
|
|
|
|
|
|
// Create a funding request and start the workflow.
|
|
|
|
errChan := make(chan error, 1)
|
|
|
|
initReq := &openChanReq{
|
|
|
|
targetPubkey: bob.privKey.PubKey(),
|
|
|
|
chainHash: *activeNetParams.GenesisHash,
|
|
|
|
localFundingAmt: 500000,
|
|
|
|
pushAmt: lnwire.NewMSatFromSatoshis(0),
|
|
|
|
private: false,
|
|
|
|
updates: updateChan,
|
|
|
|
err: errChan,
|
|
|
|
}
|
|
|
|
|
2018-07-06 05:32:22 +03:00
|
|
|
alice.fundingMgr.initFundingWorkflow(bob, initReq)
|
2018-03-13 05:11:40 +03:00
|
|
|
|
|
|
|
// Alice should have sent the OpenChannel message to Bob.
|
|
|
|
var aliceMsg lnwire.Message
|
|
|
|
select {
|
|
|
|
case aliceMsg = <-alice.msgChan:
|
|
|
|
case err := <-initReq.err:
|
|
|
|
t.Fatalf("error init funding workflow: %v", err)
|
|
|
|
case <-time.After(time.Second * 5):
|
|
|
|
t.Fatalf("alice did not send OpenChannel message")
|
|
|
|
}
|
|
|
|
|
|
|
|
openChannelReq, ok := aliceMsg.(*lnwire.OpenChannel)
|
|
|
|
if !ok {
|
|
|
|
errorMsg, gotError := aliceMsg.(*lnwire.Error)
|
|
|
|
if gotError {
|
|
|
|
t.Fatalf("expected OpenChannel to be sent "+
|
|
|
|
"from bob, instead got error: %v",
|
2019-09-20 11:55:21 +03:00
|
|
|
errorMsg.Error())
|
2018-03-13 05:11:40 +03:00
|
|
|
}
|
|
|
|
t.Fatalf("expected OpenChannel to be sent from "+
|
|
|
|
"alice, instead got %T", aliceMsg)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Alice should have a new pending reservation.
|
|
|
|
assertNumPendingReservations(t, alice, bobPubKey, 1)
|
|
|
|
|
|
|
|
// Let Bob handle the init message.
|
2018-07-06 05:32:22 +03:00
|
|
|
bob.fundingMgr.processFundingOpen(openChannelReq, alice)
|
2018-03-13 05:11:40 +03:00
|
|
|
|
|
|
|
// Bob should answer with an AcceptChannel.
|
|
|
|
assertFundingMsgSent(t, bob.msgChan, "AcceptChannel")
|
|
|
|
|
|
|
|
// Bob should have a new pending reservation.
|
|
|
|
assertNumPendingReservations(t, bob, alicePubKey, 1)
|
|
|
|
|
|
|
|
// Make sure Bob's reservation times out and then run his zombie sweeper.
|
|
|
|
time.Sleep(1 * time.Millisecond)
|
|
|
|
go bob.fundingMgr.pruneZombieReservations()
|
|
|
|
|
|
|
|
// Bob should have sent an Error message to Alice.
|
|
|
|
assertErrorSent(t, bob.msgChan)
|
|
|
|
|
|
|
|
// Bob's zombie reservation should have been pruned.
|
|
|
|
assertNumPendingReservations(t, bob, alicePubKey, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestFundingManagerPeerTimeoutAfterFundingAccept checks that the zombie sweeper
|
|
|
|
// will properly clean up a zombie reservation that times out after the
|
|
|
|
// fundingAcceptMsg has been handled.
|
|
|
|
func TestFundingManagerPeerTimeoutAfterFundingAccept(t *testing.T) {
|
2019-07-11 14:14:35 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2019-07-11 14:14:35 +03:00
|
|
|
alice, bob := setupFundingManagers(t)
|
2018-03-13 05:11:40 +03:00
|
|
|
defer tearDownFundingManagers(t, alice, bob)
|
|
|
|
|
|
|
|
// We will consume the channel updates as we go, so no buffering is needed.
|
|
|
|
updateChan := make(chan *lnrpc.OpenStatusUpdate)
|
|
|
|
|
|
|
|
// Create a funding request and start the workflow.
|
|
|
|
errChan := make(chan error, 1)
|
|
|
|
initReq := &openChanReq{
|
|
|
|
targetPubkey: bob.privKey.PubKey(),
|
|
|
|
chainHash: *activeNetParams.GenesisHash,
|
|
|
|
localFundingAmt: 500000,
|
|
|
|
pushAmt: lnwire.NewMSatFromSatoshis(0),
|
|
|
|
private: false,
|
|
|
|
updates: updateChan,
|
|
|
|
err: errChan,
|
|
|
|
}
|
|
|
|
|
2018-07-06 05:32:22 +03:00
|
|
|
alice.fundingMgr.initFundingWorkflow(bob, initReq)
|
2018-03-13 05:11:40 +03:00
|
|
|
|
|
|
|
// Alice should have sent the OpenChannel message to Bob.
|
|
|
|
var aliceMsg lnwire.Message
|
|
|
|
select {
|
|
|
|
case aliceMsg = <-alice.msgChan:
|
|
|
|
case err := <-initReq.err:
|
|
|
|
t.Fatalf("error init funding workflow: %v", err)
|
|
|
|
case <-time.After(time.Second * 5):
|
|
|
|
t.Fatalf("alice did not send OpenChannel message")
|
|
|
|
}
|
|
|
|
|
|
|
|
openChannelReq, ok := aliceMsg.(*lnwire.OpenChannel)
|
|
|
|
if !ok {
|
|
|
|
errorMsg, gotError := aliceMsg.(*lnwire.Error)
|
|
|
|
if gotError {
|
|
|
|
t.Fatalf("expected OpenChannel to be sent "+
|
|
|
|
"from bob, instead got error: %v",
|
2019-09-20 11:55:21 +03:00
|
|
|
errorMsg.Error())
|
2018-03-13 05:11:40 +03:00
|
|
|
}
|
|
|
|
t.Fatalf("expected OpenChannel to be sent from "+
|
|
|
|
"alice, instead got %T", aliceMsg)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Alice should have a new pending reservation.
|
|
|
|
assertNumPendingReservations(t, alice, bobPubKey, 1)
|
|
|
|
|
|
|
|
// Let Bob handle the init message.
|
2018-07-06 05:32:22 +03:00
|
|
|
bob.fundingMgr.processFundingOpen(openChannelReq, alice)
|
2018-03-13 05:11:40 +03:00
|
|
|
|
|
|
|
// Bob should answer with an AcceptChannel.
|
|
|
|
acceptChannelResponse := assertFundingMsgSent(
|
|
|
|
t, bob.msgChan, "AcceptChannel",
|
|
|
|
).(*lnwire.AcceptChannel)
|
|
|
|
|
|
|
|
// Bob should have a new pending reservation.
|
|
|
|
assertNumPendingReservations(t, bob, alicePubKey, 1)
|
|
|
|
|
|
|
|
// Forward the response to Alice.
|
2018-07-06 05:32:22 +03:00
|
|
|
alice.fundingMgr.processFundingAccept(acceptChannelResponse, bob)
|
2018-03-13 05:11:40 +03:00
|
|
|
|
|
|
|
// Alice responds with a FundingCreated messages.
|
|
|
|
assertFundingMsgSent(t, alice.msgChan, "FundingCreated")
|
|
|
|
|
|
|
|
// Make sure Alice's reservation times out and then run her zombie sweeper.
|
|
|
|
time.Sleep(1 * time.Millisecond)
|
|
|
|
go alice.fundingMgr.pruneZombieReservations()
|
|
|
|
|
|
|
|
// Alice should have sent an Error message to Bob.
|
|
|
|
assertErrorSent(t, alice.msgChan)
|
|
|
|
|
|
|
|
// Alice's zombie reservation should have been pruned.
|
|
|
|
assertNumPendingReservations(t, alice, bobPubKey, 0)
|
|
|
|
}
|
|
|
|
|
2017-06-08 20:48:07 +03:00
|
|
|
func TestFundingManagerFundingTimeout(t *testing.T) {
|
2019-07-11 14:14:35 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2019-07-11 14:14:35 +03:00
|
|
|
alice, bob := setupFundingManagers(t)
|
2017-09-01 12:42:40 +03:00
|
|
|
defer tearDownFundingManagers(t, alice, bob)
|
2017-06-08 20:48:07 +03:00
|
|
|
|
|
|
|
// We will consume the channel updates as we go, so no buffering is needed.
|
|
|
|
updateChan := make(chan *lnrpc.OpenStatusUpdate)
|
|
|
|
|
|
|
|
// Run through the process of opening the channel, up until the funding
|
|
|
|
// transaction is broadcasted.
|
2019-07-11 14:14:37 +03:00
|
|
|
_, _ = openChannel(t, alice, bob, 500000, 0, 1, updateChan, true)
|
2017-06-08 20:48:07 +03:00
|
|
|
|
|
|
|
// Bob will at this point be waiting for the funding transaction to be
|
|
|
|
// confirmed, so the channel should be considered pending.
|
|
|
|
pendingChannels, err := bob.fundingMgr.cfg.Wallet.Cfg.Database.FetchPendingChannels()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to fetch pending channels: %v", err)
|
|
|
|
}
|
|
|
|
if len(pendingChannels) != 1 {
|
|
|
|
t.Fatalf("Expected Bob to have 1 pending channel, had %v",
|
|
|
|
len(pendingChannels))
|
|
|
|
}
|
|
|
|
|
2019-05-19 01:11:17 +03:00
|
|
|
// We expect Bob to forget the channel after 2016 blocks (2 weeks), so
|
|
|
|
// mine 2016-1, and check that it is still pending.
|
2017-06-08 20:48:07 +03:00
|
|
|
bob.mockNotifier.epochChan <- &chainntnfs.BlockEpoch{
|
2019-05-19 01:11:17 +03:00
|
|
|
Height: fundingBroadcastHeight + maxWaitNumBlocksFundingConf - 1,
|
2017-06-08 20:48:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Bob should still be waiting for the channel to open.
|
2018-02-23 03:16:33 +03:00
|
|
|
assertNumPendingChannelsRemains(t, bob, 1)
|
2017-06-08 20:48:07 +03:00
|
|
|
|
|
|
|
bob.mockNotifier.epochChan <- &chainntnfs.BlockEpoch{
|
2019-05-19 01:11:17 +03:00
|
|
|
Height: fundingBroadcastHeight + maxWaitNumBlocksFundingConf,
|
2017-06-08 20:48:07 +03:00
|
|
|
}
|
|
|
|
|
2018-05-30 13:51:44 +03:00
|
|
|
// Bob should have sent an Error message to Alice.
|
|
|
|
assertErrorSent(t, bob.msgChan)
|
|
|
|
|
2017-10-25 23:17:22 +03:00
|
|
|
// Should not be pending anymore.
|
2018-02-23 03:16:33 +03:00
|
|
|
assertNumPendingChannelsBecomes(t, bob, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestFundingManagerFundingNotTimeoutInitiator checks that if the user was
|
2018-03-13 05:11:40 +03:00
|
|
|
// the channel initiator, that it does not timeout when the lnd restarts.
|
2018-02-23 03:16:33 +03:00
|
|
|
func TestFundingManagerFundingNotTimeoutInitiator(t *testing.T) {
|
2019-07-11 14:14:35 +03:00
|
|
|
t.Parallel()
|
2018-02-23 03:16:33 +03:00
|
|
|
|
2019-07-11 14:14:35 +03:00
|
|
|
alice, bob := setupFundingManagers(t)
|
2018-02-23 03:16:33 +03:00
|
|
|
defer tearDownFundingManagers(t, alice, bob)
|
|
|
|
|
|
|
|
// We will consume the channel updates as we go, so no buffering is needed.
|
|
|
|
updateChan := make(chan *lnrpc.OpenStatusUpdate)
|
|
|
|
|
|
|
|
// Run through the process of opening the channel, up until the funding
|
|
|
|
// transaction is broadcasted.
|
2019-07-11 14:14:37 +03:00
|
|
|
_, _ = openChannel(t, alice, bob, 500000, 0, 1, updateChan, true)
|
2018-02-23 03:16:33 +03:00
|
|
|
|
|
|
|
// Alice will at this point be waiting for the funding transaction to be
|
|
|
|
// confirmed, so the channel should be considered pending.
|
|
|
|
pendingChannels, err := alice.fundingMgr.cfg.Wallet.Cfg.Database.FetchPendingChannels()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to fetch pending channels: %v", err)
|
|
|
|
}
|
|
|
|
if len(pendingChannels) != 1 {
|
|
|
|
t.Fatalf("Expected Alice to have 1 pending channel, had %v",
|
|
|
|
len(pendingChannels))
|
|
|
|
}
|
|
|
|
|
|
|
|
recreateAliceFundingManager(t, alice)
|
|
|
|
|
2018-03-11 04:09:03 +03:00
|
|
|
// We should receive the rebroadcasted funding txn.
|
|
|
|
select {
|
|
|
|
case <-alice.publTxChan:
|
|
|
|
case <-time.After(time.Second * 5):
|
|
|
|
t.Fatalf("alice did not publish funding tx")
|
|
|
|
}
|
|
|
|
|
2018-09-16 11:40:10 +03:00
|
|
|
// Increase the height to 1 minus the maxWaitNumBlocksFundingConf height.
|
2018-02-23 03:16:33 +03:00
|
|
|
alice.mockNotifier.epochChan <- &chainntnfs.BlockEpoch{
|
|
|
|
Height: fundingBroadcastHeight + maxWaitNumBlocksFundingConf - 1,
|
|
|
|
}
|
|
|
|
|
|
|
|
bob.mockNotifier.epochChan <- &chainntnfs.BlockEpoch{
|
|
|
|
Height: fundingBroadcastHeight + maxWaitNumBlocksFundingConf - 1,
|
|
|
|
}
|
|
|
|
|
2018-09-16 11:40:10 +03:00
|
|
|
// Assert both and Alice and Bob still have 1 pending channels.
|
2018-02-23 03:16:33 +03:00
|
|
|
assertNumPendingChannelsRemains(t, alice, 1)
|
|
|
|
|
|
|
|
assertNumPendingChannelsRemains(t, bob, 1)
|
|
|
|
|
2018-09-16 11:40:10 +03:00
|
|
|
// Increase both Alice and Bob to maxWaitNumBlocksFundingConf height.
|
2018-02-23 03:16:33 +03:00
|
|
|
alice.mockNotifier.epochChan <- &chainntnfs.BlockEpoch{
|
|
|
|
Height: fundingBroadcastHeight + maxWaitNumBlocksFundingConf,
|
|
|
|
}
|
|
|
|
|
|
|
|
bob.mockNotifier.epochChan <- &chainntnfs.BlockEpoch{
|
|
|
|
Height: fundingBroadcastHeight + maxWaitNumBlocksFundingConf,
|
|
|
|
}
|
|
|
|
|
2018-09-16 11:40:10 +03:00
|
|
|
// Since Alice was the initiator, the channel should not have timed out.
|
2018-02-23 03:16:33 +03:00
|
|
|
assertNumPendingChannelsRemains(t, alice, 1)
|
|
|
|
|
2018-05-30 13:51:44 +03:00
|
|
|
// Bob should have sent an Error message to Alice.
|
|
|
|
assertErrorSent(t, bob.msgChan)
|
|
|
|
|
2018-09-16 11:40:10 +03:00
|
|
|
// Since Bob was not the initiator, the channel should timeout.
|
2018-02-23 03:16:33 +03:00
|
|
|
assertNumPendingChannelsBecomes(t, bob, 0)
|
2017-06-08 20:48:07 +03:00
|
|
|
}
|
2017-09-01 12:42:40 +03:00
|
|
|
|
|
|
|
// TestFundingManagerReceiveFundingLockedTwice checks that the fundingManager
|
|
|
|
// continues to operate as expected in case we receive a duplicate fundingLocked
|
|
|
|
// message.
|
|
|
|
func TestFundingManagerReceiveFundingLockedTwice(t *testing.T) {
|
2019-07-11 14:14:35 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2019-07-11 14:14:35 +03:00
|
|
|
alice, bob := setupFundingManagers(t)
|
2017-09-01 12:42:40 +03:00
|
|
|
defer tearDownFundingManagers(t, alice, bob)
|
|
|
|
|
|
|
|
// We will consume the channel updates as we go, so no buffering is needed.
|
|
|
|
updateChan := make(chan *lnrpc.OpenStatusUpdate)
|
|
|
|
|
|
|
|
// Run through the process of opening the channel, up until the funding
|
|
|
|
// transaction is broadcasted.
|
2019-01-12 20:59:46 +03:00
|
|
|
localAmt := btcutil.Amount(500000)
|
|
|
|
pushAmt := btcutil.Amount(0)
|
|
|
|
capacity := localAmt + pushAmt
|
2019-07-11 14:14:37 +03:00
|
|
|
fundingOutPoint, fundingTx := openChannel(
|
|
|
|
t, alice, bob, localAmt, pushAmt, 1, updateChan, true,
|
|
|
|
)
|
2017-09-01 12:42:40 +03:00
|
|
|
|
|
|
|
// Notify that transaction was mined
|
2019-07-11 14:14:37 +03:00
|
|
|
alice.mockNotifier.oneConfChannel <- &chainntnfs.TxConfirmation{
|
|
|
|
Tx: fundingTx,
|
|
|
|
}
|
|
|
|
bob.mockNotifier.oneConfChannel <- &chainntnfs.TxConfirmation{
|
|
|
|
Tx: fundingTx,
|
|
|
|
}
|
2017-09-01 12:42:40 +03:00
|
|
|
|
|
|
|
// The funding transaction was mined, so assert that both funding
|
|
|
|
// managers now have the state of this channel 'markedOpen' in their
|
|
|
|
// internal state machine.
|
2017-09-25 01:03:07 +03:00
|
|
|
assertMarkedOpen(t, alice, bob, fundingOutPoint)
|
2017-09-01 12:42:40 +03:00
|
|
|
|
|
|
|
// After the funding transaction is mined, Alice will send
|
|
|
|
// fundingLocked to Bob.
|
2018-03-13 05:11:40 +03:00
|
|
|
fundingLockedAlice := assertFundingMsgSent(
|
|
|
|
t, alice.msgChan, "FundingLocked",
|
|
|
|
).(*lnwire.FundingLocked)
|
2017-09-01 12:42:40 +03:00
|
|
|
|
|
|
|
// And similarly Bob will send funding locked to Alice.
|
2018-03-13 05:11:40 +03:00
|
|
|
fundingLockedBob := assertFundingMsgSent(
|
|
|
|
t, bob.msgChan, "FundingLocked",
|
|
|
|
).(*lnwire.FundingLocked)
|
2017-09-01 12:42:40 +03:00
|
|
|
|
|
|
|
// Check that the state machine is updated accordingly
|
2017-09-25 01:03:07 +03:00
|
|
|
assertFundingLockedSent(t, alice, bob, fundingOutPoint)
|
2017-09-01 12:42:40 +03:00
|
|
|
|
2017-11-14 04:32:27 +03:00
|
|
|
// Make sure both fundingManagers send the expected channel
|
|
|
|
// announcements.
|
2019-01-12 20:59:46 +03:00
|
|
|
assertChannelAnnouncements(t, alice, bob, capacity)
|
2017-11-14 02:59:17 +03:00
|
|
|
|
|
|
|
// Check that the state machine is updated accordingly
|
|
|
|
assertAddedToRouterGraph(t, alice, bob, fundingOutPoint)
|
|
|
|
|
2017-11-14 04:32:27 +03:00
|
|
|
// The funding transaction is now confirmed, wait for the
|
2017-09-25 01:03:07 +03:00
|
|
|
// OpenStatusUpdate_ChanOpen update
|
|
|
|
waitForOpenUpdate(t, updateChan)
|
2017-09-01 12:42:40 +03:00
|
|
|
|
2017-09-25 01:03:07 +03:00
|
|
|
// Send the fundingLocked message twice to Alice, and once to Bob.
|
2018-07-06 05:32:22 +03:00
|
|
|
alice.fundingMgr.processFundingLocked(fundingLockedBob, bob)
|
|
|
|
alice.fundingMgr.processFundingLocked(fundingLockedBob, bob)
|
|
|
|
bob.fundingMgr.processFundingLocked(fundingLockedAlice, alice)
|
2017-09-01 12:42:40 +03:00
|
|
|
|
2017-09-25 01:03:07 +03:00
|
|
|
// Check that they notify the breach arbiter and peer about the new
|
|
|
|
// channel.
|
|
|
|
assertHandleFundingLocked(t, alice, bob)
|
2017-09-01 12:42:40 +03:00
|
|
|
|
|
|
|
// Alice should not send the channel state the second time, as the
|
|
|
|
// second funding locked should just be ignored.
|
|
|
|
select {
|
2018-07-06 05:32:22 +03:00
|
|
|
case <-alice.newChannels:
|
2017-09-01 12:42:40 +03:00
|
|
|
t.Fatalf("alice sent new channel to peer a second time")
|
|
|
|
case <-time.After(time.Millisecond * 300):
|
|
|
|
// Expected
|
|
|
|
}
|
|
|
|
|
|
|
|
// Another fundingLocked should also be ignored, since Alice should
|
|
|
|
// have updated her database at this point.
|
2018-07-06 05:32:22 +03:00
|
|
|
alice.fundingMgr.processFundingLocked(fundingLockedBob, bob)
|
2017-09-01 12:42:40 +03:00
|
|
|
select {
|
2018-07-06 05:32:22 +03:00
|
|
|
case <-alice.newChannels:
|
2017-09-01 12:42:40 +03:00
|
|
|
t.Fatalf("alice sent new channel to peer a second time")
|
|
|
|
case <-time.After(time.Millisecond * 300):
|
|
|
|
// Expected
|
|
|
|
}
|
|
|
|
|
2017-11-14 04:32:27 +03:00
|
|
|
// Notify that six confirmations has been reached on funding transaction.
|
2019-07-11 14:14:37 +03:00
|
|
|
alice.mockNotifier.sixConfChannel <- &chainntnfs.TxConfirmation{
|
|
|
|
Tx: fundingTx,
|
|
|
|
}
|
|
|
|
bob.mockNotifier.sixConfChannel <- &chainntnfs.TxConfirmation{
|
|
|
|
Tx: fundingTx,
|
|
|
|
}
|
2017-11-14 04:32:27 +03:00
|
|
|
|
|
|
|
// Make sure the fundingManagers exchange announcement signatures.
|
|
|
|
assertAnnouncementSignatures(t, alice, bob)
|
|
|
|
|
|
|
|
// The internal state-machine should now have deleted the channelStates
|
|
|
|
// from the database, as the channel is announced.
|
|
|
|
assertNoChannelState(t, alice, bob, fundingOutPoint)
|
2017-09-01 12:42:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// TestFundingManagerRestartAfterChanAnn checks that the fundingManager properly
|
|
|
|
// handles receiving a fundingLocked after the its own fundingLocked and channel
|
|
|
|
// announcement is sent and gets restarted.
|
|
|
|
func TestFundingManagerRestartAfterChanAnn(t *testing.T) {
|
2019-07-11 14:14:35 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2019-07-11 14:14:35 +03:00
|
|
|
alice, bob := setupFundingManagers(t)
|
2017-09-01 12:42:40 +03:00
|
|
|
defer tearDownFundingManagers(t, alice, bob)
|
|
|
|
|
|
|
|
// We will consume the channel updates as we go, so no buffering is needed.
|
|
|
|
updateChan := make(chan *lnrpc.OpenStatusUpdate)
|
|
|
|
|
|
|
|
// Run through the process of opening the channel, up until the funding
|
|
|
|
// transaction is broadcasted.
|
2019-01-12 20:59:46 +03:00
|
|
|
localAmt := btcutil.Amount(500000)
|
|
|
|
pushAmt := btcutil.Amount(0)
|
|
|
|
capacity := localAmt + pushAmt
|
2019-07-11 14:14:37 +03:00
|
|
|
fundingOutPoint, fundingTx := openChannel(
|
|
|
|
t, alice, bob, localAmt, pushAmt, 1, updateChan, true,
|
|
|
|
)
|
2017-09-01 12:42:40 +03:00
|
|
|
|
|
|
|
// Notify that transaction was mined
|
2019-07-11 14:14:37 +03:00
|
|
|
alice.mockNotifier.oneConfChannel <- &chainntnfs.TxConfirmation{
|
|
|
|
Tx: fundingTx,
|
|
|
|
}
|
|
|
|
bob.mockNotifier.oneConfChannel <- &chainntnfs.TxConfirmation{
|
|
|
|
Tx: fundingTx,
|
|
|
|
}
|
2017-09-01 12:42:40 +03:00
|
|
|
|
|
|
|
// The funding transaction was mined, so assert that both funding
|
|
|
|
// managers now have the state of this channel 'markedOpen' in their
|
|
|
|
// internal state machine.
|
2017-09-25 01:03:07 +03:00
|
|
|
assertMarkedOpen(t, alice, bob, fundingOutPoint)
|
2017-09-01 12:42:40 +03:00
|
|
|
|
|
|
|
// After the funding transaction is mined, Alice will send
|
|
|
|
// fundingLocked to Bob.
|
2018-03-13 05:11:40 +03:00
|
|
|
fundingLockedAlice := assertFundingMsgSent(
|
|
|
|
t, alice.msgChan, "FundingLocked",
|
|
|
|
).(*lnwire.FundingLocked)
|
2017-09-01 12:42:40 +03:00
|
|
|
|
|
|
|
// And similarly Bob will send funding locked to Alice.
|
2018-03-13 05:11:40 +03:00
|
|
|
fundingLockedBob := assertFundingMsgSent(
|
|
|
|
t, bob.msgChan, "FundingLocked",
|
|
|
|
).(*lnwire.FundingLocked)
|
2017-09-01 12:42:40 +03:00
|
|
|
|
|
|
|
// Check that the state machine is updated accordingly
|
2017-09-25 01:03:07 +03:00
|
|
|
assertFundingLockedSent(t, alice, bob, fundingOutPoint)
|
2017-09-01 12:42:40 +03:00
|
|
|
|
2017-11-14 04:32:27 +03:00
|
|
|
// Make sure both fundingManagers send the expected channel
|
|
|
|
// announcements.
|
2019-01-12 20:59:46 +03:00
|
|
|
assertChannelAnnouncements(t, alice, bob, capacity)
|
2017-11-14 02:59:17 +03:00
|
|
|
|
|
|
|
// Check that the state machine is updated accordingly
|
|
|
|
assertAddedToRouterGraph(t, alice, bob, fundingOutPoint)
|
|
|
|
|
2017-11-14 04:32:27 +03:00
|
|
|
// The funding transaction is now confirmed, wait for the
|
2017-09-01 12:42:40 +03:00
|
|
|
// OpenStatusUpdate_ChanOpen update
|
2017-09-25 01:03:07 +03:00
|
|
|
waitForOpenUpdate(t, updateChan)
|
2017-09-01 12:42:40 +03:00
|
|
|
|
|
|
|
// At this point we restart Alice's fundingManager, before she receives
|
|
|
|
// the fundingLocked message. After restart, she will receive it, and
|
|
|
|
// we expect her to be able to handle it correctly.
|
|
|
|
recreateAliceFundingManager(t, alice)
|
|
|
|
|
|
|
|
// Exchange the fundingLocked messages.
|
2018-07-06 05:32:22 +03:00
|
|
|
alice.fundingMgr.processFundingLocked(fundingLockedBob, bob)
|
|
|
|
bob.fundingMgr.processFundingLocked(fundingLockedAlice, alice)
|
2017-09-01 12:42:40 +03:00
|
|
|
|
2017-09-25 01:03:07 +03:00
|
|
|
// Check that they notify the breach arbiter and peer about the new
|
|
|
|
// channel.
|
|
|
|
assertHandleFundingLocked(t, alice, bob)
|
2017-11-14 04:32:27 +03:00
|
|
|
|
|
|
|
// Notify that six confirmations has been reached on funding transaction.
|
2019-07-11 14:14:37 +03:00
|
|
|
alice.mockNotifier.sixConfChannel <- &chainntnfs.TxConfirmation{
|
|
|
|
Tx: fundingTx,
|
|
|
|
}
|
|
|
|
bob.mockNotifier.sixConfChannel <- &chainntnfs.TxConfirmation{
|
|
|
|
Tx: fundingTx,
|
|
|
|
}
|
2017-11-14 04:32:27 +03:00
|
|
|
|
|
|
|
// Make sure both fundingManagers send the expected channel announcements.
|
|
|
|
assertAnnouncementSignatures(t, alice, bob)
|
|
|
|
|
|
|
|
// The internal state-machine should now have deleted the channelStates
|
|
|
|
// from the database, as the channel is announced.
|
|
|
|
assertNoChannelState(t, alice, bob, fundingOutPoint)
|
2017-09-01 12:42:40 +03:00
|
|
|
}
|
|
|
|
|
2017-11-14 04:32:27 +03:00
|
|
|
// TestFundingManagerRestartAfterReceivingFundingLocked checks that the
|
|
|
|
// fundingManager continues to operate as expected after it has received
|
2017-09-01 12:42:40 +03:00
|
|
|
// fundingLocked and then gets restarted.
|
2017-11-14 04:32:27 +03:00
|
|
|
func TestFundingManagerRestartAfterReceivingFundingLocked(t *testing.T) {
|
2019-07-11 14:14:35 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2019-07-11 14:14:35 +03:00
|
|
|
alice, bob := setupFundingManagers(t)
|
2017-09-01 12:42:40 +03:00
|
|
|
defer tearDownFundingManagers(t, alice, bob)
|
|
|
|
|
|
|
|
// We will consume the channel updates as we go, so no buffering is needed.
|
|
|
|
updateChan := make(chan *lnrpc.OpenStatusUpdate)
|
|
|
|
|
|
|
|
// Run through the process of opening the channel, up until the funding
|
|
|
|
// transaction is broadcasted.
|
2019-01-12 20:59:46 +03:00
|
|
|
localAmt := btcutil.Amount(500000)
|
|
|
|
pushAmt := btcutil.Amount(0)
|
|
|
|
capacity := localAmt + pushAmt
|
2019-07-11 14:14:37 +03:00
|
|
|
fundingOutPoint, fundingTx := openChannel(
|
|
|
|
t, alice, bob, localAmt, pushAmt, 1, updateChan, true,
|
|
|
|
)
|
2017-09-01 12:42:40 +03:00
|
|
|
|
|
|
|
// Notify that transaction was mined
|
2019-07-11 14:14:37 +03:00
|
|
|
alice.mockNotifier.oneConfChannel <- &chainntnfs.TxConfirmation{
|
|
|
|
Tx: fundingTx,
|
|
|
|
}
|
|
|
|
bob.mockNotifier.oneConfChannel <- &chainntnfs.TxConfirmation{
|
|
|
|
Tx: fundingTx,
|
|
|
|
}
|
2017-09-01 12:42:40 +03:00
|
|
|
|
|
|
|
// The funding transaction was mined, so assert that both funding
|
|
|
|
// managers now have the state of this channel 'markedOpen' in their
|
|
|
|
// internal state machine.
|
2017-09-25 01:03:07 +03:00
|
|
|
assertMarkedOpen(t, alice, bob, fundingOutPoint)
|
2017-09-01 12:42:40 +03:00
|
|
|
|
|
|
|
// After the funding transaction is mined, Alice will send
|
|
|
|
// fundingLocked to Bob.
|
2018-03-13 05:11:40 +03:00
|
|
|
fundingLockedAlice := assertFundingMsgSent(
|
|
|
|
t, alice.msgChan, "FundingLocked",
|
|
|
|
).(*lnwire.FundingLocked)
|
2017-09-01 12:42:40 +03:00
|
|
|
|
|
|
|
// And similarly Bob will send funding locked to Alice.
|
2018-03-13 05:11:40 +03:00
|
|
|
fundingLockedBob := assertFundingMsgSent(
|
|
|
|
t, bob.msgChan, "FundingLocked",
|
|
|
|
).(*lnwire.FundingLocked)
|
2017-09-01 12:42:40 +03:00
|
|
|
|
|
|
|
// Check that the state machine is updated accordingly
|
2017-09-25 01:03:07 +03:00
|
|
|
assertFundingLockedSent(t, alice, bob, fundingOutPoint)
|
2017-09-01 12:42:40 +03:00
|
|
|
|
|
|
|
// Let Alice immediately get the fundingLocked message.
|
2018-07-06 05:32:22 +03:00
|
|
|
alice.fundingMgr.processFundingLocked(fundingLockedBob, bob)
|
2017-09-01 12:42:40 +03:00
|
|
|
|
2017-11-14 02:59:17 +03:00
|
|
|
// Also let Bob get the fundingLocked message.
|
2018-07-06 05:32:22 +03:00
|
|
|
bob.fundingMgr.processFundingLocked(fundingLockedAlice, alice)
|
2017-11-14 02:59:17 +03:00
|
|
|
|
|
|
|
// Check that they notify the breach arbiter and peer about the new
|
|
|
|
// channel.
|
|
|
|
assertHandleFundingLocked(t, alice, bob)
|
|
|
|
|
|
|
|
// At this point we restart Alice's fundingManager.
|
2017-09-01 12:42:40 +03:00
|
|
|
recreateAliceFundingManager(t, alice)
|
|
|
|
|
2017-11-14 04:32:27 +03:00
|
|
|
// Make sure both fundingManagers send the expected channel
|
|
|
|
// announcements.
|
2019-01-12 20:59:46 +03:00
|
|
|
assertChannelAnnouncements(t, alice, bob, capacity)
|
2017-11-14 02:59:17 +03:00
|
|
|
|
|
|
|
// Check that the state machine is updated accordingly
|
|
|
|
assertAddedToRouterGraph(t, alice, bob, fundingOutPoint)
|
|
|
|
|
|
|
|
// Notify that six confirmations has been reached on funding transaction.
|
2019-07-11 14:14:37 +03:00
|
|
|
alice.mockNotifier.sixConfChannel <- &chainntnfs.TxConfirmation{
|
|
|
|
Tx: fundingTx,
|
|
|
|
}
|
|
|
|
bob.mockNotifier.sixConfChannel <- &chainntnfs.TxConfirmation{
|
|
|
|
Tx: fundingTx,
|
|
|
|
}
|
2017-09-01 12:42:40 +03:00
|
|
|
|
2017-09-25 01:03:07 +03:00
|
|
|
// Make sure both fundingManagers send the expected channel announcements.
|
2017-11-14 04:32:27 +03:00
|
|
|
assertAnnouncementSignatures(t, alice, bob)
|
2017-09-01 12:42:40 +03:00
|
|
|
|
|
|
|
// The internal state-machine should now have deleted the channelStates
|
|
|
|
// from the database, as the channel is announced.
|
2017-09-25 01:03:07 +03:00
|
|
|
assertNoChannelState(t, alice, bob, fundingOutPoint)
|
2017-11-14 02:59:17 +03:00
|
|
|
}
|
2017-09-01 12:42:40 +03:00
|
|
|
|
2017-11-14 04:32:27 +03:00
|
|
|
// TestFundingManagerPrivateChannel tests that if we open a private channel
|
|
|
|
// (a channel not supposed to be announced to the rest of the network),
|
|
|
|
// the announcementSignatures nor the nodeAnnouncement messages are sent.
|
2017-11-14 02:59:17 +03:00
|
|
|
func TestFundingManagerPrivateChannel(t *testing.T) {
|
2019-07-11 14:14:35 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2019-07-11 14:14:35 +03:00
|
|
|
alice, bob := setupFundingManagers(t)
|
2017-11-14 02:59:17 +03:00
|
|
|
defer tearDownFundingManagers(t, alice, bob)
|
|
|
|
|
|
|
|
// We will consume the channel updates as we go, so no buffering is needed.
|
|
|
|
updateChan := make(chan *lnrpc.OpenStatusUpdate)
|
|
|
|
|
|
|
|
// Run through the process of opening the channel, up until the funding
|
|
|
|
// transaction is broadcasted.
|
2019-01-12 20:59:46 +03:00
|
|
|
localAmt := btcutil.Amount(500000)
|
|
|
|
pushAmt := btcutil.Amount(0)
|
|
|
|
capacity := localAmt + pushAmt
|
2019-07-11 14:14:37 +03:00
|
|
|
fundingOutPoint, fundingTx := openChannel(
|
|
|
|
t, alice, bob, localAmt, pushAmt, 1, updateChan, false,
|
|
|
|
)
|
2017-11-14 02:59:17 +03:00
|
|
|
|
|
|
|
// Notify that transaction was mined
|
2019-07-11 14:14:37 +03:00
|
|
|
alice.mockNotifier.oneConfChannel <- &chainntnfs.TxConfirmation{
|
|
|
|
Tx: fundingTx,
|
|
|
|
}
|
|
|
|
bob.mockNotifier.oneConfChannel <- &chainntnfs.TxConfirmation{
|
|
|
|
Tx: fundingTx,
|
|
|
|
}
|
2017-11-14 02:59:17 +03:00
|
|
|
|
|
|
|
// The funding transaction was mined, so assert that both funding
|
|
|
|
// managers now have the state of this channel 'markedOpen' in their
|
|
|
|
// internal state machine.
|
|
|
|
assertMarkedOpen(t, alice, bob, fundingOutPoint)
|
|
|
|
|
|
|
|
// After the funding transaction is mined, Alice will send
|
|
|
|
// fundingLocked to Bob.
|
2018-03-13 05:11:40 +03:00
|
|
|
fundingLockedAlice := assertFundingMsgSent(
|
|
|
|
t, alice.msgChan, "FundingLocked",
|
|
|
|
).(*lnwire.FundingLocked)
|
2017-11-14 02:59:17 +03:00
|
|
|
|
|
|
|
// And similarly Bob will send funding locked to Alice.
|
2018-03-13 05:11:40 +03:00
|
|
|
fundingLockedBob := assertFundingMsgSent(
|
|
|
|
t, bob.msgChan, "FundingLocked",
|
|
|
|
).(*lnwire.FundingLocked)
|
2017-11-14 02:59:17 +03:00
|
|
|
|
|
|
|
// Check that the state machine is updated accordingly
|
|
|
|
assertFundingLockedSent(t, alice, bob, fundingOutPoint)
|
|
|
|
|
2017-11-14 04:32:27 +03:00
|
|
|
// Make sure both fundingManagers send the expected channel
|
|
|
|
// announcements.
|
2019-01-12 20:59:46 +03:00
|
|
|
assertChannelAnnouncements(t, alice, bob, capacity)
|
2017-11-14 02:59:17 +03:00
|
|
|
|
2017-11-14 04:32:27 +03:00
|
|
|
// The funding transaction is now confirmed, wait for the
|
|
|
|
// OpenStatusUpdate_ChanOpen update
|
|
|
|
waitForOpenUpdate(t, updateChan)
|
2017-11-14 02:59:17 +03:00
|
|
|
|
2017-11-14 04:32:27 +03:00
|
|
|
// Exchange the fundingLocked messages.
|
2018-07-06 05:32:22 +03:00
|
|
|
alice.fundingMgr.processFundingLocked(fundingLockedBob, bob)
|
|
|
|
bob.fundingMgr.processFundingLocked(fundingLockedAlice, alice)
|
2017-11-14 02:59:17 +03:00
|
|
|
|
2017-11-14 04:32:27 +03:00
|
|
|
// Check that they notify the breach arbiter and peer about the new
|
|
|
|
// channel.
|
|
|
|
assertHandleFundingLocked(t, alice, bob)
|
|
|
|
|
|
|
|
// Notify that six confirmations has been reached on funding transaction.
|
2019-07-11 14:14:37 +03:00
|
|
|
alice.mockNotifier.sixConfChannel <- &chainntnfs.TxConfirmation{
|
|
|
|
Tx: fundingTx,
|
|
|
|
}
|
|
|
|
bob.mockNotifier.sixConfChannel <- &chainntnfs.TxConfirmation{
|
|
|
|
Tx: fundingTx,
|
|
|
|
}
|
2017-11-14 04:32:27 +03:00
|
|
|
|
|
|
|
// Since this is a private channel, we shouldn't receive the
|
2018-10-18 01:59:02 +03:00
|
|
|
// announcement signatures.
|
2017-11-14 02:59:17 +03:00
|
|
|
select {
|
|
|
|
case ann := <-alice.announceChan:
|
|
|
|
t.Fatalf("unexpectedly got channel announcement message: %v", ann)
|
2017-11-14 04:32:27 +03:00
|
|
|
case <-time.After(300 * time.Millisecond):
|
2017-11-14 02:59:17 +03:00
|
|
|
// Expected
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case ann := <-bob.announceChan:
|
|
|
|
t.Fatalf("unexpectedly got channel announcement message: %v", ann)
|
2017-11-14 04:32:27 +03:00
|
|
|
case <-time.After(300 * time.Millisecond):
|
2017-11-14 02:59:17 +03:00
|
|
|
// Expected
|
|
|
|
}
|
|
|
|
|
2018-10-18 01:59:02 +03:00
|
|
|
// We should however receive each side's node announcement.
|
|
|
|
select {
|
|
|
|
case msg := <-alice.msgChan:
|
|
|
|
if _, ok := msg.(*lnwire.NodeAnnouncement); !ok {
|
|
|
|
t.Fatalf("expected to receive node announcement")
|
|
|
|
}
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
t.Fatalf("expected to receive node announcement")
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case msg := <-bob.msgChan:
|
|
|
|
if _, ok := msg.(*lnwire.NodeAnnouncement); !ok {
|
|
|
|
t.Fatalf("expected to receive node announcement")
|
|
|
|
}
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
t.Fatalf("expected to receive node announcement")
|
|
|
|
}
|
|
|
|
|
2017-11-14 02:59:17 +03:00
|
|
|
// The internal state-machine should now have deleted the channelStates
|
|
|
|
// from the database, as the channel is announced.
|
|
|
|
assertNoChannelState(t, alice, bob, fundingOutPoint)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestFundingManagerPrivateRestart tests that the privacy guarantees granted
|
|
|
|
// by the private channel persist even on restart. This means that the
|
2017-11-14 04:32:27 +03:00
|
|
|
// announcement signatures nor the node announcement messages are sent upon
|
2017-11-14 02:59:17 +03:00
|
|
|
// restart.
|
|
|
|
func TestFundingManagerPrivateRestart(t *testing.T) {
|
2019-07-11 14:14:35 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2019-07-11 14:14:35 +03:00
|
|
|
alice, bob := setupFundingManagers(t)
|
2017-11-14 02:59:17 +03:00
|
|
|
defer tearDownFundingManagers(t, alice, bob)
|
|
|
|
|
|
|
|
// We will consume the channel updates as we go, so no buffering is needed.
|
|
|
|
updateChan := make(chan *lnrpc.OpenStatusUpdate)
|
|
|
|
|
|
|
|
// Run through the process of opening the channel, up until the funding
|
|
|
|
// transaction is broadcasted.
|
2019-01-12 20:59:46 +03:00
|
|
|
localAmt := btcutil.Amount(500000)
|
|
|
|
pushAmt := btcutil.Amount(0)
|
|
|
|
capacity := localAmt + pushAmt
|
2019-07-11 14:14:37 +03:00
|
|
|
fundingOutPoint, fundingTx := openChannel(
|
|
|
|
t, alice, bob, localAmt, pushAmt, 1, updateChan, false,
|
|
|
|
)
|
2017-11-14 02:59:17 +03:00
|
|
|
|
|
|
|
// Notify that transaction was mined
|
2019-07-11 14:14:37 +03:00
|
|
|
alice.mockNotifier.oneConfChannel <- &chainntnfs.TxConfirmation{
|
|
|
|
Tx: fundingTx,
|
|
|
|
}
|
|
|
|
bob.mockNotifier.oneConfChannel <- &chainntnfs.TxConfirmation{
|
|
|
|
Tx: fundingTx,
|
|
|
|
}
|
2017-11-14 02:59:17 +03:00
|
|
|
|
|
|
|
// The funding transaction was mined, so assert that both funding
|
|
|
|
// managers now have the state of this channel 'markedOpen' in their
|
|
|
|
// internal state machine.
|
|
|
|
assertMarkedOpen(t, alice, bob, fundingOutPoint)
|
|
|
|
|
|
|
|
// After the funding transaction is mined, Alice will send
|
|
|
|
// fundingLocked to Bob.
|
2018-03-13 05:11:40 +03:00
|
|
|
fundingLockedAlice := assertFundingMsgSent(
|
|
|
|
t, alice.msgChan, "FundingLocked",
|
|
|
|
).(*lnwire.FundingLocked)
|
2017-11-14 02:59:17 +03:00
|
|
|
|
|
|
|
// And similarly Bob will send funding locked to Alice.
|
2018-03-13 05:11:40 +03:00
|
|
|
fundingLockedBob := assertFundingMsgSent(
|
|
|
|
t, bob.msgChan, "FundingLocked",
|
|
|
|
).(*lnwire.FundingLocked)
|
2017-11-14 02:59:17 +03:00
|
|
|
|
|
|
|
// Check that the state machine is updated accordingly
|
|
|
|
assertFundingLockedSent(t, alice, bob, fundingOutPoint)
|
|
|
|
|
2017-11-14 04:32:27 +03:00
|
|
|
// Make sure both fundingManagers send the expected channel
|
|
|
|
// announcements.
|
2019-01-12 20:59:46 +03:00
|
|
|
assertChannelAnnouncements(t, alice, bob, capacity)
|
2017-11-14 02:59:17 +03:00
|
|
|
|
2017-11-14 04:32:27 +03:00
|
|
|
// Note: We don't check for the addedToRouterGraph state because in
|
|
|
|
// the private channel mode, the state is quickly changed from
|
|
|
|
// addedToRouterGraph to deleted from the database since the public
|
|
|
|
// announcement phase is skipped.
|
2017-11-14 02:59:17 +03:00
|
|
|
|
2017-11-14 04:32:27 +03:00
|
|
|
// The funding transaction is now confirmed, wait for the
|
|
|
|
// OpenStatusUpdate_ChanOpen update
|
|
|
|
waitForOpenUpdate(t, updateChan)
|
|
|
|
|
|
|
|
// Exchange the fundingLocked messages.
|
2018-07-06 05:32:22 +03:00
|
|
|
alice.fundingMgr.processFundingLocked(fundingLockedBob, bob)
|
|
|
|
bob.fundingMgr.processFundingLocked(fundingLockedAlice, alice)
|
2017-11-14 02:59:17 +03:00
|
|
|
|
2017-11-14 04:32:27 +03:00
|
|
|
// Check that they notify the breach arbiter and peer about the new
|
|
|
|
// channel.
|
|
|
|
assertHandleFundingLocked(t, alice, bob)
|
|
|
|
|
|
|
|
// Notify that six confirmations has been reached on funding transaction.
|
2019-07-11 14:14:37 +03:00
|
|
|
alice.mockNotifier.sixConfChannel <- &chainntnfs.TxConfirmation{
|
|
|
|
Tx: fundingTx,
|
|
|
|
}
|
|
|
|
bob.mockNotifier.sixConfChannel <- &chainntnfs.TxConfirmation{
|
|
|
|
Tx: fundingTx,
|
|
|
|
}
|
2017-11-14 02:59:17 +03:00
|
|
|
|
|
|
|
// Since this is a private channel, we shouldn't receive the public
|
2018-10-18 01:59:02 +03:00
|
|
|
// channel announcement messages.
|
|
|
|
select {
|
|
|
|
case ann := <-alice.announceChan:
|
|
|
|
t.Fatalf("unexpectedly got channel announcement message: %v", ann)
|
|
|
|
case <-time.After(300 * time.Millisecond):
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case ann := <-bob.announceChan:
|
|
|
|
t.Fatalf("unexpectedly got channel announcement message: %v", ann)
|
|
|
|
case <-time.After(300 * time.Millisecond):
|
|
|
|
}
|
|
|
|
|
|
|
|
// We should however receive each side's node announcement.
|
|
|
|
select {
|
|
|
|
case msg := <-alice.msgChan:
|
|
|
|
if _, ok := msg.(*lnwire.NodeAnnouncement); !ok {
|
|
|
|
t.Fatalf("expected to receive node announcement")
|
|
|
|
}
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
t.Fatalf("expected to receive node announcement")
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case msg := <-bob.msgChan:
|
|
|
|
if _, ok := msg.(*lnwire.NodeAnnouncement); !ok {
|
|
|
|
t.Fatalf("expected to receive node announcement")
|
|
|
|
}
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
t.Fatalf("expected to receive node announcement")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Restart Alice's fundingManager so we can prove that the public
|
|
|
|
// channel announcements are not sent upon restart and that the private
|
|
|
|
// setting persists upon restart.
|
|
|
|
recreateAliceFundingManager(t, alice)
|
|
|
|
|
2017-11-14 02:59:17 +03:00
|
|
|
select {
|
|
|
|
case ann := <-alice.announceChan:
|
|
|
|
t.Fatalf("unexpectedly got channel announcement message: %v", ann)
|
2017-11-14 04:32:27 +03:00
|
|
|
case <-time.After(300 * time.Millisecond):
|
2017-11-14 02:59:17 +03:00
|
|
|
// Expected
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case ann := <-bob.announceChan:
|
|
|
|
t.Fatalf("unexpectedly got channel announcement message: %v", ann)
|
2017-11-14 04:32:27 +03:00
|
|
|
case <-time.After(300 * time.Millisecond):
|
2017-11-14 02:59:17 +03:00
|
|
|
// Expected
|
|
|
|
}
|
|
|
|
|
|
|
|
// The internal state-machine should now have deleted the channelStates
|
|
|
|
// from the database, as the channel is announced.
|
|
|
|
assertNoChannelState(t, alice, bob, fundingOutPoint)
|
2017-09-01 12:42:40 +03:00
|
|
|
}
|
2018-04-05 20:17:17 +03:00
|
|
|
|
|
|
|
// TestFundingManagerCustomChannelParameters checks that custom requirements we
|
|
|
|
// specify during the channel funding flow is preserved correcly on both sides.
|
|
|
|
func TestFundingManagerCustomChannelParameters(t *testing.T) {
|
2019-07-11 14:14:35 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2019-07-11 14:14:35 +03:00
|
|
|
alice, bob := setupFundingManagers(t)
|
2018-04-05 20:17:17 +03:00
|
|
|
defer tearDownFundingManagers(t, alice, bob)
|
|
|
|
|
2018-04-05 20:53:06 +03:00
|
|
|
// This is the custom parameters we'll use.
|
2018-04-05 20:17:17 +03:00
|
|
|
const csvDelay = 67
|
2018-04-05 20:53:06 +03:00
|
|
|
const minHtlc = 1234
|
2018-04-05 20:17:17 +03:00
|
|
|
|
|
|
|
// We will consume the channel updates as we go, so no buffering is
|
|
|
|
// needed.
|
|
|
|
updateChan := make(chan *lnrpc.OpenStatusUpdate)
|
|
|
|
|
2019-01-12 20:59:46 +03:00
|
|
|
localAmt := btcutil.Amount(5000000)
|
|
|
|
pushAmt := btcutil.Amount(0)
|
|
|
|
capacity := localAmt + pushAmt
|
|
|
|
|
2018-04-05 20:17:17 +03:00
|
|
|
// Create a funding request with the custom parameters and start the
|
|
|
|
// workflow.
|
|
|
|
errChan := make(chan error, 1)
|
|
|
|
initReq := &openChanReq{
|
|
|
|
targetPubkey: bob.privKey.PubKey(),
|
|
|
|
chainHash: *activeNetParams.GenesisHash,
|
2019-01-12 20:59:46 +03:00
|
|
|
localFundingAmt: localAmt,
|
|
|
|
pushAmt: lnwire.NewMSatFromSatoshis(pushAmt),
|
2018-04-05 20:17:17 +03:00
|
|
|
private: false,
|
2018-04-05 20:53:06 +03:00
|
|
|
minHtlc: minHtlc,
|
2018-04-05 20:17:17 +03:00
|
|
|
remoteCsvDelay: csvDelay,
|
|
|
|
updates: updateChan,
|
|
|
|
err: errChan,
|
|
|
|
}
|
|
|
|
|
2018-07-06 05:32:22 +03:00
|
|
|
alice.fundingMgr.initFundingWorkflow(bob, initReq)
|
2018-04-05 20:17:17 +03:00
|
|
|
|
|
|
|
// Alice should have sent the OpenChannel message to Bob.
|
|
|
|
var aliceMsg lnwire.Message
|
|
|
|
select {
|
|
|
|
case aliceMsg = <-alice.msgChan:
|
|
|
|
case err := <-initReq.err:
|
|
|
|
t.Fatalf("error init funding workflow: %v", err)
|
|
|
|
case <-time.After(time.Second * 5):
|
|
|
|
t.Fatalf("alice did not send OpenChannel message")
|
|
|
|
}
|
|
|
|
|
|
|
|
openChannelReq, ok := aliceMsg.(*lnwire.OpenChannel)
|
|
|
|
if !ok {
|
|
|
|
errorMsg, gotError := aliceMsg.(*lnwire.Error)
|
|
|
|
if gotError {
|
|
|
|
t.Fatalf("expected OpenChannel to be sent "+
|
|
|
|
"from bob, instead got error: %v",
|
2019-09-20 11:55:21 +03:00
|
|
|
errorMsg.Error())
|
2018-04-05 20:17:17 +03:00
|
|
|
}
|
|
|
|
t.Fatalf("expected OpenChannel to be sent from "+
|
|
|
|
"alice, instead got %T", aliceMsg)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the custom CSV delay is sent as part of OpenChannel.
|
|
|
|
if openChannelReq.CsvDelay != csvDelay {
|
|
|
|
t.Fatalf("expected OpenChannel to have CSV delay %v, got %v",
|
|
|
|
csvDelay, openChannelReq.CsvDelay)
|
|
|
|
}
|
|
|
|
|
2018-04-05 20:53:06 +03:00
|
|
|
// Check that the custom minHTLC value is sent.
|
|
|
|
if openChannelReq.HtlcMinimum != minHtlc {
|
|
|
|
t.Fatalf("expected OpenChannel to have minHtlc %v, got %v",
|
|
|
|
minHtlc, openChannelReq.HtlcMinimum)
|
|
|
|
}
|
|
|
|
|
2018-04-05 20:17:17 +03:00
|
|
|
chanID := openChannelReq.PendingChannelID
|
|
|
|
|
|
|
|
// Let Bob handle the init message.
|
2018-07-06 05:32:22 +03:00
|
|
|
bob.fundingMgr.processFundingOpen(openChannelReq, alice)
|
2018-04-05 20:17:17 +03:00
|
|
|
|
|
|
|
// Bob should answer with an AcceptChannel message.
|
|
|
|
acceptChannelResponse := assertFundingMsgSent(
|
|
|
|
t, bob.msgChan, "AcceptChannel",
|
|
|
|
).(*lnwire.AcceptChannel)
|
|
|
|
|
|
|
|
// Bob should require the default delay of 4.
|
|
|
|
if acceptChannelResponse.CsvDelay != 4 {
|
|
|
|
t.Fatalf("expected AcceptChannel to have CSV delay %v, got %v",
|
|
|
|
4, acceptChannelResponse.CsvDelay)
|
|
|
|
}
|
|
|
|
|
2018-04-05 20:53:06 +03:00
|
|
|
// And the default MinHTLC value of 5.
|
|
|
|
if acceptChannelResponse.HtlcMinimum != 5 {
|
|
|
|
t.Fatalf("expected AcceptChannel to have minHtlc %v, got %v",
|
|
|
|
5, acceptChannelResponse.HtlcMinimum)
|
|
|
|
}
|
|
|
|
|
2018-04-05 20:17:17 +03:00
|
|
|
// Forward the response to Alice.
|
2018-07-06 05:32:22 +03:00
|
|
|
alice.fundingMgr.processFundingAccept(acceptChannelResponse, bob)
|
2018-04-05 20:17:17 +03:00
|
|
|
|
|
|
|
// Alice responds with a FundingCreated message.
|
|
|
|
fundingCreated := assertFundingMsgSent(
|
|
|
|
t, alice.msgChan, "FundingCreated",
|
|
|
|
).(*lnwire.FundingCreated)
|
|
|
|
|
|
|
|
// Helper method for checking the CSV delay stored for a reservation.
|
|
|
|
assertDelay := func(resCtx *reservationWithCtx,
|
|
|
|
ourDelay, theirDelay uint16) error {
|
|
|
|
|
|
|
|
ourCsvDelay := resCtx.reservation.OurContribution().CsvDelay
|
|
|
|
if ourCsvDelay != ourDelay {
|
|
|
|
return fmt.Errorf("expected our CSV delay to be %v, "+
|
|
|
|
"was %v", ourDelay, ourCsvDelay)
|
|
|
|
}
|
|
|
|
|
|
|
|
theirCsvDelay := resCtx.reservation.TheirContribution().CsvDelay
|
|
|
|
if theirCsvDelay != theirDelay {
|
|
|
|
return fmt.Errorf("expected their CSV delay to be %v, "+
|
|
|
|
"was %v", theirDelay, theirCsvDelay)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-04-05 20:53:06 +03:00
|
|
|
// Helper method for checking the MinHtlc value stored for a
|
|
|
|
// reservation.
|
|
|
|
assertMinHtlc := func(resCtx *reservationWithCtx,
|
|
|
|
expOurMinHtlc, expTheirMinHtlc lnwire.MilliSatoshi) error {
|
|
|
|
|
|
|
|
ourMinHtlc := resCtx.reservation.OurContribution().MinHTLC
|
|
|
|
if ourMinHtlc != expOurMinHtlc {
|
|
|
|
return fmt.Errorf("expected our minHtlc to be %v, "+
|
|
|
|
"was %v", expOurMinHtlc, ourMinHtlc)
|
|
|
|
}
|
|
|
|
|
|
|
|
theirMinHtlc := resCtx.reservation.TheirContribution().MinHTLC
|
|
|
|
if theirMinHtlc != expTheirMinHtlc {
|
|
|
|
return fmt.Errorf("expected their minHtlc to be %v, "+
|
|
|
|
"was %v", expTheirMinHtlc, theirMinHtlc)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-04-05 20:17:17 +03:00
|
|
|
// Check that the custom channel parameters were properly set in the
|
|
|
|
// channel reservation.
|
|
|
|
resCtx, err := alice.fundingMgr.getReservationCtx(bobPubKey, chanID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to find ctx: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Alice's CSV delay should be 4 since Bob sent the fedault value, and
|
|
|
|
// Bob's should be 67 since Alice sent the custom value.
|
|
|
|
if err := assertDelay(resCtx, 4, csvDelay); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-04-05 20:53:06 +03:00
|
|
|
// The minimum HTLC value Alice can offer should be 5, and the minimum
|
|
|
|
// Bob can offer should be 1234.
|
|
|
|
if err := assertMinHtlc(resCtx, 5, minHtlc); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-04-05 20:17:17 +03:00
|
|
|
// Also make sure the parameters are properly set on Bob's end.
|
|
|
|
resCtx, err = bob.fundingMgr.getReservationCtx(alicePubKey, chanID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to find ctx: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := assertDelay(resCtx, csvDelay, 4); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-04-05 20:53:06 +03:00
|
|
|
|
|
|
|
if err := assertMinHtlc(resCtx, minHtlc, 5); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-07-26 16:48:09 +03:00
|
|
|
|
|
|
|
// Give the message to Bob.
|
|
|
|
bob.fundingMgr.processFundingCreated(fundingCreated, alice)
|
|
|
|
|
|
|
|
// Finally, Bob should send the FundingSigned message.
|
|
|
|
fundingSigned := assertFundingMsgSent(
|
|
|
|
t, bob.msgChan, "FundingSigned",
|
|
|
|
).(*lnwire.FundingSigned)
|
|
|
|
|
|
|
|
// Forward the signature to Alice.
|
|
|
|
alice.fundingMgr.processFundingSigned(fundingSigned, bob)
|
|
|
|
|
|
|
|
// After Alice processes the singleFundingSignComplete message, she will
|
|
|
|
// broadcast the funding transaction to the network. We expect to get a
|
|
|
|
// channel update saying the channel is pending.
|
|
|
|
var pendingUpdate *lnrpc.OpenStatusUpdate
|
|
|
|
select {
|
|
|
|
case pendingUpdate = <-updateChan:
|
|
|
|
case <-time.After(time.Second * 5):
|
|
|
|
t.Fatalf("alice did not send OpenStatusUpdate_ChanPending")
|
|
|
|
}
|
|
|
|
|
|
|
|
_, ok = pendingUpdate.Update.(*lnrpc.OpenStatusUpdate_ChanPending)
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("OpenStatusUpdate was not OpenStatusUpdate_ChanPending")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for Alice to published the funding tx to the network.
|
2019-07-11 14:14:37 +03:00
|
|
|
var fundingTx *wire.MsgTx
|
2018-07-26 16:48:09 +03:00
|
|
|
select {
|
2019-07-11 14:14:37 +03:00
|
|
|
case fundingTx = <-alice.publTxChan:
|
2018-07-26 16:48:09 +03:00
|
|
|
case <-time.After(time.Second * 5):
|
|
|
|
t.Fatalf("alice did not publish funding tx")
|
|
|
|
}
|
2018-08-22 10:32:43 +03:00
|
|
|
|
|
|
|
// Notify that transaction was mined.
|
2019-07-11 14:14:37 +03:00
|
|
|
alice.mockNotifier.oneConfChannel <- &chainntnfs.TxConfirmation{
|
|
|
|
Tx: fundingTx,
|
|
|
|
}
|
|
|
|
bob.mockNotifier.oneConfChannel <- &chainntnfs.TxConfirmation{
|
|
|
|
Tx: fundingTx,
|
|
|
|
}
|
2018-08-22 10:32:43 +03:00
|
|
|
|
|
|
|
// After the funding transaction is mined, Alice will send
|
|
|
|
// fundingLocked to Bob.
|
|
|
|
_ = assertFundingMsgSent(
|
|
|
|
t, alice.msgChan, "FundingLocked",
|
|
|
|
).(*lnwire.FundingLocked)
|
|
|
|
|
|
|
|
// And similarly Bob will send funding locked to Alice.
|
|
|
|
_ = assertFundingMsgSent(
|
|
|
|
t, bob.msgChan, "FundingLocked",
|
|
|
|
).(*lnwire.FundingLocked)
|
|
|
|
|
|
|
|
// Make sure both fundingManagers send the expected channel
|
|
|
|
// announcements. Alice should advertise the default MinHTLC value of
|
|
|
|
// 5, while bob should advertise the value minHtlc, since Alice
|
|
|
|
// required him to use it.
|
2019-01-12 20:59:46 +03:00
|
|
|
assertChannelAnnouncements(t, alice, bob, capacity, 5, minHtlc)
|
2018-08-22 10:32:43 +03:00
|
|
|
|
|
|
|
// The funding transaction is now confirmed, wait for the
|
|
|
|
// OpenStatusUpdate_ChanOpen update
|
|
|
|
waitForOpenUpdate(t, updateChan)
|
2018-04-05 20:17:17 +03:00
|
|
|
}
|
2018-07-26 20:23:18 +03:00
|
|
|
|
|
|
|
// TestFundingManagerMaxPendingChannels checks that trying to open another
|
|
|
|
// channel with the same peer when MaxPending channels are pending fails.
|
|
|
|
func TestFundingManagerMaxPendingChannels(t *testing.T) {
|
2019-07-11 14:14:35 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2018-07-26 20:23:18 +03:00
|
|
|
const maxPending = 4
|
|
|
|
|
2019-07-11 14:14:35 +03:00
|
|
|
alice, bob := setupFundingManagers(
|
|
|
|
t, func(cfg *fundingConfig) {
|
|
|
|
cfg.MaxPendingChannels = maxPending
|
|
|
|
},
|
|
|
|
)
|
2018-07-26 20:23:18 +03:00
|
|
|
defer tearDownFundingManagers(t, alice, bob)
|
|
|
|
|
|
|
|
// Create openChanReqs for maxPending+1 channels.
|
|
|
|
var initReqs []*openChanReq
|
|
|
|
for i := 0; i < maxPending+1; i++ {
|
|
|
|
updateChan := make(chan *lnrpc.OpenStatusUpdate)
|
|
|
|
errChan := make(chan error, 1)
|
|
|
|
initReq := &openChanReq{
|
|
|
|
targetPubkey: bob.privKey.PubKey(),
|
|
|
|
chainHash: *activeNetParams.GenesisHash,
|
|
|
|
localFundingAmt: 5000000,
|
|
|
|
pushAmt: lnwire.NewMSatFromSatoshis(0),
|
|
|
|
private: false,
|
|
|
|
updates: updateChan,
|
|
|
|
err: errChan,
|
|
|
|
}
|
|
|
|
initReqs = append(initReqs, initReq)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Kick of maxPending+1 funding workflows.
|
|
|
|
var accepts []*lnwire.AcceptChannel
|
|
|
|
var lastOpen *lnwire.OpenChannel
|
|
|
|
for i, initReq := range initReqs {
|
|
|
|
alice.fundingMgr.initFundingWorkflow(bob, initReq)
|
|
|
|
|
|
|
|
// Alice should have sent the OpenChannel message to Bob.
|
|
|
|
var aliceMsg lnwire.Message
|
|
|
|
select {
|
|
|
|
case aliceMsg = <-alice.msgChan:
|
|
|
|
case err := <-initReq.err:
|
|
|
|
t.Fatalf("error init funding workflow: %v", err)
|
|
|
|
case <-time.After(time.Second * 5):
|
|
|
|
t.Fatalf("alice did not send OpenChannel message")
|
|
|
|
}
|
|
|
|
|
|
|
|
openChannelReq, ok := aliceMsg.(*lnwire.OpenChannel)
|
|
|
|
if !ok {
|
|
|
|
errorMsg, gotError := aliceMsg.(*lnwire.Error)
|
|
|
|
if gotError {
|
|
|
|
t.Fatalf("expected OpenChannel to be sent "+
|
|
|
|
"from bob, instead got error: %v",
|
2019-09-20 11:55:21 +03:00
|
|
|
errorMsg.Error())
|
2018-07-26 20:23:18 +03:00
|
|
|
}
|
|
|
|
t.Fatalf("expected OpenChannel to be sent from "+
|
|
|
|
"alice, instead got %T", aliceMsg)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Let Bob handle the init message.
|
|
|
|
bob.fundingMgr.processFundingOpen(openChannelReq, alice)
|
|
|
|
|
|
|
|
// Bob should answer with an AcceptChannel message for the
|
|
|
|
// first maxPending channels.
|
|
|
|
if i < maxPending {
|
|
|
|
acceptChannelResponse := assertFundingMsgSent(
|
|
|
|
t, bob.msgChan, "AcceptChannel",
|
|
|
|
).(*lnwire.AcceptChannel)
|
|
|
|
accepts = append(accepts, acceptChannelResponse)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// For the last channel, Bob should answer with an error.
|
|
|
|
lastOpen = openChannelReq
|
|
|
|
_ = assertFundingMsgSent(
|
|
|
|
t, bob.msgChan, "Error",
|
|
|
|
).(*lnwire.Error)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Forward the responses to Alice.
|
|
|
|
var signs []*lnwire.FundingSigned
|
|
|
|
for _, accept := range accepts {
|
|
|
|
alice.fundingMgr.processFundingAccept(accept, bob)
|
|
|
|
|
|
|
|
// Alice responds with a FundingCreated message.
|
|
|
|
fundingCreated := assertFundingMsgSent(
|
|
|
|
t, alice.msgChan, "FundingCreated",
|
|
|
|
).(*lnwire.FundingCreated)
|
|
|
|
|
|
|
|
// Give the message to Bob.
|
|
|
|
bob.fundingMgr.processFundingCreated(fundingCreated, alice)
|
|
|
|
|
|
|
|
// Finally, Bob should send the FundingSigned message.
|
|
|
|
fundingSigned := assertFundingMsgSent(
|
|
|
|
t, bob.msgChan, "FundingSigned",
|
|
|
|
).(*lnwire.FundingSigned)
|
|
|
|
|
|
|
|
signs = append(signs, fundingSigned)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sending another init request from Alice should still make Bob
|
|
|
|
// respond with an error.
|
|
|
|
bob.fundingMgr.processFundingOpen(lastOpen, alice)
|
|
|
|
_ = assertFundingMsgSent(
|
|
|
|
t, bob.msgChan, "Error",
|
|
|
|
).(*lnwire.Error)
|
|
|
|
|
|
|
|
// Give the FundingSigned messages to Alice.
|
2019-07-11 14:14:37 +03:00
|
|
|
var txs []*wire.MsgTx
|
2018-07-26 20:23:18 +03:00
|
|
|
for i, sign := range signs {
|
|
|
|
alice.fundingMgr.processFundingSigned(sign, bob)
|
|
|
|
|
|
|
|
// Alice should send a status update for each channel, and
|
|
|
|
// publish a funding tx to the network.
|
|
|
|
var pendingUpdate *lnrpc.OpenStatusUpdate
|
|
|
|
select {
|
|
|
|
case pendingUpdate = <-initReqs[i].updates:
|
|
|
|
case <-time.After(time.Second * 5):
|
|
|
|
t.Fatalf("alice did not send OpenStatusUpdate_ChanPending")
|
|
|
|
}
|
|
|
|
|
|
|
|
_, ok := pendingUpdate.Update.(*lnrpc.OpenStatusUpdate_ChanPending)
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("OpenStatusUpdate was not OpenStatusUpdate_ChanPending")
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
2019-07-11 14:14:37 +03:00
|
|
|
case tx := <-alice.publTxChan:
|
|
|
|
txs = append(txs, tx)
|
2018-07-26 20:23:18 +03:00
|
|
|
case <-time.After(time.Second * 5):
|
|
|
|
t.Fatalf("alice did not publish funding tx")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sending another init request from Alice should still make Bob
|
|
|
|
// respond with an error, since the funding transactions are not
|
|
|
|
// confirmed yet,
|
|
|
|
bob.fundingMgr.processFundingOpen(lastOpen, alice)
|
|
|
|
_ = assertFundingMsgSent(
|
|
|
|
t, bob.msgChan, "Error",
|
|
|
|
).(*lnwire.Error)
|
|
|
|
|
|
|
|
// Notify that the transactions were mined.
|
|
|
|
for i := 0; i < maxPending; i++ {
|
2019-07-11 14:14:37 +03:00
|
|
|
alice.mockNotifier.oneConfChannel <- &chainntnfs.TxConfirmation{
|
|
|
|
Tx: txs[i],
|
|
|
|
}
|
|
|
|
bob.mockNotifier.oneConfChannel <- &chainntnfs.TxConfirmation{
|
|
|
|
Tx: txs[i],
|
|
|
|
}
|
2018-07-26 20:23:18 +03:00
|
|
|
|
|
|
|
// Expect both to be sending FundingLocked.
|
|
|
|
_ = assertFundingMsgSent(
|
|
|
|
t, alice.msgChan, "FundingLocked",
|
|
|
|
).(*lnwire.FundingLocked)
|
|
|
|
|
|
|
|
_ = assertFundingMsgSent(
|
|
|
|
t, bob.msgChan, "FundingLocked",
|
|
|
|
).(*lnwire.FundingLocked)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now opening another channel should work.
|
|
|
|
bob.fundingMgr.processFundingOpen(lastOpen, alice)
|
|
|
|
|
|
|
|
// Bob should answer with an AcceptChannel message.
|
|
|
|
_ = assertFundingMsgSent(
|
|
|
|
t, bob.msgChan, "AcceptChannel",
|
|
|
|
).(*lnwire.AcceptChannel)
|
2018-04-05 20:17:17 +03:00
|
|
|
}
|
2018-10-12 00:29:29 +03:00
|
|
|
|
|
|
|
// TestFundingManagerRejectPush checks behaviour of 'rejectpush'
|
|
|
|
// option, namely that non-zero incoming push amounts are disabled.
|
|
|
|
func TestFundingManagerRejectPush(t *testing.T) {
|
2019-07-11 14:14:35 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2018-10-12 00:29:29 +03:00
|
|
|
// Enable 'rejectpush' option and initialize funding managers.
|
2019-07-11 14:14:35 +03:00
|
|
|
alice, bob := setupFundingManagers(
|
|
|
|
t, func(cfg *fundingConfig) {
|
|
|
|
cfg.RejectPush = true
|
|
|
|
},
|
|
|
|
)
|
|
|
|
defer tearDownFundingManagers(t, alice, bob)
|
2018-10-12 00:29:29 +03:00
|
|
|
|
|
|
|
// Create a funding request and start the workflow.
|
|
|
|
updateChan := make(chan *lnrpc.OpenStatusUpdate)
|
|
|
|
errChan := make(chan error, 1)
|
|
|
|
initReq := &openChanReq{
|
|
|
|
targetPubkey: bob.privKey.PubKey(),
|
|
|
|
chainHash: *activeNetParams.GenesisHash,
|
|
|
|
localFundingAmt: 500000,
|
|
|
|
pushAmt: lnwire.NewMSatFromSatoshis(10),
|
|
|
|
private: true,
|
|
|
|
updates: updateChan,
|
|
|
|
err: errChan,
|
|
|
|
}
|
|
|
|
|
|
|
|
alice.fundingMgr.initFundingWorkflow(bob, initReq)
|
|
|
|
|
|
|
|
// Alice should have sent the OpenChannel message to Bob.
|
|
|
|
var aliceMsg lnwire.Message
|
|
|
|
select {
|
|
|
|
case aliceMsg = <-alice.msgChan:
|
|
|
|
case err := <-initReq.err:
|
|
|
|
t.Fatalf("error init funding workflow: %v", err)
|
|
|
|
case <-time.After(time.Second * 5):
|
|
|
|
t.Fatalf("alice did not send OpenChannel message")
|
|
|
|
}
|
|
|
|
|
|
|
|
openChannelReq, ok := aliceMsg.(*lnwire.OpenChannel)
|
|
|
|
if !ok {
|
|
|
|
errorMsg, gotError := aliceMsg.(*lnwire.Error)
|
|
|
|
if gotError {
|
|
|
|
t.Fatalf("expected OpenChannel to be sent "+
|
|
|
|
"from bob, instead got error: %v",
|
2019-09-20 11:55:21 +03:00
|
|
|
errorMsg.Error())
|
2018-10-12 00:29:29 +03:00
|
|
|
}
|
|
|
|
t.Fatalf("expected OpenChannel to be sent from "+
|
|
|
|
"alice, instead got %T", aliceMsg)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Let Bob handle the init message.
|
|
|
|
bob.fundingMgr.processFundingOpen(openChannelReq, alice)
|
|
|
|
|
|
|
|
// Assert Bob responded with an ErrNonZeroPushAmount error.
|
|
|
|
err := assertFundingMsgSent(t, bob.msgChan, "Error").(*lnwire.Error)
|
2019-09-20 11:55:21 +03:00
|
|
|
if !strings.Contains(err.Error(), "Non-zero push amounts are disabled") {
|
2018-10-12 00:29:29 +03:00
|
|
|
t.Fatalf("expected ErrNonZeroPushAmount error, got \"%v\"",
|
2019-09-20 11:55:21 +03:00
|
|
|
err.Error())
|
2018-10-12 00:29:29 +03:00
|
|
|
}
|
|
|
|
}
|
2019-01-12 04:02:11 +03:00
|
|
|
|
|
|
|
// TestFundingManagerMaxConfs ensures that we don't accept a funding proposal
|
|
|
|
// that proposes a MinAcceptDepth greater than the maximum number of
|
|
|
|
// confirmations we're willing to accept.
|
|
|
|
func TestFundingManagerMaxConfs(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2019-07-11 14:14:35 +03:00
|
|
|
alice, bob := setupFundingManagers(t)
|
2019-01-12 04:02:11 +03:00
|
|
|
defer tearDownFundingManagers(t, alice, bob)
|
|
|
|
|
|
|
|
// Create a funding request and start the workflow.
|
|
|
|
updateChan := make(chan *lnrpc.OpenStatusUpdate)
|
|
|
|
errChan := make(chan error, 1)
|
|
|
|
initReq := &openChanReq{
|
|
|
|
targetPubkey: bob.privKey.PubKey(),
|
|
|
|
chainHash: *activeNetParams.GenesisHash,
|
|
|
|
localFundingAmt: 500000,
|
|
|
|
pushAmt: lnwire.NewMSatFromSatoshis(10),
|
|
|
|
private: false,
|
|
|
|
updates: updateChan,
|
|
|
|
err: errChan,
|
|
|
|
}
|
|
|
|
|
|
|
|
alice.fundingMgr.initFundingWorkflow(bob, initReq)
|
|
|
|
|
|
|
|
// Alice should have sent the OpenChannel message to Bob.
|
|
|
|
var aliceMsg lnwire.Message
|
|
|
|
select {
|
|
|
|
case aliceMsg = <-alice.msgChan:
|
|
|
|
case err := <-initReq.err:
|
|
|
|
t.Fatalf("error init funding workflow: %v", err)
|
|
|
|
case <-time.After(time.Second * 5):
|
|
|
|
t.Fatalf("alice did not send OpenChannel message")
|
|
|
|
}
|
|
|
|
|
|
|
|
openChannelReq, ok := aliceMsg.(*lnwire.OpenChannel)
|
|
|
|
if !ok {
|
|
|
|
errorMsg, gotError := aliceMsg.(*lnwire.Error)
|
|
|
|
if gotError {
|
|
|
|
t.Fatalf("expected OpenChannel to be sent "+
|
|
|
|
"from bob, instead got error: %v",
|
2019-09-20 11:55:21 +03:00
|
|
|
errorMsg.Error())
|
2019-01-12 04:02:11 +03:00
|
|
|
}
|
|
|
|
t.Fatalf("expected OpenChannel to be sent from "+
|
|
|
|
"alice, instead got %T", aliceMsg)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Let Bob handle the init message.
|
|
|
|
bob.fundingMgr.processFundingOpen(openChannelReq, alice)
|
|
|
|
|
|
|
|
// Bob should answer with an AcceptChannel message.
|
|
|
|
acceptChannelResponse := assertFundingMsgSent(
|
|
|
|
t, bob.msgChan, "AcceptChannel",
|
|
|
|
).(*lnwire.AcceptChannel)
|
|
|
|
|
|
|
|
// Modify the AcceptChannel message Bob is proposing to including a
|
|
|
|
// MinAcceptDepth Alice won't be willing to accept.
|
|
|
|
acceptChannelResponse.MinAcceptDepth = chainntnfs.MaxNumConfs + 1
|
|
|
|
|
|
|
|
alice.fundingMgr.processFundingAccept(acceptChannelResponse, bob)
|
|
|
|
|
|
|
|
// Alice should respond back with an error indicating MinAcceptDepth is
|
|
|
|
// too large.
|
|
|
|
err := assertFundingMsgSent(t, alice.msgChan, "Error").(*lnwire.Error)
|
2019-09-20 11:55:21 +03:00
|
|
|
if !strings.Contains(err.Error(), "minimum depth") {
|
2019-01-12 04:02:11 +03:00
|
|
|
t.Fatalf("expected ErrNumConfsTooLarge, got \"%v\"",
|
2019-09-20 11:55:21 +03:00
|
|
|
err.Error())
|
2019-01-12 04:02:11 +03:00
|
|
|
}
|
|
|
|
}
|
2019-07-11 14:14:38 +03:00
|
|
|
|
|
|
|
// TestFundingManagerFundAll tests that we can initiate a funding request to
|
|
|
|
// use the funds remaining in the wallet. This should produce a funding tx with
|
|
|
|
// no change output.
|
|
|
|
func TestFundingManagerFundAll(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
// We set up our mock wallet to control a list of UTXOs that sum to
|
|
|
|
// less than the max channel size.
|
|
|
|
allCoins := []*lnwallet.Utxo{
|
|
|
|
{
|
|
|
|
AddressType: lnwallet.WitnessPubKey,
|
|
|
|
Value: btcutil.Amount(
|
|
|
|
0.05 * btcutil.SatoshiPerBitcoin,
|
|
|
|
),
|
2019-11-01 07:39:17 +03:00
|
|
|
PkScript: coinPkScript,
|
2019-07-11 14:14:38 +03:00
|
|
|
OutPoint: wire.OutPoint{
|
|
|
|
Hash: chainhash.Hash{},
|
|
|
|
Index: 0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
AddressType: lnwallet.WitnessPubKey,
|
|
|
|
Value: btcutil.Amount(
|
|
|
|
0.06 * btcutil.SatoshiPerBitcoin,
|
|
|
|
),
|
2019-11-01 07:39:17 +03:00
|
|
|
PkScript: coinPkScript,
|
2019-07-11 14:14:38 +03:00
|
|
|
OutPoint: wire.OutPoint{
|
|
|
|
Hash: chainhash.Hash{},
|
|
|
|
Index: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
spendAmt btcutil.Amount
|
|
|
|
change bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
// We will spend all the funds in the wallet, and
|
|
|
|
// expects no change output.
|
|
|
|
spendAmt: btcutil.Amount(
|
|
|
|
0.11 * btcutil.SatoshiPerBitcoin,
|
|
|
|
),
|
|
|
|
change: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// We spend a little less than the funds in the wallet,
|
|
|
|
// so a change output should be created.
|
|
|
|
spendAmt: btcutil.Amount(
|
|
|
|
0.10 * btcutil.SatoshiPerBitcoin,
|
|
|
|
),
|
|
|
|
change: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
alice, bob := setupFundingManagers(t)
|
|
|
|
defer tearDownFundingManagers(t, alice, bob)
|
|
|
|
|
|
|
|
alice.fundingMgr.cfg.Wallet.WalletController.(*mockWalletController).utxos = allCoins
|
|
|
|
|
|
|
|
// We will consume the channel updates as we go, so no
|
|
|
|
// buffering is needed.
|
|
|
|
updateChan := make(chan *lnrpc.OpenStatusUpdate)
|
|
|
|
|
|
|
|
// Initiate a fund channel, and inspect the funding tx.
|
|
|
|
pushAmt := btcutil.Amount(0)
|
|
|
|
fundingTx := fundChannel(
|
|
|
|
t, alice, bob, test.spendAmt, pushAmt, true, 1,
|
|
|
|
updateChan, true,
|
|
|
|
)
|
|
|
|
|
|
|
|
// Check whether the expected change output is present.
|
|
|
|
if test.change && len(fundingTx.TxOut) != 2 {
|
|
|
|
t.Fatalf("expected 2 outputs, had %v",
|
|
|
|
len(fundingTx.TxOut))
|
|
|
|
}
|
|
|
|
|
|
|
|
if !test.change && len(fundingTx.TxOut) != 1 {
|
|
|
|
t.Fatalf("expected 1 output, had %v",
|
|
|
|
len(fundingTx.TxOut))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Inputs should be all funds in the wallet.
|
|
|
|
if len(fundingTx.TxIn) != len(allCoins) {
|
|
|
|
t.Fatalf("Had %d inputs, expected %d",
|
|
|
|
len(fundingTx.TxIn), len(allCoins))
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, txIn := range fundingTx.TxIn {
|
|
|
|
if txIn.PreviousOutPoint != allCoins[i].OutPoint {
|
|
|
|
t.Fatalf("expected outpoint to be %v, was %v",
|
|
|
|
allCoins[i].OutPoint,
|
|
|
|
txIn.PreviousOutPoint)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-03 12:38:29 +03:00
|
|
|
|
|
|
|
// TestGetUpfrontShutdown tests different combinations of inputs for getting a
|
|
|
|
// shutdown script. It varies whether the peer has the feature set, whether
|
|
|
|
// the user has provided a script and our local configuration to test that
|
|
|
|
// GetUpfrontShutdownScript returns the expected outcome.
|
|
|
|
func TestGetUpfrontShutdownScript(t *testing.T) {
|
|
|
|
upfrontScript := []byte("upfront script")
|
|
|
|
generatedScript := []byte("generated script")
|
|
|
|
|
|
|
|
getScript := func() (lnwire.DeliveryAddress, error) {
|
|
|
|
return generatedScript, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
getScript func() (lnwire.DeliveryAddress, error)
|
|
|
|
upfrontScript lnwire.DeliveryAddress
|
|
|
|
peerEnabled bool
|
|
|
|
localEnabled bool
|
|
|
|
expectedScript lnwire.DeliveryAddress
|
|
|
|
expectedErr error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "peer disabled, no shutdown",
|
|
|
|
getScript: getScript,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "peer disabled, upfront provided",
|
|
|
|
upfrontScript: upfrontScript,
|
|
|
|
expectedErr: errUpfrontShutdownScriptNotSupported,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "peer enabled, upfront provided",
|
|
|
|
upfrontScript: upfrontScript,
|
|
|
|
peerEnabled: true,
|
|
|
|
expectedScript: upfrontScript,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "peer enabled, local disabled",
|
|
|
|
peerEnabled: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "local enabled, no upfront script",
|
|
|
|
getScript: getScript,
|
|
|
|
peerEnabled: true,
|
|
|
|
localEnabled: true,
|
|
|
|
expectedScript: generatedScript,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "local enabled, upfront script",
|
|
|
|
peerEnabled: true,
|
|
|
|
upfrontScript: upfrontScript,
|
|
|
|
localEnabled: true,
|
|
|
|
expectedScript: upfrontScript,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
test := test
|
|
|
|
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
var mockPeer testNode
|
|
|
|
|
|
|
|
// If the remote peer in the test should support upfront shutdown,
|
|
|
|
// add the feature bit.
|
|
|
|
if test.peerEnabled {
|
|
|
|
mockPeer.remoteFeatures = []lnwire.FeatureBit{
|
|
|
|
lnwire.UpfrontShutdownScriptOptional,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the command line option in config as needed.
|
|
|
|
cfg = &config{EnableUpfrontShutdown: test.localEnabled}
|
|
|
|
|
|
|
|
addr, err := getUpfrontShutdownScript(
|
|
|
|
&mockPeer, test.upfrontScript, test.getScript,
|
|
|
|
)
|
|
|
|
if err != test.expectedErr {
|
|
|
|
t.Fatalf("got: %v, expected error: %v", err, test.expectedErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.Equal(addr, test.expectedScript) {
|
|
|
|
t.Fatalf("expected address: %x, got: %x",
|
|
|
|
test.expectedScript, addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|