diff --git a/src/test/validation_tests.cpp b/src/test/validation_tests.cpp --- a/src/test/validation_tests.cpp +++ b/src/test/validation_tests.cpp @@ -1,5 +1,5 @@ // Copyright (c) 2011-2016 The Bitcoin Core developers -// Copyright (c) 2017 The Bitcoin 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. @@ -7,6 +7,7 @@ #include "config.h" #include "consensus/consensus.h" #include "primitives/transaction.h" +#include "script/interpreter.h" #include "test/test_bitcoin.h" #include "util.h" #include "validation.h" @@ -17,6 +18,10 @@ #include +// defined in validation.cpp but not declared in validation.h - dont want to +// expose in header just for tests +uint32_t GetBlockScriptFlags(const Config &config, const CBlockIndex *pindex); + static CBlock makeLargeDummyBlock(const size_t num_tx) { CBlock block; block.vtx.reserve(num_tx); @@ -76,4 +81,30 @@ BOOST_CHECK_NO_THROW({ LoadExternalBlockFile(config, fp, 0); }); } +BOOST_AUTO_TEST_CASE(getblockscriptflags_monolith) { + GlobalConfig config; + + // Create genesis block and add new block to tip + CBlockIndex newBlock; + CBlockIndex *genesisBlock = chainActive.Tip(); + newBlock.pprev = genesisBlock; + chainActive.SetTip(&newBlock); + + // Check that monolith hard fork is not enabled + BOOST_CHECK(!IsMonolithEnabled(config, genesisBlock)); + + uint32_t flags = GetBlockScriptFlags(config, chainActive.Tip()); + BOOST_CHECK((flags & SCRIPT_ENABLE_OPCODES_MONOLITH) == 0); + + // Activate monolith hard fork (May 15, 2018) + const int64_t monolithTime = + config.GetChainParams().GetConsensus().monolithActivationTime; + genesisBlock->nTime = monolithTime; + + BOOST_CHECK(IsMonolithEnabled(config, genesisBlock)); + + flags = GetBlockScriptFlags(config, chainActive.Tip()); + BOOST_CHECK((flags & SCRIPT_ENABLE_OPCODES_MONOLITH) != 0); +} + BOOST_AUTO_TEST_SUITE_END() diff --git a/src/validation.cpp b/src/validation.cpp --- a/src/validation.cpp +++ b/src/validation.cpp @@ -1,6 +1,6 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto // Copyright (c) 2009-2016 The Bitcoin Core developers -// Copyright (c) 2017 The Bitcoin 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. @@ -195,8 +195,8 @@ static void FindFilesToPrune(std::set &setFilesToPrune, uint64_t nPruneAfterHeight); static FILE *OpenUndoFile(const CDiskBlockPos &pos, bool fReadOnly = false); -static uint32_t GetBlockScriptFlags(const CBlockIndex *pindex, - const Config &config); +uint32_t GetBlockScriptFlags(const Config &config, + const CBlockIndex *pindex); static bool IsFinalTx(const CTransaction &tx, int nBlockHeight, int64_t nBlockTime) { @@ -969,15 +969,6 @@ gArgs.GetArg("-promiscuousmempoolflags", scriptVerifyFlags); } - // Check against previous transactions. This is done last to help - // prevent CPU exhaustion denial-of-service attacks. - PrecomputedTransactionData txdata(tx); - if (!CheckInputs(tx, state, view, true, scriptVerifyFlags, true, false, - txdata)) { - // State filled in by CheckInputs. - return false; - } - // Check again against the current block tip's script verification flags // to cache our script execution flags. This is, of course, useless if // the next block has different script flags from the previous one, but @@ -994,7 +985,18 @@ // invalid blocks (using TestBlockValidity), however allowing such // transactions into the mempool can be exploited as a DoS attack. uint32_t currentBlockScriptVerifyFlags = - GetBlockScriptFlags(chainActive.Tip(), config); + GetBlockScriptFlags(config, chainActive.Tip()); + scriptVerifyFlags |= currentBlockScriptVerifyFlags; + + // Check against previous transactions. This is done last to help + // prevent CPU exhaustion denial-of-service attacks. + PrecomputedTransactionData txdata(tx); + if (!CheckInputs(tx, state, view, true, scriptVerifyFlags, true, false, + txdata)) { + // State filled in by CheckInputs. + return false; + } + if (!CheckInputsFromMempoolAndCache(tx, state, view, pool, currentBlockScriptVerifyFlags, true, txdata)) { @@ -1540,7 +1542,21 @@ ScriptErrorString(check.GetScriptError()))); } } - + if (!(flags & SCRIPT_ENABLE_OPCODES_MONOLITH)) { + // Check whether the failure was caused by an opcode used in + // the monolith fork deployment. We do not want to trigger DoS + // protection and cause a network split if so. + CScriptCheck check2(scriptPubKey, amount, tx, i, + flags & + SCRIPT_ENABLE_OPCODES_MONOLITH, + sigCacheStore, txdata); + if (!check2()) { + return state.DoS( + 100, false, REJECT_INVALID, + strprintf("monolith-fork-script-failure (%s)", + ScriptErrorString(check.GetScriptError()))); + } + } // Failures of other flags indicate a transaction that is invalid in // new blocks, e.g. a invalid P2SH. We DoS ban such nodes as they // are not following the protocol. That said during an upgrade @@ -1849,8 +1865,8 @@ static ThresholdConditionCache warningcache[VERSIONBITS_NUM_BITS]; // Returns the script flags which should be checked for a given block -static uint32_t GetBlockScriptFlags(const CBlockIndex *pindex, - const Config &config) { +uint32_t GetBlockScriptFlags(const Config &config, + const CBlockIndex *pindex) { AssertLockHeld(cs_main); const Consensus::Params &consensusparams = config.GetChainParams().GetConsensus(); @@ -1873,8 +1889,7 @@ } // Start enforcing BIP112 (CHECKSEQUENCEVERIFY) using versionbits logic. - if (VersionBitsState(pindex->pprev, consensusparams, - Consensus::DEPLOYMENT_CSV, + if (VersionBitsState(pindex->pprev, consensusparams, Consensus::DEPLOYMENT_CSV, versionbitscache) == THRESHOLD_ACTIVE) { flags |= SCRIPT_VERIFY_CHECKSEQUENCEVERIFY; } @@ -1894,6 +1909,11 @@ flags |= SCRIPT_VERIFY_NULLFAIL; } + if (IsMonolithEnabled(config, pindex)) { + // When the May 15, 2018 HF is enabled, activate new opcodes. + flags |= SCRIPT_ENABLE_OPCODES_MONOLITH; + } + return flags; } @@ -2045,7 +2065,7 @@ nLockTimeFlags |= LOCKTIME_VERIFY_SEQUENCE; } - uint32_t flags = GetBlockScriptFlags(pindex, config); + uint32_t flags = GetBlockScriptFlags(config, pindex); int64_t nTime2 = GetTimeMicros(); nTimeForks += nTime2 - nTime1; @@ -2390,7 +2410,8 @@ } /** - * Update chainActive and related internal data structures when adding a new block to the chain tip. + * Update chainActive and related internal data structures when adding a new + * block to the chain tip. */ static void UpdateTip(const Config &config, CBlockIndex *pindexNew) { const Consensus::Params &consensusParams = @@ -2521,6 +2542,15 @@ return false; } + // If this block was activating the monolith opcodes, then we need to + // remove any transactions that use the monolith opcodes from the mempool. + // There is no easy way to do this so we'll just discard the whole mempool + // and then add the transaction of the block we just disconnected back. + if (IsMonolithEnabled(config, pindexDelete) && + !IsMonolithEnabled(config, pindexDelete->pprev)) { + mempool.clear(); + } + if (disconnectpool) { // Save transactions to re-add to mempool at end of reorg for (const auto &tx : boost::adaptors::reverse(block.vtx)) { @@ -3576,7 +3606,7 @@ if (!IsMonolithEnabled(config, pindexPrev)) { // When the May 15, 2018 HF is not enabled, block cannot be bigger - // than 8MB . + // than 8MB. const uint64_t currentBlockSize = ::GetSerializeSize(block, SER_NETWORK, PROTOCOL_VERSION); if (currentBlockSize > 8 * ONE_MEGABYTE) { diff --git a/test/functional/monolith-opcodes.py b/test/functional/monolith-opcodes.py new file mode 100755 --- /dev/null +++ b/test/functional/monolith-opcodes.py @@ -0,0 +1,286 @@ +#!/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'monolith-fork-script-failure (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', "-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. + 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. + b0 = 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 + + def get_banscore(): + s = node.getpeerinfo() + return s[0]['banscore'] + + # Normal txns can get in the mempool before monolith activates. + 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. + b1 = block(1) + update_block(1, txns) + yield accepted() + + # Transactions with the monolith opcodes are rejected. + opand_txns = create_fund_and_spend_tx(out[1], [OP_AND], [0x01, 0x01]) + send_transaction_to_mempool(opand_txns[0]) + oldbs = get_banscore() + assert_raises_rpc_error(-26, RPC_DISABLED_OPCODE_ERROR, + node.sendrawtransaction, ToHex(opand_txns[1])) + # Check that ban score does not increase for p2p transaction relaying. + assert_equal(oldbs, get_banscore()) + + # Check that blocks containing monolith opcodes are rejected as well. + b9000 = block(9000) + update_block(9000, opand_txns) + yield rejected(RejectResult(16, b'blk-bad-inputs')) + # Relaying of an invalid block will increase the banscore. + assert(oldbs < get_banscore()) + + # Rewind bad block. + tip(1) + + # Create block immediately preceding monolith activation by two milliseconds. + # This is based on time only and not MTP activation. + b2 = block(2) + b2.nTime = MONOLITH_START_TIME - 2 + update_block(2, []) + yield accepted() + for i in range(5): + block(3 + i) + test.blocks_and_transactions.append([self.tip, True]) + yield test + + # Check we are just before the monolith activation time by two milliseconds. + # Again this is based on time only and not MTP activation. + assert_equal( + node.getblockheader(node.getbestblockhash())['mediantime'], + MONOLITH_START_TIME - 2) + + # Txns with monolith opcodes are still rejected. + assert_raises_rpc_error(-26, RPC_DISABLED_OPCODE_ERROR, + node.sendrawtransaction, ToHex(opand_txns[1])) + + # Create new pre-fork block preceding monolith activation time by one millisecond. + # Again, fork does not activate until MTP has been met. + b9001 = block(9001) + b9001.nTime = MONOLITH_START_TIME - 1 + update_block(9001, opand_txns) + yield rejected(RejectResult(16, b'blk-bad-inputs')) + # This should increase the banscore since it's an invalid block. + assert(oldbs < get_banscore()) + + # Rewind bad block + tip(7) + + # Submit block exceeding monolith activation time (but not MTP activation). + # Include premature monolith opcode txns and test for failure. + b9002 = block(9002) + b9002.nTime = MONOLITH_START_TIME + 2000 + update_block(9002, opand_txns) + yield rejected(RejectResult(16, b'blk-bad-inputs')) + + # Test that monolith transactions are not relayed until valid MTP activation. + assert_raises_rpc_error(-26, RPC_DISABLED_OPCODE_ERROR, + node.sendrawtransaction, ToHex(opand_txns[1])) + + # Rewind bad block + tip(7) + + # Create 3 more blocks until MTP activation occurs. + # ***MTP activation occurs here*** + for x in range(3): + block(8 + x) + yield accepted() + + # Txns with monolith opcodes are now valid. + send_transaction_to_mempool(opand_txns[0]) + opand_txid = send_transaction_to_mempool(opand_txns[1]) + assert(opand_txid in set(node.getrawmempool())) + + # Force a re-org post-activation. + # This should not clear the mempool. + postforkblockid = node.getbestblockhash() + node.invalidateblock(postforkblockid) + assert(opand_txid in set(node.getrawmempool())) + + # Deactivating monolith and force a re-org. + # This should clear the mempool. + forkblockid = node.getbestblockhash() + node.invalidateblock(node.getbestblockhash()) + 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())) + + # Reconnect invalidated blocks and mine new block with monolith opcodes. + node.reconsiderblock(forkblockid) + node.reconsiderblock(postforkblockid) + b11 = block(11) + update_block(11, opand_txns) + yield accepted() + + +if __name__ == '__main__': + MonolithOpcodeTest().main()