Skip to content

Commit e2d4e67

Browse files
author
MarcoFalke
committed
Merge bitcoin/bitcoin#21840: test: Misc refactor to get rid of &foo[0] raw pointers
fa8a888 bench: Remove duplicate constants (MarcoFalke) 000098f test: Use throwing variant accessor (MarcoFalke) fa2197c test: Use loop to register RPCs (MarcoFalke) Pull request description: Simplify test code ACKs for top commit: Empact: Code Review ACK fa8a888 practicalswift: cr ACK fa8a888 promag: Code review ACK fa8a888. Tree-SHA512: 6a5bebaa9a3f43e9c332f4fbff606e9ece6dc8b95a769980082cc022f8e9bde6083c1e4a0145dcbf3741f514d6e97b4198f201a1bf1370ebf43bd3a5c0f85981
2 parents bf5e6a7 + fa8a888 commit e2d4e67

File tree

3 files changed

+20
-31
lines changed

3 files changed

+20
-31
lines changed

src/bench/block_assemble.cpp

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <consensus/validation.h>
77
#include <crypto/sha256.h>
88
#include <test/util/mining.h>
9+
#include <test/util/script.h>
910
#include <test/util/setup_common.h>
1011
#include <test/util/wallet.h>
1112
#include <txmempool.h>
@@ -18,23 +19,17 @@ static void AssembleBlock(benchmark::Bench& bench)
1819
{
1920
const auto test_setup = MakeNoLogFileContext<const TestingSetup>();
2021

21-
const std::vector<unsigned char> op_true{OP_TRUE};
2222
CScriptWitness witness;
23-
witness.stack.push_back(op_true);
24-
25-
uint256 witness_program;
26-
CSHA256().Write(&op_true[0], op_true.size()).Finalize(witness_program.begin());
27-
28-
const CScript SCRIPT_PUB{CScript(OP_0) << std::vector<unsigned char>{witness_program.begin(), witness_program.end()}};
23+
witness.stack.push_back(WITNESS_STACK_ELEM_OP_TRUE);
2924

3025
// Collect some loose transactions that spend the coinbases of our mined blocks
3126
constexpr size_t NUM_BLOCKS{200};
3227
std::array<CTransactionRef, NUM_BLOCKS - COINBASE_MATURITY + 1> txs;
3328
for (size_t b{0}; b < NUM_BLOCKS; ++b) {
3429
CMutableTransaction tx;
35-
tx.vin.push_back(MineBlock(test_setup->m_node, SCRIPT_PUB));
30+
tx.vin.push_back(MineBlock(test_setup->m_node, P2WSH_OP_TRUE));
3631
tx.vin.back().scriptWitness = witness;
37-
tx.vout.emplace_back(1337, SCRIPT_PUB);
32+
tx.vout.emplace_back(1337, P2WSH_OP_TRUE);
3833
if (NUM_BLOCKS - b >= COINBASE_MATURITY)
3934
txs.at(b) = MakeTransactionRef(tx);
4035
}
@@ -48,7 +43,7 @@ static void AssembleBlock(benchmark::Bench& bench)
4843
}
4944

5045
bench.run([&] {
51-
PrepareBlock(test_setup->m_node, SCRIPT_PUB);
46+
PrepareBlock(test_setup->m_node, P2WSH_OP_TRUE);
5247
});
5348
}
5449

src/qt/test/rpcnestedtests.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,16 @@ static RPCHelpMan rpcNestedTest_rpc()
3535
}
3636

3737
static const CRPCCommand vRPCCommands[] = {
38-
{"test", &rpcNestedTest_rpc},
38+
{"rpcNestedTest", &rpcNestedTest_rpc},
3939
};
4040

