diff --git a/src/validation.cpp b/src/validation.cpp --- a/src/validation.cpp +++ b/src/validation.cpp @@ -310,6 +310,11 @@ return IsMagneticAnomalyEnabled(config, chainActive.Tip()); } +static bool IsGreatWallEnabledForCurrentBlock(const Config &config) { + AssertLockHeld(cs_main); + return IsGreatWallEnabled(config, chainActive.Tip()); +} + // Command-line argument "-replayprotectionactivationtime=" will // cause the node to switch to replay protected SigHash ForkID value when the // median timestamp of the previous 11 blocks is greater than or equal to @@ -635,6 +640,10 @@ extraFlags |= SCRIPT_ENABLE_CHECKDATASIG; } + if (IsGreatWallEnabledForCurrentBlock(config)) { + extraFlags |= SCRIPT_ENABLE_SCHNORR; + } + // Check inputs based on the set of flags we activate. uint32_t scriptVerifyFlags = STANDARD_SCRIPT_VERIFY_FLAGS; if (!config.GetChainParams().RequireStandard()) { @@ -1575,6 +1584,14 @@ flags |= SCRIPT_VERIFY_CLEANSTACK; } + // When the great wall fork is enabled, we start accepting 65/64-byte + // Schnorr signatures in CHECKSIG and CHECKDATASIG respectively, and their + // verify variants. We also stop accepting 65 byte signatures in + // CHECKMULTISIG and its verify variant. + if (IsGreatWallEnabled(config, pChainTip)) { + flags |= SCRIPT_ENABLE_SCHNORR; + } + // We make sure this node will have replay protection during the next hard // fork. if (IsReplayProtectionEnabled(config, pChainTip)) { @@ -1909,10 +1926,13 @@ nTimeCallbacks * MILLI / nBlocksTotal); // If we just activated the replay protection with that block, it means - // transaction in the mempool are now invalid. As a result, we need to clear - // the mempool. - if (IsReplayProtectionEnabled(config, pindex) && - !IsReplayProtectionEnabled(config, pindex->pprev)) { + // transactions in the mempool are now invalid. As a result, we need to + // clear the mempool. We also need to clear when Schnorr activates to + // expunge any possible 64/65-byte signatures. + if ((IsReplayProtectionEnabled(config, pindex) && + !IsReplayProtectionEnabled(config, pindex->pprev)) || + (IsGreatWallEnabled(config, pindex) && + !IsGreatWallEnabled(config, pindex->pprev))) { g_mempool.clear(); } @@ -2209,8 +2229,12 @@ // remove transactions that are replay protected 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 (IsReplayProtectionEnabled(config, pindexDelete) && - !IsReplayProtectionEnabled(config, pindexDelete->pprev)) { + // Likewise we clear out the mempool when Schnorr deactivates, to get rid + // of any Schnorr sigs. + if ((IsReplayProtectionEnabled(config, pindexDelete) && + !IsReplayProtectionEnabled(config, pindexDelete->pprev)) || + (IsGreatWallEnabled(config, pindexDelete) && + !IsGreatWallEnabled(config, pindexDelete->pprev))) { LogPrint(BCLog::MEMPOOL, "Clearing mempool for reorg"); g_mempool.clear(); diff --git a/test/functional/abc-schnorr-checksig-activation.py b/test/functional/abc-schnorr-checksig-activation.py new file mode 100755 --- /dev/null +++ b/test/functional/abc-schnorr-checksig-activation.py @@ -0,0 +1,343 @@ +#!/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 tests the activation of Schnorr transaction signatures: +- rejection prior to upgrade both in mempool and blocks. +- acceptance after upgrade both in mempool and blocks. +- advance and rewind mempool drop tests. (note: advance test requires + a temporary patch to bitcoind; see fakeDER comment below) + +Derived from abc-replay-protection.py +""" + +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 +GREAT_WALL_START_TIME = 2000000000 + +# If we don't do this, autoreplay protection will activate simultaneous with +# great_wall and schnorr sigs will mysteriously fail. +REPLAY_PROTECTION_START_TIME = GREAT_WALL_START_TIME*2 + +# Error due to passing a Schnorr signature before upgrade. +BAD_SIG_ERROR = b'mandatory-script-verify-flag-failed (Non-canonical DER signature)' +RPC_BAD_SIG_ERROR = "16: " + \ + BAD_SIG_ERROR.decode("utf-8") +# Error due to passing Schnorr into multisig after upgrade. +BAD_MULTISIG_ERROR = b'mandatory-script-verify-flag-failed (Signature cannot be 65 bytes in CHECKMULTISIG)' +RPC_BAD_MULTISIG_ERROR = "16: " + \ + BAD_MULTISIG_ERROR.decode("utf-8") +# Error due to invalid signature when STRICTENC is on +NULLFAIL_ERROR = b'mandatory-script-verify-flag-failed (Signature must be zero for failed CHECK(MULTI)SIG operation)' +RPC_NULLFAIL_ERROR = "16: " + \ + NULLFAIL_ERROR.decode("utf-8") + +# For normal test running: +fakeDER = b'' + +# To properly test activation, we need to make txes with 64 byte ECDSA sigs. +# The easiest way to do this is to fake them, and then temporarily modify +# VerifySignature in src/script/interpreter.cpp to always `return true;` +# for ECDSA sigs, instead of `return pubkey.VerifyECDSA(sighash, vchSig);` +# Once that patch is done, you can uncomment the following and tests should +# pass. +#fakeDER = bytes.fromhex('303e021d4444444444444444444444444444444444444444444444444444444444021d4444444444444444444444444444444444444444444444444444444444') + +assert len(fakeDER) in [0, 64] + + +class PreviousSpendableOutput(object): + + def __init__(self, tx=CTransaction(), n=-1): + self.tx = tx + self.n = n # the output we're spending + + +class SchnorrActivationTest(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', + "-greatwallactivationtime=%d" % GREAT_WALL_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) + network_thread_start() + self.nodes[0].setmocktime(GREAT_WALL_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 its coinbase can be spent by a later block + def save_spendable_output(): + spendable_outputs.append(self.tip) + + # get a coinbase 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(199): + 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"Schnorr!"*4) + public_key = private_key.get_pubkey() # uncompressed + + def create_fund_and_spend_tx(spend, multi=False, fakesig=None): + if multi: + script = CScript([OP_1, public_key, OP_1, OP_CHECKMULTISIG]) + else: + script = CScript([public_key, OP_CHECKSIG]) + + # Fund transaction + 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 = SIGHASH_ALL | SIGHASH_FORKID + hashbyte = bytes([sighashtype & 0xff]) + sighash = SignatureHashForkId( + script, txspend, 0, sighashtype, 50 * COIN) + if fakesig: + sig = fakesig + hashbyte + else: + sig = private_key.sign_schnorr(sighash) + hashbyte + if multi: + txspend.vin[0].scriptSig = CScript([b'', sig]) + else: + 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 + + txfund, txspend = create_fund_and_spend_tx(out[0]) + multisigtxfund, multisigtxspend = create_fund_and_spend_tx( + out[1], multi=True) + send_transaction_to_mempool(txfund) + send_transaction_to_mempool(multisigtxfund) + + block(1) + update_block(1, [txfund, multisigtxfund]) + yield accepted() + + # We are before the upgrade, no Schnorrs get in the mempool. + assert_raises_rpc_error(-26, RPC_BAD_SIG_ERROR, + node.sendrawtransaction, ToHex(txspend)) + assert_raises_rpc_error(-26, RPC_BAD_SIG_ERROR, + node.sendrawtransaction, ToHex(multisigtxspend)) + + # And block containing them are rejected as well. + block(2) + update_block(2, [txspend]) + yield rejected(RejectResult(16, b'blk-bad-inputs')) + # Rewind bad block + tip(1) + + block(3) + update_block(3, [multisigtxspend]) + yield rejected(RejectResult(16, b'blk-bad-inputs')) + # Rewind bad block + tip(1) + + if fakeDER: + fakedtxfund, fakedtxspend = create_fund_and_spend_tx( + out[5], fakesig=fakeDER) + send_transaction_to_mempool(fakedtxfund) + block(4) + update_block(4, [fakedtxfund]) + yield accepted() + + # Create a block that would activate the schnorr + bfork = block(5555) + bfork.nTime = GREAT_WALL_START_TIME - 1 + update_block(5555, []) + yield accepted() + + for i in range(5): + block(5200 + 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'], + GREAT_WALL_START_TIME - 1) + + # We are just before the upgrade, still no Schnorrs get in the mempool, + assert_raises_rpc_error(-26, RPC_BAD_SIG_ERROR, + node.sendrawtransaction, ToHex(txspend)) + assert_raises_rpc_error(-26, RPC_BAD_SIG_ERROR, + node.sendrawtransaction, ToHex(multisigtxspend)) + # ... nor in blocks. + block(10) + update_block(10, [txspend]) + yield rejected(RejectResult(16, b'blk-bad-inputs')) + # Rewind bad block + tip(5204) + block(11) + update_block(11, [multisigtxspend]) + yield rejected(RejectResult(16, b'blk-bad-inputs')) + # Rewind bad block + tip(5204) + + if fakeDER: + # Throw a "valid" 65-byte ECDSA signature into the mempool just + # prior to the activation. + faked_tx_id = send_transaction_to_mempool(fakedtxspend) + + # Activate the Schnorr! + block(5556) + yield accepted() + + if fakeDER: + # The 65-byte ECDSA sig must be ejected. + assert(faked_tx_id not in set(node.getrawmempool())) + # If we try to re-add it, it's invalid Schnorr and hence fails + # VerifySignature, and then raises NULLFAIL. + assert_raises_rpc_error(-26, RPC_NULLFAIL_ERROR, + node.sendrawtransaction, ToHex(fakedtxspend)) + # And it can't be mined either... + block(15) + update_block(15, [fakedtxspend]) + yield rejected(RejectResult(16, b'blk-bad-inputs')) + # Rewind bad block + tip(5556) + + # The multisig throws a different error now + assert_raises_rpc_error(-26, RPC_BAD_MULTISIG_ERROR, + node.sendrawtransaction, ToHex(multisigtxspend)) + # And it still can't be mined + block(16) + update_block(16, [multisigtxspend]) + yield rejected(RejectResult(16, b'blk-bad-inputs')) + # Rewind bad block + tip(5556) + + # The Schnorr transaction is now valid + spend_tx_id = send_transaction_to_mempool(txspend) + # It can also be mined + block(21) + update_block(21, [txspend]) + yield accepted() + + # Ok, now we check if a reorg work properly accross the activation. + postforkblockid = node.getbestblockhash() + node.invalidateblock(postforkblockid) + assert(spend_tx_id in set(node.getrawmempool())) + + # Deactivating upgrade. + forkblockid = node.getbestblockhash() + node.invalidateblock(forkblockid) + assert(spend_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(spend_tx_id not in set(node.getrawmempool())) + + +if __name__ == '__main__': + SchnorrActivationTest().main()