You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
566 lines
22 KiB
566 lines
22 KiB
9 years ago
|
#!/usr/bin/env python3
|
||
7 years ago
|
# Copyright (c) 2014-2017 The Bitcoin Core developers
|
||
9 years ago
|
# Distributed under the MIT software license, see the accompanying
|
||
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||
8 years ago
|
"""Test the RBF code."""
|
||
9 years ago
|
|
||
|
from test_framework.test_framework import BitcoinTestFramework
|
||
|
from test_framework.util import *
|
||
|
from test_framework.script import *
|
||
|
from test_framework.mininode import *
|
||
|
|
||
|
MAX_REPLACEMENT_LIMIT = 100
|
||
|
|
||
|
def txToHex(tx):
|
||
9 years ago
|
return bytes_to_hex_str(tx.serialize())
|
||
9 years ago
|
|
||
|
def make_utxo(node, amount, confirmed=True, scriptPubKey=CScript([1])):
|
||
|
"""Create a txout with a given amount and scriptPubKey
|
||
|
|
||
|
Mines coins as needed.
|
||
|
|
||
|
confirmed - txouts created will be confirmed in the blockchain;
|
||
|
unconfirmed otherwise.
|
||
|
"""
|
||
|
fee = 1*COIN
|
||
|
while node.getbalance() < satoshi_round((amount + fee)/COIN):
|
||
|
node.generate(100)
|
||
|
|
||
|
new_addr = node.getnewaddress()
|
||
|
txid = node.sendtoaddress(new_addr, satoshi_round((amount+fee)/COIN))
|
||
|
tx1 = node.getrawtransaction(txid, 1)
|
||
|
txid = int(txid, 16)
|
||
|
i = None
|
||
|
|
||
|
for i, txout in enumerate(tx1['vout']):
|
||
|
if txout['scriptPubKey']['addresses'] == [new_addr]:
|
||
|
break
|
||
|
assert i is not None
|
||
|
|
||
|
tx2 = CTransaction()
|
||
|
tx2.vin = [CTxIn(COutPoint(txid, i))]
|
||
|
tx2.vout = [CTxOut(amount, scriptPubKey)]
|
||
|
tx2.rehash()
|
||
|
|
||
9 years ago
|
signed_tx = node.signrawtransaction(txToHex(tx2))
|
||
9 years ago
|
|
||
|
txid = node.sendrawtransaction(signed_tx['hex'], True)
|
||
|
|
||
|
# If requested, ensure txouts are confirmed.
|
||
|
if confirmed:
|
||
9 years ago
|
mempool_size = len(node.getrawmempool())
|
||
|
while mempool_size > 0:
|
||
9 years ago
|
node.generate(1)
|
||
9 years ago
|
new_size = len(node.getrawmempool())
|
||
|
# Error out if we have something stuck in the mempool, as this
|
||
|
# would likely be a bug.
|
||
|
assert(new_size < mempool_size)
|
||
|
mempool_size = new_size
|
||
9 years ago
|
|
||
|
return COutPoint(int(txid, 16), 0)
|
||
|
|
||
|
class ReplaceByFeeTest(BitcoinTestFramework):
|
||
|
|
||
8 years ago
|
def set_test_params(self):
|
||
7 years ago
|
self.num_nodes = 2
|
||
8 years ago
|
self.extra_args= [["-maxorphantx=1000",
|
||
|
"-whitelist=127.0.0.1",
|
||
|
"-limitancestorcount=50",
|
||
|
"-limitancestorsize=101",
|
||
|
"-limitdescendantcount=200",
|
||
7 years ago
|
"-limitdescendantsize=101"],
|
||
|
["-mempoolreplacement=0"]]
|
||
9 years ago
|
|
||
|
def run_test(self):
|
||
7 years ago
|
# Leave IBD
|
||
7 years ago
|
self.nodes[0].generate(1)
|
||
|
|
||
9 years ago
|
make_utxo(self.nodes[0], 1*COIN)
|
||
|
|
||
7 years ago
|
# Ensure nodes are synced
|
||
|
self.sync_all()
|
||
|
|
||
8 years ago
|
self.log.info("Running test simple doublespend...")
|
||
9 years ago
|
self.test_simple_doublespend()
|
||
|
|
||
8 years ago
|
self.log.info("Running test doublespend chain...")
|
||
9 years ago
|
self.test_doublespend_chain()
|
||
|
|
||
8 years ago
|
self.log.info("Running test doublespend tree...")
|
||
9 years ago
|
self.test_doublespend_tree()
|
||
|
|
||
8 years ago
|
self.log.info("Running test replacement feeperkb...")
|
||
9 years ago
|
self.test_replacement_feeperkb()
|
||
|
|
||
8 years ago
|
self.log.info("Running test spends of conflicting outputs...")
|
||
9 years ago
|
self.test_spends_of_conflicting_outputs()
|
||
|
|
||
8 years ago
|
self.log.info("Running test new unconfirmed inputs...")
|
||
9 years ago
|
self.test_new_unconfirmed_inputs()
|
||
|
|
||
8 years ago
|
self.log.info("Running test too many replacements...")
|
||
9 years ago
|
self.test_too_many_replacements()
|
||
|
|
||
8 years ago
|
self.log.info("Running test opt-in...")
|
||
9 years ago
|
self.test_opt_in()
|
||
|
|
||
9 years ago
|
self.log.info("Running test RPC...")
|
||
|
self.test_rpc()
|
||
|
|
||
8 years ago
|
self.log.info("Running test prioritised transactions...")
|
||
9 years ago
|
self.test_prioritised_transactions()
|
||
|
|
||
8 years ago
|
self.log.info("Passed")
|
||
9 years ago
|
|
||
|
def test_simple_doublespend(self):
|
||
|
"""Simple doublespend"""
|
||
9 years ago
|
tx0_outpoint = make_utxo(self.nodes[0], int(1.1*COIN))
|
||
9 years ago
|
|
||
7 years ago
|
# make_utxo may have generated a bunch of blocks, so we need to sync
|
||
|
# before we can spend the coins generated, or else the resulting
|
||
|
# transactions might not be accepted by our peers.
|
||
|
self.sync_all()
|
||
|
|
||
9 years ago
|
tx1a = CTransaction()
|
||
|
tx1a.vin = [CTxIn(tx0_outpoint, nSequence=0)]
|
||
|
tx1a.vout = [CTxOut(1*COIN, CScript([b'a']))]
|
||
|
tx1a_hex = txToHex(tx1a)
|
||
|
tx1a_txid = self.nodes[0].sendrawtransaction(tx1a_hex, True)
|
||
|
|
||
7 years ago
|
self.sync_all()
|
||
7 years ago
|
|
||
9 years ago
|
# Should fail because we haven't changed the fee
|
||
|
tx1b = CTransaction()
|
||
|
tx1b.vin = [CTxIn(tx0_outpoint, nSequence=0)]
|
||
|
tx1b.vout = [CTxOut(1*COIN, CScript([b'b']))]
|
||
|
tx1b_hex = txToHex(tx1b)
|
||
|
|
||
8 years ago
|
# This will raise an exception due to insufficient fee
|
||
7 years ago
|
assert_raises_rpc_error(-26, "insufficient fee", self.nodes[0].sendrawtransaction, tx1b_hex, True)
|
||
7 years ago
|
# This will raise an exception due to transaction replacement being disabled
|
||
7 years ago
|
assert_raises_rpc_error(-26, "txn-mempool-conflict", self.nodes[1].sendrawtransaction, tx1b_hex, True)
|
||
9 years ago
|
|
||
|
# Extra 0.1 BTC fee
|
||
|
tx1b = CTransaction()
|
||
|
tx1b.vin = [CTxIn(tx0_outpoint, nSequence=0)]
|
||
9 years ago
|
tx1b.vout = [CTxOut(int(0.9*COIN), CScript([b'b']))]
|
||
9 years ago
|
tx1b_hex = txToHex(tx1b)
|
||
7 years ago
|
# Replacement still disabled even with "enough fee"
|
||
7 years ago
|
assert_raises_rpc_error(-26, "txn-mempool-conflict", self.nodes[1].sendrawtransaction, tx1b_hex, True)
|
||
7 years ago
|
# Works when enabled
|
||
9 years ago
|
tx1b_txid = self.nodes[0].sendrawtransaction(tx1b_hex, True)
|
||
|
|
||
|
mempool = self.nodes[0].getrawmempool()
|
||
|
|
||
|
assert (tx1a_txid not in mempool)
|
||
|
assert (tx1b_txid in mempool)
|
||
|
|
||
|
assert_equal(tx1b_hex, self.nodes[0].getrawtransaction(tx1b_txid))
|
||
|
|
||
7 years ago
|
# Second node is running mempoolreplacement=0, will not replace originally-seen txn
|
||
|
mempool = self.nodes[1].getrawmempool()
|
||
|
assert tx1a_txid in mempool
|
||
|
assert tx1b_txid not in mempool
|
||
|
|
||
9 years ago
|
def test_doublespend_chain(self):
|
||
|
"""Doublespend of a long chain"""
|
||
|
|
||
|
initial_nValue = 50*COIN
|
||
|
tx0_outpoint = make_utxo(self.nodes[0], initial_nValue)
|
||
|
|
||
|
prevout = tx0_outpoint
|
||
|
remaining_value = initial_nValue
|
||
|
chain_txids = []
|
||
|
while remaining_value > 10*COIN:
|
||
|
remaining_value -= 1*COIN
|
||
|
tx = CTransaction()
|
||
|
tx.vin = [CTxIn(prevout, nSequence=0)]
|
||
|
tx.vout = [CTxOut(remaining_value, CScript([1]))]
|
||
|
tx_hex = txToHex(tx)
|
||
|
txid = self.nodes[0].sendrawtransaction(tx_hex, True)
|
||
|
chain_txids.append(txid)
|
||
|
prevout = COutPoint(int(txid, 16), 0)
|
||
|
|
||
|
# Whether the double-spend is allowed is evaluated by including all
|
||
|
# child fees - 40 BTC - so this attempt is rejected.
|
||
|
dbl_tx = CTransaction()
|
||
|
dbl_tx.vin = [CTxIn(tx0_outpoint, nSequence=0)]
|
||
|
dbl_tx.vout = [CTxOut(initial_nValue - 30*COIN, CScript([1]))]
|
||
|
dbl_tx_hex = txToHex(dbl_tx)
|
||
|
|
||
8 years ago
|
# This will raise an exception due to insufficient fee
|
||
7 years ago
|
assert_raises_rpc_error(-26, "insufficient fee", self.nodes[0].sendrawtransaction, dbl_tx_hex, True)
|
||
9 years ago
|
|
||
|
# Accepted with sufficient fee
|
||
|
dbl_tx = CTransaction()
|
||
|
dbl_tx.vin = [CTxIn(tx0_outpoint, nSequence=0)]
|
||
|
dbl_tx.vout = [CTxOut(1*COIN, CScript([1]))]
|
||
|
dbl_tx_hex = txToHex(dbl_tx)
|
||
|
self.nodes[0].sendrawtransaction(dbl_tx_hex, True)
|
||
|
|
||
|
mempool = self.nodes[0].getrawmempool()
|
||
|
for doublespent_txid in chain_txids:
|
||
|
assert(doublespent_txid not in mempool)
|
||
|
|
||
|
def test_doublespend_tree(self):
|
||
|
"""Doublespend of a big tree of transactions"""
|
||
|
|
||
|
initial_nValue = 50*COIN
|
||
|
tx0_outpoint = make_utxo(self.nodes[0], initial_nValue)
|
||
|
|
||
|
def branch(prevout, initial_value, max_txs, tree_width=5, fee=0.0001*COIN, _total_txs=None):
|
||
|
if _total_txs is None:
|
||
|
_total_txs = [0]
|
||
|
if _total_txs[0] >= max_txs:
|
||
|
return
|
||
|
|
||
|
txout_value = (initial_value - fee) // tree_width
|
||
|
if txout_value < fee:
|
||
|
return
|
||
|
|
||
|
vout = [CTxOut(txout_value, CScript([i+1]))
|
||
|
for i in range(tree_width)]
|
||
|
tx = CTransaction()
|
||
|
tx.vin = [CTxIn(prevout, nSequence=0)]
|
||
|
tx.vout = vout
|
||
|
tx_hex = txToHex(tx)
|
||
|
|
||
|
assert(len(tx.serialize()) < 100000)
|
||
|
txid = self.nodes[0].sendrawtransaction(tx_hex, True)
|
||
|
yield tx
|
||
|
_total_txs[0] += 1
|
||
|
|
||
|
txid = int(txid, 16)
|
||
|
|
||
|
for i, txout in enumerate(tx.vout):
|
||
|
for x in branch(COutPoint(txid, i), txout_value,
|
||
|
max_txs,
|
||
|
tree_width=tree_width, fee=fee,
|
||
|
_total_txs=_total_txs):
|
||
|
yield x
|
||
|
|
||
9 years ago
|
fee = int(0.0001*COIN)
|
||
9 years ago
|
n = MAX_REPLACEMENT_LIMIT
|
||
|
tree_txs = list(branch(tx0_outpoint, initial_nValue, n, fee=fee))
|
||
|
assert_equal(len(tree_txs), n)
|
||
|
|
||
|
# Attempt double-spend, will fail because too little fee paid
|
||
|
dbl_tx = CTransaction()
|
||
|
dbl_tx.vin = [CTxIn(tx0_outpoint, nSequence=0)]
|
||
|
dbl_tx.vout = [CTxOut(initial_nValue - fee*n, CScript([1]))]
|
||
|
dbl_tx_hex = txToHex(dbl_tx)
|
||
8 years ago
|
# This will raise an exception due to insufficient fee
|
||
7 years ago
|
assert_raises_rpc_error(-26, "insufficient fee", self.nodes[0].sendrawtransaction, dbl_tx_hex, True)
|
||
9 years ago
|
|
||
|
# 1 BTC fee is enough
|
||
|
dbl_tx = CTransaction()
|
||
|
dbl_tx.vin = [CTxIn(tx0_outpoint, nSequence=0)]
|
||
|
dbl_tx.vout = [CTxOut(initial_nValue - fee*n - 1*COIN, CScript([1]))]
|
||
|
dbl_tx_hex = txToHex(dbl_tx)
|
||
|
self.nodes[0].sendrawtransaction(dbl_tx_hex, True)
|
||
|
|
||
|
mempool = self.nodes[0].getrawmempool()
|
||
|
|
||
|
for tx in tree_txs:
|
||
|
tx.rehash()
|
||
|
assert (tx.hash not in mempool)
|
||
|
|
||
|
# Try again, but with more total transactions than the "max txs
|
||
|
# double-spent at once" anti-DoS limit.
|
||
|
for n in (MAX_REPLACEMENT_LIMIT+1, MAX_REPLACEMENT_LIMIT*2):
|
||
9 years ago
|
fee = int(0.0001*COIN)
|
||
9 years ago
|
tx0_outpoint = make_utxo(self.nodes[0], initial_nValue)
|
||
|
tree_txs = list(branch(tx0_outpoint, initial_nValue, n, fee=fee))
|
||
|
assert_equal(len(tree_txs), n)
|
||
|
|
||
|
dbl_tx = CTransaction()
|
||
|
dbl_tx.vin = [CTxIn(tx0_outpoint, nSequence=0)]
|
||
|
dbl_tx.vout = [CTxOut(initial_nValue - 2*fee*n, CScript([1]))]
|
||
|
dbl_tx_hex = txToHex(dbl_tx)
|
||
8 years ago
|
# This will raise an exception
|
||
7 years ago
|
assert_raises_rpc_error(-26, "too many potential replacements", self.nodes[0].sendrawtransaction, dbl_tx_hex, True)
|
||
9 years ago
|
|
||
|
for tx in tree_txs:
|
||
|
tx.rehash()
|
||
|
self.nodes[0].getrawtransaction(tx.hash)
|
||
|
|
||
|
def test_replacement_feeperkb(self):
|
||
|
"""Replacement requires fee-per-KB to be higher"""
|
||
9 years ago
|
tx0_outpoint = make_utxo(self.nodes[0], int(1.1*COIN))
|
||
9 years ago
|
|
||
|
tx1a = CTransaction()
|
||
|
tx1a.vin = [CTxIn(tx0_outpoint, nSequence=0)]
|
||
|
tx1a.vout = [CTxOut(1*COIN, CScript([b'a']))]
|
||
|
tx1a_hex = txToHex(tx1a)
|
||
7 years ago
|
self.nodes[0].sendrawtransaction(tx1a_hex, True)
|
||
9 years ago
|
|
||
|
# Higher fee, but the fee per KB is much lower, so the replacement is
|
||
|
# rejected.
|
||
|
tx1b = CTransaction()
|
||
|
tx1b.vin = [CTxIn(tx0_outpoint, nSequence=0)]
|
||
9 years ago
|
tx1b.vout = [CTxOut(int(0.001*COIN), CScript([b'a'*999000]))]
|
||
9 years ago
|
tx1b_hex = txToHex(tx1b)
|
||
|
|
||
8 years ago
|
# This will raise an exception due to insufficient fee
|
||
7 years ago
|
assert_raises_rpc_error(-26, "insufficient fee", self.nodes[0].sendrawtransaction, tx1b_hex, True)
|
||
9 years ago
|
|
||
|
def test_spends_of_conflicting_outputs(self):
|
||
|
"""Replacements that spend conflicting tx outputs are rejected"""
|
||
9 years ago
|
utxo1 = make_utxo(self.nodes[0], int(1.2*COIN))
|
||
|
utxo2 = make_utxo(self.nodes[0], 3*COIN)
|
||
9 years ago
|
|
||
|
tx1a = CTransaction()
|
||
|
tx1a.vin = [CTxIn(utxo1, nSequence=0)]
|
||
9 years ago
|
tx1a.vout = [CTxOut(int(1.1*COIN), CScript([b'a']))]
|
||
9 years ago
|
tx1a_hex = txToHex(tx1a)
|
||
|
tx1a_txid = self.nodes[0].sendrawtransaction(tx1a_hex, True)
|
||
|
|
||
|
tx1a_txid = int(tx1a_txid, 16)
|
||
|
|
||
|
# Direct spend an output of the transaction we're replacing.
|
||
|
tx2 = CTransaction()
|
||
|
tx2.vin = [CTxIn(utxo1, nSequence=0), CTxIn(utxo2, nSequence=0)]
|
||
|
tx2.vin.append(CTxIn(COutPoint(tx1a_txid, 0), nSequence=0))
|
||
|
tx2.vout = tx1a.vout
|
||
|
tx2_hex = txToHex(tx2)
|
||
|
|
||
8 years ago
|
# This will raise an exception
|
||
7 years ago
|
assert_raises_rpc_error(-26, "bad-txns-spends-conflicting-tx", self.nodes[0].sendrawtransaction, tx2_hex, True)
|
||
9 years ago
|
|
||
|
# Spend tx1a's output to test the indirect case.
|
||
|
tx1b = CTransaction()
|
||
|
tx1b.vin = [CTxIn(COutPoint(tx1a_txid, 0), nSequence=0)]
|
||
9 years ago
|
tx1b.vout = [CTxOut(1*COIN, CScript([b'a']))]
|
||
9 years ago
|
tx1b_hex = txToHex(tx1b)
|
||
|
tx1b_txid = self.nodes[0].sendrawtransaction(tx1b_hex, True)
|
||
|
tx1b_txid = int(tx1b_txid, 16)
|
||
|
|
||
|
tx2 = CTransaction()
|
||
|
tx2.vin = [CTxIn(utxo1, nSequence=0), CTxIn(utxo2, nSequence=0),
|
||
|
CTxIn(COutPoint(tx1b_txid, 0))]
|
||
|
tx2.vout = tx1a.vout
|
||
|
tx2_hex = txToHex(tx2)
|
||
|
|
||
8 years ago
|
# This will raise an exception
|
||
7 years ago
|
assert_raises_rpc_error(-26, "bad-txns-spends-conflicting-tx", self.nodes[0].sendrawtransaction, tx2_hex, True)
|
||
9 years ago
|
|
||
|
def test_new_unconfirmed_inputs(self):
|
||
|
"""Replacements that add new unconfirmed inputs are rejected"""
|
||
9 years ago
|
confirmed_utxo = make_utxo(self.nodes[0], int(1.1*COIN))
|
||
|
unconfirmed_utxo = make_utxo(self.nodes[0], int(0.1*COIN), False)
|
||
9 years ago
|
|
||
|
tx1 = CTransaction()
|
||
|
tx1.vin = [CTxIn(confirmed_utxo)]
|
||
9 years ago
|
tx1.vout = [CTxOut(1*COIN, CScript([b'a']))]
|
||
9 years ago
|
tx1_hex = txToHex(tx1)
|
||
7 years ago
|
self.nodes[0].sendrawtransaction(tx1_hex, True)
|
||
9 years ago
|
|
||
|
tx2 = CTransaction()
|
||
|
tx2.vin = [CTxIn(confirmed_utxo), CTxIn(unconfirmed_utxo)]
|
||
|
tx2.vout = tx1.vout
|
||
|
tx2_hex = txToHex(tx2)
|
||
|
|
||
8 years ago
|
# This will raise an exception
|
||
7 years ago
|
assert_raises_rpc_error(-26, "replacement-adds-unconfirmed", self.nodes[0].sendrawtransaction, tx2_hex, True)
|
||
9 years ago
|
|
||
|
def test_too_many_replacements(self):
|
||
|
"""Replacements that evict too many transactions are rejected"""
|
||
|
# Try directly replacing more than MAX_REPLACEMENT_LIMIT
|
||
|
# transactions
|
||
|
|
||
|
# Start by creating a single transaction with many outputs
|
||
|
initial_nValue = 10*COIN
|
||
|
utxo = make_utxo(self.nodes[0], initial_nValue)
|
||
9 years ago
|
fee = int(0.0001*COIN)
|
||
9 years ago
|
split_value = int((initial_nValue-fee)/(MAX_REPLACEMENT_LIMIT+1))
|
||
|
|
||
|
outputs = []
|
||
|
for i in range(MAX_REPLACEMENT_LIMIT+1):
|
||
|
outputs.append(CTxOut(split_value, CScript([1])))
|
||
|
|
||
|
splitting_tx = CTransaction()
|
||
|
splitting_tx.vin = [CTxIn(utxo, nSequence=0)]
|
||
|
splitting_tx.vout = outputs
|
||
|
splitting_tx_hex = txToHex(splitting_tx)
|
||
|
|
||
|
txid = self.nodes[0].sendrawtransaction(splitting_tx_hex, True)
|
||
|
txid = int(txid, 16)
|
||
|
|
||
|
# Now spend each of those outputs individually
|
||
|
for i in range(MAX_REPLACEMENT_LIMIT+1):
|
||
|
tx_i = CTransaction()
|
||
|
tx_i.vin = [CTxIn(COutPoint(txid, i), nSequence=0)]
|
||
|
tx_i.vout = [CTxOut(split_value-fee, CScript([b'a']))]
|
||
|
tx_i_hex = txToHex(tx_i)
|
||
|
self.nodes[0].sendrawtransaction(tx_i_hex, True)
|
||
|
|
||
|
# Now create doublespend of the whole lot; should fail.
|
||
|
# Need a big enough fee to cover all spending transactions and have
|
||
|
# a higher fee rate
|
||
|
double_spend_value = (split_value-100*fee)*(MAX_REPLACEMENT_LIMIT+1)
|
||
|
inputs = []
|
||
|
for i in range(MAX_REPLACEMENT_LIMIT+1):
|
||
|
inputs.append(CTxIn(COutPoint(txid, i), nSequence=0))
|
||
|
double_tx = CTransaction()
|
||
|
double_tx.vin = inputs
|
||
|
double_tx.vout = [CTxOut(double_spend_value, CScript([b'a']))]
|
||
|
double_tx_hex = txToHex(double_tx)
|
||
|
|
||
8 years ago
|
# This will raise an exception
|
||
7 years ago
|
assert_raises_rpc_error(-26, "too many potential replacements", self.nodes[0].sendrawtransaction, double_tx_hex, True)
|
||
9 years ago
|
|
||
|
# If we remove an input, it should pass
|
||
|
double_tx = CTransaction()
|
||
|
double_tx.vin = inputs[0:-1]
|
||
|
double_tx.vout = [CTxOut(double_spend_value, CScript([b'a']))]
|
||
|
double_tx_hex = txToHex(double_tx)
|
||
|
self.nodes[0].sendrawtransaction(double_tx_hex, True)
|
||
|
|
||
|
def test_opt_in(self):
|
||
8 years ago
|
"""Replacing should only work if orig tx opted in"""
|
||
9 years ago
|
tx0_outpoint = make_utxo(self.nodes[0], int(1.1*COIN))
|
||
9 years ago
|
|
||
|
# Create a non-opting in transaction
|
||
|
tx1a = CTransaction()
|
||
|
tx1a.vin = [CTxIn(tx0_outpoint, nSequence=0xffffffff)]
|
||
|
tx1a.vout = [CTxOut(1*COIN, CScript([b'a']))]
|
||
|
tx1a_hex = txToHex(tx1a)
|
||
|
tx1a_txid = self.nodes[0].sendrawtransaction(tx1a_hex, True)
|
||
|
|
||
|
# Shouldn't be able to double-spend
|
||
|
tx1b = CTransaction()
|
||
|
tx1b.vin = [CTxIn(tx0_outpoint, nSequence=0)]
|
||
9 years ago
|
tx1b.vout = [CTxOut(int(0.9*COIN), CScript([b'b']))]
|
||
9 years ago
|
tx1b_hex = txToHex(tx1b)
|
||
|
|
||
8 years ago
|
# This will raise an exception
|
||
7 years ago
|
assert_raises_rpc_error(-26, "txn-mempool-conflict", self.nodes[0].sendrawtransaction, tx1b_hex, True)
|
||
9 years ago
|
|
||
9 years ago
|
tx1_outpoint = make_utxo(self.nodes[0], int(1.1*COIN))
|
||
9 years ago
|
|
||
|
# Create a different non-opting in transaction
|
||
|
tx2a = CTransaction()
|
||
|
tx2a.vin = [CTxIn(tx1_outpoint, nSequence=0xfffffffe)]
|
||
|
tx2a.vout = [CTxOut(1*COIN, CScript([b'a']))]
|
||
|
tx2a_hex = txToHex(tx2a)
|
||
|
tx2a_txid = self.nodes[0].sendrawtransaction(tx2a_hex, True)
|
||
|
|
||
|
# Still shouldn't be able to double-spend
|
||
|
tx2b = CTransaction()
|
||
|
tx2b.vin = [CTxIn(tx1_outpoint, nSequence=0)]
|
||
9 years ago
|
tx2b.vout = [CTxOut(int(0.9*COIN), CScript([b'b']))]
|
||
9 years ago
|
tx2b_hex = txToHex(tx2b)
|
||
|
|
||
8 years ago
|
# This will raise an exception
|
||
7 years ago
|
assert_raises_rpc_error(-26, "txn-mempool-conflict", self.nodes[0].sendrawtransaction, tx2b_hex, True)
|
||
9 years ago
|
|
||
|
# Now create a new transaction that spends from tx1a and tx2a
|
||
|
# opt-in on one of the inputs
|
||
|
# Transaction should be replaceable on either input
|
||
|
|
||
|
tx1a_txid = int(tx1a_txid, 16)
|
||
|
tx2a_txid = int(tx2a_txid, 16)
|
||
|
|
||
|
tx3a = CTransaction()
|
||
|
tx3a.vin = [CTxIn(COutPoint(tx1a_txid, 0), nSequence=0xffffffff),
|
||
|
CTxIn(COutPoint(tx2a_txid, 0), nSequence=0xfffffffd)]
|
||
9 years ago
|
tx3a.vout = [CTxOut(int(0.9*COIN), CScript([b'c'])), CTxOut(int(0.9*COIN), CScript([b'd']))]
|
||
9 years ago
|
tx3a_hex = txToHex(tx3a)
|
||
|
|
||
|
self.nodes[0].sendrawtransaction(tx3a_hex, True)
|
||
|
|
||
|
tx3b = CTransaction()
|
||
|
tx3b.vin = [CTxIn(COutPoint(tx1a_txid, 0), nSequence=0)]
|
||
9 years ago
|
tx3b.vout = [CTxOut(int(0.5*COIN), CScript([b'e']))]
|
||
9 years ago
|
tx3b_hex = txToHex(tx3b)
|
||
|
|
||
|
tx3c = CTransaction()
|
||
|
tx3c.vin = [CTxIn(COutPoint(tx2a_txid, 0), nSequence=0)]
|
||
9 years ago
|
tx3c.vout = [CTxOut(int(0.5*COIN), CScript([b'f']))]
|
||
9 years ago
|
tx3c_hex = txToHex(tx3c)
|
||
|
|
||
|
self.nodes[0].sendrawtransaction(tx3b_hex, True)
|
||
|
# If tx3b was accepted, tx3c won't look like a replacement,
|
||
|
# but make sure it is accepted anyway
|
||
|
self.nodes[0].sendrawtransaction(tx3c_hex, True)
|
||
|
|
||
9 years ago
|
def test_prioritised_transactions(self):
|
||
|
# Ensure that fee deltas used via prioritisetransaction are
|
||
|
# correctly used by replacement logic
|
||
|
|
||
|
# 1. Check that feeperkb uses modified fees
|
||
9 years ago
|
tx0_outpoint = make_utxo(self.nodes[0], int(1.1*COIN))
|
||
9 years ago
|
|
||
|
tx1a = CTransaction()
|
||
|
tx1a.vin = [CTxIn(tx0_outpoint, nSequence=0)]
|
||
|
tx1a.vout = [CTxOut(1*COIN, CScript([b'a']))]
|
||
|
tx1a_hex = txToHex(tx1a)
|
||
|
tx1a_txid = self.nodes[0].sendrawtransaction(tx1a_hex, True)
|
||
|
|
||
|
# Higher fee, but the actual fee per KB is much lower.
|
||
|
tx1b = CTransaction()
|
||
|
tx1b.vin = [CTxIn(tx0_outpoint, nSequence=0)]
|
||
9 years ago
|
tx1b.vout = [CTxOut(int(0.001*COIN), CScript([b'a'*740000]))]
|
||
9 years ago
|
tx1b_hex = txToHex(tx1b)
|
||
|
|
||
|
# Verify tx1b cannot replace tx1a.
|
||
7 years ago
|
assert_raises_rpc_error(-26, "insufficient fee", self.nodes[0].sendrawtransaction, tx1b_hex, True)
|
||
9 years ago
|
|
||
|
# Use prioritisetransaction to set tx1a's fee to 0.
|
||
8 years ago
|
self.nodes[0].prioritisetransaction(txid=tx1a_txid, fee_delta=int(-0.1*COIN))
|
||
9 years ago
|
|
||
|
# Now tx1b should be able to replace tx1a
|
||
|
tx1b_txid = self.nodes[0].sendrawtransaction(tx1b_hex, True)
|
||
|
|
||
|
assert(tx1b_txid in self.nodes[0].getrawmempool())
|
||
|
|
||
|
# 2. Check that absolute fee checks use modified fee.
|
||
9 years ago
|
tx1_outpoint = make_utxo(self.nodes[0], int(1.1*COIN))
|
||
9 years ago
|
|
||
|
tx2a = CTransaction()
|
||
|
tx2a.vin = [CTxIn(tx1_outpoint, nSequence=0)]
|
||
|
tx2a.vout = [CTxOut(1*COIN, CScript([b'a']))]
|
||
|
tx2a_hex = txToHex(tx2a)
|
||
7 years ago
|
self.nodes[0].sendrawtransaction(tx2a_hex, True)
|
||
9 years ago
|
|
||
|
# Lower fee, but we'll prioritise it
|
||
|
tx2b = CTransaction()
|
||
|
tx2b.vin = [CTxIn(tx1_outpoint, nSequence=0)]
|
||
9 years ago
|
tx2b.vout = [CTxOut(int(1.01*COIN), CScript([b'a']))]
|
||
9 years ago
|
tx2b.rehash()
|
||
|
tx2b_hex = txToHex(tx2b)
|
||
|
|
||
|
# Verify tx2b cannot replace tx2a.
|
||
7 years ago
|
assert_raises_rpc_error(-26, "insufficient fee", self.nodes[0].sendrawtransaction, tx2b_hex, True)
|
||
9 years ago
|
|
||
|
# Now prioritise tx2b to have a higher modified fee
|
||
8 years ago
|
self.nodes[0].prioritisetransaction(txid=tx2b.hash, fee_delta=int(0.1*COIN))
|
||
9 years ago
|
|
||
|
# tx2b should now be accepted
|
||
|
tx2b_txid = self.nodes[0].sendrawtransaction(tx2b_hex, True)
|
||
|
|
||
|
assert(tx2b_txid in self.nodes[0].getrawmempool())
|
||
|
|
||
9 years ago
|
def test_rpc(self):
|
||
|
us0 = self.nodes[0].listunspent()[0]
|
||
8 years ago
|
ins = [us0]
|
||
9 years ago
|
outs = {self.nodes[0].getnewaddress() : Decimal(1.0000000)}
|
||
|
rawtx0 = self.nodes[0].createrawtransaction(ins, outs, 0, True)
|
||
|
rawtx1 = self.nodes[0].createrawtransaction(ins, outs, 0, False)
|
||
|
json0 = self.nodes[0].decoderawtransaction(rawtx0)
|
||
|
json1 = self.nodes[0].decoderawtransaction(rawtx1)
|
||
|
assert_equal(json0["vin"][0]["sequence"], 4294967293)
|
||
|
assert_equal(json1["vin"][0]["sequence"], 4294967295)
|
||
|
|
||
|
rawtx2 = self.nodes[0].createrawtransaction([], outs)
|
||
8 years ago
|
frawtx2a = self.nodes[0].fundrawtransaction(rawtx2, {"replaceable": True})
|
||
|
frawtx2b = self.nodes[0].fundrawtransaction(rawtx2, {"replaceable": False})
|
||
9 years ago
|
|
||
|
json0 = self.nodes[0].decoderawtransaction(frawtx2a['hex'])
|
||
|
json1 = self.nodes[0].decoderawtransaction(frawtx2b['hex'])
|
||
|
assert_equal(json0["vin"][0]["sequence"], 4294967293)
|
||
|
assert_equal(json1["vin"][0]["sequence"], 4294967294)
|
||
|
|
||
9 years ago
|
if __name__ == '__main__':
|
||
|
ReplaceByFeeTest().main()
|