package lnwallet_test
import (
"bytes"
"crypto/sha256"
"encoding/hex"
"fmt"
"io/ioutil"
"math/rand"
"net"
"os"
"os/exec"
"path/filepath"
"reflect"
"runtime"
"strings"
"testing"
"time"
"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/btcjson"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/integration/rpctest"
"github.com/btcsuite/btcd/mempool"
"github.com/btcsuite/btcd/rpcclient"
"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"
"github.com/lightningnetwork/lnd/chainntnfs"
"github.com/lightningnetwork/lnd/chainntnfs/btcdnotify"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/channeldb/kvdb"
"github.com/lightningnetwork/lnd/input"
"github.com/lightningnetwork/lnd/keychain"
"github.com/lightningnetwork/lnd/lntest/wait"
"github.com/lightningnetwork/lnd/lnwallet"
"github.com/lightningnetwork/lnd/lnwallet/btcwallet"
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
"github.com/lightningnetwork/lnd/lnwallet/chanfunding"
"github.com/lightningnetwork/lnd/lnwire"
)
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,
}
testHdSeed = chainhash.Hash{
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,
}
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,
}
netParams = &chaincfg.RegressionNetParams
chainHash = netParams.GenesisHash
_, alicePub = btcec.PrivKeyFromBytes(btcec.S256(), testHdSeed[:])
_, bobPub = btcec.PrivKeyFromBytes(btcec.S256(), bobsPrivKey)
numReqConfs uint16 = 1
csvDelay uint16 = 4
bobAddr, _ = net.ResolveTCPAddr("tcp", "10.0.0.2:9000")
aliceAddr, _ = net.ResolveTCPAddr("tcp", "10.0.0.3:9000")
)
func assertProperBalance(t *testing.T, lw *lnwallet.LightningWallet,
numConfirms int32, amount float64) {
balance, err := lw.ConfirmedBalance(numConfirms)
if err != nil {
t.Fatalf("unable to query for balance: %v", err)
}
if balance.ToBTC() != amount {
t.Fatalf("wallet credits not properly loaded, should have 40BTC, "+
"instead have %v", balance)
}
}
func assertReservationDeleted(res *lnwallet.ChannelReservation, t *testing.T) {
if err := res.Cancel(); err == nil {
t.Fatalf("reservation wasn't deleted from wallet")
}
}
func mineAndAssertTxInBlock(t *testing.T, miner *rpctest.Harness,
txid chainhash.Hash) {
t.Helper()
if err := waitForMempoolTx(miner, &txid); err != nil {
t.Fatalf("unable to find %v in the mempool: %v", txid, err)
}
blockHashes, err := miner.Node.Generate(1)
if err != nil {
t.Fatalf("unable to generate new block: %v", err)
}
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)
}
}
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
}
func sendCoins(t *testing.T, miner *rpctest.Harness,
sender, receiver *lnwallet.LightningWallet, output *wire.TxOut,
feeRate chainfee.SatPerKWeight) *wire.MsgTx {
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
}
func assertTxInWallet(t *testing.T, w *lnwallet.LightningWallet,
txHash chainhash.Hash, confirmed bool) {
t.Helper()
if !confirmed && w.BackEnd() == "neutrino" {
return
}
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)
}
return
}
t.Fatalf("transaction %v not found", txHash)
}
func loadTestCredits(miner *rpctest.Harness, w *lnwallet.LightningWallet,
numOutputs int, btcPerOutput float64) error {
switch w.BackEnd() {
case "neutrino":
time.Sleep(time.Second)
}
satoshiPerOutput, err := btcutil.NewAmount(btcPerOutput)
if err != nil {
return fmt.Errorf("unable to create amt: %v", err)
}
expectedBalance, err := w.ConfirmedBalance(1)
if err != nil {
return err
}
expectedBalance += btcutil.Amount(int64(satoshiPerOutput) * int64(numOutputs))
addrs := make([]btcutil.Address, 0, numOutputs)
for i := 0; i < numOutputs; i++ {
walletAddr, err := w.NewAddress(lnwallet.WitnessPubKey, false)
if err != nil {
return err
}
script, err := txscript.PayToAddrScript(walletAddr)
if err != nil {
return err
}
addrs = append(addrs, walletAddr)
output := &wire.TxOut{
Value: int64(satoshiPerOutput),
PkScript: script,
}
if _, err := miner.SendOutputs([]*wire.TxOut{output}, 2500); err != nil {
return err
}
}
if _, err := miner.Node.Generate(10); err != nil {
return err
}
ticker := time.NewTicker(100 * time.Millisecond)
timeout := time.After(30 * time.Second)
for range ticker.C {
balance, err := w.ConfirmedBalance(1)
if err != nil {
return err
}
if balance == expectedBalance {
break
}
select {
case <-timeout:
synced, _, err := w.IsSynced()
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:
}
}
ticker.Stop()
return nil
}
func createTestWallet(tempTestDir string, miningNode *rpctest.Harness,
netParams *chaincfg.Params, notifier chainntnfs.ChainNotifier,
wc lnwallet.WalletController, keyRing keychain.SecretKeyRing,
signer input.Signer, bio lnwallet.BlockChainIO) (*lnwallet.LightningWallet, error) {
dbDir := filepath.Join(tempTestDir, "cdb")
cdb, err := channeldb.Open(dbDir)
if err != nil {
return nil, err
}
cfg := lnwallet.Config{
Database: cdb,
Notifier: notifier,
SecretKeyRing: keyRing,
WalletController: wc,
Signer: signer,
ChainIO: bio,
FeeEstimator: chainfee.NewStaticEstimator(2500, 0),
DefaultConstraints: channeldb.ChannelConstraints{
DustLimit: 500,
MaxPendingAmount: lnwire.NewMSatFromSatoshis(btcutil.SatoshiPerBitcoin) * 100,
ChanReserve: 100,
MinHTLC: 400,
MaxAcceptedHtlcs: 900,
},
NetParams: *netParams,
}
wallet, err := lnwallet.NewLightningWallet(cfg)
if err != nil {
return nil, err
}
if err := wallet.Startup(); err != nil {
return nil, err
}
if err := loadTestCredits(miningNode, wallet, 20, 4); err != nil {
return nil, err
}
return wallet, nil
}
func testDualFundingReservationWorkflow(miner *rpctest.Harness,
alice, bob *lnwallet.LightningWallet, t *testing.T) {
fundingAmount, err := btcutil.NewAmount(5)
if err != nil {
t.Fatalf("unable to create amt: %v", err)
}
feePerKw, err := alice.Cfg.FeeEstimator.EstimateFeePerKW(1)
if err != nil {
t.Fatalf("unable to query fee estimator: %v", err)
}
aliceReq := &lnwallet.InitFundingReserveMsg{
ChainHash: chainHash,
NodeID: bobPub,
NodeAddr: bobAddr,
LocalFundingAmt: fundingAmount,
RemoteFundingAmt: fundingAmount,
CommitFeePerKw: feePerKw,
FundingFeePerKw: feePerKw,
PushMSat: 0,
Flags: lnwire.FFAnnounceChannel,
}
aliceChanReservation, err := alice.InitChannelReservation(aliceReq)
if err != nil {
t.Fatalf("unable to initialize funding reservation: %v", err)
}
aliceChanReservation.SetNumConfsRequired(numReqConfs)
channelConstraints := &channeldb.ChannelConstraints{
DustLimit: lnwallet.DefaultDustLimit(),
ChanReserve: fundingAmount / 100,
MaxPendingAmount: lnwire.NewMSatFromSatoshis(fundingAmount),
MinHTLC: 1,
MaxAcceptedHtlcs: input.MaxHTLCNumber / 2,
CsvDelay: csvDelay,
}
err = aliceChanReservation.CommitConstraints(channelConstraints)
if err != nil {
t.Fatalf("unable to verify constraints: %v", err)
}
aliceContribution := aliceChanReservation.OurContribution()
if len(aliceContribution.Inputs) != 2 {
t.Fatalf("outputs for funding tx not properly selected, have %v "+
"outputs should have 2", len(aliceContribution.Inputs))
}
assertContributionInitPopulated(t, aliceContribution)
bobReq := &lnwallet.InitFundingReserveMsg{
ChainHash: chainHash,
NodeID: alicePub,
NodeAddr: aliceAddr,
LocalFundingAmt: fundingAmount,
RemoteFundingAmt: fundingAmount,
CommitFeePerKw: feePerKw,
FundingFeePerKw: feePerKw,
PushMSat: 0,
Flags: lnwire.FFAnnounceChannel,
}
bobChanReservation, err := bob.InitChannelReservation(bobReq)
if err != nil {
t.Fatalf("bob unable to init channel reservation: %v", err)
}
err = bobChanReservation.CommitConstraints(channelConstraints)
if err != nil {
t.Fatalf("unable to verify constraints: %v", err)
}
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)
}
assertContributionInitPopulated(t, bobChanReservation.TheirContribution())
bobContribution := bobChanReservation.OurContribution()
err = aliceChanReservation.ProcessContribution(bobContribution)
if err != nil {
t.Fatalf("alice unable to process bob's contribution: %v", err)
}
assertContributionInitPopulated(t, aliceChanReservation.TheirContribution())
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")
}
bobFundingSigs, bobCommitSig := bobChanReservation.OurSignatures()
if bobFundingSigs == nil {
t.Fatalf("bob's funding signatures not populated")
}
if bobCommitSig == nil {
t.Fatalf("bob's commit signatures not populated")
}
_, err = aliceChanReservation.CompleteReservation(
bobFundingSigs, bobCommitSig,
)
if err != nil {
for _, in := range aliceChanReservation.FinalFundingTx().TxIn {
fmt.Println(in.PreviousOutPoint.String())
}
t.Fatalf("unable to consume alice's sigs: %v", err)
}
_, err = bobChanReservation.CompleteReservation(
aliceFundingSigs, aliceCommitSig,
)
if err != nil {
t.Fatalf("unable to consume bob's sigs: %v", err)
}
fundingTx := aliceChanReservation.FinalFundingTx()
if fundingTx == nil {
t.Fatalf("funding transaction never created!")
}
fundingSha := fundingTx.TxHash()
aliceChannels, err := alice.Cfg.Database.FetchOpenChannels(bobPub)
if err != nil {
t.Fatalf("unable to retrieve channel from DB: %v", err)
}
if !bytes.Equal(aliceChannels[0].FundingOutpoint.Hash[:], fundingSha[:]) {
t.Fatalf("channel state not properly saved")
}
if !aliceChannels[0].ChanType.IsDualFunder() {
t.Fatalf("channel not detected as dual funder")
}
bobChannels, err := bob.Cfg.Database.FetchOpenChannels(alicePub)
if err != nil {
t.Fatalf("unable to retrieve channel from DB: %v", err)
}
if !bytes.Equal(bobChannels[0].FundingOutpoint.Hash[:], fundingSha[:]) {
t.Fatalf("channel state not properly saved")
}
if !bobChannels[0].ChanType.IsDualFunder() {
t.Fatalf("channel not detected as dual funder")
}
if err := alice.PublishTransaction(fundingTx); err != nil {
t.Fatalf("unable to publish funding tx: %v", err)
}
err = waitForMempoolTx(miner, &fundingSha)
if err != nil {
t.Fatalf("tx not relayed to miner: %v", err)
}
blockHashes, err := miner.Node.Generate(1)
if err != nil {
t.Fatalf("unable to generate block: %v", err)
}
block, err := miner.Node.GetBlock(blockHashes[0])
if err != nil {
t.Fatalf("unable to find block: %v", err)
}
if len(block.Transactions) != 2 {
t.Fatalf("funding transaction wasn't mined: %v", err)
}
blockTx := block.Transactions[1]
if blockTx.TxHash() != fundingSha {
t.Fatalf("incorrect transaction was mined")
}
assertReservationDeleted(aliceChanReservation, t)
assertReservationDeleted(bobChanReservation, t)
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)
}
}
func testFundingTransactionLockedOutputs(miner *rpctest.Harness,
alice, _ *lnwallet.LightningWallet, t *testing.T) {
fundingAmount, err := btcutil.NewAmount(8)
if err != nil {
t.Fatalf("unable to create amt: %v", err)
}
feePerKw, err := alice.Cfg.FeeEstimator.EstimateFeePerKW(1)
if err != nil {
t.Fatalf("unable to query fee estimator: %v", err)
}
req := &lnwallet.InitFundingReserveMsg{
ChainHash: chainHash,
NodeID: bobPub,
NodeAddr: bobAddr,
LocalFundingAmt: fundingAmount,
RemoteFundingAmt: 0,
CommitFeePerKw: feePerKw,
FundingFeePerKw: feePerKw,
PushMSat: 0,
Flags: lnwire.FFAnnounceChannel,
PendingChanID: [32]byte{0, 1, 2, 3},
}
if _, err := alice.InitChannelReservation(req); err != nil {
t.Fatalf("unable to initialize funding reservation 1: %v", err)
}
amt, err := btcutil.NewAmount(900)
if err != nil {
t.Fatalf("unable to create amt: %v", err)
}
failedReq := &lnwallet.InitFundingReserveMsg{
ChainHash: chainHash,
NodeID: bobPub,
NodeAddr: bobAddr,
LocalFundingAmt: amt,
RemoteFundingAmt: 0,
CommitFeePerKw: feePerKw,
FundingFeePerKw: feePerKw,
PushMSat: 0,
Flags: lnwire.FFAnnounceChannel,
PendingChanID: [32]byte{1, 2, 3, 4},
}
failedReservation, err := alice.InitChannelReservation(failedReq)
if err == nil {
t.Fatalf("not error returned, should fail on coin selection")
}
if _, ok := err.(*chanfunding.ErrInsufficientFunds); !ok {
t.Fatalf("error not coinselect error: %v", err)
}
if failedReservation != nil {
t.Fatalf("reservation should be nil")
}
}
func testFundingCancellationNotEnoughFunds(miner *rpctest.Harness,
alice, _ *lnwallet.LightningWallet, t *testing.T) {
feePerKw, err := alice.Cfg.FeeEstimator.EstimateFeePerKW(1)
if err != nil {
t.Fatalf("unable to query fee estimator: %v", err)
}
fundingAmount, err := btcutil.NewAmount(44)
if err != nil {
t.Fatalf("unable to create amt: %v", err)
}
req := &lnwallet.InitFundingReserveMsg{
ChainHash: chainHash,
NodeID: bobPub,
NodeAddr: bobAddr,
LocalFundingAmt: fundingAmount,
RemoteFundingAmt: 0,
CommitFeePerKw: feePerKw,
FundingFeePerKw: feePerKw,
PushMSat: 0,
Flags: lnwire.FFAnnounceChannel,
PendingChanID: [32]byte{2, 3, 4, 5},
}
chanReservation, err := alice.InitChannelReservation(req)
if err != nil {
t.Fatalf("unable to initialize funding reservation: %v", err)
}
req.PendingChanID = [32]byte{3, 4, 5, 6}
_, err = alice.InitChannelReservation(req)
if _, ok := err.(*chanfunding.ErrInsufficientFunds); !ok {
t.Fatalf("coin selection succeeded should have insufficient funds: %v",
err)
}
if err := chanReservation.Cancel(); err != nil {
t.Fatalf("unable to cancel reservation: %v", err)
}
lockedOutPoints := alice.LockedOutpoints()
if len(lockedOutPoints) != 0 {
t.Fatalf("outpoints still locked")
}
numReservations := alice.ActiveReservations()
if len(alice.ActiveReservations()) != 0 {
t.Fatalf("should have 0 reservations, instead have %v",
numReservations)
}
req.PendingChanID = [32]byte{4, 5, 6, 7, 8}
if _, err := alice.InitChannelReservation(req); err != nil {
t.Fatalf("unable to initialize funding reservation: %v", err)
}
}
func testCancelNonExistentReservation(miner *rpctest.Harness,
alice, _ *lnwallet.LightningWallet, t *testing.T) {
feePerKw, err := alice.Cfg.FeeEstimator.EstimateFeePerKW(1)
if err != nil {
t.Fatalf("unable to query fee estimator: %v", err)
}
res, err := lnwallet.NewChannelReservation(
10000, 10000, feePerKw, alice, 22, 10, &testHdSeed,
lnwire.FFAnnounceChannel, lnwallet.CommitmentTypeTweakless,
nil, [32]byte{}, 0,
)
if err != nil {
t.Fatalf("unable to create res: %v", err)
}
if err := res.Cancel(); err == nil {
t.Fatalf("canceled non-existent reservation")
}
}
func testReservationInitiatorBalanceBelowDustCancel(miner *rpctest.Harness,
alice, _ *lnwallet.LightningWallet, t *testing.T) {
const numBTC = 4
fundingAmount, err := btcutil.NewAmount(numBTC)
if err != nil {
t.Fatalf("unable to create amt: %v", err)
}
feePerKw := chainfee.SatPerKWeight(
numBTC * numBTC * btcutil.SatoshiPerBitcoin,
)
req := &lnwallet.InitFundingReserveMsg{
ChainHash: chainHash,
NodeID: bobPub,
NodeAddr: bobAddr,
LocalFundingAmt: fundingAmount,
RemoteFundingAmt: 0,
CommitFeePerKw: feePerKw,
FundingFeePerKw: 1000,
PushMSat: 0,
Flags: lnwire.FFAnnounceChannel,
CommitType: lnwallet.CommitmentTypeTweakless,
}
_, err = alice.InitChannelReservation(req)
switch {
case err == nil:
t.Fatalf("initialization should have failed due to " +
"insufficient local amount")
case !strings.Contains(err.Error(), "funder balance too small"):
t.Fatalf("incorrect error: %v", err)
}
}
func assertContributionInitPopulated(t *testing.T, c *lnwallet.ChannelContribution) {
_, _, line, _ := runtime.Caller(1)
if c.FirstCommitmentPoint == nil {
t.Fatalf("line #%v: commitment point not fond", line)
}
if c.CsvDelay == 0 {
t.Fatalf("line #%v: csv delay not set", line)
}
if c.MultiSigKey.PubKey == nil {
t.Fatalf("line #%v: multi-sig key not set", line)
}
if c.RevocationBasePoint.PubKey == nil {
t.Fatalf("line #%v: revocation key not set", line)
}
if c.PaymentBasePoint.PubKey == nil {
t.Fatalf("line #%v: payment key not set", line)
}
if c.DelayBasePoint.PubKey == nil {
t.Fatalf("line #%v: delay key not set", line)
}
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,
alice, bob *lnwallet.LightningWallet, t *testing.T,
commitType lnwallet.CommitmentType,
aliceChanFunder chanfunding.Assembler, fetchFundingTx func() *wire.MsgTx,
pendingChanID [32]byte, thawHeight uint32) {
fundingAmt, err := btcutil.NewAmount(4)
if err != nil {
t.Fatalf("unable to create amt: %v", err)
}
pushAmt := lnwire.NewMSatFromSatoshis(btcutil.SatoshiPerBitcoin)
feePerKw, err := alice.Cfg.FeeEstimator.EstimateFeePerKW(1)
if err != nil {
t.Fatalf("unable to query fee estimator: %v", err)
}
aliceReq := &lnwallet.InitFundingReserveMsg{
ChainHash: chainHash,
PendingChanID: pendingChanID,
NodeID: bobPub,
NodeAddr: bobAddr,
LocalFundingAmt: fundingAmt,
RemoteFundingAmt: 0,
CommitFeePerKw: feePerKw,
FundingFeePerKw: feePerKw,
PushMSat: pushAmt,
Flags: lnwire.FFAnnounceChannel,
CommitType: commitType,
ChanFunder: aliceChanFunder,
}
aliceChanReservation, err := alice.InitChannelReservation(aliceReq)
if err != nil {
t.Fatalf("unable to init channel reservation: %v", err)
}
aliceChanReservation.SetNumConfsRequired(numReqConfs)
channelConstraints := &channeldb.ChannelConstraints{
DustLimit: lnwallet.DefaultDustLimit(),
ChanReserve: fundingAmt / 100,
MaxPendingAmount: lnwire.NewMSatFromSatoshis(fundingAmt),
MinHTLC: 1,
MaxAcceptedHtlcs: input.MaxHTLCNumber / 2,
CsvDelay: csvDelay,
}
err = aliceChanReservation.CommitConstraints(channelConstraints)
if err != nil {
t.Fatalf("unable to verify constraints: %v", err)
}
aliceContribution := aliceChanReservation.OurContribution()
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))
}
}
assertContributionInitPopulated(t, aliceContribution)
bobReq := &lnwallet.InitFundingReserveMsg{
ChainHash: chainHash,
PendingChanID: pendingChanID,
NodeID: alicePub,
NodeAddr: aliceAddr,
LocalFundingAmt: 0,
RemoteFundingAmt: fundingAmt,
CommitFeePerKw: feePerKw,
FundingFeePerKw: feePerKw,
PushMSat: pushAmt,
Flags: lnwire.FFAnnounceChannel,
CommitType: commitType,
}
bobChanReservation, err := bob.InitChannelReservation(bobReq)
if err != nil {
t.Fatalf("unable to create bob reservation: %v", err)
}
err = bobChanReservation.CommitConstraints(channelConstraints)
if err != nil {
t.Fatalf("unable to verify constraints: %v", err)
}
bobChanReservation.SetNumConfsRequired(numReqConfs)
bobContribution := bobChanReservation.OurContribution()
assertContributionInitPopulated(t, bobContribution)
err = bobChanReservation.ProcessSingleContribution(aliceContribution)
if err != nil {
t.Fatalf("bob unable to process alice's contribution: %v", err)
}
assertContributionInitPopulated(t, bobChanReservation.TheirContribution())
err = aliceChanReservation.ProcessContribution(bobContribution)
if err != nil {
t.Fatalf("alice unable to process bob's contribution")
}
assertContributionInitPopulated(t, bobChanReservation.TheirContribution())
aliceRemoteContribution := aliceChanReservation.TheirContribution()
aliceFundingSigs, aliceCommitSig := aliceChanReservation.OurSignatures()
if fetchFundingTx == nil && aliceFundingSigs == nil {
t.Fatalf("funding sigs not found")
}
if aliceCommitSig == nil {
t.Fatalf("commitment sig not found")
}
if aliceChanReservation.FinalFundingTx() == nil && fetchFundingTx == nil {
t.Fatalf("funding transaction never created!")
}
if aliceChanReservation.FundingOutpoint() == nil {
t.Fatalf("funding outpoint never created!")
}
if len(aliceRemoteContribution.Inputs) != 0 {
t.Fatalf("bob shouldn't have any inputs, instead has %v",
len(aliceRemoteContribution.Inputs))
}
if len(aliceRemoteContribution.ChangeOutputs) != 0 {
t.Fatalf("bob shouldn't have any change outputs, instead "+
"has %v",
aliceRemoteContribution.ChangeOutputs[0].Value)
}
fundingPoint := aliceChanReservation.FundingOutpoint()
_, err = bobChanReservation.CompleteReservationSingle(
fundingPoint, aliceCommitSig,
)
if err != nil {
t.Fatalf("bob unable to consume single reservation: %v", err)
}
_, 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)
}
var fundingTx *wire.MsgTx
if fetchFundingTx != nil {
fundingTx = fetchFundingTx()
} else {
fundingTx = aliceChanReservation.FinalFundingTx()
}
fundingSha := fundingTx.TxHash()
aliceChannels, err := alice.Cfg.Database.FetchOpenChannels(bobPub)
if err != nil {
t.Fatalf("unable to retrieve channel from DB: %v", err)
}
if len(aliceChannels) != 1 {
t.Fatalf("alice didn't save channel state: %v", err)
}
if !bytes.Equal(aliceChannels[0].FundingOutpoint.Hash[:], fundingSha[:]) {
t.Fatalf("channel state not properly saved: %v vs %v",
hex.EncodeToString(aliceChannels[0].FundingOutpoint.Hash[:]),
hex.EncodeToString(fundingSha[:]))
}
if !aliceChannels[0].IsInitiator {
t.Fatalf("alice not detected as channel initiator")
}
if !aliceChannels[0].ChanType.IsSingleFunder() {
t.Fatalf("channel type is incorrect, expected %v instead got %v",
channeldb.SingleFunderBit, aliceChannels[0].ChanType)
}
bobChannels, err := bob.Cfg.Database.FetchOpenChannels(alicePub)
if err != nil {
t.Fatalf("unable to retrieve channel from DB: %v", err)
}
if len(bobChannels) != 1 {
t.Fatalf("bob didn't save channel state: %v", err)
}
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[:]))
}
if bobChannels[0].IsInitiator {
t.Fatalf("bob not detected as channel responder")
}
if !bobChannels[0].ChanType.IsSingleFunder() {
t.Fatalf("channel type is incorrect, expected %v instead got %v",
channeldb.SingleFunderBit, bobChannels[0].ChanType)
}
if err := alice.PublishTransaction(fundingTx); err != nil {
t.Fatalf("unable to publish funding tx: %v", err)
}
err = waitForMempoolTx(miner, &fundingSha)
if err != nil {
t.Fatalf("tx not relayed to miner: %v", err)
}
blockHashes, err := miner.Node.Generate(1)
if err != nil {
t.Fatalf("unable to generate block: %v", err)
}
block, err := miner.Node.GetBlock(blockHashes[0])
if err != nil {
t.Fatalf("unable to find block: %v", err)
}
if len(block.Transactions) != 2 {
t.Fatalf("funding transaction wasn't mined: %d",
len(block.Transactions))
}
blockTx := block.Transactions[1]
if blockTx.TxHash() != fundingSha {
t.Fatalf("incorrect transaction was mined")
}
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)
}
assertReservationDeleted(aliceChanReservation, t)
assertReservationDeleted(bobChanReservation, t)
}
func testListTransactionDetails(miner *rpctest.Harness,
alice, _ *lnwallet.LightningWallet, t *testing.T) {
const numTxns = 5
const outputAmt = btcutil.SatoshiPerBitcoin
txids := make(map[chainhash.Hash]struct{})
for i := 0; i < numTxns; i++ {
addr, err := alice.NewAddress(lnwallet.WitnessPubKey, false)
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)
}
output := &wire.TxOut{
Value: outputAmt,
PkScript: script,
}
txid, err := miner.SendOutputs([]*wire.TxOut{output}, 2500)
if err != nil {
t.Fatalf("unable to send coinbase: %v", err)
}
txids[*txid] = struct{}{}
}
const numBlocksMined = 10
blocks, err := miner.Node.Generate(numBlocksMined)
if err != nil {
t.Fatalf("unable to mine blocks: %v", err)
}
err = waitForWalletSync(miner, alice)
if err != nil {
t.Fatalf("Couldn't sync Alice's wallet: %v", err)
}
txDetails, err := alice.ListTransactionDetails()
if err != nil {
t.Fatalf("unable to fetch tx details: %v", err)
}
blockTxOuts := make(map[chainhash.Hash]map[chainhash.Hash][]*wire.TxOut)
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)
}
if !bytes.Equal(txDetail.BlockHash[:], blocks[0][:]) {
t.Fatalf("block hash mismatch, got %v expected %v",
txDetail.BlockHash, blocks[0])
}
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)
}
}
delete(txids, txDetail.Hash)
}
if len(txids) != 0 {
t.Fatalf("all transactions not found in details: left=%v, "+
"returned_set=%v", spew.Sdump(txids),
spew.Sdump(txDetails))
}
minerAddr, err := miner.NewAddress()
if err != nil {
t.Fatalf("unable to generate address: %v", err)
}
outputScript, err := txscript.PayToAddrScript(minerAddr)
if err != nil {
t.Fatalf("unable to make output script: %v", err)
}
burnOutput := wire.NewTxOut(outputAmt, outputScript)
burnTX, err := alice.SendOutputs([]*wire.TxOut{burnOutput}, 2500)
if err != nil {
t.Fatalf("unable to create burn tx: %v", err)
}
burnTXID := burnTX.TxHash()
err = waitForMempoolTx(miner, &burnTXID)
if err != nil {
t.Fatalf("tx not relayed to miner: %v", err)
}
err = waitForWalletSync(miner, alice)
if err != nil {
t.Fatalf("Couldn't sync Alice's wallet: %v", err)
}
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
}
mempoolTxFound = true
if txDetail.NumConfirmations != 0 {
t.Fatalf("num confs incorrect, got %v expected %v",
txDetail.NumConfirmations, 0)
}
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)
}
}
if !mempoolTxFound {
t.Fatalf("unable to find mempool tx in tx details!")
}
burnBlock, err := miner.Node.Generate(1)
if err != nil {
t.Fatalf("unable to mine block: %v", err)
}
err = waitForWalletSync(miner, alice)
if err != nil {
t.Fatalf("Couldn't sync Alice's wallet: %v", err)
}
txDetails, err = alice.ListTransactionDetails()
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)
}
if txDetail.Value >= -outputAmt {
fmt.Println(spew.Sdump(txDetail))
t.Fatalf("tx value incorrect, got %v expected %v",
int64(txDetail.Value), -int64(outputAmt))
}
if !bytes.Equal(txDetail.BlockHash[:], burnBlock[0][:]) {
t.Fatalf("block hash mismatch, got %v expected %v",
txDetail.BlockHash, burnBlock[0])
}
}
if !burnTxFound {
t.Fatal("tx burning btc not found")
}
}
func testTransactionSubscriptions(miner *rpctest.Harness,
alice, _ *lnwallet.LightningWallet, t *testing.T) {
txClient, err := alice.SubscribeTransactions()
if err != nil {
t.Skipf("unable to generate tx subscription: %v", err)
}
defer txClient.Cancel()
const (
outputAmt = btcutil.SatoshiPerBitcoin
numTxns = 3
)
errCh1 := make(chan error, 1)
switch alice.BackEnd() {
case "neutrino":
default:
go func() {
for i := 0; i < numTxns; i++ {
txDetail := <-txClient.UnconfirmedTransactions()
if txDetail.NumConfirmations != 0 {
errCh1 <- fmt.Errorf("incorrect number of confs, "+
"expected %v got %v", 0,
txDetail.NumConfirmations)
return
}
if txDetail.Value != outputAmt {
errCh1 <- fmt.Errorf("incorrect output amt, "+
"expected %v got %v", outputAmt,
txDetail.Value)
return
}
if txDetail.BlockHash != nil {
errCh1 <- fmt.Errorf("block hash should be nil, "+
"is instead %v",
txDetail.BlockHash)
return
}
}
errCh1 <- nil
}()
}
for i := 0; i < numTxns; i++ {
addr, err := alice.NewAddress(lnwallet.WitnessPubKey, false)
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)
}
output := &wire.TxOut{
Value: outputAmt,
PkScript: script,
}
txid, err := miner.SendOutputs([]*wire.TxOut{output}, 2500)
if err != nil {
t.Fatalf("unable to send coinbase: %v", err)
}
err = waitForMempoolTx(miner, txid)
if err != nil {
t.Fatalf("tx not relayed to miner: %v", err)
}
}
switch alice.BackEnd() {
case "neutrino":
default:
select {
case <-time.After(time.Second * 10):
t.Fatalf("transactions not received after 10 seconds")
case err := <-errCh1:
if err != nil {
t.Fatal(err)
}
}
}
errCh2 := make(chan error, 1)
go func() {
for i := 0; i < numTxns; i++ {
txDetail := <-txClient.ConfirmedTransactions()
if txDetail.NumConfirmations != 1 {
errCh2 <- fmt.Errorf("incorrect number of confs for %s, expected %v got %v",
txDetail.Hash, 1, txDetail.NumConfirmations)
return
}
if txDetail.Value != outputAmt {
errCh2 <- fmt.Errorf("incorrect output amt, expected %v got %v in txid %s",
outputAmt, txDetail.Value, txDetail.Hash)
return
}
}
errCh2 <- nil
}()
if _, err := miner.Node.Generate(1); err != nil {
t.Fatalf("unable to generate block: %v", err)
}
select {
case <-time.After(time.Second * 5):
t.Fatalf("transactions not received after 5 seconds")
case err := <-errCh2:
if err != nil {
t.Fatal(err)
}
}
b := txscript.NewScriptBuilder()
b.AddOp(txscript.OP_RETURN)
outputScript, err := b.Script()
if err != nil {
t.Fatalf("unable to make output script: %v", err)
}
burnOutput := wire.NewTxOut(outputAmt, outputScript)
tx, err := alice.SendOutputs([]*wire.TxOut{burnOutput}, 2500)
if err != nil {
t.Fatalf("unable to create burn tx: %v", err)
}
txid := tx.TxHash()
err = waitForMempoolTx(miner, &txid)
if err != nil {
t.Fatalf("tx not relayed to miner: %v", err)
}
err = waitForWalletSync(miner, alice)
if err != nil {
t.Fatalf("Couldn't sync Alice's wallet: %v", err)
}
select {
case <-time.After(time.Second * 10):
t.Fatalf("transactions not received after 10 seconds")
case unConfTx := <-txClient.UnconfirmedTransactions():
if unConfTx.Hash != txid {
t.Fatalf("wrong txn notified: expected %v got %v",
txid, unConfTx.Hash)
}
}
}
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)
}
return keyScript, nil
}
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)
}
blockHashes, err := r.Node.Generate(1)
if err != nil {
return fmt.Errorf("unable to generate block: %v", err)
}
block, err := r.Node.GetBlock(blockHashes[0])
if err != nil {
return fmt.Errorf("unable to find block: %v", err)
}
if len(block.Transactions) != 2 {
return fmt.Errorf("expected 2 txs in block, got %d",
len(block.Transactions))
}
blockTx := block.Transactions[1]
if blockTx.TxHash() != tx.TxHash() {
return fmt.Errorf("incorrect transaction was mined")
}
time.Sleep(1 * time.Second)
return nil
}
func txFromOutput(tx *wire.MsgTx, signer input.Signer, fromPubKey,
payToPubKey *btcec.PublicKey, txFee btcutil.Amount,
rbf bool) (*wire.MsgTx, error) {
keyScript, err := scriptFromKey(fromPubKey)
if err != nil {
return nil, fmt.Errorf("unable to generate script: %v", err)
}
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
tx1 := wire.NewMsgTx(2)
sequence := wire.MaxTxInSequenceNum
if rbf {
sequence = mempool.MaxRBFSequence
}
tx1.AddTxIn(&wire.TxIn{
PreviousOutPoint: wire.OutPoint{
Hash: tx.TxHash(),
Index: outputIndex,
},
Sequence: sequence,
})
payToScript, err := scriptFromKey(payToPubKey)
if err != nil {
return nil, fmt.Errorf("unable to generate script: %v", err)
}
tx1.AddTxOut(&wire.TxOut{
Value: outputValue - int64(txFee),
PkScript: payToScript,
})
signDesc := &input.SignDescriptor{
KeyDesc: keychain.KeyDescriptor{
PubKey: fromPubKey,
},
WitnessScript: keyScript,
Output: tx.TxOut[outputIndex],
HashType: txscript.SigHashAll,
SigHashes: txscript.NewTxSigHashes(tx1),
InputIndex: 0, }
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.Serialize(), byte(txscript.SigHashAll))
witness[1] = fromPubKey.SerializeCompressed()
tx1.TxIn[0].Witness = witness
vm, err := txscript.NewEngine(
keyScript, tx1, 0, txscript.StandardVerifyFlags, nil,
nil, outputValue,
)
if err != nil {
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)
}
return tx1, nil
}
func newTx(t *testing.T, r *rpctest.Harness, pubKey *btcec.PublicKey,
alice *lnwallet.LightningWallet, rbf bool) *wire.MsgTx {
t.Helper()
keyScript, err := scriptFromKey(pubKey)
if err != nil {
t.Fatalf("unable to generate script: %v", err)
}
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)
}
if err := mineAndAssert(r, tx); err != nil {
t.Fatalf("unable to mine tx: %v", err)
}
txFee := btcutil.Amount(0.001 * btcutil.SatoshiPerBitcoin)
tx1, err := txFromOutput(
tx, alice.Cfg.Signer, pubKey, pubKey, txFee, rbf,
)
if err != nil {
t.Fatal(err)
}
return tx1
}
func testPublishTransaction(r *rpctest.Harness,
alice, _ *lnwallet.LightningWallet, t *testing.T) {
keyDesc, err := alice.DeriveNextKey(keychain.KeyFamilyMultiSig)
if err != nil {
t.Fatalf("unable to obtain public key: %v", err)
}
tx1 := newTx(t, r, keyDesc.PubKey, alice, false)
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)
}
if err := alice.PublishTransaction(tx1); err != nil {
t.Fatalf("unable to publish: %v", err)
}
if _, err := r.Node.Generate(1); err != nil {
t.Fatalf("unable to generate block: %v", err)
}
tx2 := newTx(t, r, keyDesc.PubKey, alice, false)
if err := alice.PublishTransaction(tx2); err != nil {
t.Fatalf("unable to publish: %v", err)
}
if err := mineAndAssert(r, tx2); err != nil {
t.Fatalf("unable to mine tx: %v", err)
}
if err := alice.PublishTransaction(tx2); err != nil {
t.Fatalf("unable to publish: %v", err)
}
var (
txFee = btcutil.Amount(0.005 * btcutil.SatoshiPerBitcoin)
tx3, tx3Spend *wire.MsgTx
)
for _, rbf := range []bool{false, true} {
tx3 = newTx(t, r, keyDesc.PubKey, alice, false)
if err := alice.PublishTransaction(tx3); err != nil {
t.Fatalf("unable to publish: %v", err)
}
if err := mineAndAssert(r, tx3); err != nil {
t.Fatalf("unable to mine tx: %v", err)
}
tx4, err := txFromOutput(
tx3, alice.Cfg.Signer, keyDesc.PubKey,
keyDesc.PubKey, txFee, rbf,
)
if err != nil {
t.Fatal(err)
}
if err := alice.PublishTransaction(tx4); err != nil {
t.Fatalf("unable to publish: %v", err)
}
tx3Spend = tx4
txid4 := tx4.TxHash()
err = waitForMempoolTx(r, &txid4)
if err != nil {
t.Fatalf("tx not relayed to miner: %v", err)
}
keyDesc2, err := alice.DeriveNextKey(
keychain.KeyFamilyMultiSig,
)
if err != nil {
t.Fatalf("unable to obtain public key: %v", err)
}
tx5, err := txFromOutput(
tx3, alice.Cfg.Signer, keyDesc.PubKey,
keyDesc2.PubKey, txFee, rbf,
)
if err != nil {
t.Fatal(err)
}
err = alice.PublishTransaction(tx5)
if err != lnwallet.ErrDoubleSpend {
t.Fatalf("expected ErrDoubleSpend, got: %v", err)
}
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)
}
expErr := lnwallet.ErrDoubleSpend
if rbf {
expErr = nil
tx3Spend = tx6
}
err = alice.PublishTransaction(tx6)
if err != expErr {
t.Fatalf("expected ErrDoubleSpend, got: %v", err)
}
if err := mineAndAssert(r, tx3Spend); err != nil {
t.Fatalf("unable to mine tx: %v", err)
}
}
if alice.BackEnd() != "neutrino" {
pubKey4, err := alice.DeriveNextKey(
keychain.KeyFamilyMultiSig,
)
if err != nil {
t.Fatalf("unable to obtain public key: %v", err)
}
tx7, err := txFromOutput(
tx3, alice.Cfg.Signer, keyDesc.PubKey,
pubKey4.PubKey, txFee, false,
)
if err != nil {
t.Fatal(err)
}
err = alice.PublishTransaction(tx7)
if err != lnwallet.ErrDoubleSpend {
t.Fatalf("expected ErrDoubleSpend, got: %v", err)
}
}
}
func testSignOutputUsingTweaks(r *rpctest.Harness,
alice, _ *lnwallet.LightningWallet, t *testing.T) {
pubKey, err := alice.DeriveNextKey(
keychain.KeyFamilyMultiSig,
)
if err != nil {
t.Fatalf("unable to obtain public key: %v", err)
}
commitPreimage := bytes.Repeat([]byte{2}, 32)
commitSecret, commitPoint := btcec.PrivKeyFromBytes(btcec.S256(),
commitPreimage)
revocationKey := input.DeriveRevocationPubkey(pubKey.PubKey, commitPoint)
commitTweak := input.SingleTweakBytes(commitPoint, pubKey.PubKey)
tweakedPub := input.TweakPubKey(pubKey.PubKey, commitPoint)
baseKey := pubKey
for i := 0; i < 2; i++ {
var tweakedKey *btcec.PublicKey
if i == 0 {
tweakedKey = tweakedPub
} else {
tweakedKey = revocationKey
}
pubkeyHash := btcutil.Hash160(tweakedKey.SerializeCompressed())
keyAddr, err := btcutil.NewAddressWitnessPubKeyHash(pubkeyHash,
&chaincfg.RegressionNetParams)
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)
}
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)
}
txid := tx.TxHash()
err = waitForMempoolTx(r, &txid)
if err != nil {
t.Fatalf("tx not relayed to miner: %v", err)
}
var outputIndex uint32
if bytes.Equal(tx.TxOut[0].PkScript, keyScript) {
outputIndex = 0
} else {
outputIndex = 1
}
sweepTx := wire.NewMsgTx(2)
sweepTx.AddTxIn(&wire.TxIn{
PreviousOutPoint: wire.OutPoint{
Hash: txid,
Index: outputIndex,
},
})
sweepTx.AddTxOut(&wire.TxOut{
Value: 1000,
PkScript: keyScript,
})
signDesc := &input.SignDescriptor{
KeyDesc: keychain.KeyDescriptor{
PubKey: baseKey.PubKey,
},
WitnessScript: keyScript,
Output: newOutput,
HashType: txscript.SigHashAll,
SigHashes: txscript.NewTxSigHashes(sweepTx),
InputIndex: 0,
}
if i == 0 {
signDesc.SingleTweak = commitTweak
} else {
signDesc.DoubleTweak = commitSecret
}
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.Serialize(), byte(txscript.SigHashAll))
witness[1] = tweakedKey.SerializeCompressed()
sweepTx.TxIn[0].Witness = witness
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)
}
}
}
func testReorgWalletBalance(r *rpctest.Harness, w *lnwallet.LightningWallet,
_ *lnwallet.LightningWallet, t *testing.T) {
_, err := r.Node.Generate(5)
if err != nil {
t.Fatalf("unable to generate blocks on passed node: %v", err)
}
err = waitForWalletSync(r, w)
if err != nil {
t.Fatalf("unable to sync wallet: %v", err)
}
err = loadTestCredits(r, w, 20, 4)
if err != nil {
t.Fatalf("unable to send money to lnwallet: %v", err)
}
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,
}
tx, err := w.SendOutputs([]*wire.TxOut{output}, 2500)
if err != nil {
t.Fatalf("unable to send outputs: %v", err)
}
txid := tx.TxHash()
err = waitForMempoolTx(r, &txid)
if err != nil {
t.Fatalf("tx not relayed to miner: %v", err)
}
_, err = r.Node.Generate(50)
if err != nil {
t.Fatalf("unable to generate blocks on passed node: %v", err)
}
err = waitForWalletSync(r, w)
if err != nil {
t.Fatalf("unable to sync wallet: %v", err)
}
origBalance, err := w.ConfirmedBalance(1)
if err != nil {
t.Fatalf("unable to query for balance: %v", err)
}
r2, err := rpctest.New(r.ActiveNet, nil, []string{"--txindex"})
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()
newBalance, err := w.ConfirmedBalance(1)
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)
}
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)
}
for i := 0; i < 5; i++ {
timeout := time.After(30 * time.Second)
stillConnected := true
var peers []btcjson.GetPeerInfoResult
for stillConnected {
time.Sleep(100 * time.Millisecond)
select {
case <-timeout:
t.Fatalf("timeout waiting for miner disconnect")
default:
}
err = r2.Node.AddNode(r.P2PAddress(), rpcclient.ANRemove)
if err != nil {
t.Fatalf("unable to disconnect mining nodes: %v",
err)
}
peers, err = r2.Node.GetPeerInfo()
if err != nil {
t.Fatalf("unable to get peer info: %v", err)
}
stillConnected = false
for _, peer := range peers {
if peer.Addr == r.P2PAddress() {
stillConnected = true
break
}
}
}
_, 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)
}
err = r2.Node.AddNode(r.P2PAddress(), rpcclient.ANAdd)
if err != nil {
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)
}
}
err = rpctest.JoinNodes([]*rpctest.Harness{r2, r},
rpctest.Blocks)
if err != nil {
t.Fatalf("unable to synchronize mining nodes: %v", err)
}
err = waitForWalletSync(r, w)
if err != nil {
t.Fatalf("unable to sync wallet: %v", err)
}
}
newBalance, err = w.ConfirmedBalance(1)
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)
}
}
func testChangeOutputSpendConfirmation(r *rpctest.Harness,
alice, bob *lnwallet.LightningWallet, t *testing.T) {
aliceBalance, err := alice.ConfirmedBalance(0)
if err != nil {
t.Fatalf("unable to retrieve alice's balance: %v", err)
}
bobPkScript := newPkScript(t, bob, lnwallet.WitnessPubKey)
txFeeRate := chainfee.SatPerKWeight(2500)
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)
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)
}
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)
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)
tx = sendCoins(t, r, alice, bob, output, txFeeRate)
txHash = tx.TxHash()
assertTxInWallet(t, alice, txHash, true)
assertTxInWallet(t, bob, txHash, true)
if err := loadTestCredits(r, alice, 20, 4); err != nil {
t.Fatalf("unable to replenish alice's wallet: %v", err)
}
}
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)
}
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 addr1.String() != addr2.String() {
t.Fatalf("addresses changed w/o use: %v vs %v", addr1, addr2)
}
addrScript, err := txscript.PayToAddrScript(addr1)
if err != nil {
t.Fatalf("unable to convert addr to script: %v", err)
}
feeRate := chainfee.SatPerKWeight(2500)
output := &wire.TxOut{
Value: 1000000,
PkScript: addrScript,
}
sendCoins(t, miner, bob, alice, output, feeRate)
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")
}
}
}
func testCreateSimpleTx(r *rpctest.Harness, w *lnwallet.LightningWallet,
_ *lnwallet.LightningWallet, t *testing.T) {
err := loadTestCredits(r, w, 20, 4)
if err != nil {
t.Fatalf("unable to send money to lnwallet: %v", err)
}
testCases := []struct {
outVals []int64
feeRate chainfee.SatPerKWeight
valid bool
}{
{
outVals: []int64{},
feeRate: 2500,
valid: false, },
{
outVals: []int64{200},
feeRate: 2500,
valid: false, },
{
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,
},
}
for i, test := range testCases {
feeRate := test.feeRate
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
}
createTx, createErr := w.CreateSimpleTx(
outputs, feeRate, true,
)
switch {
case test.valid && createErr != nil:
fmt.Println(spew.Sdump(createTx.Tx))
t.Fatalf("got unexpected error when creating tx: %v",
createErr)
case !test.valid && createErr == nil:
t.Fatalf("test #%v should have failed on tx "+
"creation", i)
}
tx, sendErr := w.SendOutputs(outputs, feeRate)
switch {
case test.valid && sendErr != nil:
t.Fatalf("got unexpected error when sending tx: %v",
sendErr)
case !test.valid && sendErr == nil:
t.Fatalf("test #%v should fail for tx sending", i)
}
if createErr != sendErr {
t.Fatalf("error creating tx (%v) different "+
"from error sending outputs (%v)",
createErr, sendErr)
}
if !test.valid {
continue
}
txid := tx.TxHash()
err = waitForMempoolTx(r, &txid)
if err != nil {
t.Fatalf("tx not relayed to miner: %v", err)
}
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))
}
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))
}
}
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
}
if err := assertSimilarTx(createTx.Tx, tx); err != nil {
t.Fatalf("transactions not similar: %v", err)
}
}
}
func testSignOutputCreateAccount(r *rpctest.Harness, w *lnwallet.LightningWallet,
_ *lnwallet.LightningWallet, t *testing.T) {
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,
}
_, err := w.Cfg.Signer.SignOutputRaw(fakeTx, signDesc)
if err != nil {
t.Fatalf("unable to sign for output with non-existent "+
"account: %v", err)
}
}
type walletTestCase struct {
name string
test func(miner *rpctest.Harness, alice, bob *lnwallet.LightningWallet,
test *testing.T)
}
var walletTests = []walletTestCase{
{
name: "change output spend confirmation",
test: testChangeOutputSpendConfirmation,
},
{
name: "insane fee reject",
test: testReservationInitiatorBalanceBelowDustCancel,
},
{
name: "single funding workflow",
test: func(miner *rpctest.Harness, alice,
bob *lnwallet.LightningWallet, t *testing.T) {
testSingleFunderReservationWorkflow(
miner, alice, bob, t,
lnwallet.CommitmentTypeLegacy, nil,
nil, [32]byte{}, 0,
)
},
},
{
name: "single funding workflow tweakless",
test: func(miner *rpctest.Harness, alice,
bob *lnwallet.LightningWallet, t *testing.T) {
testSingleFunderReservationWorkflow(
miner, alice, bob, t,
lnwallet.CommitmentTypeTweakless, nil,
nil, [32]byte{}, 0,
)
},
},
{
name: "single funding workflow external funding tx",
test: testSingleFunderExternalFundingTx,
},
{
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,
},
{
name: "publish transaction",
test: testPublishTransaction,
},
{
name: "signed with tweaked pubkeys",
test: testSignOutputUsingTweaks,
},
{
name: "test cancel non-existent reservation",
test: testCancelNonExistentReservation,
},
{
name: "last unused addr",
test: testLastUnusedAddr,
},
{
name: "reorg wallet balance",
test: testReorgWalletBalance,
},
{
name: "create simple tx",
test: testCreateSimpleTx,
},
{
name: "test sign create account",
test: testSignOutputCreateAccount,
},
}
func clearWalletStates(a, b *lnwallet.LightningWallet) error {
a.ResetReservations()
b.ResetReservations()
if err := a.Cfg.Database.Wipe(); err != nil {
return err
}
return b.Cfg.Database.Wipe()
}
func waitForMempoolTx(r *rpctest.Harness, txid *chainhash.Hash) error {
var found bool
var tx *btcutil.Tx
var err error
timeout := time.After(30 * time.Second)
for !found {
select {
case <-timeout:
return fmt.Errorf("timeout after 10s")
default:
}
time.Sleep(100 * time.Millisecond)
tx, err = r.Node.GetRawTransaction(txid)
if err != nil {
switch e := err.(type) {
case *btcjson.RPCError:
if e.Code == btcjson.ErrRPCNoTxInfo {
continue
}
default:
}
return err
}
if tx != nil && tx.MsgTx().TxHash() == *txid {
found = true
}
}
return nil
}
func waitForWalletSync(r *rpctest.Harness, w *lnwallet.LightningWallet) error {
var (
synced bool
err error
bestHash, knownHash *chainhash.Hash
bestHeight, knownHeight int32
)
timeout := time.After(10 * time.Second)
for !synced {
select {
case <-timeout:
return fmt.Errorf("timeout after 30s")
case <-time.Tick(100 * time.Millisecond):
}
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)
}
synced, _, err = w.IsSynced()
if err != nil {
return err
}
}
return nil
}
func testSingleFunderExternalFundingTx(miner *rpctest.Harness,
alice, bob *lnwallet.LightningWallet, t *testing.T) {
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)
}
chanAmt := 4 * btcutil.SatoshiPerBitcoin
aliceChanFunder := chanfunding.NewWalletAssembler(chanfunding.WalletConfig{
CoinSource: lnwallet.NewCoinSource(alice),
CoinSelectLocker: alice,
CoinLocker: alice,
Signer: alice.Cfg.Signer,
DustLimit: 600,
})
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)
}
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)
}
thawHeight := uint32(200)
aliceExternalFunder := chanfunding.NewCannedAssembler(
thawHeight, *chanPoint, btcutil.Amount(chanAmt), &aliceFundingKey,
bobFundingKey.PubKey, true,
)
bobShimIntent, err := chanfunding.NewCannedAssembler(
thawHeight, *chanPoint, btcutil.Amount(chanAmt), &bobFundingKey,
aliceFundingKey.PubKey, false,
).ProvisionChannel(&chanfunding.Request{
LocalAmt: btcutil.Amount(chanAmt),
MinConfs: 1,
FeeRate: 253,
ChangeAddr: func() (btcutil.Address, error) {
return bob.NewAddress(lnwallet.WitnessPubKey, true)
},
})
if err != nil {
t.Fatalf("unable to create shim intent for bob: %v", err)
}
pendingChanID := testHdSeed
err = bob.RegisterFundingIntent(pendingChanID, bobShimIntent)
if err != nil {
t.Fatalf("unable to register intent: %v", err)
}
testSingleFunderReservationWorkflow(
miner, alice, bob, t, lnwallet.CommitmentTypeTweakless,
aliceExternalFunder, func() *wire.MsgTx {
return fundingTx
}, pendingChanID, thawHeight,
)
}
func TestLightningWallet(t *testing.T) {
t.Parallel()
miningNode, err := rpctest.New(netParams, nil, []string{"--txindex"})
if err != nil {
t.Fatalf("unable to create mining node: %v", err)
}
defer miningNode.TearDown()
if err := miningNode.SetUp(true, 25); err != nil {
t.Fatalf("unable to set up mining node: %v", err)
}
numBlocks := netParams.MinerConfirmationWindow * 2
if _, err := miningNode.Node.Generate(numBlocks); err != nil {
t.Fatalf("unable to generate blocks: %v", err)
}
rpcConfig := miningNode.RPCConfig()
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)
}
hintCache, err := chainntnfs.NewHeightHintCache(db)
if err != nil {
t.Fatalf("unable to create height hint cache: %v", err)
}
chainNotifier, err := btcdnotify.New(
&rpcConfig, netParams, hintCache, hintCache,
)
if err != nil {
t.Fatalf("unable to create notifier: %v", err)
}
if err := chainNotifier.Start(); err != nil {
t.Fatalf("unable to start notifier: %v", err)
}
for _, walletDriver := range lnwallet.RegisteredWallets() {
for _, backEnd := range walletDriver.BackEnds() {
if !runTests(t, walletDriver, backEnd, miningNode,
rpcConfig, chainNotifier) {
return
}
}
}
}
func runTests(t *testing.T, walletDriver *lnwallet.WalletDriver,
backEnd string, miningNode *rpctest.Harness,
rpcConfig rpcclient.ConnConfig,
chainNotifier chainntnfs.ChainNotifier) bool {
var (
bio lnwallet.BlockChainIO
aliceSigner input.Signer
bobSigner input.Signer
aliceKeyRing keychain.SecretKeyRing
bobKeyRing keychain.SecretKeyRing
aliceWalletController lnwallet.WalletController
bobWalletController lnwallet.WalletController
)
tempTestDirAlice, err := ioutil.TempDir("", "lnwallet")
if err != nil {
t.Fatalf("unable to create temp directory: %v", err)
}
defer os.RemoveAll(tempTestDirAlice)
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,
rpcConfig.Host, rpcConfig.User, rpcConfig.Pass,
rpcConfig.Certificates, false, 20)
if err != nil {
t.Fatalf("unable to make chain rpc: %v", err)
}
bobClient, err = chain.NewRPCClient(netParams,
rpcConfig.Host, rpcConfig.User, rpcConfig.Pass,
rpcConfig.Certificates, false, 20)
if err != nil {
t.Fatalf("unable to make chain rpc: %v", err)
}
case "neutrino":
neutrino.BanDuration = time.Millisecond * 100
neutrino.QueryTimeout = time.Millisecond * 500
neutrino.QueryNumRetries = 1
aliceDB, err := walletdb.Create(
"bdb", tempTestDirAlice+"/neutrino.db", true,
)
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()
aliceClient = chain.NewNeutrinoClient(
netParams, aliceChain,
)
bobDB, err := walletdb.Create(
"bdb", tempTestDirBob+"/neutrino.db", true,
)
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()
bobClient = chain.NewNeutrinoClient(
netParams, bobChain,
)
case "bitcoind":
tempBitcoindDir, err := ioutil.TempDir("", "bitcoind")
if err != nil {
t.Fatalf("unable to create temp directory: %v", err)
}
zmqBlockHost := "ipc:///" + tempBitcoindDir + "/blocks.socket"
zmqTxHost := "ipc:///" + tempBitcoindDir + "/tx.socket"
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",
"-zmqpubrawblock="+zmqBlockHost,
"-zmqpubrawtx="+zmqTxHost,
)
err = bitcoind.Start()
if err != nil {
t.Fatalf("couldn't start bitcoind: %v", err)
}
defer bitcoind.Wait()
defer bitcoind.Process.Kill()
host := fmt.Sprintf("127.0.0.1:%d", rpcPort)
var chainConn *chain.BitcoindConn
err = wait.NoError(func() error {
chainConn, err = chain.NewBitcoindConn(
netParams, host, "weks", "weks",
zmqBlockHost, zmqTxHost,
100*time.Millisecond,
)
if err != nil {
return err
}
return chainConn.Start()
}, 10*time.Second)
if err != nil {
t.Fatalf("unable to establish connection to "+
"bitcoind: %v", err)
}
defer chainConn.Stop()
aliceClient = chainConn.NewBitcoindClient()
bobClient = chainConn.NewBitcoindClient()
default:
t.Fatalf("unknown chain driver: %v", backEnd)
}
aliceSeed := sha256.New()
aliceSeed.Write([]byte(backEnd))
aliceSeed.Write(aliceHDSeed[:])
aliceSeedBytes := aliceSeed.Sum(nil)
aliceWalletConfig := &btcwallet.Config{
PrivatePass: []byte("alice-pass"),
HdSeed: aliceSeedBytes,
DataDir: tempTestDirAlice,
NetParams: netParams,
ChainSource: aliceClient,
CoinType: keychain.CoinTypeTestnet,
}
aliceWalletController, err = walletDriver.New(aliceWalletConfig)
if err != nil {
t.Fatalf("unable to create btcwallet: %v", err)
}
aliceSigner = aliceWalletController.(*btcwallet.BtcWallet)
aliceKeyRing = keychain.NewBtcWalletKeyRing(
aliceWalletController.(*btcwallet.BtcWallet).InternalWallet(),
keychain.CoinTypeTestnet,
)
bobSeed := sha256.New()
bobSeed.Write([]byte(backEnd))
bobSeed.Write(bobHDSeed[:])
bobSeedBytes := bobSeed.Sum(nil)
bobWalletConfig := &btcwallet.Config{
PrivatePass: []byte("bob-pass"),
HdSeed: bobSeedBytes,
DataDir: tempTestDirBob,
NetParams: netParams,
ChainSource: bobClient,
CoinType: keychain.CoinTypeTestnet,
}
bobWalletController, err = walletDriver.New(bobWalletConfig)
if err != nil {
t.Fatalf("unable to create btcwallet: %v", err)
}
bobSigner = bobWalletController.(*btcwallet.BtcWallet)
bobKeyRing = keychain.NewBtcWalletKeyRing(
bobWalletController.(*btcwallet.BtcWallet).InternalWallet(),
keychain.CoinTypeTestnet,
)
bio = bobWalletController.(*btcwallet.BtcWallet)
default:
t.Fatalf("unknown wallet driver: %v", walletType)
}
alice, err := createTestWallet(
tempTestDirAlice, miningNode, netParams,
chainNotifier, aliceWalletController, aliceKeyRing,
aliceSigner, bio,
)
if err != nil {
t.Fatalf("unable to create test ln wallet: %v", err)
}
defer alice.Shutdown()
bob, err := createTestWallet(
tempTestDirBob, miningNode, netParams,
chainNotifier, bobWalletController, bobKeyRing, bobSigner, bio,
)
if err != nil {
t.Fatalf("unable to create test ln wallet: %v", err)
}
defer bob.Shutdown()
assertProperBalance(t, alice, 1, 80)
assertProperBalance(t, bob, 1, 80)
for _, walletTest := range walletTests {
walletTest := walletTest
testName := fmt.Sprintf("%v/%v:%v", walletType, backEnd,
walletTest.name)
success := t.Run(testName, func(t *testing.T) {
if backEnd == "neutrino" &&
strings.Contains(walletTest.name, "dual funder") {
t.Skip("skipping dual funder tests for neutrino")
}
walletTest.test(miningNode, alice, bob, t)
})
if !success {
return false
}
if err := clearWalletStates(alice, bob); err !=
nil && err != kvdb.ErrBucketNotFound {
t.Fatalf("unable to wipe wallet state: %v", err)
}
}
return true
}