4141
void RPCNestedTests::rpcNestedTests()
4242
{
4343
// do some test setup
4444
// could be moved to a more generic place when we add more tests on QT level
45-
tableRPC.appendCommand("rpcNestedTest", &vRPCCommands[0]);
45+
for (const auto& c : vRPCCommands) {
46+
tableRPC.appendCommand(c.name, &c);
47+
}
4648

4749
TestingSetup test;
4850
m_node.setContext(&test.m_node);

src/test/script_standard_tests.cpp

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -199,23 +199,20 @@ BOOST_AUTO_TEST_CASE(script_standard_ExtractDestination)
199199
s.clear();
200200
s << ToByteVector(pubkey) << OP_CHECKSIG;
201201
BOOST_CHECK(ExtractDestination(s, address));
202-
BOOST_CHECK(std::get_if<PKHash>(&address) &&
203-
*std::get_if<PKHash>(&address) == PKHash(pubkey));
202+
BOOST_CHECK(std::get<PKHash>(address) == PKHash(pubkey));
204203

205204
// TxoutType::PUBKEYHASH
206205
s.clear();
207206
s << OP_DUP << OP_HASH160 << ToByteVector(pubkey.GetID()) << OP_EQUALVERIFY << OP_CHECKSIG;
208207
BOOST_CHECK(ExtractDestination(s, address));
209-
BOOST_CHECK(std::get_if<PKHash>(&address) &&
210-
*std::get_if<PKHash>(&address) == PKHash(pubkey));
208+
BOOST_CHECK(std::get<PKHash>(address) == PKHash(pubkey));
211209

212210
// TxoutType::SCRIPTHASH
213211
CScript redeemScript(s); // initialize with leftover P2PKH script
214212
s.clear();
215213
s << OP_HASH160 << ToByteVector(CScriptID(redeemScript)) << OP_EQUAL;
216214
BOOST_CHECK(ExtractDestination(s, address));
217-
BOOST_CHECK(std::get_if<ScriptHash>(&address) &&
218-
*std::get_if<ScriptHash>(&address) == ScriptHash(redeemScript));
215+
BOOST_CHECK(std::get<ScriptHash>(address) == ScriptHash(redeemScript));
219216

220217
// TxoutType::MULTISIG
221218
s.clear();
@@ -233,15 +230,15 @@ BOOST_AUTO_TEST_CASE(script_standard_ExtractDestination)
233230
BOOST_CHECK(ExtractDestination(s, address));
234231
WitnessV0KeyHash keyhash;
235232
CHash160().Write(pubkey).Finalize(keyhash);
236-
BOOST_CHECK(std::get_if<WitnessV0KeyHash>(&address) && *std::get_if<WitnessV0KeyHash>(&address) == keyhash);
233+
BOOST_CHECK(std::get<WitnessV0KeyHash>(address) == keyhash);
237234

238235
// TxoutType::WITNESS_V0_SCRIPTHASH
239236
s.clear();
240237
WitnessV0ScriptHash scripthash;
241238
CSHA256().Write(redeemScript.data(), redeemScript.size()).Finalize(scripthash.begin());
242239
s << OP_0 << ToByteVector(scripthash);
243240
BOOST_CHECK(ExtractDestination(s, address));
244-
BOOST_CHECK(std::get_if<WitnessV0ScriptHash>(&address) && *std::get_if<WitnessV0ScriptHash>(&address) == scripthash);
241+
BOOST_CHECK(std::get<WitnessV0ScriptHash>(address) == scripthash);
245242

246243
// TxoutType::WITNESS_UNKNOWN with unknown version
247244
s.clear();
@@ -251,7 +248,7 @@ BOOST_AUTO_TEST_CASE(script_standard_ExtractDestination)
251248
unk.length = 33;
252249
unk.version = 1;
253250
std::copy(pubkey.begin(), pubkey.end(), unk.program);
254-
BOOST_CHECK(std::get_if<WitnessUnknown>(&address) && *std::get_if<WitnessUnknown>(&address) == unk);
251+
BOOST_CHECK(std::get<WitnessUnknown>(address) == unk);
255252
}
256253

257254
BOOST_AUTO_TEST_CASE(script_standard_ExtractDestinations)
@@ -275,8 +272,7 @@ BOOST_AUTO_TEST_CASE(script_standard_ExtractDestinations)
275272
BOOST_CHECK_EQUAL(whichType, TxoutType::PUBKEY);
276273
BOOST_CHECK_EQUAL(addresses.size(), 1U);
277274
BOOST_CHECK_EQUAL(nRequired, 1);
278-
BOOST_CHECK(std::get_if<PKHash>(&addresses[0]) &&
279-
*std::get_if<PKHash>(&addresses[0]) == PKHash(pubkeys[0]));
275+
BOOST_CHECK(std::get<PKHash>(addresses[0]) == PKHash(pubkeys[0]));
280276

281277
// TxoutType::PUBKEYHASH
282278
s.clear();
@@ -285,8 +281,7 @@ BOOST_AUTO_TEST_CASE(script_standard_ExtractDestinations)
285281
BOOST_CHECK_EQUAL(whichType, TxoutType::PUBKEYHASH);
286282
BOOST_CHECK_EQUAL(addresses.size(), 1U);
287283
BOOST_CHECK_EQUAL(nRequired, 1);
288-
BOOST_CHECK(std::get_if<PKHash>(&addresses[0]) &&
289-
*std::get_if<PKHash>(&addresses[0]) == PKHash(pubkeys[0]));
284+
BOOST_CHECK(std::get<PKHash>(addresses[0]) == PKHash(pubkeys[0]));
290285

291286
// TxoutType::SCRIPTHASH
292287
CScript redeemScript(s); // initialize with leftover P2PKH script
@@ -296,8 +291,7 @@ BOOST_AUTO_TEST_CASE(script_standard_ExtractDestinations)
296291
BOOST_CHECK_EQUAL(whichType, TxoutType::SCRIPTHASH);
297292
BOOST_CHECK_EQUAL(addresses.size(), 1U);
298293
BOOST_CHECK_EQUAL(nRequired, 1);
299-
BOOST_CHECK(std::get_if<ScriptHash>(&addresses[0]) &&
300-
*std::get_if<ScriptHash>(&addresses[0]) == ScriptHash(redeemScript));
294+
BOOST_CHECK(std::get<ScriptHash>(addresses[0]) == ScriptHash(redeemScript));
301295

302296
// TxoutType::MULTISIG
303297
s.clear();
@@ -309,10 +303,8 @@ BOOST_AUTO_TEST_CASE(script_standard_ExtractDestinations)
309303
BOOST_CHECK_EQUAL(whichType, TxoutType::MULTISIG);
310304
BOOST_CHECK_EQUAL(addresses.size(), 2U);
311305
BOOST_CHECK_EQUAL(nRequired, 2);
312-
BOOST_CHECK(std::get_if<PKHash>(&addresses[0]) &&
313-
*std::get_if<PKHash>(&addresses[0]) == PKHash(pubkeys[0]));
314-
BOOST_CHECK(std::get_if<PKHash>(&addresses[1]) &&
315-
*std::get_if<PKHash>(&addresses[1]) == PKHash(pubkeys[1]));
306+
BOOST_CHECK(std::get<PKHash>(addresses[0]) == PKHash(pubkeys[0]));
307+
BOOST_CHECK(std::get<PKHash>(addresses[1]) == PKHash(pubkeys[1]));
316308

317309
// TxoutType::NULL_DATA
318310
s.clear();

0 commit comments

Comments
 (0)