Changeset View
Changeset View
Standalone View
Standalone View
test/functional/feature_nulldummy.py
#!/usr/bin/env python3 | #!/usr/bin/env python3 | ||||
# Copyright (c) 2016 The Bitcoin Core developers | # Copyright (c) 2016 The Bitcoin Core developers | ||||
# Copyright (c) 2019 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. | ||||
from test_framework.test_framework import BitcoinTestFramework | from test_framework.blocktools import create_coinbase, create_block, make_conform_to_ctor | ||||
from test_framework.util import * | from test_framework.cdefs import GREAT_WALL_ACTIVATION_TIME | ||||
from test_framework.mininode import CTransaction, network_thread_start | from test_framework.mininode import CTransaction | ||||
from test_framework.blocktools import create_coinbase, create_block | |||||
from test_framework.script import CScript | from test_framework.script import CScript | ||||
from test_framework.test_framework import BitcoinTestFramework | |||||
from test_framework.util import hex_str_to_bytes, bytes_to_hex_str, assert_equal, assert_raises_rpc_error | |||||
from io import BytesIO | from io import BytesIO | ||||
import time | import time | ||||
IN_THE_FUTURE = int(time.time()) * 2 | |||||
deadalnix: No. | |||||
NULLDUMMY_ERROR = "64: non-mandatory-script-verify-flag (Dummy CHECKMULTISIG argument must be zero)" | NULLDUMMY_ERROR = "64: non-mandatory-script-verify-flag (Dummy CHECKMULTISIG argument must be zero)" | ||||
def trueDummy(tx): | def trueDummy(tx): | ||||
scriptSig = CScript(tx.vin[0].scriptSig) | scriptSig = CScript(tx.vin[0].scriptSig) | ||||
newscript = [] | newscript = [] | ||||
for i in scriptSig: | for i in scriptSig: | ||||
if (len(newscript) == 0): | if (len(newscript) == 0): | ||||
assert(len(i) == 0) | assert(len(i) == 0) | ||||
newscript.append(b'\x51') | newscript.append(b'\x51') | ||||
else: | else: | ||||
newscript.append(i) | newscript.append(i) | ||||
tx.vin[0].scriptSig = CScript(newscript) | tx.vin[0].scriptSig = CScript(newscript) | ||||
tx.rehash() | tx.rehash() | ||||
''' | ''' | ||||
This test is meant to exercise NULLDUMMY softfork. | This test is meant to exercise NULLDUMMY enforcement during 'great wall' | ||||
Connect to a single node. | protocol upgrade. | ||||
Generate 2 blocks (save the coinbases for later). | |||||
Generate 427 more blocks. | |||||
[Policy/Consensus] Check that NULLDUMMY compliant transactions are accepted in the 430th block. | |||||
[Policy] Check that non-NULLDUMMY transactions are rejected before activation. | |||||
[Consensus] Check that the new NULLDUMMY rules are not enforced on the 431st block. | |||||
[Policy/Consensus] Check that the new NULLDUMMY rules are enforced on the 432nd block. | |||||
''' | ''' | ||||
def fetch_best_header(node): | |||||
deadalnixUnsubmitted Not Done Inline ActionsWhile I have nothing aainst these per se, mixing refactoring with logic change makes the hole changeset very hard to work with. Please split refactoring out of this diff so we can see clearly what logic changed. deadalnix: While I have nothing aainst these per se, mixing refactoring with logic change makes the hole… | |||||
return LastBlock(node.getblockheader(node.getbestblockhash())) | |||||
class LastBlock: | |||||
def __init__(self, header): | |||||
self.header = header | |||||
def __getattr__(self, name): | |||||
return self.header[name] | |||||
def tip(self): | |||||
return int(self.hash, 16) | |||||
class NULLDUMMYTest(BitcoinTestFramework): | class NULLDUMMYTest(BitcoinTestFramework): | ||||
def set_test_params(self): | def set_test_params(self): | ||||
self.lastblock = None | |||||
self.num_nodes = 1 | self.num_nodes = 1 | ||||
self.setup_clean_chain = True | self.setup_clean_chain = True | ||||
self.extra_args = [['-whitelist=127.0.0.1']] | |||||
self.extra_args = [[ | |||||
'-whitelist=127.0.0.1', | |||||
'-greatwallactivationtime=%d' % GREAT_WALL_ACTIVATION_TIME, | |||||
deadalnixUnsubmitted Not Done Inline ActionsIsn't this completely redundant ? deadalnix: Isn't this completely redundant ? | |||||
"-replayprotectionactivationtime=%d" % (IN_THE_FUTURE)]] | |||||
# NULLDUMMY is NOT activated at the beginning of the test. | |||||
self.mocktime = GREAT_WALL_ACTIVATION_TIME - 1000 | |||||
def run_test(self): | def run_test(self): | ||||
self.address = self.nodes[0].getnewaddress() | node = self.nodes[0] | ||||
self.ms_address = self.nodes[0].addmultisigaddress(1, [self.address]) | self.address = node.getnewaddress() | ||||
self.ms_address = node.addmultisigaddress(1, [self.address]) | |||||
network_thread_start() | # Create and mature coinbase for spending | ||||
self.coinbase_blocks = self.nodes[0].generate(2) # Block 2 | self.coinbase_blocks = node.generate(2) | ||||
coinbase_txid = [] | coinbase_txid = [] | ||||
for i in self.coinbase_blocks: | for i in self.coinbase_blocks: | ||||
coinbase_txid.append(self.nodes[0].getblock(i)['tx'][0]) | coinbase_txid.append(node.getblock(i)['tx'][0]) | ||||
self.nodes[0].generate(427) # Block 429 | node.generate(101) | ||||
self.lastblockhash = self.nodes[0].getbestblockhash() | |||||
self.tip = int("0x" + self.lastblockhash, 0) | self.lastblock = fetch_best_header(node) | ||||
self.lastblockheight = 429 | |||||
self.lastblocktime = int(time.time()) + 429 | |||||
self.log.info( | self.log.info( | ||||
"Test 1: NULLDUMMY compliant base transactions should be accepted to mempool and mined before activation [430]") | "Test 1 [Policy/Consensus]: NULLDUMMY compliant base transactions should be accepted to mempool and mined before activation") | ||||
test1txs = [self.create_transaction( | test1txs = [] | ||||
self.nodes[0], coinbase_txid[0], self.ms_address, 49)] | test1txs.append(self.create_transaction( | ||||
txid1 = self.nodes[0].sendrawtransaction( | node, coinbase_txid[0], self.ms_address, 49)) | ||||
txid1 = node.sendrawtransaction( | |||||
bytes_to_hex_str(test1txs[0].serialize()), True) | bytes_to_hex_str(test1txs[0].serialize()), True) | ||||
test1txs.append(self.create_transaction( | test1txs.append(self.create_transaction( | ||||
self.nodes[0], txid1, self.ms_address, 48)) | node, txid1, self.ms_address, 48)) | ||||
txid2 = self.nodes[0].sendrawtransaction( | txid2 = node.sendrawtransaction( | ||||
bytes_to_hex_str(test1txs[1].serialize()), True) | bytes_to_hex_str(test1txs[1].serialize()), True) | ||||
self.block_submit(self.nodes[0], test1txs, True) | |||||
self.block_submit(node, test1txs, accept=True) | |||||
self.log.info( | self.log.info( | ||||
"Test 2: Non-NULLDUMMY base multisig transaction should not be accepted to mempool before activation") | "Test 2 [Policy]: Non-NULLDUMMY base multisig transaction should not be accepted to mempool before activation") | ||||
test2tx = self.create_transaction( | test2tx = self.create_transaction( | ||||
self.nodes[0], txid2, self.ms_address, 48) | node, txid2, self.ms_address, 48) | ||||
trueDummy(test2tx) | trueDummy(test2tx) | ||||
txid4 = test2tx.hash | |||||
assert_raises_rpc_error(-26, NULLDUMMY_ERROR, | assert_raises_rpc_error(-26, NULLDUMMY_ERROR, | ||||
self.nodes[0].sendrawtransaction, bytes_to_hex_str(test2tx.serialize()), True) | node.sendrawtransaction, bytes_to_hex_str(test2tx.serialize()), True) | ||||
self.log.info( | |||||
"Test 3 [Consensus]:!Non-NULLDUMMY base transactions should be accepted in a block before activation") | |||||
self.block_submit(node, [test2tx], accept=True) | |||||
self.activate_nulldummy_enforcement(node) | |||||
self.log.info( | |||||
"Test 4 [Consensus]: Non-NULLDUMMY base multisig transaction is invalid after activation") | |||||
test4tx = self.create_transaction(node, txid4, self.address, amount=47) | |||||
trueDummy(test4tx) | |||||
assert_raises_rpc_error(-26, NULLDUMMY_ERROR, node.sendrawtransaction, | |||||
bytes_to_hex_str(test4tx.serialize()), True) | |||||
self.block_submit(node, [test4tx], accept=False) | |||||
self.log.info( | self.log.info( | ||||
"Test 3: Non-NULLDUMMY base transactions should be accepted in a block before activation [431]") | "Test 5 [Consensus]: NULLDUMMY compliant base transactions should be accepted to mempool and in block after activation") | ||||
self.block_submit(self.nodes[0], [test2tx], True) | test5tx = self.create_transaction(node, txid4, self.address, amount=47) | ||||
node.sendrawtransaction(bytes_to_hex_str(test5tx.serialize()), True) | |||||
self.block_submit(node, [test5tx], accept=True) | |||||
def create_transaction(self, node, txid, to_address, amount): | def create_transaction(self, node, txid, to_address, amount): | ||||
inputs = [{"txid": txid, "vout": 0}] | inputs = [{"txid": txid, "vout": 0}] | ||||
outputs = {to_address: amount} | outputs = {to_address: amount} | ||||
rawtx = node.createrawtransaction(inputs, outputs) | rawtx = node.createrawtransaction(inputs, outputs) | ||||
signresult = node.signrawtransaction(rawtx) | signresult = node.signrawtransaction(rawtx) | ||||
tx = CTransaction() | tx = CTransaction() | ||||
f = BytesIO(hex_str_to_bytes(signresult['hex'])) | f = BytesIO(hex_str_to_bytes(signresult['hex'])) | ||||
tx.deserialize(f) | tx.deserialize(f) | ||||
return tx | return tx | ||||
def block_submit(self, node, txs, accept=False): | def block_submit(self, node, txs, accept=False): | ||||
block = create_block(self.tip, create_coinbase( | block = create_block(self.lastblock.tip(), create_coinbase( | ||||
self.lastblockheight + 1), self.lastblocktime + 1) | self.lastblock.height + 1), self.lastblock.time + 1) | ||||
block.nVersion = 4 | block.nVersion = 4 | ||||
for tx in txs: | block.vtx.extend(txs) | ||||
tx.rehash() | make_conform_to_ctor(block) | ||||
block.vtx.append(tx) | |||||
block.vtx = [block.vtx[0]] + \ | |||||
sorted(block.vtx[1:], key=lambda tx: tx.get_id()) | |||||
block.hashMerkleRoot = block.calc_merkle_root() | block.hashMerkleRoot = block.calc_merkle_root() | ||||
block.rehash() | block.rehash() | ||||
block.solve() | block.solve() | ||||
node.submitblock(bytes_to_hex_str(block.serialize())) | node.submitblock(bytes_to_hex_str(block.serialize())) | ||||
if (accept): | if (accept): | ||||
assert_equal(node.getbestblockhash(), block.hash) | assert_equal(node.getbestblockhash(), block.hash) | ||||
self.tip = block.sha256 | self.lastblock = fetch_best_header(node) | ||||
self.lastblockhash = block.hash | |||||
self.lastblocktime += 1 | |||||
self.lastblockheight += 1 | |||||
else: | else: | ||||
assert_equal(node.getbestblockhash(), self.lastblockhash) | assert_equal(node.getbestblockhash(), self.lastblock.hash) | ||||
def activate_nulldummy_enforcement(self, node): | |||||
while True: | |||||
self.lastblock = fetch_best_header(node) | |||||
if self.lastblock.mediantime >= GREAT_WALL_ACTIVATION_TIME: | |||||
return | |||||
next_time = self.lastblock.time + 600 | |||||
coinbase = create_coinbase(self.lastblock.height + 1) | |||||
block = create_block(self.lastblock.tip(), coinbase, next_time) | |||||
node.setmocktime(next_time) | |||||
node.submitblock(bytes_to_hex_str(block.serialize())) | |||||
if __name__ == '__main__': | if __name__ == '__main__': | ||||
NULLDUMMYTest().main() | NULLDUMMYTest().main() |
No.