Changeset View
Changeset View
Standalone View
Standalone View
test/functional/monolith-opcodes.py
- This file was added.
Property | Old Value | New Value |
---|---|---|
File Mode | null | 100755 |
#!/usr/bin/env python3 | |||||
# Copyright (c) 2015-2016 The Bitcoin Core developers | |||||
# Copyright (c) 2017-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 opcodes and the different consensus | |||||
related to this activation. | |||||
""" | |||||
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 | |||||
MONOLITH_START_TIME = 2000000000 | |||||
# Error due to disabled opcode | |||||
DISABLED_OPCODE_ERROR = b'mandatory-script-verify-flag-failed (Attempted to use a disabled opcode)' | |||||
RPC_DISABLED_OPCODE_ERROR = "16: " + DISABLED_OPCODE_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 MonolithOpcodeTest(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', '-debug', "-monolithactivationtime=%d" % MONOLITH_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(MONOLITH_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 | |||||
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"monolithopcodes") | |||||
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, scriptPubKey=[OP_TRUE], scriptSig=[]): | |||||
# Fund transaction | |||||
txfund = create_transaction( | |||||
spend.tx, spend.n, b'', 50 * COIN, CScript(scriptPubKey)) | |||||
txfund.rehash() | |||||
# Spend transaction | |||||
txspend = CTransaction() | |||||
txspend.vout.append( | |||||
CTxOut(50 * COIN - 1000, CScript(scriptPubKey))) | |||||
txspend.vin.append(CTxIn(COutPoint(txfund.sha256, 0), b'')) | |||||
txspend.vin[0].scriptSig = CScript(scriptSig) | |||||
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 monolith, normal tx can 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 they get mined in a block properly. | |||||
block(1) | |||||
update_block(1, txns) | |||||
yield accepted() | |||||
# transactions with the new opcodes are rejected. | |||||
opand_txns = create_fund_and_spend_tx(out[1], [OP_AND], [0x01, 0x01]) | |||||
send_transaction_to_mempool(opand_txns[0]) | |||||
assert_raises_rpc_error(-26, RPC_DISABLED_OPCODE_ERROR, | |||||
node.sendrawtransaction, ToHex(opand_txns[1])) | |||||
# And block containing them are rejected as well. | |||||
block(2) | |||||
update_block(2, opand_txns) | |||||
yield rejected(RejectResult(16, b'blk-bad-inputs')) | |||||
# Rewind bad block | |||||
tip(1) | |||||
# Create a block that would activate monolith. | |||||
bfork = block(5555) | |||||
bfork.nTime = MONOLITH_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'], | |||||
MONOLITH_START_TIME - 1) | |||||
# We are just before the fork, txns with new opcodes are still rejected | |||||
assert_raises_rpc_error(-26, RPC_DISABLED_OPCODE_ERROR, | |||||
node.sendrawtransaction, ToHex(opand_txns[1])) | |||||
block(3) | |||||
update_block(3, opand_txns) | |||||
yield rejected(RejectResult(16, b'blk-bad-inputs')) | |||||
# Rewind bad block | |||||
tip(5104) | |||||
# Activate monolith | |||||
block(5556) | |||||
yield accepted() | |||||
# The re-enabled opcode transaction is now valid | |||||
# IAMHERE | |||||
send_transaction_to_mempool(opand_txns[0]) | |||||
opand_txid = send_transaction_to_mempool(opand_txns[1]) | |||||
# They also can also be mined | |||||
b5 = block(5) | |||||
update_block(5, opand_txns) | |||||
yield accepted() | |||||
# Ok, now we check if a reorg work properly across the activation. | |||||
postforkblockid = node.getbestblockhash() | |||||
node.invalidateblock(postforkblockid) | |||||
assert(opand_txid in set(node.getrawmempool())) | |||||
# Deactivating monolith | |||||
forkblockid = node.getbestblockhash() | |||||
node.invalidateblock(forkblockid) | |||||
assert(opand_txid 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(opand_txid not in set(node.getrawmempool())) | |||||
if __name__ == '__main__': | |||||
MonolithOpcodeTest().main() |