lnwallet/interface_test: add RBF test cases to testPublishTransaction

Checks that we get ErrDoubleSpend as expected when publishing a
conflicting mempool transaction with the same fee as the existing one,
and that we can publish a replacement with a higher fee successfully.
This commit is contained in:
Johan T. Halseth committed 2019-09-25 08:04:04 +02:00
1 parent 61e1b48f57
commit 2e9452916e
1 file changed
+57 -35
+57 -35
View File
@@ -22,6 +22,7 @@ import (
"github.com/btcsuite/btcd/chaincfg" "github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/integration/rpctest" "github.com/btcsuite/btcd/integration/rpctest"
"github.com/btcsuite/btcd/mempool"
"github.com/btcsuite/btcd/rpcclient" "github.com/btcsuite/btcd/rpcclient"
"github.com/btcsuite/btcd/txscript" "github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcd/wire"
@@ -1472,8 +1473,8 @@ func mineAndAssert(r *rpctest.Harness, tx *wire.MsgTx) error {
// txFromOutput takes a tx paying to fromPubKey, and creates a new tx that // 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. // spends the output from this tx, to an address derived from payToPubKey.
func txFromOutput(tx *wire.MsgTx, signer input.Signer, fromPubKey, func txFromOutput(tx *wire.MsgTx, signer input.Signer, fromPubKey,
payToPubKey *btcec.PublicKey, txFee btcutil.Amount) ( payToPubKey *btcec.PublicKey, txFee btcutil.Amount,
*wire.MsgTx, error) { rbf bool) (*wire.MsgTx, error) {
// Generate the script we want to spend from. // Generate the script we want to spend from.
keyScript, err := scriptFromKey(fromPubKey) keyScript, err := scriptFromKey(fromPubKey)
@@ -1494,13 +1495,20 @@ func txFromOutput(tx *wire.MsgTx, signer input.Signer, fromPubKey,
// referenced output. // referenced output.
tx1 := wire.NewMsgTx(2) tx1 := wire.NewMsgTx(2)
// 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
}
tx1.AddTxIn(&wire.TxIn{ tx1.AddTxIn(&wire.TxIn{
PreviousOutPoint: wire.OutPoint{ PreviousOutPoint: wire.OutPoint{
Hash: tx.TxHash(), Hash: tx.TxHash(),
Index: outputIndex, Index: outputIndex,
}, },
// We don't support RBF, so set sequence to max. Sequence: sequence,
Sequence: wire.MaxTxInSequenceNum,
}) })
// Create a script to pay to. // Create a script to pay to.
@@ -1585,7 +1593,7 @@ func newTx(t *testing.T, r *rpctest.Harness, pubKey *btcec.PublicKey,
// Create a new unconfirmed tx that spends this output. // Create a new unconfirmed tx that spends this output.
txFee := btcutil.Amount(0.1 * btcutil.SatoshiPerBitcoin) txFee := btcutil.Amount(0.1 * btcutil.SatoshiPerBitcoin)
tx1, err := txFromOutput( tx1, err := txFromOutput(
tx, alice.Cfg.Signer, pubKey, pubKey, txFee, tx, alice.Cfg.Signer, pubKey, pubKey, txFee, rbf,
) )
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@@ -1657,10 +1665,18 @@ func testPublishTransaction(r *rpctest.Harness,
t.Fatalf("unable to publish: %v", err) t.Fatalf("unable to publish: %v", err)
} }
// We'll do the next mempool check on both RBF and non-RBF enabled
// transactions.
var (
txFee = btcutil.Amount(0.05 * btcutil.SatoshiPerBitcoin)
tx3, tx3Spend *wire.MsgTx
)
for _, rbf := range []bool{false, true} {
// Now we'll try to double spend an output with a different // Now we'll try to double spend an output with a different
// transaction. Create a new tx and publish it. This is the output // transaction. Create a new tx and publish it. This is the
// we'll try to double spend. // output we'll try to double spend.
tx3 := newTx(t, r, keyDesc.PubKey, alice, false) tx3 = newTx(t, r, keyDesc.PubKey, alice, false)
if err := alice.PublishTransaction(tx3); err != nil { if err := alice.PublishTransaction(tx3); err != nil {
t.Fatalf("unable to publish: %v", err) t.Fatalf("unable to publish: %v", err)
} }
@@ -1670,12 +1686,11 @@ func testPublishTransaction(r *rpctest.Harness,
t.Fatalf("unable to mine tx: %v", err) t.Fatalf("unable to mine tx: %v", err)
} }
// Now we create a transaction that spends the output from the tx just // Now we create a transaction that spends the output from the
// mined. // tx just mined.
txFee := btcutil.Amount(0.05 * btcutil.SatoshiPerBitcoin)
tx4, err := txFromOutput( tx4, err := txFromOutput(
tx3, alice.Cfg.Signer, keyDesc.PubKey, keyDesc.PubKey, tx3, alice.Cfg.Signer, keyDesc.PubKey,
txFee, keyDesc.PubKey, txFee, rbf,
) )
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@@ -1686,6 +1701,10 @@ func testPublishTransaction(r *rpctest.Harness,
t.Fatalf("unable to publish: %v", err) t.Fatalf("unable to publish: %v", err)
} }
// Keep track of the last successfully published tx to spend
// tx3.
tx3Spend = tx4
txid4 := tx4.TxHash() txid4 := tx4.TxHash()
err = waitForMempoolTx(r, &txid4) err = waitForMempoolTx(r, &txid4)
if err != nil { if err != nil {
@@ -1701,12 +1720,12 @@ func testPublishTransaction(r *rpctest.Harness,
t.Fatalf("unable to obtain public key: %v", err) t.Fatalf("unable to obtain public key: %v", err)
} }
// Create a new transaction that spends the output from tx3, and that // Create a new transaction that spends the output from tx3,
// pays to a different address. We expect this to be rejected because // and that pays to a different address. We expect this to be
// it is a double spend. // rejected because it is a double spend.
tx5, err := txFromOutput( tx5, err := txFromOutput(
tx3, alice.Cfg.Signer, keyDesc.PubKey, keyDesc2.PubKey, tx3, alice.Cfg.Signer, keyDesc.PubKey,
txFee, keyDesc2.PubKey, txFee, rbf,
) )
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@@ -1717,29 +1736,40 @@ func testPublishTransaction(r *rpctest.Harness,
t.Fatalf("expected ErrDoubleSpend, got: %v", err) t.Fatalf("expected ErrDoubleSpend, got: %v", err)
} }
// Create another transaction that spends the same output, but has a // Create another transaction that spends the same output, but
// higher fee. We expect also this tx to be rejected, since the // has a higher fee. We expect also this tx to be rejected for
// sequence number of tx3 is set to Max, indicating it is not // non-RBF enabled transactions, while it should succeed
// replacable. // otherwise.
pubKey3, err := alice.DeriveNextKey(keychain.KeyFamilyMultiSig) pubKey3, err := alice.DeriveNextKey(keychain.KeyFamilyMultiSig)
if err != nil { if err != nil {
t.Fatalf("unable to obtain public key: %v", err) t.Fatalf("unable to obtain public key: %v", err)
} }
tx6, err := txFromOutput( tx6, err := txFromOutput(
tx3, alice.Cfg.Signer, keyDesc.PubKey, tx3, alice.Cfg.Signer, keyDesc.PubKey,
pubKey3.PubKey, 2*txFee, pubKey3.PubKey, 2*txFee, rbf,
) )
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
// Expect rejection. // Expect rejection in non-RBF case.
expErr := lnwallet.ErrDoubleSpend
if rbf {
// Expect success in rbf case.
expErr = nil
tx3Spend = tx6
}
err = alice.PublishTransaction(tx6) err = alice.PublishTransaction(tx6)
if err != lnwallet.ErrDoubleSpend { if err != expErr {
t.Fatalf("expected ErrDoubleSpend, got: %v", err) 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)
}
}
// At last we try to spend an output already spent by a confirmed // At last we try to spend an output already spent by a confirmed
// transaction. // transaction.
// TODO(halseth): we currently skip this test for neutrino, as the // TODO(halseth): we currently skip this test for neutrino, as the
@@ -1747,11 +1777,6 @@ func testPublishTransaction(r *rpctest.Harness,
// accept it. Should look into if this is the behavior also for // accept it. Should look into if this is the behavior also for
// bitcoind, and update test accordingly. // bitcoind, and update test accordingly.
if alice.BackEnd() != "neutrino" { if alice.BackEnd() != "neutrino" {
// Mine the tx spending tx3.
if err := mineAndAssert(r, tx4); err != nil {
t.Fatalf("unable to mine tx: %v", err)
}
// Create another tx spending tx3. // Create another tx spending tx3.
pubKey4, err := alice.DeriveNextKey( pubKey4, err := alice.DeriveNextKey(
keychain.KeyFamilyMultiSig, keychain.KeyFamilyMultiSig,
@@ -1761,7 +1786,7 @@ func testPublishTransaction(r *rpctest.Harness,
} }
tx7, err := txFromOutput( tx7, err := txFromOutput(
tx3, alice.Cfg.Signer, keyDesc.PubKey, tx3, alice.Cfg.Signer, keyDesc.PubKey,
pubKey4.PubKey, txFee, pubKey4.PubKey, txFee, false,
) )
if err != nil { if err != nil {
@@ -1774,9 +1799,6 @@ func testPublishTransaction(r *rpctest.Harness,
t.Fatalf("expected ErrDoubleSpend, got: %v", err) t.Fatalf("expected ErrDoubleSpend, got: %v", err)
} }
} }
// TODO(halseth): test replaceable transactions when btcd gets RBF
// support.
} }
func testSignOutputUsingTweaks(r *rpctest.Harness, func testSignOutputUsingTweaks(r *rpctest.Harness,