2019-01-24 14:28:25 +01:00
|
|
|
package lnd
|
2017-07-14 21:05:55 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-03-05 17:20:36 +01:00
|
|
|
crand "crypto/rand"
|
|
|
|
"encoding/binary"
|
|
|
|
"io"
|
2017-07-14 21:05:55 +02:00
|
|
|
"io/ioutil"
|
|
|
|
"math/rand"
|
|
|
|
"net"
|
|
|
|
"os"
|
2019-02-15 02:13:44 +01:00
|
|
|
"time"
|
2017-07-14 21:05:55 +02:00
|
|
|
|
2018-07-18 04:23:47 +02:00
|
|
|
"github.com/btcsuite/btcd/btcec"
|
|
|
|
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
|
|
|
"github.com/btcsuite/btcd/wire"
|
|
|
|
"github.com/btcsuite/btcutil"
|
2017-07-14 21:05:55 +02:00
|
|
|
"github.com/lightningnetwork/lnd/chainntnfs"
|
|
|
|
"github.com/lightningnetwork/lnd/channeldb"
|
2018-01-17 05:56:51 +01:00
|
|
|
"github.com/lightningnetwork/lnd/contractcourt"
|
2017-07-14 21:05:55 +02:00
|
|
|
"github.com/lightningnetwork/lnd/htlcswitch"
|
2019-01-16 15:47:43 +01:00
|
|
|
"github.com/lightningnetwork/lnd/input"
|
2018-02-18 00:29:01 +01:00
|
|
|
"github.com/lightningnetwork/lnd/keychain"
|
2017-07-14 21:05:55 +02:00
|
|
|
"github.com/lightningnetwork/lnd/lnwallet"
|
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
2019-02-15 02:13:44 +01:00
|
|
|
"github.com/lightningnetwork/lnd/netann"
|
2017-07-14 21:05:55 +02:00
|
|
|
"github.com/lightningnetwork/lnd/shachain"
|
2018-08-01 06:01:45 +02:00
|
|
|
"github.com/lightningnetwork/lnd/ticker"
|
2017-07-14 21:05:55 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
alicesPrivKey = []byte{
|
|
|
|
0x2b, 0xd8, 0x06, 0xc9, 0x7f, 0x0e, 0x00, 0xaf,
|
|
|
|
0x1a, 0x1f, 0xc3, 0x32, 0x8f, 0xa7, 0x63, 0xa9,
|
|
|
|
0x26, 0x97, 0x23, 0xc8, 0xdb, 0x8f, 0xac, 0x4f,
|
|
|
|
0x93, 0xaf, 0x71, 0xdb, 0x18, 0x6d, 0x6e, 0x90,
|
|
|
|
}
|
|
|
|
|
|
|
|
bobsPrivKey = []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,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use a hard-coded HD seed.
|
|
|
|
testHdSeed = [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,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Just use some arbitrary bytes as delivery script.
|
|
|
|
dummyDeliveryScript = alicesPrivKey[:]
|
2018-03-11 02:27:51 +01:00
|
|
|
|
|
|
|
// testTx is used as the default funding txn for single-funder channels.
|
|
|
|
testTx = &wire.MsgTx{
|
|
|
|
Version: 1,
|
|
|
|
TxIn: []*wire.TxIn{
|
|
|
|
{
|
|
|
|
PreviousOutPoint: wire.OutPoint{
|
|
|
|
Hash: chainhash.Hash{},
|
|
|
|
Index: 0xffffffff,
|
|
|
|
},
|
|
|
|
SignatureScript: []byte{0x04, 0x31, 0xdc, 0x00, 0x1b, 0x01, 0x62},
|
|
|
|
Sequence: 0xffffffff,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
TxOut: []*wire.TxOut{
|
|
|
|
{
|
|
|
|
Value: 5000000000,
|
|
|
|
PkScript: []byte{
|
|
|
|
0x41, // OP_DATA_65
|
|
|
|
0x04, 0xd6, 0x4b, 0xdf, 0xd0, 0x9e, 0xb1, 0xc5,
|
|
|
|
0xfe, 0x29, 0x5a, 0xbd, 0xeb, 0x1d, 0xca, 0x42,
|
|
|
|
0x81, 0xbe, 0x98, 0x8e, 0x2d, 0xa0, 0xb6, 0xc1,
|
|
|
|
0xc6, 0xa5, 0x9d, 0xc2, 0x26, 0xc2, 0x86, 0x24,
|
|
|
|
0xe1, 0x81, 0x75, 0xe8, 0x51, 0xc9, 0x6b, 0x97,
|
|
|
|
0x3d, 0x81, 0xb0, 0x1c, 0xc3, 0x1f, 0x04, 0x78,
|
|
|
|
0x34, 0xbc, 0x06, 0xd6, 0xd6, 0xed, 0xf6, 0x20,
|
|
|
|
0xd1, 0x84, 0x24, 0x1a, 0x6a, 0xed, 0x8b, 0x63,
|
|
|
|
0xa6, // 65-byte signature
|
|
|
|
0xac, // OP_CHECKSIG
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
LockTime: 5,
|
|
|
|
}
|
2017-07-14 21:05:55 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// createTestPeer creates a channel between two nodes, and returns a peer for
|
|
|
|
// one of the nodes, together with the channel seen from both nodes.
|
|
|
|
func createTestPeer(notifier chainntnfs.ChainNotifier,
|
|
|
|
publTx chan *wire.MsgTx) (*peer, *lnwallet.LightningChannel,
|
|
|
|
*lnwallet.LightningChannel, func(), error) {
|
|
|
|
|
|
|
|
aliceKeyPriv, aliceKeyPub := btcec.PrivKeyFromBytes(btcec.S256(),
|
|
|
|
alicesPrivKey)
|
|
|
|
bobKeyPriv, bobKeyPub := btcec.PrivKeyFromBytes(btcec.S256(),
|
|
|
|
bobsPrivKey)
|
|
|
|
|
|
|
|
channelCapacity := btcutil.Amount(10 * 1e8)
|
|
|
|
channelBal := channelCapacity / 2
|
|
|
|
aliceDustLimit := btcutil.Amount(200)
|
|
|
|
bobDustLimit := btcutil.Amount(1300)
|
|
|
|
csvTimeoutAlice := uint32(5)
|
|
|
|
csvTimeoutBob := uint32(4)
|
|
|
|
|
|
|
|
prevOut := &wire.OutPoint{
|
|
|
|
Hash: chainhash.Hash(testHdSeed),
|
|
|
|
Index: 0,
|
|
|
|
}
|
|
|
|
fundingTxIn := wire.NewTxIn(prevOut, nil, nil)
|
|
|
|
|
|
|
|
aliceCfg := channeldb.ChannelConfig{
|
|
|
|
ChannelConstraints: channeldb.ChannelConstraints{
|
|
|
|
DustLimit: aliceDustLimit,
|
2017-08-22 08:25:41 +02:00
|
|
|
MaxPendingAmount: lnwire.MilliSatoshi(rand.Int63()),
|
2017-07-14 21:05:55 +02:00
|
|
|
ChanReserve: btcutil.Amount(rand.Int63()),
|
2017-08-22 08:25:41 +02:00
|
|
|
MinHTLC: lnwire.MilliSatoshi(rand.Int63()),
|
2017-07-14 21:05:55 +02:00
|
|
|
MaxAcceptedHtlcs: uint16(rand.Int31()),
|
2018-12-10 22:56:41 +01:00
|
|
|
CsvDelay: uint16(csvTimeoutAlice),
|
2017-07-14 21:05:55 +02:00
|
|
|
},
|
2018-02-18 00:29:01 +01:00
|
|
|
MultiSigKey: keychain.KeyDescriptor{
|
|
|
|
PubKey: aliceKeyPub,
|
|
|
|
},
|
|
|
|
RevocationBasePoint: keychain.KeyDescriptor{
|
|
|
|
PubKey: aliceKeyPub,
|
|
|
|
},
|
|
|
|
PaymentBasePoint: keychain.KeyDescriptor{
|
|
|
|
PubKey: aliceKeyPub,
|
|
|
|
},
|
|
|
|
DelayBasePoint: keychain.KeyDescriptor{
|
|
|
|
PubKey: aliceKeyPub,
|
|
|
|
},
|
|
|
|
HtlcBasePoint: keychain.KeyDescriptor{
|
|
|
|
PubKey: aliceKeyPub,
|
|
|
|
},
|
2017-07-14 21:05:55 +02:00
|
|
|
}
|
|
|
|
bobCfg := channeldb.ChannelConfig{
|
|
|
|
ChannelConstraints: channeldb.ChannelConstraints{
|
|
|
|
DustLimit: bobDustLimit,
|
2017-08-22 08:25:41 +02:00
|
|
|
MaxPendingAmount: lnwire.MilliSatoshi(rand.Int63()),
|
2017-07-14 21:05:55 +02:00
|
|
|
ChanReserve: btcutil.Amount(rand.Int63()),
|
2017-08-22 08:25:41 +02:00
|
|
|
MinHTLC: lnwire.MilliSatoshi(rand.Int63()),
|
2017-07-14 21:05:55 +02:00
|
|
|
MaxAcceptedHtlcs: uint16(rand.Int31()),
|
2018-12-10 22:56:41 +01:00
|
|
|
CsvDelay: uint16(csvTimeoutBob),
|
2017-07-14 21:05:55 +02:00
|
|
|
},
|
2018-02-18 00:29:01 +01:00
|
|
|
MultiSigKey: keychain.KeyDescriptor{
|
|
|
|
PubKey: bobKeyPub,
|
|
|
|
},
|
|
|
|
RevocationBasePoint: keychain.KeyDescriptor{
|
|
|
|
PubKey: bobKeyPub,
|
|
|
|
},
|
|
|
|
PaymentBasePoint: keychain.KeyDescriptor{
|
|
|
|
PubKey: bobKeyPub,
|
|
|
|
},
|
|
|
|
DelayBasePoint: keychain.KeyDescriptor{
|
|
|
|
PubKey: bobKeyPub,
|
|
|
|
},
|
|
|
|
HtlcBasePoint: keychain.KeyDescriptor{
|
|
|
|
PubKey: bobKeyPub,
|
|
|
|
},
|
2017-07-14 21:05:55 +02:00
|
|
|
}
|
|
|
|
|
2018-02-18 00:29:01 +01:00
|
|
|
bobRoot, err := chainhash.NewHash(bobKeyPriv.Serialize())
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, nil, err
|
|
|
|
}
|
|
|
|
bobPreimageProducer := shachain.NewRevocationProducer(*bobRoot)
|
2017-07-14 21:05:55 +02:00
|
|
|
bobFirstRevoke, err := bobPreimageProducer.AtIndex(0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, nil, err
|
|
|
|
}
|
2019-01-16 15:47:43 +01:00
|
|
|
bobCommitPoint := input.ComputeCommitmentPoint(bobFirstRevoke[:])
|
2017-07-14 21:05:55 +02:00
|
|
|
|
2018-02-18 00:29:01 +01:00
|
|
|
aliceRoot, err := chainhash.NewHash(aliceKeyPriv.Serialize())
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, nil, err
|
|
|
|
}
|
|
|
|
alicePreimageProducer := shachain.NewRevocationProducer(*aliceRoot)
|
2017-07-14 21:05:55 +02:00
|
|
|
aliceFirstRevoke, err := alicePreimageProducer.AtIndex(0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, nil, err
|
|
|
|
}
|
2019-01-16 15:47:43 +01:00
|
|
|
aliceCommitPoint := input.ComputeCommitmentPoint(aliceFirstRevoke[:])
|
2017-07-14 21:05:55 +02:00
|
|
|
|
|
|
|
aliceCommitTx, bobCommitTx, err := lnwallet.CreateCommitmentTxns(channelBal,
|
|
|
|
channelBal, &aliceCfg, &bobCfg, aliceCommitPoint, bobCommitPoint,
|
2017-12-22 19:26:16 +01:00
|
|
|
*fundingTxIn)
|
2017-07-14 21:05:55 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
alicePath, err := ioutil.TempDir("", "alicedb")
|
|
|
|
dbAlice, err := channeldb.Open(alicePath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
bobPath, err := ioutil.TempDir("", "bobdb")
|
|
|
|
dbBob, err := channeldb.Open(bobPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, nil, err
|
|
|
|
}
|
|
|
|
|
2018-12-18 09:02:27 +01:00
|
|
|
estimator := lnwallet.NewStaticFeeEstimator(12500, 0)
|
2018-07-28 03:20:58 +02:00
|
|
|
feePerKw, err := estimator.EstimateFeePerKW(1)
|
2017-11-23 20:41:20 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, nil, err
|
|
|
|
}
|
|
|
|
|
2017-11-11 00:24:29 +01:00
|
|
|
// TODO(roasbeef): need to factor in commit fee?
|
|
|
|
aliceCommit := channeldb.ChannelCommitment{
|
|
|
|
CommitHeight: 0,
|
|
|
|
LocalBalance: lnwire.NewMSatFromSatoshis(channelBal),
|
|
|
|
RemoteBalance: lnwire.NewMSatFromSatoshis(channelBal),
|
2018-02-13 15:15:14 +01:00
|
|
|
FeePerKw: btcutil.Amount(feePerKw),
|
2019-01-16 15:47:43 +01:00
|
|
|
CommitFee: feePerKw.FeeForWeight(input.CommitWeight),
|
2017-11-11 00:24:29 +01:00
|
|
|
CommitTx: aliceCommitTx,
|
|
|
|
CommitSig: bytes.Repeat([]byte{1}, 71),
|
|
|
|
}
|
|
|
|
bobCommit := channeldb.ChannelCommitment{
|
|
|
|
CommitHeight: 0,
|
|
|
|
LocalBalance: lnwire.NewMSatFromSatoshis(channelBal),
|
|
|
|
RemoteBalance: lnwire.NewMSatFromSatoshis(channelBal),
|
2018-02-13 15:15:14 +01:00
|
|
|
FeePerKw: btcutil.Amount(feePerKw),
|
2019-01-16 15:47:43 +01:00
|
|
|
CommitFee: feePerKw.FeeForWeight(input.CommitWeight),
|
2017-11-11 00:24:29 +01:00
|
|
|
CommitTx: bobCommitTx,
|
|
|
|
CommitSig: bytes.Repeat([]byte{1}, 71),
|
|
|
|
}
|
|
|
|
|
2018-03-05 17:20:36 +01:00
|
|
|
var chanIDBytes [8]byte
|
|
|
|
if _, err := io.ReadFull(crand.Reader, chanIDBytes[:]); err != nil {
|
|
|
|
return nil, nil, nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
shortChanID := lnwire.NewShortChanIDFromInt(
|
|
|
|
binary.BigEndian.Uint64(chanIDBytes[:]),
|
|
|
|
)
|
|
|
|
|
2017-07-14 21:05:55 +02:00
|
|
|
aliceChannelState := &channeldb.OpenChannel{
|
|
|
|
LocalChanCfg: aliceCfg,
|
|
|
|
RemoteChanCfg: bobCfg,
|
|
|
|
IdentityPub: aliceKeyPub,
|
|
|
|
FundingOutpoint: *prevOut,
|
2018-05-02 01:27:20 +02:00
|
|
|
ShortChannelID: shortChanID,
|
2017-07-14 21:05:55 +02:00
|
|
|
ChanType: channeldb.SingleFunder,
|
|
|
|
IsInitiator: true,
|
|
|
|
Capacity: channelCapacity,
|
|
|
|
RemoteCurrentRevocation: bobCommitPoint,
|
|
|
|
RevocationProducer: alicePreimageProducer,
|
|
|
|
RevocationStore: shachain.NewRevocationStore(),
|
2017-11-11 00:24:29 +01:00
|
|
|
LocalCommitment: aliceCommit,
|
|
|
|
RemoteCommitment: aliceCommit,
|
2017-07-14 21:05:55 +02:00
|
|
|
Db: dbAlice,
|
2018-03-05 17:20:36 +01:00
|
|
|
Packager: channeldb.NewChannelPackager(shortChanID),
|
2018-03-11 02:27:51 +01:00
|
|
|
FundingTxn: testTx,
|
2017-07-14 21:05:55 +02:00
|
|
|
}
|
|
|
|
bobChannelState := &channeldb.OpenChannel{
|
|
|
|
LocalChanCfg: bobCfg,
|
|
|
|
RemoteChanCfg: aliceCfg,
|
|
|
|
IdentityPub: bobKeyPub,
|
|
|
|
FundingOutpoint: *prevOut,
|
|
|
|
ChanType: channeldb.SingleFunder,
|
|
|
|
IsInitiator: false,
|
|
|
|
Capacity: channelCapacity,
|
|
|
|
RemoteCurrentRevocation: aliceCommitPoint,
|
|
|
|
RevocationProducer: bobPreimageProducer,
|
|
|
|
RevocationStore: shachain.NewRevocationStore(),
|
2017-11-11 00:24:29 +01:00
|
|
|
LocalCommitment: bobCommit,
|
|
|
|
RemoteCommitment: bobCommit,
|
2017-07-14 21:05:55 +02:00
|
|
|
Db: dbBob,
|
2018-03-05 17:20:36 +01:00
|
|
|
Packager: channeldb.NewChannelPackager(shortChanID),
|
2017-07-14 21:05:55 +02:00
|
|
|
}
|
|
|
|
|
2018-09-06 03:22:29 +02:00
|
|
|
aliceAddr := &net.TCPAddr{
|
2017-11-11 00:24:29 +01:00
|
|
|
IP: net.ParseIP("127.0.0.1"),
|
|
|
|
Port: 18555,
|
|
|
|
}
|
|
|
|
|
2018-09-06 03:22:29 +02:00
|
|
|
if err := aliceChannelState.SyncPending(aliceAddr, 0); err != nil {
|
2017-11-11 00:24:29 +01:00
|
|
|
return nil, nil, nil, nil, err
|
|
|
|
}
|
|
|
|
|
2018-09-06 03:22:29 +02:00
|
|
|
bobAddr := &net.TCPAddr{
|
2017-07-14 21:05:55 +02:00
|
|
|
IP: net.ParseIP("127.0.0.1"),
|
|
|
|
Port: 18556,
|
|
|
|
}
|
|
|
|
|
2018-09-06 03:22:29 +02:00
|
|
|
if err := bobChannelState.SyncPending(bobAddr, 0); err != nil {
|
2017-07-14 21:05:55 +02:00
|
|
|
return nil, nil, nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanUpFunc := func() {
|
|
|
|
os.RemoveAll(bobPath)
|
|
|
|
os.RemoveAll(alicePath)
|
|
|
|
}
|
|
|
|
|
|
|
|
aliceSigner := &mockSigner{aliceKeyPriv}
|
|
|
|
bobSigner := &mockSigner{bobKeyPriv}
|
|
|
|
|
multi: replace per channel sigPool with global daemon level sigPool
In this commit, we remove the per channel `sigPool` within the
`lnwallet.LightningChannel` struct. With this change, we ensure that as
the number of channels grows, the number of gouroutines idling in the
sigPool stays constant. It's the case that currently on the daemon, most
channels are likely inactive, with only a hand full actually
consistently carrying out channel updates. As a result, this change
should reduce the amount of idle CPU usage, as we have less active
goroutines in select loops.
In order to make this change, the `SigPool` itself has been publicly
exported such that outside callers can make a `SigPool` and pass it into
newly created channels. Since the sig pool now lives outside the
channel, we were also able to do away with the Stop() method on the
channel all together.
Finally, the server is the sub-system that is currently responsible for
managing the `SigPool` within lnd.
2018-12-15 01:35:07 +01:00
|
|
|
alicePool := lnwallet.NewSigPool(1, aliceSigner)
|
2018-01-17 05:56:51 +01:00
|
|
|
channelAlice, err := lnwallet.NewLightningChannel(
|
2019-04-15 14:24:43 +02:00
|
|
|
aliceSigner, aliceChannelState, alicePool,
|
2018-01-17 05:56:51 +01:00
|
|
|
)
|
2017-07-14 21:05:55 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, nil, err
|
|
|
|
}
|
multi: replace per channel sigPool with global daemon level sigPool
In this commit, we remove the per channel `sigPool` within the
`lnwallet.LightningChannel` struct. With this change, we ensure that as
the number of channels grows, the number of gouroutines idling in the
sigPool stays constant. It's the case that currently on the daemon, most
channels are likely inactive, with only a hand full actually
consistently carrying out channel updates. As a result, this change
should reduce the amount of idle CPU usage, as we have less active
goroutines in select loops.
In order to make this change, the `SigPool` itself has been publicly
exported such that outside callers can make a `SigPool` and pass it into
newly created channels. Since the sig pool now lives outside the
channel, we were also able to do away with the Stop() method on the
channel all together.
Finally, the server is the sub-system that is currently responsible for
managing the `SigPool` within lnd.
2018-12-15 01:35:07 +01:00
|
|
|
alicePool.Start()
|
|
|
|
|
|
|
|
bobPool := lnwallet.NewSigPool(1, bobSigner)
|
2018-01-17 05:56:51 +01:00
|
|
|
channelBob, err := lnwallet.NewLightningChannel(
|
2019-04-15 14:24:43 +02:00
|
|
|
bobSigner, bobChannelState, bobPool,
|
2018-01-17 05:56:51 +01:00
|
|
|
)
|
2017-07-14 21:05:55 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, nil, err
|
|
|
|
}
|
multi: replace per channel sigPool with global daemon level sigPool
In this commit, we remove the per channel `sigPool` within the
`lnwallet.LightningChannel` struct. With this change, we ensure that as
the number of channels grows, the number of gouroutines idling in the
sigPool stays constant. It's the case that currently on the daemon, most
channels are likely inactive, with only a hand full actually
consistently carrying out channel updates. As a result, this change
should reduce the amount of idle CPU usage, as we have less active
goroutines in select loops.
In order to make this change, the `SigPool` itself has been publicly
exported such that outside callers can make a `SigPool` and pass it into
newly created channels. Since the sig pool now lives outside the
channel, we were also able to do away with the Stop() method on the
channel all together.
Finally, the server is the sub-system that is currently responsible for
managing the `SigPool` within lnd.
2018-12-15 01:35:07 +01:00
|
|
|
bobPool.Start()
|
2017-07-14 21:05:55 +02:00
|
|
|
|
2018-10-23 12:08:03 +02:00
|
|
|
chainIO := &mockChainIO{
|
|
|
|
bestHeight: fundingBroadcastHeight,
|
|
|
|
}
|
2017-07-14 21:05:55 +02:00
|
|
|
wallet := &lnwallet.LightningWallet{
|
|
|
|
WalletController: &mockWalletController{
|
|
|
|
rootKey: aliceKeyPriv,
|
|
|
|
publishedTransactions: publTx,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
cc := &chainControl{
|
|
|
|
feeEstimator: estimator,
|
|
|
|
chainIO: chainIO,
|
|
|
|
chainNotifier: notifier,
|
|
|
|
wallet: wallet,
|
|
|
|
}
|
|
|
|
|
2018-01-21 05:27:25 +01:00
|
|
|
breachArbiter := &breachArbiter{}
|
2017-07-14 21:05:55 +02:00
|
|
|
|
2018-01-17 05:56:51 +01:00
|
|
|
chainArb := contractcourt.NewChainArbitrator(
|
breacharbiter: properly account for second-level spends during breach remedy
In this commit, we address an un accounted for case during the breach
remedy process. If the remote node actually went directly to the second
layer during a channel breach attempt, then we wouldn’t properly be
able to sweep with out justice transaction, as some HTLC inputs may
actually be spent at that point.
In order to address this case, we’ll now catch the transaction
rejection, then check to see which input was spent, promote that to a
second level spend, and repeat as necessary. At the end of this loop,
any inputs which have been spent to the second level will have had the
prevouts and witnesses updated.
In order to perform this transition, we now also store the second level
witness script in the database. This allow us to modify the sign desc
with the proper input value, as well as witness script.
2018-01-23 02:11:02 +01:00
|
|
|
contractcourt.ChainArbitratorConfig{
|
|
|
|
Notifier: notifier,
|
|
|
|
ChainIO: chainIO,
|
|
|
|
}, dbAlice,
|
2018-01-17 05:56:51 +01:00
|
|
|
)
|
breacharbiter: properly account for second-level spends during breach remedy
In this commit, we address an un accounted for case during the breach
remedy process. If the remote node actually went directly to the second
layer during a channel breach attempt, then we wouldn’t properly be
able to sweep with out justice transaction, as some HTLC inputs may
actually be spent at that point.
In order to address this case, we’ll now catch the transaction
rejection, then check to see which input was spent, promote that to a
second level spend, and repeat as necessary. At the end of this loop,
any inputs which have been spent to the second level will have had the
prevouts and witnesses updated.
In order to perform this transition, we now also store the second level
witness script in the database. This allow us to modify the sign desc
with the proper input value, as well as witness script.
2018-01-23 02:11:02 +01:00
|
|
|
chainArb.WatchNewChannel(aliceChannelState)
|
2018-01-17 05:56:51 +01:00
|
|
|
|
2017-07-14 21:05:55 +02:00
|
|
|
s := &server{
|
|
|
|
chanDB: dbAlice,
|
|
|
|
cc: cc,
|
|
|
|
breachArbiter: breachArbiter,
|
2018-01-17 05:56:51 +01:00
|
|
|
chainArb: chainArb,
|
2017-07-14 21:05:55 +02:00
|
|
|
}
|
2018-06-01 05:31:40 +02:00
|
|
|
|
|
|
|
_, currentHeight, err := s.cc.chainIO.GetBestBlock()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, nil, err
|
|
|
|
}
|
|
|
|
|
2018-01-18 02:15:41 +01:00
|
|
|
htlcSwitch, err := htlcswitch.New(htlcswitch.Config{
|
2018-03-05 17:20:36 +01:00
|
|
|
DB: dbAlice,
|
|
|
|
SwitchPackager: channeldb.NewSwitchPackager(),
|
2018-06-01 05:31:40 +02:00
|
|
|
Notifier: notifier,
|
2018-08-01 06:01:45 +02:00
|
|
|
FwdEventTicker: ticker.New(
|
|
|
|
htlcswitch.DefaultFwdEventInterval),
|
|
|
|
LogEventTicker: ticker.New(
|
|
|
|
htlcswitch.DefaultLogInterval),
|
2019-05-30 18:26:24 +02:00
|
|
|
AckEventTicker: ticker.New(
|
|
|
|
htlcswitch.DefaultAckInterval),
|
2018-06-01 05:31:40 +02:00
|
|
|
}, uint32(currentHeight))
|
2018-01-18 02:15:41 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, nil, err
|
|
|
|
}
|
2019-02-15 02:13:44 +01:00
|
|
|
if err = htlcSwitch.Start(); err != nil {
|
|
|
|
return nil, nil, nil, nil, err
|
|
|
|
}
|
2018-01-18 02:15:41 +01:00
|
|
|
s.htlcSwitch = htlcSwitch
|
2019-02-15 02:13:44 +01:00
|
|
|
|
|
|
|
nodeSignerAlice := netann.NewNodeSigner(aliceKeyPriv)
|
|
|
|
|
|
|
|
const chanActiveTimeout = time.Minute
|
|
|
|
|
|
|
|
chanStatusMgr, err := netann.NewChanStatusManager(&netann.ChanStatusConfig{
|
|
|
|
ChanStatusSampleInterval: 30 * time.Second,
|
|
|
|
ChanEnableTimeout: chanActiveTimeout,
|
|
|
|
ChanDisableTimeout: 2 * time.Minute,
|
|
|
|
DB: dbAlice,
|
|
|
|
Graph: dbAlice.ChannelGraph(),
|
|
|
|
MessageSigner: nodeSignerAlice,
|
|
|
|
OurPubKey: aliceKeyPub,
|
|
|
|
IsChannelActive: s.htlcSwitch.HasActiveLink,
|
|
|
|
ApplyChannelUpdate: func(*lnwire.ChannelUpdate) error { return nil },
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, nil, err
|
|
|
|
}
|
2019-03-13 01:34:57 +01:00
|
|
|
if err = chanStatusMgr.Start(); err != nil {
|
|
|
|
return nil, nil, nil, nil, err
|
|
|
|
}
|
2019-02-15 02:13:44 +01:00
|
|
|
s.chanStatusMgr = chanStatusMgr
|
2017-07-14 21:05:55 +02:00
|
|
|
|
|
|
|
alicePeer := &peer{
|
2018-09-06 03:22:29 +02:00
|
|
|
addr: &lnwire.NetAddress{
|
|
|
|
IdentityKey: aliceKeyPub,
|
|
|
|
Address: aliceAddr,
|
|
|
|
},
|
|
|
|
|
2017-07-14 21:05:55 +02:00
|
|
|
server: s,
|
2018-02-07 04:11:11 +01:00
|
|
|
sendQueue: make(chan outgoingMsg, 1),
|
|
|
|
outgoingQueue: make(chan outgoingMsg, outgoingQueueLen),
|
2017-07-14 21:05:55 +02:00
|
|
|
|
|
|
|
activeChannels: make(map[lnwire.ChannelID]*lnwallet.LightningChannel),
|
|
|
|
newChannels: make(chan *newChannelMsg, 1),
|
|
|
|
|
2017-11-23 20:41:20 +01:00
|
|
|
activeChanCloses: make(map[lnwire.ChannelID]*channelCloser),
|
|
|
|
localCloseChanReqs: make(chan *htlcswitch.ChanClose),
|
|
|
|
chanCloseMsgs: make(chan *closeMsg),
|
2017-07-14 21:05:55 +02:00
|
|
|
|
2019-02-15 02:13:44 +01:00
|
|
|
chanActiveTimeout: chanActiveTimeout,
|
|
|
|
|
2017-07-14 21:05:55 +02:00
|
|
|
queueQuit: make(chan struct{}),
|
|
|
|
quit: make(chan struct{}),
|
|
|
|
}
|
|
|
|
|
|
|
|
chanID := lnwire.NewChanIDFromOutPoint(channelAlice.ChannelPoint())
|
|
|
|
alicePeer.activeChannels[chanID] = channelAlice
|
|
|
|
|
2019-02-15 02:13:33 +01:00
|
|
|
alicePeer.wg.Add(1)
|
2017-07-14 21:05:55 +02:00
|
|
|
go alicePeer.channelManager()
|
|
|
|
|
|
|
|
return alicePeer, channelAlice, channelBob, cleanUpFunc, nil
|
|
|
|
}
|