Skip to content

Commit f1c339d

Browse files
author
MarcoFalke
committed
Merge #21211: test: Move P2WSH_OP_TRUE to shared test library
22220ef test: Move P2WSH_OP_TRUE to shared test library (MarcoFalke) Pull request description: Otherwise it can't be used in other tests (unit, fuzz, bench, ...) ACKs for top commit: darosior: ACK 22220ef Tree-SHA512: 1b636e751281291f7c21ac51c3d014f6a565144c9482974391c516228e756442b077655eda970eb8bdb12974b97855a909b2b60d518026a8d5f41aa15ec7cbc8
2 parents 09eb46c + 22220ef commit f1c339d

File tree

3 files changed

+26
-15
lines changed

3 files changed

+26
-15
lines changed

src/Makefile.test_util.include

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ TEST_UTIL_H = \
1212
test/util/logging.h \
1313
test/util/mining.h \
1414
test/util/net.h \
15+
test/util/script.h \
1516
test/util/setup_common.h \
1617
test/util/str.h \
1718
test/util/transaction_utils.h \

src/test/util/script.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright (c) 2021 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#ifndef BITCOIN_TEST_UTIL_SCRIPT_H
6+
#define BITCOIN_TEST_UTIL_SCRIPT_H
7+
8+
#include <crypto/sha256.h>
9+
#include <script/script.h>
10+
11+
static const std::vector<uint8_t> WITNESS_STACK_ELEM_OP_TRUE{uint8_t{OP_TRUE}};
12+
static const CScript P2WSH_OP_TRUE{
13+
CScript{}
14+
<< OP_0
15+
<< ToByteVector([] {
16+
uint256 hash;
17+
CSHA256().Write(WITNESS_STACK_ELEM_OP_TRUE.data(), WITNESS_STACK_ELEM_OP_TRUE.size()).Finalize(hash.begin());
18+
return hash;
19+
}())};
20+
21+
#endif // BITCOIN_TEST_UTIL_SCRIPT_H

src/test/validation_block_tests.cpp

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,14 @@
1111
#include <pow.h>
1212
#include <random.h>
1313
#include <script/standard.h>
14+
#include <test/util/script.h>
1415
#include <test/util/setup_common.h>
1516
#include <util/time.h>
1617
#include <validation.h>
1718
#include <validationinterface.h>
1819

1920
#include <thread>
2021

21-
static const std::vector<unsigned char> V_OP_TRUE{OP_TRUE};
22-
2322
namespace validation_block_tests {
2423
struct MinerTestingSetup : public RegTestingSetup {
2524
std::shared_ptr<CBlock> Block(const uint256& prev_hash);
@@ -64,27 +63,17 @@ std::shared_ptr<CBlock> MinerTestingSetup::Block(const uint256& prev_hash)
6463
static int i = 0;
6564
static uint64_t time = Params().GenesisBlock().nTime;
6665

67-
CScript pubKey;
68-
pubKey << i++ << OP_TRUE;
69-
70-
auto ptemplate = BlockAssembler(*m_node.mempool, Params()).CreateNewBlock(pubKey);
66+
auto ptemplate = BlockAssembler(*m_node.mempool, Params()).CreateNewBlock(CScript{} << i++ << OP_TRUE);
7167
auto pblock = std::make_shared<CBlock>(ptemplate->block);
7268
pblock->hashPrevBlock = prev_hash;
7369
pblock->nTime = ++time;
7470

75-
pubKey.clear();
76-
{
77-
WitnessV0ScriptHash witness_program;
78-
CSHA256().Write(&V_OP_TRUE[0], V_OP_TRUE.size()).Finalize(witness_program.begin());
79-
pubKey << OP_0 << ToByteVector(witness_program);
80-
}
81-
8271
// Make the coinbase transaction with two outputs:
8372
// One zero-value one that has a unique pubkey to make sure that blocks at the same height can have a different hash
8473
// Another one that has the coinbase reward in a P2WSH with OP_TRUE as witness program to make it easy to spend
8574
CMutableTransaction txCoinbase(*pblock->vtx[0]);
8675
txCoinbase.vout.resize(2);
87-
txCoinbase.vout[1].scriptPubKey = pubKey;
76+
txCoinbase.vout[1].scriptPubKey = P2WSH_OP_TRUE;
8877
txCoinbase.vout[1].nValue = txCoinbase.vout[0].nValue;
8978
txCoinbase.vout[0].nValue = 0;
9079
txCoinbase.vin[0].scriptWitness.SetNull();
@@ -254,7 +243,7 @@ BOOST_AUTO_TEST_CASE(mempool_locks_reorg)
254243
for (int num_txs = 22; num_txs > 0; --num_txs) {
255244
CMutableTransaction mtx;
256245
mtx.vin.push_back(CTxIn{COutPoint{last_mined->vtx[0]->GetHash(), 1}, CScript{}});
257-
mtx.vin[0].scriptWitness.stack.push_back(V_OP_TRUE);
246+
mtx.vin[0].scriptWitness.stack.push_back(WITNESS_STACK_ELEM_OP_TRUE);
258247
mtx.vout.push_back(last_mined->vtx[0]->vout[1]);
259248
mtx.vout[0].nValue -= 1000;
260249
txs.push_back(MakeTransactionRef(mtx));

0 commit comments

Comments
 (0)