diff --git a/qa/pull-tester/rpc-tests.py b/qa/pull-tester/rpc-tests.py --- a/qa/pull-tester/rpc-tests.py +++ b/qa/pull-tester/rpc-tests.py @@ -154,6 +154,7 @@ 'listsinceblock.py', 'p2p-leaktests.py', #'abc-cmdline.py', # FIXME: produces stderr output - run manually until we fix test framework + 'abc-p2p-activation.py', 'abc-p2p-fullblocktest.py', 'abc-rpc.py', ] diff --git a/qa/rpc-tests/abc-p2p-activation.py b/qa/rpc-tests/abc-p2p-activation.py new file mode 100755 --- /dev/null +++ b/qa/rpc-tests/abc-p2p-activation.py @@ -0,0 +1,274 @@ +#!/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 * +from test_framework.comptool import TestManager, TestInstance, RejectResult +from test_framework.blocktools import * +from test_framework.key import CECKey +from test_framework.script import * +from test_framework.cdefs import MAX_BLOCK_SIGOPS_PER_MB, MAX_TX_SIGOPS_COUNT, HF_START_TIME + + +class PreviousSpendableOutput(object): + def __init__(self, tx = CTransaction(), n = -1): + self.tx = tx + self.n = n # the output we're spending + + +class FullBlockTest(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 __init__(self): + super().__init__() + self.excessive_block_size = 16 * ONE_MEGABYTE + self.num_nodes = 1 + self.block_heights = {} + self.coinbase_key = CECKey() + self.coinbase_key.set_secretbytes(b"fatstacks") + self.coinbase_pubkey = self.coinbase_key.get_pubkey() + self.tip = None + self.blocks = {} + + def setup_network(self): + self.extra_args = [['-debug', + '-whitelist=127.0.0.1', + "-excessiveblocksize=%d" + % self.excessive_block_size ]] + self.nodes = start_nodes(self.num_nodes, self.options.tmpdir, + self.extra_args, + binary=[self.options.testbinary]) + + def add_options(self, parser): + super().add_options(parser) + parser.add_option("--runbarelyexpensive", dest="runbarelyexpensive", default=True) + + 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].setexcessiveblock(self.excessive_block_size) + self.test.run() + + def add_transactions_to_block(self, block, tx_list): + [ tx.rehash() for tx in tx_list ] + block.vtx.extend(tx_list) + + # this is a little handier to use than the version in blocktools.py + def create_tx(self, spend_tx, n, value, script=CScript([OP_TRUE])): + tx = create_transaction(spend_tx, n, b"", value, script) + return tx + + # sign a transaction, using the key we know about + # this signs input 0 in tx, which is assumed to be spending output n in spend_tx + def sign_tx(self, tx, spend_tx, n): + scriptPubKey = bytearray(spend_tx.vout[n].scriptPubKey) + if (scriptPubKey[0] == OP_TRUE): # an anyone-can-spend + tx.vin[0].scriptSig = CScript() + return + (sighash, err) = SignatureHash(spend_tx.vout[n].scriptPubKey, tx, 0, SIGHASH_ALL) + tx.vin[0].scriptSig = CScript([self.coinbase_key.sign(sighash) + bytes(bytearray([SIGHASH_ALL]))]) + + def create_and_sign_transaction(self, spend_tx, n, value, script=CScript([OP_TRUE])): + tx = self.create_tx(spend_tx, n, value, script) + self.sign_tx(tx, spend_tx, n) + tx.rehash() + return tx + + def next_block(self, number, spend=None, additional_coinbase_value=0, script=None, extra_sigops=0, block_size=0, solve=True): + """ + Create a block on top of self.tip, and advance self.tip to point to the new block + if spend is specified, then 1 satoshi will be spent from that to an anyone-can-spend + output, and rest will go to fees. + """ + if self.tip == None: + base_block_hash = self.genesis_hash + # One second after the genesis block + block_time = 1296688602+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, self.coinbase_pubkey) + coinbase.vout[0].nValue += additional_coinbase_value + if (spend != None): + coinbase.vout[0].nValue += spend.tx.vout[spend.n].nValue - 1 # all but one satoshi to fees + coinbase.rehash() + block = create_block(base_block_hash, coinbase, block_time) + spendable_output = None + if (spend != None): + tx = CTransaction() + tx.vin.append(CTxIn(COutPoint(spend.tx.sha256, spend.n), b"", 0xffffffff)) # no signature yet + # This copies the java comparison tool testing behavior: the first + # txout has a garbage scriptPubKey, "to make sure we're not + # pre-verifying too much" (?) + tx.vout.append(CTxOut(0, CScript([random.randint(0,255), height & 255]))) + if script == None: + tx.vout.append(CTxOut(1, CScript([OP_TRUE]))) + else: + tx.vout.append(CTxOut(1, script)) + spendable_output = PreviousSpendableOutput(tx, 0) + + # Now sign it if necessary + scriptSig = b"" + scriptPubKey = bytearray(spend.tx.vout[spend.n].scriptPubKey) + if (scriptPubKey[0] == OP_TRUE): # looks like an anyone-can-spend + scriptSig = CScript([OP_TRUE]) + else: + # We have to actually sign it + (sighash, err) = SignatureHash(spend.tx.vout[spend.n].scriptPubKey, tx, 0, SIGHASH_ALL) + scriptSig = CScript([self.coinbase_key.sign(sighash) + bytes(bytearray([SIGHASH_ALL]))]) + tx.vin[0].scriptSig = scriptSig + # Now add the transaction to the block + self.add_transactions_to_block(block, [tx]) + block.hashMerkleRoot = block.calc_merkle_root() + if spendable_output != None and block_size > 0: + while len(block.serialize()) < block_size: + tx = CTransaction() + script_length = block_size - len(block.serialize()) - 79 + if script_length > 510000: + script_length = 500000 + tx_sigops = min(extra_sigops, script_length, MAX_TX_SIGOPS_COUNT) + extra_sigops -= tx_sigops + script_pad_len = script_length - tx_sigops + script_output = CScript([b'\x00' * script_pad_len] + [OP_CHECKSIG] * tx_sigops) + tx.vout.append(CTxOut(0, CScript([OP_TRUE]))) + tx.vout.append(CTxOut(0, script_output)) + tx.vin.append(CTxIn(COutPoint(spendable_output.tx.sha256, spendable_output.n))) + spendable_output = PreviousSpendableOutput(tx, 0) + self.add_transactions_to_block(block, [tx]) + block.hashMerkleRoot = block.calc_merkle_root() + # Make sure the math above worked out to produce the correct block size + # (the math will fail if there are too many transactions in the block) + assert_equal(len(block.serialize()), block_size) + # Make sure all the requested sigops have been included + assert_equal(extra_sigops, 0) + if solve: + 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): + 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, block_size=LEGACY_MAX_BLOCK_SIZE) + 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()) + + # block up to LEGACY_MAX_BLOCK_SIZE are accepted. + block(1, spend=out[0], block_size=LEGACY_MAX_BLOCK_SIZE) + yield accepted() + + # bigger block are reject as the fork isn't activated yet. + block(2, spend=out[1], block_size=LEGACY_MAX_BLOCK_SIZE + 1) + yield rejected(RejectResult(16, b'bad-blk-length')) + + # Rewind bad block + tip(1) + + # bigger block are reject as the fork isn't activated yet. + b03 = block(3, spend=out[1]) + b03.nTime = HF_START_TIME + update_block(3, []) + yield accepted() + + # Pile up 4 blocks on top to get to the point just before activation. + block(4, spend=out[2]) + yield accepted() + block(5, spend=out[3]) + yield accepted() + block(6, spend=out[4]) + yield accepted() + block(7, spend=out[5]) + yield accepted() + + # bigger block are still reject as the fork isn't activated yet. + block(8, spend=out[6], block_size=LEGACY_MAX_BLOCK_SIZE + 1) + yield rejected(RejectResult(16, b'bad-blk-length')) + + # Rewind bad block + tip(7) + + # Pile up another block, to activate. + block(9, spend=out[6]) + yield accepted() + + # HF is active, now we can create bigger blocks. + block(10, spend=out[7], block_size=LEGACY_MAX_BLOCK_SIZE + 1) + yield accepted() + + +if __name__ == '__main__': + FullBlockTest().main() diff --git a/qa/rpc-tests/test_framework/cdefs.py b/qa/rpc-tests/test_framework/cdefs.py --- a/qa/rpc-tests/test_framework/cdefs.py +++ b/qa/rpc-tests/test_framework/cdefs.py @@ -43,6 +43,8 @@ # blocks (network rule) COINBASE_MATURITY = 100 +# Time at which the HF starts. +HF_START_TIME = 1296688602 + 10000 if __name__ == "__main__": # Output values if run standalone to verify diff --git a/src/chainparams.cpp b/src/chainparams.cpp --- a/src/chainparams.cpp +++ b/src/chainparams.cpp @@ -16,7 +16,8 @@ #include "chainparamsseeds.h" -static const int64_t HF_START_TIME = 10000; +// 10000s after the regtest genesis block. +static const int64_t HF_START_TIME = 1296688602 + 10000; static const std::string ANTI_REPLAY_COMMITMENT = "Placeholder for the anti replay commitment"; diff --git a/src/config.cpp b/src/config.cpp --- a/src/config.cpp +++ b/src/config.cpp @@ -9,7 +9,7 @@ bool GlobalConfig::SetMaxBlockSize(uint64_t maxBlockSize) { // Do not allow maxBlockSize to be set below historic 1MB limit - if (maxBlockSize < ONE_MEGABYTE) { + if (maxBlockSize < LEGACY_MAX_BLOCK_SIZE) { return false; } diff --git a/src/consensus/consensus.h b/src/consensus/consensus.h --- a/src/consensus/consensus.h +++ b/src/consensus/consensus.h @@ -12,6 +12,8 @@ static const uint64_t ONE_MEGABYTE = 1000000; /** The maximum allowed size for a transaction, in bytes */ static const uint64_t MAX_TX_SIZE = ONE_MEGABYTE; +/** The maximum allowed size for a block, before the UAHF */ +static const uint64_t LEGACY_MAX_BLOCK_SIZE = ONE_MEGABYTE; /** Default setting for maximum allowed size for a block, in bytes */ static const uint64_t DEFAULT_MAX_BLOCK_SIZE = ONE_MEGABYTE; /** The maximum allowed number of signature check operations per MB in a block diff --git a/src/rpc/abc.cpp b/src/rpc/abc.cpp --- a/src/rpc/abc.cpp +++ b/src/rpc/abc.cpp @@ -54,12 +54,12 @@ } // Do not allow maxBlockSize to be set below historic 1MB limit - if (ebs < ONE_MEGABYTE) + if (ebs < LEGACY_MAX_BLOCK_SIZE) throw JSONRPCError( RPC_INVALID_PARAMETER, std::string( "Invalid parameter, excessiveblock must be larger than ") + - std::to_string(ONE_MEGABYTE)); + std::to_string(LEGACY_MAX_BLOCK_SIZE)); // Set the new max block size. if (!config.SetMaxBlockSize(ebs)) { diff --git a/src/validation.cpp b/src/validation.cpp --- a/src/validation.cpp +++ b/src/validation.cpp @@ -595,6 +595,11 @@ return true; } +static bool IsUAHFenabled(const Consensus::Params &consensusParams, + int64_t nMedianPastTime) { + return nMedianPastTime >= consensusParams.hfStartTime; +} + static bool AcceptToMemoryPoolWorker( const Config &config, CTxMemPool &pool, CValidationState &state, const CTransactionRef &ptx, bool fLimitFree, bool *pfMissingInputs, @@ -1098,10 +1103,9 @@ std::string strCmd = GetArg("-alertnotify", ""); if (strCmd.empty()) return; - // Alert text should be plain ascii coming from a trusted source, but to - // be safe we first strip anything not in safeChars, then add single quotes - // around - // the whole string before passing it to the shell: + // Alert text should be plain ascii coming from a trusted source, but to be + // safe we first strip anything not in safeChars, then add single quotes + // around the whole string before passing it to the shell: std::string singleQuote("'"); std::string safeStatus = SanitizeString(strMessage); safeStatus = singleQuote + safeStatus + singleQuote; @@ -1118,8 +1122,7 @@ if (IsInitialBlockDownload()) return; // If our best fork is no longer within 72 blocks (+/- 12 hours if no one - // mines it) - // of our head, drop it + // mines it) of our head, drop it if (pindexBestForkTip && chainActive.Height() - pindexBestForkTip->nHeight >= 72) pindexBestForkTip = NULL; @@ -1826,6 +1829,24 @@ nLockTimeFlags |= LOCKTIME_VERIFY_SEQUENCE; } + // Sigops counting. We need to do it again because of P2SH. + uint64_t nSigOpsCount = 0; + const uint64_t currentBlockSize = + ::GetSerializeSize(block, SER_NETWORK, PROTOCOL_VERSION); + const uint64_t nMaxSigOpsCount = GetMaxBlockSigOpsCount(currentBlockSize); + + // Check if UAHF is enabled and act accordingly. + int64_t nPrevMedianPastTime = pindex->pprev->GetMedianTimePast(); + bool uahfEnabled = + IsUAHFenabled(chainparams.GetConsensus(), nPrevMedianPastTime); + + // When UAHF is not enabled, block cannot be bigger than + // LEGACY_MAX_BLOCK_SIZE . + if (!uahfEnabled && currentBlockSize > LEGACY_MAX_BLOCK_SIZE) { + return state.DoS(100, false, REJECT_INVALID, "bad-blk-length", false, + "size limits failed"); + } + int64_t nTime2 = GetTimeMicros(); nTimeForks += nTime2 - nTime1; LogPrint("bench", " - Fork checks: %.2fms [%.2fs]\n", @@ -1840,12 +1861,6 @@ CAmount nFees = 0; int nInputs = 0; - // Sigops counting. We need to do it again because of P2SH. - uint64_t nSigOpsCount = 0; - auto currentBlockSize = - ::GetSerializeSize(block, SER_NETWORK, PROTOCOL_VERSION); - auto nMaxSigOpsCount = GetMaxBlockSigOpsCount(currentBlockSize); - CDiskTxPos pos(pindex->GetBlockPos(), GetSizeOfCompactSize(block.vtx.size())); std::vector<std::pair<uint256, CDiskTxPos>> vPos; @@ -3173,7 +3188,7 @@ "non-final transaction"); } - if (nMedianPastTime >= consensusParams.hfStartTime && + if (IsUAHFenabled(consensusParams, nMedianPastTime) && nHeight <= consensusParams.antiReplayOpReturnSunsetHeight) { for (const CTxOut &o : tx.vout) { if (o.scriptPubKey.IsCommitment(