2016-08-13 01:50:47 +03:00
|
|
|
package lnwallet_test
|
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"
|
2018-09-05 04:52:16 +03:00
|
|
|
"crypto/sha256"
|
2016-06-21 20:40:20 +03:00
|
|
|
"encoding/hex"
|
2016-02-03 10:59:27 +03:00
|
|
|
"fmt"
|
2015-11-27 09:53:38 +03:00
|
|
|
"io/ioutil"
|
2017-11-10 03:30:20 +03:00
|
|
|
"math/rand"
|
2016-10-27 00:56:48 +03:00
|
|
|
"net"
|
2015-11-27 09:53:38 +03:00
|
|
|
"os"
|
2017-11-10 03:30:20 +03:00
|
|
|
"os/exec"
|
2016-03-24 10:01:35 +03:00
|
|
|
"path/filepath"
|
2017-12-06 20:20:05 +03:00
|
|
|
"reflect"
|
2017-07-30 06:03:10 +03:00
|
|
|
"runtime"
|
2017-11-26 22:32:57 +03:00
|
|
|
"strings"
|
2015-11-27 09:53:38 +03:00
|
|
|
"testing"
|
|
|
|
"time"
|
2015-11-19 01:59:07 +03:00
|
|
|
|
2018-07-17 02:50:47 +03:00
|
|
|
"github.com/btcsuite/btcd/btcec"
|
2018-06-05 04:41:41 +03:00
|
|
|
"github.com/btcsuite/btcd/btcjson"
|
|
|
|
"github.com/btcsuite/btcd/chaincfg"
|
|
|
|
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
2018-07-17 02:50:47 +03:00
|
|
|
"github.com/btcsuite/btcd/integration/rpctest"
|
2019-09-19 15:59:07 +03:00
|
|
|
"github.com/btcsuite/btcd/mempool"
|
2018-06-05 04:41:41 +03:00
|
|
|
"github.com/btcsuite/btcd/rpcclient"
|
2018-07-17 02:50:47 +03:00
|
|
|
"github.com/btcsuite/btcd/txscript"
|
|
|
|
"github.com/btcsuite/btcd/wire"
|
|
|
|
"github.com/btcsuite/btcutil"
|
|
|
|
"github.com/btcsuite/btcwallet/chain"
|
|
|
|
"github.com/btcsuite/btcwallet/walletdb"
|
|
|
|
_ "github.com/btcsuite/btcwallet/walletdb/bdb"
|
|
|
|
"github.com/davecgh/go-spew/spew"
|
|
|
|
"github.com/lightninglabs/neutrino"
|
2016-08-13 01:50:47 +03:00
|
|
|
"github.com/lightningnetwork/lnd/chainntnfs"
|
2016-08-04 08:31:20 +03:00
|
|
|
"github.com/lightningnetwork/lnd/chainntnfs/btcdnotify"
|
2016-03-24 10:01:35 +03:00
|
|
|
"github.com/lightningnetwork/lnd/channeldb"
|
2020-01-10 05:46:17 +03:00
|
|
|
"github.com/lightningnetwork/lnd/channeldb/kvdb"
|
2019-01-16 17:47:43 +03:00
|
|
|
"github.com/lightningnetwork/lnd/input"
|
2018-02-18 02:25:55 +03:00
|
|
|
"github.com/lightningnetwork/lnd/keychain"
|
2016-08-13 01:50:47 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnwallet"
|
|
|
|
"github.com/lightningnetwork/lnd/lnwallet/btcwallet"
|
2019-10-31 05:43:05 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
|
2019-11-01 07:39:17 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnwallet/chanfunding"
|
2017-08-22 09:20:57 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
2015-11-27 09:53:38 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
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.
|
2017-07-30 06:03:10 +03:00
|
|
|
testHdSeed = chainhash.Hash{
|
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,
|
|
|
|
}
|
2016-06-21 20:40:20 +03:00
|
|
|
|
2017-07-30 06:03:10 +03:00
|
|
|
aliceHDSeed = chainhash.Hash{
|
|
|
|
0xb7, 0x94, 0x38, 0x5f, 0x2d, 0x1e, 0xf7, 0xab,
|
|
|
|
0x4d, 0x92, 0x73, 0xd1, 0x90, 0x63, 0x81, 0xb4,
|
|
|
|
0x4f, 0x2f, 0x6f, 0x25, 0x18, 0xa3, 0xef, 0xb9,
|
|
|
|
0x64, 0x49, 0x18, 0x83, 0x31, 0x98, 0x47, 0x53,
|
|
|
|
}
|
|
|
|
bobHDSeed = chainhash.Hash{
|
|
|
|
0xb7, 0x94, 0x38, 0x5f, 0x2d, 0x1e, 0xf7, 0xab,
|
|
|
|
0x4d, 0x92, 0x73, 0xd1, 0x90, 0x63, 0x81, 0xb4,
|
|
|
|
0x4f, 0x2f, 0x6f, 0x25, 0x98, 0xa3, 0xef, 0xb9,
|
|
|
|
0x69, 0x49, 0x18, 0x83, 0x31, 0x98, 0x47, 0x53,
|
|
|
|
}
|
|
|
|
|
2017-11-10 03:30:20 +03:00
|
|
|
netParams = &chaincfg.RegressionNetParams
|
2017-07-30 06:03:10 +03:00
|
|
|
chainHash = netParams.GenesisHash
|
|
|
|
|
|
|
|
_, alicePub = btcec.PrivKeyFromBytes(btcec.S256(), testHdSeed[:])
|
|
|
|
_, bobPub = btcec.PrivKeyFromBytes(btcec.S256(), bobsPrivKey)
|
2016-10-26 02:40:47 +03:00
|
|
|
|
2016-06-21 20:40:20 +03:00
|
|
|
// The number of confirmations required to consider any created channel
|
|
|
|
// open.
|
2017-07-30 06:03:10 +03:00
|
|
|
numReqConfs uint16 = 1
|
|
|
|
|
|
|
|
csvDelay uint16 = 4
|
2016-10-27 00:56:48 +03:00
|
|
|
|
2017-07-30 06:03:10 +03:00
|
|
|
bobAddr, _ = net.ResolveTCPAddr("tcp", "10.0.0.2:9000")
|
|
|
|
aliceAddr, _ = net.ResolveTCPAddr("tcp", "10.0.0.3:9000")
|
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.
|
2018-03-26 05:15:05 +03:00
|
|
|
func assertProperBalance(t *testing.T, lw *lnwallet.LightningWallet,
|
|
|
|
numConfirms int32, amount float64) {
|
|
|
|
|
2018-02-20 05:58:57 +03:00
|
|
|
balance, err := lw.ConfirmedBalance(numConfirms)
|
2015-12-16 22:22:36 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to query for balance: %v", err)
|
|
|
|
}
|
2018-03-26 05:15:05 +03:00
|
|
|
if balance.ToBTC() != amount {
|
2016-06-21 20:40:20 +03:00
|
|
|
t.Fatalf("wallet credits not properly loaded, should have 40BTC, "+
|
2015-12-16 22:22:36 +03:00
|
|
|
"instead have %v", balance)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-22 09:12:46 +03:00
|
|
|
func assertReservationDeleted(res *lnwallet.ChannelReservation, t *testing.T) {
|
|
|
|
if err := res.Cancel(); err == nil {
|
|
|
|
t.Fatalf("reservation wasn't deleted from wallet")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-14 07:01:54 +03:00
|
|
|
// mineAndAssertTxInBlock asserts that a transaction is included within the next
|
|
|
|
// block mined.
|
|
|
|
func mineAndAssertTxInBlock(t *testing.T, miner *rpctest.Harness,
|
|
|
|
txid chainhash.Hash) {
|
|
|
|
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
// First, we'll wait for the transaction to arrive in the mempool.
|
|
|
|
if err := waitForMempoolTx(miner, &txid); err != nil {
|
|
|
|
t.Fatalf("unable to find %v in the mempool: %v", txid, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We'll mined a block to confirm it.
|
|
|
|
blockHashes, err := miner.Node.Generate(1)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to generate new block: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, we'll check it was actually mined in this block.
|
|
|
|
block, err := miner.Node.GetBlock(blockHashes[0])
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get block %v: %v", blockHashes[0], err)
|
|
|
|
}
|
|
|
|
if len(block.Transactions) != 2 {
|
|
|
|
t.Fatalf("expected 2 transactions in block, found %d",
|
|
|
|
len(block.Transactions))
|
|
|
|
}
|
|
|
|
txHash := block.Transactions[1].TxHash()
|
|
|
|
if txHash != txid {
|
|
|
|
t.Fatalf("expected transaction %v to be mined, found %v", txid,
|
|
|
|
txHash)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// newPkScript generates a new public key script of the given address type.
|
|
|
|
func newPkScript(t *testing.T, w *lnwallet.LightningWallet,
|
|
|
|
addrType lnwallet.AddressType) []byte {
|
|
|
|
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
addr, err := w.NewAddress(addrType, false)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create new address: %v", err)
|
|
|
|
}
|
|
|
|
pkScript, err := txscript.PayToAddrScript(addr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create output script: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return pkScript
|
|
|
|
}
|
|
|
|
|
|
|
|
// sendCoins is a helper function that encompasses all the things needed for two
|
|
|
|
// parties to send on-chain funds to each other.
|
|
|
|
func sendCoins(t *testing.T, miner *rpctest.Harness,
|
|
|
|
sender, receiver *lnwallet.LightningWallet, output *wire.TxOut,
|
2019-10-31 05:43:05 +03:00
|
|
|
feeRate chainfee.SatPerKWeight) *wire.MsgTx { //nolint:unparam
|
2018-11-14 07:01:54 +03:00
|
|
|
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
tx, err := sender.SendOutputs([]*wire.TxOut{output}, 2500)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send transaction: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
mineAndAssertTxInBlock(t, miner, tx.TxHash())
|
|
|
|
|
|
|
|
if err := waitForWalletSync(miner, sender); err != nil {
|
|
|
|
t.Fatalf("unable to sync alice: %v", err)
|
|
|
|
}
|
|
|
|
if err := waitForWalletSync(miner, receiver); err != nil {
|
|
|
|
t.Fatalf("unable to sync bob: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return tx
|
|
|
|
}
|
|
|
|
|
|
|
|
// assertTxInWallet asserts that a transaction exists in the wallet with the
|
|
|
|
// expected confirmation status.
|
|
|
|
func assertTxInWallet(t *testing.T, w *lnwallet.LightningWallet,
|
|
|
|
txHash chainhash.Hash, confirmed bool) {
|
|
|
|
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
// If the backend is Neutrino, then we can't determine unconfirmed
|
|
|
|
// transactions since it's not aware of the mempool.
|
|
|
|
if !confirmed && w.BackEnd() == "neutrino" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// We'll fetch all of our transaction and go through each one until
|
|
|
|
// finding the expected transaction with its expected confirmation
|
|
|
|
// status.
|
|
|
|
txs, err := w.ListTransactionDetails()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to retrieve transactions: %v", err)
|
|
|
|
}
|
|
|
|
for _, tx := range txs {
|
|
|
|
if tx.Hash != txHash {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if tx.NumConfirmations <= 0 && confirmed {
|
|
|
|
t.Fatalf("expected transaction %v to be confirmed",
|
|
|
|
txHash)
|
|
|
|
}
|
|
|
|
if tx.NumConfirmations > 0 && !confirmed {
|
|
|
|
t.Fatalf("expected transaction %v to be unconfirmed",
|
|
|
|
txHash)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We've found the transaction and it matches the desired
|
|
|
|
// confirmation status, so we can exit.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Fatalf("transaction %v not found", txHash)
|
|
|
|
}
|
|
|
|
|
2017-07-30 06:03:10 +03:00
|
|
|
func loadTestCredits(miner *rpctest.Harness, w *lnwallet.LightningWallet,
|
2018-03-26 05:15:05 +03:00
|
|
|
numOutputs int, btcPerOutput float64) error {
|
2015-11-27 09:53:38 +03:00
|
|
|
|
2017-11-10 03:30:20 +03:00
|
|
|
// For initial neutrino connection, wait a second.
|
|
|
|
// TODO(aakselrod): Eliminate the need for this.
|
|
|
|
switch w.BackEnd() {
|
|
|
|
case "neutrino":
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
}
|
2016-02-03 10:59:27 +03:00
|
|
|
// Using the mining node, spend from a coinbase output numOutputs to
|
|
|
|
// give us btcPerOutput with each output.
|
2018-03-26 05:15:05 +03:00
|
|
|
satoshiPerOutput, err := btcutil.NewAmount(btcPerOutput)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to create amt: %v", err)
|
|
|
|
}
|
2018-02-20 05:58:57 +03:00
|
|
|
expectedBalance, err := w.ConfirmedBalance(1)
|
2017-06-29 19:48:10 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-03-26 05:15:05 +03:00
|
|
|
expectedBalance += btcutil.Amount(int64(satoshiPerOutput) * int64(numOutputs))
|
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-08-13 01:50:47 +03:00
|
|
|
walletAddr, err := w.NewAddress(lnwallet.WitnessPubKey, false)
|
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)
|
|
|
|
|
2017-02-10 02:28:32 +03:00
|
|
|
output := &wire.TxOut{
|
2018-03-26 05:15:05 +03:00
|
|
|
Value: int64(satoshiPerOutput),
|
2017-02-10 02:28:32 +03:00
|
|
|
PkScript: script,
|
|
|
|
}
|
2018-07-28 04:20:58 +03:00
|
|
|
if _, err := miner.SendOutputs([]*wire.TxOut{output}, 2500); 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
|
|
|
// Wait until the wallet has finished syncing up to the main chain.
|
|
|
|
ticker := time.NewTicker(100 * time.Millisecond)
|
2017-11-10 03:30:20 +03:00
|
|
|
timeout := time.After(30 * time.Second)
|
2017-02-23 21:59:50 +03:00
|
|
|
|
|
|
|
for range ticker.C {
|
2018-02-20 05:58:57 +03:00
|
|
|
balance, err := w.ConfirmedBalance(1)
|
2017-02-23 21:59:50 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if balance == expectedBalance {
|
|
|
|
break
|
2016-02-03 10:59:27 +03:00
|
|
|
}
|
2017-11-10 03:30:20 +03:00
|
|
|
select {
|
|
|
|
case <-timeout:
|
2018-02-21 06:15:40 +03:00
|
|
|
synced, _, err := w.IsSynced()
|
2017-11-10 03:30:20 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return fmt.Errorf("timed out after 30 seconds "+
|
|
|
|
"waiting for balance %v, current balance %v, "+
|
|
|
|
"synced: %t", expectedBalance, balance, synced)
|
|
|
|
default:
|
|
|
|
}
|
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
|
|
|
|
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-08-13 01:50:47 +03:00
|
|
|
func createTestWallet(tempTestDir string, miningNode *rpctest.Harness,
|
|
|
|
netParams *chaincfg.Params, notifier chainntnfs.ChainNotifier,
|
2018-02-18 02:25:55 +03:00
|
|
|
wc lnwallet.WalletController, keyRing keychain.SecretKeyRing,
|
2019-01-16 17:47:43 +03:00
|
|
|
signer input.Signer, bio lnwallet.BlockChainIO) (*lnwallet.LightningWallet, error) {
|
2016-02-03 10:59:27 +03:00
|
|
|
|
2016-03-24 10:01:35 +03:00
|
|
|
dbDir := filepath.Join(tempTestDir, "cdb")
|
2016-12-22 23:09:19 +03:00
|
|
|
cdb, err := channeldb.Open(dbDir)
|
2016-03-24 10:01:35 +03:00
|
|
|
if err != nil {
|
2016-08-13 01:50:47 +03:00
|
|
|
return nil, err
|
2016-03-24 10:01:35 +03:00
|
|
|
}
|
|
|
|
|
2017-07-30 21:46:59 +03:00
|
|
|
cfg := lnwallet.Config{
|
|
|
|
Database: cdb,
|
|
|
|
Notifier: notifier,
|
2018-02-18 02:25:55 +03:00
|
|
|
SecretKeyRing: keyRing,
|
2017-07-30 21:46:59 +03:00
|
|
|
WalletController: wc,
|
|
|
|
Signer: signer,
|
|
|
|
ChainIO: bio,
|
2019-10-31 05:43:05 +03:00
|
|
|
FeeEstimator: chainfee.NewStaticEstimator(2500, 0),
|
2017-07-30 21:46:59 +03:00
|
|
|
DefaultConstraints: channeldb.ChannelConstraints{
|
|
|
|
DustLimit: 500,
|
2017-08-22 09:20:57 +03:00
|
|
|
MaxPendingAmount: lnwire.NewMSatFromSatoshis(btcutil.SatoshiPerBitcoin) * 100,
|
2017-07-30 21:46:59 +03:00
|
|
|
ChanReserve: 100,
|
|
|
|
MinHTLC: 400,
|
|
|
|
MaxAcceptedHtlcs: 900,
|
|
|
|
},
|
|
|
|
NetParams: *netParams,
|
|
|
|
}
|
|
|
|
|
|
|
|
wallet, err := lnwallet.NewLightningWallet(cfg)
|
2016-08-04 08:31:20 +03:00
|
|
|
if err != nil {
|
2016-08-13 01:50:47 +03:00
|
|
|
return nil, err
|
2016-08-04 08:31:20 +03:00
|
|
|
}
|
|
|
|
|
2016-01-07 02:12:06 +03:00
|
|
|
if err := wallet.Startup(); err != nil {
|
2016-08-13 01:50:47 +03:00
|
|
|
return nil, err
|
2016-01-07 02:12:06 +03:00
|
|
|
}
|
2015-11-27 09:53:38 +03:00
|
|
|
|
2016-08-13 01:50:47 +03:00
|
|
|
// Load our test wallet with 20 outputs each holding 4BTC.
|
|
|
|
if err := loadTestCredits(miningNode, wallet, 20, 4); err != nil {
|
|
|
|
return nil, err
|
2015-11-27 09:53:38 +03:00
|
|
|
}
|
|
|
|
|
2016-08-13 01:50:47 +03:00
|
|
|
return wallet, nil
|
2015-11-19 01:59:07 +03:00
|
|
|
}
|
|
|
|
|
2017-07-30 21:46:59 +03:00
|
|
|
func testDualFundingReservationWorkflow(miner *rpctest.Harness,
|
|
|
|
alice, bob *lnwallet.LightningWallet, t *testing.T) {
|
2016-10-16 00:12:18 +03:00
|
|
|
|
2018-03-26 05:15:05 +03:00
|
|
|
fundingAmount, err := btcutil.NewAmount(5)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create amt: %v", err)
|
|
|
|
}
|
2015-11-29 23:20:16 +03:00
|
|
|
|
2017-07-30 21:46:59 +03:00
|
|
|
// In this scenario, we'll test a dual funder reservation, with each
|
|
|
|
// side putting in 10 BTC.
|
|
|
|
|
|
|
|
// Alice initiates a channel funded with 5 BTC for each side, so 10 BTC
|
|
|
|
// total. She also generates 2 BTC in change.
|
2018-07-28 04:20:58 +03:00
|
|
|
feePerKw, err := alice.Cfg.FeeEstimator.EstimateFeePerKW(1)
|
2017-11-23 09:29:08 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to query fee estimator: %v", err)
|
|
|
|
}
|
2018-08-10 05:16:14 +03:00
|
|
|
aliceReq := &lnwallet.InitFundingReserveMsg{
|
2019-07-11 14:14:36 +03:00
|
|
|
ChainHash: chainHash,
|
|
|
|
NodeID: bobPub,
|
|
|
|
NodeAddr: bobAddr,
|
|
|
|
LocalFundingAmt: fundingAmount,
|
|
|
|
RemoteFundingAmt: fundingAmount,
|
|
|
|
CommitFeePerKw: feePerKw,
|
|
|
|
FundingFeePerKw: feePerKw,
|
|
|
|
PushMSat: 0,
|
|
|
|
Flags: lnwire.FFAnnounceChannel,
|
2018-08-10 05:16:14 +03:00
|
|
|
}
|
|
|
|
aliceChanReservation, err := alice.InitChannelReservation(aliceReq)
|
2015-11-29 23:20:16 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to initialize funding reservation: %v", err)
|
|
|
|
}
|
2017-07-30 21:46:59 +03:00
|
|
|
aliceChanReservation.SetNumConfsRequired(numReqConfs)
|
2018-12-11 00:58:34 +03:00
|
|
|
channelConstraints := &channeldb.ChannelConstraints{
|
|
|
|
DustLimit: lnwallet.DefaultDustLimit(),
|
|
|
|
ChanReserve: fundingAmount / 100,
|
|
|
|
MaxPendingAmount: lnwire.NewMSatFromSatoshis(fundingAmount),
|
|
|
|
MinHTLC: 1,
|
2019-01-16 17:47:43 +03:00
|
|
|
MaxAcceptedHtlcs: input.MaxHTLCNumber / 2,
|
2018-12-11 00:58:34 +03:00
|
|
|
CsvDelay: csvDelay,
|
|
|
|
}
|
2019-01-30 04:56:28 +03:00
|
|
|
err = aliceChanReservation.CommitConstraints(channelConstraints)
|
2018-05-14 21:04:34 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to verify constraints: %v", err)
|
|
|
|
}
|
2015-11-29 23:20:16 +03:00
|
|
|
|
|
|
|
// The channel reservation should now be populated with a multi-sig key
|
2016-11-16 23:54:27 +03:00
|
|
|
// from our HD chain, a change output with 3 BTC, and 2 outputs
|
|
|
|
// selected of 4 BTC each. Additionally, the rest of the items needed
|
|
|
|
// to fulfill a funding contribution should also have been filled in.
|
2017-07-30 21:46:59 +03:00
|
|
|
aliceContribution := aliceChanReservation.OurContribution()
|
|
|
|
if len(aliceContribution.Inputs) != 2 {
|
2015-11-29 23:20:16 +03:00
|
|
|
t.Fatalf("outputs for funding tx not properly selected, have %v "+
|
2017-07-30 21:46:59 +03:00
|
|
|
"outputs should have 2", len(aliceContribution.Inputs))
|
2015-11-29 23:20:16 +03:00
|
|
|
}
|
2017-07-30 21:46:59 +03:00
|
|
|
assertContributionInitPopulated(t, aliceContribution)
|
|
|
|
|
|
|
|
// Bob does the same, generating his own contribution. He then also
|
|
|
|
// receives' Alice's contribution, and consumes that so we can continue
|
|
|
|
// the funding process.
|
2018-08-10 05:16:14 +03:00
|
|
|
bobReq := &lnwallet.InitFundingReserveMsg{
|
2019-07-11 14:14:36 +03:00
|
|
|
ChainHash: chainHash,
|
|
|
|
NodeID: alicePub,
|
|
|
|
NodeAddr: aliceAddr,
|
|
|
|
LocalFundingAmt: fundingAmount,
|
|
|
|
RemoteFundingAmt: fundingAmount,
|
|
|
|
CommitFeePerKw: feePerKw,
|
|
|
|
FundingFeePerKw: feePerKw,
|
|
|
|
PushMSat: 0,
|
|
|
|
Flags: lnwire.FFAnnounceChannel,
|
2018-08-10 05:16:14 +03:00
|
|
|
}
|
|
|
|
bobChanReservation, err := bob.InitChannelReservation(bobReq)
|
2017-07-30 21:46:59 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bob unable to init channel reservation: %v", err)
|
2015-12-19 06:47:47 +03:00
|
|
|
}
|
2019-01-30 04:56:28 +03:00
|
|
|
err = bobChanReservation.CommitConstraints(channelConstraints)
|
2018-05-14 21:04:34 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to verify constraints: %v", err)
|
|
|
|
}
|
2017-07-30 21:46:59 +03:00
|
|
|
bobChanReservation.SetNumConfsRequired(numReqConfs)
|
|
|
|
|
|
|
|
assertContributionInitPopulated(t, bobChanReservation.OurContribution())
|
|
|
|
|
|
|
|
err = bobChanReservation.ProcessContribution(aliceContribution)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bob unable to process alice's contribution: %v", err)
|
2015-11-29 23:20:16 +03:00
|
|
|
}
|
2017-07-30 21:46:59 +03:00
|
|
|
assertContributionInitPopulated(t, bobChanReservation.TheirContribution())
|
|
|
|
|
|
|
|
bobContribution := bobChanReservation.OurContribution()
|
|
|
|
|
|
|
|
// Bob then sends over his contribution, which will be consumed by
|
|
|
|
// Alice. After this phase, Alice should have all the necessary
|
|
|
|
// material required to craft the funding transaction and commitment
|
|
|
|
// transactions.
|
|
|
|
err = aliceChanReservation.ProcessContribution(bobContribution)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("alice unable to process bob's contribution: %v", err)
|
2015-12-28 23:16:20 +03:00
|
|
|
}
|
2017-07-30 21:46:59 +03:00
|
|
|
assertContributionInitPopulated(t, aliceChanReservation.TheirContribution())
|
2015-11-29 23:20:16 +03:00
|
|
|
|
2017-07-30 21:46:59 +03:00
|
|
|
// At this point, all Alice's signatures should be fully populated.
|
|
|
|
aliceFundingSigs, aliceCommitSig := aliceChanReservation.OurSignatures()
|
|
|
|
if aliceFundingSigs == nil {
|
|
|
|
t.Fatalf("alice's funding signatures not populated")
|
|
|
|
}
|
|
|
|
if aliceCommitSig == nil {
|
|
|
|
t.Fatalf("alice's commit signatures not populated")
|
2015-11-29 23:20:16 +03:00
|
|
|
}
|
|
|
|
|
2017-07-30 21:46:59 +03:00
|
|
|
// Additionally, Bob's signatures should also be fully populated.
|
|
|
|
bobFundingSigs, bobCommitSig := bobChanReservation.OurSignatures()
|
|
|
|
if bobFundingSigs == nil {
|
|
|
|
t.Fatalf("bob's funding signatures not populated")
|
2015-12-23 07:32:18 +03:00
|
|
|
}
|
2017-07-30 21:46:59 +03:00
|
|
|
if bobCommitSig == nil {
|
|
|
|
t.Fatalf("bob's commit signatures not populated")
|
|
|
|
}
|
|
|
|
|
2018-02-18 02:25:55 +03:00
|
|
|
// To conclude, we'll consume first Alice's signatures with Bob, and
|
2017-07-30 21:46:59 +03:00
|
|
|
// then the other way around.
|
|
|
|
_, err = aliceChanReservation.CompleteReservation(
|
|
|
|
bobFundingSigs, bobCommitSig,
|
|
|
|
)
|
|
|
|
if err != nil {
|
2017-11-10 03:30:20 +03:00
|
|
|
for _, in := range aliceChanReservation.FinalFundingTx().TxIn {
|
|
|
|
fmt.Println(in.PreviousOutPoint.String())
|
|
|
|
}
|
2017-07-30 21:46:59 +03:00
|
|
|
t.Fatalf("unable to consume alice's sigs: %v", err)
|
2015-11-29 23:20:16 +03:00
|
|
|
}
|
2017-07-30 21:46:59 +03:00
|
|
|
_, err = bobChanReservation.CompleteReservation(
|
|
|
|
aliceFundingSigs, aliceCommitSig,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to consume bob's sigs: %v", err)
|
2016-06-30 22:13:46 +03:00
|
|
|
}
|
2016-08-13 01:50:47 +03:00
|
|
|
|
2017-07-30 21:46:59 +03:00
|
|
|
// At this point, the funding tx should have been populated.
|
|
|
|
fundingTx := aliceChanReservation.FinalFundingTx()
|
2016-08-13 01:50:47 +03:00
|
|
|
if fundingTx == nil {
|
2015-11-29 23:20:16 +03:00
|
|
|
t.Fatalf("funding transaction never created!")
|
|
|
|
}
|
2016-08-13 01:50:47 +03:00
|
|
|
|
2017-07-30 21:46:59 +03:00
|
|
|
// The resulting active channel state should have been persisted to the
|
|
|
|
// DB.
|
|
|
|
fundingSha := fundingTx.TxHash()
|
|
|
|
aliceChannels, err := alice.Cfg.Database.FetchOpenChannels(bobPub)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to retrieve channel from DB: %v", err)
|
2015-12-23 07:32:18 +03:00
|
|
|
}
|
2017-07-30 21:46:59 +03:00
|
|
|
if !bytes.Equal(aliceChannels[0].FundingOutpoint.Hash[:], fundingSha[:]) {
|
|
|
|
t.Fatalf("channel state not properly saved")
|
2015-11-29 23:20:16 +03:00
|
|
|
}
|
2019-10-31 05:24:49 +03:00
|
|
|
if !aliceChannels[0].ChanType.IsDualFunder() {
|
2017-07-30 21:46:59 +03:00
|
|
|
t.Fatalf("channel not detected as dual funder")
|
2015-12-19 06:47:47 +03:00
|
|
|
}
|
2017-07-30 21:46:59 +03:00
|
|
|
bobChannels, err := bob.Cfg.Database.FetchOpenChannels(alicePub)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to retrieve channel from DB: %v", err)
|
2015-11-29 23:20:16 +03:00
|
|
|
}
|
2017-07-30 21:46:59 +03:00
|
|
|
if !bytes.Equal(bobChannels[0].FundingOutpoint.Hash[:], fundingSha[:]) {
|
|
|
|
t.Fatalf("channel state not properly saved")
|
2015-12-23 07:32:18 +03:00
|
|
|
}
|
2019-10-31 05:24:49 +03:00
|
|
|
if !bobChannels[0].ChanType.IsDualFunder() {
|
2017-07-30 21:46:59 +03:00
|
|
|
t.Fatalf("channel not detected as dual funder")
|
2015-11-29 23:20:16 +03:00
|
|
|
}
|
|
|
|
|
2018-07-26 20:17:42 +03:00
|
|
|
// Let Alice publish the funding transaction.
|
|
|
|
if err := alice.PublishTransaction(fundingTx); err != nil {
|
|
|
|
t.Fatalf("unable to publish funding tx: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-07-30 21:46:59 +03:00
|
|
|
// Mine a single block, the funding transaction should be included
|
|
|
|
// within this block.
|
2017-11-10 03:30:20 +03:00
|
|
|
err = waitForMempoolTx(miner, &fundingSha)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("tx not relayed to miner: %v", err)
|
|
|
|
}
|
2017-07-30 21:46:59 +03:00
|
|
|
blockHashes, err := miner.Node.Generate(1)
|
2015-12-28 23:15:46 +03:00
|
|
|
if err != nil {
|
2017-07-30 21:46:59 +03:00
|
|
|
t.Fatalf("unable to generate block: %v", err)
|
2015-12-28 23:15:46 +03:00
|
|
|
}
|
2017-07-30 21:46:59 +03:00
|
|
|
block, err := miner.Node.GetBlock(blockHashes[0])
|
2017-01-31 07:21:52 +03:00
|
|
|
if err != nil {
|
2017-07-30 21:46:59 +03:00
|
|
|
t.Fatalf("unable to find block: %v", err)
|
2015-11-29 23:20:16 +03:00
|
|
|
}
|
2017-07-30 21:46:59 +03:00
|
|
|
if len(block.Transactions) != 2 {
|
|
|
|
t.Fatalf("funding transaction wasn't mined: %v", err)
|
2015-12-26 09:05:55 +03:00
|
|
|
}
|
2017-07-30 21:46:59 +03:00
|
|
|
blockTx := block.Transactions[1]
|
|
|
|
if blockTx.TxHash() != fundingSha {
|
|
|
|
t.Fatalf("incorrect transaction was mined")
|
2015-12-26 09:05:55 +03:00
|
|
|
}
|
2017-07-30 21:46:59 +03:00
|
|
|
|
|
|
|
assertReservationDeleted(aliceChanReservation, t)
|
|
|
|
assertReservationDeleted(bobChanReservation, t)
|
2017-11-10 03:30:20 +03:00
|
|
|
|
|
|
|
// Wait for wallets to catch up to prevent issues in subsequent tests.
|
|
|
|
err = waitForWalletSync(miner, alice)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to sync alice: %v", err)
|
|
|
|
}
|
|
|
|
err = waitForWalletSync(miner, bob)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to sync bob: %v", err)
|
|
|
|
}
|
2015-11-19 01:59:07 +03:00
|
|
|
}
|
|
|
|
|
2016-08-13 01:50:47 +03:00
|
|
|
func testFundingTransactionLockedOutputs(miner *rpctest.Harness,
|
2017-07-30 21:46:59 +03:00
|
|
|
alice, _ *lnwallet.LightningWallet, t *testing.T) {
|
2016-10-16 00:12:18 +03:00
|
|
|
|
2016-09-08 22:45:04 +03:00
|
|
|
// Create a single channel asking for 16 BTC total.
|
2018-03-26 05:15:05 +03:00
|
|
|
fundingAmount, err := btcutil.NewAmount(8)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create amt: %v", err)
|
|
|
|
}
|
2018-07-28 04:20:58 +03:00
|
|
|
feePerKw, err := alice.Cfg.FeeEstimator.EstimateFeePerKW(1)
|
2017-11-23 09:29:08 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to query fee estimator: %v", err)
|
|
|
|
}
|
2018-08-10 05:16:14 +03:00
|
|
|
req := &lnwallet.InitFundingReserveMsg{
|
2019-07-11 14:14:36 +03:00
|
|
|
ChainHash: chainHash,
|
|
|
|
NodeID: bobPub,
|
|
|
|
NodeAddr: bobAddr,
|
|
|
|
LocalFundingAmt: fundingAmount,
|
|
|
|
RemoteFundingAmt: 0,
|
|
|
|
CommitFeePerKw: feePerKw,
|
|
|
|
FundingFeePerKw: feePerKw,
|
|
|
|
PushMSat: 0,
|
|
|
|
Flags: lnwire.FFAnnounceChannel,
|
2020-03-31 10:13:16 +03:00
|
|
|
PendingChanID: [32]byte{0, 1, 2, 3},
|
2018-08-10 05:16:14 +03:00
|
|
|
}
|
|
|
|
if _, err := alice.InitChannelReservation(req); err != nil {
|
2015-12-16 22:22:36 +03:00
|
|
|
t.Fatalf("unable to initialize funding reservation 1: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-09-08 22:45:04 +03:00
|
|
|
// Now attempt to reserve funds for another channel, this time
|
|
|
|
// requesting 900 BTC. We only have around 64BTC worth of outpoints
|
|
|
|
// that aren't locked, so this should fail.
|
2018-03-26 05:15:05 +03:00
|
|
|
amt, err := btcutil.NewAmount(900)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create amt: %v", err)
|
|
|
|
}
|
2018-08-10 05:16:14 +03:00
|
|
|
failedReq := &lnwallet.InitFundingReserveMsg{
|
2019-07-11 14:14:36 +03:00
|
|
|
ChainHash: chainHash,
|
|
|
|
NodeID: bobPub,
|
|
|
|
NodeAddr: bobAddr,
|
|
|
|
LocalFundingAmt: amt,
|
|
|
|
RemoteFundingAmt: 0,
|
|
|
|
CommitFeePerKw: feePerKw,
|
|
|
|
FundingFeePerKw: feePerKw,
|
|
|
|
PushMSat: 0,
|
|
|
|
Flags: lnwire.FFAnnounceChannel,
|
2020-03-31 10:13:16 +03:00
|
|
|
PendingChanID: [32]byte{1, 2, 3, 4},
|
2018-08-10 05:16:14 +03:00
|
|
|
}
|
|
|
|
failedReservation, err := alice.InitChannelReservation(failedReq)
|
2015-12-16 22:22:36 +03:00
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("not error returned, should fail on coin selection")
|
|
|
|
}
|
2019-11-01 07:39:17 +03:00
|
|
|
if _, ok := err.(*chanfunding.ErrInsufficientFunds); !ok {
|
2015-12-16 22:22:36 +03:00
|
|
|
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-08-13 01:50:47 +03:00
|
|
|
func testFundingCancellationNotEnoughFunds(miner *rpctest.Harness,
|
2017-07-30 21:46:59 +03:00
|
|
|
alice, _ *lnwallet.LightningWallet, t *testing.T) {
|
2016-08-13 01:50:47 +03:00
|
|
|
|
2018-07-28 04:20:58 +03:00
|
|
|
feePerKw, err := alice.Cfg.FeeEstimator.EstimateFeePerKW(1)
|
2017-11-23 09:29:08 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to query fee estimator: %v", err)
|
|
|
|
}
|
2017-05-17 04:55:22 +03:00
|
|
|
|
2016-08-13 01:50:47 +03:00
|
|
|
// Create a reservation for 44 BTC.
|
2018-03-26 05:15:05 +03:00
|
|
|
fundingAmount, err := btcutil.NewAmount(44)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create amt: %v", err)
|
|
|
|
}
|
2018-08-10 05:16:14 +03:00
|
|
|
req := &lnwallet.InitFundingReserveMsg{
|
2019-07-11 14:14:36 +03:00
|
|
|
ChainHash: chainHash,
|
|
|
|
NodeID: bobPub,
|
|
|
|
NodeAddr: bobAddr,
|
|
|
|
LocalFundingAmt: fundingAmount,
|
|
|
|
RemoteFundingAmt: 0,
|
|
|
|
CommitFeePerKw: feePerKw,
|
|
|
|
FundingFeePerKw: feePerKw,
|
|
|
|
PushMSat: 0,
|
|
|
|
Flags: lnwire.FFAnnounceChannel,
|
2020-03-31 10:13:16 +03:00
|
|
|
PendingChanID: [32]byte{2, 3, 4, 5},
|
2018-08-10 05:16:14 +03:00
|
|
|
}
|
|
|
|
chanReservation, err := alice.InitChannelReservation(req)
|
2015-12-16 23:41:15 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to initialize funding reservation: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-08-13 01:50:47 +03:00
|
|
|
// Attempt to create another channel with 44 BTC, this should fail.
|
2020-03-31 10:13:16 +03:00
|
|
|
req.PendingChanID = [32]byte{3, 4, 5, 6}
|
2018-08-10 05:16:14 +03:00
|
|
|
_, err = alice.InitChannelReservation(req)
|
2019-11-01 07:39:17 +03:00
|
|
|
if _, ok := err.(*chanfunding.ErrInsufficientFunds); !ok {
|
2018-02-07 06:11:11 +03:00
|
|
|
t.Fatalf("coin selection succeeded should have insufficient funds: %v",
|
2016-08-13 01:50:47 +03:00
|
|
|
err)
|
2015-12-16 23:41:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2017-07-30 21:46:59 +03:00
|
|
|
lockedOutPoints := alice.LockedOutpoints()
|
2015-12-16 23:41:15 +03:00
|
|
|
if len(lockedOutPoints) != 0 {
|
|
|
|
t.Fatalf("outpoints still locked")
|
|
|
|
}
|
|
|
|
|
2016-08-13 01:50:47 +03:00
|
|
|
// Reservation ID should no longer be tracked.
|
2017-07-30 21:46:59 +03:00
|
|
|
numReservations := alice.ActiveReservations()
|
|
|
|
if len(alice.ActiveReservations()) != 0 {
|
2016-08-13 01:50:47 +03:00
|
|
|
t.Fatalf("should have 0 reservations, instead have %v",
|
|
|
|
numReservations)
|
2015-12-16 23:41:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(roasbeef): create method like Balance that ignores locked
|
|
|
|
// outpoints, will let us fail early/fast instead of querying and
|
|
|
|
// attempting coin selection.
|
|
|
|
|
2016-11-16 23:54:27 +03:00
|
|
|
// Request to fund a new channel should now succeed.
|
2020-03-31 10:13:16 +03:00
|
|
|
req.PendingChanID = [32]byte{4, 5, 6, 7, 8}
|
2018-08-10 05:16:14 +03:00
|
|
|
if _, err := alice.InitChannelReservation(req); err != nil {
|
2015-12-16 23:41:15 +03:00
|
|
|
t.Fatalf("unable to initialize funding reservation: %v", err)
|
|
|
|
}
|
2015-11-19 01:59:07 +03:00
|
|
|
}
|
|
|
|
|
2018-02-07 06:11:11 +03:00
|
|
|
func testCancelNonExistentReservation(miner *rpctest.Harness,
|
2017-07-30 21:46:59 +03:00
|
|
|
alice, _ *lnwallet.LightningWallet, t *testing.T) {
|
2016-08-13 01:50:47 +03:00
|
|
|
|
2018-07-28 04:20:58 +03:00
|
|
|
feePerKw, err := alice.Cfg.FeeEstimator.EstimateFeePerKW(1)
|
2017-11-23 09:29:08 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to query fee estimator: %v", err)
|
|
|
|
}
|
2016-10-16 00:12:18 +03:00
|
|
|
|
2015-12-16 23:41:15 +03:00
|
|
|
// Create our own reservation, give it some ID.
|
2017-11-26 22:32:57 +03:00
|
|
|
res, err := lnwallet.NewChannelReservation(
|
2018-07-28 04:20:58 +03:00
|
|
|
10000, 10000, feePerKw, alice, 22, 10, &testHdSeed,
|
2020-03-06 18:11:48 +03:00
|
|
|
lnwire.FFAnnounceChannel, lnwallet.CommitmentTypeTweakless,
|
2020-03-14 02:57:48 +03:00
|
|
|
nil, [32]byte{}, 0,
|
2017-11-23 09:29:08 +03:00
|
|
|
)
|
2017-11-26 22:32:57 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create res: %v", err)
|
|
|
|
}
|
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 {
|
2019-10-03 18:22:43 +03:00
|
|
|
t.Fatalf("canceled non-existent reservation")
|
2015-12-16 23:41:15 +03:00
|
|
|
}
|
2015-11-19 01:59:07 +03:00
|
|
|
}
|
|
|
|
|
2017-11-26 22:32:57 +03:00
|
|
|
func testReservationInitiatorBalanceBelowDustCancel(miner *rpctest.Harness,
|
|
|
|
alice, _ *lnwallet.LightningWallet, t *testing.T) {
|
|
|
|
|
2019-07-11 14:14:36 +03:00
|
|
|
// We'll attempt to create a new reservation with an extremely high
|
|
|
|
// commitment fee rate. This should push our balance into the negative
|
|
|
|
// and result in a failure to create the reservation.
|
2018-07-28 04:20:58 +03:00
|
|
|
const numBTC = 4
|
|
|
|
fundingAmount, err := btcutil.NewAmount(numBTC)
|
2018-03-26 05:15:05 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create amt: %v", err)
|
|
|
|
}
|
2018-07-28 04:20:58 +03:00
|
|
|
|
2019-10-31 05:43:05 +03:00
|
|
|
feePerKw := chainfee.SatPerKWeight(
|
2018-07-28 04:20:58 +03:00
|
|
|
numBTC * numBTC * btcutil.SatoshiPerBitcoin,
|
|
|
|
)
|
2018-08-10 05:16:14 +03:00
|
|
|
req := &lnwallet.InitFundingReserveMsg{
|
2019-07-11 14:14:36 +03:00
|
|
|
ChainHash: chainHash,
|
|
|
|
NodeID: bobPub,
|
|
|
|
NodeAddr: bobAddr,
|
|
|
|
LocalFundingAmt: fundingAmount,
|
|
|
|
RemoteFundingAmt: 0,
|
|
|
|
CommitFeePerKw: feePerKw,
|
2019-07-11 14:14:36 +03:00
|
|
|
FundingFeePerKw: 1000,
|
2019-07-11 14:14:36 +03:00
|
|
|
PushMSat: 0,
|
|
|
|
Flags: lnwire.FFAnnounceChannel,
|
2020-03-06 18:11:48 +03:00
|
|
|
CommitType: lnwallet.CommitmentTypeTweakless,
|
2018-08-10 05:16:14 +03:00
|
|
|
}
|
|
|
|
_, err = alice.InitChannelReservation(req)
|
2017-11-26 22:32:57 +03:00
|
|
|
switch {
|
|
|
|
case err == nil:
|
2018-02-07 06:11:11 +03:00
|
|
|
t.Fatalf("initialization should have failed due to " +
|
2017-11-26 22:32:57 +03:00
|
|
|
"insufficient local amount")
|
|
|
|
|
2018-03-01 01:07:13 +03:00
|
|
|
case !strings.Contains(err.Error(), "Funder balance too small"):
|
2017-11-26 22:32:57 +03:00
|
|
|
t.Fatalf("incorrect error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-30 21:46:59 +03:00
|
|
|
func assertContributionInitPopulated(t *testing.T, c *lnwallet.ChannelContribution) {
|
|
|
|
_, _, line, _ := runtime.Caller(1)
|
2016-08-13 01:50:47 +03:00
|
|
|
|
2017-07-30 21:46:59 +03:00
|
|
|
if c.FirstCommitmentPoint == nil {
|
|
|
|
t.Fatalf("line #%v: commitment point not fond", line)
|
|
|
|
}
|
2016-10-16 00:12:18 +03:00
|
|
|
|
2017-07-30 21:46:59 +03:00
|
|
|
if c.CsvDelay == 0 {
|
|
|
|
t.Fatalf("line #%v: csv delay not set", line)
|
|
|
|
}
|
2016-06-21 20:40:20 +03:00
|
|
|
|
2018-02-18 02:25:55 +03:00
|
|
|
if c.MultiSigKey.PubKey == nil {
|
2017-07-30 21:46:59 +03:00
|
|
|
t.Fatalf("line #%v: multi-sig key not set", line)
|
|
|
|
}
|
2018-02-18 02:25:55 +03:00
|
|
|
if c.RevocationBasePoint.PubKey == nil {
|
2017-07-30 21:46:59 +03:00
|
|
|
t.Fatalf("line #%v: revocation key not set", line)
|
|
|
|
}
|
2018-02-18 02:25:55 +03:00
|
|
|
if c.PaymentBasePoint.PubKey == nil {
|
2017-07-30 21:46:59 +03:00
|
|
|
t.Fatalf("line #%v: payment key not set", line)
|
|
|
|
}
|
2018-02-18 02:25:55 +03:00
|
|
|
if c.DelayBasePoint.PubKey == nil {
|
2017-07-30 21:46:59 +03:00
|
|
|
t.Fatalf("line #%v: delay key not set", line)
|
2016-06-21 20:40:20 +03:00
|
|
|
}
|
|
|
|
|
2017-07-30 21:46:59 +03:00
|
|
|
if c.DustLimit == 0 {
|
|
|
|
t.Fatalf("line #%v: dust limit not set", line)
|
|
|
|
}
|
|
|
|
if c.MaxPendingAmount == 0 {
|
|
|
|
t.Fatalf("line #%v: max pending amt not set", line)
|
|
|
|
}
|
|
|
|
if c.ChanReserve == 0 {
|
|
|
|
t.Fatalf("line #%v: chan reserve not set", line)
|
|
|
|
}
|
|
|
|
if c.MinHTLC == 0 {
|
|
|
|
t.Fatalf("line #%v: min htlc not set", line)
|
|
|
|
}
|
|
|
|
if c.MaxAcceptedHtlcs == 0 {
|
|
|
|
t.Fatalf("line #%v: max accepted htlc's not set", line)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testSingleFunderReservationWorkflow(miner *rpctest.Harness,
|
2020-03-06 18:11:48 +03:00
|
|
|
alice, bob *lnwallet.LightningWallet, t *testing.T,
|
|
|
|
commitType lnwallet.CommitmentType,
|
2020-03-14 02:57:48 +03:00
|
|
|
aliceChanFunder chanfunding.Assembler, fetchFundingTx func() *wire.MsgTx,
|
|
|
|
pendingChanID [32]byte, thawHeight uint32) {
|
2017-07-30 21:46:59 +03:00
|
|
|
|
|
|
|
// For this scenario, Alice will be the channel initiator while bob
|
|
|
|
// will act as the responder to the workflow.
|
|
|
|
|
|
|
|
// First, Alice will Initialize a reservation for a channel with 4 BTC
|
|
|
|
// funded solely by us. We'll also initially push 1 BTC of the channel
|
|
|
|
// towards Bob's side.
|
2018-03-26 05:15:05 +03:00
|
|
|
fundingAmt, err := btcutil.NewAmount(4)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create amt: %v", err)
|
|
|
|
}
|
2017-08-22 09:20:57 +03:00
|
|
|
pushAmt := lnwire.NewMSatFromSatoshis(btcutil.SatoshiPerBitcoin)
|
2018-07-28 04:20:58 +03:00
|
|
|
feePerKw, err := alice.Cfg.FeeEstimator.EstimateFeePerKW(1)
|
2017-11-23 09:29:08 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to query fee estimator: %v", err)
|
|
|
|
}
|
2018-08-10 05:16:14 +03:00
|
|
|
aliceReq := &lnwallet.InitFundingReserveMsg{
|
2019-07-11 14:14:36 +03:00
|
|
|
ChainHash: chainHash,
|
2019-11-01 07:47:50 +03:00
|
|
|
PendingChanID: pendingChanID,
|
2019-07-11 14:14:36 +03:00
|
|
|
NodeID: bobPub,
|
|
|
|
NodeAddr: bobAddr,
|
|
|
|
LocalFundingAmt: fundingAmt,
|
|
|
|
RemoteFundingAmt: 0,
|
|
|
|
CommitFeePerKw: feePerKw,
|
|
|
|
FundingFeePerKw: feePerKw,
|
|
|
|
PushMSat: pushAmt,
|
|
|
|
Flags: lnwire.FFAnnounceChannel,
|
2020-03-06 18:11:48 +03:00
|
|
|
CommitType: commitType,
|
2019-11-01 07:47:50 +03:00
|
|
|
ChanFunder: aliceChanFunder,
|
2018-08-10 05:16:14 +03:00
|
|
|
}
|
|
|
|
aliceChanReservation, err := alice.InitChannelReservation(aliceReq)
|
2016-06-21 20:40:20 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to init channel reservation: %v", err)
|
|
|
|
}
|
2017-07-30 21:46:59 +03:00
|
|
|
aliceChanReservation.SetNumConfsRequired(numReqConfs)
|
2018-12-11 00:58:34 +03:00
|
|
|
channelConstraints := &channeldb.ChannelConstraints{
|
|
|
|
DustLimit: lnwallet.DefaultDustLimit(),
|
|
|
|
ChanReserve: fundingAmt / 100,
|
|
|
|
MaxPendingAmount: lnwire.NewMSatFromSatoshis(fundingAmt),
|
|
|
|
MinHTLC: 1,
|
2019-01-16 17:47:43 +03:00
|
|
|
MaxAcceptedHtlcs: input.MaxHTLCNumber / 2,
|
2018-12-11 00:58:34 +03:00
|
|
|
CsvDelay: csvDelay,
|
|
|
|
}
|
2019-01-30 04:56:28 +03:00
|
|
|
err = aliceChanReservation.CommitConstraints(channelConstraints)
|
2018-05-14 21:04:34 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to verify constraints: %v", err)
|
|
|
|
}
|
2016-06-21 20:40:20 +03:00
|
|
|
|
2019-11-01 07:47:50 +03:00
|
|
|
// Verify all contribution fields have been set properly, but only if
|
|
|
|
// Alice is the funder herself.
|
2017-07-30 21:46:59 +03:00
|
|
|
aliceContribution := aliceChanReservation.OurContribution()
|
2019-11-01 07:47:50 +03:00
|
|
|
if fetchFundingTx == nil {
|
|
|
|
if len(aliceContribution.Inputs) < 1 {
|
|
|
|
t.Fatalf("outputs for funding tx not properly "+
|
|
|
|
"selected, have %v outputs should at least 1",
|
|
|
|
len(aliceContribution.Inputs))
|
|
|
|
}
|
|
|
|
if len(aliceContribution.ChangeOutputs) != 1 {
|
|
|
|
t.Fatalf("coin selection failed, should have one "+
|
|
|
|
"change outputs, instead have: %v",
|
|
|
|
len(aliceContribution.ChangeOutputs))
|
|
|
|
}
|
2016-06-21 20:40:20 +03:00
|
|
|
}
|
2017-07-30 21:46:59 +03:00
|
|
|
assertContributionInitPopulated(t, aliceContribution)
|
|
|
|
|
|
|
|
// Next, Bob receives the initial request, generates a corresponding
|
|
|
|
// reservation initiation, then consume Alice's contribution.
|
2018-08-10 05:16:14 +03:00
|
|
|
bobReq := &lnwallet.InitFundingReserveMsg{
|
2019-07-11 14:14:36 +03:00
|
|
|
ChainHash: chainHash,
|
2019-11-01 07:47:50 +03:00
|
|
|
PendingChanID: pendingChanID,
|
2019-07-11 14:14:36 +03:00
|
|
|
NodeID: alicePub,
|
|
|
|
NodeAddr: aliceAddr,
|
|
|
|
LocalFundingAmt: 0,
|
|
|
|
RemoteFundingAmt: fundingAmt,
|
|
|
|
CommitFeePerKw: feePerKw,
|
|
|
|
FundingFeePerKw: feePerKw,
|
|
|
|
PushMSat: pushAmt,
|
|
|
|
Flags: lnwire.FFAnnounceChannel,
|
2020-03-06 18:11:48 +03:00
|
|
|
CommitType: commitType,
|
2018-08-10 05:16:14 +03:00
|
|
|
}
|
|
|
|
bobChanReservation, err := bob.InitChannelReservation(bobReq)
|
2017-07-30 21:46:59 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create bob reservation: %v", err)
|
2016-06-21 20:40:20 +03:00
|
|
|
}
|
2019-01-30 04:56:28 +03:00
|
|
|
err = bobChanReservation.CommitConstraints(channelConstraints)
|
2018-05-14 21:04:34 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to verify constraints: %v", err)
|
|
|
|
}
|
2017-07-30 21:46:59 +03:00
|
|
|
bobChanReservation.SetNumConfsRequired(numReqConfs)
|
|
|
|
|
|
|
|
// We'll ensure that Bob's contribution also gets generated properly.
|
|
|
|
bobContribution := bobChanReservation.OurContribution()
|
|
|
|
assertContributionInitPopulated(t, bobContribution)
|
|
|
|
|
|
|
|
// With his contribution generated, he can now process Alice's
|
|
|
|
// contribution.
|
|
|
|
err = bobChanReservation.ProcessSingleContribution(aliceContribution)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bob unable to process alice's contribution: %v", err)
|
2016-06-21 20:40:20 +03:00
|
|
|
}
|
2017-07-30 21:46:59 +03:00
|
|
|
assertContributionInitPopulated(t, bobChanReservation.TheirContribution())
|
2016-06-21 20:40:20 +03:00
|
|
|
|
2017-07-30 21:46:59 +03:00
|
|
|
// Bob will next send over his contribution to Alice, we simulate this
|
|
|
|
// by having Alice immediately process his contribution.
|
|
|
|
err = aliceChanReservation.ProcessContribution(bobContribution)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("alice unable to process bob's contribution")
|
2016-06-21 20:40:20 +03:00
|
|
|
}
|
2017-07-30 21:46:59 +03:00
|
|
|
assertContributionInitPopulated(t, bobChanReservation.TheirContribution())
|
2016-06-21 20:40:20 +03:00
|
|
|
|
2017-07-30 21:46:59 +03:00
|
|
|
// At this point, Alice should have generated all the signatures
|
|
|
|
// required for the funding transaction, as well as Alice's commitment
|
2019-11-01 07:47:50 +03:00
|
|
|
// signature to bob, but only if the funding transaction was
|
|
|
|
// constructed internally.
|
2017-07-30 21:46:59 +03:00
|
|
|
aliceRemoteContribution := aliceChanReservation.TheirContribution()
|
|
|
|
aliceFundingSigs, aliceCommitSig := aliceChanReservation.OurSignatures()
|
2019-11-01 07:47:50 +03:00
|
|
|
if fetchFundingTx == nil && aliceFundingSigs == nil {
|
2016-09-08 22:45:04 +03:00
|
|
|
t.Fatalf("funding sigs not found")
|
2016-06-21 20:40:20 +03:00
|
|
|
}
|
2017-07-30 21:46:59 +03:00
|
|
|
if aliceCommitSig == nil {
|
2016-06-21 20:40:20 +03:00
|
|
|
t.Fatalf("commitment sig not found")
|
|
|
|
}
|
2017-07-30 21:46:59 +03:00
|
|
|
|
|
|
|
// Additionally, the funding tx and the funding outpoint should have
|
|
|
|
// been populated.
|
2019-11-01 07:47:50 +03:00
|
|
|
if aliceChanReservation.FinalFundingTx() == nil && fetchFundingTx == nil {
|
2016-06-21 20:40:20 +03:00
|
|
|
t.Fatalf("funding transaction never created!")
|
|
|
|
}
|
2017-07-30 21:46:59 +03:00
|
|
|
if aliceChanReservation.FundingOutpoint() == nil {
|
|
|
|
t.Fatalf("funding outpoint never created!")
|
|
|
|
}
|
|
|
|
|
2016-06-21 20:40:20 +03:00
|
|
|
// Their funds should also be filled in.
|
2017-07-30 21:46:59 +03:00
|
|
|
if len(aliceRemoteContribution.Inputs) != 0 {
|
2016-06-21 20:40:20 +03:00
|
|
|
t.Fatalf("bob shouldn't have any inputs, instead has %v",
|
2017-07-30 21:46:59 +03:00
|
|
|
len(aliceRemoteContribution.Inputs))
|
2016-06-21 20:40:20 +03:00
|
|
|
}
|
2017-07-30 21:46:59 +03:00
|
|
|
if len(aliceRemoteContribution.ChangeOutputs) != 0 {
|
2016-06-21 20:40:20 +03:00
|
|
|
t.Fatalf("bob shouldn't have any change outputs, instead "+
|
2017-07-30 21:46:59 +03:00
|
|
|
"has %v",
|
|
|
|
aliceRemoteContribution.ChangeOutputs[0].Value)
|
2016-06-21 20:40:20 +03:00
|
|
|
}
|
|
|
|
|
2017-07-30 21:46:59 +03:00
|
|
|
// Next, Alice will send over her signature for Bob's commitment
|
|
|
|
// transaction, as well as the funding outpoint.
|
|
|
|
fundingPoint := aliceChanReservation.FundingOutpoint()
|
|
|
|
_, err = bobChanReservation.CompleteReservationSingle(
|
|
|
|
fundingPoint, aliceCommitSig,
|
|
|
|
)
|
2016-06-21 20:40:20 +03:00
|
|
|
if err != nil {
|
2017-07-30 21:46:59 +03:00
|
|
|
t.Fatalf("bob unable to consume single reservation: %v", err)
|
2016-06-21 20:40:20 +03:00
|
|
|
}
|
|
|
|
|
2017-07-30 21:46:59 +03:00
|
|
|
// Finally, we'll conclude the reservation process by sending over
|
|
|
|
// Bob's commitment signature, which is the final thing Alice needs to
|
|
|
|
// be able to safely broadcast the funding transaction.
|
|
|
|
_, bobCommitSig := bobChanReservation.OurSignatures()
|
|
|
|
if bobCommitSig == nil {
|
|
|
|
t.Fatalf("bob failed to generate commitment signature: %v", err)
|
|
|
|
}
|
|
|
|
_, err = aliceChanReservation.CompleteReservation(
|
|
|
|
nil, bobCommitSig,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("alice unable to complete reservation: %v", err)
|
|
|
|
}
|
2016-06-21 20:40:20 +03:00
|
|
|
|
2019-11-01 07:47:50 +03:00
|
|
|
// If the caller provided an alternative way to obtain the funding tx,
|
|
|
|
// then we'll use that. Otherwise, we'll obtain it directly from Alice.
|
|
|
|
var fundingTx *wire.MsgTx
|
|
|
|
if fetchFundingTx != nil {
|
|
|
|
fundingTx = fetchFundingTx()
|
|
|
|
} else {
|
|
|
|
fundingTx = aliceChanReservation.FinalFundingTx()
|
|
|
|
}
|
|
|
|
|
2017-07-30 21:46:59 +03:00
|
|
|
// The resulting active channel state should have been persisted to the
|
|
|
|
// DB for both Alice and Bob.
|
2017-01-06 00:56:27 +03:00
|
|
|
fundingSha := fundingTx.TxHash()
|
2017-07-30 21:46:59 +03:00
|
|
|
aliceChannels, err := alice.Cfg.Database.FetchOpenChannels(bobPub)
|
2016-06-21 20:40:20 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to retrieve channel from DB: %v", err)
|
|
|
|
}
|
2017-07-30 21:46:59 +03:00
|
|
|
if len(aliceChannels) != 1 {
|
|
|
|
t.Fatalf("alice didn't save channel state: %v", err)
|
|
|
|
}
|
|
|
|
if !bytes.Equal(aliceChannels[0].FundingOutpoint.Hash[:], fundingSha[:]) {
|
2016-06-21 20:40:20 +03:00
|
|
|
t.Fatalf("channel state not properly saved: %v vs %v",
|
2017-07-30 21:46:59 +03:00
|
|
|
hex.EncodeToString(aliceChannels[0].FundingOutpoint.Hash[:]),
|
2016-06-21 20:40:20 +03:00
|
|
|
hex.EncodeToString(fundingSha[:]))
|
|
|
|
}
|
2017-07-30 21:46:59 +03:00
|
|
|
if !aliceChannels[0].IsInitiator {
|
2016-11-16 23:54:27 +03:00
|
|
|
t.Fatalf("alice not detected as channel initiator")
|
|
|
|
}
|
2019-08-01 06:16:52 +03:00
|
|
|
if !aliceChannels[0].ChanType.IsSingleFunder() {
|
2016-11-16 23:54:27 +03:00
|
|
|
t.Fatalf("channel type is incorrect, expected %v instead got %v",
|
2019-10-31 05:24:49 +03:00
|
|
|
channeldb.SingleFunderBit, aliceChannels[0].ChanType)
|
2016-11-16 23:54:27 +03:00
|
|
|
}
|
2016-06-21 20:40:20 +03:00
|
|
|
|
2017-07-30 21:46:59 +03:00
|
|
|
bobChannels, err := bob.Cfg.Database.FetchOpenChannels(alicePub)
|
2016-06-21 20:40:20 +03:00
|
|
|
if err != nil {
|
2017-07-30 21:46:59 +03:00
|
|
|
t.Fatalf("unable to retrieve channel from DB: %v", err)
|
2016-06-21 20:40:20 +03:00
|
|
|
}
|
2017-07-30 21:46:59 +03:00
|
|
|
if len(bobChannels) != 1 {
|
|
|
|
t.Fatalf("bob didn't save channel state: %v", err)
|
2016-06-21 20:40:20 +03:00
|
|
|
}
|
2017-07-30 21:46:59 +03:00
|
|
|
if !bytes.Equal(bobChannels[0].FundingOutpoint.Hash[:], fundingSha[:]) {
|
|
|
|
t.Fatalf("channel state not properly saved: %v vs %v",
|
|
|
|
hex.EncodeToString(bobChannels[0].FundingOutpoint.Hash[:]),
|
|
|
|
hex.EncodeToString(fundingSha[:]))
|
2016-06-21 20:40:20 +03:00
|
|
|
}
|
2017-07-30 21:46:59 +03:00
|
|
|
if bobChannels[0].IsInitiator {
|
|
|
|
t.Fatalf("bob not detected as channel responder")
|
2016-06-21 20:40:20 +03:00
|
|
|
}
|
2019-08-01 06:16:52 +03:00
|
|
|
if !bobChannels[0].ChanType.IsSingleFunder() {
|
2017-07-30 21:46:59 +03:00
|
|
|
t.Fatalf("channel type is incorrect, expected %v instead got %v",
|
2019-10-31 05:24:49 +03:00
|
|
|
channeldb.SingleFunderBit, bobChannels[0].ChanType)
|
2016-06-21 20:40:20 +03:00
|
|
|
}
|
|
|
|
|
2018-07-26 20:17:42 +03:00
|
|
|
// Let Alice publish the funding transaction.
|
|
|
|
if err := alice.PublishTransaction(fundingTx); err != nil {
|
|
|
|
t.Fatalf("unable to publish funding tx: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-07-30 21:46:59 +03:00
|
|
|
// Mine a single block, the funding transaction should be included
|
|
|
|
// within this block.
|
2017-11-10 03:30:20 +03:00
|
|
|
err = waitForMempoolTx(miner, &fundingSha)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("tx not relayed to miner: %v", err)
|
|
|
|
}
|
2017-07-30 21:46:59 +03:00
|
|
|
blockHashes, err := miner.Node.Generate(1)
|
2016-06-21 20:40:20 +03:00
|
|
|
if err != nil {
|
2017-07-30 21:46:59 +03:00
|
|
|
t.Fatalf("unable to generate block: %v", err)
|
2016-06-21 20:40:20 +03:00
|
|
|
}
|
2017-07-30 21:46:59 +03:00
|
|
|
block, err := miner.Node.GetBlock(blockHashes[0])
|
2016-11-16 23:54:27 +03:00
|
|
|
if err != nil {
|
2017-07-30 21:46:59 +03:00
|
|
|
t.Fatalf("unable to find block: %v", err)
|
2016-11-16 23:54:27 +03:00
|
|
|
}
|
2017-07-30 21:46:59 +03:00
|
|
|
if len(block.Transactions) != 2 {
|
2017-11-10 03:30:20 +03:00
|
|
|
t.Fatalf("funding transaction wasn't mined: %d",
|
|
|
|
len(block.Transactions))
|
2016-06-21 20:40:20 +03:00
|
|
|
}
|
2017-07-30 21:46:59 +03:00
|
|
|
blockTx := block.Transactions[1]
|
|
|
|
if blockTx.TxHash() != fundingSha {
|
|
|
|
t.Fatalf("incorrect transaction was mined")
|
2016-06-21 20:40:20 +03:00
|
|
|
}
|
|
|
|
|
2020-03-14 02:57:48 +03:00
|
|
|
// If a frozen channel was requested, then we expect that both channel
|
|
|
|
// types show as being a frozen channel type.
|
|
|
|
aliceChanFrozen := aliceChannels[0].ChanType.IsFrozen()
|
|
|
|
bobChanFrozen := bobChannels[0].ChanType.IsFrozen()
|
|
|
|
if thawHeight != 0 && (!aliceChanFrozen || !bobChanFrozen) {
|
|
|
|
t.Fatalf("expected both alice and bob to have frozen chans: "+
|
|
|
|
"alice_frozen=%v, bob_frozen=%v", aliceChanFrozen,
|
|
|
|
bobChanFrozen)
|
|
|
|
}
|
|
|
|
if thawHeight != bobChannels[0].ThawHeight {
|
|
|
|
t.Fatalf("wrong thaw height: expected %v got %v", thawHeight,
|
|
|
|
bobChannels[0].ThawHeight)
|
|
|
|
}
|
|
|
|
if thawHeight != aliceChannels[0].ThawHeight {
|
|
|
|
t.Fatalf("wrong thaw height: expected %v got %v", thawHeight,
|
|
|
|
aliceChannels[0].ThawHeight)
|
|
|
|
}
|
|
|
|
|
2017-07-30 21:46:59 +03:00
|
|
|
assertReservationDeleted(aliceChanReservation, t)
|
|
|
|
assertReservationDeleted(bobChanReservation, t)
|
2016-06-21 20:40:20 +03:00
|
|
|
}
|
|
|
|
|
2017-07-30 21:46:59 +03:00
|
|
|
func testListTransactionDetails(miner *rpctest.Harness,
|
|
|
|
alice, _ *lnwallet.LightningWallet, t *testing.T) {
|
2016-10-16 00:12:18 +03:00
|
|
|
|
|
|
|
// Create 5 new outputs spendable by the wallet.
|
|
|
|
const numTxns = 5
|
|
|
|
const outputAmt = btcutil.SatoshiPerBitcoin
|
2017-01-06 00:56:27 +03:00
|
|
|
txids := make(map[chainhash.Hash]struct{})
|
2016-10-16 00:12:18 +03:00
|
|
|
for i := 0; i < numTxns; i++ {
|
2017-07-30 21:46:59 +03:00
|
|
|
addr, err := alice.NewAddress(lnwallet.WitnessPubKey, false)
|
2016-10-16 00:12:18 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create new address: %v", err)
|
|
|
|
}
|
|
|
|
script, err := txscript.PayToAddrScript(addr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create output script: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-02-10 02:28:32 +03:00
|
|
|
output := &wire.TxOut{
|
|
|
|
Value: outputAmt,
|
|
|
|
PkScript: script,
|
|
|
|
}
|
2018-07-28 04:20:58 +03:00
|
|
|
txid, err := miner.SendOutputs([]*wire.TxOut{output}, 2500)
|
2016-10-16 00:12:18 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send coinbase: %v", err)
|
|
|
|
}
|
|
|
|
txids[*txid] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate 10 blocks to mine all the transactions created above.
|
|
|
|
const numBlocksMined = 10
|
|
|
|
blocks, err := miner.Node.Generate(numBlocksMined)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to mine blocks: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next, fetch all the current transaction details.
|
2017-11-10 03:30:20 +03:00
|
|
|
err = waitForWalletSync(miner, alice)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Couldn't sync Alice's wallet: %v", err)
|
|
|
|
}
|
2017-07-30 21:46:59 +03:00
|
|
|
txDetails, err := alice.ListTransactionDetails()
|
2016-10-16 00:12:18 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to fetch tx details: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-12-06 20:20:05 +03:00
|
|
|
// This is a mapping from:
|
|
|
|
// blockHash -> transactionHash -> transactionOutputs
|
|
|
|
blockTxOuts := make(map[chainhash.Hash]map[chainhash.Hash][]*wire.TxOut)
|
|
|
|
|
2016-10-16 00:12:18 +03:00
|
|
|
// Each of the transactions created above should be found with the
|
|
|
|
// proper details populated.
|
|
|
|
for _, txDetail := range txDetails {
|
|
|
|
if _, ok := txids[txDetail.Hash]; !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if txDetail.NumConfirmations != numBlocksMined {
|
|
|
|
t.Fatalf("num confs incorrect, got %v expected %v",
|
|
|
|
txDetail.NumConfirmations, numBlocksMined)
|
|
|
|
}
|
|
|
|
if txDetail.Value != outputAmt {
|
|
|
|
t.Fatalf("tx value incorrect, got %v expected %v",
|
|
|
|
txDetail.Value, outputAmt)
|
|
|
|
}
|
2017-12-06 20:20:05 +03:00
|
|
|
|
2016-10-16 00:12:18 +03:00
|
|
|
if !bytes.Equal(txDetail.BlockHash[:], blocks[0][:]) {
|
|
|
|
t.Fatalf("block hash mismatch, got %v expected %v",
|
|
|
|
txDetail.BlockHash, blocks[0])
|
|
|
|
}
|
|
|
|
|
2017-12-06 20:20:05 +03:00
|
|
|
// This fetches the transactions in a block so that we can compare the
|
|
|
|
// txouts stored in the mined transaction against the ones in the transaction
|
|
|
|
// details
|
|
|
|
if _, ok := blockTxOuts[*txDetail.BlockHash]; !ok {
|
|
|
|
fetchedBlock, err := alice.Cfg.ChainIO.GetBlock(txDetail.BlockHash)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err fetching block: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
transactions :=
|
|
|
|
make(map[chainhash.Hash][]*wire.TxOut, len(fetchedBlock.Transactions))
|
|
|
|
for _, tx := range fetchedBlock.Transactions {
|
|
|
|
transactions[tx.TxHash()] = tx.TxOut
|
|
|
|
}
|
|
|
|
|
|
|
|
blockTxOuts[fetchedBlock.BlockHash()] = transactions
|
|
|
|
}
|
|
|
|
|
|
|
|
if txOuts, ok := blockTxOuts[*txDetail.BlockHash][txDetail.Hash]; !ok {
|
|
|
|
t.Fatalf("tx (%v) not found in block (%v)",
|
|
|
|
txDetail.Hash, txDetail.BlockHash)
|
|
|
|
} else {
|
|
|
|
var destinationAddresses []btcutil.Address
|
|
|
|
|
|
|
|
for _, txOut := range txOuts {
|
|
|
|
_, addrs, _, err :=
|
|
|
|
txscript.ExtractPkScriptAddrs(txOut.PkScript, &alice.Cfg.NetParams)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err extract script addresses: %s", err)
|
|
|
|
}
|
|
|
|
destinationAddresses = append(destinationAddresses, addrs...)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(txDetail.DestAddresses, destinationAddresses) {
|
|
|
|
t.Fatalf("destination addresses mismatch, got %v expected %v",
|
|
|
|
txDetail.DestAddresses, destinationAddresses)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-16 00:12:18 +03:00
|
|
|
delete(txids, txDetail.Hash)
|
|
|
|
}
|
|
|
|
if len(txids) != 0 {
|
2018-09-13 06:51:27 +03:00
|
|
|
t.Fatalf("all transactions not found in details: left=%v, "+
|
|
|
|
"returned_set=%v", spew.Sdump(txids),
|
|
|
|
spew.Sdump(txDetails))
|
2016-10-16 00:12:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Next create a transaction paying to an output which isn't under the
|
|
|
|
// wallet's control.
|
2019-07-10 09:39:46 +03:00
|
|
|
minerAddr, err := miner.NewAddress()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to generate address: %v", err)
|
|
|
|
}
|
|
|
|
outputScript, err := txscript.PayToAddrScript(minerAddr)
|
2016-10-16 00:12:18 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to make output script: %v", err)
|
|
|
|
}
|
|
|
|
burnOutput := wire.NewTxOut(outputAmt, outputScript)
|
2018-11-05 14:30:32 +03:00
|
|
|
burnTX, err := alice.SendOutputs([]*wire.TxOut{burnOutput}, 2500)
|
2016-10-16 00:12:18 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create burn tx: %v", err)
|
|
|
|
}
|
2018-11-05 14:30:32 +03:00
|
|
|
burnTXID := burnTX.TxHash()
|
|
|
|
err = waitForMempoolTx(miner, &burnTXID)
|
2017-11-10 03:30:20 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("tx not relayed to miner: %v", err)
|
|
|
|
}
|
2018-09-13 06:51:27 +03:00
|
|
|
|
|
|
|
// Before we mine the next block, we'll ensure that the above
|
|
|
|
// transaction shows up in the set of unconfirmed transactions returned
|
|
|
|
// by ListTransactionDetails.
|
|
|
|
err = waitForWalletSync(miner, alice)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Couldn't sync Alice's wallet: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We should be able to find the transaction above in the set of
|
|
|
|
// returned transactions, and it should have a confirmation of -1,
|
|
|
|
// indicating that it's not yet mined.
|
|
|
|
txDetails, err = alice.ListTransactionDetails()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to fetch tx details: %v", err)
|
|
|
|
}
|
|
|
|
var mempoolTxFound bool
|
|
|
|
for _, txDetail := range txDetails {
|
|
|
|
if !bytes.Equal(txDetail.Hash[:], burnTXID[:]) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that we've found the transaction, ensure that it has a
|
|
|
|
// negative number of confirmations to indicate that it's
|
|
|
|
// unconfirmed.
|
|
|
|
mempoolTxFound = true
|
|
|
|
if txDetail.NumConfirmations != 0 {
|
|
|
|
t.Fatalf("num confs incorrect, got %v expected %v",
|
|
|
|
txDetail.NumConfirmations, 0)
|
|
|
|
}
|
2019-07-10 09:39:46 +03:00
|
|
|
|
|
|
|
// We test that each txDetail has destination addresses. This ensures
|
|
|
|
// that even when we have 0 confirmation transactions, the destination
|
|
|
|
// addresses are returned.
|
|
|
|
var match bool
|
|
|
|
for _, addr := range txDetail.DestAddresses {
|
|
|
|
if addr.String() == minerAddr.String() {
|
|
|
|
match = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !match {
|
|
|
|
t.Fatalf("minerAddr: %v should have been a dest addr", minerAddr)
|
|
|
|
}
|
2018-09-13 06:51:27 +03:00
|
|
|
}
|
|
|
|
if !mempoolTxFound {
|
|
|
|
t.Fatalf("unable to find mempool tx in tx details!")
|
|
|
|
}
|
|
|
|
|
2016-10-16 00:12:18 +03:00
|
|
|
burnBlock, err := miner.Node.Generate(1)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to mine block: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch the transaction details again, the new transaction should be
|
|
|
|
// shown as debiting from the wallet's balance.
|
2017-11-10 03:30:20 +03:00
|
|
|
err = waitForWalletSync(miner, alice)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Couldn't sync Alice's wallet: %v", err)
|
|
|
|
}
|
2017-07-30 21:46:59 +03:00
|
|
|
txDetails, err = alice.ListTransactionDetails()
|
2016-10-16 00:12:18 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to fetch tx details: %v", err)
|
|
|
|
}
|
|
|
|
var burnTxFound bool
|
|
|
|
for _, txDetail := range txDetails {
|
|
|
|
if !bytes.Equal(txDetail.Hash[:], burnTXID[:]) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
burnTxFound = true
|
|
|
|
if txDetail.NumConfirmations != 1 {
|
|
|
|
t.Fatalf("num confs incorrect, got %v expected %v",
|
|
|
|
txDetail.NumConfirmations, 1)
|
|
|
|
}
|
2017-11-24 07:02:33 +03:00
|
|
|
|
|
|
|
// We assert that the value is greater than the amount we
|
2018-02-07 06:11:11 +03:00
|
|
|
// attempted to send, as the wallet should have paid some amount
|
2017-11-24 07:02:33 +03:00
|
|
|
// of network fees.
|
|
|
|
if txDetail.Value >= -outputAmt {
|
2017-11-23 09:29:08 +03:00
|
|
|
fmt.Println(spew.Sdump(txDetail))
|
2016-10-16 00:12:18 +03:00
|
|
|
t.Fatalf("tx value incorrect, got %v expected %v",
|
2017-11-23 09:29:08 +03:00
|
|
|
int64(txDetail.Value), -int64(outputAmt))
|
2016-10-16 00:12:18 +03:00
|
|
|
}
|
|
|
|
if !bytes.Equal(txDetail.BlockHash[:], burnBlock[0][:]) {
|
|
|
|
t.Fatalf("block hash mismatch, got %v expected %v",
|
|
|
|
txDetail.BlockHash, burnBlock[0])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !burnTxFound {
|
2017-02-10 02:28:32 +03:00
|
|
|
t.Fatal("tx burning btc not found")
|
2016-10-16 00:12:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-30 21:46:59 +03:00
|
|
|
func testTransactionSubscriptions(miner *rpctest.Harness,
|
|
|
|
alice, _ *lnwallet.LightningWallet, t *testing.T) {
|
2016-10-16 00:12:18 +03:00
|
|
|
|
|
|
|
// First, check to see if this wallet meets the TransactionNotifier
|
|
|
|
// interface, if not then we'll skip this test for this particular
|
|
|
|
// implementation of the WalletController.
|
2017-07-30 21:46:59 +03:00
|
|
|
txClient, err := alice.SubscribeTransactions()
|
2016-10-16 00:12:18 +03:00
|
|
|
if err != nil {
|
2017-11-10 03:30:20 +03:00
|
|
|
t.Skipf("unable to generate tx subscription: %v", err)
|
2016-10-16 00:12:18 +03:00
|
|
|
}
|
|
|
|
defer txClient.Cancel()
|
|
|
|
|
|
|
|
const (
|
|
|
|
outputAmt = btcutil.SatoshiPerBitcoin
|
|
|
|
numTxns = 3
|
|
|
|
)
|
2019-09-16 14:20:24 +03:00
|
|
|
errCh1 := make(chan error, 1)
|
2017-11-10 03:30:20 +03:00
|
|
|
switch alice.BackEnd() {
|
|
|
|
case "neutrino":
|
|
|
|
// Neutrino doesn't listen for unconfirmed transactions.
|
|
|
|
default:
|
|
|
|
go func() {
|
|
|
|
for i := 0; i < numTxns; i++ {
|
|
|
|
txDetail := <-txClient.UnconfirmedTransactions()
|
|
|
|
if txDetail.NumConfirmations != 0 {
|
2019-09-16 14:20:24 +03:00
|
|
|
errCh1 <- fmt.Errorf("incorrect number of confs, "+
|
2017-11-10 03:30:20 +03:00
|
|
|
"expected %v got %v", 0,
|
|
|
|
txDetail.NumConfirmations)
|
2019-09-16 14:20:24 +03:00
|
|
|
return
|
2017-11-10 03:30:20 +03:00
|
|
|
}
|
|
|
|
if txDetail.Value != outputAmt {
|
2019-09-16 14:20:24 +03:00
|
|
|
errCh1 <- fmt.Errorf("incorrect output amt, "+
|
2017-11-10 03:30:20 +03:00
|
|
|
"expected %v got %v", outputAmt,
|
|
|
|
txDetail.Value)
|
2019-09-16 14:20:24 +03:00
|
|
|
return
|
2017-11-10 03:30:20 +03:00
|
|
|
}
|
|
|
|
if txDetail.BlockHash != nil {
|
2019-09-16 14:20:24 +03:00
|
|
|
errCh1 <- fmt.Errorf("block hash should be nil, "+
|
2017-11-10 03:30:20 +03:00
|
|
|
"is instead %v",
|
|
|
|
txDetail.BlockHash)
|
2019-09-16 14:20:24 +03:00
|
|
|
return
|
2017-11-10 03:30:20 +03:00
|
|
|
}
|
2016-10-16 00:12:18 +03:00
|
|
|
}
|
2019-09-16 14:20:24 +03:00
|
|
|
errCh1 <- nil
|
2017-11-10 03:30:20 +03:00
|
|
|
}()
|
|
|
|
}
|
2016-10-16 00:12:18 +03:00
|
|
|
|
|
|
|
// Next, fetch a fresh address from the wallet, create 3 new outputs
|
|
|
|
// with the pkScript.
|
|
|
|
for i := 0; i < numTxns; i++ {
|
2017-07-30 21:46:59 +03:00
|
|
|
addr, err := alice.NewAddress(lnwallet.WitnessPubKey, false)
|
2016-10-16 00:12:18 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create new address: %v", err)
|
|
|
|
}
|
|
|
|
script, err := txscript.PayToAddrScript(addr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create output script: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-02-10 02:28:32 +03:00
|
|
|
output := &wire.TxOut{
|
|
|
|
Value: outputAmt,
|
|
|
|
PkScript: script,
|
|
|
|
}
|
2018-07-28 04:20:58 +03:00
|
|
|
txid, err := miner.SendOutputs([]*wire.TxOut{output}, 2500)
|
2017-11-10 03:30:20 +03:00
|
|
|
if err != nil {
|
2016-10-16 00:12:18 +03:00
|
|
|
t.Fatalf("unable to send coinbase: %v", err)
|
|
|
|
}
|
2017-11-10 03:30:20 +03:00
|
|
|
err = waitForMempoolTx(miner, txid)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("tx not relayed to miner: %v", err)
|
|
|
|
}
|
2016-10-16 00:12:18 +03:00
|
|
|
}
|
|
|
|
|
2017-11-10 03:30:20 +03:00
|
|
|
switch alice.BackEnd() {
|
|
|
|
case "neutrino":
|
|
|
|
// Neutrino doesn't listen for on unconfirmed transactions.
|
|
|
|
default:
|
|
|
|
// We should receive a notification for all three transactions
|
|
|
|
// generated above.
|
|
|
|
select {
|
|
|
|
case <-time.After(time.Second * 10):
|
|
|
|
t.Fatalf("transactions not received after 10 seconds")
|
2019-09-16 14:20:24 +03:00
|
|
|
case err := <-errCh1:
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-11-10 03:30:20 +03:00
|
|
|
}
|
2016-10-16 00:12:18 +03:00
|
|
|
}
|
|
|
|
|
2019-09-16 14:20:24 +03:00
|
|
|
errCh2 := make(chan error, 1)
|
2016-10-16 00:12:18 +03:00
|
|
|
go func() {
|
|
|
|
for i := 0; i < numTxns; i++ {
|
|
|
|
txDetail := <-txClient.ConfirmedTransactions()
|
|
|
|
if txDetail.NumConfirmations != 1 {
|
2019-09-16 14:20:24 +03:00
|
|
|
errCh2 <- fmt.Errorf("incorrect number of confs for %s, expected %v got %v",
|
2017-11-10 03:30:20 +03:00
|
|
|
txDetail.Hash, 1, txDetail.NumConfirmations)
|
2019-09-16 14:20:24 +03:00
|
|
|
return
|
2016-10-16 00:12:18 +03:00
|
|
|
}
|
|
|
|
if txDetail.Value != outputAmt {
|
2019-09-16 14:20:24 +03:00
|
|
|
errCh2 <- fmt.Errorf("incorrect output amt, expected %v got %v in txid %s",
|
2017-11-10 03:30:20 +03:00
|
|
|
outputAmt, txDetail.Value, txDetail.Hash)
|
2019-09-16 14:20:24 +03:00
|
|
|
return
|
2016-10-16 00:12:18 +03:00
|
|
|
}
|
|
|
|
}
|
2019-09-16 14:20:24 +03:00
|
|
|
errCh2 <- nil
|
2016-10-16 00:12:18 +03:00
|
|
|
}()
|
|
|
|
|
|
|
|
// Next mine a single block, all the transactions generated above
|
|
|
|
// should be included.
|
|
|
|
if _, err := miner.Node.Generate(1); err != nil {
|
|
|
|
t.Fatalf("unable to generate block: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We should receive a notification for all three transactions
|
|
|
|
// since they should be mined in the next block.
|
|
|
|
select {
|
|
|
|
case <-time.After(time.Second * 5):
|
2017-11-10 03:30:20 +03:00
|
|
|
t.Fatalf("transactions not received after 5 seconds")
|
2019-09-16 14:20:24 +03:00
|
|
|
case err := <-errCh2:
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-11-19 04:11:59 +03:00
|
|
|
}
|
2018-09-13 06:59:20 +03:00
|
|
|
|
|
|
|
// We'll also ensure that the client is able to send our new
|
|
|
|
// notifications when we _create_ transactions ourselves that spend our
|
|
|
|
// own outputs.
|
|
|
|
b := txscript.NewScriptBuilder()
|
2019-10-30 05:32:40 +03:00
|
|
|
b.AddOp(txscript.OP_RETURN)
|
2018-09-13 06:59:20 +03:00
|
|
|
outputScript, err := b.Script()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to make output script: %v", err)
|
|
|
|
}
|
|
|
|
burnOutput := wire.NewTxOut(outputAmt, outputScript)
|
2018-11-05 14:30:32 +03:00
|
|
|
tx, err := alice.SendOutputs([]*wire.TxOut{burnOutput}, 2500)
|
2018-09-13 06:59:20 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create burn tx: %v", err)
|
|
|
|
}
|
2018-11-05 14:30:32 +03:00
|
|
|
txid := tx.TxHash()
|
|
|
|
err = waitForMempoolTx(miner, &txid)
|
2018-09-13 06:59:20 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("tx not relayed to miner: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Before we mine the next block, we'll ensure that the above
|
|
|
|
// transaction shows up in the set of unconfirmed transactions returned
|
|
|
|
// by ListTransactionDetails.
|
|
|
|
err = waitForWalletSync(miner, alice)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Couldn't sync Alice's wallet: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// As we just sent the transaction and it was landed in the mempool, we
|
|
|
|
// should get a notification for a new unconfirmed transactions
|
|
|
|
select {
|
|
|
|
case <-time.After(time.Second * 10):
|
|
|
|
t.Fatalf("transactions not received after 10 seconds")
|
|
|
|
case unConfTx := <-txClient.UnconfirmedTransactions():
|
2018-11-05 14:30:32 +03:00
|
|
|
if unConfTx.Hash != txid {
|
2018-09-13 06:59:20 +03:00
|
|
|
t.Fatalf("wrong txn notified: expected %v got %v",
|
|
|
|
txid, unConfTx.Hash)
|
|
|
|
}
|
|
|
|
}
|
2016-11-19 04:11:59 +03:00
|
|
|
}
|
|
|
|
|
2019-09-19 15:59:07 +03:00
|
|
|
// scriptFromKey creates a P2WKH script from the given pubkey.
|
|
|
|
func scriptFromKey(pubkey *btcec.PublicKey) ([]byte, error) {
|
|
|
|
pubkeyHash := btcutil.Hash160(pubkey.SerializeCompressed())
|
|
|
|
keyAddr, err := btcutil.NewAddressWitnessPubKeyHash(
|
|
|
|
pubkeyHash, &chaincfg.RegressionNetParams,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to create addr: %v", err)
|
|
|
|
}
|
|
|
|
keyScript, err := txscript.PayToAddrScript(keyAddr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to generate script: %v", err)
|
|
|
|
}
|
2018-01-19 16:24:30 +03:00
|
|
|
|
2019-09-19 15:59:07 +03:00
|
|
|
return keyScript, nil
|
|
|
|
}
|
2018-01-19 16:24:30 +03:00
|
|
|
|
2019-09-19 15:59:07 +03:00
|
|
|
// mineAndAssert mines a block and ensures the passed TX is part of that block.
|
|
|
|
func mineAndAssert(r *rpctest.Harness, tx *wire.MsgTx) error {
|
|
|
|
txid := tx.TxHash()
|
|
|
|
err := waitForMempoolTx(r, &txid)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("tx not relayed to miner: %v", err)
|
|
|
|
}
|
2018-01-19 16:24:30 +03:00
|
|
|
|
2019-09-19 15:59:07 +03:00
|
|
|
blockHashes, err := r.Node.Generate(1)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to generate block: %v", err)
|
|
|
|
}
|
2018-01-19 16:24:30 +03:00
|
|
|
|
2019-09-19 15:59:07 +03:00
|
|
|
block, err := r.Node.GetBlock(blockHashes[0])
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to find block: %v", err)
|
|
|
|
}
|
2018-01-19 16:24:30 +03:00
|
|
|
|
2019-09-19 15:59:07 +03:00
|
|
|
if len(block.Transactions) != 2 {
|
|
|
|
return fmt.Errorf("expected 2 txs in block, got %d",
|
|
|
|
len(block.Transactions))
|
2018-01-19 16:24:30 +03:00
|
|
|
}
|
|
|
|
|
2019-09-19 15:59:07 +03:00
|
|
|
blockTx := block.Transactions[1]
|
|
|
|
if blockTx.TxHash() != tx.TxHash() {
|
|
|
|
return fmt.Errorf("incorrect transaction was mined")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sleep for a second before returning, to make sure the block has
|
|
|
|
// propagated.
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// txFromOutput takes a tx paying to fromPubKey, and creates a new tx that
|
|
|
|
// spends the output from this tx, to an address derived from payToPubKey.
|
|
|
|
func txFromOutput(tx *wire.MsgTx, signer input.Signer, fromPubKey,
|
2019-09-19 15:59:07 +03:00
|
|
|
payToPubKey *btcec.PublicKey, txFee btcutil.Amount,
|
|
|
|
rbf bool) (*wire.MsgTx, error) {
|
2019-09-19 15:59:07 +03:00
|
|
|
|
|
|
|
// Generate the script we want to spend from.
|
|
|
|
keyScript, err := scriptFromKey(fromPubKey)
|
2018-01-19 16:24:30 +03:00
|
|
|
if err != nil {
|
2019-09-19 15:59:07 +03:00
|
|
|
return nil, fmt.Errorf("unable to generate script: %v", err)
|
2018-01-19 16:24:30 +03:00
|
|
|
}
|
2019-09-19 15:59:07 +03:00
|
|
|
|
|
|
|
// We assume the output was paid to the keyScript made earlier.
|
|
|
|
var outputIndex uint32
|
|
|
|
if len(tx.TxOut) == 1 || bytes.Equal(tx.TxOut[0].PkScript, keyScript) {
|
|
|
|
outputIndex = 0
|
|
|
|
} else {
|
|
|
|
outputIndex = 1
|
|
|
|
}
|
|
|
|
outputValue := tx.TxOut[outputIndex].Value
|
|
|
|
|
|
|
|
// With the index located, we can create a transaction spending the
|
|
|
|
// referenced output.
|
|
|
|
tx1 := wire.NewMsgTx(2)
|
|
|
|
|
2019-09-19 15:59:07 +03:00
|
|
|
// If we want to create a tx that signals replacement, set its
|
|
|
|
// sequence number to the max one that signals replacement.
|
|
|
|
// Otherwise we just use the standard max sequence.
|
|
|
|
sequence := wire.MaxTxInSequenceNum
|
|
|
|
if rbf {
|
|
|
|
sequence = mempool.MaxRBFSequence
|
|
|
|
}
|
|
|
|
|
2019-09-19 15:59:07 +03:00
|
|
|
tx1.AddTxIn(&wire.TxIn{
|
|
|
|
PreviousOutPoint: wire.OutPoint{
|
|
|
|
Hash: tx.TxHash(),
|
|
|
|
Index: outputIndex,
|
|
|
|
},
|
2019-09-19 15:59:07 +03:00
|
|
|
Sequence: sequence,
|
2019-09-19 15:59:07 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
// Create a script to pay to.
|
|
|
|
payToScript, err := scriptFromKey(payToPubKey)
|
2018-01-19 16:24:30 +03:00
|
|
|
if err != nil {
|
2019-09-19 15:59:07 +03:00
|
|
|
return nil, fmt.Errorf("unable to generate script: %v", err)
|
2018-01-19 16:24:30 +03:00
|
|
|
}
|
2019-09-19 15:59:07 +03:00
|
|
|
tx1.AddTxOut(&wire.TxOut{
|
|
|
|
Value: outputValue - int64(txFee),
|
|
|
|
PkScript: payToScript,
|
|
|
|
})
|
|
|
|
|
|
|
|
// Now we can populate the sign descriptor which we'll use to generate
|
|
|
|
// the signature.
|
|
|
|
signDesc := &input.SignDescriptor{
|
|
|
|
KeyDesc: keychain.KeyDescriptor{
|
|
|
|
PubKey: fromPubKey,
|
|
|
|
},
|
|
|
|
WitnessScript: keyScript,
|
|
|
|
Output: tx.TxOut[outputIndex],
|
|
|
|
HashType: txscript.SigHashAll,
|
|
|
|
SigHashes: txscript.NewTxSigHashes(tx1),
|
|
|
|
InputIndex: 0, // Has only one input.
|
|
|
|
}
|
|
|
|
|
|
|
|
// With the descriptor created, we use it to generate a signature, then
|
|
|
|
// manually create a valid witness stack we'll use for signing.
|
|
|
|
spendSig, err := signer.SignOutputRaw(tx1, signDesc)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to generate signature: %v", err)
|
|
|
|
}
|
|
|
|
witness := make([][]byte, 2)
|
|
|
|
witness[0] = append(spendSig, byte(txscript.SigHashAll))
|
|
|
|
witness[1] = fromPubKey.SerializeCompressed()
|
|
|
|
tx1.TxIn[0].Witness = witness
|
|
|
|
|
|
|
|
// Finally, attempt to validate the completed transaction. This should
|
|
|
|
// succeed if the wallet was able to properly generate the proper
|
|
|
|
// private key.
|
|
|
|
vm, err := txscript.NewEngine(
|
|
|
|
keyScript, tx1, 0, txscript.StandardVerifyFlags, nil,
|
|
|
|
nil, outputValue,
|
|
|
|
)
|
2018-01-19 16:24:30 +03:00
|
|
|
if err != nil {
|
2019-09-19 15:59:07 +03:00
|
|
|
return nil, fmt.Errorf("unable to create engine: %v", err)
|
|
|
|
}
|
|
|
|
if err := vm.Execute(); err != nil {
|
|
|
|
return nil, fmt.Errorf("spend is invalid: %v", err)
|
2018-01-19 16:24:30 +03:00
|
|
|
}
|
|
|
|
|
2019-09-19 15:59:07 +03:00
|
|
|
return tx1, nil
|
|
|
|
}
|
2018-01-19 16:24:30 +03:00
|
|
|
|
2019-09-19 15:59:07 +03:00
|
|
|
// newTx sends coins from Alice's wallet, mines this transaction, and creates a
|
|
|
|
// new, unconfirmed tx that spends this output to pubKey.
|
|
|
|
func newTx(t *testing.T, r *rpctest.Harness, pubKey *btcec.PublicKey,
|
|
|
|
alice *lnwallet.LightningWallet, rbf bool) *wire.MsgTx {
|
|
|
|
t.Helper()
|
2018-01-19 16:24:30 +03:00
|
|
|
|
2019-09-19 15:59:07 +03:00
|
|
|
keyScript, err := scriptFromKey(pubKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to generate script: %v", err)
|
2018-01-19 16:24:30 +03:00
|
|
|
}
|
|
|
|
|
2019-09-19 15:59:07 +03:00
|
|
|
// Instruct the wallet to fund the output with a newly created
|
|
|
|
// transaction.
|
|
|
|
newOutput := &wire.TxOut{
|
|
|
|
Value: btcutil.SatoshiPerBitcoin,
|
|
|
|
PkScript: keyScript,
|
|
|
|
}
|
|
|
|
tx, err := alice.SendOutputs([]*wire.TxOut{newOutput}, 2500)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create output: %v", err)
|
|
|
|
}
|
2018-01-19 16:24:30 +03:00
|
|
|
|
2019-09-19 15:59:07 +03:00
|
|
|
// Query for the transaction generated above so we can located the
|
|
|
|
// index of our output.
|
|
|
|
if err := mineAndAssert(r, tx); err != nil {
|
|
|
|
t.Fatalf("unable to mine tx: %v", err)
|
|
|
|
}
|
2018-01-19 16:24:30 +03:00
|
|
|
|
2019-09-19 15:59:07 +03:00
|
|
|
// Create a new unconfirmed tx that spends this output.
|
2019-10-30 05:32:40 +03:00
|
|
|
txFee := btcutil.Amount(0.001 * btcutil.SatoshiPerBitcoin)
|
2019-09-19 15:59:07 +03:00
|
|
|
tx1, err := txFromOutput(
|
2019-09-19 15:59:07 +03:00
|
|
|
tx, alice.Cfg.Signer, pubKey, pubKey, txFee, rbf,
|
2019-09-19 15:59:07 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-01-19 16:24:30 +03:00
|
|
|
|
2019-09-19 15:59:07 +03:00
|
|
|
return tx1
|
|
|
|
}
|
2018-01-19 16:24:30 +03:00
|
|
|
|
2019-09-19 15:59:07 +03:00
|
|
|
// testPublishTransaction checks that PublishTransaction returns the expected
|
|
|
|
// error types in case the transaction being published conflicts with the
|
|
|
|
// current mempool or chain.
|
|
|
|
func testPublishTransaction(r *rpctest.Harness,
|
|
|
|
alice, _ *lnwallet.LightningWallet, t *testing.T) {
|
|
|
|
|
|
|
|
// Generate a pubkey, and pay-to-addr script.
|
|
|
|
keyDesc, err := alice.DeriveNextKey(keychain.KeyFamilyMultiSig)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to obtain public key: %v", err)
|
2018-01-19 16:24:30 +03:00
|
|
|
}
|
|
|
|
|
2019-09-19 15:59:07 +03:00
|
|
|
// We will first check that publishing a transaction already in the
|
|
|
|
// mempool does NOT return an error. Create the tx.
|
|
|
|
tx1 := newTx(t, r, keyDesc.PubKey, alice, false)
|
2018-01-19 16:24:30 +03:00
|
|
|
|
|
|
|
// Publish the transaction.
|
|
|
|
if err := alice.PublishTransaction(tx1); err != nil {
|
|
|
|
t.Fatalf("unable to publish: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
txid1 := tx1.TxHash()
|
|
|
|
err = waitForMempoolTx(r, &txid1)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("tx not relayed to miner: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-09-19 15:59:07 +03:00
|
|
|
// Publish the exact same transaction again. This should not return an
|
|
|
|
// error, even though the transaction is already in the mempool.
|
2018-01-19 16:24:30 +03:00
|
|
|
if err := alice.PublishTransaction(tx1); err != nil {
|
|
|
|
t.Fatalf("unable to publish: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mine the transaction.
|
|
|
|
if _, err := r.Node.Generate(1); err != nil {
|
|
|
|
t.Fatalf("unable to generate block: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-09-19 15:59:07 +03:00
|
|
|
// We'll now test that we don't get an error if we try to publish a
|
|
|
|
// transaction that is already mined.
|
2018-01-19 16:24:30 +03:00
|
|
|
//
|
2019-09-19 15:59:07 +03:00
|
|
|
// Create a new transaction. We must do this to properly test the
|
|
|
|
// reject messages from our peers. They might only send us a reject
|
|
|
|
// message for a given tx once, so we create a new to make sure it is
|
|
|
|
// not just immediately rejected.
|
|
|
|
tx2 := newTx(t, r, keyDesc.PubKey, alice, false)
|
2018-01-19 16:24:30 +03:00
|
|
|
|
|
|
|
// Publish this tx.
|
|
|
|
if err := alice.PublishTransaction(tx2); err != nil {
|
|
|
|
t.Fatalf("unable to publish: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mine the transaction.
|
2019-09-19 15:59:07 +03:00
|
|
|
if err := mineAndAssert(r, tx2); err != nil {
|
2018-01-19 16:24:30 +03:00
|
|
|
t.Fatalf("unable to mine tx: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-09-19 15:59:07 +03:00
|
|
|
// Publish the transaction again. It is already mined, and we don't
|
|
|
|
// expect this to return an error.
|
2018-01-19 16:24:30 +03:00
|
|
|
if err := alice.PublishTransaction(tx2); err != nil {
|
|
|
|
t.Fatalf("unable to publish: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-09-19 15:59:07 +03:00
|
|
|
// We'll do the next mempool check on both RBF and non-RBF enabled
|
|
|
|
// transactions.
|
|
|
|
var (
|
2019-10-30 05:32:40 +03:00
|
|
|
txFee = btcutil.Amount(0.005 * btcutil.SatoshiPerBitcoin)
|
2019-09-19 15:59:07 +03:00
|
|
|
tx3, tx3Spend *wire.MsgTx
|
|
|
|
)
|
2018-01-19 16:24:30 +03:00
|
|
|
|
2019-09-19 15:59:07 +03:00
|
|
|
for _, rbf := range []bool{false, true} {
|
|
|
|
// Now we'll try to double spend an output with a different
|
|
|
|
// transaction. Create a new tx and publish it. This is the
|
|
|
|
// output we'll try to double spend.
|
|
|
|
tx3 = newTx(t, r, keyDesc.PubKey, alice, false)
|
|
|
|
if err := alice.PublishTransaction(tx3); err != nil {
|
|
|
|
t.Fatalf("unable to publish: %v", err)
|
|
|
|
}
|
2018-01-19 16:24:30 +03:00
|
|
|
|
2019-09-19 15:59:07 +03:00
|
|
|
// Mine the transaction.
|
|
|
|
if err := mineAndAssert(r, tx3); err != nil {
|
|
|
|
t.Fatalf("unable to mine tx: %v", err)
|
|
|
|
}
|
2019-09-19 15:59:07 +03:00
|
|
|
|
2019-09-19 15:59:07 +03:00
|
|
|
// Now we create a transaction that spends the output from the
|
|
|
|
// tx just mined.
|
|
|
|
tx4, err := txFromOutput(
|
|
|
|
tx3, alice.Cfg.Signer, keyDesc.PubKey,
|
|
|
|
keyDesc.PubKey, txFee, rbf,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-01-19 16:24:30 +03:00
|
|
|
|
2019-09-19 15:59:07 +03:00
|
|
|
// This should be accepted into the mempool.
|
|
|
|
if err := alice.PublishTransaction(tx4); err != nil {
|
|
|
|
t.Fatalf("unable to publish: %v", err)
|
|
|
|
}
|
2018-01-19 16:24:30 +03:00
|
|
|
|
2019-09-19 15:59:07 +03:00
|
|
|
// Keep track of the last successfully published tx to spend
|
|
|
|
// tx3.
|
|
|
|
tx3Spend = tx4
|
2018-01-19 16:24:30 +03:00
|
|
|
|
2019-09-19 15:59:07 +03:00
|
|
|
txid4 := tx4.TxHash()
|
|
|
|
err = waitForMempoolTx(r, &txid4)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("tx not relayed to miner: %v", err)
|
|
|
|
}
|
2019-09-19 15:59:07 +03:00
|
|
|
|
2019-09-19 15:59:07 +03:00
|
|
|
// Create a new key we'll pay to, to ensure we create a unique
|
|
|
|
// transaction.
|
|
|
|
keyDesc2, err := alice.DeriveNextKey(
|
|
|
|
keychain.KeyFamilyMultiSig,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to obtain public key: %v", err)
|
|
|
|
}
|
2018-01-19 16:24:30 +03:00
|
|
|
|
2019-09-19 15:59:07 +03:00
|
|
|
// Create a new transaction that spends the output from tx3,
|
|
|
|
// and that pays to a different address. We expect this to be
|
|
|
|
// rejected because it is a double spend.
|
|
|
|
tx5, err := txFromOutput(
|
|
|
|
tx3, alice.Cfg.Signer, keyDesc.PubKey,
|
|
|
|
keyDesc2.PubKey, txFee, rbf,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-09-19 15:59:07 +03:00
|
|
|
|
2019-09-19 15:59:07 +03:00
|
|
|
err = alice.PublishTransaction(tx5)
|
|
|
|
if err != lnwallet.ErrDoubleSpend {
|
|
|
|
t.Fatalf("expected ErrDoubleSpend, got: %v", err)
|
|
|
|
}
|
2018-01-19 16:24:30 +03:00
|
|
|
|
2019-09-19 15:59:07 +03:00
|
|
|
// Create another transaction that spends the same output, but
|
|
|
|
// has a higher fee. We expect also this tx to be rejected for
|
|
|
|
// non-RBF enabled transactions, while it should succeed
|
|
|
|
// otherwise.
|
|
|
|
pubKey3, err := alice.DeriveNextKey(keychain.KeyFamilyMultiSig)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to obtain public key: %v", err)
|
|
|
|
}
|
|
|
|
tx6, err := txFromOutput(
|
|
|
|
tx3, alice.Cfg.Signer, keyDesc.PubKey,
|
|
|
|
pubKey3.PubKey, 2*txFee, rbf,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Expect rejection in non-RBF case.
|
|
|
|
expErr := lnwallet.ErrDoubleSpend
|
|
|
|
if rbf {
|
|
|
|
// Expect success in rbf case.
|
|
|
|
expErr = nil
|
|
|
|
tx3Spend = tx6
|
|
|
|
}
|
|
|
|
err = alice.PublishTransaction(tx6)
|
|
|
|
if err != expErr {
|
|
|
|
t.Fatalf("expected ErrDoubleSpend, got: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mine the tx spending tx3.
|
|
|
|
if err := mineAndAssert(r, tx3Spend); err != nil {
|
|
|
|
t.Fatalf("unable to mine tx: %v", err)
|
|
|
|
}
|
2018-01-19 16:24:30 +03:00
|
|
|
}
|
|
|
|
|
2019-09-19 15:59:07 +03:00
|
|
|
// At last we try to spend an output already spent by a confirmed
|
|
|
|
// transaction.
|
|
|
|
// TODO(halseth): we currently skip this test for neutrino, as the
|
|
|
|
// backing btcd node will consider the tx being an orphan, and will
|
|
|
|
// accept it. Should look into if this is the behavior also for
|
|
|
|
// bitcoind, and update test accordingly.
|
2018-01-19 16:24:30 +03:00
|
|
|
if alice.BackEnd() != "neutrino" {
|
|
|
|
// Create another tx spending tx3.
|
2018-02-20 05:58:57 +03:00
|
|
|
pubKey4, err := alice.DeriveNextKey(
|
|
|
|
keychain.KeyFamilyMultiSig,
|
|
|
|
)
|
2018-01-19 16:24:30 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to obtain public key: %v", err)
|
|
|
|
}
|
2019-09-19 15:59:07 +03:00
|
|
|
tx7, err := txFromOutput(
|
|
|
|
tx3, alice.Cfg.Signer, keyDesc.PubKey,
|
2019-09-19 15:59:07 +03:00
|
|
|
pubKey4.PubKey, txFee, false,
|
2019-09-19 15:59:07 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-01-19 16:24:30 +03:00
|
|
|
|
|
|
|
// Expect rejection.
|
2019-09-19 15:59:07 +03:00
|
|
|
err = alice.PublishTransaction(tx7)
|
|
|
|
if err != lnwallet.ErrDoubleSpend {
|
2018-01-19 16:24:30 +03:00
|
|
|
t.Fatalf("expected ErrDoubleSpend, got: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-30 05:37:08 +03:00
|
|
|
func testSignOutputUsingTweaks(r *rpctest.Harness,
|
|
|
|
alice, _ *lnwallet.LightningWallet, t *testing.T) {
|
2016-11-19 04:11:59 +03:00
|
|
|
|
|
|
|
// We'd like to test the ability of the wallet's Signer implementation
|
|
|
|
// to be able to sign with a private key derived from tweaking the
|
|
|
|
// specific public key. This scenario exercises the case when the
|
2017-07-30 05:37:08 +03:00
|
|
|
// wallet needs to sign for a sweep of a revoked output, or just claim
|
|
|
|
// any output that pays to a tweaked key.
|
2016-11-19 04:11:59 +03:00
|
|
|
|
2017-07-30 05:37:08 +03:00
|
|
|
// First, generate a new public key under the control of the wallet,
|
2016-11-19 04:11:59 +03:00
|
|
|
// then generate a revocation key using it.
|
2018-02-18 02:25:55 +03:00
|
|
|
pubKey, err := alice.DeriveNextKey(
|
|
|
|
keychain.KeyFamilyMultiSig,
|
|
|
|
)
|
2016-11-19 04:11:59 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to obtain public key: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-07-30 05:37:08 +03:00
|
|
|
// As we'd like to test both single tweak, and double tweak spends,
|
|
|
|
// we'll generate a commitment pre-image, then derive a revocation key
|
|
|
|
// and single tweak from that.
|
|
|
|
commitPreimage := bytes.Repeat([]byte{2}, 32)
|
|
|
|
commitSecret, commitPoint := btcec.PrivKeyFromBytes(btcec.S256(),
|
|
|
|
commitPreimage)
|
|
|
|
|
2019-01-16 17:47:43 +03:00
|
|
|
revocationKey := input.DeriveRevocationPubkey(pubKey.PubKey, commitPoint)
|
|
|
|
commitTweak := input.SingleTweakBytes(commitPoint, pubKey.PubKey)
|
2017-07-30 05:37:08 +03:00
|
|
|
|
2019-01-16 17:47:43 +03:00
|
|
|
tweakedPub := input.TweakPubKey(pubKey.PubKey, commitPoint)
|
2017-07-30 05:37:08 +03:00
|
|
|
|
|
|
|
// As we'd like to test both single and double tweaks, we'll repeat
|
|
|
|
// the same set up twice. The first will use a regular single tweak,
|
|
|
|
// and the second will use a double tweak.
|
|
|
|
baseKey := pubKey
|
|
|
|
for i := 0; i < 2; i++ {
|
|
|
|
var tweakedKey *btcec.PublicKey
|
|
|
|
if i == 0 {
|
|
|
|
tweakedKey = tweakedPub
|
|
|
|
} else {
|
|
|
|
tweakedKey = revocationKey
|
|
|
|
}
|
2016-11-19 04:11:59 +03:00
|
|
|
|
2017-07-30 05:37:08 +03:00
|
|
|
// Using the given key for the current iteration, we'll
|
|
|
|
// generate a regular p2wkh from that.
|
|
|
|
pubkeyHash := btcutil.Hash160(tweakedKey.SerializeCompressed())
|
|
|
|
keyAddr, err := btcutil.NewAddressWitnessPubKeyHash(pubkeyHash,
|
2017-11-10 03:30:20 +03:00
|
|
|
&chaincfg.RegressionNetParams)
|
2017-07-30 05:37:08 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create addr: %v", err)
|
|
|
|
}
|
|
|
|
keyScript, err := txscript.PayToAddrScript(keyAddr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to generate script: %v", err)
|
|
|
|
}
|
2016-11-19 04:11:59 +03:00
|
|
|
|
2017-07-30 05:37:08 +03:00
|
|
|
// With the script fully assembled, instruct the wallet to fund
|
|
|
|
// the output with a newly created transaction.
|
|
|
|
newOutput := &wire.TxOut{
|
|
|
|
Value: btcutil.SatoshiPerBitcoin,
|
|
|
|
PkScript: keyScript,
|
|
|
|
}
|
2018-11-05 14:30:32 +03:00
|
|
|
tx, err := alice.SendOutputs([]*wire.TxOut{newOutput}, 2500)
|
2017-07-30 05:37:08 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create output: %v", err)
|
|
|
|
}
|
2018-11-05 14:30:32 +03:00
|
|
|
txid := tx.TxHash()
|
2017-07-30 05:37:08 +03:00
|
|
|
// Query for the transaction generated above so we can located
|
|
|
|
// the index of our output.
|
2018-11-05 14:30:32 +03:00
|
|
|
err = waitForMempoolTx(r, &txid)
|
2017-11-10 03:30:20 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("tx not relayed to miner: %v", err)
|
|
|
|
}
|
2017-07-30 05:37:08 +03:00
|
|
|
var outputIndex uint32
|
2018-11-05 14:30:32 +03:00
|
|
|
if bytes.Equal(tx.TxOut[0].PkScript, keyScript) {
|
2017-07-30 05:37:08 +03:00
|
|
|
outputIndex = 0
|
|
|
|
} else {
|
|
|
|
outputIndex = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// With the index located, we can create a transaction spending
|
|
|
|
// the referenced output.
|
|
|
|
sweepTx := wire.NewMsgTx(2)
|
|
|
|
sweepTx.AddTxIn(&wire.TxIn{
|
|
|
|
PreviousOutPoint: wire.OutPoint{
|
2018-11-05 14:30:32 +03:00
|
|
|
Hash: txid,
|
2017-07-30 05:37:08 +03:00
|
|
|
Index: outputIndex,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
sweepTx.AddTxOut(&wire.TxOut{
|
|
|
|
Value: 1000,
|
|
|
|
PkScript: keyScript,
|
|
|
|
})
|
|
|
|
|
|
|
|
// Now we can populate the sign descriptor which we'll use to
|
|
|
|
// generate the signature. Within the descriptor we set the
|
|
|
|
// private tweak value as the key in the script is derived
|
|
|
|
// based on this tweak value and the key we originally
|
|
|
|
// generated above.
|
2019-01-16 17:47:43 +03:00
|
|
|
signDesc := &input.SignDescriptor{
|
2018-02-18 02:25:55 +03:00
|
|
|
KeyDesc: keychain.KeyDescriptor{
|
|
|
|
PubKey: baseKey.PubKey,
|
|
|
|
},
|
2017-07-30 05:37:08 +03:00
|
|
|
WitnessScript: keyScript,
|
|
|
|
Output: newOutput,
|
|
|
|
HashType: txscript.SigHashAll,
|
|
|
|
SigHashes: txscript.NewTxSigHashes(sweepTx),
|
|
|
|
InputIndex: 0,
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this is the first, loop, we'll use the generated single
|
|
|
|
// tweak, otherwise, we'll use the double tweak.
|
|
|
|
if i == 0 {
|
|
|
|
signDesc.SingleTweak = commitTweak
|
|
|
|
} else {
|
|
|
|
signDesc.DoubleTweak = commitSecret
|
|
|
|
}
|
|
|
|
|
|
|
|
// With the descriptor created, we use it to generate a
|
|
|
|
// signature, then manually create a valid witness stack we'll
|
|
|
|
// use for signing.
|
|
|
|
spendSig, err := alice.Cfg.Signer.SignOutputRaw(sweepTx, signDesc)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to generate signature: %v", err)
|
|
|
|
}
|
|
|
|
witness := make([][]byte, 2)
|
|
|
|
witness[0] = append(spendSig, byte(txscript.SigHashAll))
|
|
|
|
witness[1] = tweakedKey.SerializeCompressed()
|
|
|
|
sweepTx.TxIn[0].Witness = witness
|
|
|
|
|
|
|
|
// Finally, attempt to validate the completed transaction. This
|
|
|
|
// should succeed if the wallet was able to properly generate
|
|
|
|
// the proper private key.
|
|
|
|
vm, err := txscript.NewEngine(keyScript,
|
|
|
|
sweepTx, 0, txscript.StandardVerifyFlags, nil,
|
|
|
|
nil, int64(btcutil.SatoshiPerBitcoin))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create engine: %v", err)
|
|
|
|
}
|
|
|
|
if err := vm.Execute(); err != nil {
|
|
|
|
t.Fatalf("spend #%v is invalid: %v", i, err)
|
|
|
|
}
|
2016-10-16 00:12:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-29 19:48:10 +03:00
|
|
|
func testReorgWalletBalance(r *rpctest.Harness, w *lnwallet.LightningWallet,
|
|
|
|
_ *lnwallet.LightningWallet, t *testing.T) {
|
2018-02-18 02:25:55 +03:00
|
|
|
|
2017-06-29 19:48:10 +03:00
|
|
|
// We first mine a few blocks to ensure any transactions still in the
|
|
|
|
// mempool confirm, and then get the original balance, before a
|
|
|
|
// reorganization that doesn't invalidate any existing transactions or
|
|
|
|
// create any new non-coinbase transactions. We'll then check if it's
|
|
|
|
// the same after the empty reorg.
|
|
|
|
_, err := r.Node.Generate(5)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to generate blocks on passed node: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Give wallet time to catch up.
|
2017-11-10 03:30:20 +03:00
|
|
|
err = waitForWalletSync(r, w)
|
2017-06-29 19:48:10 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to sync wallet: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send some money from the miner to the wallet
|
|
|
|
err = loadTestCredits(r, w, 20, 4)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send money to lnwallet: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send some money from the wallet back to the miner.
|
|
|
|
// Grab a fresh address from the miner to house this output.
|
|
|
|
minerAddr, err := r.NewAddress()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to generate address for miner: %v", err)
|
|
|
|
}
|
|
|
|
script, err := txscript.PayToAddrScript(minerAddr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create pay to addr script: %v", err)
|
|
|
|
}
|
|
|
|
output := &wire.TxOut{
|
|
|
|
Value: 1e8,
|
|
|
|
PkScript: script,
|
|
|
|
}
|
2018-11-05 14:30:32 +03:00
|
|
|
tx, err := w.SendOutputs([]*wire.TxOut{output}, 2500)
|
2017-11-10 03:30:20 +03:00
|
|
|
if err != nil {
|
2017-06-29 19:48:10 +03:00
|
|
|
t.Fatalf("unable to send outputs: %v", err)
|
|
|
|
}
|
2018-11-05 14:30:32 +03:00
|
|
|
txid := tx.TxHash()
|
|
|
|
err = waitForMempoolTx(r, &txid)
|
2017-11-10 03:30:20 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("tx not relayed to miner: %v", err)
|
|
|
|
}
|
2017-06-29 19:48:10 +03:00
|
|
|
_, err = r.Node.Generate(50)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to generate blocks on passed node: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Give wallet time to catch up.
|
2017-11-10 03:30:20 +03:00
|
|
|
err = waitForWalletSync(r, w)
|
2017-06-29 19:48:10 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to sync wallet: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the original balance.
|
2018-02-20 05:58:57 +03:00
|
|
|
origBalance, err := w.ConfirmedBalance(1)
|
2017-06-29 19:48:10 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to query for balance: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now we cause a reorganization as follows.
|
|
|
|
// Step 1: create a new miner and start it.
|
2018-08-17 05:48:25 +03:00
|
|
|
r2, err := rpctest.New(r.ActiveNet, nil, []string{"--txindex"})
|
2017-06-29 19:48:10 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create mining node: %v", err)
|
|
|
|
}
|
|
|
|
err = r2.SetUp(false, 0)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to set up mining node: %v", err)
|
|
|
|
}
|
|
|
|
defer r2.TearDown()
|
2018-02-20 05:58:57 +03:00
|
|
|
newBalance, err := w.ConfirmedBalance(1)
|
2017-06-29 19:48:10 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to query for balance: %v", err)
|
|
|
|
}
|
|
|
|
if origBalance != newBalance {
|
|
|
|
t.Fatalf("wallet balance incorrect, should have %v, "+
|
|
|
|
"instead have %v", origBalance, newBalance)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Step 2: connect the miner to the passed miner and wait for
|
|
|
|
// synchronization.
|
|
|
|
err = r2.Node.AddNode(r.P2PAddress(), rpcclient.ANAdd)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to connect mining nodes together: %v", err)
|
|
|
|
}
|
|
|
|
err = rpctest.JoinNodes([]*rpctest.Harness{r2, r}, rpctest.Blocks)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to synchronize mining nodes: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-08-01 06:29:39 +03:00
|
|
|
// Step 3: Do a set of reorgs by disconnecting the two miners, mining
|
2017-06-29 19:48:10 +03:00
|
|
|
// one block on the passed miner and two on the created miner,
|
|
|
|
// connecting them, and waiting for them to sync.
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
// Wait for disconnection
|
2017-11-07 22:05:53 +03:00
|
|
|
timeout := time.After(30 * time.Second)
|
2017-11-10 03:30:20 +03:00
|
|
|
stillConnected := true
|
|
|
|
var peers []btcjson.GetPeerInfoResult
|
|
|
|
for stillConnected {
|
2017-11-07 22:05:53 +03:00
|
|
|
// Allow for timeout
|
2017-11-10 03:30:20 +03:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
2017-11-07 22:05:53 +03:00
|
|
|
select {
|
|
|
|
case <-timeout:
|
|
|
|
t.Fatalf("timeout waiting for miner disconnect")
|
|
|
|
default:
|
|
|
|
}
|
2017-11-10 03:30:20 +03:00
|
|
|
err = r2.Node.AddNode(r.P2PAddress(), rpcclient.ANRemove)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to disconnect mining nodes: %v",
|
|
|
|
err)
|
|
|
|
}
|
2017-11-07 22:05:53 +03:00
|
|
|
peers, err = r2.Node.GetPeerInfo()
|
2017-06-29 19:48:10 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get peer info: %v", err)
|
|
|
|
}
|
2017-11-10 03:30:20 +03:00
|
|
|
stillConnected = false
|
|
|
|
for _, peer := range peers {
|
|
|
|
if peer.Addr == r.P2PAddress() {
|
|
|
|
stillConnected = true
|
|
|
|
break
|
|
|
|
}
|
2017-06-29 19:48:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_, err = r.Node.Generate(2)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to generate blocks on passed node: %v",
|
|
|
|
err)
|
|
|
|
}
|
|
|
|
_, err = r2.Node.Generate(3)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to generate blocks on created node: %v",
|
|
|
|
err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Step 5: Reconnect the miners and wait for them to synchronize.
|
|
|
|
err = r2.Node.AddNode(r.P2PAddress(), rpcclient.ANAdd)
|
|
|
|
if err != nil {
|
2017-11-10 03:30:20 +03:00
|
|
|
switch err := err.(type) {
|
|
|
|
case *btcjson.RPCError:
|
|
|
|
if err.Code != -8 {
|
|
|
|
t.Fatalf("unable to connect mining "+
|
|
|
|
"nodes together: %v", err)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
t.Fatalf("unable to connect mining nodes "+
|
|
|
|
"together: %v", err)
|
|
|
|
}
|
2017-06-29 19:48:10 +03:00
|
|
|
}
|
|
|
|
err = rpctest.JoinNodes([]*rpctest.Harness{r2, r},
|
|
|
|
rpctest.Blocks)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to synchronize mining nodes: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Give wallet time to catch up.
|
2017-11-10 03:30:20 +03:00
|
|
|
err = waitForWalletSync(r, w)
|
2017-06-29 19:48:10 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to sync wallet: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now we check that the wallet balance stays the same.
|
2018-02-20 05:58:57 +03:00
|
|
|
newBalance, err = w.ConfirmedBalance(1)
|
2017-06-29 19:48:10 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to query for balance: %v", err)
|
|
|
|
}
|
|
|
|
if origBalance != newBalance {
|
|
|
|
t.Fatalf("wallet balance incorrect, should have %v, "+
|
|
|
|
"instead have %v", origBalance, newBalance)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-14 07:01:54 +03:00
|
|
|
// testChangeOutputSpendConfirmation ensures that when we attempt to spend a
|
|
|
|
// change output created by the wallet, the wallet receives its confirmation
|
|
|
|
// once included in the chain.
|
|
|
|
func testChangeOutputSpendConfirmation(r *rpctest.Harness,
|
|
|
|
alice, bob *lnwallet.LightningWallet, t *testing.T) {
|
|
|
|
|
|
|
|
// In order to test that we see the confirmation of a transaction that
|
|
|
|
// spends an output created by SendOutputs, we'll start by emptying
|
|
|
|
// Alice's wallet so that no other UTXOs can be picked. To do so, we'll
|
|
|
|
// generate an address for Bob, who will receive all the coins.
|
|
|
|
// Assuming a balance of 80 BTC and a transaction fee of 2500 sat/kw,
|
|
|
|
// we'll craft the following transaction so that Alice doesn't have any
|
|
|
|
// UTXOs left.
|
|
|
|
aliceBalance, err := alice.ConfirmedBalance(0)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to retrieve alice's balance: %v", err)
|
|
|
|
}
|
|
|
|
bobPkScript := newPkScript(t, bob, lnwallet.WitnessPubKey)
|
|
|
|
|
|
|
|
// We'll use a transaction fee of 13020 satoshis, which will allow us to
|
|
|
|
// sweep all of Alice's balance in one transaction containing 1 input
|
|
|
|
// and 1 output.
|
|
|
|
//
|
|
|
|
// TODO(wilmer): replace this once SendOutputs easily supports sending
|
|
|
|
// all funds in one transaction.
|
2019-10-31 05:43:05 +03:00
|
|
|
txFeeRate := chainfee.SatPerKWeight(2500)
|
2018-11-14 07:01:54 +03:00
|
|
|
txFee := btcutil.Amount(14380)
|
|
|
|
output := &wire.TxOut{
|
|
|
|
Value: int64(aliceBalance - txFee),
|
|
|
|
PkScript: bobPkScript,
|
|
|
|
}
|
|
|
|
tx := sendCoins(t, r, alice, bob, output, txFeeRate)
|
|
|
|
txHash := tx.TxHash()
|
|
|
|
assertTxInWallet(t, alice, txHash, true)
|
|
|
|
assertTxInWallet(t, bob, txHash, true)
|
|
|
|
|
|
|
|
// With the transaction sent and confirmed, Alice's balance should now
|
|
|
|
// be 0.
|
|
|
|
aliceBalance, err = alice.ConfirmedBalance(0)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to retrieve alice's balance: %v", err)
|
|
|
|
}
|
|
|
|
if aliceBalance != 0 {
|
|
|
|
t.Fatalf("expected alice's balance to be 0 BTC, found %v",
|
|
|
|
aliceBalance)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now, we'll send an output back to Alice from Bob of 1 BTC.
|
|
|
|
alicePkScript := newPkScript(t, alice, lnwallet.WitnessPubKey)
|
|
|
|
output = &wire.TxOut{
|
|
|
|
Value: btcutil.SatoshiPerBitcoin,
|
|
|
|
PkScript: alicePkScript,
|
|
|
|
}
|
|
|
|
tx = sendCoins(t, r, bob, alice, output, txFeeRate)
|
|
|
|
txHash = tx.TxHash()
|
|
|
|
assertTxInWallet(t, alice, txHash, true)
|
|
|
|
assertTxInWallet(t, bob, txHash, true)
|
|
|
|
|
|
|
|
// Alice now has an available output to spend, but it was not a change
|
|
|
|
// output, which is what the test expects. Therefore, we'll generate one
|
|
|
|
// by sending Bob back some coins.
|
|
|
|
output = &wire.TxOut{
|
|
|
|
Value: btcutil.SatoshiPerBitcent,
|
|
|
|
PkScript: bobPkScript,
|
|
|
|
}
|
|
|
|
tx = sendCoins(t, r, alice, bob, output, txFeeRate)
|
|
|
|
txHash = tx.TxHash()
|
|
|
|
assertTxInWallet(t, alice, txHash, true)
|
|
|
|
assertTxInWallet(t, bob, txHash, true)
|
|
|
|
|
|
|
|
// Then, we'll spend the change output and ensure we see its
|
|
|
|
// confirmation come in.
|
|
|
|
tx = sendCoins(t, r, alice, bob, output, txFeeRate)
|
|
|
|
txHash = tx.TxHash()
|
|
|
|
assertTxInWallet(t, alice, txHash, true)
|
|
|
|
assertTxInWallet(t, bob, txHash, true)
|
|
|
|
|
|
|
|
// Finally, we'll replenish Alice's wallet with some more coins to
|
|
|
|
// ensure she has enough for any following test cases.
|
|
|
|
if err := loadTestCredits(r, alice, 20, 4); err != nil {
|
|
|
|
t.Fatalf("unable to replenish alice's wallet: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-20 06:16:39 +03:00
|
|
|
// testLastUnusedAddr tests that the LastUnusedAddress returns the address if
|
|
|
|
// it isn't used, and also that once the address becomes used, then it's
|
|
|
|
// properly rotated.
|
|
|
|
func testLastUnusedAddr(miner *rpctest.Harness,
|
|
|
|
alice, bob *lnwallet.LightningWallet, t *testing.T) {
|
|
|
|
|
|
|
|
if _, err := miner.Node.Generate(1); err != nil {
|
|
|
|
t.Fatalf("unable to generate block: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We'll repeat this test for each address type to ensure they're all
|
|
|
|
// rotated properly.
|
|
|
|
addrTypes := []lnwallet.AddressType{
|
|
|
|
lnwallet.WitnessPubKey, lnwallet.NestedWitnessPubKey,
|
|
|
|
}
|
|
|
|
for _, addrType := range addrTypes {
|
|
|
|
addr1, err := alice.LastUnusedAddress(addrType)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get addr: %v", err)
|
|
|
|
}
|
|
|
|
addr2, err := alice.LastUnusedAddress(addrType)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get addr: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we generate two addresses back to back, then we should
|
|
|
|
// get the same addr, as none of them have been used yet.
|
|
|
|
if addr1.String() != addr2.String() {
|
|
|
|
t.Fatalf("addresses changed w/o use: %v vs %v", addr1, addr2)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next, we'll have Bob pay to Alice's new address. This should
|
|
|
|
// trigger address rotation at the backend wallet.
|
|
|
|
addrScript, err := txscript.PayToAddrScript(addr1)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to convert addr to script: %v", err)
|
|
|
|
}
|
2019-10-31 05:43:05 +03:00
|
|
|
feeRate := chainfee.SatPerKWeight(2500)
|
2019-02-20 06:16:39 +03:00
|
|
|
output := &wire.TxOut{
|
|
|
|
Value: 1000000,
|
|
|
|
PkScript: addrScript,
|
|
|
|
}
|
|
|
|
sendCoins(t, miner, bob, alice, output, feeRate)
|
|
|
|
|
|
|
|
// If we make a new address, then it should be brand new, as
|
|
|
|
// the prior address has been used.
|
|
|
|
addr3, err := alice.LastUnusedAddress(addrType)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get addr: %v", err)
|
|
|
|
}
|
|
|
|
if addr1.String() == addr3.String() {
|
|
|
|
t.Fatalf("address should have changed but didn't")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-05 16:22:30 +03:00
|
|
|
// testCreateSimpleTx checks that a call to CreateSimpleTx will return a
|
|
|
|
// transaction that is equal to the one that is being created by SendOutputs in
|
|
|
|
// a subsequent call.
|
|
|
|
func testCreateSimpleTx(r *rpctest.Harness, w *lnwallet.LightningWallet,
|
|
|
|
_ *lnwallet.LightningWallet, t *testing.T) {
|
|
|
|
|
|
|
|
// Send some money from the miner to the wallet
|
|
|
|
err := loadTestCredits(r, w, 20, 4)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send money to lnwallet: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// The test cases we will run through for all backends.
|
|
|
|
testCases := []struct {
|
|
|
|
outVals []int64
|
2019-10-31 05:43:05 +03:00
|
|
|
feeRate chainfee.SatPerKWeight
|
2019-03-05 16:22:30 +03:00
|
|
|
valid bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
outVals: []int64{},
|
|
|
|
feeRate: 2500,
|
|
|
|
valid: false, // No outputs.
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
2019-06-19 03:52:50 +03:00
|
|
|
outVals: []int64{200},
|
2019-03-05 16:22:30 +03:00
|
|
|
feeRate: 2500,
|
|
|
|
valid: false, // Dust output.
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
outVals: []int64{1e8},
|
|
|
|
feeRate: 2500,
|
|
|
|
valid: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
outVals: []int64{1e8, 2e8, 1e8, 2e7, 3e5},
|
|
|
|
feeRate: 2500,
|
|
|
|
valid: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
outVals: []int64{1e8, 2e8, 1e8, 2e7, 3e5},
|
|
|
|
feeRate: 12500,
|
|
|
|
valid: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
outVals: []int64{1e8, 2e8, 1e8, 2e7, 3e5},
|
|
|
|
feeRate: 50000,
|
|
|
|
valid: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
outVals: []int64{1e8, 2e8, 1e8, 2e7, 3e5, 1e8, 2e8,
|
|
|
|
1e8, 2e7, 3e5},
|
|
|
|
feeRate: 44250,
|
|
|
|
valid: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-06-19 03:52:50 +03:00
|
|
|
for i, test := range testCases {
|
2019-03-05 16:22:30 +03:00
|
|
|
feeRate := test.feeRate
|
|
|
|
|
|
|
|
// Grab some fresh addresses from the miner that we will send
|
|
|
|
// to.
|
|
|
|
outputs := make([]*wire.TxOut, len(test.outVals))
|
|
|
|
for i, outVal := range test.outVals {
|
|
|
|
minerAddr, err := r.NewAddress()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to generate address for "+
|
|
|
|
"miner: %v", err)
|
|
|
|
}
|
|
|
|
script, err := txscript.PayToAddrScript(minerAddr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create pay to addr "+
|
|
|
|
"script: %v", err)
|
|
|
|
}
|
|
|
|
output := &wire.TxOut{
|
|
|
|
Value: outVal,
|
|
|
|
PkScript: script,
|
|
|
|
}
|
|
|
|
|
|
|
|
outputs[i] = output
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now try creating a tx spending to these outputs.
|
|
|
|
createTx, createErr := w.CreateSimpleTx(
|
|
|
|
outputs, feeRate, true,
|
|
|
|
)
|
2019-06-19 03:52:50 +03:00
|
|
|
switch {
|
|
|
|
case test.valid && createErr != nil:
|
2019-03-05 16:22:30 +03:00
|
|
|
fmt.Println(spew.Sdump(createTx.Tx))
|
|
|
|
t.Fatalf("got unexpected error when creating tx: %v",
|
|
|
|
createErr)
|
2019-06-19 03:52:50 +03:00
|
|
|
|
|
|
|
case !test.valid && createErr == nil:
|
|
|
|
t.Fatalf("test #%v should have failed on tx "+
|
|
|
|
"creation", i)
|
2019-03-05 16:22:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Also send to these outputs. This should result in a tx
|
|
|
|
// _very_ similar to the one we just created being sent. The
|
|
|
|
// only difference is that the dry run tx is not signed, and
|
|
|
|
// that the change output position might be different.
|
|
|
|
tx, sendErr := w.SendOutputs(outputs, feeRate)
|
2019-06-19 03:52:50 +03:00
|
|
|
switch {
|
|
|
|
case test.valid && sendErr != nil:
|
2019-03-05 16:22:30 +03:00
|
|
|
t.Fatalf("got unexpected error when sending tx: %v",
|
|
|
|
sendErr)
|
2019-06-19 03:52:50 +03:00
|
|
|
|
|
|
|
case !test.valid && sendErr == nil:
|
|
|
|
t.Fatalf("test #%v should fail for tx sending", i)
|
2019-03-05 16:22:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// We expected either both to not fail, or both to fail with
|
|
|
|
// the same error.
|
|
|
|
if createErr != sendErr {
|
|
|
|
t.Fatalf("error creating tx (%v) different "+
|
|
|
|
"from error sending outputs (%v)",
|
|
|
|
createErr, sendErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we expected the creation to fail, then this test is over.
|
|
|
|
if !test.valid {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
txid := tx.TxHash()
|
|
|
|
err = waitForMempoolTx(r, &txid)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("tx not relayed to miner: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper method to check that the two txs are similar.
|
|
|
|
assertSimilarTx := func(a, b *wire.MsgTx) error {
|
|
|
|
if a.Version != b.Version {
|
|
|
|
return fmt.Errorf("different versions: "+
|
|
|
|
"%v vs %v", a.Version, b.Version)
|
|
|
|
}
|
|
|
|
if a.LockTime != b.LockTime {
|
|
|
|
return fmt.Errorf("different locktimes: "+
|
|
|
|
"%v vs %v", a.LockTime, b.LockTime)
|
|
|
|
}
|
|
|
|
if len(a.TxIn) != len(b.TxIn) {
|
|
|
|
return fmt.Errorf("different number of "+
|
|
|
|
"inputs: %v vs %v", len(a.TxIn),
|
|
|
|
len(b.TxIn))
|
|
|
|
}
|
|
|
|
if len(a.TxOut) != len(b.TxOut) {
|
|
|
|
return fmt.Errorf("different number of "+
|
|
|
|
"outputs: %v vs %v", len(a.TxOut),
|
|
|
|
len(b.TxOut))
|
|
|
|
}
|
|
|
|
|
|
|
|
// They should be spending the same inputs.
|
|
|
|
for i := range a.TxIn {
|
|
|
|
prevA := a.TxIn[i].PreviousOutPoint
|
|
|
|
prevB := b.TxIn[i].PreviousOutPoint
|
|
|
|
if prevA != prevB {
|
|
|
|
return fmt.Errorf("different inputs: "+
|
|
|
|
"%v vs %v", spew.Sdump(prevA),
|
|
|
|
spew.Sdump(prevB))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// They should have the same outputs. Since the change
|
|
|
|
// output position gets randomized, they are not
|
|
|
|
// guaranteed to be in the same order.
|
|
|
|
for _, outA := range a.TxOut {
|
|
|
|
found := false
|
|
|
|
for _, outB := range b.TxOut {
|
|
|
|
if reflect.DeepEqual(outA, outB) {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
return fmt.Errorf("did not find "+
|
|
|
|
"output %v", spew.Sdump(outA))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assert that our "template tx" was similar to the one that
|
|
|
|
// ended up being sent.
|
|
|
|
if err := assertSimilarTx(createTx.Tx, tx); err != nil {
|
|
|
|
t.Fatalf("transactions not similar: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
lnwallet: when signing create account if not found
In this commit, we address an edge case that can happen a user rescans
w/ their seed, while retaining their existing `channel.db`. Once they
rescan, if they go to sign for a channel sweep for example, the
commitment key family (actually an account) may not yet have been
created, causing the signing attempt to fail.
We remedy this always creating the account if we go to sign, and the
account isn't found. The change has been structured to make this the
exception, so we'll avoid always needing to do 2 DB hits (check if
account exists, sign), each time we sign.
A new test has been added to exercise this behavior. If the diff from
the `signer.go` file is removed, then the test will fail.
2019-08-21 05:08:03 +03:00
|
|
|
// testSignOutputCreateAccount tests that we're able to properly sign for an
|
|
|
|
// output if the target account hasn't yet been created on disk. In this case,
|
|
|
|
// we'll create the account, then sign.
|
|
|
|
func testSignOutputCreateAccount(r *rpctest.Harness, w *lnwallet.LightningWallet,
|
|
|
|
_ *lnwallet.LightningWallet, t *testing.T) {
|
|
|
|
|
|
|
|
// First, we'll create a sign desc that references a non-default key
|
|
|
|
// family. Under the hood, key families are actually accounts, so this
|
|
|
|
// should force create of the account so we can sign with it.
|
|
|
|
fakeTx := wire.NewMsgTx(2)
|
|
|
|
fakeTx.AddTxIn(&wire.TxIn{
|
|
|
|
PreviousOutPoint: wire.OutPoint{
|
|
|
|
Hash: chainhash.Hash{},
|
|
|
|
Index: 0,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
signDesc := &input.SignDescriptor{
|
|
|
|
KeyDesc: keychain.KeyDescriptor{
|
|
|
|
KeyLocator: keychain.KeyLocator{
|
|
|
|
Family: 99,
|
|
|
|
Index: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
WitnessScript: []byte{},
|
|
|
|
Output: &wire.TxOut{
|
|
|
|
Value: 1000,
|
|
|
|
},
|
|
|
|
HashType: txscript.SigHashAll,
|
|
|
|
SigHashes: txscript.NewTxSigHashes(fakeTx),
|
|
|
|
InputIndex: 0,
|
|
|
|
}
|
|
|
|
|
|
|
|
// We'll now sign and expect this to succeed, as even though the
|
|
|
|
// account doesn't exist atm, it should be created in order to process
|
|
|
|
// the inbound signing request.
|
|
|
|
_, err := w.Cfg.Signer.SignOutputRaw(fakeTx, signDesc)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to sign for output with non-existent "+
|
|
|
|
"account: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-30 04:48:01 +03:00
|
|
|
type walletTestCase struct {
|
|
|
|
name string
|
|
|
|
test func(miner *rpctest.Harness, alice, bob *lnwallet.LightningWallet,
|
|
|
|
test *testing.T)
|
2015-12-19 06:48:55 +03:00
|
|
|
}
|
|
|
|
|
2017-07-30 04:48:01 +03:00
|
|
|
var walletTests = []walletTestCase{
|
2018-11-14 07:01:54 +03:00
|
|
|
{
|
|
|
|
// TODO(wilmer): this test should remain first until the wallet
|
|
|
|
// can properly craft a transaction that spends all of its
|
|
|
|
// on-chain funds.
|
|
|
|
name: "change output spend confirmation",
|
|
|
|
test: testChangeOutputSpendConfirmation,
|
|
|
|
},
|
2017-11-26 22:32:57 +03:00
|
|
|
{
|
|
|
|
name: "insane fee reject",
|
|
|
|
test: testReservationInitiatorBalanceBelowDustCancel,
|
|
|
|
},
|
2017-07-30 04:48:01 +03:00
|
|
|
{
|
|
|
|
name: "single funding workflow",
|
2019-08-01 06:16:52 +03:00
|
|
|
test: func(miner *rpctest.Harness, alice,
|
|
|
|
bob *lnwallet.LightningWallet, t *testing.T) {
|
|
|
|
|
|
|
|
testSingleFunderReservationWorkflow(
|
2020-03-06 18:11:48 +03:00
|
|
|
miner, alice, bob, t,
|
2020-03-14 02:57:48 +03:00
|
|
|
lnwallet.CommitmentTypeLegacy, nil,
|
|
|
|
nil, [32]byte{}, 0,
|
2019-08-01 06:16:52 +03:00
|
|
|
)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "single funding workflow tweakless",
|
|
|
|
test: func(miner *rpctest.Harness, alice,
|
|
|
|
bob *lnwallet.LightningWallet, t *testing.T) {
|
|
|
|
|
|
|
|
testSingleFunderReservationWorkflow(
|
2020-03-06 18:11:48 +03:00
|
|
|
miner, alice, bob, t,
|
2020-03-14 02:57:48 +03:00
|
|
|
lnwallet.CommitmentTypeTweakless, nil,
|
|
|
|
nil, [32]byte{}, 0,
|
2019-08-01 06:16:52 +03:00
|
|
|
)
|
|
|
|
},
|
2017-07-30 04:48:01 +03:00
|
|
|
},
|
2019-11-01 07:47:50 +03:00
|
|
|
{
|
|
|
|
name: "single funding workflow external funding tx",
|
|
|
|
test: testSingleFunderExternalFundingTx,
|
|
|
|
},
|
2017-07-30 04:48:01 +03:00
|
|
|
{
|
|
|
|
name: "dual funder workflow",
|
|
|
|
test: testDualFundingReservationWorkflow,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "output locking",
|
|
|
|
test: testFundingTransactionLockedOutputs,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "reservation insufficient funds",
|
|
|
|
test: testFundingCancellationNotEnoughFunds,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "transaction subscriptions",
|
|
|
|
test: testTransactionSubscriptions,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "transaction details",
|
|
|
|
test: testListTransactionDetails,
|
|
|
|
},
|
2018-01-19 16:24:30 +03:00
|
|
|
{
|
|
|
|
name: "publish transaction",
|
|
|
|
test: testPublishTransaction,
|
|
|
|
},
|
2017-07-30 04:48:01 +03:00
|
|
|
{
|
|
|
|
name: "signed with tweaked pubkeys",
|
|
|
|
test: testSignOutputUsingTweaks,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "test cancel non-existent reservation",
|
2018-02-07 06:11:11 +03:00
|
|
|
test: testCancelNonExistentReservation,
|
2017-07-30 04:48:01 +03:00
|
|
|
},
|
2019-02-20 06:16:39 +03:00
|
|
|
{
|
|
|
|
name: "last unused addr",
|
|
|
|
test: testLastUnusedAddr,
|
|
|
|
},
|
2017-06-29 19:48:10 +03:00
|
|
|
{
|
|
|
|
name: "reorg wallet balance",
|
|
|
|
test: testReorgWalletBalance,
|
|
|
|
},
|
2019-03-05 16:22:30 +03:00
|
|
|
{
|
|
|
|
name: "create simple tx",
|
|
|
|
test: testCreateSimpleTx,
|
|
|
|
},
|
lnwallet: when signing create account if not found
In this commit, we address an edge case that can happen a user rescans
w/ their seed, while retaining their existing `channel.db`. Once they
rescan, if they go to sign for a channel sweep for example, the
commitment key family (actually an account) may not yet have been
created, causing the signing attempt to fail.
We remedy this always creating the account if we go to sign, and the
account isn't found. The change has been structured to make this the
exception, so we'll avoid always needing to do 2 DB hits (check if
account exists, sign), each time we sign.
A new test has been added to exercise this behavior. If the diff from
the `signer.go` file is removed, then the test will fail.
2019-08-21 05:08:03 +03:00
|
|
|
{
|
|
|
|
name: "test sign create account",
|
|
|
|
test: testSignOutputCreateAccount,
|
|
|
|
},
|
2015-12-19 06:48:55 +03:00
|
|
|
}
|
|
|
|
|
2017-07-30 21:42:07 +03:00
|
|
|
func clearWalletStates(a, b *lnwallet.LightningWallet) error {
|
|
|
|
a.ResetReservations()
|
|
|
|
b.ResetReservations()
|
2016-06-21 20:40:20 +03:00
|
|
|
|
2017-07-30 21:42:07 +03:00
|
|
|
if err := a.Cfg.Database.Wipe(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return b.Cfg.Database.Wipe()
|
2015-11-19 01:59:07 +03:00
|
|
|
}
|
2015-12-16 22:22:36 +03:00
|
|
|
|
2017-11-10 03:30:20 +03:00
|
|
|
func waitForMempoolTx(r *rpctest.Harness, txid *chainhash.Hash) error {
|
|
|
|
var found bool
|
|
|
|
var tx *btcutil.Tx
|
2017-06-29 19:48:10 +03:00
|
|
|
var err error
|
2018-03-12 23:52:16 +03:00
|
|
|
timeout := time.After(30 * time.Second)
|
2017-11-10 03:30:20 +03:00
|
|
|
for !found {
|
|
|
|
// Do a short wait
|
|
|
|
select {
|
|
|
|
case <-timeout:
|
|
|
|
return fmt.Errorf("timeout after 10s")
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
|
|
|
|
// Check for the harness' knowledge of the txid
|
|
|
|
tx, err = r.Node.GetRawTransaction(txid)
|
2017-06-29 19:48:10 +03:00
|
|
|
if err != nil {
|
2017-11-10 03:30:20 +03:00
|
|
|
switch e := err.(type) {
|
|
|
|
case *btcjson.RPCError:
|
|
|
|
if e.Code == btcjson.ErrRPCNoTxInfo {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
}
|
2017-06-29 19:48:10 +03:00
|
|
|
return err
|
|
|
|
}
|
2017-11-10 03:30:20 +03:00
|
|
|
if tx != nil && tx.MsgTx().TxHash() == *txid {
|
|
|
|
found = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func waitForWalletSync(r *rpctest.Harness, w *lnwallet.LightningWallet) error {
|
2018-08-01 06:29:39 +03:00
|
|
|
var (
|
|
|
|
synced bool
|
|
|
|
err error
|
|
|
|
bestHash, knownHash *chainhash.Hash
|
|
|
|
bestHeight, knownHeight int32
|
|
|
|
)
|
2018-08-09 05:45:10 +03:00
|
|
|
timeout := time.After(10 * time.Second)
|
2017-11-10 03:30:20 +03:00
|
|
|
for !synced {
|
|
|
|
// Do a short wait
|
2017-06-29 19:48:10 +03:00
|
|
|
select {
|
|
|
|
case <-timeout:
|
2018-08-01 06:29:39 +03:00
|
|
|
return fmt.Errorf("timeout after 30s")
|
2018-08-09 05:45:10 +03:00
|
|
|
case <-time.Tick(100 * time.Millisecond):
|
2017-06-29 19:48:10 +03:00
|
|
|
}
|
2017-11-10 03:30:20 +03:00
|
|
|
|
|
|
|
// Check whether the chain source of the wallet is caught up to
|
|
|
|
// the harness it's supposed to be catching up to.
|
|
|
|
bestHash, bestHeight, err = r.Node.GetBestBlock()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
knownHash, knownHeight, err = w.Cfg.ChainIO.GetBestBlock()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if knownHeight != bestHeight {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if *knownHash != *bestHash {
|
|
|
|
return fmt.Errorf("hash at height %d doesn't match: "+
|
|
|
|
"expected %s, got %s", bestHeight, bestHash,
|
|
|
|
knownHash)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for synchronization.
|
2018-02-21 06:15:40 +03:00
|
|
|
synced, _, err = w.IsSynced()
|
2017-11-10 03:30:20 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-06-29 19:48:10 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-01 07:47:50 +03:00
|
|
|
// testSingleFunderExternalFundingTx tests that the wallet is able to properly
|
|
|
|
// carry out a funding flow backed by a channel point that has been crafted
|
|
|
|
// outside the wallet.
|
|
|
|
func testSingleFunderExternalFundingTx(miner *rpctest.Harness,
|
|
|
|
alice, bob *lnwallet.LightningWallet, t *testing.T) {
|
|
|
|
|
|
|
|
// First, we'll obtain multi-sig keys from both Alice and Bob which
|
|
|
|
// simulates them exchanging keys on a higher level.
|
|
|
|
aliceFundingKey, err := alice.DeriveNextKey(keychain.KeyFamilyMultiSig)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to obtain alice funding key: %v", err)
|
|
|
|
}
|
|
|
|
bobFundingKey, err := bob.DeriveNextKey(keychain.KeyFamilyMultiSig)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to obtain bob funding key: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We'll now set up for them to open a 4 BTC channel, with 1 BTC pushed
|
|
|
|
// to Bob's side.
|
|
|
|
chanAmt := 4 * btcutil.SatoshiPerBitcoin
|
|
|
|
|
|
|
|
// Simulating external funding negotiation, we'll now create the
|
|
|
|
// funding transaction for both parties. Utilizing existing tools,
|
|
|
|
// we'll create a new chanfunding.Assembler hacked by Alice's wallet.
|
|
|
|
aliceChanFunder := chanfunding.NewWalletAssembler(chanfunding.WalletConfig{
|
|
|
|
CoinSource: lnwallet.NewCoinSource(alice),
|
|
|
|
CoinSelectLocker: alice,
|
|
|
|
CoinLocker: alice,
|
|
|
|
Signer: alice.Cfg.Signer,
|
|
|
|
DustLimit: 600,
|
|
|
|
})
|
|
|
|
|
|
|
|
// With the chan funder created, we'll now provision a funding intent,
|
|
|
|
// bind the keys we obtained above, and finally obtain our funding
|
|
|
|
// transaction and outpoint.
|
|
|
|
fundingIntent, err := aliceChanFunder.ProvisionChannel(&chanfunding.Request{
|
|
|
|
LocalAmt: btcutil.Amount(chanAmt),
|
|
|
|
MinConfs: 1,
|
|
|
|
FeeRate: 253,
|
|
|
|
ChangeAddr: func() (btcutil.Address, error) {
|
|
|
|
return alice.NewAddress(lnwallet.WitnessPubKey, true)
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to perform coin selection: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// With our intent created, we'll instruct it to finalize the funding
|
|
|
|
// transaction, and also hand us the outpoint so we can simulate
|
|
|
|
// external crafting of the funding transaction.
|
|
|
|
var (
|
|
|
|
fundingTx *wire.MsgTx
|
|
|
|
chanPoint *wire.OutPoint
|
|
|
|
)
|
|
|
|
if fullIntent, ok := fundingIntent.(*chanfunding.FullIntent); ok {
|
|
|
|
fullIntent.BindKeys(&aliceFundingKey, bobFundingKey.PubKey)
|
|
|
|
|
|
|
|
fundingTx, err = fullIntent.CompileFundingTx(nil, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to compile funding tx: %v", err)
|
|
|
|
}
|
|
|
|
chanPoint, err = fullIntent.ChanPoint()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to obtain chan point: %v", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
t.Fatalf("expected full intent, instead got: %T", fullIntent)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that we have the fully constructed funding transaction, we'll
|
|
|
|
// create a new shim external funder out of it for Alice, and prep a
|
|
|
|
// shim intent for Bob.
|
2020-03-14 02:57:48 +03:00
|
|
|
thawHeight := uint32(200)
|
2019-11-01 07:47:50 +03:00
|
|
|
aliceExternalFunder := chanfunding.NewCannedAssembler(
|
2020-03-14 02:57:48 +03:00
|
|
|
thawHeight, *chanPoint, btcutil.Amount(chanAmt), &aliceFundingKey,
|
2019-11-01 07:47:50 +03:00
|
|
|
bobFundingKey.PubKey, true,
|
|
|
|
)
|
|
|
|
bobShimIntent, err := chanfunding.NewCannedAssembler(
|
2020-03-14 02:57:48 +03:00
|
|
|
thawHeight, *chanPoint, btcutil.Amount(chanAmt), &bobFundingKey,
|
2019-11-01 07:47:50 +03:00
|
|
|
aliceFundingKey.PubKey, false,
|
2019-12-21 00:14:15 +03:00
|
|
|
).ProvisionChannel(&chanfunding.Request{
|
|
|
|
LocalAmt: btcutil.Amount(chanAmt),
|
|
|
|
MinConfs: 1,
|
|
|
|
FeeRate: 253,
|
|
|
|
ChangeAddr: func() (btcutil.Address, error) {
|
|
|
|
return bob.NewAddress(lnwallet.WitnessPubKey, true)
|
|
|
|
},
|
|
|
|
})
|
2019-11-01 07:47:50 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create shim intent for bob: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// At this point, we have everything we need to carry out our test, so
|
|
|
|
// we'll being the funding flow between Alice and Bob.
|
|
|
|
//
|
|
|
|
// However, before we do so, we'll register a new shim intent for Bob,
|
|
|
|
// so he knows what keys to use when he receives the funding request
|
|
|
|
// from Alice.
|
|
|
|
pendingChanID := testHdSeed
|
|
|
|
err = bob.RegisterFundingIntent(pendingChanID, bobShimIntent)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to register intent: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now we can carry out the single funding flow as normal, we'll
|
|
|
|
// specify our external funder and funding transaction, as well as the
|
|
|
|
// pending channel ID generated above to allow Alice and Bob to track
|
|
|
|
// the funding flow externally.
|
|
|
|
testSingleFunderReservationWorkflow(
|
2020-03-06 18:11:48 +03:00
|
|
|
miner, alice, bob, t, lnwallet.CommitmentTypeTweakless,
|
|
|
|
aliceExternalFunder, func() *wire.MsgTx {
|
2019-11-01 07:47:50 +03:00
|
|
|
return fundingTx
|
2020-03-14 02:57:48 +03:00
|
|
|
}, pendingChanID, thawHeight,
|
2019-11-01 07:47:50 +03:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2016-08-13 01:50:47 +03:00
|
|
|
// TestInterfaces tests all registered interfaces with a unified set of tests
|
2018-02-07 06:11:11 +03:00
|
|
|
// which exercise each of the required methods found within the WalletController
|
2016-08-13 01:50:47 +03:00
|
|
|
// interface.
|
|
|
|
//
|
|
|
|
// NOTE: In the future, when additional implementations of the WalletController
|
|
|
|
// interface have been implemented, in order to ensure the new concrete
|
|
|
|
// implementation is automatically tested, two steps must be undertaken. First,
|
|
|
|
// one needs add a "non-captured" (_) import from the new sub-package. This
|
2017-07-30 21:42:07 +03:00
|
|
|
// import should trigger an init() method within the package which registers
|
2016-08-13 01:50:47 +03:00
|
|
|
// the interface. Second, an additional case in the switch within the main loop
|
|
|
|
// below needs to be added which properly initializes the interface.
|
|
|
|
//
|
2016-06-21 20:40:20 +03:00
|
|
|
// TODO(roasbeef): purge bobNode in favor of dual lnwallet's
|
2015-12-19 06:48:55 +03:00
|
|
|
func TestLightningWallet(t *testing.T) {
|
2017-06-17 01:59:20 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2016-02-03 10:59:27 +03:00
|
|
|
// Initialize the harness around a btcd node which will serve as our
|
|
|
|
// dedicated miner to generate blocks, cause re-orgs, etc. We'll set
|
2018-02-07 06:11:11 +03:00
|
|
|
// up this node with a chain length of 125, so we have plenty of BTC
|
2016-02-03 10:59:27 +03:00
|
|
|
// to play around with.
|
2018-08-17 05:48:25 +03:00
|
|
|
miningNode, err := rpctest.New(netParams, nil, []string{"--txindex"})
|
2016-02-03 10:59:27 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create mining node: %v", err)
|
|
|
|
}
|
2016-11-16 23:54:27 +03:00
|
|
|
defer miningNode.TearDown()
|
2016-02-03 10:59:27 +03:00
|
|
|
if err := miningNode.SetUp(true, 25); err != nil {
|
|
|
|
t.Fatalf("unable to set up mining node: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-01-06 00:20:29 +03:00
|
|
|
// Next mine enough blocks in order for segwit and the CSV package
|
2017-11-10 03:30:20 +03:00
|
|
|
// soft-fork to activate on RegNet.
|
2017-01-06 00:20:29 +03:00
|
|
|
numBlocks := netParams.MinerConfirmationWindow * 2
|
|
|
|
if _, err := miningNode.Node.Generate(numBlocks); err != nil {
|
|
|
|
t.Fatalf("unable to generate blocks: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-08-13 01:50:47 +03:00
|
|
|
rpcConfig := miningNode.RPCConfig()
|
|
|
|
|
2018-05-22 22:49:38 +03:00
|
|
|
tempDir, err := ioutil.TempDir("", "channeldb")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create temp dir: %v", err)
|
|
|
|
}
|
|
|
|
db, err := channeldb.Open(tempDir)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create db: %v", err)
|
|
|
|
}
|
2018-10-02 00:19:56 +03:00
|
|
|
hintCache, err := chainntnfs.NewHeightHintCache(db)
|
2018-05-22 22:49:38 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create height hint cache: %v", err)
|
|
|
|
}
|
2018-12-07 08:14:10 +03:00
|
|
|
chainNotifier, err := btcdnotify.New(
|
|
|
|
&rpcConfig, netParams, hintCache, hintCache,
|
|
|
|
)
|
2015-12-19 06:48:55 +03:00
|
|
|
if err != nil {
|
2016-08-13 01:50:47 +03:00
|
|
|
t.Fatalf("unable to create notifier: %v", err)
|
2015-12-19 06:48:55 +03:00
|
|
|
}
|
2016-08-13 01:50:47 +03:00
|
|
|
if err := chainNotifier.Start(); err != nil {
|
|
|
|
t.Fatalf("unable to start notifier: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-11-10 03:30:20 +03:00
|
|
|
for _, walletDriver := range lnwallet.RegisteredWallets() {
|
|
|
|
for _, backEnd := range walletDriver.BackEnds() {
|
2019-11-01 07:48:06 +03:00
|
|
|
if !runTests(t, walletDriver, backEnd, miningNode,
|
|
|
|
rpcConfig, chainNotifier) {
|
|
|
|
return
|
|
|
|
}
|
2017-11-10 03:30:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// runTests runs all of the tests for a single interface implementation and
|
|
|
|
// chain back-end combination. This makes it easier to use `defer` as well as
|
|
|
|
// factoring out the test logic from the loop which cycles through the
|
|
|
|
// interface implementations.
|
|
|
|
func runTests(t *testing.T, walletDriver *lnwallet.WalletDriver,
|
|
|
|
backEnd string, miningNode *rpctest.Harness,
|
|
|
|
rpcConfig rpcclient.ConnConfig,
|
2019-11-01 07:48:06 +03:00
|
|
|
chainNotifier chainntnfs.ChainNotifier) bool {
|
2018-09-05 04:52:16 +03:00
|
|
|
|
2017-07-30 21:42:07 +03:00
|
|
|
var (
|
|
|
|
bio lnwallet.BlockChainIO
|
|
|
|
|
2019-01-16 17:47:43 +03:00
|
|
|
aliceSigner input.Signer
|
|
|
|
bobSigner input.Signer
|
2017-07-30 21:42:07 +03:00
|
|
|
|
2018-02-18 02:25:55 +03:00
|
|
|
aliceKeyRing keychain.SecretKeyRing
|
|
|
|
bobKeyRing keychain.SecretKeyRing
|
|
|
|
|
2017-07-30 21:42:07 +03:00
|
|
|
aliceWalletController lnwallet.WalletController
|
|
|
|
bobWalletController lnwallet.WalletController
|
|
|
|
)
|
|
|
|
|
2017-11-10 03:30:20 +03:00
|
|
|
tempTestDirAlice, err := ioutil.TempDir("", "lnwallet")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create temp directory: %v", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tempTestDirAlice)
|
2016-08-13 01:50:47 +03:00
|
|
|
|
2017-11-10 03:30:20 +03:00
|
|
|
tempTestDirBob, err := ioutil.TempDir("", "lnwallet")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create temp directory: %v", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tempTestDirBob)
|
|
|
|
|
|
|
|
walletType := walletDriver.WalletType
|
|
|
|
switch walletType {
|
|
|
|
case "btcwallet":
|
|
|
|
var aliceClient, bobClient chain.Interface
|
|
|
|
switch backEnd {
|
|
|
|
case "btcd":
|
|
|
|
aliceClient, err = chain.NewRPCClient(netParams,
|
2017-06-06 01:05:53 +03:00
|
|
|
rpcConfig.Host, rpcConfig.User, rpcConfig.Pass,
|
|
|
|
rpcConfig.Certificates, false, 20)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to make chain rpc: %v", err)
|
|
|
|
}
|
2017-11-10 03:30:20 +03:00
|
|
|
bobClient, err = chain.NewRPCClient(netParams,
|
2017-07-30 21:42:07 +03:00
|
|
|
rpcConfig.Host, rpcConfig.User, rpcConfig.Pass,
|
|
|
|
rpcConfig.Certificates, false, 20)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to make chain rpc: %v", err)
|
|
|
|
}
|
2018-02-18 02:25:55 +03:00
|
|
|
|
2017-11-10 03:30:20 +03:00
|
|
|
case "neutrino":
|
|
|
|
// Set some package-level variable to speed up
|
|
|
|
// operation for tests.
|
|
|
|
neutrino.BanDuration = time.Millisecond * 100
|
|
|
|
neutrino.QueryTimeout = time.Millisecond * 500
|
2018-06-05 04:41:41 +03:00
|
|
|
neutrino.QueryNumRetries = 1
|
2018-02-18 02:25:55 +03:00
|
|
|
|
2017-11-10 03:30:20 +03:00
|
|
|
// Start Alice - open a database, start a neutrino
|
|
|
|
// instance, and initialize a btcwallet driver for it.
|
2019-10-04 18:10:49 +03:00
|
|
|
aliceDB, err := walletdb.Create(
|
|
|
|
"bdb", tempTestDirAlice+"/neutrino.db", true,
|
|
|
|
)
|
2017-11-10 03:30:20 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create DB: %v", err)
|
|
|
|
}
|
|
|
|
defer aliceDB.Close()
|
|
|
|
aliceChain, err := neutrino.NewChainService(
|
|
|
|
neutrino.Config{
|
|
|
|
DataDir: tempTestDirAlice,
|
|
|
|
Database: aliceDB,
|
|
|
|
ChainParams: *netParams,
|
|
|
|
ConnectPeers: []string{
|
|
|
|
miningNode.P2PAddress(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to make neutrino: %v", err)
|
|
|
|
}
|
|
|
|
aliceChain.Start()
|
|
|
|
defer aliceChain.Stop()
|
2018-03-27 00:14:47 +03:00
|
|
|
aliceClient = chain.NewNeutrinoClient(
|
|
|
|
netParams, aliceChain,
|
|
|
|
)
|
2017-11-10 03:30:20 +03:00
|
|
|
|
|
|
|
// Start Bob - open a database, start a neutrino
|
|
|
|
// instance, and initialize a btcwallet driver for it.
|
2019-10-04 18:10:49 +03:00
|
|
|
bobDB, err := walletdb.Create(
|
|
|
|
"bdb", tempTestDirBob+"/neutrino.db", true,
|
|
|
|
)
|
2017-11-10 03:30:20 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create DB: %v", err)
|
|
|
|
}
|
|
|
|
defer bobDB.Close()
|
|
|
|
bobChain, err := neutrino.NewChainService(
|
|
|
|
neutrino.Config{
|
|
|
|
DataDir: tempTestDirBob,
|
|
|
|
Database: bobDB,
|
|
|
|
ChainParams: *netParams,
|
|
|
|
ConnectPeers: []string{
|
|
|
|
miningNode.P2PAddress(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to make neutrino: %v", err)
|
|
|
|
}
|
|
|
|
bobChain.Start()
|
|
|
|
defer bobChain.Stop()
|
2018-03-27 00:14:47 +03:00
|
|
|
bobClient = chain.NewNeutrinoClient(
|
|
|
|
netParams, bobChain,
|
|
|
|
)
|
2018-02-18 02:25:55 +03:00
|
|
|
|
2017-11-10 03:30:20 +03:00
|
|
|
case "bitcoind":
|
|
|
|
// Start a bitcoind instance.
|
|
|
|
tempBitcoindDir, err := ioutil.TempDir("", "bitcoind")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create temp directory: %v", err)
|
|
|
|
}
|
2018-07-17 02:50:47 +03:00
|
|
|
zmqBlockHost := "ipc:///" + tempBitcoindDir + "/blocks.socket"
|
|
|
|
zmqTxHost := "ipc:///" + tempBitcoindDir + "/tx.socket"
|
2017-11-10 03:30:20 +03:00
|
|
|
defer os.RemoveAll(tempBitcoindDir)
|
|
|
|
rpcPort := rand.Int()%(65536-1024) + 1024
|
|
|
|
bitcoind := exec.Command(
|
|
|
|
"bitcoind",
|
|
|
|
"-datadir="+tempBitcoindDir,
|
|
|
|
"-regtest",
|
|
|
|
"-connect="+miningNode.P2PAddress(),
|
|
|
|
"-txindex",
|
|
|
|
"-rpcauth=weks:469e9bb14ab2360f8e226efed5ca6f"+
|
|
|
|
"d$507c670e800a95284294edb5773b05544b"+
|
|
|
|
"220110063096c221be9933c82d38e1",
|
|
|
|
fmt.Sprintf("-rpcport=%d", rpcPort),
|
|
|
|
"-disablewallet",
|
2018-07-17 02:50:47 +03:00
|
|
|
"-zmqpubrawblock="+zmqBlockHost,
|
|
|
|
"-zmqpubrawtx="+zmqTxHost,
|
2017-11-10 03:30:20 +03:00
|
|
|
)
|
|
|
|
err = bitcoind.Start()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("couldn't start bitcoind: %v", err)
|
|
|
|
}
|
|
|
|
defer bitcoind.Wait()
|
|
|
|
defer bitcoind.Process.Kill()
|
|
|
|
|
2018-07-17 02:50:47 +03:00
|
|
|
// Wait for the bitcoind instance to start up.
|
|
|
|
time.Sleep(time.Second)
|
2017-11-10 03:30:20 +03:00
|
|
|
|
2018-07-17 02:50:47 +03:00
|
|
|
host := fmt.Sprintf("127.0.0.1:%d", rpcPort)
|
|
|
|
chainConn, err := chain.NewBitcoindConn(
|
|
|
|
netParams, host, "weks", "weks", zmqBlockHost,
|
|
|
|
zmqTxHost, 100*time.Millisecond,
|
|
|
|
)
|
2016-08-13 01:50:47 +03:00
|
|
|
if err != nil {
|
2018-07-17 02:50:47 +03:00
|
|
|
t.Fatalf("unable to establish connection to "+
|
|
|
|
"bitcoind: %v", err)
|
|
|
|
}
|
|
|
|
if err := chainConn.Start(); err != nil {
|
|
|
|
t.Fatalf("unable to establish connection to "+
|
|
|
|
"bitcoind: %v", err)
|
2016-08-13 01:50:47 +03:00
|
|
|
}
|
2018-07-17 02:50:47 +03:00
|
|
|
defer chainConn.Stop()
|
|
|
|
|
|
|
|
// Create a btcwallet bitcoind client for both Alice and
|
|
|
|
// Bob.
|
2018-09-13 06:55:51 +03:00
|
|
|
aliceClient = chainConn.NewBitcoindClient()
|
|
|
|
bobClient = chainConn.NewBitcoindClient()
|
2016-08-13 01:50:47 +03:00
|
|
|
default:
|
2017-11-10 03:30:20 +03:00
|
|
|
t.Fatalf("unknown chain driver: %v", backEnd)
|
2016-08-13 01:50:47 +03:00
|
|
|
}
|
|
|
|
|
2018-09-05 04:52:16 +03:00
|
|
|
aliceSeed := sha256.New()
|
|
|
|
aliceSeed.Write([]byte(backEnd))
|
|
|
|
aliceSeed.Write(aliceHDSeed[:])
|
|
|
|
aliceSeedBytes := aliceSeed.Sum(nil)
|
|
|
|
|
2017-11-10 03:30:20 +03:00
|
|
|
aliceWalletConfig := &btcwallet.Config{
|
2019-04-13 00:13:48 +03:00
|
|
|
PrivatePass: []byte("alice-pass"),
|
|
|
|
HdSeed: aliceSeedBytes,
|
|
|
|
DataDir: tempTestDirAlice,
|
|
|
|
NetParams: netParams,
|
|
|
|
ChainSource: aliceClient,
|
|
|
|
CoinType: keychain.CoinTypeTestnet,
|
2017-11-10 03:30:20 +03:00
|
|
|
}
|
|
|
|
aliceWalletController, err = walletDriver.New(aliceWalletConfig)
|
2017-07-30 21:42:07 +03:00
|
|
|
if err != nil {
|
2017-11-10 03:30:20 +03:00
|
|
|
t.Fatalf("unable to create btcwallet: %v", err)
|
2017-07-30 21:42:07 +03:00
|
|
|
}
|
2017-11-10 03:30:20 +03:00
|
|
|
aliceSigner = aliceWalletController.(*btcwallet.BtcWallet)
|
2018-02-18 02:25:55 +03:00
|
|
|
aliceKeyRing = keychain.NewBtcWalletKeyRing(
|
|
|
|
aliceWalletController.(*btcwallet.BtcWallet).InternalWallet(),
|
2018-03-13 03:32:37 +03:00
|
|
|
keychain.CoinTypeTestnet,
|
2018-02-18 02:25:55 +03:00
|
|
|
)
|
2017-11-10 03:30:20 +03:00
|
|
|
|
2018-09-05 04:52:16 +03:00
|
|
|
bobSeed := sha256.New()
|
|
|
|
bobSeed.Write([]byte(backEnd))
|
|
|
|
bobSeed.Write(bobHDSeed[:])
|
|
|
|
bobSeedBytes := bobSeed.Sum(nil)
|
|
|
|
|
2017-11-10 03:30:20 +03:00
|
|
|
bobWalletConfig := &btcwallet.Config{
|
2019-04-13 00:13:48 +03:00
|
|
|
PrivatePass: []byte("bob-pass"),
|
|
|
|
HdSeed: bobSeedBytes,
|
|
|
|
DataDir: tempTestDirBob,
|
|
|
|
NetParams: netParams,
|
|
|
|
ChainSource: bobClient,
|
|
|
|
CoinType: keychain.CoinTypeTestnet,
|
2017-11-10 03:30:20 +03:00
|
|
|
}
|
|
|
|
bobWalletController, err = walletDriver.New(bobWalletConfig)
|
2016-08-13 01:50:47 +03:00
|
|
|
if err != nil {
|
2017-11-10 03:30:20 +03:00
|
|
|
t.Fatalf("unable to create btcwallet: %v", err)
|
|
|
|
}
|
|
|
|
bobSigner = bobWalletController.(*btcwallet.BtcWallet)
|
2018-02-18 02:25:55 +03:00
|
|
|
bobKeyRing = keychain.NewBtcWalletKeyRing(
|
|
|
|
bobWalletController.(*btcwallet.BtcWallet).InternalWallet(),
|
2018-03-13 03:32:37 +03:00
|
|
|
keychain.CoinTypeTestnet,
|
2018-02-18 02:25:55 +03:00
|
|
|
)
|
2017-11-10 03:30:20 +03:00
|
|
|
bio = bobWalletController.(*btcwallet.BtcWallet)
|
|
|
|
default:
|
|
|
|
t.Fatalf("unknown wallet driver: %v", walletType)
|
|
|
|
}
|
2015-12-19 06:48:55 +03:00
|
|
|
|
2017-11-10 03:30:20 +03:00
|
|
|
// Funding via 20 outputs with 4BTC each.
|
2018-02-18 02:25:55 +03:00
|
|
|
alice, err := createTestWallet(
|
|
|
|
tempTestDirAlice, miningNode, netParams,
|
|
|
|
chainNotifier, aliceWalletController, aliceKeyRing,
|
|
|
|
aliceSigner, bio,
|
|
|
|
)
|
2017-11-10 03:30:20 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create test ln wallet: %v", err)
|
|
|
|
}
|
|
|
|
defer alice.Shutdown()
|
|
|
|
|
2018-02-18 02:25:55 +03:00
|
|
|
bob, err := createTestWallet(
|
|
|
|
tempTestDirBob, miningNode, netParams,
|
2019-11-01 07:48:06 +03:00
|
|
|
chainNotifier, bobWalletController, bobKeyRing, bobSigner, bio,
|
2018-02-18 02:25:55 +03:00
|
|
|
)
|
2017-11-10 03:30:20 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create test ln wallet: %v", err)
|
|
|
|
}
|
|
|
|
defer bob.Shutdown()
|
|
|
|
|
|
|
|
// Both wallets should now have 80BTC available for
|
|
|
|
// spending.
|
|
|
|
assertProperBalance(t, alice, 1, 80)
|
|
|
|
assertProperBalance(t, bob, 1, 80)
|
|
|
|
|
|
|
|
// Execute every test, clearing possibly mutated
|
|
|
|
// wallet state after each step.
|
|
|
|
for _, walletTest := range walletTests {
|
2019-11-21 06:54:47 +03:00
|
|
|
|
|
|
|
walletTest := walletTest
|
|
|
|
|
2017-11-10 03:30:20 +03:00
|
|
|
testName := fmt.Sprintf("%v/%v:%v", walletType, backEnd,
|
|
|
|
walletTest.name)
|
|
|
|
success := t.Run(testName, func(t *testing.T) {
|
2019-11-21 06:54:47 +03:00
|
|
|
if backEnd == "neutrino" &&
|
|
|
|
strings.Contains(walletTest.name, "dual funder") {
|
|
|
|
t.Skip("skipping dual funder tests for neutrino")
|
|
|
|
}
|
|
|
|
|
2017-11-10 03:30:20 +03:00
|
|
|
walletTest.test(miningNode, alice, bob, t)
|
|
|
|
})
|
|
|
|
if !success {
|
2019-11-01 07:48:06 +03:00
|
|
|
return false
|
2017-11-10 03:30:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(roasbeef): possible reset mining
|
|
|
|
// node's chainstate to initial level, cleanly
|
|
|
|
// wipe buckets
|
|
|
|
if err := clearWalletStates(alice, bob); err !=
|
2020-01-10 05:46:17 +03:00
|
|
|
nil && err != kvdb.ErrBucketNotFound {
|
2017-11-10 03:30:20 +03:00
|
|
|
t.Fatalf("unable to wipe wallet state: %v", err)
|
2016-06-21 20:40:20 +03:00
|
|
|
}
|
2015-12-19 06:48:55 +03:00
|
|
|
}
|
2019-11-01 07:48:06 +03:00
|
|
|
|
|
|
|
return true
|
2015-12-16 22:22:36 +03:00
|
|
|
}
|