diff --git a/test/functional/abc-checkdatasig-activation.py b/test/functional/abc-checkdatasig-activation.py deleted file mode 100755 index cf79e3b01..000000000 --- a/test/functional/abc-checkdatasig-activation.py +++ /dev/null @@ -1,174 +0,0 @@ -#!/usr/bin/env python3 -# Copyright (c) 2018 The Bitcoin developers -# Distributed under the MIT software license, see the accompanying -# file COPYING or http://www.opensource.org/licenses/mit-license.php. -""" -This test checks activation of OP_CHECKDATASIG -""" - -from test_framework.test_framework import ComparisonTestFramework -from test_framework.util import satoshi_round, assert_equal, assert_raises_rpc_error -from test_framework.comptool import TestManager, TestInstance, RejectResult -from test_framework.blocktools import * -from test_framework.script import * - -# far into the future -MAGNETIC_ANOMALY_START_TIME = 2000000000 - -# Error due to invalid opcodes -BAD_OPCODE_ERROR = b'mandatory-script-verify-flag-failed (Opcode missing or not understood)' -RPC_BAD_OPCODE_ERROR = "16: " + \ - BAD_OPCODE_ERROR.decode("utf-8") - - -class PreviousSpendableOutput(): - - def __init__(self, tx=CTransaction(), n=-1): - self.tx = tx - self.n = n # the output we're spending - - -class CheckDataSigActivationTest(ComparisonTestFramework): - - def set_test_params(self): - self.num_nodes = 1 - self.setup_clean_chain = True - self.extra_args = [['-whitelist=127.0.0.1', - "-magneticanomalyactivationtime=%d" % MAGNETIC_ANOMALY_START_TIME, - "-replayprotectionactivationtime=%d" % (2 * MAGNETIC_ANOMALY_START_TIME)]] - - def create_checkdatasig_tx(self, count): - node = self.nodes[0] - utxos = node.listunspent() - assert(len(utxos) > 0) - utxo = utxos[0] - tx = CTransaction() - value = int(satoshi_round(utxo["amount"]) * COIN) // count - tx.vin = [CTxIn(COutPoint(int(utxo["txid"], 16), utxo["vout"]))] - tx.vout = [] - signature = bytearray.fromhex( - '30440220256c12175e809381f97637933ed6ab97737d263eaaebca6add21bced67fd12a402205ce29ecc1369d6fc1b51977ed38faaf41119e3be1d7edfafd7cfaf0b6061bd07') - message = bytearray.fromhex('') - pubkey = bytearray.fromhex( - '038282263212c609d9ea2a6e3e172de238d8c39cabd5ac1ca10646e23fd5f51508') - for _ in range(count): - tx.vout.append(CTxOut(value, CScript( - [signature, message, pubkey, OP_CHECKDATASIG]))) - tx.vout[0].nValue -= node.calculate_fee(tx) - tx_signed = node.signrawtransaction(ToHex(tx))["hex"] - return tx_signed - - def run_test(self): - self.test = TestManager(self, self.options.tmpdir) - self.test.add_all_connections(self.nodes) - # Start up network handling in another thread - NetworkThread().start() - self.test.run() - - def get_tests(self): - node = self.nodes[0] - - # First, we generate some coins to spend. - node.generate(125) - - # Create various outputs using the OP_CHECKDATASIG - # to check for activation. - tx_hex = self.create_checkdatasig_tx(25) - txid = node.sendrawtransaction(tx_hex) - assert(txid in set(node.getrawmempool())) - - node.generate(1) - assert(txid not in set(node.getrawmempool())) - - # register the spendable outputs. - tx = FromHex(CTransaction(), tx_hex) - tx.rehash() - spendable_checkdatasigs = [PreviousSpendableOutput(tx, i) - for i in range(len(tx.vout))] - - def spend_checkdatasig(): - outpoint = spendable_checkdatasigs.pop() - out = outpoint.tx.vout[outpoint.n] - tx = CTransaction() - tx.vin = [CTxIn(COutPoint(outpoint.tx.sha256, outpoint.n))] - tx.vout = [CTxOut(out.nValue, CScript([])), - CTxOut(0, CScript([random.getrandbits(800), OP_RETURN]))] - tx.vout[0].nValue -= node.calculate_fee(tx) - tx.rehash() - return tx - - # Check that transactions using checkdatasig are not accepted yet. - self.log.info("Try to use the checkdatasig opcodes before activation") - - tx0 = spend_checkdatasig() - tx0_hex = ToHex(tx0) - assert_raises_rpc_error(-26, RPC_BAD_OPCODE_ERROR, - node.sendrawtransaction, tx0_hex) - - # Push MTP forward just before activation. - self.log.info("Pushing MTP just before the activation and check again") - node.setmocktime(MAGNETIC_ANOMALY_START_TIME) - - # returns a test case that asserts that the current tip was accepted - def accepted(tip): - return TestInstance([[tip, True]]) - - # returns a test case that asserts that the current tip was rejected - def rejected(tip, reject=None): - if reject is None: - return TestInstance([[tip, False]]) - else: - return TestInstance([[tip, reject]]) - - def next_block(block_time): - # get block height - blockchaininfo = node.getblockchaininfo() - height = int(blockchaininfo['blocks']) - - # create the block - coinbase = create_coinbase(height) - coinbase.rehash() - block = create_block( - int(node.getbestblockhash(), 16), coinbase, block_time) - - # Do PoW, which is cheap on regnet - block.solve() - return block - - for i in range(6): - b = next_block(MAGNETIC_ANOMALY_START_TIME + i - 1) - yield accepted(b) - - # Check again just before the activation time - assert_equal(node.getblockheader(node.getbestblockhash())['mediantime'], - MAGNETIC_ANOMALY_START_TIME - 1) - assert_raises_rpc_error(-26, RPC_BAD_OPCODE_ERROR, - node.sendrawtransaction, tx0_hex) - - def add_tx(block, tx): - block.vtx.append(tx) - block.hashMerkleRoot = block.calc_merkle_root() - block.solve() - - b = next_block(MAGNETIC_ANOMALY_START_TIME + 6) - add_tx(b, tx0) - yield rejected(b, RejectResult(16, b'blk-bad-inputs')) - - self.log.info("Activates checkdatasig") - fork_block = next_block(MAGNETIC_ANOMALY_START_TIME + 6) - yield accepted(fork_block) - - assert_equal(node.getblockheader(node.getbestblockhash())['mediantime'], - MAGNETIC_ANOMALY_START_TIME) - - tx0id = node.sendrawtransaction(tx0_hex) - assert(tx0id in set(node.getrawmempool())) - - # Transactions can also be included in blocks. - magneticanomalyblock = next_block(MAGNETIC_ANOMALY_START_TIME + 7) - add_tx(magneticanomalyblock, tx0) - yield accepted(magneticanomalyblock) - - -if __name__ == '__main__': - CheckDataSigActivationTest().main() diff --git a/test/functional/abc-magnetic-anomaly-activation.py b/test/functional/abc-magnetic-anomaly-activation.py deleted file mode 100755 index 944471134..000000000 --- a/test/functional/abc-magnetic-anomaly-activation.py +++ /dev/null @@ -1,277 +0,0 @@ -#!/usr/bin/env python3 -# Copyright (c) 2018 The Bitcoin developers -# Distributed under the MIT software license, see the accompanying -# file COPYING or http://www.opensource.org/licenses/mit-license.php. -""" -This test checks that simple features of the magnetic anomaly fork -activates properly. More complex features are given their own tests. -""" - -from test_framework.test_framework import ComparisonTestFramework -from test_framework.util import assert_equal, assert_raises_rpc_error -from test_framework.comptool import TestManager, TestInstance, RejectResult -from test_framework.blocktools import create_coinbase, create_block -from test_framework.mininode import * -from test_framework.script import * -from test_framework.cdefs import MIN_TX_SIZE -from collections import deque - -# far into the future -MAGNETIC_ANOMALY_START_TIME = 2000000000 -RPC_VERIFY_REJECTED = -26 - - -class PreviousSpendableOutput(): - - def __init__(self, tx=CTransaction(), n=-1): - self.tx = tx - self.n = n # the output we're spending - - -class MagneticAnomalyActivationTest(ComparisonTestFramework): - - def set_test_params(self): - self.num_nodes = 1 - self.setup_clean_chain = True - self.block_heights = {} - self.tip = None - self.blocks = {} - self.extra_args = [['-whitelist=127.0.0.1', - "-magneticanomalyactivationtime=%d" % MAGNETIC_ANOMALY_START_TIME, - "-replayprotectionactivationtime=%d" % (2 * MAGNETIC_ANOMALY_START_TIME)]] - - def run_test(self): - self.test = TestManager(self, self.options.tmpdir) - self.test.add_all_connections(self.nodes) - # Start up network handling in another thread - NetworkThread().start() - # Set the blocksize to 2MB as initial condition - self.nodes[0].setmocktime(MAGNETIC_ANOMALY_START_TIME) - self.test.run() - - def add_transactions_to_block(self, block, tx_list): - [tx.rehash() for tx in tx_list] - block.vtx.extend(tx_list) - - def new_transaction(self, spend, tx_size=0, pushonly=True, cleanstack=True): - tx = CTransaction() - # Make sure we have plenty enough to spend going forward. - spendable_outputs = deque([spend]) - - # Spend from one of the spendable outputs - spend = spendable_outputs.popleft() - tx.vin.append(CTxIn(COutPoint(spend.tx.sha256, spend.n))) - extra_ops = [] - if pushonly == False: - extra_ops += [OP_TRUE, OP_DROP] - if cleanstack == False: - extra_ops += [OP_TRUE] - tx.vin[0].scriptSig = CScript(extra_ops) - - # Add spendable outputs - for i in range(2): - tx.vout.append(CTxOut(0, CScript([OP_TRUE]))) - spendable_outputs.append(PreviousSpendableOutput(tx, i)) - - # Put some random data into the transaction in order to randomize ids. - if tx_size == 0: - tx.vout.append( - CTxOut(0, CScript([random.getrandbits(8), OP_RETURN]))) - else: - # Create an input to pad the transaction. - tx.vout.append(CTxOut(0, CScript([OP_RETURN]))) - - # Estimate the size of the padding. - push_size = tx_size - len(tx.serialize()) - 1 - - # Because several field are of variable size, we grow the push slowly - # up to the requested size. - while len(tx.serialize()) < tx_size: - # Ensure the padding has a left most bit on, so it's - # exactly the correct number of bits. - padding = random.randrange( - 1 << 8 * push_size - 2, 1 << 8 * push_size - 1) - tx.vout[2] = CTxOut(0, CScript([padding, OP_RETURN])) - push_size += 1 - - assert_equal(len(tx.serialize()), tx_size) - - tx.rehash() - return tx - - def next_block(self, number, spend_tx=None): - if self.tip == None: - base_block_hash = self.genesis_hash - import time - block_time = int(time.time()) + 1 - else: - base_block_hash = self.tip.sha256 - block_time = self.tip.nTime + 1 - # First create the coinbase - height = self.block_heights[base_block_hash] + 1 - coinbase = create_coinbase(height) - coinbase.rehash() - if spend_tx == None: - # We need to have something to spend to fill the block. - block = create_block(base_block_hash, coinbase, block_time) - else: - # All but one satoshi to fees - #coinbase.vout[0].nValue += spend.tx.vout[spend.n].nValue - 1 - coinbase.vout[0].nValue += spend_tx.vin[0].prevout.n - 1 - coinbase.rehash() - block = create_block(base_block_hash, coinbase, block_time) - - # Add the transaction to the block - self.add_transactions_to_block(block, [spend_tx]) - - # Now that we added a bunch of transactions, we need to recompute - # the merkle root. - block.hashMerkleRoot = block.calc_merkle_root() - - # Do PoW, which is cheap on regnet - block.solve() - self.tip = block - self.block_heights[block.sha256] = height - assert number not in self.blocks - self.blocks[number] = block - return block - - 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 - def save_spendable_output(): - spendable_outputs.append(self.tip) - - # get an output that we previously marked as spendable - def get_spendable_output(): - return PreviousSpendableOutput(spendable_outputs.pop(0).vtx[0], 0) - - # returns a test case that asserts that the current tip was accepted - def accepted(): - return TestInstance([[self.tip, True]]) - - # returns a test case that asserts that the current tip was rejected - def rejected(reject=None): - if reject is None: - return TestInstance([[self.tip, False]]) - else: - return TestInstance([[self.tip, reject]]) - - # move the tip back to a previous block - def tip(number): - self.tip = self.blocks[number] - - # adds transactions to the block and updates state - def update_block(block_number, new_transactions=[]): - block = self.blocks[block_number] - self.add_transactions_to_block(block, new_transactions) - old_sha256 = block.sha256 - block.hashMerkleRoot = block.calc_merkle_root() - block.solve() - # Update the internal state just like in next_block - self.tip = block - if block.sha256 != old_sha256: - self.block_heights[block.sha256] = self.block_heights[old_sha256] - del self.block_heights[old_sha256] - self.blocks[block_number] = block - return block - - # shorthand for functions - block = self.next_block - transaction = self.new_transaction - - # Create a new block - block(0) - save_spendable_output() - yield accepted() - - # Now we need that block to mature so we can spend the coinbase. - test = TestInstance(sync_every_block=False) - for i in range(99): - block(5000 + i) - test.blocks_and_transactions.append([self.tip, True]) - save_spendable_output() - yield test - - # collect spendable outputs now to avoid cluttering the code later on - out = [] - for i in range(100): - out.append(get_spendable_output()) - - # Let's build some blocks and test them. - for i in range(15): - n = i + 1 - block(n) - yield accepted() - - # Start moving MTP forward - bfork = block(5555) - bfork.nTime = MAGNETIC_ANOMALY_START_TIME - 1 - update_block(5555) - yield accepted() - - # Get to one block of the Nov 15, 2018 HF activation - for i in range(5): - block(5100 + i) - test.blocks_and_transactions.append([self.tip, True]) - yield test - - # Check that the MTP is just before the configured fork point. - assert_equal(node.getblockheader(node.getbestblockhash())['mediantime'], - MAGNETIC_ANOMALY_START_TIME - 1) - - # Check that block with small transactions, non push only signatures and - # non clean stack are still accepted. - small_tx_block = block(4444, - transaction(out[0], MIN_TX_SIZE - 1, pushonly=False, cleanstack=False)) - assert_equal(len(small_tx_block.vtx[1].serialize()), MIN_TX_SIZE - 1) - yield accepted() - - # Now MTP is exactly the fork time. Small transaction are now rejected. - assert_equal(node.getblockheader(node.getbestblockhash())['mediantime'], - MAGNETIC_ANOMALY_START_TIME) - - # Now that the for activated, it is not possible to have - # small transactions anymore. - small_tx_block = block(4445, transaction(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. - tip(4444) - - # Now that the for activated, it is not possible to have - # non push only transactions. - non_pushonly_tx_block = block(4446, - transaction(out[1], MIN_TX_SIZE, pushonly=False)) - yield rejected(RejectResult(16, b'blk-bad-inputs')) - - # Rewind bad block. - tip(4444) - - # Now that the for activated, it is not possible to have - # non clean stack transactions. - non_cleanstack_tx_block = block(4447, - transaction(out[1], MIN_TX_SIZE, cleanstack=False)) - yield rejected(RejectResult(16, b'blk-bad-inputs')) - - # Rewind bad block. - tip(4444) - - # Verfiy that ATMP doesn't accept undersize transactions - undersized_tx = transaction(out[1], MIN_TX_SIZE - 1) - assert_raises_rpc_error(RPC_VERIFY_REJECTED, "bad-txns-undersize", - node.sendrawtransaction, ToHex(undersized_tx), True) - - # But large transactions are still ok. - large_tx_block = block(3333, transaction(out[1], MIN_TX_SIZE)) - assert_equal(len(large_tx_block.vtx[1].serialize()), MIN_TX_SIZE) - yield accepted() - - -if __name__ == '__main__': - MagneticAnomalyActivationTest().main() diff --git a/test/functional/abc-magnetic-anomaly-mining.py b/test/functional/abc-magnetic-anomaly-mining.py index 4847180f0..d3a39502d 100755 --- a/test/functional/abc-magnetic-anomaly-mining.py +++ b/test/functional/abc-magnetic-anomaly-mining.py @@ -1,122 +1,121 @@ #!/usr/bin/env python3 # Copyright (c) 2014-2016 The Bitcoin Core developers # Distributed under the MIT software license, see the accompanying # file COPYING or http://www.opensource.org/licenses/mit-license.php. """ Test that mining RPC continues to supply correct transaction metadata after the Nov 2018 protocol upgrade which engages canonical transaction ordering """ import time import random import decimal from test_framework.blocktools import create_coinbase from test_framework.test_framework import BitcoinTestFramework from test_framework.util import * class CTORMiningTest(BitcoinTestFramework): def set_test_params(self): # Setup two nodes so we can getblocktemplate # it errors out if it is not connected to other nodes self.num_nodes = 2 self.setup_clean_chain = True self.block_heights = {} self.tip = None self.blocks = {} self.mocktime = int(time.time()) - 600 * 100 extra_arg = ['-spendzeroconfchange=0', '-whitelist=127.0.0.1', - "-magneticanomalyactivationtime=%d" % self.mocktime, "-replayprotectionactivationtime=%d" % (10 * self.mocktime)] self.extra_args = [extra_arg, extra_arg] def run_test(self): mining_node = self.nodes[0] # Helper for updating the times def update_time(): mining_node.setmocktime(self.mocktime) self.mocktime = self.mocktime + 600 mining_node.getnewaddress() # Generate some unspent utxos and also # activate magnetic anomaly for x in range(150): update_time() mining_node.generate(1) update_time() unspent = mining_node.listunspent() transactions = {} # Spend all our coinbases while len(unspent): inputs = [] # Grab a random number of inputs for _ in range(random.randrange(1, 5)): txin = unspent.pop() inputs.append({ 'txid': txin['txid'], 'vout': 0 # This is a coinbase }) if len(unspent) == 0: break outputs = {} # Calculate a unique fee for this transaction fee = decimal.Decimal(random.randint( 1000, 2000)) / decimal.Decimal(1e8) # Spend to the same number of outputs as inputs, so we can leave # the amounts unchanged and avoid rounding errors. # # NOTE: There will be 1 sigop per output (which equals the number # of inputs now). We need this randomization to ensure the # numbers are properly following the transactions in the block # template metadata addr = "" for _ in range(len(inputs)): addr = mining_node.getnewaddress() output = { # 50 BCH per coinbase addr: decimal.Decimal(50) } outputs.update(output) # Take the fee off the last output to avoid rounding errors we # need the exact fee later for assertions outputs[addr] -= fee rawtx = mining_node.createrawtransaction(inputs, outputs) signedtx = mining_node.signrawtransaction(rawtx) txid = mining_node.sendrawtransaction(signedtx['hex']) # number of outputs is the same as the number of sigops in this # case transactions.update({txid: {'fee': fee, 'sigops': len(outputs)}}) tmpl = mining_node.getblocktemplate() assert 'proposal' in tmpl['capabilities'] assert 'coinbasetxn' not in tmpl # Check the template transaction metadata and ordering last_txid = 0 for txn in tmpl['transactions'][1:]: txid = txn['txid'] txnMetadata = transactions[txid] expectedFeeSats = int(txnMetadata['fee'] * 10**8) expectedSigOps = txnMetadata['sigops'] txid_decoded = int(txid, 16) # Assert we got the expected metadata assert(expectedFeeSats == txn['fee']) assert(expectedSigOps == txn['sigops']) # Assert transaction ids are in order assert(last_txid == 0 or last_txid < txid_decoded) last_txid = txid_decoded if __name__ == '__main__': CTORMiningTest().main() diff --git a/test/functional/abc-replay-protection.py b/test/functional/abc-replay-protection.py index 3ea638ff3..435f61aac 100755 --- a/test/functional/abc-replay-protection.py +++ b/test/functional/abc-replay-protection.py @@ -1,283 +1,282 @@ #!/usr/bin/env python3 # Copyright (c) 2015-2016 The Bitcoin Core developers # Copyright (c) 2017 The Bitcoin developers # Distributed under the MIT software license, see the accompanying # file COPYING or http://www.opensource.org/licenses/mit-license.php. """ This test checks activation of UAHF and the different consensus related to this activation. It is derived from the much more complex p2p-fullblocktest. """ from test_framework.test_framework import ComparisonTestFramework from test_framework.util import assert_equal, assert_raises_rpc_error from test_framework.comptool import TestManager, TestInstance, RejectResult from test_framework.blocktools import * import time from test_framework.key import CECKey from test_framework.script import * # far into the future REPLAY_PROTECTION_START_TIME = 2000000000 -MAGNETIC_ANOMALY_START_TIME = 4000000000 # Error due to invalid signature INVALID_SIGNATURE_ERROR = b'mandatory-script-verify-flag-failed (Signature must be zero for failed CHECK(MULTI)SIG operation)' RPC_INVALID_SIGNATURE_ERROR = "16: " + \ INVALID_SIGNATURE_ERROR.decode("utf-8") class PreviousSpendableOutput(object): def __init__(self, tx=CTransaction(), n=-1): self.tx = tx self.n = n # the output we're spending class ReplayProtectionTest(ComparisonTestFramework): def set_test_params(self): self.num_nodes = 1 self.setup_clean_chain = True self.block_heights = {} self.tip = None self.blocks = {} self.extra_args = [['-whitelist=127.0.0.1', - "-magneticanomalyactivationtime=%d" % MAGNETIC_ANOMALY_START_TIME, "-replayprotectionactivationtime=%d" % REPLAY_PROTECTION_START_TIME]] def run_test(self): self.test = TestManager(self, self.options.tmpdir) self.test.add_all_connections(self.nodes) # Start up network handling in another thread NetworkThread().start() self.nodes[0].setmocktime(REPLAY_PROTECTION_START_TIME) self.test.run() def next_block(self, number): if self.tip == None: base_block_hash = self.genesis_hash block_time = int(time.time()) + 1 else: base_block_hash = self.tip.sha256 block_time = self.tip.nTime + 1 # First create the coinbase height = self.block_heights[base_block_hash] + 1 coinbase = create_coinbase(height) coinbase.rehash() block = create_block(base_block_hash, coinbase, block_time) # Do PoW, which is cheap on regnet block.solve() self.tip = block self.block_heights[block.sha256] = height assert number not in self.blocks self.blocks[number] = block return block def get_tests(self): self.genesis_hash = int(self.nodes[0].getbestblockhash(), 16) self.block_heights[self.genesis_hash] = 0 spendable_outputs = [] # save the current tip so it can be spent by a later block def save_spendable_output(): spendable_outputs.append(self.tip) # get an output that we previously marked as spendable def get_spendable_output(): return PreviousSpendableOutput(spendable_outputs.pop(0).vtx[0], 0) # returns a test case that asserts that the current tip was accepted def accepted(): return TestInstance([[self.tip, True]]) # returns a test case that asserts that the current tip was rejected def rejected(reject=None): if reject is None: return TestInstance([[self.tip, False]]) else: return TestInstance([[self.tip, reject]]) # move the tip back to a previous block def tip(number): self.tip = self.blocks[number] # adds transactions to the block and updates state def update_block(block_number, new_transactions): [tx.rehash() for tx in new_transactions] block = self.blocks[block_number] block.vtx.extend(new_transactions) old_sha256 = block.sha256 + make_conform_to_ctor(block) block.hashMerkleRoot = block.calc_merkle_root() block.solve() # Update the internal state just like in next_block self.tip = block if block.sha256 != old_sha256: self.block_heights[ block.sha256] = self.block_heights[old_sha256] del self.block_heights[old_sha256] self.blocks[block_number] = block return block # shorthand for functions block = self.next_block node = self.nodes[0] # Create a new block block(0) save_spendable_output() yield accepted() # Now we need that block to mature so we can spend the coinbase. test = TestInstance(sync_every_block=False) for i in range(99): block(5000 + i) test.blocks_and_transactions.append([self.tip, True]) save_spendable_output() yield test # collect spendable outputs now to avoid cluttering the code later on out = [] for i in range(100): out.append(get_spendable_output()) # Generate a key pair to test P2SH sigops count private_key = CECKey() private_key.set_secretbytes(b"replayprotection") public_key = private_key.get_pubkey() # This is a little handier to use than the version in blocktools.py def create_fund_and_spend_tx(spend, forkvalue=0): # Fund transaction script = CScript([public_key, OP_CHECKSIG]) txfund = create_transaction( spend.tx, spend.n, b'', 50 * COIN, script) txfund.rehash() # Spend transaction txspend = CTransaction() txspend.vout.append(CTxOut(50 * COIN - 1000, CScript([OP_TRUE]))) txspend.vin.append(CTxIn(COutPoint(txfund.sha256, 0), b'')) # Sign the transaction sighashtype = (forkvalue << 8) | SIGHASH_ALL | SIGHASH_FORKID sighash = SignatureHashForkId( script, txspend, 0, sighashtype, 50 * COIN) sig = private_key.sign(sighash) + \ bytes(bytearray([SIGHASH_ALL | SIGHASH_FORKID])) txspend.vin[0].scriptSig = CScript([sig]) txspend.rehash() return [txfund, txspend] def send_transaction_to_mempool(tx): tx_id = node.sendrawtransaction(ToHex(tx)) assert(tx_id in set(node.getrawmempool())) return tx_id # Before the fork, no replay protection required to get in the mempool. txns = create_fund_and_spend_tx(out[0]) send_transaction_to_mempool(txns[0]) send_transaction_to_mempool(txns[1]) # And txns get mined in a block properly. block(1) update_block(1, txns) yield accepted() # Replay protected transactions are rejected. replay_txns = create_fund_and_spend_tx(out[1], 0xffdead) send_transaction_to_mempool(replay_txns[0]) assert_raises_rpc_error(-26, RPC_INVALID_SIGNATURE_ERROR, node.sendrawtransaction, ToHex(replay_txns[1])) # And block containing them are rejected as well. block(2) update_block(2, replay_txns) yield rejected(RejectResult(16, b'blk-bad-inputs')) # Rewind bad block tip(1) # Create a block that would activate the replay protection. bfork = block(5555) bfork.nTime = REPLAY_PROTECTION_START_TIME - 1 update_block(5555, []) yield accepted() for i in range(5): block(5100 + i) test.blocks_and_transactions.append([self.tip, True]) yield test # Check we are just before the activation time assert_equal(node.getblockheader(node.getbestblockhash())['mediantime'], REPLAY_PROTECTION_START_TIME - 1) # We are just before the fork, replay protected txns still are rejected assert_raises_rpc_error(-26, RPC_INVALID_SIGNATURE_ERROR, node.sendrawtransaction, ToHex(replay_txns[1])) block(3) update_block(3, replay_txns) yield rejected(RejectResult(16, b'blk-bad-inputs')) # Rewind bad block tip(5104) # Send some non replay protected txns in the mempool to check # they get cleaned at activation. txns = create_fund_and_spend_tx(out[2]) send_transaction_to_mempool(txns[0]) tx_id = send_transaction_to_mempool(txns[1]) # Activate the replay protection block(5556) yield accepted() # Non replay protected transactions are not valid anymore, # so they should be removed from the mempool. assert(tx_id not in set(node.getrawmempool())) # Good old transactions are now invalid. send_transaction_to_mempool(txns[0]) assert_raises_rpc_error(-26, RPC_INVALID_SIGNATURE_ERROR, node.sendrawtransaction, ToHex(txns[1])) # They also cannot be mined block(4) update_block(4, txns) yield rejected(RejectResult(16, b'blk-bad-inputs')) # Rewind bad block tip(5556) # The replay protected transaction is now valid send_transaction_to_mempool(replay_txns[0]) replay_tx_id = send_transaction_to_mempool(replay_txns[1]) # They also can also be mined b5 = block(5) update_block(5, replay_txns) yield accepted() # Ok, now we check if a reorg work properly accross the activation. postforkblockid = node.getbestblockhash() node.invalidateblock(postforkblockid) assert(replay_tx_id in set(node.getrawmempool())) # Deactivating replay protection. forkblockid = node.getbestblockhash() node.invalidateblock(forkblockid) assert(replay_tx_id not in set(node.getrawmempool())) # Check that we also do it properly on deeper reorg. node.reconsiderblock(forkblockid) node.reconsiderblock(postforkblockid) node.invalidateblock(forkblockid) assert(replay_tx_id not in set(node.getrawmempool())) if __name__ == '__main__': ReplayProtectionTest().main() diff --git a/test/functional/abc-transaction-ordering.py b/test/functional/abc-transaction-ordering.py index 60fed33c4..9f46a6c43 100755 --- a/test/functional/abc-transaction-ordering.py +++ b/test/functional/abc-transaction-ordering.py @@ -1,290 +1,247 @@ #!/usr/bin/env python3 # Copyright (c) 2018 The Bitcoin developers # Distributed under the MIT software license, see the accompanying # file COPYING or http://www.opensource.org/licenses/mit-license.php. """ This test checks that the node software accepts transactions in non topological order once the feature is activated. """ from test_framework.test_framework import ComparisonTestFramework from test_framework.util import assert_equal, assert_raises_rpc_error from test_framework.comptool import TestManager, TestInstance, RejectResult from test_framework.blocktools import * import time from test_framework.key import CECKey from test_framework.script import * from collections import deque # far into the future -MAGNETIC_ANOMALY_START_TIME = 2000000000 +REPLAY_PROTECTION_START_TIME = 2000000000 class PreviousSpendableOutput(): def __init__(self, tx=CTransaction(), n=-1): self.tx = tx self.n = n # the output we're spending class TransactionOrderingTest(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): self.num_nodes = 1 self.setup_clean_chain = True self.block_heights = {} self.tip = None self.blocks = {} self.extra_args = [['-whitelist=127.0.0.1', '-relaypriority=0', - "-magneticanomalyactivationtime=%d" % MAGNETIC_ANOMALY_START_TIME, - "-replayprotectionactivationtime=%d" % (2 * MAGNETIC_ANOMALY_START_TIME)]] + "-replayprotectionactivationtime={}".format(REPLAY_PROTECTION_START_TIME)]] def run_test(self): self.test = TestManager(self, self.options.tmpdir) self.test.add_all_connections(self.nodes) # Start up network handling in another thread NetworkThread().start() # Set the blocksize to 2MB as initial condition - self.nodes[0].setmocktime(MAGNETIC_ANOMALY_START_TIME) self.test.run() def add_transactions_to_block(self, block, tx_list): [tx.rehash() for tx in tx_list] block.vtx.extend(tx_list) def next_block(self, number, spend=None, tx_count=0): if self.tip == None: base_block_hash = self.genesis_hash block_time = int(time.time()) + 1 else: base_block_hash = self.tip.sha256 block_time = self.tip.nTime + 1 # First create the coinbase height = self.block_heights[base_block_hash] + 1 coinbase = create_coinbase(height) coinbase.rehash() if spend == None: # We need to have something to spend to fill the block. block = create_block(base_block_hash, coinbase, block_time) else: # all but one satoshi to fees coinbase.vout[0].nValue += spend.tx.vout[spend.n].nValue - 1 coinbase.rehash() block = create_block(base_block_hash, coinbase, block_time) # Make sure we have plenty enough to spend going forward. spendable_outputs = deque([spend]) def get_base_transaction(): # Create the new transaction tx = CTransaction() # Spend from one of the spendable outputs spend = spendable_outputs.popleft() tx.vin.append(CTxIn(COutPoint(spend.tx.sha256, spend.n))) # Add spendable outputs for i in range(4): tx.vout.append(CTxOut(0, CScript([OP_TRUE]))) spendable_outputs.append(PreviousSpendableOutput(tx, i)) # Put some random data into the transaction in order to randomize ids. # This also ensures that transaction are larger than 100 bytes. rand = random.getrandbits(256) tx.vout.append(CTxOut(0, CScript([rand, OP_RETURN]))) return tx tx = get_base_transaction() # 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. tx.rehash() # Add the transaction to the block 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 # the 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 block.solve() self.tip = block self.block_heights[block.sha256] = height assert number not in self.blocks self.blocks[number] = block return block 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 def save_spendable_output(): spendable_outputs.append(self.tip) # get an output that we previously marked as spendable def get_spendable_output(): return PreviousSpendableOutput(spendable_outputs.pop(0).vtx[0], 0) # returns a test case that asserts that the current tip was accepted def accepted(): return TestInstance([[self.tip, True]]) # returns a test case that asserts that the current tip was rejected def rejected(reject=None): if reject is None: return TestInstance([[self.tip, False]]) else: return TestInstance([[self.tip, reject]]) # move the tip back to a previous block def tip(number): self.tip = self.blocks[number] # adds transactions to the block and updates state def update_block(block_number, new_transactions=[]): block = self.blocks[block_number] self.add_transactions_to_block(block, new_transactions) old_sha256 = block.sha256 block.hashMerkleRoot = block.calc_merkle_root() block.solve() # Update the internal state just like in next_block self.tip = block if block.sha256 != old_sha256: self.block_heights[block.sha256] = self.block_heights[old_sha256] del self.block_heights[old_sha256] self.blocks[block_number] = block return block # shorthand for functions block = self.next_block # Create a new block block(0) save_spendable_output() yield accepted() # Now we need that block to mature so we can spend the coinbase. test = TestInstance(sync_every_block=False) for i in range(99): block(5000 + i) test.blocks_and_transactions.append([self.tip, True]) save_spendable_output() yield test # collect spendable outputs now to avoid cluttering the code later on out = [] for i in range(100): out.append(get_spendable_output()) # Let's build some blocks and test them. - for i in range(15): + for i in range(17): n = i + 1 block(n) yield accepted() - # Start moving MTP forward - bfork = block(5555) - bfork.nTime = MAGNETIC_ANOMALY_START_TIME - 1 - update_block(5555) + block(5556) yield accepted() - # Get to one block of the Nov 15, 2018 HF activation - for i in range(5): - block(5100 + i) - test.blocks_and_transactions.append([self.tip, True]) - yield test - - # Check that the MTP is just before the configured fork point. - assert_equal(node.getblockheader(node.getbestblockhash())['mediantime'], - MAGNETIC_ANOMALY_START_TIME - 1) - - # Activate the Nov 15, 2018 HF - block(5556, out[16], tx_count=16) - yield accepted() - - # Now MTP is exactly the fork time. Transactions are expected to be ordered now. - assert_equal(node.getblockheader(node.getbestblockhash())['mediantime'], - MAGNETIC_ANOMALY_START_TIME) - # Block with regular ordering are now rejected. block(5557, out[17], tx_count=16) yield rejected(RejectResult(16, b'tx-ordering')) # Rewind bad block. tip(5556) # After we activate the Nov 15, 2018 HF, transaction order is enforced. def ordered_block(block_number, spend): b = block(block_number, spend=spend, tx_count=16) - b.vtx = [b.vtx[0]] + sorted(b.vtx[1:], key=lambda tx: tx.get_id()) + make_conform_to_ctor(b) update_block(block_number) return b # Now that the fork activated, we need to order transaction per txid. ordered_block(4445, out[17]) 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) - - assert_equal(len(node.getrawmempool()), 15) - node.generate(1) - generatedblockhash = node.getbestblockhash() - assert(forkblockhash != generatedblockhash) - - # Reconstruct tip. - tip_hash = node.getbestblockhash() - self.tip = CBlock() - self.tip.sha256 = int(tip_hash, 16) - self.tip.nTime = timestamp = node.getblock(tip_hash)['time'] - self.block_heights[self.tip.sha256] = node.getblock(tip_hash)['height'] - ordered_block(4446, out[18]) yield accepted() # Generate a block with a duplicated transaction. double_tx_block = ordered_block(4447, out[19]) assert_equal(len(double_tx_block.vtx), 16) double_tx_block.vtx = double_tx_block.vtx[:8] + \ [double_tx_block.vtx[8]] + double_tx_block.vtx[8:] update_block(4447) yield rejected(RejectResult(16, b'bad-txns-duplicate')) # Rewind bad block. tip(4446) # Check over two blocks. proper_block = ordered_block(4448, out[20]) yield accepted() replay_tx_block = ordered_block(4449, out[21]) assert_equal(len(replay_tx_block.vtx), 16) replay_tx_block.vtx.append(proper_block.vtx[5]) replay_tx_block.vtx = [replay_tx_block.vtx[0]] + \ sorted(replay_tx_block.vtx[1:], key=lambda tx: tx.get_id()) update_block(4449) yield rejected(RejectResult(16, b'bad-txns-BIP30')) if __name__ == '__main__': TransactionOrderingTest().main() diff --git a/test/functional/timing.json b/test/functional/timing.json index bdd1fe008..feeb13845 100644 --- a/test/functional/timing.json +++ b/test/functional/timing.json @@ -1,374 +1,366 @@ [ { "name": "abandonconflict.py", "time": 18 }, - { - "name": "abc-checkdatasig-activation.py", - "time": 5 - }, { "name": "abc-cmdline.py", "time": 8 }, { "name": "abc-finalize-block.py", "time": 7 }, { "name": "abc-high_priority_transaction.py", "time": 16 }, - { - "name": "abc-magnetic-anomaly-activation.py", - "time": 8 - }, { "name": "abc-magnetic-anomaly-mining.py", "time": 19 }, { "name": "abc-mempool-accept-txn.py", "time": 10 }, { "name": "abc-p2p-compactblocks.py", "time": 743 }, { "name": "abc-p2p-fullblocktest.py", "time": 125 }, { "name": "abc-parkedchain.py", "time": 16 }, { "name": "abc-replay-protection.py", "time": 6 }, { "name": "abc-rpc.py", "time": 2 }, { "name": "abc-sync-chain.py", "time": 4 }, { "name": "abc-transaction-ordering.py", "time": 8 }, { "name": "assumevalid.py", "time": 66 }, { "name": "bip65-cltv-p2p.py", "time": 17 }, { "name": "bip68-112-113-p2p.py", "time": 28 }, { "name": "bip68-sequence.py", "time": 22 }, { "name": "bipdersig-p2p.py", "time": 46 }, { "name": "bitcoin_cli.py", "time": 3 }, { "name": "blockchain.py", "time": 11 }, { "name": "dbcrash.py", "time": 537 }, { "name": "conf_args.py", "time": 13 }, { "name": "decodescript.py", "time": 2 }, { "name": "disablewallet.py", "time": 2 }, { "name": "disconnect_ban.py", "time": 7 }, { "name": "example_test.py", "time": 3 }, { "name": "fundrawtransaction.py", "time": 42 }, { "name": "getblocktemplate_longpoll.py", "time": 68 }, { "name": "getchaintips.py", "time": 5 }, { "name": "httpbasics.py", "time": 3 }, { "name": "import-rescan.py", "time": 18 }, { "name": "importmulti.py", "time": 16 }, { "name": "importprunedfunds.py", "time": 5 }, { "name": "invalidateblock.py", "time": 8 }, { "name": "invalidblockrequest.py", "time": 5 }, { "name": "invalidtxrequest.py", "time": 6 }, { "name": "keypool-topup.py", "time": 18 }, { "name": "keypool.py", "time": 9 }, { "name": "listsinceblock.py", "time": 6 }, { "name": "listtransactions.py", "time": 7 }, { "name": "maxuploadtarget.py", "time": 29 }, { "name": "mempool_limit.py", "time": 7 }, { "name": "mempool_packages.py", "time": 70 }, { "name": "mempool_persist.py", "time": 19 }, { "name": "mempool_reorg.py", "time": 3 }, { "name": "mempool_resurrect_test.py", "time": 3 }, { "name": "mempool_spendcoinbase.py", "time": 2 }, { "name": "merkle_blocks.py", "time": 5 }, { "name": "minchainwork.py", "time": 7 }, { "name": "mining.py", "time": 3 }, { "name": "multi_rpc.py", "time": 4 }, { "name": "multiwallet.py", "time": 9 }, { "name": "net.py", "time": 3 }, { "name": "notifications.py", "time": 7 }, { "name": "nulldummy.py", "time": 6 }, { "name": "p2p-acceptblock.py", "time": 45 }, { "name": "p2p-compactblocks.py", "time": 21 }, { "name": "p2p-feefilter.py", "time": 26 }, { "name": "p2p-fingerprint.py", "time": 9 }, { "name": "p2p-fullblocktest.py", "time": 189 }, { "name": "p2p-leaktests.py", "time": 8 }, { "name": "p2p-mempool.py", "time": 3 }, { "name": "p2p-timeouts.py", "time": 65 }, { "name": "preciousblock.py", "time": 4 }, { "name": "prioritise_transaction.py", "time": 6 }, { "name": "proxy_test.py", "time": 4 }, { "name": "pruning.py", "time": 1039 }, { "name": "rawtransactions.py", "time": 16 }, { "name": "reindex.py", "time": 13 }, { "name": "resendwallettransactions.py", "time": 5 }, { "name": "rest.py", "time": 9 }, { "name": "rpcbind_test.py", "time": 24 }, { "name": "rpcnamedargs.py", "time": 3 }, { "name": "sendheaders.py", "time": 18 }, { "name": "signmessages.py", "time": 2 }, { "name": "signrawtransactions.py", "time": 2 }, { "name": "txn_clone.py", "time": 5 }, { "name": "txn_clone.py --mineblock", "time": 5 }, { "name": "txn_doublespend.py", "time": 5 }, { "name": "txn_doublespend.py --mineblock", "time": 5 }, { "name": "uacomment.py", "time": 6 }, { "name": "uptime.py", "time": 2 }, { "name": "wallet.py", "time": 67 }, { "name": "wallet_accounts.py", "time": 14 }, { "name": "wallet_dump.py", "time": 8 }, { "name": "wallet_encryption.py", "time": 9 }, { "name": "wallet_hd.py", "time": 122 }, { "name": "wallet_receivedby.py", "time": 9 }, { "name": "walletbackup.py", "time": 147 }, { "name": "zapwallettxes.py", "time": 12 }, { "name": "zmq_test.py", "time": 7 } ] \ No newline at end of file