Skip to content

Commit f613e5d

Browse files
committed
[test] move mining helper functions into test library
1 parent 2cb4e8b commit f613e5d

File tree

7 files changed

+79
-59
lines changed

7 files changed

+79
-59
lines changed

src/Makefile.test_util.include

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ EXTRA_LIBRARIES += \
1010
TEST_UTIL_H = \
1111
test/util/blockfilter.h \
1212
test/util/logging.h \
13+
test/util/mining.h \
1314
test/util/setup_common.h \
1415
test/util/str.h \
1516
test/util/transaction_utils.h
@@ -19,6 +20,7 @@ libtest_util_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
1920
libtest_util_a_SOURCES = \
2021
test/util/blockfilter.cpp \
2122
test/util/logging.cpp \
23+
test/util/mining.cpp \
2224
test/util/setup_common.cpp \
2325
test/util/str.cpp \
2426
test/util/transaction_utils.cpp \
@@ -28,4 +30,3 @@ LIBTEST_UTIL += $(LIBBITCOIN_SERVER)
2830
LIBTEST_UTIL += $(LIBBITCOIN_COMMON)
2931
LIBTEST_UTIL += $(LIBBITCOIN_UTIL)
3032
LIBTEST_UTIL += $(LIBBITCOIN_CRYPTO_BASE)
31-

src/bench/block_assemble.cpp

Lines changed: 1 addition & 0 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.h>
9+
#include <test/util/mining.h>
910
#include <txmempool.h>
1011
#include <validation.h>
1112

src/bench/wallet_balance.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <node/context.h>
88
#include <optional.h>
99
#include <test/util.h>
10+
#include <test/util/mining.h>
1011
#include <validationinterface.h>
1112
#include <wallet/wallet.h>
1213

src/test/util.cpp

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,9 @@
44

55
#include <test/util.h>
66

7-
#include <chainparams.h>
8-
#include <consensus/merkle.h>
97
#include <key_io.h>
10-
#include <miner.h>
118
#include <outputtype.h>
12-
#include <pow.h>
139
#include <script/standard.h>
14-
#include <validation.h>
15-
#include <validationinterface.h>
1610
#ifdef ENABLE_WALLET
1711
#include <wallet/wallet.h>
1812
#endif
@@ -44,41 +38,3 @@ void importaddress(CWallet& wallet, const std::string& address)
4438
wallet.SetAddressBook(dest, /* label */ "", "receive");
4539
}
4640
#endif // ENABLE_WALLET
47-
48-
CTxIn generatetoaddress(const std::string& address)
49-
{
50-
const auto dest = DecodeDestination(address);
51-
assert(IsValidDestination(dest));
52-
const auto coinbase_script = GetScriptForDestination(dest);
53-
54-
return MineBlock(coinbase_script);
55-
}
56-
57-
CTxIn MineBlock(const CScript& coinbase_scriptPubKey)
58-
{
59-
auto block = PrepareBlock(coinbase_scriptPubKey);
60-
61-
while (!CheckProofOfWork(block->GetHash(), block->nBits, Params().GetConsensus())) {
62-
++block->nNonce;
63-
assert(block->nNonce);
64-
}
65-
66-
bool processed{ProcessNewBlock(Params(), block, true, nullptr)};
67-
assert(processed);
68-
69-
return CTxIn{block->vtx[0]->GetHash(), 0};
70-
}
71-
72-
std::shared_ptr<CBlock> PrepareBlock(const CScript& coinbase_scriptPubKey)
73-
{
74-
auto block = std::make_shared<CBlock>(
75-
BlockAssembler{Params()}
76-
.CreateNewBlock(coinbase_scriptPubKey)
77-
->block);
78-
79-
LOCK(cs_main);
80-
block->nTime = ::ChainActive().Tip()->GetMedianTimePast() + 1;
81-
block->hashMerkleRoot = BlockMerkleRoot(*block);
82-
83-
return block;
84-
}

src/test/util.h

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,19 @@
55
#ifndef BITCOIN_TEST_UTIL_H
66
#define BITCOIN_TEST_UTIL_H
77

8-
#include <memory>
98
#include <string>
109

11-
class CBlock;
12-
class CScript;
13-
class CTxIn;
1410
class CWallet;
1511

1612
// Constants //
1713

1814
extern const std::string ADDRESS_BCRT1_UNSPENDABLE;
1915

20-
// Lower-level utils //
21-
22-
/** Returns the generated coin */
23-
CTxIn MineBlock(const CScript& coinbase_scriptPubKey);
24-
/** Prepare a block to be mined */
25-
std::shared_ptr<CBlock> PrepareBlock(const CScript& coinbase_scriptPubKey);
26-
27-
2816
// RPC-like //
2917

3018
/** Import the address to the wallet */
3119
void importaddress(CWallet& wallet, const std::string& address);
3220
/** Returns a new address from the wallet */
3321
std::string getnewaddress(CWallet& w);
34-
/** Returns the generated coin */
35-
CTxIn generatetoaddress(const std::string& address);
3622

3723
#endif // BITCOIN_TEST_UTIL_H

src/test/util/mining.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright (c) 2019 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+
#include <test/util/mining.h>
6+
7+
#include <chainparams.h>
8+
#include <consensus/merkle.h>
9+
#include <key_io.h>
10+
#include <miner.h>
11+
#include <pow.h>
12+
#include <script/standard.h>
13+
#include <validation.h>
14+
15+
CTxIn generatetoaddress(const std::string& address)
16+
{
17+
const auto dest = DecodeDestination(address);
18+
assert(IsValidDestination(dest));
19+
const auto coinbase_script = GetScriptForDestination(dest);
20+
21+
return MineBlock(coinbase_script);
22+
}
23+
24+
CTxIn MineBlock(const CScript& coinbase_scriptPubKey)
25+
{
26+
auto block = PrepareBlock(coinbase_scriptPubKey);
27+
28+
while (!CheckProofOfWork(block->GetHash(), block->nBits, Params().GetConsensus())) {
29+
++block->nNonce;
30+
assert(block->nNonce);
31+
}
32+
33+
bool processed{ProcessNewBlock(Params(), block, true, nullptr)};
34+
assert(processed);
35+
36+
return CTxIn{block->vtx[0]->GetHash(), 0};
37+
}
38+
39+
std::shared_ptr<CBlock> PrepareBlock(const CScript& coinbase_scriptPubKey)
40+
{
41+
auto block = std::make_shared<CBlock>(
42+
BlockAssembler{Params()}
43+
.CreateNewBlock(coinbase_scriptPubKey)
44+
->block);
45+
46+
LOCK(cs_main);
47+
block->nTime = ::ChainActive().Tip()->GetMedianTimePast() + 1;
48+
block->hashMerkleRoot = BlockMerkleRoot(*block);
49+
50+
return block;
51+
}

src/test/util/mining.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright (c) 2019 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_MINING_H
6+
#define BITCOIN_TEST_UTIL_MINING_H
7+
8+
#include <memory>
9+
#include <string>
10+
11+
class CBlock;
12+
class CScript;
13+
class CTxIn;
14+
15+
/** Returns the generated coin */
16+
CTxIn MineBlock(const CScript& coinbase_scriptPubKey);
17+
18+
/** Prepare a block to be mined */
19+
std::shared_ptr<CBlock> PrepareBlock(const CScript& coinbase_scriptPubKey);
20+
21+
/** RPC-like helper function, returns the generated coin */
22+
CTxIn generatetoaddress(const std::string& address);
23+
24+
#endif // BITCOIN_TEST_UTIL_MINING_H

0 commit comments

Comments
 (0)