def run_test(self): test = TestManager(self, self.options.tmpdir) test.add_all_connections(self.nodes) self.tip = None self.block_time = None NetworkThread().start() # Start up network handling in another thread test.run()
def run_test(self): test = TestManager(self, self.options.tmpdir) test.add_all_connections(self.nodes) self.tip = None self.block_time = None network_thread_start() test.run()
def run_test(self): # Set up the comparison tool TestManager test = TestManager(self, self.options.tmpdir) test.add_all_connections(self.nodes) # Load scripts self.scripts = ScriptTestFile([script_valid_file, script_invalid_file]) self.scripts.load_files() # Some variables we re-use between test instances (to build blocks) self.tip = None self.block_time = None NetworkThread().start() # Start up network handling in another thread test.run()
class BIP135ForksTest(ComparisonTestFramework): def __init__(self): super().__init__() self.setup_clean_chain = True self.num_nodes = 1 self.defined_forks = [ "bip135test%d" % i for i in range(7,9) ] def setup_network(self): ''' sets up with a custom forks.csv to test threshold / grace conditions also recomputes deployment start times since node is shut down and restarted between main test sections. ''' # write a custom forks.csv file containing test deployments. # blocks are 1 second apart by default in this regtest fork_csv_filename = os.path.join(self.options.tmpdir, "forks.csv") # forks.csv fields: # network,bit,name,starttime,timeout,windowsize,threshold,minlockedblocks,minlockedtime,gbtforce with open(fork_csv_filename, 'wt') as fh: # use current time to compute offset starttimes self.init_time = int(time.time()) # starttimes are offset by 30 seconds from init_time self.fork_starttime = self.init_time + 30 fh.write( "# deployment info for network 'regtest':\n" + ########## GRACE PERIOD TESTING BITS (7-8) ############ # bit 7: Test threshold not reached and timeout exceeded. Should result in failure to activate "regtest,7,bip135test7,%d,%d,10,9,5,0,true\n" % (self.fork_starttime, self.fork_starttime + 50) + # bit 8: Test threshold reached and timeout exceeded. Should result in successful activation "regtest,8,bip135test8,%d,%d,10,8,5,0,true\n" % (self.fork_starttime, self.fork_starttime + 50) + ########## NOT USED SO FAR ############ "regtest,9,bip135test9,%d,999999999999,100,9,0,0,true\n" % (self.fork_starttime) ) self.nodes = start_nodes(1, self.options.tmpdir, extra_args=[['-debug', '-whitelist=127.0.0.1', "-forks=%s" % fork_csv_filename]], binary=[self.options.testbinary]) def run_test(self): self.test = TestManager(self, self.options.tmpdir) self.test.add_all_connections(self.nodes) NetworkThread().start() # Start up network handling in another thread self.test.run() def generate_blocks(self, number, version, test_blocks = []): for i in range(number): #logging.info ("generate_blocks: creating block on tip %x, height %d, time %d" % (self.tip, self.height, self.last_block_time + 1)) block = create_block(self.tip, create_coinbase(self.height), self.last_block_time + 1) block.nVersion = version block.rehash() block.solve() test_blocks.append([block, True]) self.last_block_time += 1 self.tip = block.sha256 self.height += 1 return test_blocks def get_bip135_status(self, key): info = self.nodes[0].getblockchaininfo() return info['bip135_forks'][key] def print_rpc_status(self): for f in self.defined_forks: info = self.nodes[0].getblockchaininfo() logging.info(info['bip135_forks'][f]) def test_BIP135GraceConditions(self): logging.info("begin test_BIP135GraceConditions test") node = self.nodes[0] self.tip = int("0x" + node.getbestblockhash(), 0) header = node.getblockheader("0x%x" % self.tip) assert_equal(header['height'], 0) # Test 1 # generate a block, seems necessary to get RPC working self.coinbase_blocks = self.nodes[0].generate(1) self.height = 2 self.last_block_time = int(time.time()) self.tip = int("0x" + node.getbestblockhash(), 0) test_blocks = self.generate_blocks(1, 0x20000000) # do not set bit 0 yet yield TestInstance(test_blocks, sync_every_block=False) bcinfo = self.nodes[0].getblockchaininfo() # check bits 7-15 , they should be in DEFINED for f in self.defined_forks: assert_equal(bcinfo['bip135_forks'][f]['bit'], int(f[10:])) assert_equal(bcinfo['bip135_forks'][f]['status'], 'defined') # move to starttime moved_to_started = False bcinfo = self.nodes[0].getblockchaininfo() tip_mediantime = int(bcinfo['mediantime']) while tip_mediantime < self.fork_starttime or self.height % 10: test_blocks = self.generate_blocks(1, 0x20000000) yield TestInstance(test_blocks, sync_every_block=False) bcinfo = self.nodes[0].getblockchaininfo() tip_mediantime = int(bcinfo['mediantime']) for f in self.defined_forks: # transition to STARTED must occur if this is true if tip_mediantime >= self.fork_starttime and self.height % 10 == 0: moved_to_started = True if moved_to_started: assert_equal(bcinfo['bip135_forks'][f]['status'], 'started') else: assert_equal(bcinfo['bip135_forks'][f]['status'], 'defined') # Lock one of them them in by producing 8 signaling blocks out of 10. # The one that is not locked in will be one block away from lock in. test_blocks = self.generate_blocks(8, 0x203fff80) # last two blocks don't need to signal test_blocks = self.generate_blocks(2, 0x20000000, test_blocks) yield TestInstance(test_blocks, sync_every_block=False) # check bits 7-8 , only 8 should be LOCKED_IN bcinfo = self.nodes[0].getblockchaininfo() logging.info("checking all grace period forks are locked in") activation_states = [ bcinfo['bip135_forks'][f]['status'] for f in self.defined_forks ] assert_equal(activation_states, ['started', 'locked_in' ]) # now we just check that they turn ACTIVE only when their configured # conditions are all met. Reminder: window size is 10 blocks, inter- # block time is 1 sec for the synthesized chain. # # Grace conditions for the bits 7-8: # ----------------------------------- # bit 7: minlockedblocks= 5, minlockedtime= 0 -> started next sync # bit 8: minlockedblocks= 5, minlockedtime= 0 -> activate next sync # check the forks supposed to activate just one period after lock-in ("at next sync") # and move the time to just before timeout. Bit 8 should become active and neither should fail. # Set the last block time to 6 seconds before the timeout...since blocks get mined one second # apart this will put the MTP at 1 second behind the timeout, and thus the activation will not fail. self.last_block_time = self.fork_starttime + 50 - 6 test_blocks = self.generate_blocks(10, 0x20000000) yield TestInstance(test_blocks, sync_every_block=False) bcinfo = self.nodes[0].getblockchaininfo() activation_states = [ bcinfo['bip135_forks'][f]['status'] for f in self.defined_forks ] assert_equal(activation_states, ['started', 'active' ]) # Move the time to the timeout. Bit 7 never locked in and should be 'failed', # whereas, Bit 8 should still be active. test_blocks = self.generate_blocks(10, 0x20000000) yield TestInstance(test_blocks, sync_every_block=False) bcinfo = self.nodes[0].getblockchaininfo() activation_states = [ bcinfo['bip135_forks'][f]['status'] for f in self.defined_forks ] assert_equal(activation_states, ['failed', 'active' ]) def get_tests(self): ''' run various tests ''' # CSV (bit 0) for backward compatibility with BIP9 for test in itertools.chain( self.test_BIP135GraceConditions(), # test grace periods ): yield test
class BIP9SoftForksTest(ComparisonTestFramework): def set_test_params(self): self.num_nodes = 1 self.extra_args = [['-whitelist=127.0.0.1']] self.setup_clean_chain = True def run_test(self): self.test = TestManager(self, self.options.tmpdir) self.test.add_all_connections(self.nodes) network_thread_start() self.test.run() def create_transaction(self, node, coinbase, to_address, amount): from_txid = node.getblock(coinbase)['tx'][0] inputs = [{ "txid" : from_txid, "vout" : 0}] outputs = { to_address : amount } rawtx = node.createrawtransaction(inputs, outputs) tx = CTransaction() f = BytesIO(hex_str_to_bytes(rawtx)) tx.deserialize(f) tx.nVersion = 2 return tx def sign_transaction(self, node, tx): signresult = node.signrawtransaction(bytes_to_hex_str(tx.serialize())) tx = CTransaction() f = BytesIO(hex_str_to_bytes(signresult['hex'])) tx.deserialize(f) return tx def generate_blocks(self, number, version, test_blocks = []): for i in range(number): block = create_block(self.tip, create_coinbase(self.height), self.last_block_time + 1) block.nVersion = version block.rehash() block.solve() test_blocks.append([block, True]) self.last_block_time += 1 self.tip = block.sha256 self.height += 1 return test_blocks def get_bip9_status(self, key): info = self.nodes[0].getblockchaininfo() return info['bip9_softforks'][key] def test_BIP(self, bipName, activated_version, invalidate, invalidatePostSignature, bitno): assert_equal(self.get_bip9_status(bipName)['status'], 'defined') assert_equal(self.get_bip9_status(bipName)['since'], 0) # generate some coins for later self.coinbase_blocks = self.nodes[0].generate(2) self.height = 3 # height of the next block to build self.tip = int("0x" + self.nodes[0].getbestblockhash(), 0) self.nodeaddress = self.nodes[0].getnewaddress() self.last_block_time = int(time.time()) assert_equal(self.get_bip9_status(bipName)['status'], 'defined') assert_equal(self.get_bip9_status(bipName)['since'], 0) tmpl = self.nodes[0].getblocktemplate({}) assert(bipName not in tmpl['rules']) assert(bipName not in tmpl['vbavailable']) assert_equal(tmpl['vbrequired'], 0) assert_equal(tmpl['version'], 0x20000000) # Test 1 # Advance from DEFINED to STARTED test_blocks = self.generate_blocks(141, 4) yield TestInstance(test_blocks, sync_every_block=False) assert_equal(self.get_bip9_status(bipName)['status'], 'started') assert_equal(self.get_bip9_status(bipName)['since'], 144) assert_equal(self.get_bip9_status(bipName)['statistics']['elapsed'], 0) assert_equal(self.get_bip9_status(bipName)['statistics']['count'], 0) tmpl = self.nodes[0].getblocktemplate({}) assert(bipName not in tmpl['rules']) assert_equal(tmpl['vbavailable'][bipName], bitno) assert_equal(tmpl['vbrequired'], 0) assert(tmpl['version'] & activated_version) # Test 1-A # check stats after max number of "signalling not" blocks such that LOCKED_IN still possible this period test_blocks = self.generate_blocks(36, 4, test_blocks) # 0x00000004 (signalling not) test_blocks = self.generate_blocks(10, activated_version) # 0x20000001 (signalling ready) yield TestInstance(test_blocks, sync_every_block=False) assert_equal(self.get_bip9_status(bipName)['statistics']['elapsed'], 46) assert_equal(self.get_bip9_status(bipName)['statistics']['count'], 10) assert_equal(self.get_bip9_status(bipName)['statistics']['possible'], True) # Test 1-B # check stats after one additional "signalling not" block -- LOCKED_IN no longer possible this period test_blocks = self.generate_blocks(1, 4, test_blocks) # 0x00000004 (signalling not) yield TestInstance(test_blocks, sync_every_block=False) assert_equal(self.get_bip9_status(bipName)['statistics']['elapsed'], 47) assert_equal(self.get_bip9_status(bipName)['statistics']['count'], 10) assert_equal(self.get_bip9_status(bipName)['statistics']['possible'], False) # Test 1-C # finish period with "ready" blocks, but soft fork will still fail to advance to LOCKED_IN test_blocks = self.generate_blocks(97, activated_version) # 0x20000001 (signalling ready) yield TestInstance(test_blocks, sync_every_block=False) assert_equal(self.get_bip9_status(bipName)['statistics']['elapsed'], 0) assert_equal(self.get_bip9_status(bipName)['statistics']['count'], 0) assert_equal(self.get_bip9_status(bipName)['statistics']['possible'], True) assert_equal(self.get_bip9_status(bipName)['status'], 'started') # Test 2 # Fail to achieve LOCKED_IN 100 out of 144 signal bit 1 # using a variety of bits to simulate multiple parallel softforks test_blocks = self.generate_blocks(50, activated_version) # 0x20000001 (signalling ready) test_blocks = self.generate_blocks(20, 4, test_blocks) # 0x00000004 (signalling not) test_blocks = self.generate_blocks(50, activated_version, test_blocks) # 0x20000101 (signalling ready) test_blocks = self.generate_blocks(24, 4, test_blocks) # 0x20010000 (signalling not) yield TestInstance(test_blocks, sync_every_block=False) assert_equal(self.get_bip9_status(bipName)['status'], 'started') assert_equal(self.get_bip9_status(bipName)['since'], 144) assert_equal(self.get_bip9_status(bipName)['statistics']['elapsed'], 0) assert_equal(self.get_bip9_status(bipName)['statistics']['count'], 0) tmpl = self.nodes[0].getblocktemplate({}) assert(bipName not in tmpl['rules']) assert_equal(tmpl['vbavailable'][bipName], bitno) assert_equal(tmpl['vbrequired'], 0) assert(tmpl['version'] & activated_version) # Test 3 # 108 out of 144 signal bit 1 to achieve LOCKED_IN # using a variety of bits to simulate multiple parallel softforks test_blocks = self.generate_blocks(57, activated_version) # 0x20000001 (signalling ready) test_blocks = self.generate_blocks(26, 4, test_blocks) # 0x00000004 (signalling not) test_blocks = self.generate_blocks(50, activated_version, test_blocks) # 0x20000101 (signalling ready) test_blocks = self.generate_blocks(10, 4, test_blocks) # 0x20010000 (signalling not) yield TestInstance(test_blocks, sync_every_block=False) # check counting stats and "possible" flag before last block of this period achieves LOCKED_IN... assert_equal(self.get_bip9_status(bipName)['statistics']['elapsed'], 143) assert_equal(self.get_bip9_status(bipName)['statistics']['count'], 107) assert_equal(self.get_bip9_status(bipName)['statistics']['possible'], True) assert_equal(self.get_bip9_status(bipName)['status'], 'started') # ...continue with Test 3 test_blocks = self.generate_blocks(1, activated_version) # 0x20000001 (signalling ready) yield TestInstance(test_blocks, sync_every_block=False) assert_equal(self.get_bip9_status(bipName)['status'], 'locked_in') assert_equal(self.get_bip9_status(bipName)['since'], 576) tmpl = self.nodes[0].getblocktemplate({}) assert(bipName not in tmpl['rules']) # Test 4 # 143 more version 536870913 blocks (waiting period-1) test_blocks = self.generate_blocks(143, 4) yield TestInstance(test_blocks, sync_every_block=False) assert_equal(self.get_bip9_status(bipName)['status'], 'locked_in') assert_equal(self.get_bip9_status(bipName)['since'], 576) tmpl = self.nodes[0].getblocktemplate({}) assert(bipName not in tmpl['rules']) # Test 5 # Check that the new rule is enforced spendtx = self.create_transaction(self.nodes[0], self.coinbase_blocks[0], self.nodeaddress, 1.0) invalidate(spendtx) spendtx = self.sign_transaction(self.nodes[0], spendtx) spendtx.rehash() invalidatePostSignature(spendtx) spendtx.rehash() block = create_block(self.tip, create_coinbase(self.height), self.last_block_time + 1) block.nVersion = activated_version block.vtx.append(spendtx) block.hashMerkleRoot = block.calc_merkle_root() block.rehash() block.solve() self.last_block_time += 1 self.tip = block.sha256 self.height += 1 yield TestInstance([[block, True]]) assert_equal(self.get_bip9_status(bipName)['status'], 'active') assert_equal(self.get_bip9_status(bipName)['since'], 720) tmpl = self.nodes[0].getblocktemplate({}) assert(bipName in tmpl['rules']) assert(bipName not in tmpl['vbavailable']) assert_equal(tmpl['vbrequired'], 0) assert(not (tmpl['version'] & (1 << bitno))) # Test 6 # Check that the new sequence lock rules are enforced spendtx = self.create_transaction(self.nodes[0], self.coinbase_blocks[1], self.nodeaddress, 1.0) invalidate(spendtx) spendtx = self.sign_transaction(self.nodes[0], spendtx) spendtx.rehash() invalidatePostSignature(spendtx) spendtx.rehash() block = create_block(self.tip, create_coinbase(self.height), self.last_block_time + 1) block.nVersion = 5 block.vtx.append(spendtx) block.hashMerkleRoot = block.calc_merkle_root() block.rehash() block.solve() self.last_block_time += 1 yield TestInstance([[block, False]]) # Restart all self.test.clear_all_connections() self.stop_nodes() self.nodes = [] shutil.rmtree(self.options.tmpdir + "/node0") self.setup_chain() self.setup_network() self.test.add_all_connections(self.nodes) network_thread_start() self.test.p2p_connections[0].wait_for_verack() def get_tests(self): for test in itertools.chain( self.test_BIP('csv', 0x20000001, self.sequence_lock_invalidate, self.donothing, 0), self.test_BIP('csv', 0x20000001, self.mtp_invalidate, self.donothing, 0), self.test_BIP('csv', 0x20000001, self.donothing, self.csv_invalidate, 0) ): yield test def donothing(self, tx): return def csv_invalidate(self, tx): """Modify the signature in vin 0 of the tx to fail CSV Prepends -1 CSV DROP in the scriptSig itself. """ tx.vin[0].scriptSig = CScript([OP_1NEGATE, OP_CHECKSEQUENCEVERIFY, OP_DROP] + list(CScript(tx.vin[0].scriptSig))) def sequence_lock_invalidate(self, tx): """Modify the nSequence to make it fails once sequence lock rule is activated (high timespan). """ tx.vin[0].nSequence = 0x00FFFFFF tx.nLockTime = 0 def mtp_invalidate(self, tx): """Modify the nLockTime to make it fails once MTP rule is activated.""" # Disable Sequence lock, Activate nLockTime tx.vin[0].nSequence = 0x90FFFFFF tx.nLockTime = self.last_block_time
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 set_test_params(self): self.num_nodes = 1 self.setup_clean_chain = True self.block_heights = {} self.tip = None self.blocks = {} self.excessive_block_size = 16 * ONE_MEGABYTE self.extra_args = [['-norelaypriority', '-whitelist=127.0.0.1', '-limitancestorcount=999999', '-limitancestorsize=999999', '-limitdescendantcount=999999', '-limitdescendantsize=999999', '-maxmempool=99999', "-monolithactivationtime=%d" % MONOLITH_START_TIME, "-excessiveblocksize=%d" % self.excessive_block_size]] 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.nodes[0].setmocktime(MONOLITH_START_TIME) 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 def next_block(self, number, spend=None, script=CScript([OP_TRUE]), block_size=0, extra_txns=0): 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() if spend == None: # We need to have something to spend to fill the block. assert_equal(block_size, 0) block = create_block(base_block_hash, coinbase, block_time) else: # all but one satoshi to fees coinbase.vout[0].nValue += spend.tx.vout[spend.n].nValue - 1 coinbase.rehash() block = create_block(base_block_hash, coinbase, block_time) # Make sure we have plenty engough to spend going forward. spendable_outputs = deque([spend]) def get_base_transaction(): # Create the new transaction tx = CTransaction() # Spend from one of the spendable outputs spend = spendable_outputs.popleft() tx.vin.append(CTxIn(COutPoint(spend.tx.sha256, spend.n))) # Add spendable outputs for i in range(4): tx.vout.append(CTxOut(0, CScript([OP_TRUE]))) spendable_outputs.append(PreviousSpendableOutput(tx, i)) return tx tx = get_base_transaction() # Make it the same format as transaction added for padding and save the size. # It's missing the padding output, so we add a constant to account for it. tx.rehash() base_tx_size = len(tx.serialize()) + 18 # If a specific script is required, add it. if script != None: tx.vout.append(CTxOut(1, script)) # Put some random data into the first transaction of the chain to randomize ids. tx.vout.append( CTxOut(0, CScript([random.randint(0, 256), OP_RETURN]))) # Add the transaction to the block self.add_transactions_to_block(block, [tx]) # Add transaction until we reach the expected transaction count for _ in range(extra_txns): self.add_transactions_to_block(block, [get_base_transaction()]) # If we have a block size requirement, just fill # the block until we get there current_block_size = len(block.serialize()) while current_block_size < block_size: # We will add a new transaction. That means the size of # the field enumerating how many transaction go in the block # may change. current_block_size -= len(ser_compact_size(len(block.vtx))) current_block_size += len(ser_compact_size(len(block.vtx) + 1)) # Create the new transaction tx = get_base_transaction() # Add padding to fill the block. script_length = block_size - current_block_size - base_tx_size if script_length > 510000: if script_length < 1000000: # Make sure we don't find ourselves in a position where we # need to generate a transaction smaller than what we expected. script_length = script_length // 2 else: script_length = 500000 script_output = CScript([b'\x00' * script_length]) tx.vout.append(CTxOut(0, script_output)) # Add the tx to the list of transactions to be included # in the block. self.add_transactions_to_block(block, [tx]) current_block_size += len(tx.serialize()) # Now that we added a bunch of transaction, we need to recompute # the merkle root. block.hashMerkleRoot = block.calc_merkle_root() # Check that the block size is what's expected if block_size > 0: assert_equal(len(block.serialize()), block_size) # 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): 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) 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() # Fork block bfork = block(5555) bfork.nTime = MONOLITH_START_TIME update_block(5555, []) test.blocks_and_transactions.append([self.tip, True]) # Get to one block of the May 15, 2018 HF activation for i in range(5): block(5100 + i) test.blocks_and_transactions.append([self.tip, True]) # Send it all to the node at once. yield test # collect spendable outputs now to avoid cluttering the code later on out = [] for i in range(100): out.append(get_spendable_output()) # Check that compact block also work for big blocks node = self.nodes[0] peer = TestNode() peer.add_connection(NodeConn('127.0.0.1', p2p_port(0), node, peer)) # Wait for connection to be etablished peer.wait_for_verack() # Wait for SENDCMPCT def received_sendcmpct(): return (peer.last_sendcmpct != None) wait_until(received_sendcmpct, timeout=30) sendcmpct = msg_sendcmpct() sendcmpct.version = 1 sendcmpct.announce = True peer.send_and_ping(sendcmpct) # Exchange headers def received_getheaders(): return (peer.last_getheaders != None) wait_until(received_getheaders, timeout=30) # Return the favor peer.send_message(peer.last_getheaders) # Wait for the header list def received_headers(): return (peer.last_headers != None) wait_until(received_headers, timeout=30) # It's like we know about the same headers ! peer.send_message(peer.last_headers) # Send a block b1 = block(1, spend=out[0], block_size=ONE_MEGABYTE + 1) yield accepted() # Checks the node to forward it via compact block def received_block(): return (peer.last_cmpctblock != None) wait_until(received_block, timeout=30) # Was it our block ? cmpctblk_header = peer.last_cmpctblock.header_and_shortids.header cmpctblk_header.calc_sha256() assert(cmpctblk_header.sha256 == b1.sha256) # Send a large block with numerous transactions. peer.clear_block_data() b2 = block(2, spend=out[1], extra_txns=70000, block_size=self.excessive_block_size - 1000) yield accepted() # Checks the node forwards it via compact block wait_until(received_block, timeout=30) # Was it our block ? cmpctblk_header = peer.last_cmpctblock.header_and_shortids.header cmpctblk_header.calc_sha256() assert(cmpctblk_header.sha256 == b2.sha256) # In order to avoid having to resend a ton of transactions, we invalidate # b2, which will send all its transactions in the mempool. node.invalidateblock(node.getbestblockhash()) # Let's send a compact block and see if the node accepts it. # Let's modify b2 and use it so that we can reuse the mempool. tx = b2.vtx[0] tx.vout.append(CTxOut(0, CScript([random.randint(0, 256), OP_RETURN]))) tx.rehash() b2.vtx[0] = tx b2.hashMerkleRoot = b2.calc_merkle_root() b2.solve() # Now we create the compact block and send it comp_block = HeaderAndShortIDs() comp_block.initialize_from_block(b2) peer.send_and_ping(msg_cmpctblock(comp_block.to_p2p())) # Check that compact block is received properly assert(int(node.getbestblockhash(), 16) == b2.sha256)
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 set_test_params(self): self.num_nodes = 1 self.setup_clean_chain = True self.block_heights = {} self.tip = None self.blocks = {} self.excessive_block_size = 100 * ONE_MEGABYTE self.extra_args = [['-whitelist=127.0.0.1', "-monolithactivationtime=%d" % MONOLITH_START_TIME, "-replayprotectionactivationtime=%d" % ( 2 * MONOLITH_START_TIME), "-excessiveblocksize=%d" % self.excessive_block_size]] 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.nodes[0].setmocktime(MONOLITH_START_TIME) 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, value, script=CScript([OP_TRUE])): tx = create_transaction(spend.tx, spend.n, b"", value, script) return tx def next_block(self, number, spend=None, script=CScript([OP_TRUE]), block_size=0, extra_sigops=0): 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() if spend == None: # We need to have something to spend to fill the block. assert_equal(block_size, 0) block = create_block(base_block_hash, coinbase, block_time) else: # all but one satoshi to fees coinbase.vout[0].nValue += spend.tx.vout[spend.n].nValue - 1 coinbase.rehash() block = create_block(base_block_hash, coinbase, block_time) # Make sure we have plenty engough to spend going forward. spendable_outputs = deque([spend]) def get_base_transaction(): # Create the new transaction tx = CTransaction() # Spend from one of the spendable outputs spend = spendable_outputs.popleft() tx.vin.append(CTxIn(COutPoint(spend.tx.sha256, spend.n))) # Add spendable outputs for i in range(4): tx.vout.append(CTxOut(0, CScript([OP_TRUE]))) spendable_outputs.append(PreviousSpendableOutput(tx, i)) return tx tx = get_base_transaction() # Make it the same format as transaction added for padding and save the size. # It's missing the padding output, so we add a constant to account for it. tx.rehash() base_tx_size = len(tx.serialize()) + 18 # If a specific script is required, add it. if script != None: tx.vout.append(CTxOut(1, script)) # Put some random data into the first transaction of the chain to randomize ids. tx.vout.append( CTxOut(0, CScript([random.randint(0, 256), OP_RETURN]))) # Add the transaction to the block self.add_transactions_to_block(block, [tx]) # If we have a block size requirement, just fill # the block until we get there current_block_size = len(block.serialize()) while current_block_size < block_size: # We will add a new transaction. That means the size of # the field enumerating how many transaction go in the block # may change. current_block_size -= len(ser_compact_size(len(block.vtx))) current_block_size += len(ser_compact_size(len(block.vtx) + 1)) # Create the new transaction tx = get_base_transaction() # Add padding to fill the block. script_length = block_size - current_block_size - base_tx_size if script_length > 510000: if script_length < 1000000: # Make sure we don't find ourselves in a position where we # need to generate a transaction smaller than what we expected. script_length = script_length // 2 else: 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, script_output)) # Add the tx to the list of transactions to be included # in the block. self.add_transactions_to_block(block, [tx]) current_block_size += len(tx.serialize()) # Now that we added a bunch of transaction, we need to recompute # the merkle root. block.hashMerkleRoot = block.calc_merkle_root() # Check that the block size is what's expected if block_size > 0: assert_equal(len(block.serialize()), block_size) # 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): node = self.nodes[0] self.genesis_hash = int(node.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) 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()) # Let's build some blocks and test them. for i in range(15): n = i + 1 block(n, spend=out[i], block_size=n * ONE_MEGABYTE // 2) yield accepted() # Start moving MTP forward bfork = block(5555, out[15], block_size=8 * ONE_MEGABYTE) bfork.nTime = MONOLITH_START_TIME - 1 update_block(5555, []) yield accepted() # Get to one block of the May 15, 2018 HF activation for i in range(5): block(5100 + i) test.blocks_and_transactions.append([self.tip, True]) yield test # Check that the MTP is just before the configured fork point. assert_equal(node.getblockheader(node.getbestblockhash())['mediantime'], MONOLITH_START_TIME - 1) # Before we acivate the May 15, 2018 HF, 8MB is the limit. block(4444, spend=out[16], block_size=8 * ONE_MEGABYTE + 1) yield rejected(RejectResult(16, b'bad-blk-length')) # Rewind bad block. tip(5104) # Actiavte the May 15, 2018 HF block(5556) yield accepted() # Now MTP is exactly the fork time. Bigger blocks are now accepted. assert_equal(node.getblockheader(node.getbestblockhash())['mediantime'], MONOLITH_START_TIME) # block of maximal size block(17, spend=out[16], block_size=self.excessive_block_size) yield accepted() # Reject oversized blocks with bad-blk-length error block(18, spend=out[17], block_size=self.excessive_block_size + 1) yield rejected(RejectResult(16, b'bad-blk-length')) # Rewind bad block. tip(17) # Accept many sigops lots_of_checksigs = CScript( [OP_CHECKSIG] * MAX_BLOCK_SIGOPS_PER_MB) block(19, spend=out[17], script=lots_of_checksigs, block_size=ONE_MEGABYTE) yield accepted() block(20, spend=out[18], script=lots_of_checksigs, block_size=ONE_MEGABYTE, extra_sigops=1) yield rejected(RejectResult(16, b'bad-blk-sigops')) # Rewind bad block tip(19) # Accept 40k sigops per block > 1MB and <= 2MB block(21, spend=out[18], script=lots_of_checksigs, extra_sigops=MAX_BLOCK_SIGOPS_PER_MB, block_size=ONE_MEGABYTE + 1) yield accepted() # Accept 40k sigops per block > 1MB and <= 2MB block(22, spend=out[19], script=lots_of_checksigs, extra_sigops=MAX_BLOCK_SIGOPS_PER_MB, block_size=2 * ONE_MEGABYTE) yield accepted() # Reject more than 40k sigops per block > 1MB and <= 2MB. block(23, spend=out[20], script=lots_of_checksigs, extra_sigops=MAX_BLOCK_SIGOPS_PER_MB + 1, block_size=ONE_MEGABYTE + 1) yield rejected(RejectResult(16, b'bad-blk-sigops')) # Rewind bad block tip(22) # Reject more than 40k sigops per block > 1MB and <= 2MB. block(24, spend=out[20], script=lots_of_checksigs, extra_sigops=MAX_BLOCK_SIGOPS_PER_MB + 1, block_size=2 * ONE_MEGABYTE) yield rejected(RejectResult(16, b'bad-blk-sigops')) # Rewind bad block tip(22) # Accept 60k sigops per block > 2MB and <= 3MB block(25, spend=out[20], script=lots_of_checksigs, extra_sigops=2 * MAX_BLOCK_SIGOPS_PER_MB, block_size=2 * ONE_MEGABYTE + 1) yield accepted() # Accept 60k sigops per block > 2MB and <= 3MB block(26, spend=out[21], script=lots_of_checksigs, extra_sigops=2 * MAX_BLOCK_SIGOPS_PER_MB, block_size=3 * ONE_MEGABYTE) yield accepted() # Reject more than 40k sigops per block > 1MB and <= 2MB. block(27, spend=out[22], script=lots_of_checksigs, extra_sigops=2 * MAX_BLOCK_SIGOPS_PER_MB + 1, block_size=2 * ONE_MEGABYTE + 1) yield rejected(RejectResult(16, b'bad-blk-sigops')) # Rewind bad block tip(26) # Reject more than 40k sigops per block > 1MB and <= 2MB. block(28, spend=out[22], script=lots_of_checksigs, extra_sigops=2 * MAX_BLOCK_SIGOPS_PER_MB + 1, block_size=3 * ONE_MEGABYTE) yield rejected(RejectResult(16, b'bad-blk-sigops')) # Rewind bad block tip(26) # Too many sigops in one txn too_many_tx_checksigs = CScript( [OP_CHECKSIG] * (MAX_BLOCK_SIGOPS_PER_MB + 1)) block( 29, spend=out[22], script=too_many_tx_checksigs, block_size=ONE_MEGABYTE + 1) yield rejected(RejectResult(16, b'bad-txn-sigops')) # Rewind bad block tip(26) # Generate a key pair to test P2SH sigops count private_key = CECKey() private_key.set_secretbytes(b"fatstacks") public_key = private_key.get_pubkey() # P2SH # Build the redeem script, hash it, use hash to create the p2sh script redeem_script = CScript( [public_key] + [OP_2DUP, OP_CHECKSIGVERIFY] * 5 + [OP_CHECKSIG]) redeem_script_hash = hash160(redeem_script) p2sh_script = CScript([OP_HASH160, redeem_script_hash, OP_EQUAL]) # Create a p2sh transaction p2sh_tx = self.create_tx(out[22], 1, p2sh_script) # Add the transaction to the block block(30) update_block(30, [p2sh_tx]) yield accepted() # Creates a new transaction using the p2sh transaction included in the # last block def spend_p2sh_tx(output_script=CScript([OP_TRUE])): # Create the transaction spent_p2sh_tx = CTransaction() spent_p2sh_tx.vin.append(CTxIn(COutPoint(p2sh_tx.sha256, 0), b'')) spent_p2sh_tx.vout.append(CTxOut(1, output_script)) # Sign the transaction using the redeem script sighash = SignatureHashForkId( redeem_script, spent_p2sh_tx, 0, SIGHASH_ALL | SIGHASH_FORKID, p2sh_tx.vout[0].nValue) sig = private_key.sign(sighash) + \ bytes(bytearray([SIGHASH_ALL | SIGHASH_FORKID])) spent_p2sh_tx.vin[0].scriptSig = CScript([sig, redeem_script]) spent_p2sh_tx.rehash() return spent_p2sh_tx # Sigops p2sh limit p2sh_sigops_limit = MAX_BLOCK_SIGOPS_PER_MB - \ redeem_script.GetSigOpCount(True) # Too many sigops in one p2sh txn too_many_p2sh_sigops = CScript([OP_CHECKSIG] * (p2sh_sigops_limit + 1)) block(31, spend=out[23], block_size=ONE_MEGABYTE + 1) update_block(31, [spend_p2sh_tx(too_many_p2sh_sigops)]) yield rejected(RejectResult(16, b'bad-txn-sigops')) # Rewind bad block tip(30) # Max sigops in one p2sh txn max_p2sh_sigops = CScript([OP_CHECKSIG] * (p2sh_sigops_limit)) block(32, spend=out[23], block_size=ONE_MEGABYTE + 1) update_block(32, [spend_p2sh_tx(max_p2sh_sigops)]) yield accepted() # Submit a very large block via RPC large_block = block( 33, spend=out[24], block_size=self.excessive_block_size) node.submitblock(ToHex(large_block))
def run_test(self): test = TestManager(self, self.options.tmpdir) # Don't call test.add_all_connections because there is only one node. NetworkThread().start() # Start up network handling in another thread test.run()
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.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.forkid_key = CECKey() self.forkid_key.set_secretbytes(b"forkid") self.forkid_pubkey = self.forkid_key.get_pubkey() self.tip = None self.blocks = {} def setup_network(self): self.extra_args = [[ '-debug', '-norelaypriority', "-uahfstarttime=%d" % UAHF_START_TIME, '-whitelist=127.0.0.1', '-par=1' ]] 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() # Mock the time so that block activating the HF will be accepted self.nodes[0].setmocktime(UAHF_START_TIME) 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 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, 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 node = self.nodes[0] # 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) # Create a transaction that we will use to test SIGHASH_FORID script_forkid = CScript([self.forkid_pubkey, OP_CHECKSIG]) tx_forkid = self.create_and_sign_transaction(out[1].tx, out[1].n, 1, script_forkid) # Create a block that would activate the HF. We also add the # transaction that will allow us to test SIGHASH_FORKID b03 = block(3) b03.nTime = UAHF_START_TIME update_block(3, [tx_forkid]) 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 rejected 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) # build a transaction using SIGHASH_FORKID tx_spend = self.create_tx(tx_forkid, 0, 1, CScript([OP_TRUE])) sighash_spend = SignatureHashForkId(script_forkid, tx_spend, 0, SIGHASH_FORKID | SIGHASH_ALL, 1) sig_forkid = self.forkid_key.sign(sighash_spend) tx_spend.vin[0].scriptSig = CScript( [sig_forkid + bytes(bytearray([SIGHASH_FORKID | SIGHASH_ALL]))]) tx_spend.rehash() # This transaction can't get into the mempool yet try: node.sendrawtransaction(ToHex(tx_spend)) except JSONRPCException as exp: assert_equal(exp.error["message"], RPC_SIGHASH_FORKID_ERROR) else: assert (False) # The transaction is rejected, so the mempool should still be empty assert_equal(set(node.getrawmempool()), set()) # check that SIGHASH_FORKID transaction are still rejected block(9) update_block(9, [tx_spend]) yield rejected(RejectResult(16, SIGHASH_INVALID_ERROR)) # Rewind bad block tip(7) # Pile up another block, to activate. OP_RETURN anti replay # outputs are still considered valid. antireplay_script = CScript([OP_RETURN, ANTI_REPLAY_COMMITMENT]) block(10, spend=out[6], script=antireplay_script) yield accepted() # Now that the HF is activated, replay protected tx are # accepted in the mempool tx_spend_id = node.sendrawtransaction(ToHex(tx_spend)) assert_equal(set(node.getrawmempool()), {tx_spend_id}) # HF is active now, we MUST create a big block. block(11, spend=out[7], block_size=LEGACY_MAX_BLOCK_SIZE) yield rejected(RejectResult(16, b'bad-blk-too-small')) # Rewind bad block tip(10) # HF is active, now we can create bigger blocks and use # SIGHASH_FORKID replay protection. block(12, spend=out[7], block_size=LEGACY_MAX_BLOCK_SIZE + 1) update_block(12, [tx_spend]) yield accepted() # We save this block id to test reorg fork_block_id = node.getbestblockhash() # The transaction has been mined, it's not in the mempool anymore assert_equal(set(node.getrawmempool()), set()) # Test OP_RETURN replay protection block(13, spend=out[8], script=antireplay_script) yield rejected(RejectResult(16, b'bad-txn-replay')) # Rewind bad block tip(12) # Check that only the first block has to be > 1MB block(14, spend=out[8]) yield accepted() # Now we reorg just when the HF activated. The # SIGHASH_FORKID transaction is back in the mempool node.invalidateblock(fork_block_id) assert (tx_spend_id in set(node.getrawmempool())) # And now just before when the HF activated. The # SIGHASH_FORKID should be kicked out the mempool node.invalidateblock(node.getbestblockhash()) assert (tx_spend_id not in set(node.getrawmempool()))
class MagneticActivationTest(ComparisonTestFramework): def set_test_params(self): self.num_nodes = 1 self.setup_clean_chain = True self.extra_args = [['-whitelist=127.0.0.1', "-magneticactivationtime=%d" % MAGNETIC_START_TIME, "-replayprotectionactivationtime=%d" % (2 * MAGNETIC_START_TIME)]] def create_and_tx(self, count): node = self.nodes[0] utxos = node.listunspent() assert(len(utxos) > 0) utxo = utxos[0] tx = CTransaction() value = int(satoshi_round( utxo["amount"] - self.relayfee) * COIN) // count tx.vin = [CTxIn(COutPoint(int(utxo["txid"], 16), utxo["vout"]))] tx.vout = [] for _ in range(count): tx.vout.append(CTxOut(value, CScript([OP_1, OP_1, OP_MUL]))) tx_signed = node.signrawtransaction(ToHex(tx))["hex"] return tx_signed 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.test.run() def get_tests(self): node = self.nodes[0] self.relayfee = self.nodes[0].getnetworkinfo()["relayfee"] # First, we generate some coins to spend. node.generate(125) # Create various outputs using the OP_MUL to check for activation. tx_hex = self.create_and_tx(25) txid = node.sendrawtransaction(tx_hex) assert(txid in set(node.getrawmempool())) node.generate(1) assert(txid not in set(node.getrawmempool())) # register the spendable outputs. tx = FromHex(CTransaction(), tx_hex) tx.rehash() spendable = [PreviousSpendableOutput( tx, i) for i in range(len(tx.vout))] def spending_tx(): outpoint = spendable.pop() out = outpoint.tx.vout[outpoint.n] value = int(out.nValue - (self.relayfee * COIN)) tx = CTransaction() tx.vin = [CTxIn(COutPoint(outpoint.tx.sha256, outpoint.n))] tx.vout = [CTxOut(value, CScript([]))] tx.rehash() return tx # Check that large opreturn are not accepted yet. self.log.info("Try to use the magnetic opcodes before activation") tx0 = spending_tx() tx0_hex = ToHex(tx0) assert_raises_rpc_error(-26, RPC_DISABLED_OPCODE_ERROR, node.sendrawtransaction, tx0_hex) # Push MTP forward just before activation. self.log.info("Pushing MTP just before the activation and check again") node.setmocktime(MAGNETIC_START_TIME) # returns a test case that asserts that the current tip was accepted def accepted(tip): return TestInstance([[tip, True]]) # returns a test case that asserts that the current tip was rejected def rejected(tip, reject=None): if reject is None: return TestInstance([[tip, False]]) else: return TestInstance([[tip, reject]]) # create a new block at the tip of the chain with the given time # but don't add it to the chain yet def next_block(block_time): # get block height blockchaininfo = node.getblockchaininfo() height = int(blockchaininfo['blocks']) # create the block coinbase = create_coinbase(height) coinbase.rehash() block = create_block( int(node.getbestblockhash(), 16), coinbase, block_time) # Do PoW, which is cheap on regnet block.solve() return block for i in range(6): b = next_block(MAGNETIC_START_TIME + i - 1) yield accepted(b) # Check again just before the activation time assert_equal(node.getblockheader(node.getbestblockhash())['mediantime'], MAGNETIC_START_TIME - 1) assert_raises_rpc_error(-26, RPC_DISABLED_OPCODE_ERROR, node.sendrawtransaction, tx0_hex) def add_tx(block, tx): block.vtx.append(tx) block.hashMerkleRoot = block.calc_merkle_root() block.solve() # the last block before activation b = next_block(MAGNETIC_START_TIME + 6) add_tx(b, tx0) yield rejected(b, RejectResult(16, b'blk-bad-inputs')) self.log.info("After this block, the new opcodes are activated") fork_block = next_block(MAGNETIC_START_TIME + 6) yield accepted(fork_block) assert_equal(node.getblockheader(node.getbestblockhash())['mediantime'], MAGNETIC_START_TIME) tx0id = node.sendrawtransaction(tx0_hex) assert(tx0id in set(node.getrawmempool())) # Transactions can also be included in blocks. magneticblock = next_block(MAGNETIC_START_TIME + 7) add_tx(magneticblock, tx0) yield accepted(magneticblock) self.log.info("Cause a reorg that deactivate the magnetic opcodes") # Invalidate the magnetic block, ensure tx0 gets back to the mempool. assert(tx0id not in set(node.getrawmempool())) node.invalidateblock(format(magneticblock.sha256, 'x')) assert(tx0id in set(node.getrawmempool())) node.invalidateblock(format(fork_block.sha256, 'x')) assert(tx0id not in set(node.getrawmempool()))
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.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 = {} self.excessive_block_size = 16 * ONE_MEGABYTE self.extra_args = [['-norelaypriority', '-whitelist=127.0.0.1', '-limitancestorcount=9999', '-limitancestorsize=9999', '-limitdescendantcount=9999', '-limitdescendantsize=9999', '-maxmempool=999', "-excessiveblocksize=%d" % self.excessive_block_size]] 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 = SignatureHashForkId( spend_tx.vout[n].scriptPubKey, tx, 0, SIGHASH_ALL | SIGHASH_FORKID, spend_tx.vout[n].nValue) tx.vin[0].scriptSig = CScript( [self.coinbase_key.sign(sighash) + bytes(bytearray([SIGHASH_ALL | SIGHASH_FORKID]))]) 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 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, 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() # no signature yet tx.vin.append( CTxIn(COutPoint(spend.tx.sha256, spend.n), b"", 0xffffffff)) # We put some random data into the first transaction of the chain # to randomize ids tx.vout.append( CTxOut(0, CScript([random.randint(0, 255), OP_DROP, OP_TRUE]))) 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 = SignatureHashForkId( spend.tx.vout[spend.n].scriptPubKey, tx, 0, SIGHASH_ALL | SIGHASH_FORKID, spend.tx.vout[spend.n].nValue) scriptSig = CScript( [self.coinbase_key.sign(sighash) + bytes(bytearray([SIGHASH_ALL | SIGHASH_FORKID]))]) 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) 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()) # Let's build some blocks and test them. for i in range(16): n = i + 1 block(n, spend=out[i], block_size=n * ONE_MEGABYTE) yield accepted() # block of maximal size block(17, spend=out[16], block_size=self.excessive_block_size) yield accepted() # Reject oversized blocks with bad-blk-length error block(18, spend=out[17], block_size=self.excessive_block_size + 1) yield rejected(RejectResult(16, b'bad-blk-length')) # Rewind bad block. tip(17) # Accept many sigops lots_of_checksigs = CScript( [OP_CHECKSIG] * (MAX_BLOCK_SIGOPS_PER_MB - 1)) block( 19, spend=out[17], script=lots_of_checksigs, block_size=ONE_MEGABYTE) yield accepted() too_many_blk_checksigs = CScript( [OP_CHECKSIG] * MAX_BLOCK_SIGOPS_PER_MB) block( 20, spend=out[18], script=too_many_blk_checksigs, block_size=ONE_MEGABYTE) yield rejected(RejectResult(16, b'bad-blk-sigops')) # Rewind bad block tip(19) # Accept 40k sigops per block > 1MB and <= 2MB block(21, spend=out[18], script=lots_of_checksigs, extra_sigops=MAX_BLOCK_SIGOPS_PER_MB, block_size=ONE_MEGABYTE + 1) yield accepted() # Accept 40k sigops per block > 1MB and <= 2MB block(22, spend=out[19], script=lots_of_checksigs, extra_sigops=MAX_BLOCK_SIGOPS_PER_MB, block_size=2 * ONE_MEGABYTE) yield accepted() # Reject more than 40k sigops per block > 1MB and <= 2MB. block(23, spend=out[20], script=lots_of_checksigs, extra_sigops=MAX_BLOCK_SIGOPS_PER_MB + 1, block_size=ONE_MEGABYTE + 1) yield rejected(RejectResult(16, b'bad-blk-sigops')) # Rewind bad block tip(22) # Reject more than 40k sigops per block > 1MB and <= 2MB. block(24, spend=out[20], script=lots_of_checksigs, extra_sigops=MAX_BLOCK_SIGOPS_PER_MB + 1, block_size=2 * ONE_MEGABYTE) yield rejected(RejectResult(16, b'bad-blk-sigops')) # Rewind bad block tip(22) # Accept 60k sigops per block > 2MB and <= 3MB block(25, spend=out[20], script=lots_of_checksigs, extra_sigops=2 * MAX_BLOCK_SIGOPS_PER_MB, block_size=2 * ONE_MEGABYTE + 1) yield accepted() # Accept 60k sigops per block > 2MB and <= 3MB block(26, spend=out[21], script=lots_of_checksigs, extra_sigops=2 * MAX_BLOCK_SIGOPS_PER_MB, block_size=3 * ONE_MEGABYTE) yield accepted() # Reject more than 40k sigops per block > 1MB and <= 2MB. block(27, spend=out[22], script=lots_of_checksigs, extra_sigops=2 * MAX_BLOCK_SIGOPS_PER_MB + 1, block_size=2 * ONE_MEGABYTE + 1) yield rejected(RejectResult(16, b'bad-blk-sigops')) # Rewind bad block tip(26) # Reject more than 40k sigops per block > 1MB and <= 2MB. block(28, spend=out[22], script=lots_of_checksigs, extra_sigops=2 * MAX_BLOCK_SIGOPS_PER_MB + 1, block_size=3 * ONE_MEGABYTE) yield rejected(RejectResult(16, b'bad-blk-sigops')) # Rewind bad block tip(26) # Too many sigops in one txn too_many_tx_checksigs = CScript( [OP_CHECKSIG] * (MAX_BLOCK_SIGOPS_PER_MB + 1)) block( 29, spend=out[22], script=too_many_tx_checksigs, block_size=ONE_MEGABYTE + 1) yield rejected(RejectResult(16, b'bad-txn-sigops')) # Rewind bad block tip(26) # P2SH # Build the redeem script, hash it, use hash to create the p2sh script redeem_script = CScript([self.coinbase_pubkey] + [ OP_2DUP, OP_CHECKSIGVERIFY] * 5 + [OP_CHECKSIG]) redeem_script_hash = hash160(redeem_script) p2sh_script = CScript([OP_HASH160, redeem_script_hash, OP_EQUAL]) # Create a p2sh transaction p2sh_tx = self.create_and_sign_transaction( out[22].tx, out[22].n, 1, p2sh_script) # Add the transaction to the block block(30) update_block(30, [p2sh_tx]) yield accepted() # Creates a new transaction using the p2sh transaction included in the # last block def spend_p2sh_tx(output_script=CScript([OP_TRUE])): # Create the transaction spent_p2sh_tx = CTransaction() spent_p2sh_tx.vin.append(CTxIn(COutPoint(p2sh_tx.sha256, 0), b'')) spent_p2sh_tx.vout.append(CTxOut(1, output_script)) # Sign the transaction using the redeem script sighash = SignatureHashForkId( redeem_script, spent_p2sh_tx, 0, SIGHASH_ALL | SIGHASH_FORKID, p2sh_tx.vout[0].nValue) sig = self.coinbase_key.sign(sighash) + bytes( bytearray([SIGHASH_ALL | SIGHASH_FORKID])) spent_p2sh_tx.vin[0].scriptSig = CScript([sig, redeem_script]) spent_p2sh_tx.rehash() return spent_p2sh_tx # Sigops p2sh limit p2sh_sigops_limit = MAX_BLOCK_SIGOPS_PER_MB - \ redeem_script.GetSigOpCount(True) # Too many sigops in one p2sh txn too_many_p2sh_sigops = CScript([OP_CHECKSIG] * (p2sh_sigops_limit + 1)) block(31, spend=out[23], block_size=ONE_MEGABYTE + 1) update_block(31, [spend_p2sh_tx(too_many_p2sh_sigops)]) yield rejected(RejectResult(16, b'bad-txn-sigops')) # Rewind bad block tip(30) # Max sigops in one p2sh txn max_p2sh_sigops = CScript([OP_CHECKSIG] * (p2sh_sigops_limit)) block(32, spend=out[23], block_size=ONE_MEGABYTE + 1) update_block(32, [spend_p2sh_tx(max_p2sh_sigops)]) yield accepted() # Check that compact block also work for big blocks node = self.nodes[0] peer = TestNode() peer.add_connection(NodeConn('127.0.0.1', p2p_port(0), node, peer)) # Start up network handling in another thread and wait for connection # to be etablished NetworkThread().start() peer.wait_for_verack() # Wait for SENDCMPCT def received_sendcmpct(): return (peer.last_sendcmpct != None) got_sendcmpt = wait_until(received_sendcmpct, timeout=30) assert(got_sendcmpt) sendcmpct = msg_sendcmpct() sendcmpct.version = 1 sendcmpct.announce = True peer.send_and_ping(sendcmpct) # Exchange headers def received_getheaders(): return (peer.last_getheaders != None) got_getheaders = wait_until(received_getheaders, timeout=30) assert(got_getheaders) # Return the favor peer.send_message(peer.last_getheaders) # Wait for the header list def received_headers(): return (peer.last_headers != None) got_headers = wait_until(received_headers, timeout=30) assert(got_headers) # It's like we know about the same headers ! peer.send_message(peer.last_headers) # Send a block b33 = block(33, spend=out[24], block_size=ONE_MEGABYTE + 1) yield accepted() # Checks the node to forward it via compact block def received_block(): return (peer.last_cmpctblock != None) got_cmpctblock = wait_until(received_block, timeout=30) assert(got_cmpctblock) # Was it our block ? cmpctblk_header = peer.last_cmpctblock.header_and_shortids.header cmpctblk_header.calc_sha256() assert(cmpctblk_header.sha256 == b33.sha256) # Send a bigger block peer.clear_block_data() b34 = block(34, spend=out[25], block_size=8 * ONE_MEGABYTE) yield accepted() # Checks the node to forward it via compact block got_cmpctblock = wait_until(received_block, timeout=30) assert(got_cmpctblock) # Was it our block ? cmpctblk_header = peer.last_cmpctblock.header_and_shortids.header cmpctblk_header.calc_sha256() assert(cmpctblk_header.sha256 == b34.sha256) # Let's send a compact block and see if the node accepts it. # First, we generate the block and send all transaction to the mempool b35 = block(35, spend=out[26], block_size=8 * ONE_MEGABYTE) for i in range(1, len(b35.vtx)): node.sendrawtransaction(ToHex(b35.vtx[i]), True) # Now we create the compact block and send it comp_block = HeaderAndShortIDs() comp_block.initialize_from_block(b35) peer.send_and_ping(msg_cmpctblock(comp_block.to_p2p())) # Check that compact block is received properly assert(int(node.getbestblockhash(), 16) == b35.sha256)
class MonolithActivationTest(ComparisonTestFramework): def set_test_params(self): self.num_nodes = 1 self.setup_clean_chain = True self.extra_args = [['-whitelist=127.0.0.1', "-monolithactivationtime=%d" % MONOLITH_START_TIME, "-replayprotectionactivationtime=%d" % (2 * MONOLITH_START_TIME)]] def create_and_tx(self, count): node = self.nodes[0] utxos = node.listunspent() assert(len(utxos) > 0) utxo = utxos[0] tx = CTransaction() value = int(satoshi_round( utxo["amount"] - self.relayfee) * COIN) // count tx.vin = [CTxIn(COutPoint(int(utxo["txid"], 16), utxo["vout"]))] tx.vout = [] for _ in range(count): tx.vout.append(CTxOut(value, CScript([OP_1, OP_1, OP_AND]))) tx_signed = node.signrawtransaction(ToHex(tx))["hex"] return tx_signed 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.test.run() def get_tests(self): node = self.nodes[0] self.relayfee = self.nodes[0].getnetworkinfo()["relayfee"] # First, we generate some coins to spend. node.generate(125) # Create various outputs using the OP_AND to check for activation. tx_hex = self.create_and_tx(25) txid = node.sendrawtransaction(tx_hex) assert(txid in set(node.getrawmempool())) node.generate(1) assert(txid not in set(node.getrawmempool())) # register the spendable outputs. tx = FromHex(CTransaction(), tx_hex) tx.rehash() spendable_ands = [PreviousSpendableOutput( tx, i) for i in range(len(tx.vout))] def spend_and(): outpoint = spendable_ands.pop() out = outpoint.tx.vout[outpoint.n] value = int(out.nValue - (self.relayfee * COIN)) tx = CTransaction() tx.vin = [CTxIn(COutPoint(outpoint.tx.sha256, outpoint.n))] tx.vout = [CTxOut(value, CScript([]))] tx.rehash() return tx # Check that large opreturn are not accepted yet. self.log.info("Try to use the monolith opcodes before activation") tx0 = spend_and() tx0_hex = ToHex(tx0) assert_raises_rpc_error(-26, RPC_DISABLED_OPCODE_ERROR, node.sendrawtransaction, tx0_hex) # Push MTP forward just before activation. self.log.info("Pushing MTP just before the activation and check again") node.setmocktime(MONOLITH_START_TIME) # returns a test case that asserts that the current tip was accepted def accepted(tip): return TestInstance([[tip, True]]) # returns a test case that asserts that the current tip was rejected def rejected(tip, reject=None): if reject is None: return TestInstance([[tip, False]]) else: return TestInstance([[tip, reject]]) def next_block(block_time): # get block height blockchaininfo = node.getblockchaininfo() height = int(blockchaininfo['blocks']) # create the block coinbase = create_coinbase(height) coinbase.rehash() block = create_block( int(node.getbestblockhash(), 16), coinbase, block_time) # Do PoW, which is cheap on regnet block.solve() return block for i in range(6): b = next_block(MONOLITH_START_TIME + i - 1) yield accepted(b) # Check again just before the activation time assert_equal(node.getblockheader(node.getbestblockhash())['mediantime'], MONOLITH_START_TIME - 1) assert_raises_rpc_error(-26, RPC_DISABLED_OPCODE_ERROR, node.sendrawtransaction, tx0_hex) def add_tx(block, tx): block.vtx.append(tx) block.hashMerkleRoot = block.calc_merkle_root() block.solve() b = next_block(MONOLITH_START_TIME + 6) add_tx(b, tx0) yield rejected(b, RejectResult(16, b'blk-bad-inputs')) self.log.info("Activates the new opcodes") fork_block = next_block(MONOLITH_START_TIME + 6) yield accepted(fork_block) assert_equal(node.getblockheader(node.getbestblockhash())['mediantime'], MONOLITH_START_TIME) tx0id = node.sendrawtransaction(tx0_hex) assert(tx0id in set(node.getrawmempool())) # Transactions can also be included in blocks. monolithblock = next_block(MONOLITH_START_TIME + 7) add_tx(monolithblock, tx0) yield accepted(monolithblock) self.log.info("Cause a reorg that deactivate the monolith opcodes") # Invalidate the monolith block, ensure tx0 gets back to the mempool. assert(tx0id not in set(node.getrawmempool())) node.invalidateblock(format(monolithblock.sha256, 'x')) assert(tx0id in set(node.getrawmempool())) node.invalidateblock(format(fork_block.sha256, 'x')) assert(tx0id not in set(node.getrawmempool()))
class BSV128MBlocks(ComparisonTestFramework): def set_test_params(self): self.num_nodes = 1 self.setup_clean_chain = True self.block_heights = {} self.tip = None self.blocks = {} self.excessive_block_size = 128 * ONE_MEGABYTE self.extra_args = [[ '-whitelist=127.0.0.1', "-excessiveblocksize=%d" % self.excessive_block_size ]] def add_options(self, parser): super().add_options(parser) 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) def next_block(self, number, spend=None, script=CScript([OP_TRUE]), block_size=0, extra_sigops=0): 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() if spend == None: # We need to have something to spend to fill the block. assert_equal(block_size, 0) block = create_block(base_block_hash, coinbase, block_time) else: # all but one satoshi to fees coinbase.vout[0].nValue += spend.tx.vout[spend.n].nValue - 1 coinbase.rehash() block = create_block(base_block_hash, coinbase, block_time) # Make sure we have plenty engough to spend going forward. spendable_outputs = deque([spend]) def get_base_transaction(): # Create the new transaction tx = CTransaction() # Spend from one of the spendable outputs spend = spendable_outputs.popleft() tx.vin.append(CTxIn(COutPoint(spend.tx.sha256, spend.n))) # Add spendable outputs for i in range(4): tx.vout.append(CTxOut(0, CScript([OP_TRUE]))) spendable_outputs.append(PreviousSpendableOutput(tx, i)) return tx tx = get_base_transaction() # Make it the same format as transaction added for padding and save the size. # It's missing the padding output, so we add a constant to account for it. tx.rehash() base_tx_size = len(tx.serialize()) + 18 # If a specific script is required, add it. if script != None: tx.vout.append(CTxOut(1, script)) # Put some random data into the first transaction of the chain to randomize ids. tx.vout.append( CTxOut(0, CScript([random.randint(0, 256), OP_RETURN]))) # Add the transaction to the block self.add_transactions_to_block(block, [tx]) # If we have a block size requirement, just fill # the block until we get there current_block_size = len(block.serialize()) while current_block_size < block_size: # We will add a new transaction. That means the size of # the field enumerating how many transaction go in the block # may change. current_block_size -= len(ser_compact_size(len(block.vtx))) current_block_size += len(ser_compact_size(len(block.vtx) + 1)) # Create the new transaction tx = get_base_transaction() # Add padding to fill the block. script_length = block_size - current_block_size - base_tx_size if script_length > 510000: if script_length < 1000000: # Make sure we don't find ourselves in a position where we # need to generate a transaction smaller than what we expected. script_length = script_length // 2 else: 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, script_output)) # Add the tx to the list of transactions to be included # in the block. self.add_transactions_to_block(block, [tx]) current_block_size += len(tx.serialize()) # Now that we added a bunch of transaction, we need to recompute # the merkle root. block.hashMerkleRoot = block.calc_merkle_root() # Check that the block size is what's expected if block_size > 0: assert_equal(len(block.serialize()), block_size) # 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): node = self.nodes[0] self.genesis_hash = int(node.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) 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 of maximal size block(1, spend=out[0], block_size=self.excessive_block_size) yield accepted() # Reject oversized blocks with bad-blk-length error block(2, spend=out[1], block_size=self.excessive_block_size + 1) yield rejected(RejectResult(16, b'bad-blk-length'))
def run_test(self): test = TestManager(self, self.options.tmpdir) test.add_all_connections(self.nodes) # Start up network handling in another thread NetworkThread().start() test.run()
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 set_test_params(self): self.num_nodes = 1 self.setup_clean_chain = True self.block_heights = {} self.coinbase_key = CECKey() self.coinbase_key.set_secretbytes(b"horsebattery") self.coinbase_pubkey = self.coinbase_key.get_pubkey() self.tip = None self.blocks = {} def setup_network(self): self.extra_args = [['-norelaypriority']] self.add_nodes(self.num_nodes, self.extra_args) self.start_nodes() 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() 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 = SignatureHashForkId( spend_tx.vout[n].scriptPubKey, tx, 0, SIGHASH_ALL | SIGHASH_FORKID, spend_tx.vout[n].nValue) tx.vin[0].scriptSig = CScript( [self.coinbase_key.sign(sighash) + bytes(bytearray([SIGHASH_ALL | SIGHASH_FORKID]))]) 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=CScript([OP_TRUE])): 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, self.coinbase_pubkey) coinbase.vout[0].nValue += additional_coinbase_value coinbase.rehash() if spend == None: block = create_block(base_block_hash, coinbase, block_time) else: # all but one satoshi to fees coinbase.vout[0].nValue += spend.tx.vout[ spend.n].nValue - 1 coinbase.rehash() block = create_block(base_block_hash, coinbase, block_time) # spend 1 satoshi tx = create_transaction(spend.tx, spend.n, b"", 1, script) self.sign_tx(tx, spend.tx, spend.n) self.add_transactions_to_block(block, [tx]) block.hashMerkleRoot = block.calc_merkle_root() # Do PoW, which is very inexpensive 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): 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_tx = self.create_tx # shorthand for variables 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(33): out.append(get_spendable_output()) # P2SH # Build the redeem script, hash it, use hash to create the p2sh script redeem_script = CScript([self.coinbase_pubkey] + [ OP_2DUP, OP_CHECKSIGVERIFY] * 5 + [OP_CHECKSIG]) redeem_script_hash = hash160(redeem_script) p2sh_script = CScript([OP_HASH160, redeem_script_hash, OP_EQUAL]) # Creates a new transaction using a p2sh transaction as input def spend_p2sh_tx(p2sh_tx_to_spend, output_script=CScript([OP_TRUE])): # Create the transaction spent_p2sh_tx = CTransaction() spent_p2sh_tx.vin.append( CTxIn(COutPoint(p2sh_tx_to_spend.sha256, 0), b'')) spent_p2sh_tx.vout.append(CTxOut(1, output_script)) # Sign the transaction using the redeem script sighash = SignatureHashForkId( redeem_script, spent_p2sh_tx, 0, SIGHASH_ALL | SIGHASH_FORKID, p2sh_tx_to_spend.vout[0].nValue) sig = self.coinbase_key.sign( sighash) + bytes(bytearray([SIGHASH_ALL | SIGHASH_FORKID])) spent_p2sh_tx.vin[0].scriptSig = CScript([sig, redeem_script]) spent_p2sh_tx.rehash() return spent_p2sh_tx # P2SH tests # Create a p2sh transaction p2sh_tx = self.create_and_sign_transaction( out[0].tx, out[0].n, 1, p2sh_script) # Add the transaction to the block block(1) update_block(1, [p2sh_tx]) yield accepted() # Sigops p2sh limit for the mempool test p2sh_sigops_limit_mempool = MAX_STANDARD_TX_SIGOPS - \ redeem_script.GetSigOpCount(True) # Too many sigops in one p2sh script too_many_p2sh_sigops_mempool = CScript( [OP_CHECKSIG] * (p2sh_sigops_limit_mempool + 1)) # A transaction with this output script can't get into the mempool assert_raises_rpc_error(-26, RPC_TXNS_TOO_MANY_SIGOPS_ERROR, node.sendrawtransaction, ToHex(spend_p2sh_tx(p2sh_tx, too_many_p2sh_sigops_mempool))) # The transaction is rejected, so the mempool should still be empty assert_equal(set(node.getrawmempool()), set()) # Max sigops in one p2sh txn max_p2sh_sigops_mempool = CScript( [OP_CHECKSIG] * (p2sh_sigops_limit_mempool)) # A transaction with this output script can get into the mempool max_p2sh_sigops_txn = spend_p2sh_tx(p2sh_tx, max_p2sh_sigops_mempool) max_p2sh_sigops_txn_id = node.sendrawtransaction( ToHex(max_p2sh_sigops_txn)) assert_equal(set(node.getrawmempool()), {max_p2sh_sigops_txn_id}) # Mine the transaction block(2, spend=out[1]) update_block(2, [max_p2sh_sigops_txn]) yield accepted() # The transaction has been mined, it's not in the mempool anymore assert_equal(set(node.getrawmempool()), set())
class CheckDataSigActivationTest(ComparisonTestFramework): def set_test_params(self): self.num_nodes = 1 self.setup_clean_chain = True self.extra_args = [[ '-whitelist=127.0.0.1', "-magneticanomalyactivationtime=%d" % MAGNETIC_ANOMALY_START_TIME, "-replayprotectionactivationtime=%d" % (2 * MAGNETIC_ANOMALY_START_TIME) ]] def create_checkdatasig_tx(self, count): node = self.nodes[0] utxos = node.listunspent() assert (len(utxos) > 0) utxo = utxos[0] tx = CTransaction() value = int(satoshi_round(utxo["amount"]) * COIN) // count tx.vin = [CTxIn(COutPoint(int(utxo["txid"], 16), utxo["vout"]))] tx.vout = [] signature = bytearray.fromhex( '30440220256c12175e809381f97637933ed6ab97737d263eaaebca6add21bced67fd12a402205ce29ecc1369d6fc1b51977ed38faaf41119e3be1d7edfafd7cfaf0b6061bd07' ) message = bytearray.fromhex('') pubkey = bytearray.fromhex( '038282263212c609d9ea2a6e3e172de238d8c39cabd5ac1ca10646e23fd5f51508' ) for _ in range(count): tx.vout.append( CTxOut(value, CScript([signature, message, pubkey, OP_CHECKDATASIG]))) tx.vout[0].nValue -= node.calculate_fee(tx) tx_signed = node.signrawtransaction(ToHex(tx))["hex"] return tx_signed 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.test.run() def get_tests(self): node = self.nodes[0] # First, we generate some coins to spend. node.generate(125) # Create various outputs using the OP_CHECKDATASIG # to check for activation. tx_hex = self.create_checkdatasig_tx(25) txid = node.sendrawtransaction(tx_hex) assert (txid in set(node.getrawmempool())) node.generate(1) assert (txid not in set(node.getrawmempool())) # register the spendable outputs. tx = FromHex(CTransaction(), tx_hex) tx.rehash() spendable_checkdatasigs = [ PreviousSpendableOutput(tx, i) for i in range(len(tx.vout)) ] def spend_checkdatasig(): outpoint = spendable_checkdatasigs.pop() out = outpoint.tx.vout[outpoint.n] tx = CTransaction() tx.vin = [CTxIn(COutPoint(outpoint.tx.sha256, outpoint.n))] tx.vout = [ CTxOut(out.nValue, CScript([])), CTxOut(0, CScript([random.getrandbits(800), OP_RETURN])) ] tx.vout[0].nValue -= node.calculate_fee(tx) tx.rehash() return tx # Check that transactions using checkdatasig are not accepted yet. self.log.info("Try to use the checkdatasig opcodes before activation") tx0 = spend_checkdatasig() tx0_hex = ToHex(tx0) assert_raises_rpc_error(-26, RPC_BAD_OPCODE_ERROR, node.sendrawtransaction, tx0_hex) # Push MTP forward just before activation. self.log.info("Pushing MTP just before the activation and check again") node.setmocktime(MAGNETIC_ANOMALY_START_TIME) # returns a test case that asserts that the current tip was accepted def accepted(tip): return TestInstance([[tip, True]]) # returns a test case that asserts that the current tip was rejected def rejected(tip, reject=None): if reject is None: return TestInstance([[tip, False]]) else: return TestInstance([[tip, reject]]) def next_block(block_time): # get block height blockchaininfo = node.getblockchaininfo() height = int(blockchaininfo['blocks']) # create the block coinbase = create_coinbase(height) coinbase.rehash() block = create_block(int(node.getbestblockhash(), 16), coinbase, block_time) # Do PoW, which is cheap on regnet block.solve() return block for i in range(6): b = next_block(MAGNETIC_ANOMALY_START_TIME + i - 1) yield accepted(b) # Check again just before the activation time assert_equal( node.getblockheader(node.getbestblockhash())['mediantime'], MAGNETIC_ANOMALY_START_TIME - 1) assert_raises_rpc_error(-26, RPC_BAD_OPCODE_ERROR, node.sendrawtransaction, tx0_hex) def add_tx(block, tx): block.vtx.append(tx) block.hashMerkleRoot = block.calc_merkle_root() block.solve() b = next_block(MAGNETIC_ANOMALY_START_TIME + 6) add_tx(b, tx0) yield rejected(b, RejectResult(16, b'blk-bad-inputs')) self.log.info("Activates checkdatasig") fork_block = next_block(MAGNETIC_ANOMALY_START_TIME + 6) yield accepted(fork_block) assert_equal( node.getblockheader(node.getbestblockhash())['mediantime'], MAGNETIC_ANOMALY_START_TIME) tx0id = node.sendrawtransaction(tx0_hex) assert (tx0id in set(node.getrawmempool())) # Transactions can also be included in blocks. magneticanomalyblock = next_block(MAGNETIC_ANOMALY_START_TIME + 7) add_tx(magneticanomalyblock, tx0) yield accepted(magneticanomalyblock) self.log.info("Cause a reorg that deactivate the checkdatasig opcodes") # Invalidate the checkdatasig block, ensure tx0 gets back to the mempool. assert (tx0id not in set(node.getrawmempool())) node.invalidateblock(format(magneticanomalyblock.sha256, 'x')) assert (tx0id in set(node.getrawmempool())) node.invalidateblock(format(fork_block.sha256, 'x')) assert (tx0id not in set(node.getrawmempool()))
class May152018ActivationTest(ComparisonTestFramework): def __init__(self): self.num_nodes = 1 def set_test_params(self): self.setup_clean_chain = True self.extra_args = [['-whitelist=127.0.0.1']] def create_and_tx(self, count): node = self.nodes[0] utxos = node.listunspent() assert (len(utxos) > 0) utxo = utxos[0] tx = CTransaction() value = int( satoshi_round(utxo["amount"] - self.relayfee) * COIN) // count tx.vin = [CTxIn(COutPoint(int(utxo["txid"], 16), utxo["vout"]))] tx.vout = [] for _ in range(count): tx.vout.append(CTxOut(value, CScript([OP_1, OP_1, OP_AND]))) tx_signed = node.signrawtransaction(ToHex(tx), None, None, "ALL|FORKID")["hex"] return tx_signed 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.test.run() def get_tests(self): node = self.nodes[0] self.relayfee = self.nodes[0].getnetworkinfo()["relayfee"] # First, we generate some coins to spend. node.setmocktime(MAY152018_START_TIME - 1000) node.generate(125) # Create various outputs using the OP_AND to check for activation. tx_hex = self.create_and_tx(25) txid = node.sendrawtransaction(tx_hex) assert (txid in set(node.getrawmempool())) node.generate(1) assert (txid not in set(node.getrawmempool())) # register the spendable outputs. tx = FromHex(CTransaction(), tx_hex) tx.rehash() spendable_ands = [ PreviousSpendableOutput(tx, i) for i in range(len(tx.vout)) ] def spend_and(): outpoint = spendable_ands.pop() out = outpoint.tx.vout[outpoint.n] value = int(out.nValue - (self.relayfee * COIN)) tx = CTransaction() tx.vin = [CTxIn(COutPoint(outpoint.tx.sha256, outpoint.n))] tx.vout = [CTxOut(value, CScript([]))] tx.rehash() return tx # Check that large opreturn are not accepted yet. logging.info("Try to use the may152018 opcodes before activation") tx0 = spend_and() tx0_hex = ToHex(tx0) assert_raises_rpc_error(-26, RPC_DISABLED_OPCODE_ERROR, node.sendrawtransaction, tx0_hex) # Push MTP forward just before activation. logging.info("Pushing MTP just before the activation and check again") node.setmocktime(MAY152018_START_TIME) # returns a test case that asserts that the current tip was accepted def accepted(tip): return TestInstance([[tip, True]]) # returns a test case that asserts that the current tip was rejected def rejected(tip, reject=None): if reject is None: return TestInstance([[tip, False]]) else: return TestInstance([[tip, reject]]) def next_block(block_time): # get block height blockchaininfo = node.getblockchaininfo() height = int(blockchaininfo['blocks']) # create the block coinbase = create_coinbase(height) coinbase.rehash() block = create_block(int(node.getbestblockhash(), 16), coinbase, block_time) # Do PoW, which is cheap on regnet block.solve() return block for i in range(6): b = next_block(MAY152018_START_TIME + i - 1) yield accepted(b) # Check again just before the activation time assert_equal( node.getblockheader(node.getbestblockhash())['mediantime'], MAY152018_START_TIME - 1) assert_raises_rpc_error(-26, RPC_DISABLED_OPCODE_ERROR, node.sendrawtransaction, tx0_hex) def add_tx(block, tx): block.vtx.append(tx) block.hashMerkleRoot = block.calc_merkle_root() block.solve() b = next_block(MAY152018_START_TIME + 6) add_tx(b, tx0) # In this next step we don't check the reason code for the expected failure because of timing we # can not be sure whether checking the block will fail on signature validation or validation during # checkinputs. We can only be certain that we must have a failure. yield rejected(b) logging.info("Activates the new opcodes") fork_block = next_block(MAY152018_START_TIME + 6) yield accepted(fork_block) assert_equal( node.getblockheader(node.getbestblockhash())['mediantime'], MAY152018_START_TIME) tx_hex = self.create_and_tx(25) tx0id = node.sendrawtransaction(tx_hex) assert (tx0id in set(node.getrawmempool())) # Transactions can also be included in blocks. may152018block = next_block(MAY152018_START_TIME + 7) tx0 = FromHex(CTransaction(), tx_hex) tx0.rehash() add_tx(may152018block, tx0) yield accepted(may152018block) logging.info("Cause a reorg that deactivate the may152018 opcodes") # Invalidate the may152018 block, ensure tx0 gets back to the mempool. assert (tx0id not in set(node.getrawmempool())) node.invalidateblock(format(may152018block.sha256, 'x')) assert (tx0id in set(node.getrawmempool())) node.invalidateblock(format(fork_block.sha256, 'x')) assert (tx0id not in set(node.getrawmempool()))
def run_test(self): return #TODO p2p stuff throwing format errors test = TestManager(self, self.options.tmpdir) test.add_all_connections(self.nodes) NetworkThread().start() # Start up network handling in another thread test.run()
class BIP9SoftForksTest(ComparisonTestFramework): def __init__(self): self.num_nodes = 1 def setup_network(self): self.nodes = start_nodes(1, self.options.tmpdir, extra_args=[['-debug', '-whitelist=127.0.0.1']], binary=[self.options.testbinary]) def run_test(self): self.test = TestManager(self, self.options.tmpdir) self.test.add_all_connections(self.nodes) NetworkThread().start() # Start up network handling in another thread self.test.run() def create_transaction(self, node, coinbase, to_address, amount): from_txid = node.getblock(coinbase)['tx'][0] inputs = [{ "txid" : from_txid, "vout" : 0}] outputs = { to_address : amount } rawtx = node.createrawtransaction(inputs, outputs) tx = CTransaction() f = cStringIO.StringIO(unhexlify(rawtx)) tx.deserialize(f) tx.nVersion = 2 return tx def sign_transaction(self, node, tx): signresult = node.signrawtransaction(hexlify(tx.serialize())) tx = CTransaction() f = cStringIO.StringIO(unhexlify(signresult['hex'])) tx.deserialize(f) return tx def generate_blocks(self, number, version, test_blocks = []): for i in xrange(number): block = create_block(self.tip, create_coinbase(absoluteHeight=self.height), self.last_block_time + 1) block.nVersion = version block.rehash() block.solve() test_blocks.append([block, True]) self.last_block_time += 1 self.tip = block.sha256 self.height += 1 return test_blocks def get_bip9_status(self, key): info = self.nodes[0].getblockchaininfo() for row in info['bip9_softforks']: if row['id'] == key: return row raise IndexError ('key:"%s" not found' % key) def test_BIP(self, bipName, activated_version, invalidate, invalidatePostSignature): # generate some coins for later self.coinbase_blocks = self.nodes[0].generate(2) self.height = 3 # height of the next block to build self.tip = int ("0x" + self.nodes[0].getbestblockhash() + "L", 0) self.nodeaddress = self.nodes[0].getnewaddress() self.last_block_time = int(time.time()) assert_equal(self.get_bip9_status(bipName)['status'], 'defined') # Test 1 # Advance from DEFINED to STARTED test_blocks = self.generate_blocks(141, 4) yield TestInstance(test_blocks, sync_every_block=False) assert_equal(self.get_bip9_status(bipName)['status'], 'started') # Test 2 # Fail to achieve LOCKED_IN 100 out of 144 signal bit 1 # using a variety of bits to simulate multiple parallel softforks test_blocks = self.generate_blocks(50, activated_version) # 0x20000001 (signalling ready) test_blocks = self.generate_blocks(20, 4, test_blocks) # 0x00000004 (signalling not) test_blocks = self.generate_blocks(50, activated_version, test_blocks) # 0x20000101 (signalling ready) test_blocks = self.generate_blocks(24, 4, test_blocks) # 0x20010000 (signalling not) yield TestInstance(test_blocks, sync_every_block=False) assert_equal(self.get_bip9_status(bipName)['status'], 'started') # Test 3 # 108 out of 144 signal bit 1 to achieve LOCKED_IN # using a variety of bits to simulate multiple parallel softforks test_blocks = self.generate_blocks(58, activated_version) # 0x20000001 (signalling ready) test_blocks = self.generate_blocks(26, 4, test_blocks) # 0x00000004 (signalling not) test_blocks = self.generate_blocks(50, activated_version, test_blocks) # 0x20000101 (signalling ready) test_blocks = self.generate_blocks(10, 4, test_blocks) # 0x20010000 (signalling not) yield TestInstance(test_blocks, sync_every_block=False) assert_equal(self.get_bip9_status(bipName)['status'], 'locked_in') # Test 4 # 143 more version 536870913 blocks (waiting period-1) test_blocks = self.generate_blocks(143, 4) yield TestInstance(test_blocks, sync_every_block=False) assert_equal(self.get_bip9_status(bipName)['status'], 'locked_in') # Test 5 # Check that the new rule is enforced spendtx = self.create_transaction(self.nodes[0], self.coinbase_blocks[0], self.nodeaddress, 1.0) invalidate(spendtx) spendtx = self.sign_transaction(self.nodes[0], spendtx) spendtx.rehash() invalidatePostSignature(spendtx) spendtx.rehash() block = create_block(self.tip, create_coinbase(absoluteHeight=self.height), self.last_block_time + 1) block.nVersion = activated_version block.vtx.append(spendtx) block.hashMerkleRoot = block.calc_merkle_root() block.rehash() block.solve() self.last_block_time += 1 self.tip = block.sha256 self.height += 1 yield TestInstance([[block, True]]) assert_equal(self.get_bip9_status(bipName)['status'], 'active') # Test 6 # Check that the new sequence lock rules are enforced spendtx = self.create_transaction(self.nodes[0], self.coinbase_blocks[1], self.nodeaddress, 1.0) invalidate(spendtx) spendtx = self.sign_transaction(self.nodes[0], spendtx) spendtx.rehash() invalidatePostSignature(spendtx) spendtx.rehash() block = create_block(self.tip, create_coinbase(absoluteHeight=self.height), self.last_block_time + 1) block.nVersion = 5 block.vtx.append(spendtx) block.hashMerkleRoot = block.calc_merkle_root() block.rehash() block.solve() self.last_block_time += 1 yield TestInstance([[block, False]]) # Restart all stop_nodes(self.nodes) wait_bitcoinds() shutil.rmtree(self.options.tmpdir) self.setup_chain() self.setup_network() self.test.clear_all_connections() self.test.add_all_connections(self.nodes) NetworkThread().start() # Start up network handling in another thread def get_tests(self): for test in itertools.chain( self.test_BIP('csv', 536870913, self.sequence_lock_invalidate, self.donothing), self.test_BIP('csv', 536870913, self.mtp_invalidate, self.donothing), self.test_BIP('csv', 536870913, self.donothing, self.csv_invalidate) ): yield test def donothing(self, tx): return def csv_invalidate(self, tx): '''Modify the signature in vin 0 of the tx to fail CSV Prepends -1 CSV DROP in the scriptSig itself. ''' tx.vin[0].scriptSig = CScript([OP_1NEGATE, OP_NOP3, OP_DROP] + list(CScript(tx.vin[0].scriptSig))) def sequence_lock_invalidate(self, tx): '''Modify the nSequence to make it fails once sequence lock rule is activated (high timespan) ''' tx.vin[0].nSequence = 0x00FFFFFF tx.nLockTime = 0 def mtp_invalidate(self, tx): '''Modify the nLockTime to make it fails once MTP rule is activated ''' # Disable Sequence lock, Activate nLockTime tx.vin[0].nSequence = 0x90FFFFFF tx.nLockTime = self.last_block_time
class ReplayProtectionTest(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', "-replayprotectionactivationtime=%d" % REPLAY_PROTECTION_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(REPLAY_PROTECTION_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"replayprotection") 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, forkvalue=0): # Fund transaction script = CScript([public_key, OP_CHECKSIG]) 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 = (forkvalue << 8) | SIGHASH_ALL | SIGHASH_FORKID sighash = SignatureHashForkId( script, txspend, 0, sighashtype, 50 * COIN) sig = private_key.sign(sighash) + \ bytes(bytearray([SIGHASH_ALL | SIGHASH_FORKID])) 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 # Before the fork, no replay protection required to 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 txns get mined in a block properly. block(1) update_block(1, txns) yield accepted() # Replay protected transactions are rejected. replay_txns = create_fund_and_spend_tx(out[1], 0xffdead) send_transaction_to_mempool(replay_txns[0]) assert_raises_rpc_error(-26, RPC_INVALID_SIGNATURE_ERROR, node.sendrawtransaction, ToHex(replay_txns[1])) # And block containing them are rejected as well. block(2) update_block(2, replay_txns) yield rejected(RejectResult(16, b'blk-bad-inputs')) # Rewind bad block tip(1) # Create a block that would activate the replay protection. bfork = block(5555) bfork.nTime = REPLAY_PROTECTION_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'], REPLAY_PROTECTION_START_TIME - 1) # We are just before the fork, replay protected txns still are rejected assert_raises_rpc_error(-26, RPC_INVALID_SIGNATURE_ERROR, node.sendrawtransaction, ToHex(replay_txns[1])) block(3) update_block(3, replay_txns) yield rejected(RejectResult(16, b'blk-bad-inputs')) # Rewind bad block tip(5104) # Send some non replay protected txns in the mempool to check # they get cleaned at activation. txns = create_fund_and_spend_tx(out[2]) send_transaction_to_mempool(txns[0]) tx_id = send_transaction_to_mempool(txns[1]) # Activate the replay protection block(5556) yield accepted() # Non replay protected transactions are not valid anymore, # so they should be removed from the mempool. assert(tx_id not in set(node.getrawmempool())) # Good old transactions are now invalid. send_transaction_to_mempool(txns[0]) assert_raises_rpc_error(-26, RPC_INVALID_SIGNATURE_ERROR, node.sendrawtransaction, ToHex(txns[1])) # They also cannot be mined block(4) update_block(4, txns) yield rejected(RejectResult(16, b'blk-bad-inputs')) # Rewind bad block tip(5556) # The replay protected transaction is now valid send_transaction_to_mempool(replay_txns[0]) replay_tx_id = send_transaction_to_mempool(replay_txns[1]) # They also can also be mined b5 = block(5) update_block(5, replay_txns) yield accepted() # Ok, now we check if a reorg work properly accross the activation. postforkblockid = node.getbestblockhash() node.invalidateblock(postforkblockid) assert(replay_tx_id in set(node.getrawmempool())) # Deactivating replay protection. forkblockid = node.getbestblockhash() node.invalidateblock(forkblockid) assert(replay_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(replay_tx_id not in set(node.getrawmempool()))
class BSV128MbActivation(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 set_test_params(self): self.num_nodes = 1 self.setup_clean_chain = True self.block_heights = {} self.tip = None self.blocks = {} self.excessive_block_size = 64 * ONE_MEGABYTE self.extra_args = [[ '-whitelist=127.0.0.1', "-magneticactivationtime=%d" % MAGNETIC_START_TIME, "-excessiveblocksize=%d" % self.excessive_block_size ]] 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() self.nodes[0].setmocktime(MAGNETIC_START_TIME) 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, value, script=CScript([OP_TRUE])): tx = create_transaction(spend.tx, spend.n, b"", value, script) return tx def next_block(self, number, spend=None, script=CScript([OP_TRUE]), block_size=0, extra_sigops=0): 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() if spend == None: # We need to have something to spend to fill the block. assert_equal(block_size, 0) block = create_block(base_block_hash, coinbase, block_time) else: # all but one satoshi to fees coinbase.vout[0].nValue += spend.tx.vout[spend.n].nValue - 1 coinbase.rehash() block = create_block(base_block_hash, coinbase, block_time) # Make sure we have plenty engough to spend going forward. spendable_outputs = deque([spend]) def get_base_transaction(): # Create the new transaction tx = CTransaction() # Spend from one of the spendable outputs spend = spendable_outputs.popleft() tx.vin.append(CTxIn(COutPoint(spend.tx.sha256, spend.n))) # Add spendable outputs for i in range(4): tx.vout.append(CTxOut(0, CScript([OP_TRUE]))) spendable_outputs.append(PreviousSpendableOutput(tx, i)) return tx tx = get_base_transaction() # Make it the same format as transaction added for padding and save the size. # It's missing the padding output, so we add a constant to account for it. tx.rehash() base_tx_size = len(tx.serialize()) + 18 # If a specific script is required, add it. if script != None: tx.vout.append(CTxOut(1, script)) # Put some random data into the first transaction of the chain to randomize ids. tx.vout.append( CTxOut(0, CScript([random.randint(0, 256), OP_RETURN]))) # Add the transaction to the block self.add_transactions_to_block(block, [tx]) # If we have a block size requirement, just fill # the block until we get there current_block_size = len(block.serialize()) while current_block_size < block_size: # We will add a new transaction. That means the size of # the field enumerating how many transaction go in the block # may change. current_block_size -= len(ser_compact_size(len(block.vtx))) current_block_size += len(ser_compact_size(len(block.vtx) + 1)) # Create the new transaction tx = get_base_transaction() # Add padding to fill the block. script_length = block_size - current_block_size - base_tx_size if script_length > 510000: if script_length < 1000000: # Make sure we don't find ourselves in a position where we # need to generate a transaction smaller than what we expected. script_length = script_length // 2 else: 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, script_output)) # Add the tx to the list of transactions to be included # in the block. self.add_transactions_to_block(block, [tx]) current_block_size += len(tx.serialize()) # Now that we added a bunch of transaction, we need to recompute # the merkle root. block.hashMerkleRoot = block.calc_merkle_root() # Check that the block size is what's expected if block_size > 0: assert_equal(len(block.serialize()), block_size) # 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): # 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 # Test no excessiveblocksize parameter specified node = self.nodes[0] self.genesis_hash = int(node.getbestblockhash(), 16) self.block_heights[self.genesis_hash] = 0 spendable_outputs = [] # shorthand for functions block = self.next_block # 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()) # Let's build some blocks and test them. for i in range(15): n = i + 1 block(n, spend=out[i], block_size=n * ONE_MEGABYTE) yield accepted() # Start moving MTP forward bfork = block(5555, out[15], block_size=32 * ONE_MEGABYTE) bfork.nTime = MAGNETIC_START_TIME - 1 update_block(5555, []) yield accepted() # Get to one block of the Nov 15, 2018 HF activation for i in range(5): block(5100 + i) test.blocks_and_transactions.append([self.tip, True]) yield test # Check that the MTP is just before the configured fork point. assert_equal( node.getblockheader(node.getbestblockhash())['mediantime'], MAGNETIC_START_TIME - 1) # Before we acivate the Nov 15, 2018 HF, 64MB is the limit. # Oversized blocks will cause us to be disconnected assert (not self.test.test_nodes[0].closed) block(4444, spend=out[16], block_size=self.excessive_block_size + 1) self.test.connections[0].send_message(msg_block((self.tip))) self.test.wait_for_disconnections() assert (self.test.test_nodes[0].closed) # Rewind bad block and remake connection to node tip(5104) self.test.clear_all_connections() self.test.add_all_connections(self.nodes) NetworkThread().start() self.test.wait_for_verack() # Activate the Nov 15, 2018 HF block(5556) yield accepted() # Now MTP is exactly the fork time. Bigger blocks are now accepted. assert_equal( node.getblockheader(node.getbestblockhash())['mediantime'], MAGNETIC_START_TIME) # Block of maximal size (excessiveblocksize) block(17, spend=out[18], block_size=self.excessive_block_size) yield accepted() # Oversized blocks will cause us to be disconnected assert (not self.test.test_nodes[0].closed) block(18, spend=out[19], block_size=self.excessive_block_size + 1) self.test.connections[0].send_message(msg_block((self.tip))) self.test.wait_for_disconnections() assert (self.test.test_nodes[0].closed) # Rewind bad block and remake connection to node tip(17) self.test.clear_all_connections() self.test.add_all_connections(self.nodes) NetworkThread().start() self.test.wait_for_verack() # Check we can still mine a good size block block(5557, spend=out[20], block_size=self.excessive_block_size) yield accepted()
class TransactionOrderingTest(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 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', "-magneticanomalyactivationtime=%d" % MAGNETIC_ANOMALY_START_TIME, "-replayprotectionactivationtime=%d" % (2 * MAGNETIC_ANOMALY_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() # Set the blocksize to 2MB as initial condition self.nodes[0].setmocktime(MAGNETIC_ANOMALY_START_TIME) 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, value, script=CScript([OP_TRUE])): tx = create_transaction(spend.tx, spend.n, b"", value, script) return tx def next_block(self, number, spend=None, tx_count=0): 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() if spend == None: # We need to have something to spend to fill the block. block = create_block(base_block_hash, coinbase, block_time) else: # all but one satoshi to fees coinbase.vout[0].nValue += spend.tx.vout[spend.n].nValue - 1 coinbase.rehash() block = create_block(base_block_hash, coinbase, block_time) # Make sure we have plenty enough to spend going forward. spendable_outputs = deque([spend]) def get_base_transaction(): # Create the new transaction tx = CTransaction() # Spend from one of the spendable outputs spend = spendable_outputs.popleft() tx.vin.append(CTxIn(COutPoint(spend.tx.sha256, spend.n))) # Add spendable outputs for i in range(4): tx.vout.append(CTxOut(0, CScript([OP_TRUE]))) spendable_outputs.append(PreviousSpendableOutput(tx, i)) return tx tx = get_base_transaction() # Make it the same format as transaction added for padding and save the size. # It's missing the padding output, so we add a constant to account for it. tx.rehash() base_tx_size = len(tx.serialize()) + 18 # Put some random data into the first transaction of the chain to randomize ids. tx.vout.append( CTxOut(0, CScript([random.randint(0, 256), OP_RETURN]))) # Add the transaction to the block self.add_transactions_to_block(block, [tx]) # If we have a transaction count requirement, just fill the block until we get there while len(block.vtx) < tx_count: # Create the new transaction and add it. tx = get_base_transaction() self.add_transactions_to_block(block, [tx]) # Now that we added a bunch of transaction, we need to recompute # the merkle root. block.hashMerkleRoot = block.calc_merkle_root() if tx_count > 0: assert_equal(len(block.vtx), tx_count) # 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): node = self.nodes[0] self.genesis_hash = int(node.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) 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()) # Let's build some blocks and test them. for i in range(15): n = i + 1 block(n) yield accepted() # Start moving MTP forward bfork = block(5555) bfork.nTime = MAGNETIC_ANOMALY_START_TIME - 1 update_block(5555) yield accepted() # Get to one block of the Nov 15, 2018 HF activation for i in range(5): block(5100 + i) test.blocks_and_transactions.append([self.tip, True]) yield test # Check that the MTP is just before the configured fork point. assert_equal( node.getblockheader(node.getbestblockhash())['mediantime'], MAGNETIC_ANOMALY_START_TIME - 1) # Before we activate the Nov 15, 2018 HF, transaction order is respected. def out_of_order_block(block_number, spend): b = block(block_number, spend=spend, tx_count=3) b.vtx[1], b.vtx[2] = b.vtx[2], b.vtx[1] update_block(block_number) return b out_of_order_block(4444, out[16]) yield rejected(RejectResult(16, b'bad-txns-inputs-missingorspent')) # Rewind bad block. tip(5104) # Activate the Nov 15, 2018 HF block(5556) yield accepted() # Now MTP is exactly the fork time. Bigger blocks are now accepted. assert_equal( node.getblockheader(node.getbestblockhash())['mediantime'], MAGNETIC_ANOMALY_START_TIME) # Now that the fork activated, we can put transactions out of order in the block. out_of_order_block(4445, out[16]) yield accepted()
def run_test(self): test = TestManager(self, self.options.tmpdir) test.add_all_connections(self.nodes) network_thread_start() test.run()
class BIP135ForksTest(ComparisonTestFramework): def __init__(self): super().__init__() self.setup_clean_chain = True self.num_nodes = 1 self.defined_forks = [ "bip135test%d" % i for i in range(0,8) ] self.first_test = 1 def setup_network(self): # blocks are 1 second apart by default in this regtest # regtest bip135 deployments are defined for a blockchain that starts at MOCKTIME enable_mocktime() # starttimes are offset by 30 seconds from init_time self.fork_starttime = get_mocktime() + 30 self.nodes = start_nodes(1, self.options.tmpdir, extra_args=[['-debug=all', '-whitelist=127.0.0.1']], binary=[self.options.testbinary]) def run_test(self): self.test = TestManager(self, self.options.tmpdir) self.test.add_all_connections(self.nodes) NetworkThread().start() # Start up network handling in another thread self.test.run() def generate_blocks(self, number, version, test_blocks = []): for i in range(number): old_tip = self.tip self.height += 1 self.last_block_time += 600 block = create_block(self.tip, create_coinbase(absoluteHeight=self.height), self.last_block_time) block.nVersion = version block.rehash() block.solve() test_blocks.append([block, True]) self.tip = block.sha256 #logging.info ("generate_blocks: created block %x on tip %x, height %d, time %d" % (self.tip, old_tip, self.height-1, self.last_block_time)) return test_blocks def print_rpc_status(self): for f in self.defined_forks[self.first_test:]: info = self.nodes[0].getblockchaininfo() logging.info(info['bip135_forks'][f]) def test_BIP135Thresholds(self): logging.basicConfig(format='%(asctime)s.%(levelname)s: %(message)s', level=logging.INFO, stream=sys.stdout) logging.info("test_BIP135Thresholds: begin") node = self.nodes[0] self.tip = int("0x" + node.getbestblockhash(), 0) header = node.getblockheader("0x%x" % self.tip) assert_equal(header['height'], 0) bcinfo = node.getblockchaininfo() for f in self.defined_forks[self.first_test:]: assert_equal(bcinfo['bip135_forks'][f]['bit'], int(f[10:])) # Test 1 # generate a block and test addition of another self.coinbase_blocks = node.generate(1) self.height = 1 self.last_block_time = get_mocktime() self.tip = int("0x" + node.getbestblockhash(), 0) test_blocks = self.generate_blocks(1, VERSIONBITS_TOP_BITS) # do not set bit 1 yet yield TestInstance(test_blocks, sync_every_block=False) # Test 2 # check initial DEFINED state # check initial forks status and getblocktemplate logging.info("begin test 2") tip_mediantime = int(node.getblockchaininfo()['mediantime']) tmpl = node.getblocktemplate({}) assert_equal(tmpl['vbrequired'], 0) assert_equal(tmpl['version'], VERSIONBITS_TOP_BITS) logging.info("initial getblocktemplate:\n%s" % tmpl) test_blocks = self.generate_blocks(96, 0x20000001) yield TestInstance(test_blocks, sync_every_block=False) while tip_mediantime < self.fork_starttime or self.height < (100 - 1): for f in self.defined_forks[self.first_test:]: assert_equal(get_bip135_status(node, f)['status'], 'defined') assert(f not in tmpl['rules']) assert(f not in tmpl['vbavailable']) test_blocks = self.generate_blocks(1, 0x20000001) yield TestInstance(test_blocks, sync_every_block=False) tip_mediantime = int(node.getblockchaininfo()['mediantime']) tmpl = node.getblocktemplate({}) # Test 3 # Advance from DEFINED to STARTED logging.info("begin test 3") bcinfo = node.getblockchaininfo() for f in self.defined_forks[self.first_test:]: if int(f[10:]) > 1: assert_equal(bcinfo['bip135_forks'][f]['status'], 'started') assert(f not in tmpl['rules']) assert(f in tmpl['vbavailable']) elif int(f[10:]) == 1: # bit 1 only becomes started at height 144 assert_equal(bcinfo['bip135_forks'][f]['status'], 'defined') # Test 4 # Advance from DEFINED to STARTED logging.info("begin test 4") test_blocks = self.generate_blocks(42, 0x20000001) yield TestInstance(test_blocks, sync_every_block=False) # move up until it starts while self.height < (144 - 1): for f in self.defined_forks[self.first_test:]: if int(f[10:]) > 1: assert_equal(bcinfo['bip135_forks'][f]['status'], 'started') assert(f not in tmpl['rules']) assert(f in tmpl['vbavailable']) else: # bit 1 only becomes started at height 144 assert_equal(bcinfo['bip135_forks'][f]['status'], 'defined') test_blocks = self.generate_blocks(1, 0x20000001) yield TestInstance(test_blocks, sync_every_block=False) bcinfo = node.getblockchaininfo() tmpl = node.getblocktemplate({}) # now it should be started assert_equal(bcinfo['bip135_forks'][self.defined_forks[1]]['status'], 'started') assert(self.defined_forks[1] not in tmpl['rules']) assert_equal(tmpl['vbavailable'][self.defined_forks[1]], 1) assert_equal(tmpl['vbrequired'], 0) assert(tmpl['version'] & VERSIONBITS_TOP_BITS + 2**1) # Test 5 # Lock-in logging.info("begin test 5") # move to start of new 100-block window test_blocks = self.generate_blocks((100 - 1) - (self.height % 100), 0x20000003) yield TestInstance(test_blocks, sync_every_block=False) assert(self.height % 100 == 99) # generate enough of bits 2-7 in next 100 blocks to lock in fork bits 2-7 # bit 1 will only be locked in at next multiple of 144 # 1 block total for bit 2 test_blocks = self.generate_blocks(1, 0x200000FD) yield TestInstance(test_blocks, sync_every_block=False) # check still STARTED until we get to multiple of window size assert_equal(get_bip135_status(node, self.defined_forks[1])['status'], 'started') # 10 blocks total for bit 3 test_blocks = self.generate_blocks(9, 0x200000F9) yield TestInstance(test_blocks, sync_every_block=False) assert_equal(get_bip135_status(node, self.defined_forks[2])['status'], 'started') # 75 blocks total for bit 4 test_blocks = self.generate_blocks(65, 0x200000F1) yield TestInstance(test_blocks, sync_every_block=False) assert_equal(get_bip135_status(node, self.defined_forks[3])['status'], 'started') # 95 blocks total for bit 5 test_blocks = self.generate_blocks(20, 0x200000E1) yield TestInstance(test_blocks, sync_every_block=False) assert_equal(get_bip135_status(node, self.defined_forks[4])['status'], 'locked_in') # 99 blocks total for bit 6 test_blocks = self.generate_blocks(4, 0x200000C1) yield TestInstance(test_blocks, sync_every_block=False) assert_equal(get_bip135_status(node, self.defined_forks[5])['status'], 'started') # 100 blocks total for bit 7 test_blocks = self.generate_blocks(1, 0x20000081) yield TestInstance(test_blocks, sync_every_block=False) assert(self.height % 100 == 99) bcinfo = node.getblockchaininfo() assert_equal(bcinfo['bip135_forks'][f]['status'], 'locked_in') for f in self.defined_forks[2:]: if int(f[10:]) == 4: assert_equal(bcinfo['bip135_forks'][f]['status'], 'active') else: assert_equal(bcinfo['bip135_forks'][f]['status'], 'locked_in') assert_equal(bcinfo['bip135_forks'][self.defined_forks[1]]['status'], 'started') # move to start of new 144-block window test_blocks = self.generate_blocks((144 - 1) - (self.height % 144), 0x20000003) yield TestInstance(test_blocks, sync_every_block=False) assert(self.height % 144 == 143) bcinfo = node.getblockchaininfo() assert_equal(bcinfo['bip135_forks'][self.defined_forks[1]]['status'], 'locked_in') # Test 6 # Activation logging.info("begin test 6") for f in self.defined_forks[2:]: assert_equal(bcinfo['bip135_forks'][f]['status'], 'active') def get_tests(self): ''' run various tests ''' for test in itertools.chain( self.test_BIP135Thresholds(), # test thresholds on other bits ): yield test