Skip to content

Commit

Permalink
test: Add basic test for BIP34
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoFalke committed Dec 29, 2018
1 parent cbb91cd commit fab17e8
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 13 deletions.
6 changes: 3 additions & 3 deletions src/chainparams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,10 +272,10 @@ class CRegTestParams : public CChainParams {
strNetworkID = "regtest";
consensus.nSubsidyHalvingInterval = 150;
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.BIP65Height = 1351; // BIP65 activated on regtest (Used in rpc activation tests)
consensus.BIP66Height = 1251; // BIP66 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 functional tests)
consensus.powLimit = uint256S("7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
consensus.nPowTargetTimespan = 14 * 24 * 60 * 60; // two weeks
consensus.nPowTargetSpacing = 10 * 60;
Expand Down
30 changes: 21 additions & 9 deletions test/functional/feature_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -874,7 +874,7 @@ def run_test(self):
# \-> 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)
b67 = self.next_block(67)
tx1 = self.create_and_sign_transaction(out[20], out[20].vout[0].nValue)
Expand Down Expand Up @@ -1169,7 +1169,7 @@ def run_test(self):
blocks = []
spend = out[32]
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()
script_length = MAX_BLOCK_BASE_SIZE - len(b.serialize()) - 69
script_output = CScript([b'\x00' * script_length])
Expand All @@ -1188,20 +1188,32 @@ def run_test(self):
self.move_tip(88)
blocks2 = []
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)

# 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)

# ... and re-org back to the first chain
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)
block = self.next_block(chain1_tip + 2)
block = self.next_block(chain1_tip + 2, version=4)
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
################

Expand Down Expand Up @@ -1229,7 +1241,7 @@ def create_and_sign_transaction(self, spend_tx, value, script=CScript([OP_TRUE])
tx.rehash()
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:
base_block_hash = self.genesis_hash
block_time = int(time.time()) + 1
Expand All @@ -1242,11 +1254,11 @@ def next_block(self, number, spend=None, additional_coinbase_value=0, script=CSc
coinbase.vout[0].nValue += additional_coinbase_value
coinbase.rehash()
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:
coinbase.vout[0].nValue += spend.vout[0].nValue - 1 # all but one satoshi to fees
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
self.sign_tx(tx, spend)
self.add_transactions_to_block(block, [tx])
Expand Down
3 changes: 2 additions & 1 deletion test/functional/test_framework/blocktools.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@
# From BIP141
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)."""
block = CBlock()
block.nVersion = version
if ntime is None:
import time
block.nTime = int(time.time() + 600)
Expand Down

0 comments on commit fab17e8

Please sign in to comment.