Skip to content

Commit ad0fc45

Browse files
author
MarcoFalke
committed
Merge bitcoin#16333: test: Set BIP34Height = 2 for regtest
222290f test: Set BIP34Height = 2 for regtest (MarcoFalke) fac90c5 test: Create all blocks with version 4 or higher (MarcoFalke) Pull request description: BIP34 is active on the current tip of mainnet, so all miners must obey it. It would be nice if it also was active in fresh regtest instances from the earliest time possible. I changed the BIP34 height to `2`, so that the block at height=1 may be used to mine a duplicate coinbase. (Needed to test mainnet behaviour) This pull is done in two commits: * test: Create all blocks with version 4 or higher: Now that BIP34 is activated earlier, we need to create blocks with a higher version number. Just bump it to 4 instead of 2 to avoid having to bump it again later. * test: Set BIP34Height = 2 for regtest: This fixes the BIP34 implementation in the tests (to match the one of the Core codebase) and updates the tests where needed ACKs for top commit: ajtowns: ACK 222290f jonatack: ACK 222290f tested and reviewed rebased to current master 5e21382 theStack: Tested ACK 222290f Tree-SHA512: d69c637a62a64b8e87de8c7f0b305823d8f4d115c1852514b923625dbbcf9a4854b5bb3771ff41702ebf47c4c182a4442c6d7c0b9f282c95a34b83e56a73939b
2 parents 5cf28d5 + 222290f commit ad0fc45

File tree

6 files changed

+18
-14
lines changed

6 files changed

+18
-14
lines changed

src/chainparams.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ class CRegTestParams : public CChainParams {
390390
consensus.signet_challenge.clear();
391391
consensus.nSubsidyHalvingInterval = 150;
392392
consensus.BIP16Exception = uint256();
393-
consensus.BIP34Height = 500; // BIP34 activated on regtest (Used in functional tests)
393+
consensus.BIP34Height = 2; // BIP34 activated on regtest (Block at height 1 not enforced for testing purposes)
394394
consensus.BIP34Hash = uint256();
395395
consensus.BIP65Height = 1351; // BIP65 activated on regtest (Used in functional tests)
396396
consensus.BIP66Height = 1251; // BIP66 activated on regtest (Used in functional tests)

src/test/validation_block_tests.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,22 +77,29 @@ std::shared_ptr<CBlock> MinerTestingSetup::Block(const uint256& prev_hash)
7777
txCoinbase.vout[1].nValue = txCoinbase.vout[0].nValue;
7878
txCoinbase.vout[0].nValue = 0;
7979
txCoinbase.vin[0].scriptWitness.SetNull();
80+
// Always pad with OP_0 at the end to avoid bad-cb-length error
81+
txCoinbase.vin[0].scriptSig = CScript{} << WITH_LOCK(::cs_main, return m_node.chainman->m_blockman.LookupBlockIndex(prev_hash)->nHeight + 1) << OP_0;
8082
pblock->vtx[0] = MakeTransactionRef(std::move(txCoinbase));
8183

8284
return pblock;
8385
}
8486

8587
std::shared_ptr<CBlock> MinerTestingSetup::FinalizeBlock(std::shared_ptr<CBlock> pblock)
8688
{
87-
LOCK(cs_main); // For m_node.chainman->m_blockman.LookupBlockIndex
88-
GenerateCoinbaseCommitment(*pblock, m_node.chainman->m_blockman.LookupBlockIndex(pblock->hashPrevBlock), Params().GetConsensus());
89+
const CBlockIndex* prev_block{WITH_LOCK(::cs_main, return m_node.chainman->m_blockman.LookupBlockIndex(pblock->hashPrevBlock))};
90+
GenerateCoinbaseCommitment(*pblock, prev_block, Params().GetConsensus());
8991

9092
pblock->hashMerkleRoot = BlockMerkleRoot(*pblock);
9193

9294
while (!CheckProofOfWork(pblock->GetHash(), pblock->nBits, Params().GetConsensus())) {
9395
++(pblock->nNonce);
9496
}
9597

98+
// submit block header, so that miner can get the block height from the
99+
// global state and the node has the topology of the chain
100+
BlockValidationState ignored;
101+
BOOST_CHECK(Assert(m_node.chainman)->ProcessNewBlockHeaders({pblock->GetBlockHeader()}, ignored, Params()));
102+
96103
return pblock;
97104
}
98105

