2016-03-19 20:58:06 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# Copyright (c) 2014-2016 The Bitcoin Core developers
|
2015-07-15 20:47:45 +02:00
|
|
|
# Distributed under the MIT software license, see the accompanying
|
|
|
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2017-01-18 00:34:40 +01:00
|
|
|
"""Test descendant package tracking code."""
|
2015-07-15 20:47:45 +02:00
|
|
|
|
|
|
|
from test_framework.test_framework import BitcoinTestFramework
|
|
|
|
from test_framework.util import *
|
2016-03-18 01:23:29 +01:00
|
|
|
from test_framework.mininode import COIN
|
2015-07-15 20:47:45 +02:00
|
|
|
|
2015-10-06 04:16:15 +02:00
|
|
|
MAX_ANCESTORS = 25
|
|
|
|
MAX_DESCENDANTS = 25
|
|
|
|
|
2015-07-15 20:47:45 +02:00
|
|
|
class MempoolPackagesTest(BitcoinTestFramework):
|
2016-05-14 13:01:31 +02:00
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
self.num_nodes = 2
|
|
|
|
self.setup_clean_chain = False
|
2015-07-15 20:47:45 +02:00
|
|
|
|
|
|
|
def setup_network(self):
|
|
|
|
self.nodes = []
|
2017-02-24 18:39:33 +01:00
|
|
|
self.nodes.append(start_node(0, self.options.tmpdir, ["-maxorphantx=1000"]))
|
|
|
|
self.nodes.append(start_node(1, self.options.tmpdir, ["-maxorphantx=1000", "-limitancestorcount=5"]))
|
2015-09-23 17:46:36 +02:00
|
|
|
connect_nodes(self.nodes[0], 1)
|
2015-07-15 20:47:45 +02:00
|
|
|
self.is_network_split = False
|
|
|
|
self.sync_all()
|
|
|
|
|
|
|
|
# Build a transaction that spends parent_txid:vout
|
|
|
|
# Return amount sent
|
2015-09-23 17:46:36 +02:00
|
|
|
def chain_transaction(self, node, parent_txid, vout, value, fee, num_outputs):
|
2015-07-15 20:47:45 +02:00
|
|
|
send_value = satoshi_round((value - fee)/num_outputs)
|
|
|
|
inputs = [ {'txid' : parent_txid, 'vout' : vout} ]
|
|
|
|
outputs = {}
|
2016-03-19 20:58:06 +01:00
|
|
|
for i in range(num_outputs):
|
2015-09-23 17:46:36 +02:00
|
|
|
outputs[node.getnewaddress()] = send_value
|
|
|
|
rawtx = node.createrawtransaction(inputs, outputs)
|
|
|
|
signedtx = node.signrawtransaction(rawtx)
|
|
|
|
txid = node.sendrawtransaction(signedtx['hex'])
|
|
|
|
fulltx = node.getrawtransaction(txid, 1)
|
2015-07-15 20:47:45 +02:00
|
|
|
assert(len(fulltx['vout']) == num_outputs) # make sure we didn't generate a change output
|
|
|
|
return (txid, send_value)
|
|
|
|
|
|
|
|
def run_test(self):
|
|
|
|
''' Mine some blocks and have them mature. '''
|
|
|
|
self.nodes[0].generate(101)
|
|
|
|
utxo = self.nodes[0].listunspent(10)
|
|
|
|
txid = utxo[0]['txid']
|
|
|
|
vout = utxo[0]['vout']
|
|
|
|
value = utxo[0]['amount']
|
|
|
|
|
|
|
|
fee = Decimal("0.0001")
|
2015-10-06 04:16:15 +02:00
|
|
|
# MAX_ANCESTORS transactions off a confirmed tx should be fine
|
2015-07-15 20:47:45 +02:00
|
|
|
chain = []
|
2016-03-19 20:58:06 +01:00
|
|
|
for i in range(MAX_ANCESTORS):
|
2015-09-23 17:46:36 +02:00
|
|
|
(txid, sent_value) = self.chain_transaction(self.nodes[0], txid, 0, value, fee, 1)
|
2015-07-15 20:47:45 +02:00
|
|
|
value = sent_value
|
|
|
|
chain.append(txid)
|
|
|
|
|
2015-10-06 04:16:15 +02:00
|
|
|
# Check mempool has MAX_ANCESTORS transactions in it, and descendant
|
2015-07-15 20:47:45 +02:00
|
|
|
# count and fees should look correct
|
|
|
|
mempool = self.nodes[0].getrawmempool(True)
|
2015-10-06 04:16:15 +02:00
|
|
|
assert_equal(len(mempool), MAX_ANCESTORS)
|
2015-07-15 20:47:45 +02:00
|
|
|
descendant_count = 1
|
|
|
|
descendant_fees = 0
|
|
|
|
descendant_size = 0
|
|
|
|
|
2016-01-04 21:15:15 +01:00
|
|
|
descendants = []
|
|
|
|
ancestors = list(chain)
|
2015-07-15 20:47:45 +02:00
|
|
|
for x in reversed(chain):
|
2016-01-04 21:15:15 +01:00
|
|
|
# Check that getmempoolentry is consistent with getrawmempool
|
|
|
|
entry = self.nodes[0].getmempoolentry(x)
|
|
|
|
assert_equal(entry, mempool[x])
|
|
|
|
|
|
|
|
# Check that the descendant calculations are correct
|
2015-07-15 20:47:45 +02:00
|
|
|
assert_equal(mempool[x]['descendantcount'], descendant_count)
|
|
|
|
descendant_fees += mempool[x]['fee']
|
2015-11-19 17:18:28 +01:00
|
|
|
assert_equal(mempool[x]['modifiedfee'], mempool[x]['fee'])
|
2015-12-22 10:43:46 +01:00
|
|
|
assert_equal(mempool[x]['descendantfees'], descendant_fees * COIN)
|
2015-07-15 20:47:45 +02:00
|
|
|
descendant_size += mempool[x]['size']
|
|
|
|
assert_equal(mempool[x]['descendantsize'], descendant_size)
|
|
|
|
descendant_count += 1
|
|
|
|
|
2016-01-04 21:15:15 +01:00
|
|
|
# Check that getmempooldescendants is correct
|
|
|
|
assert_equal(sorted(descendants), sorted(self.nodes[0].getmempooldescendants(x)))
|
|
|
|
descendants.append(x)
|
|
|
|
|
|
|
|
# Check that getmempoolancestors is correct
|
|
|
|
ancestors.remove(x)
|
|
|
|
assert_equal(sorted(ancestors), sorted(self.nodes[0].getmempoolancestors(x)))
|
|
|
|
|
|
|
|
# Check that getmempoolancestors/getmempooldescendants correctly handle verbose=true
|
|
|
|
v_ancestors = self.nodes[0].getmempoolancestors(chain[-1], True)
|
|
|
|
assert_equal(len(v_ancestors), len(chain)-1)
|
|
|
|
for x in v_ancestors.keys():
|
|
|
|
assert_equal(mempool[x], v_ancestors[x])
|
|
|
|
assert(chain[-1] not in v_ancestors.keys())
|
|
|
|
|
|
|
|
v_descendants = self.nodes[0].getmempooldescendants(chain[0], True)
|
|
|
|
assert_equal(len(v_descendants), len(chain)-1)
|
|
|
|
for x in v_descendants.keys():
|
|
|
|
assert_equal(mempool[x], v_descendants[x])
|
|
|
|
assert(chain[0] not in v_descendants.keys())
|
|
|
|
|
2015-11-19 17:18:28 +01:00
|
|
|
# Check that descendant modified fees includes fee deltas from
|
|
|
|
# prioritisetransaction
|
2017-01-20 05:37:15 +01:00
|
|
|
self.nodes[0].prioritisetransaction(chain[-1], 1000)
|
2015-11-19 17:18:28 +01:00
|
|
|
mempool = self.nodes[0].getrawmempool(True)
|
|
|
|
|
|
|
|
descendant_fees = 0
|
|
|
|
for x in reversed(chain):
|
|
|
|
descendant_fees += mempool[x]['fee']
|
2015-12-22 10:43:46 +01:00
|
|
|
assert_equal(mempool[x]['descendantfees'], descendant_fees * COIN + 1000)
|
2015-11-19 17:18:28 +01:00
|
|
|
|
2015-07-15 20:47:45 +02:00
|
|
|
# Adding one more transaction on to the chain should fail.
|
|
|
|
try:
|
2015-09-23 17:46:36 +02:00
|
|
|
self.chain_transaction(self.nodes[0], txid, vout, value, fee, 1)
|
2015-07-15 20:47:45 +02:00
|
|
|
except JSONRPCException as e:
|
2017-03-08 00:46:17 +01:00
|
|
|
self.log.info("too-long-ancestor-chain successfully rejected")
|
2015-07-15 20:47:45 +02:00
|
|
|
|
2015-11-19 17:18:28 +01:00
|
|
|
# Check that prioritising a tx before it's added to the mempool works
|
2016-01-15 02:35:21 +01:00
|
|
|
# First clear the mempool by mining a block.
|
2015-11-19 17:18:28 +01:00
|
|
|
self.nodes[0].generate(1)
|
2016-01-15 02:35:21 +01:00
|
|
|
sync_blocks(self.nodes)
|
|
|
|
assert_equal(len(self.nodes[0].getrawmempool()), 0)
|
|
|
|
# Prioritise a transaction that has been mined, then add it back to the
|
|
|
|
# mempool by using invalidateblock.
|
2017-01-20 05:37:15 +01:00
|
|
|
self.nodes[0].prioritisetransaction(chain[-1], 2000)
|
2015-11-19 17:18:28 +01:00
|
|
|
self.nodes[0].invalidateblock(self.nodes[0].getbestblockhash())
|
2016-01-15 02:35:21 +01:00
|
|
|
# Keep node1's tip synced with node0
|
|
|
|
self.nodes[1].invalidateblock(self.nodes[1].getbestblockhash())
|
|
|
|
|
|
|
|
# Now check that the transaction is in the mempool, with the right modified fee
|
2015-11-19 17:18:28 +01:00
|
|
|
mempool = self.nodes[0].getrawmempool(True)
|
|
|
|
|
|
|
|
descendant_fees = 0
|
|
|
|
for x in reversed(chain):
|
|
|
|
descendant_fees += mempool[x]['fee']
|
|
|
|
if (x == chain[-1]):
|
|
|
|
assert_equal(mempool[x]['modifiedfee'], mempool[x]['fee']+satoshi_round(0.00002))
|
2015-12-22 10:43:46 +01:00
|
|
|
assert_equal(mempool[x]['descendantfees'], descendant_fees * COIN + 2000)
|
2015-11-19 17:18:28 +01:00
|
|
|
|
2015-09-23 17:46:36 +02:00
|
|
|
# TODO: check that node1's mempool is as expected
|
|
|
|
|
2015-07-15 20:47:45 +02:00
|
|
|
# TODO: test ancestor size limits
|
|
|
|
|
|
|
|
# Now test descendant chain limits
|
|
|
|
txid = utxo[1]['txid']
|
|
|
|
value = utxo[1]['amount']
|
|
|
|
vout = utxo[1]['vout']
|
|
|
|
|
|
|
|
transaction_package = []
|
|
|
|
# First create one parent tx with 10 children
|
2015-09-23 17:46:36 +02:00
|
|
|
(txid, sent_value) = self.chain_transaction(self.nodes[0], txid, vout, value, fee, 10)
|
2015-07-15 20:47:45 +02:00
|
|
|
parent_transaction = txid
|
2016-03-19 20:58:06 +01:00
|
|
|
for i in range(10):
|
2015-07-15 20:47:45 +02:00
|
|
|
transaction_package.append({'txid': txid, 'vout': i, 'amount': sent_value})
|
|
|
|
|
2016-03-19 20:58:06 +01:00
|
|
|
for i in range(MAX_DESCENDANTS):
|
2015-07-15 20:47:45 +02:00
|
|
|
utxo = transaction_package.pop(0)
|
|
|
|
try:
|
2015-09-23 17:46:36 +02:00
|
|
|
(txid, sent_value) = self.chain_transaction(self.nodes[0], utxo['txid'], utxo['vout'], utxo['amount'], fee, 10)
|
2016-03-19 20:58:06 +01:00
|
|
|
for j in range(10):
|
2015-07-15 20:47:45 +02:00
|
|
|
transaction_package.append({'txid': txid, 'vout': j, 'amount': sent_value})
|
2015-10-06 04:16:15 +02:00
|
|
|
if i == MAX_DESCENDANTS - 2:
|
2015-07-15 20:47:45 +02:00
|
|
|
mempool = self.nodes[0].getrawmempool(True)
|
2015-10-06 04:16:15 +02:00
|
|
|
assert_equal(mempool[parent_transaction]['descendantcount'], MAX_DESCENDANTS)
|
2015-07-15 20:47:45 +02:00
|
|
|
except JSONRPCException as e:
|
2017-03-08 00:46:17 +01:00
|
|
|
self.log.info(e.error['message'])
|
2015-10-06 04:16:15 +02:00
|
|
|
assert_equal(i, MAX_DESCENDANTS - 1)
|
2017-03-08 00:46:17 +01:00
|
|
|
self.log.info("tx that would create too large descendant package successfully rejected")
|
2015-07-15 20:47:45 +02:00
|
|
|
|
2015-09-23 17:46:36 +02:00
|
|
|
# TODO: check that node1's mempool is as expected
|
|
|
|
|
2015-07-15 20:47:45 +02:00
|
|
|
# TODO: test descendant size limits
|
|
|
|
|
2015-09-23 17:46:36 +02:00
|
|
|
# Test reorg handling
|
|
|
|
# First, the basics:
|
|
|
|
self.nodes[0].generate(1)
|
|
|
|
sync_blocks(self.nodes)
|
|
|
|
self.nodes[1].invalidateblock(self.nodes[0].getbestblockhash())
|
|
|
|
self.nodes[1].reconsiderblock(self.nodes[0].getbestblockhash())
|
|
|
|
|
|
|
|
# Now test the case where node1 has a transaction T in its mempool that
|
|
|
|
# depends on transactions A and B which are in a mined block, and the
|
|
|
|
# block containing A and B is disconnected, AND B is not accepted back
|
|
|
|
# into node1's mempool because its ancestor count is too high.
|
|
|
|
|
|
|
|
# Create 8 transactions, like so:
|
|
|
|
# Tx0 -> Tx1 (vout0)
|
|
|
|
# \--> Tx2 (vout1) -> Tx3 -> Tx4 -> Tx5 -> Tx6 -> Tx7
|
|
|
|
#
|
|
|
|
# Mine them in the next block, then generate a new tx8 that spends
|
|
|
|
# Tx1 and Tx7, and add to node1's mempool, then disconnect the
|
|
|
|
# last block.
|
|
|
|
|
|
|
|
# Create tx0 with 2 outputs
|
|
|
|
utxo = self.nodes[0].listunspent()
|
|
|
|
txid = utxo[0]['txid']
|
|
|
|
value = utxo[0]['amount']
|
|
|
|
vout = utxo[0]['vout']
|
|
|
|
|
|
|
|
send_value = satoshi_round((value - fee)/2)
|
|
|
|
inputs = [ {'txid' : txid, 'vout' : vout} ]
|
|
|
|
outputs = {}
|
2016-03-19 20:58:06 +01:00
|
|
|
for i in range(2):
|
2015-09-23 17:46:36 +02:00
|
|
|
outputs[self.nodes[0].getnewaddress()] = send_value
|
|
|
|
rawtx = self.nodes[0].createrawtransaction(inputs, outputs)
|
|
|
|
signedtx = self.nodes[0].signrawtransaction(rawtx)
|
|
|
|
txid = self.nodes[0].sendrawtransaction(signedtx['hex'])
|
|
|
|
tx0_id = txid
|
|
|
|
value = send_value
|
|
|
|
|
|
|
|
# Create tx1
|
|
|
|
(tx1_id, tx1_value) = self.chain_transaction(self.nodes[0], tx0_id, 0, value, fee, 1)
|
|
|
|
|
|
|
|
# Create tx2-7
|
|
|
|
vout = 1
|
|
|
|
txid = tx0_id
|
2016-03-19 20:58:06 +01:00
|
|
|
for i in range(6):
|
2015-09-23 17:46:36 +02:00
|
|
|
(txid, sent_value) = self.chain_transaction(self.nodes[0], txid, vout, value, fee, 1)
|
|
|
|
vout = 0
|
|
|
|
value = sent_value
|
|
|
|
|
|
|
|
# Mine these in a block
|
|
|
|
self.nodes[0].generate(1)
|
|
|
|
self.sync_all()
|
|
|
|
|
|
|
|
# Now generate tx8, with a big fee
|
|
|
|
inputs = [ {'txid' : tx1_id, 'vout': 0}, {'txid' : txid, 'vout': 0} ]
|
|
|
|
outputs = { self.nodes[0].getnewaddress() : send_value + value - 4*fee }
|
|
|
|
rawtx = self.nodes[0].createrawtransaction(inputs, outputs)
|
|
|
|
signedtx = self.nodes[0].signrawtransaction(rawtx)
|
|
|
|
txid = self.nodes[0].sendrawtransaction(signedtx['hex'])
|
|
|
|
sync_mempools(self.nodes)
|
|
|
|
|
|
|
|
# Now try to disconnect the tip on each node...
|
|
|
|
self.nodes[1].invalidateblock(self.nodes[1].getbestblockhash())
|
|
|
|
self.nodes[0].invalidateblock(self.nodes[0].getbestblockhash())
|
|
|
|
sync_blocks(self.nodes)
|
|
|
|
|
2015-07-15 20:47:45 +02:00
|
|
|
if __name__ == '__main__':
|
|
|
|
MempoolPackagesTest().main()
|