Changeset View
Changeset View
Standalone View
Standalone View
test/functional/abc-magnetic-anomaly-activation.py
- This file was copied from test/functional/abc-transaction-ordering.py.
#!/usr/bin/env python3 | #!/usr/bin/env python3 | ||||
# Copyright (c) 2015-2016 The Bitcoin Core developers | # Copyright (c) 2018 The Bitcoin developers | ||||
# Copyright (c) 2017 The Bitcoin developers | |||||
# Distributed under the MIT software license, see the accompanying | # Distributed under the MIT software license, see the accompanying | ||||
# file COPYING or http://www.opensource.org/licenses/mit-license.php. | # file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
""" | """ | ||||
This test checks that the nod esoftware accepts transactions in | This test checks that simple features of the magnetic anomaly fork | ||||
non topological order once the feature is activated. | activates properly. More complex features are given their own tests. | ||||
""" | """ | ||||
from test_framework.test_framework import ComparisonTestFramework | from test_framework.test_framework import ComparisonTestFramework | ||||
from test_framework.util import assert_equal, assert_raises_rpc_error | from test_framework.util import assert_equal, assert_raises_rpc_error | ||||
from test_framework.comptool import TestManager, TestInstance, RejectResult | from test_framework.comptool import TestManager, TestInstance, RejectResult | ||||
from test_framework.blocktools import * | from test_framework.blocktools import * | ||||
import time | from test_framework.cdefs import MIN_TX_SIZE | ||||
from test_framework.key import CECKey | |||||
from test_framework.script import * | |||||
from collections import deque | from collections import deque | ||||
# far into the future | # far into the future | ||||
MAGNETIC_ANOMALY_START_TIME = 2000000000 | MAGNETIC_ANOMALY_START_TIME = 2000000000 | ||||
class PreviousSpendableOutput(): | class PreviousSpendableOutput(): | ||||
def __init__(self, tx=CTransaction(), n=-1): | def __init__(self, tx=CTransaction(), n=-1): | ||||
self.tx = tx | self.tx = tx | ||||
self.n = n # the output we're spending | self.n = n # the output we're spending | ||||
class TransactionOrderingTest(ComparisonTestFramework): | class MagneticAnomalyActivationTest(ComparisonTestFramework): | ||||
# Can either run this test as 1 node with expected answers, or two and compare them. | |||||
# Change the "outcome" variable from each TestInstance object to only do | |||||
# the comparison. | |||||
def set_test_params(self): | def set_test_params(self): | ||||
self.num_nodes = 1 | self.num_nodes = 1 | ||||
self.setup_clean_chain = True | self.setup_clean_chain = True | ||||
self.block_heights = {} | self.block_heights = {} | ||||
self.tip = None | self.tip = None | ||||
self.blocks = {} | self.blocks = {} | ||||
self.extra_args = [['-whitelist=127.0.0.1', | self.extra_args = [['-whitelist=127.0.0.1', | ||||
"-magneticanomalyactivationtime=%d" % MAGNETIC_ANOMALY_START_TIME, | "-magneticanomalyactivationtime=%d" % MAGNETIC_ANOMALY_START_TIME, | ||||
"-replayprotectionactivationtime=%d" % (2 * MAGNETIC_ANOMALY_START_TIME)]] | "-replayprotectionactivationtime=%d" % (2 * MAGNETIC_ANOMALY_START_TIME)]] | ||||
def run_test(self): | def run_test(self): | ||||
self.test = TestManager(self, self.options.tmpdir) | self.test = TestManager(self, self.options.tmpdir) | ||||
self.test.add_all_connections(self.nodes) | self.test.add_all_connections(self.nodes) | ||||
# Start up network handling in another thread | # Start up network handling in another thread | ||||
NetworkThread().start() | NetworkThread().start() | ||||
# Set the blocksize to 2MB as initial condition | # Set the blocksize to 2MB as initial condition | ||||
self.nodes[0].setmocktime(MAGNETIC_ANOMALY_START_TIME) | self.nodes[0].setmocktime(MAGNETIC_ANOMALY_START_TIME) | ||||
self.test.run() | self.test.run() | ||||
def add_transactions_to_block(self, block, tx_list): | def add_transactions_to_block(self, block, tx_list): | ||||
[tx.rehash() for tx in tx_list] | [tx.rehash() for tx in tx_list] | ||||
block.vtx.extend(tx_list) | block.vtx.extend(tx_list) | ||||
# this is a little handier to use than the version in blocktools.py | def next_block(self, number, spend=None, tx_size=0): | ||||
def create_tx(self, spend, value, script=CScript([OP_TRUE])): | |||||
tx = create_transaction(spend.tx, spend.n, b"", value, script) | |||||
return tx | |||||
def next_block(self, number, spend=None, tx_count=0): | |||||
if self.tip == None: | if self.tip == None: | ||||
base_block_hash = self.genesis_hash | base_block_hash = self.genesis_hash | ||||
block_time = int(time.time()) + 1 | block_time = int(time.time()) + 1 | ||||
else: | else: | ||||
base_block_hash = self.tip.sha256 | base_block_hash = self.tip.sha256 | ||||
block_time = self.tip.nTime + 1 | block_time = self.tip.nTime + 1 | ||||
# First create the coinbase | # First create the coinbase | ||||
height = self.block_heights[base_block_hash] + 1 | height = self.block_heights[base_block_hash] + 1 | ||||
coinbase = create_coinbase(height) | coinbase = create_coinbase(height) | ||||
coinbase.rehash() | coinbase.rehash() | ||||
if spend == None: | if spend == None: | ||||
# We need to have something to spend to fill the block. | # We need to have something to spend to fill the block. | ||||
block = create_block(base_block_hash, coinbase, block_time) | block = create_block(base_block_hash, coinbase, block_time) | ||||
else: | else: | ||||
# all but one satoshi to fees | # all but one satoshi to fees | ||||
coinbase.vout[0].nValue += spend.tx.vout[spend.n].nValue - 1 | coinbase.vout[0].nValue += spend.tx.vout[spend.n].nValue - 1 | ||||
coinbase.rehash() | coinbase.rehash() | ||||
block = create_block(base_block_hash, coinbase, block_time) | block = create_block(base_block_hash, coinbase, block_time) | ||||
# Make sure we have plenty enough to spend going forward. | # Make sure we have plenty enough to spend going forward. | ||||
spendable_outputs = deque([spend]) | spendable_outputs = deque([spend]) | ||||
def get_base_transaction(): | |||||
# Create the new transaction | # Create the new transaction | ||||
tx = CTransaction() | tx = CTransaction() | ||||
# Spend from one of the spendable outputs | # Spend from one of the spendable outputs | ||||
spend = spendable_outputs.popleft() | spend = spendable_outputs.popleft() | ||||
tx.vin.append(CTxIn(COutPoint(spend.tx.sha256, spend.n))) | tx.vin.append(CTxIn(COutPoint(spend.tx.sha256, spend.n))) | ||||
# Add spendable outputs | # Add spendable outputs | ||||
for i in range(4): | for i in range(2): | ||||
tx.vout.append(CTxOut(0, CScript([OP_TRUE]))) | tx.vout.append(CTxOut(0, CScript([OP_TRUE]))) | ||||
spendable_outputs.append(PreviousSpendableOutput(tx, i)) | spendable_outputs.append(PreviousSpendableOutput(tx, i)) | ||||
return tx | # Put some random data into the transaction in order to randomize ids. | ||||
if tx_size == 0: | |||||
tx = get_base_transaction() | tx.vout.append( | ||||
CTxOut(0, CScript([random.getrandbits(8), OP_RETURN]))) | |||||
else: | |||||
tx.vout.append( | |||||
CTxOut(0, CScript([random.getrandbits(8 * (tx_size - 82) - 1), OP_RETURN]))) | |||||
assert_equal(len(tx.serialize()), tx_size) | |||||
# Make it the same format as transaction added for padding and save the size. | # Make it the same format as transaction added for padding and save the size. | ||||
# It's missing the padding output, so we add a constant to account for it. | # It's missing the padding output, so we add a constant to account for it. | ||||
tx.rehash() | tx.rehash() | ||||
base_tx_size = len(tx.serialize()) + 18 | |||||
# Put some random data into the first transaction of the chain to randomize ids. | |||||
tx.vout.append( | |||||
CTxOut(0, CScript([random.randint(0, 256), OP_RETURN]))) | |||||
# Add the transaction to the block | # Add the transaction to the block | ||||
self.add_transactions_to_block(block, [tx]) | self.add_transactions_to_block(block, [tx]) | ||||
# If we have a transaction count requirement, just fill the block until we get there | |||||
while len(block.vtx) < tx_count: | |||||
# Create the new transaction and add it. | |||||
tx = get_base_transaction() | |||||
self.add_transactions_to_block(block, [tx]) | |||||
# Now that we added a bunch of transaction, we need to recompute | # Now that we added a bunch of transaction, we need to recompute | ||||
# the merkle root. | # the merkle root. | ||||
block.hashMerkleRoot = block.calc_merkle_root() | block.hashMerkleRoot = block.calc_merkle_root() | ||||
if tx_count > 0: | |||||
assert_equal(len(block.vtx), tx_count) | |||||
# Do PoW, which is cheap on regnet | # Do PoW, which is cheap on regnet | ||||
block.solve() | block.solve() | ||||
self.tip = block | self.tip = block | ||||
self.block_heights[block.sha256] = height | self.block_heights[block.sha256] = height | ||||
assert number not in self.blocks | assert number not in self.blocks | ||||
self.blocks[number] = block | self.blocks[number] = block | ||||
return block | return block | ||||
def get_tests(self): | def get_tests(self): | ||||
node = self.nodes[0] | node = self.nodes[0] | ||||
self.genesis_hash = int(node.getbestblockhash(), 16) | self.genesis_hash = int(node.getbestblockhash(), 16) | ||||
self.block_heights[self.genesis_hash] = 0 | self.block_heights[self.genesis_hash] = 0 | ||||
spendable_outputs = [] | spendable_outputs = [] | ||||
def get_tests(self): | |||||
node = self.nodes[0] | |||||
self.genesis_hash = int(node.getbestblockhash(), 16) | |||||
self.block_heights[self.genesis_hash] = 0 | |||||
spendable_outputs = [] | |||||
# save the current tip so it can be spent by a later block | # save the current tip so it can be spent by a later block | ||||
def save_spendable_output(): | def save_spendable_output(): | ||||
spendable_outputs.append(self.tip) | spendable_outputs.append(self.tip) | ||||
# get an output that we previously marked as spendable | # get an output that we previously marked as spendable | ||||
def get_spendable_output(): | def get_spendable_output(): | ||||
return PreviousSpendableOutput(spendable_outputs.pop(0).vtx[0], 0) | return PreviousSpendableOutput(spendable_outputs.pop(0).vtx[0], 0) | ||||
▲ Show 20 Lines • Show All 65 Lines • ▼ Show 20 Lines | def get_tests(self): | ||||
block(5100 + i) | block(5100 + i) | ||||
test.blocks_and_transactions.append([self.tip, True]) | test.blocks_and_transactions.append([self.tip, True]) | ||||
yield test | yield test | ||||
# Check that the MTP is just before the configured fork point. | # Check that the MTP is just before the configured fork point. | ||||
assert_equal(node.getblockheader(node.getbestblockhash())['mediantime'], | assert_equal(node.getblockheader(node.getbestblockhash())['mediantime'], | ||||
MAGNETIC_ANOMALY_START_TIME - 1) | MAGNETIC_ANOMALY_START_TIME - 1) | ||||
# Before we activate the Nov 15, 2018 HF, transaction order is respected. | # Check that block with small transactions are still accepted. | ||||
def ordered_block(block_number, spend): | small_tx_block = block(4444, out[0], MIN_TX_SIZE - 1) | ||||
b = block(block_number, spend=spend, tx_count=16) | assert_equal(len(small_tx_block.vtx[1].serialize()), MIN_TX_SIZE - 1) | ||||
b.vtx = [b.vtx[0]] + sorted(b.vtx[1:], key=lambda tx: tx.get_id()) | |||||
update_block(block_number) | |||||
return b | |||||
ordered_block(4444, out[16]) | |||||
yield rejected(RejectResult(16, b'bad-txns-inputs-missingorspent')) | |||||
# Rewind bad block. | |||||
tip(5104) | |||||
# Activate the Nov 15, 2018 HF | |||||
block(5556, out[16], tx_count=16) | |||||
yield accepted() | yield accepted() | ||||
# Now MTP is exactly the fork time. Bigger blocks are now accepted. | # Now MTP is exactly the fork time. Small transaction are now rejected. | ||||
assert_equal(node.getblockheader(node.getbestblockhash())['mediantime'], | assert_equal(node.getblockheader(node.getbestblockhash())['mediantime'], | ||||
MAGNETIC_ANOMALY_START_TIME) | MAGNETIC_ANOMALY_START_TIME) | ||||
# Block with regular ordering are now rejected. | # Now that the for activated, it is not possible to have | ||||
block(5557, out[17], tx_count=16) | # small transactions anymore. | ||||
yield rejected(RejectResult(16, b'tx-ordering')) | small_tx_block = block(4445, out[1], MIN_TX_SIZE - 1) | ||||
assert_equal(len(small_tx_block.vtx[1].serialize()), MIN_TX_SIZE - 1) | |||||
yield rejected(RejectResult(16, b'bad-txns-undersize')) | |||||
# Rewind bad block. | # Rewind bad block. | ||||
tip(5556) | tip(4444) | ||||
# Now that the fork activated, we need to order transaction per txid. | # But large transactions are still ok. | ||||
ordered_block(4445, out[17]) | large_tx_block = block(4446, out[1], MIN_TX_SIZE) | ||||
assert_equal(len(large_tx_block.vtx[1].serialize()), MIN_TX_SIZE) | |||||
yield accepted() | yield accepted() | ||||
# Invalidate the best block and make sure we are back at the fork point. | |||||
ctorblockhash = node.getbestblockhash() | |||||
node.invalidateblock(ctorblockhash) | |||||
forkblockhash = node.getbestblockhash() | |||||
assert(forkblockhash != ctorblockhash) | |||||
assert_equal(node.getblockheader(forkblockhash)[ | |||||
'mediantime'], MAGNETIC_ANOMALY_START_TIME) | |||||
node.generate(1) | |||||
generatedblockhash = node.getbestblockhash() | |||||
assert(forkblockhash != generatedblockhash) | |||||
if __name__ == '__main__': | if __name__ == '__main__': | ||||
TransactionOrderingTest().main() | MagneticAnomalyActivationTest().main() |