Changeset View
Changeset View
Standalone View
Standalone View
test/functional/abc-magnetic-anomaly-activation.py
#!/usr/bin/env python3 | #!/usr/bin/env python3 | ||||
# Copyright (c) 2018 The Bitcoin developers | # Copyright (c) 2018 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 simple features of the magnetic anomaly fork | This test checks that simple features of the magnetic anomaly fork | ||||
activates properly. More complex features are given their own tests. | 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 create_coinbase, create_block | ||||
from test_framework.mininode import * | |||||
from test_framework.script import * | |||||
from test_framework.cdefs import MIN_TX_SIZE | from test_framework.cdefs import MIN_TX_SIZE | ||||
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(): | ||||
Show All 23 Lines | def run_test(self): | ||||
# 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) | ||||
def next_block(self, number, spend=None, tx_size=0): | def next_block(self, number, spend=None, tx_size=0, pushonly=True): | ||||
if self.tip == None: | if self.tip == None: | ||||
base_block_hash = self.genesis_hash | base_block_hash = self.genesis_hash | ||||
import time | |||||
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() | ||||
Show All 9 Lines | def next_block(self, number, spend=None, tx_size=0, pushonly=True): | ||||
# 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]) | ||||
# 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))) | ||||
if pushonly == False: | |||||
tx.vin[0].scriptSig = CScript([OP_TRUE, OP_DROP]) | |||||
# Add spendable outputs | # Add spendable outputs | ||||
for i in range(2): | 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)) | ||||
# Put some random data into the transaction in order to randomize ids. | # Put some random data into the transaction in order to randomize ids. | ||||
if tx_size == 0: | if tx_size == 0: | ||||
tx.vout.append( | tx.vout.append( | ||||
CTxOut(0, CScript([random.getrandbits(8), OP_RETURN]))) | CTxOut(0, CScript([random.getrandbits(8), OP_RETURN]))) | ||||
else: | else: | ||||
tx.vout.append( | # Create an input to pad the transaction. | ||||
CTxOut(0, CScript([random.getrandbits(8 * (tx_size - 82) - 1), OP_RETURN]))) | 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: | |||||
tx.vout[2] = CTxOut(0, CScript( | |||||
[random.getrandbits(8 * push_size - 1), OP_RETURN])) | |||||
push_size += 1 | |||||
assert_equal(len(tx.serialize()), tx_size) | 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() | ||||
# 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]) | ||||
▲ Show 20 Lines • Show All 92 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) | ||||
# Check that block with small transactions are still accepted. | # Check that block with small transactions and non push only signatures | ||||
small_tx_block = block(4444, out[0], MIN_TX_SIZE - 1) | # are still accepted. | ||||
small_tx_block = block(4444, out[0], MIN_TX_SIZE - 1, False) | |||||
assert_equal(len(small_tx_block.vtx[1].serialize()), MIN_TX_SIZE - 1) | assert_equal(len(small_tx_block.vtx[1].serialize()), MIN_TX_SIZE - 1) | ||||
yield accepted() | yield accepted() | ||||
# Now MTP is exactly the fork time. Small transaction are now rejected. | # 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) | ||||
# Now that the for activated, it is not possible to have | # Now that the for activated, it is not possible to have | ||||
# small transactions anymore. | # small transactions anymore. | ||||
small_tx_block = block(4445, out[1], MIN_TX_SIZE - 1) | small_tx_block = block(4445, out[1], MIN_TX_SIZE - 1) | ||||
assert_equal(len(small_tx_block.vtx[1].serialize()), 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')) | yield rejected(RejectResult(16, b'bad-txns-undersize')) | ||||
# Rewind bad block. | # Rewind bad block. | ||||
tip(4444) | tip(4444) | ||||
# Now that the for activated, it is not possible to have | |||||
# non push only transactions. | |||||
non_pushonly_tx_block = block(4446, out[1], MIN_TX_SIZE, False) | |||||
assert_equal(len(small_tx_block.vtx[1].serialize()), MIN_TX_SIZE - 1) | |||||
yield rejected(RejectResult(16, b'blk-bad-inputs')) | |||||
# Rewind bad block. | |||||
tip(4444) | |||||
# But large transactions are still ok. | # But large transactions are still ok. | ||||
large_tx_block = block(4446, out[1], MIN_TX_SIZE) | large_tx_block = block(3333, out[1], MIN_TX_SIZE) | ||||
assert_equal(len(large_tx_block.vtx[1].serialize()), MIN_TX_SIZE) | assert_equal(len(large_tx_block.vtx[1].serialize()), MIN_TX_SIZE) | ||||
yield accepted() | yield accepted() | ||||
if __name__ == '__main__': | if __name__ == '__main__': | ||||
MagneticAnomalyActivationTest().main() | MagneticAnomalyActivationTest().main() |