Merge #15059: test: Add basic test for BIP34

fab17e8272 test: Add basic test for BIP34 (MarcoFalke)

Pull request description:

  BIP34 was disabled for testing, which explains why it had no test.

  Fix that by enabling it and adding a test.

Tree-SHA512: 9cb5702d474117ce6420226eb93ee09d6fb5fc856fabc8b67abe56a088cd727674e0e5462000e1afa83b911374036f90abdbdde56a8c236a75572ed47e10a00f
This commit is contained in:
Wladimir J. van der Laan 2019-01-08 15:37:33 +01:00
commit c6806ee869
No known key found for this signature in database
GPG key ID: 1E4AED62986CD25D
3 changed files with 26 additions and 13 deletions

View file

@ -272,10 +272,10 @@ public:
strNetworkID = "regtest"; strNetworkID = "regtest";
consensus.nSubsidyHalvingInterval = 150; consensus.nSubsidyHalvingInterval = 150;
consensus.BIP16Exception = uint256(); consensus.BIP16Exception = uint256();
consensus.BIP34Height = 100000000; // BIP34 has not activated on regtest (far in the future so block v1 are not rejected in tests) consensus.BIP34Height = 500; // BIP34 activated on regtest (Used in functional tests)
consensus.BIP34Hash = uint256(); consensus.BIP34Hash = uint256();
consensus.BIP65Height = 1351; // BIP65 activated on regtest (Used in rpc activation tests) consensus.BIP65Height = 1351; // BIP65 activated on regtest (Used in functional tests)
consensus.BIP66Height = 1251; // BIP66 activated on regtest (Used in rpc activation tests) consensus.BIP66Height = 1251; // BIP66 activated on regtest (Used in functional tests)
consensus.powLimit = uint256S("7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"); consensus.powLimit = uint256S("7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
consensus.nPowTargetTimespan = 14 * 24 * 60 * 60; // two weeks consensus.nPowTargetTimespan = 14 * 24 * 60 * 60; // two weeks
consensus.nPowTargetSpacing = 10 * 60; consensus.nPowTargetSpacing = 10 * 60;

View file

@ -925,7 +925,7 @@ class FullBlockTest(BitcoinTestFramework):
# \-> b67 (20) # \-> b67 (20)
# #
# #
self.log.info("Reject a block with a transaction double spending a transaction creted in the same block") self.log.info("Reject a block with a transaction double spending a transaction created in the same block")
self.move_tip(65) self.move_tip(65)
b67 = self.next_block(67) b67 = self.next_block(67)
tx1 = self.create_and_sign_transaction(out[20], out[20].vout[0].nValue) tx1 = self.create_and_sign_transaction(out[20], out[20].vout[0].nValue)
@ -1220,7 +1220,7 @@ class FullBlockTest(BitcoinTestFramework):
blocks = [] blocks = []
spend = out[32] spend = out[32]
for i in range(89, LARGE_REORG_SIZE + 89): for i in range(89, LARGE_REORG_SIZE + 89):
b = self.next_block(i, spend) b = self.next_block(i, spend, version=4)
tx = CTransaction() tx = CTransaction()
script_length = MAX_BLOCK_BASE_SIZE - len(b.serialize()) - 69 script_length = MAX_BLOCK_BASE_SIZE - len(b.serialize()) - 69
script_output = CScript([b'\x00' * script_length]) script_output = CScript([b'\x00' * script_length])
@ -1239,20 +1239,32 @@ class FullBlockTest(BitcoinTestFramework):
self.move_tip(88) self.move_tip(88)
blocks2 = [] blocks2 = []
for i in range(89, LARGE_REORG_SIZE + 89): for i in range(89, LARGE_REORG_SIZE + 89):
blocks2.append(self.next_block("alt" + str(i))) blocks2.append(self.next_block("alt" + str(i), version=4))
self.sync_blocks(blocks2, False, force_send=True) self.sync_blocks(blocks2, False, force_send=True)
# extend alt chain to trigger re-org # extend alt chain to trigger re-org
block = self.next_block("alt" + str(chain1_tip + 1)) block = self.next_block("alt" + str(chain1_tip + 1), version=4)
self.sync_blocks([block], True, timeout=480) self.sync_blocks([block], True, timeout=480)
# ... and re-org back to the first chain # ... and re-org back to the first chain
self.move_tip(chain1_tip) self.move_tip(chain1_tip)
block = self.next_block(chain1_tip + 1) block = self.next_block(chain1_tip + 1, version=4)
self.sync_blocks([block], False, force_send=True) self.sync_blocks([block], False, force_send=True)
block = self.next_block(chain1_tip + 2) block = self.next_block(chain1_tip + 2, version=4)
self.sync_blocks([block], True, timeout=480) self.sync_blocks([block], True, timeout=480)
self.log.info("Reject a block with an invalid block header version")
b_v1 = self.next_block('b_v1', version=1)
self.sync_blocks([b_v1], success=False, force_send=True, reject_reason='bad-version(0x00000001)')
self.move_tip(chain1_tip + 2)
b_cb34 = self.next_block('b_cb34', version=4)
b_cb34.vtx[0].vin[0].scriptSig = b_cb34.vtx[0].vin[0].scriptSig[:-1]
b_cb34.vtx[0].rehash()
b_cb34.hashMerkleRoot = b_cb34.calc_merkle_root()
b_cb34.solve()
self.sync_blocks([b_cb34], success=False, reject_reason='bad-cb-height', reconnect=True)
# Helper methods # Helper methods
################ ################
@ -1280,7 +1292,7 @@ class FullBlockTest(BitcoinTestFramework):
tx.rehash() tx.rehash()
return tx return tx
def next_block(self, number, spend=None, additional_coinbase_value=0, script=CScript([OP_TRUE]), solve=True): def next_block(self, number, spend=None, additional_coinbase_value=0, script=CScript([OP_TRUE]), solve=True, *, version=1):
if self.tip is None: if self.tip is None:
base_block_hash = self.genesis_hash base_block_hash = self.genesis_hash
block_time = int(time.time()) + 1 block_time = int(time.time()) + 1
@ -1293,11 +1305,11 @@ class FullBlockTest(BitcoinTestFramework):
coinbase.vout[0].nValue += additional_coinbase_value coinbase.vout[0].nValue += additional_coinbase_value
coinbase.rehash() coinbase.rehash()
if spend is None: if spend is None:
block = create_block(base_block_hash, coinbase, block_time) block = create_block(base_block_hash, coinbase, block_time, version=version)
else: else:
coinbase.vout[0].nValue += spend.vout[0].nValue - 1 # all but one satoshi to fees coinbase.vout[0].nValue += spend.vout[0].nValue - 1 # all but one satoshi to fees
coinbase.rehash() coinbase.rehash()
block = create_block(base_block_hash, coinbase, block_time) block = create_block(base_block_hash, coinbase, block_time, version=version)
tx = self.create_tx(spend, 0, 1, script) # spend 1 satoshi tx = self.create_tx(spend, 0, 1, script) # spend 1 satoshi
self.sign_tx(tx, spend) self.sign_tx(tx, spend)
self.add_transactions_to_block(block, [tx]) self.add_transactions_to_block(block, [tx])

View file

@ -46,9 +46,10 @@ MAX_BLOCK_SIGOPS = 20000
# From BIP141 # From BIP141
WITNESS_COMMITMENT_HEADER = b"\xaa\x21\xa9\xed" WITNESS_COMMITMENT_HEADER = b"\xaa\x21\xa9\xed"
def create_block(hashprev, coinbase, ntime=None): def create_block(hashprev, coinbase, ntime=None, *, version=1):
"""Create a block (with regtest difficulty).""" """Create a block (with regtest difficulty)."""
block = CBlock() block = CBlock()
block.nVersion = version
if ntime is None: if ntime is None:
import time import time
block.nTime = int(time.time() + 600) block.nTime = int(time.time() + 600)