2015-12-20 09:00:50 +03:00
|
|
|
package lnwallet
|
2015-11-19 01:59:07 +03:00
|
|
|
|
2015-11-27 09:53:38 +03:00
|
|
|
import (
|
2015-12-16 01:19:17 +03:00
|
|
|
"bytes"
|
2016-02-03 10:59:27 +03:00
|
|
|
"fmt"
|
2015-11-27 09:53:38 +03:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2016-03-24 10:01:35 +03:00
|
|
|
"path/filepath"
|
2015-11-27 09:53:38 +03:00
|
|
|
"testing"
|
|
|
|
"time"
|
2015-11-19 01:59:07 +03:00
|
|
|
|
2016-03-24 10:01:35 +03:00
|
|
|
"github.com/lightningnetwork/lnd/channeldb"
|
2016-05-15 17:17:44 +03:00
|
|
|
"github.com/roasbeef/btcd/chaincfg"
|
2016-02-03 10:59:27 +03:00
|
|
|
|
2016-02-03 23:05:53 +03:00
|
|
|
"github.com/Roasbeef/btcd/rpctest"
|
2016-05-15 17:17:44 +03:00
|
|
|
"github.com/roasbeef/btcd/btcec"
|
|
|
|
"github.com/roasbeef/btcd/txscript"
|
|
|
|
"github.com/roasbeef/btcd/wire"
|
|
|
|
"github.com/roasbeef/btcutil"
|
|
|
|
"github.com/roasbeef/btcutil/coinset"
|
|
|
|
"github.com/roasbeef/btcwallet/waddrmgr"
|
2015-11-27 09:53:38 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
privPass = []byte("private-test")
|
|
|
|
|
|
|
|
// For simplicity a single priv key controls all of our test outputs.
|
|
|
|
testWalletPrivKey = []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,
|
|
|
|
}
|
|
|
|
|
|
|
|
// We're alice :)
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
2015-12-24 21:42:29 +03:00
|
|
|
// Use a hard-coded HD seed.
|
2015-12-26 09:05:07 +03:00
|
|
|
testHdSeed = [32]byte{
|
2015-11-27 09:53:38 +03:00
|
|
|
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,
|
|
|
|
}
|
2015-12-23 07:32:18 +03:00
|
|
|
zeroHash = bytes.Repeat([]byte{0}, 32)
|
2015-11-27 09:53:38 +03:00
|
|
|
)
|
|
|
|
|
2015-12-28 23:14:26 +03:00
|
|
|
// assertProperBalance asserts than the total value of the unspent outputs
|
|
|
|
// within the wallet are *exactly* amount. If unable to retrieve the current
|
|
|
|
// balance, or the assertion fails, the test will halt with a fatal error.
|
2015-12-16 22:22:36 +03:00
|
|
|
func assertProperBalance(t *testing.T, lw *LightningWallet, numConfirms, amount int32) {
|
2016-02-03 10:59:27 +03:00
|
|
|
balance, err := lw.CalculateBalance(1)
|
2015-12-16 22:22:36 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to query for balance: %v", err)
|
|
|
|
}
|
2015-12-28 23:14:26 +03:00
|
|
|
if balance != btcutil.Amount(amount*1e8) {
|
2015-12-16 22:22:36 +03:00
|
|
|
t.Fatalf("wallet credits not properly loaded, should have 20BTC, "+
|
|
|
|
"instead have %v", balance)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-27 09:53:38 +03:00
|
|
|
// bobNode represents the other party involved as a node within LN. Bob is our
|
|
|
|
// only "default-route", we have a direct connection with him.
|
|
|
|
type bobNode struct {
|
2015-12-22 00:55:17 +03:00
|
|
|
privKey *btcec.PrivateKey
|
|
|
|
|
|
|
|
// For simplicity, used for both the commit tx and the multi-sig output.
|
|
|
|
channelKey *btcec.PublicKey
|
|
|
|
deliveryAddress btcutil.Address
|
2016-01-07 02:12:06 +03:00
|
|
|
revocation [20]byte
|
2015-12-26 09:05:07 +03:00
|
|
|
delay uint32
|
|
|
|
id [wire.HashSize]byte
|
2015-11-27 09:53:38 +03:00
|
|
|
|
|
|
|
availableOutputs []*wire.TxIn
|
|
|
|
changeOutputs []*wire.TxOut
|
|
|
|
}
|
|
|
|
|
2015-12-28 23:16:20 +03:00
|
|
|
// Contribution returns bobNode's contribution necessary to open a payment
|
|
|
|
// channel with Alice.
|
2015-12-23 07:32:18 +03:00
|
|
|
func (b *bobNode) Contribution() *ChannelContribution {
|
|
|
|
return &ChannelContribution{
|
|
|
|
Inputs: b.availableOutputs,
|
|
|
|
ChangeOutputs: b.changeOutputs,
|
|
|
|
MultiSigKey: b.channelKey,
|
|
|
|
CommitKey: b.channelKey,
|
|
|
|
DeliveryAddress: b.deliveryAddress,
|
|
|
|
RevocationHash: b.revocation,
|
|
|
|
CsvDelay: b.delay,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-27 09:53:38 +03:00
|
|
|
// signFundingTx generates signatures for all the inputs in the funding tx
|
|
|
|
// belonging to Bob.
|
2016-05-04 05:49:58 +03:00
|
|
|
// NOTE: This generates the full witness stack.
|
|
|
|
func (b *bobNode) signFundingTx(fundingTx *wire.MsgTx) ([]*InputScript, error) {
|
|
|
|
bobInputScripts := make([]*InputScript, 0, len(b.availableOutputs))
|
2015-11-27 09:53:38 +03:00
|
|
|
bobPkScript := b.changeOutputs[0].PkScript
|
2016-05-04 05:49:58 +03:00
|
|
|
|
|
|
|
inputValue := int64(7e8)
|
|
|
|
hashCache := txscript.NewTxSigHashes(fundingTx)
|
2015-11-27 09:53:38 +03:00
|
|
|
for i, _ := range fundingTx.TxIn {
|
2016-05-04 05:49:58 +03:00
|
|
|
// Alice has already signed this input.
|
|
|
|
if fundingTx.TxIn[i].Witness != nil {
|
2015-11-27 09:53:38 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-05-04 05:49:58 +03:00
|
|
|
witness, err := txscript.WitnessScript(fundingTx, hashCache, i,
|
|
|
|
inputValue, bobPkScript, txscript.SigHashAll, b.privKey,
|
2015-11-27 09:53:38 +03:00
|
|
|
true)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-05-04 05:49:58 +03:00
|
|
|
inputScript := &InputScript{Witness: witness}
|
|
|
|
bobInputScripts = append(bobInputScripts, inputScript)
|
2015-11-27 09:53:38 +03:00
|
|
|
}
|
|
|
|
|
2016-05-04 05:49:58 +03:00
|
|
|
return bobInputScripts, nil
|
2015-11-27 09:53:38 +03:00
|
|
|
}
|
|
|
|
|
2016-02-03 10:59:27 +03:00
|
|
|
// signCommitTx generates a raw signature required for generating a spend from
|
2015-12-28 23:15:46 +03:00
|
|
|
// the funding transaction.
|
2016-05-04 05:49:58 +03:00
|
|
|
func (b *bobNode) signCommitTx(commitTx *wire.MsgTx, fundingScript []byte,
|
|
|
|
channelValue int64) ([]byte, error) {
|
|
|
|
|
|
|
|
hashCache := txscript.NewTxSigHashes(commitTx)
|
|
|
|
|
|
|
|
return txscript.RawTxInWitnessSignature(commitTx, hashCache, 0,
|
|
|
|
channelValue, fundingScript, txscript.SigHashAll, b.privKey)
|
2015-12-28 23:15:46 +03:00
|
|
|
}
|
|
|
|
|
2015-11-27 09:53:38 +03:00
|
|
|
// newBobNode generates a test "ln node" to interact with Alice (us). For the
|
|
|
|
// funding transaction, bob has a single output totaling 7BTC. For our basic
|
|
|
|
// test, he'll fund the channel with 5BTC, leaving 2BTC to the change output.
|
|
|
|
// TODO(roasbeef): proper handling of change etc.
|
2016-02-03 10:59:27 +03:00
|
|
|
func newBobNode(miner *rpctest.Harness) (*bobNode, error) {
|
2015-11-27 09:53:38 +03:00
|
|
|
// First, parse Bob's priv key in order to obtain a key he'll use for the
|
|
|
|
// multi-sig funding transaction.
|
|
|
|
privKey, pubKey := btcec.PrivKeyFromBytes(btcec.S256(), bobsPrivKey)
|
|
|
|
|
|
|
|
// Next, generate an output redeemable by bob.
|
2016-05-04 05:49:58 +03:00
|
|
|
pkHash := btcutil.Hash160(pubKey.SerializeCompressed())
|
|
|
|
bobAddr, err := btcutil.NewAddressWitnessPubKeyHash(
|
|
|
|
pkHash,
|
2016-02-03 10:59:27 +03:00
|
|
|
miner.ActiveNet)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
bobAddrScript, err := txscript.PayToAddrScript(bobAddr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Give bobNode one 7 BTC output for use in creating channels.
|
2016-03-24 10:01:35 +03:00
|
|
|
output := &wire.TxOut{7e8, bobAddrScript}
|
|
|
|
mainTxid, err := miner.CoinbaseSpend([]*wire.TxOut{output})
|
2015-11-27 09:53:38 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-02-03 10:59:27 +03:00
|
|
|
|
|
|
|
// Mine a block in order to include the above output in a block. During
|
|
|
|
// the reservation workflow, we currently test to ensure that the funding
|
|
|
|
// output we're given actually exists.
|
|
|
|
if _, err := miner.Node.Generate(1); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Grab the transaction in order to locate the output index to Bob.
|
|
|
|
tx, err := miner.Node.GetRawTransaction(mainTxid)
|
2015-11-27 09:53:38 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-02-03 10:59:27 +03:00
|
|
|
found, index := findScriptOutputIndex(tx.MsgTx(), bobAddrScript)
|
|
|
|
if !found {
|
|
|
|
return nil, fmt.Errorf("output to bob never created")
|
|
|
|
}
|
|
|
|
|
|
|
|
prevOut := wire.NewOutPoint(mainTxid, index)
|
2016-03-24 10:01:35 +03:00
|
|
|
bobTxIn := wire.NewTxIn(prevOut, nil, nil)
|
2015-11-27 09:53:38 +03:00
|
|
|
|
2016-02-03 10:59:27 +03:00
|
|
|
// Using bobs priv key above, create a change output he can spend.
|
2015-11-27 09:53:38 +03:00
|
|
|
bobChangeOutput := wire.NewTxOut(2*1e8, bobAddrScript)
|
|
|
|
|
2015-12-22 00:55:17 +03:00
|
|
|
// Bob's initial revocation hash is just his private key with the first
|
|
|
|
// byte changed...
|
2016-01-07 02:12:06 +03:00
|
|
|
var revocation [20]byte
|
2015-12-22 00:55:17 +03:00
|
|
|
copy(revocation[:], bobsPrivKey)
|
|
|
|
revocation[0] = 0xff
|
|
|
|
|
2015-12-26 09:05:07 +03:00
|
|
|
// His ID is just as creative...
|
|
|
|
var id [wire.HashSize]byte
|
|
|
|
id[0] = 0xff
|
|
|
|
|
2015-11-27 09:53:38 +03:00
|
|
|
return &bobNode{
|
2015-12-26 09:05:07 +03:00
|
|
|
id: id,
|
2015-11-27 09:53:38 +03:00
|
|
|
privKey: privKey,
|
2015-12-22 00:55:17 +03:00
|
|
|
channelKey: pubKey,
|
|
|
|
deliveryAddress: bobAddr,
|
|
|
|
revocation: revocation,
|
|
|
|
delay: 5,
|
2015-11-27 09:53:38 +03:00
|
|
|
availableOutputs: []*wire.TxIn{bobTxIn},
|
|
|
|
changeOutputs: []*wire.TxOut{bobChangeOutput},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2016-02-03 10:59:27 +03:00
|
|
|
func loadTestCredits(miner *rpctest.Harness, w *LightningWallet, numOutputs, btcPerOutput int) error {
|
|
|
|
// Using the mining node, spend from a coinbase output numOutputs to
|
|
|
|
// give us btcPerOutput with each output.
|
2016-03-24 10:01:35 +03:00
|
|
|
satoshiPerOutput := int64(btcPerOutput * 1e8)
|
2016-02-03 10:59:27 +03:00
|
|
|
addrs := make([]btcutil.Address, 0, numOutputs)
|
|
|
|
for i := 0; i < numOutputs; i++ {
|
|
|
|
// Grab a fresh address from the wallet to house this output.
|
2016-05-04 05:49:58 +03:00
|
|
|
walletAddr, err := w.NewAddress(waddrmgr.DefaultAccountNum, waddrmgr.WitnessPubKey)
|
2015-11-27 09:53:38 +03:00
|
|
|
if err != nil {
|
2016-02-03 10:59:27 +03:00
|
|
|
return err
|
2015-11-27 09:53:38 +03:00
|
|
|
}
|
|
|
|
|
2016-03-24 10:01:35 +03:00
|
|
|
script, err := txscript.PayToAddrScript(walletAddr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-02-03 10:59:27 +03:00
|
|
|
addrs = append(addrs, walletAddr)
|
|
|
|
|
2016-03-24 10:01:35 +03:00
|
|
|
output := &wire.TxOut{satoshiPerOutput, script}
|
|
|
|
if _, err := miner.CoinbaseSpend([]*wire.TxOut{output}); err != nil {
|
2016-02-03 10:59:27 +03:00
|
|
|
return err
|
2015-11-27 09:53:38 +03:00
|
|
|
}
|
|
|
|
}
|
2015-11-29 02:13:57 +03:00
|
|
|
|
2016-02-03 10:59:27 +03:00
|
|
|
// TODO(roasbeef): shouldn't hardcode 10, use config param that dictates
|
|
|
|
// how many confs we wait before opening a channel.
|
|
|
|
// Generate 10 blocks with the mining node, this should mine all
|
|
|
|
// numOutputs transactions created above. We generate 10 blocks here
|
|
|
|
// in order to give all the outputs a "sufficient" number of confirmations.
|
|
|
|
if _, err := miner.Node.Generate(10); err != nil {
|
2015-11-29 02:13:57 +03:00
|
|
|
return err
|
|
|
|
}
|
2015-11-27 09:53:38 +03:00
|
|
|
|
2016-02-03 10:59:27 +03:00
|
|
|
_, bestHeight, err := miner.Node.GetBestBlock()
|
2015-11-27 09:53:38 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-02-03 10:59:27 +03:00
|
|
|
// Wait until the wallet has finished syncing up to the main chain.
|
|
|
|
ticker := time.NewTicker(100 * time.Millisecond)
|
|
|
|
out:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
|
|
|
if w.Manager.SyncedTo().Height == bestHeight {
|
|
|
|
break out
|
|
|
|
}
|
|
|
|
}
|
2015-11-29 02:13:57 +03:00
|
|
|
}
|
2016-02-03 10:59:27 +03:00
|
|
|
ticker.Stop()
|
2015-11-29 02:13:57 +03:00
|
|
|
|
2016-02-03 10:59:27 +03:00
|
|
|
// Trigger a re-scan to ensure the wallet knows of the newly created
|
|
|
|
// outputs it can spend.
|
|
|
|
if err := w.Rescan(addrs, nil); err != nil {
|
2015-11-29 02:13:57 +03:00
|
|
|
return err
|
|
|
|
}
|
2015-11-27 09:53:38 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// createTestWallet creates a test LightningWallet will a total of 20BTC
|
|
|
|
// available for funding channels.
|
2016-02-03 10:59:27 +03:00
|
|
|
func createTestWallet(miningNode *rpctest.Harness, netParams *chaincfg.Params) (string, *LightningWallet, error) {
|
2015-11-27 09:53:38 +03:00
|
|
|
privPass := []byte("private-test")
|
|
|
|
tempTestDir, err := ioutil.TempDir("", "lnwallet")
|
|
|
|
if err != nil {
|
|
|
|
return "", nil, nil
|
|
|
|
}
|
|
|
|
|
2016-02-03 10:59:27 +03:00
|
|
|
rpcConfig := miningNode.RPCConfig()
|
|
|
|
config := &Config{
|
|
|
|
PrivatePass: privPass,
|
|
|
|
HdSeed: testHdSeed[:],
|
|
|
|
DataDir: tempTestDir,
|
|
|
|
NetParams: netParams,
|
|
|
|
RpcHost: rpcConfig.Host,
|
|
|
|
RpcUser: rpcConfig.User,
|
|
|
|
RpcPass: rpcConfig.Pass,
|
|
|
|
CACert: rpcConfig.Certificates,
|
|
|
|
}
|
|
|
|
|
2016-03-24 10:01:35 +03:00
|
|
|
dbDir := filepath.Join(tempTestDir, "cdb")
|
2016-05-04 05:49:58 +03:00
|
|
|
cdb, err := channeldb.Open(dbDir, &chaincfg.SegNet4Params)
|
2016-03-24 10:01:35 +03:00
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
wallet, err := NewLightningWallet(config, cdb)
|
2015-11-27 09:53:38 +03:00
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
2016-01-07 02:12:06 +03:00
|
|
|
if err := wallet.Startup(); err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
2015-11-27 09:53:38 +03:00
|
|
|
|
2016-03-25 00:32:44 +03:00
|
|
|
cdb.RegisterCryptoSystem(&WaddrmgrEncryptorDecryptor{wallet.Manager})
|
2016-03-24 10:01:35 +03:00
|
|
|
|
2015-11-27 09:53:38 +03:00
|
|
|
// Load our test wallet with 5 outputs each holding 4BTC.
|
2016-02-03 10:59:27 +03:00
|
|
|
if err := loadTestCredits(miningNode, wallet, 5, 4); err != nil {
|
2015-11-27 09:53:38 +03:00
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return tempTestDir, wallet, nil
|
2015-11-19 01:59:07 +03:00
|
|
|
}
|
|
|
|
|
2016-02-03 10:59:27 +03:00
|
|
|
func testBasicWalletReservationWorkFlow(miner *rpctest.Harness, lnwallet *LightningWallet, t *testing.T) {
|
2015-11-29 23:20:16 +03:00
|
|
|
// Create our test wallet, will have a total of 20 BTC available for
|
2016-02-03 10:59:27 +03:00
|
|
|
bobNode, err := newBobNode(miner)
|
2015-11-29 23:20:16 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create bob node: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bob initiates a channel funded with 5 BTC for each side, so 10
|
|
|
|
// BTC total. He also generates 2 BTC in change.
|
|
|
|
fundingAmount := btcutil.Amount(5 * 1e8)
|
2015-12-26 09:05:07 +03:00
|
|
|
chanReservation, err := lnwallet.InitChannelReservation(fundingAmount,
|
2016-05-04 05:49:58 +03:00
|
|
|
bobNode.id, 4)
|
2015-11-29 23:20:16 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to initialize funding reservation: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// The channel reservation should now be populated with a multi-sig key
|
|
|
|
// from our HD chain, a change output with 3 BTC, and 2 outputs selected
|
2015-12-28 23:16:20 +03:00
|
|
|
// of 4 BTC each. Additionally, the rest of the items needed to fufill a
|
|
|
|
// funding contribution should also have been filled in.
|
2015-12-23 07:32:18 +03:00
|
|
|
ourContribution := chanReservation.OurContribution()
|
|
|
|
if len(ourContribution.Inputs) != 2 {
|
2015-11-29 23:20:16 +03:00
|
|
|
t.Fatalf("outputs for funding tx not properly selected, have %v "+
|
2015-12-23 07:32:18 +03:00
|
|
|
"outputs should have 2", len(ourContribution.Inputs))
|
2015-11-29 23:20:16 +03:00
|
|
|
}
|
2015-12-23 07:32:18 +03:00
|
|
|
if ourContribution.ChangeOutputs[0].Value != 3e8 {
|
|
|
|
t.Fatalf("coin selection failed, change output should be 3e8 "+
|
|
|
|
"satoshis, is instead %v", ourContribution.ChangeOutputs[0].Value)
|
|
|
|
}
|
|
|
|
if ourContribution.MultiSigKey == nil {
|
2015-11-29 23:20:16 +03:00
|
|
|
t.Fatalf("alice's key for multi-sig not found")
|
|
|
|
}
|
2015-12-23 07:32:18 +03:00
|
|
|
if ourContribution.CommitKey == nil {
|
2015-12-22 00:55:17 +03:00
|
|
|
t.Fatalf("alice's key for commit not found")
|
2015-12-19 06:47:47 +03:00
|
|
|
}
|
2015-12-23 07:32:18 +03:00
|
|
|
if ourContribution.DeliveryAddress == nil {
|
|
|
|
t.Fatalf("alice's final delivery address not found")
|
2015-11-29 23:20:16 +03:00
|
|
|
}
|
2015-12-23 07:32:18 +03:00
|
|
|
if bytes.Equal(ourContribution.RevocationHash[:], zeroHash) {
|
|
|
|
t.Fatalf("alice's revocation hash not found")
|
|
|
|
}
|
2015-12-28 23:16:20 +03:00
|
|
|
if ourContribution.CsvDelay == 0 {
|
|
|
|
t.Fatalf("csv delay not set")
|
|
|
|
}
|
2015-11-29 23:20:16 +03:00
|
|
|
|
2015-12-22 00:55:17 +03:00
|
|
|
// Bob sends over his output, change addr, pub keys, initial revocation,
|
|
|
|
// final delivery address, and his accepted csv delay for the commitmen
|
|
|
|
// t transactions.
|
2015-12-23 07:32:18 +03:00
|
|
|
if err := chanReservation.ProcessContribution(bobNode.Contribution()); err != nil {
|
2015-11-29 23:20:16 +03:00
|
|
|
t.Fatalf("unable to add bob's funds to the funding tx: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// At this point, the reservation should have our signatures, and a
|
|
|
|
// partial funding transaction (missing bob's sigs).
|
2015-12-23 07:32:18 +03:00
|
|
|
theirContribution := chanReservation.TheirContribution()
|
|
|
|
ourFundingSigs, ourCommitSig := chanReservation.OurSignatures()
|
|
|
|
if len(ourFundingSigs) != 2 {
|
2015-11-29 23:20:16 +03:00
|
|
|
t.Fatalf("only %v of our sigs present, should have 2",
|
2015-12-23 07:32:18 +03:00
|
|
|
len(ourFundingSigs))
|
|
|
|
}
|
|
|
|
if ourCommitSig == nil {
|
|
|
|
t.Fatalf("commitment sig not found")
|
2015-11-29 23:20:16 +03:00
|
|
|
}
|
|
|
|
// Additionally, the funding tx should have been populated.
|
2015-12-24 21:41:15 +03:00
|
|
|
if chanReservation.partialState.FundingTx == nil {
|
2015-11-29 23:20:16 +03:00
|
|
|
t.Fatalf("funding transaction never created!")
|
|
|
|
}
|
|
|
|
// Their funds should also be filled in.
|
2015-12-23 07:32:18 +03:00
|
|
|
if len(theirContribution.Inputs) != 1 {
|
2015-11-29 23:20:16 +03:00
|
|
|
t.Fatalf("bob's outputs for funding tx not properly selected, have %v "+
|
2015-12-23 07:32:18 +03:00
|
|
|
"outputs should have 2", len(theirContribution.Inputs))
|
|
|
|
}
|
|
|
|
if theirContribution.ChangeOutputs[0].Value != 2e8 {
|
|
|
|
t.Fatalf("bob should have one change output with value 2e8"+
|
|
|
|
"satoshis, is instead %v",
|
|
|
|
theirContribution.ChangeOutputs[0].Value)
|
2015-11-29 23:20:16 +03:00
|
|
|
}
|
2015-12-23 07:32:18 +03:00
|
|
|
if theirContribution.MultiSigKey == nil {
|
2015-12-19 06:47:47 +03:00
|
|
|
t.Fatalf("bob's key for multi-sig not found")
|
|
|
|
}
|
2015-12-23 07:32:18 +03:00
|
|
|
if theirContribution.CommitKey == nil {
|
2015-12-22 00:55:17 +03:00
|
|
|
t.Fatalf("bob's key for commit tx not found")
|
2015-11-29 23:20:16 +03:00
|
|
|
}
|
2015-12-23 07:32:18 +03:00
|
|
|
if theirContribution.DeliveryAddress == nil {
|
|
|
|
t.Fatalf("bob's final delivery address not found")
|
|
|
|
}
|
|
|
|
if bytes.Equal(theirContribution.RevocationHash[:], zeroHash) {
|
|
|
|
t.Fatalf("bob's revocaiton hash not found")
|
2015-11-29 23:20:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Alice responds with her output, change addr, multi-sig key and signatures.
|
|
|
|
// Bob then responds with his signatures.
|
2015-12-24 21:41:15 +03:00
|
|
|
bobsSigs, err := bobNode.signFundingTx(chanReservation.partialState.FundingTx)
|
2015-11-29 23:20:16 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to sign inputs for bob: %v", err)
|
|
|
|
}
|
2015-12-28 23:15:46 +03:00
|
|
|
commitSig, err := bobNode.signCommitTx(
|
|
|
|
chanReservation.partialState.OurCommitTx,
|
2016-05-04 05:49:58 +03:00
|
|
|
chanReservation.partialState.FundingRedeemScript,
|
|
|
|
10e8)
|
2015-12-28 23:15:46 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bob is unable to sign alice's commit tx: %v", err)
|
|
|
|
}
|
|
|
|
if err := chanReservation.CompleteReservation(bobsSigs, commitSig); err != nil {
|
2015-11-29 23:20:16 +03:00
|
|
|
t.Fatalf("unable to complete funding tx: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-12-16 01:19:17 +03:00
|
|
|
// At this point, the channel can be considered "open" when the funding
|
|
|
|
// txn hits a "comfortable" depth.
|
|
|
|
|
2015-12-28 23:15:46 +03:00
|
|
|
// The resulting active channel state should have been persisted to the DB.
|
2016-02-03 10:59:27 +03:00
|
|
|
fundingTx := chanReservation.FinalFundingTx()
|
2016-03-24 10:01:35 +03:00
|
|
|
channel, err := lnwallet.channelDB.FetchOpenChannel(bobNode.id)
|
2015-12-26 09:05:55 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to retrieve channel from DB: %v", err)
|
|
|
|
}
|
|
|
|
if channel.FundingTx.TxSha() != fundingTx.TxSha() {
|
|
|
|
t.Fatalf("channel state not properly saved")
|
|
|
|
}
|
2015-11-19 01:59:07 +03:00
|
|
|
}
|
|
|
|
|
2016-02-03 10:59:27 +03:00
|
|
|
func testFundingTransactionLockedOutputs(miner *rpctest.Harness, lnwallet *LightningWallet, t *testing.T) {
|
2015-12-16 22:22:36 +03:00
|
|
|
// Create two channels, both asking for 8 BTC each, totalling 16
|
|
|
|
// BTC.
|
|
|
|
// TODO(roasbeef): tests for concurrent funding.
|
2015-12-16 23:41:15 +03:00
|
|
|
// * also func for below
|
2015-12-16 22:22:36 +03:00
|
|
|
fundingAmount := btcutil.Amount(8 * 1e8)
|
2015-12-26 09:05:07 +03:00
|
|
|
chanReservation1, err := lnwallet.InitChannelReservation(fundingAmount,
|
2016-05-04 05:49:58 +03:00
|
|
|
testHdSeed, 4)
|
2015-12-16 22:22:36 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to initialize funding reservation 1: %v", err)
|
|
|
|
}
|
2015-12-26 09:05:07 +03:00
|
|
|
chanReservation2, err := lnwallet.InitChannelReservation(fundingAmount,
|
2016-05-04 05:49:58 +03:00
|
|
|
testHdSeed, 4)
|
2015-12-16 22:22:36 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to initialize funding reservation 2: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Neither should have any change, as all our output sizes are
|
|
|
|
// identical (4BTC).
|
2015-12-23 07:32:18 +03:00
|
|
|
ourContribution1 := chanReservation1.OurContribution()
|
|
|
|
if len(ourContribution1.Inputs) != 2 {
|
2015-12-16 22:22:36 +03:00
|
|
|
t.Fatalf("outputs for funding tx not properly selected, has %v "+
|
2015-12-23 07:32:18 +03:00
|
|
|
"outputs should have 2", len(ourContribution1.Inputs))
|
2015-12-16 22:22:36 +03:00
|
|
|
}
|
2015-12-23 07:32:18 +03:00
|
|
|
if len(ourContribution1.ChangeOutputs) != 0 {
|
2015-12-16 22:22:36 +03:00
|
|
|
t.Fatalf("funding transaction should have no change, instead has %v",
|
2015-12-23 07:32:18 +03:00
|
|
|
len(ourContribution1.ChangeOutputs))
|
2015-12-16 22:22:36 +03:00
|
|
|
}
|
2015-12-23 07:32:18 +03:00
|
|
|
ourContribution2 := chanReservation2.OurContribution()
|
|
|
|
if len(ourContribution2.Inputs) != 2 {
|
2015-12-16 22:22:36 +03:00
|
|
|
t.Fatalf("outputs for funding tx not properly selected, have %v "+
|
2015-12-23 07:32:18 +03:00
|
|
|
"outputs should have 2", len(ourContribution2.Inputs))
|
2015-12-16 22:22:36 +03:00
|
|
|
}
|
2015-12-23 07:32:18 +03:00
|
|
|
if len(ourContribution2.ChangeOutputs) != 0 {
|
2015-12-16 22:22:36 +03:00
|
|
|
t.Fatalf("funding transaction should have no change, instead has %v",
|
2015-12-23 07:32:18 +03:00
|
|
|
len(ourContribution2.ChangeOutputs))
|
2015-12-16 22:22:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now attempt to reserve funds for another channel, this time requesting
|
|
|
|
// 5 BTC. We only have 4BTC worth of outpoints that aren't locked, so
|
|
|
|
// this should fail.
|
|
|
|
amt := btcutil.Amount(8 * 1e8)
|
2015-12-26 09:05:07 +03:00
|
|
|
failedReservation, err := lnwallet.InitChannelReservation(amt,
|
2016-05-04 05:49:58 +03:00
|
|
|
testHdSeed, 4)
|
2015-12-16 22:22:36 +03:00
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("not error returned, should fail on coin selection")
|
|
|
|
}
|
|
|
|
if err != coinset.ErrCoinsNoSelectionAvailable {
|
|
|
|
t.Fatalf("error not coinselect error: %v", err)
|
|
|
|
}
|
|
|
|
if failedReservation != nil {
|
|
|
|
t.Fatalf("reservation should be nil")
|
|
|
|
}
|
2015-11-19 01:59:07 +03:00
|
|
|
}
|
|
|
|
|
2016-02-03 10:59:27 +03:00
|
|
|
func testFundingCancellationNotEnoughFunds(miner *rpctest.Harness, lnwallet *LightningWallet, t *testing.T) {
|
2015-12-16 23:41:15 +03:00
|
|
|
// Create a reservation for 12 BTC.
|
|
|
|
fundingAmount := btcutil.Amount(12 * 1e8)
|
2015-12-26 09:05:07 +03:00
|
|
|
chanReservation, err := lnwallet.InitChannelReservation(fundingAmount,
|
2016-05-04 05:49:58 +03:00
|
|
|
testHdSeed, 4)
|
2015-12-16 23:41:15 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to initialize funding reservation: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// There should be three locked outpoints.
|
2015-12-29 21:44:59 +03:00
|
|
|
lockedOutPoints := lnwallet.LockedOutpoints()
|
2015-12-16 23:41:15 +03:00
|
|
|
if len(lockedOutPoints) != 3 {
|
|
|
|
t.Fatalf("two outpoints should now be locked, instead %v are",
|
|
|
|
lockedOutPoints)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt to create another channel with 12 BTC, this should fail.
|
2015-12-26 09:05:07 +03:00
|
|
|
failedReservation, err := lnwallet.InitChannelReservation(fundingAmount,
|
2016-05-04 05:49:58 +03:00
|
|
|
testHdSeed, 4)
|
2015-12-16 23:41:15 +03:00
|
|
|
if err != coinset.ErrCoinsNoSelectionAvailable {
|
|
|
|
t.Fatalf("coin selection succeded should have insufficient funds: %+v",
|
|
|
|
failedReservation)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now cancel that old reservation.
|
|
|
|
if err := chanReservation.Cancel(); err != nil {
|
|
|
|
t.Fatalf("unable to cancel reservation: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Those outpoints should no longer be locked.
|
2015-12-29 21:44:59 +03:00
|
|
|
lockedOutPoints = lnwallet.LockedOutpoints()
|
2015-12-16 23:41:15 +03:00
|
|
|
if len(lockedOutPoints) != 0 {
|
|
|
|
t.Fatalf("outpoints still locked")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reservation ID should now longer be tracked.
|
|
|
|
_, ok := lnwallet.fundingLimbo[chanReservation.reservationID]
|
|
|
|
if ok {
|
|
|
|
t.Fatalf("funding reservation still in map")
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(roasbeef): create method like Balance that ignores locked
|
|
|
|
// outpoints, will let us fail early/fast instead of querying and
|
|
|
|
// attempting coin selection.
|
|
|
|
|
|
|
|
// Request to fund a new channel should now succeeed.
|
2015-12-26 09:05:07 +03:00
|
|
|
_, err = lnwallet.InitChannelReservation(fundingAmount,
|
2016-05-04 05:49:58 +03:00
|
|
|
testHdSeed, 4)
|
2015-12-16 23:41:15 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to initialize funding reservation: %v", err)
|
|
|
|
}
|
2015-11-19 01:59:07 +03:00
|
|
|
}
|
|
|
|
|
2016-02-03 10:59:27 +03:00
|
|
|
func testCancelNonExistantReservation(miner *rpctest.Harness, lnwallet *LightningWallet, t *testing.T) {
|
2015-12-16 23:41:15 +03:00
|
|
|
// Create our own reservation, give it some ID.
|
2016-05-04 05:49:58 +03:00
|
|
|
res := newChannelReservation(1000, 5000, lnwallet, 22)
|
2015-12-16 23:41:15 +03:00
|
|
|
|
|
|
|
// Attempt to cancel this reservation. This should fail, we know
|
|
|
|
// nothing of it.
|
2015-12-19 06:47:47 +03:00
|
|
|
if err := res.Cancel(); err == nil {
|
2015-12-16 23:41:15 +03:00
|
|
|
t.Fatalf("cancelled non-existant reservation")
|
|
|
|
}
|
2015-11-19 01:59:07 +03:00
|
|
|
}
|
|
|
|
|
2016-02-03 10:59:27 +03:00
|
|
|
func testFundingReservationInvalidCounterpartySigs(miner *rpctest.Harness, lnwallet *LightningWallet, t *testing.T) {
|
2015-12-19 06:48:55 +03:00
|
|
|
}
|
|
|
|
|
2016-02-03 10:59:27 +03:00
|
|
|
func testFundingTransactionTxFees(miner *rpctest.Harness, lnwallet *LightningWallet, t *testing.T) {
|
2015-12-19 06:48:55 +03:00
|
|
|
}
|
|
|
|
|
2016-02-03 10:59:27 +03:00
|
|
|
var walletTests = []func(miner *rpctest.Harness, w *LightningWallet, test *testing.T){
|
2015-12-19 06:48:55 +03:00
|
|
|
testBasicWalletReservationWorkFlow,
|
|
|
|
testFundingTransactionLockedOutputs,
|
|
|
|
testFundingCancellationNotEnoughFunds,
|
|
|
|
testFundingReservationInvalidCounterpartySigs,
|
|
|
|
testFundingTransactionLockedOutputs,
|
2016-02-03 10:59:27 +03:00
|
|
|
// TODO(roasbeef):
|
|
|
|
// * test for non-existant output given in funding tx
|
|
|
|
// * channel open after confirmations
|
|
|
|
// * channel update stuff
|
2015-12-19 06:48:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type testLnWallet struct {
|
|
|
|
lnwallet *LightningWallet
|
|
|
|
cleanUpFunc func()
|
|
|
|
}
|
|
|
|
|
2016-03-24 10:01:35 +03:00
|
|
|
func clearWalletState(w *LightningWallet) {
|
2015-12-19 06:48:55 +03:00
|
|
|
w.nextFundingID = 0
|
|
|
|
w.fundingLimbo = make(map[uint64]*ChannelReservation)
|
2015-12-29 21:44:59 +03:00
|
|
|
w.ResetLockedOutpoints()
|
2015-11-19 01:59:07 +03:00
|
|
|
}
|
2015-12-16 22:22:36 +03:00
|
|
|
|
2015-12-19 06:48:55 +03:00
|
|
|
func TestLightningWallet(t *testing.T) {
|
2016-02-03 10:59:27 +03:00
|
|
|
netParams := &chaincfg.SimNetParams
|
|
|
|
|
|
|
|
// Initialize the harness around a btcd node which will serve as our
|
|
|
|
// dedicated miner to generate blocks, cause re-orgs, etc. We'll set
|
|
|
|
// up this node with a chain length of 125, so we have plentyyy of BTC
|
|
|
|
// to play around with.
|
|
|
|
miningNode, err := rpctest.New(netParams, nil, nil)
|
2016-02-05 23:32:23 +03:00
|
|
|
defer miningNode.TearDown()
|
2016-02-03 10:59:27 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create mining node: %v", err)
|
|
|
|
}
|
|
|
|
if err := miningNode.SetUp(true, 25); err != nil {
|
|
|
|
t.Fatalf("unable to set up mining node: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Funding via 5 outputs with 4BTC each.
|
|
|
|
testDir, lnwallet, err := createTestWallet(miningNode, netParams)
|
2015-12-19 06:48:55 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create test ln wallet: %v", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(testDir)
|
2015-12-29 21:44:59 +03:00
|
|
|
defer lnwallet.Shutdown()
|
2015-12-19 06:48:55 +03:00
|
|
|
|
|
|
|
// The wallet should now have 20BTC available for spending.
|
|
|
|
assertProperBalance(t, lnwallet, 1, 20)
|
|
|
|
|
|
|
|
// Execute every test, clearing possibly mutated wallet state after
|
|
|
|
// each step.
|
|
|
|
for _, walletTest := range walletTests {
|
2016-02-03 10:59:27 +03:00
|
|
|
walletTest(miningNode, lnwallet, t)
|
2015-12-19 06:48:55 +03:00
|
|
|
|
2016-02-03 10:59:27 +03:00
|
|
|
// TODO(roasbeef): possible reset mining node's chainstate to
|
2016-03-24 10:01:35 +03:00
|
|
|
// initial level, cleanly wipe buckets
|
|
|
|
clearWalletState(lnwallet)
|
2015-12-19 06:48:55 +03:00
|
|
|
}
|
2015-12-16 22:22:36 +03:00
|
|
|
}
|