@@ -147,13 +154,6 @@ BOOST_AUTO_TEST_CASE(processnewblock_signals_ordering)
147154
}
148155

149156
bool ignored;
150-
BlockValidationState state;
151-
std::vector<CBlockHeader> headers;
152-
std::transform(blocks.begin(), blocks.end(), std::back_inserter(headers), [](std::shared_ptr<const CBlock> b) { return b->GetBlockHeader(); });
153-
154-
// Process all the headers so we understand the toplogy of the chain
155-
BOOST_CHECK(Assert(m_node.chainman)->ProcessNewBlockHeaders(headers, state, Params()));
156-
157157
// Connect the genesis block and drain any outstanding events
158158
BOOST_CHECK(Assert(m_node.chainman)->ProcessNewBlock(Params(), std::make_shared<CBlock>(Params().GenesisBlock()), true, &ignored));
159159
SyncWithValidationInterfaceQueue();

test/functional/feature_block.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,9 @@ def run_test(self):
373373
# b30 has a max-sized coinbase scriptSig.
374374
self.move_tip(23)
375375
b30 = self.next_block(30)
376-
b30.vtx[0].vin[0].scriptSig = b'\x00' * 100
376+
b30.vtx[0].vin[0].scriptSig = bytes(b30.vtx[0].vin[0].scriptSig) # Convert CScript to raw bytes
377+
b30.vtx[0].vin[0].scriptSig += b'\x00' * (100 - len(b30.vtx[0].vin[0].scriptSig)) # Fill with 0s
378+
assert_equal(len(b30.vtx[0].vin[0].scriptSig), 100)
377379
b30.vtx[0].rehash()
378380
b30 = self.update_block(30, [])
379381
self.send_blocks([b30], True)
@@ -833,6 +835,7 @@ def run_test(self):
833835
b61.vtx[0].rehash()
834836
b61 = self.update_block(61, [])
835837
assert_equal(duplicate_tx.serialize(), b61.vtx[0].serialize())
838+
# BIP30 is always checked on regtest, regardless of the BIP34 activation height
836839
self.send_blocks([b61], success=False, reject_reason='bad-txns-BIP30', reconnect=True)
837840

838841
# Test BIP30 (allow duplicate if spent)

test/functional/rpc_blockchain.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ def _test_getblockchaininfo(self):
140140
assert_greater_than(res['size_on_disk'], 0)
141141

142142
assert_equal(res['softforks'], {
143-
'bip34': {'type': 'buried', 'active': False, 'height': 500},
143+
'bip34': {'type': 'buried', 'active': True, 'height': 2},
144144
'bip66': {'type': 'buried', 'active': False, 'height': 1251},
145145
'bip65': {'type': 'buried', 'active': False, 'height': 1351},
146146
'csv': {'type': 'buried', 'active': False, 'height': 432},

test/functional/test_framework/blocktools.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,15 @@
6262
WITNESS_COMMITMENT_HEADER = b"\xaa\x21\xa9\xed"
6363

6464
NORMAL_GBT_REQUEST_PARAMS = {"rules": ["segwit"]}
65+
VERSIONBITS_LAST_OLD_BLOCK_VERSION = 4
6566

6667

6768
def create_block(hashprev=None, coinbase=None, ntime=None, *, version=None, tmpl=None, txlist=None):
6869
"""Create a block (with regtest difficulty)."""
6970
block = CBlock()
7071
if tmpl is None:
7172
tmpl = {}
72-
block.nVersion = version or tmpl.get('version') or 1
73+
block.nVersion = version or tmpl.get('version') or VERSIONBITS_LAST_OLD_BLOCK_VERSION
7374
block.nTime = ntime or tmpl.get('curtime') or int(time.time() + 600)
7475
block.hashPrevBlock = hashprev or int(tmpl['previousblockhash'], 0x10)
7576
if tmpl and not tmpl.get('bits') is None:

test/functional/test_framework/messages.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,7 @@ def __init__(self, header=None):
642642
self.calc_sha256()
643643

644644
def set_null(self):
645-
self.nVersion = 1
645+
self.nVersion = 4
646646
self.hashPrevBlock = 0
647647
self.hashMerkleRoot = 0
648648
self.nTime = 0

0 commit comments

Comments
 (0)