Skip to content

Commit d568bdb

Browse files
committed
Remove uses of chainActive and mapBlockIndex in wallet code
This commit does not change behavior.
1 parent 90c0b6a commit d568bdb

File tree

11 files changed

+429
-174
lines changed

11 files changed

+429
-174
lines changed

src/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ BITCOIN_CORE_H = \
144144
netbase.h \
145145
netmessagemaker.h \
146146
noui.h \
147+
optional.h \
147148
outputtype.h \
148149
policy/feerate.h \
149150
policy/fees.h \

src/interfaces/chain.cpp

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,133 @@
44

55
#include <interfaces/chain.h>
66

7+
#include <chain.h>
8+
#include <chainparams.h>
9+
#include <primitives/block.h>
710
#include <sync.h>
11+
#include <uint256.h>
812
#include <util/system.h>
913
#include <validation.h>
1014

1115
#include <memory>
16+
#include <unordered_map>
1217
#include <utility>
1318

1419
namespace interfaces {
1520
namespace {
1621

1722
class LockImpl : public Chain::Lock
1823
{
24+
Optional<int> getHeight() override
25+
{
26+
int height = ::chainActive.Height();
27+
if (height >= 0) {
28+
return height;
29+
}
30+
return nullopt;
31+
}
32+
Optional<int> getBlockHeight(const uint256& hash) override
33+
{
34+
auto it = ::mapBlockIndex.find(hash);
35+
if (it != ::mapBlockIndex.end() && it->second && ::chainActive.Contains(it->second)) {
36+
return it->second->nHeight;
37+
}
38+
return nullopt;
39+
}
40+
int getBlockDepth(const uint256& hash) override
41+
{
42+
const Optional<int> tip_height = getHeight();
43+
const Optional<int> height = getBlockHeight(hash);
44+
return tip_height && height ? *tip_height - *height + 1 : 0;
45+
}
46+
uint256 getBlockHash(int height) override
47+
{
48+
CBlockIndex* block = ::chainActive[height];
49+
assert(block != nullptr);
50+
return block->GetBlockHash();
51+
}
52+
int64_t getBlockTime(int height) override
53+
{
54+
CBlockIndex* block = ::chainActive[height];
55+
assert(block != nullptr);
56+
return block->GetBlockTime();
57+
}
58+
int64_t getBlockMedianTimePast(int height) override
59+
{
60+
CBlockIndex* block = ::chainActive[height];
61+
assert(block != nullptr);
62+
return block->GetMedianTimePast();
63+
}
64+
bool haveBlockOnDisk(int height) override
65+
{
66+
CBlockIndex* block = ::chainActive[height];
67+
return block && ((block->nStatus & BLOCK_HAVE_DATA) != 0) && block->nTx > 0;
68+
}
69+
Optional<int> findFirstBlockWithTime(int64_t time) override
70+
{
71+
CBlockIndex* block = ::chainActive.FindEarliestAtLeast(time);
72+
if (block) {
73+
return block->nHeight;
74+
}
75+
return nullopt;
76+
}
77+
Optional<int> findFirstBlockWithTimeAndHeight(int64_t time, int height) override
78+
{
79+
for (CBlockIndex* block = ::chainActive[height]; block; block = ::chainActive.Next(block)) {
80+
if (block->GetBlockTime() >= time) {
81+
return block->nHeight;
82+
}
83+
}
84+
return nullopt;
85+
}
86+
Optional<int> findPruned(int start_height, Optional<int> stop_height) override
87+
{
88+
if (::fPruneMode) {
89+
CBlockIndex* block = stop_height ? ::chainActive[*stop_height] : ::chainActive.Tip();
90+
while (block && block->nHeight >= start_height) {
91+
if ((block->nStatus & BLOCK_HAVE_DATA) == 0) {
92+
return block->nHeight;
93+
}
94+
block = block->pprev;
95+
}
96+
}
97+
return nullopt;
98+
}
99+
Optional<int> findFork(const uint256& hash, Optional<int>* height) override
100+
{
101+
const CBlockIndex *block{nullptr}, *fork{nullptr};
102+
auto it = ::mapBlockIndex.find(hash);
103+
if (it != ::mapBlockIndex.end()) {
104+
block = it->second;
105+
fork = ::chainActive.FindFork(block);
106+
}
107+
if (height != nullptr) {
108+
if (block) {
109+
*height = block->nHeight;
110+
} else {
111+
height->reset();
112+
}
113+
}
114+
if (fork) {
115+
return fork->nHeight;
116+
}
117+
return nullopt;
118+
}
119+
bool isPotentialTip(const uint256& hash) override
120+
{
121+
if (::chainActive.Tip()->GetBlockHash() == hash) return true;
122+
auto it = ::mapBlockIndex.find(hash);
123+
return it != ::mapBlockIndex.end() && it->second->GetAncestor(::chainActive.Height()) == ::chainActive.Tip();
124+
}
125+
CBlockLocator getLocator() override { return ::chainActive.GetLocator(); }
126+
Optional<int> findLocatorFork(const CBlockLocator& locator) override
127+
{
128+
LockAnnotation lock(::cs_main);
129+
if (CBlockIndex* fork = FindForkInGlobalIndex(::chainActive, locator)) {
130+
return fork->nHeight;
131+
}
132+
return nullopt;
133+
}
19134
};
20135

21136
class LockingStateImpl : public LockImpl, public UniqueLock<CCriticalSection>
@@ -35,6 +150,34 @@ class ChainImpl : public Chain
35150
return std::move(result);
36151
}
37152
std::unique_ptr<Chain::Lock> assumeLocked() override { return MakeUnique<LockImpl>(); }
153+
bool findBlock(const uint256& hash, CBlock* block, int64_t* time, int64_t* time_max) override
154+
{
155+
CBlockIndex* index;
156+
{
157+
LOCK(cs_main);
158+
auto it = ::mapBlockIndex.find(hash);
159+
if (it == ::mapBlockIndex.end()) {
160+
return false;
161+
}
162+
index = it->second;
163+
if (time) {
164+
*time = index->GetBlockTime();
165+
}
166+
if (time_max) {
167+
*time_max = index->GetBlockTimeMax();
168+
}
169+
}
170+
if (block && !ReadBlockFromDisk(*block, index, Params().GetConsensus())) {
171+
block->SetNull();
172+
}
173+
return true;
174+
}
175+
double guessVerificationProgress(const uint256& block_hash) override
176+
{
177+
LOCK(cs_main);
178+
auto it = ::mapBlockIndex.find(block_hash);
179+
return GuessVerificationProgress(Params().TxData(), it != ::mapBlockIndex.end() ? it->second : nullptr);
180+
}
38181
};
39182

40183
} // namespace

src/interfaces/chain.h

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,17 @@
55
#ifndef BITCOIN_INTERFACES_CHAIN_H
66
#define BITCOIN_INTERFACES_CHAIN_H
77

8+
#include <optional.h>
9+
810
#include <memory>
11+
#include <stdint.h>
912
#include <string>
1013
#include <vector>
1114

15+
class CBlock;
1216
class CScheduler;
17+
class uint256;
18+
struct CBlockLocator;
1319

1420
namespace interfaces {
1521

@@ -28,6 +34,67 @@ class Chain
2834
{
2935
public:
3036
virtual ~Lock() {}
37+
38+
//! Get current chain height, not including genesis block (returns 0 if
39+
//! chain only contains genesis block, nothing if chain does not contain
40+
//! any blocks).
41+
virtual Optional<int> getHeight() = 0;
42+
43+
//! Get block height above genesis block. Returns 0 for genesis block,
44+
//! 1 for following block, and so on. Returns nothing for a block not
45+
//! included in the current chain.
46+
virtual Optional<int> getBlockHeight(const uint256& hash) = 0;
47+
48+
//! Get block depth. Returns 1 for chain tip, 2 for preceding block, and
49+
//! so on. Returns 0 for a block not included in the current chain.
50+
virtual int getBlockDepth(const uint256& hash) = 0;
51+
52+
//! Get block hash.
53+
virtual uint256 getBlockHash(int height) = 0;
54+
55+
//! Get block time.
56+
virtual int64_t getBlockTime(int height) = 0;
57+
58+
//! Get block median time past.
59+
virtual int64_t getBlockMedianTimePast(int height) = 0;
60+
61+
//! Check that the full block is available on disk (ie has not been
62+
//! pruned), and contains transactions.
63+
virtual bool haveBlockOnDisk(int height) = 0;
64+
65+
//! Return height of the first block in the chain with timestamp equal
66+
//! or greater than the given time, or nothing if there is no block with
67+
//! a high enough timestamp.
68+
virtual Optional<int> findFirstBlockWithTime(int64_t time) = 0;
69+
70+
//! Return height of the first block in the chain with timestamp equal
71+
//! or greater than the given time and height equal or greater than the
72+
//! given height, or nothing if there is no such block.
73+
//!
74+
//! Calling this with height 0 is equivalent to calling
75+
//! findFirstBlockWithTime, but less efficient because it requires a
76+
//! linear instead of a binary search.
77+
virtual Optional<int> findFirstBlockWithTimeAndHeight(int64_t time, int height) = 0;
78+
79+
//! Return height of last block in the specified range which is pruned, or
80+
//! nothing if no block in the range is pruned. Range is inclusive.
81+
virtual Optional<int> findPruned(int start_height = 0, Optional<int> stop_height = nullopt) = 0;
82+
83+
//! Return height of the highest block on the chain that is an ancestor
84+
//! of the specified block. Also return the height of the specified
85+
//! block as an optional output parameter.
86+
virtual Optional<int> findFork(const uint256& hash, Optional<int>* height) = 0;
87+
88+
//! Return true if block hash points to the current chain tip, or to a
89+
//! possible descendant of the current chain tip that isn't currently
90+
//! connected.
91+
virtual bool isPotentialTip(const uint256& hash) = 0;
92+
93+
//! Get locator for the current chain tip.
94+
virtual CBlockLocator getLocator() = 0;
95+
96+
//! Return height of block on the chain using locator.
97+
virtual Optional<int> findLocatorFork(const CBlockLocator& locator) = 0;
3198
};
3299

33100
//! Return Lock interface. Chain is locked when this is called, and
@@ -38,6 +105,21 @@ class Chain
38105
//! method is temporary and is only used in a few places to avoid changing
39106
//! behavior while code is transitioned to use the Chain::Lock interface.
40107
virtual std::unique_ptr<Lock> assumeLocked() = 0;
108+
109+
//! Return whether node has the block and optionally return block metadata
110+
//! or contents.
111+
//!
112+
//! If a block pointer is provided to retrieve the block contents, and the
113+
//! block exists but doesn't have data (for example due to pruning), the
114+
//! block will be empty and all fields set to null.
115+
virtual bool findBlock(const uint256& hash,
116+
CBlock* block = nullptr,
117+
int64_t* time = nullptr,
118+
int64_t* max_time = nullptr) = 0;
119+
120+
//! Estimate fraction of total transactions verified if blocks up to
121+
//! given height are verified.
122+
virtual double guessVerificationProgress(const uint256& block_hash) = 0;
41123
};
42124

43125
//! Interface to let node manage chain clients (wallets, or maybe tools for

src/interfaces/wallet.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ class WalletImpl : public Wallet
332332
if (mi == m_wallet.mapWallet.end()) {
333333
return false;
334334
}
335-
num_blocks = ::chainActive.Height();
335+
num_blocks = locked_chain->getHeight().value_or(-1);
336336
tx_status = MakeWalletTxStatus(*locked_chain, mi->second);
337337
return true;
338338
}
@@ -346,7 +346,7 @@ class WalletImpl : public Wallet
346346
LOCK(m_wallet.cs_wallet);
347347
auto mi = m_wallet.mapWallet.find(txid);
348348
if (mi != m_wallet.mapWallet.end()) {
349-
num_blocks = ::chainActive.Height();
349+
num_blocks = locked_chain->getHeight().value_or(-1);
350350
in_mempool = mi->second.InMempool();
351351
order_form = mi->second.vOrderForm;
352352
tx_status = MakeWalletTxStatus(*locked_chain, mi->second);
@@ -377,7 +377,7 @@ class WalletImpl : public Wallet
377377
return false;
378378
}
379379
balances = getBalances();
380-
num_blocks = ::chainActive.Height();
380+
num_blocks = locked_chain->getHeight().value_or(-1);
381381
return true;
382382
}
383383
CAmount getBalance() override { return m_wallet.GetBalance(); }

src/optional.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright (c) 2017 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_OPTIONAL_H
6+
#define BITCOIN_OPTIONAL_H
7+
8+
#include <boost/optional.hpp>
9+
10+
//! Substitute for C++17 std::optional
11+
template <typename T>
12+
using Optional = boost::optional<T>;
13+
14+
//! Substitute for C++17 std::nullopt
15+
static auto& nullopt = boost::none;
16+
17+
#endif // BITCOIN_OPTIONAL_H

src/qt/test/wallettests.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <qt/test/wallettests.h>
22
#include <qt/test/util.h>
33

4+
#include <init.h>
45
#include <interfaces/chain.h>
56
#include <interfaces/node.h>
67
#include <base58.h>
@@ -146,7 +147,7 @@ void TestGUI()
146147
auto locked_chain = wallet->chain().lock();
147148
WalletRescanReserver reserver(wallet.get());
148149
reserver.reserve();
149-
wallet->ScanForWalletTransactions(chainActive.Genesis(), nullptr, reserver, true);
150+
wallet->ScanForWalletTransactions(locked_chain->getBlockHash(0), {} /* stop block */, reserver, true /* update */);
150151
}
151152
wallet->SetBroadcastTransactions(true);
152153

src/wallet/rpcdump.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -366,8 +366,7 @@ UniValue importprunedfunds(const JSONRPCRequest& request)
366366
if (merkleBlock.txn.ExtractMatches(vMatch, vIndex) == merkleBlock.header.hashMerkleRoot) {
367367

368368
auto locked_chain = pwallet->chain().lock();
369-
const CBlockIndex* pindex = LookupBlockIndex(merkleBlock.header.GetHash());
370-
if (!pindex || !chainActive.Contains(pindex)) {
369+
if (!locked_chain->getBlockHeight(merkleBlock.header.GetHash())) {
371370
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found in chain");
372371
}
373372

@@ -551,7 +550,8 @@ UniValue importwallet(const JSONRPCRequest& request)
551550
if (!file.is_open()) {
552551
throw JSONRPCError(RPC_INVALID_PARAMETER, "Cannot open wallet dump file");
553552
}
554-
nTimeBegin = chainActive.Tip()->GetBlockTime();
553+
Optional<int> tip_height = locked_chain->getHeight();
554+
nTimeBegin = tip_height ? locked_chain->getBlockTime(*tip_height) : 0;
555555

556556
int64_t nFilesize = std::max((int64_t)1, (int64_t)file.tellg());
557557
file.seekg(0, file.beg);
@@ -750,8 +750,9 @@ UniValue dumpwallet(const JSONRPCRequest& request)
750750
// produce output
751751
file << strprintf("# Wallet dump created by Bitcoin %s\n", CLIENT_BUILD);
752752
file << strprintf("# * Created on %s\n", FormatISO8601DateTime(GetTime()));
753-
file << strprintf("# * Best block at time of backup was %i (%s),\n", chainActive.Height(), chainActive.Tip()->GetBlockHash().ToString());
754-
file << strprintf("# mined on %s\n", FormatISO8601DateTime(chainActive.Tip()->GetBlockTime()));
753+
const Optional<int> tip_height = locked_chain->getHeight();
754+
file << strprintf("# * Best block at time of backup was %i (%s),\n", tip_height.value_or(-1), tip_height ? locked_chain->getBlockHash(*tip_height).ToString() : "(missing block hash)");
755+
file << strprintf("# mined on %s\n", tip_height ? FormatISO8601DateTime(locked_chain->getBlockTime(*tip_height)) : "(missing block time)");
755756
file << "\n";
756757

757758
// add the base58check encoded extended master if the wallet uses HD
@@ -1148,15 +1149,16 @@ UniValue importmulti(const JSONRPCRequest& mainRequest)
11481149
EnsureWalletIsUnlocked(pwallet);
11491150

11501151
// Verify all timestamps are present before importing any keys.
1151-
now = chainActive.Tip() ? chainActive.Tip()->GetMedianTimePast() : 0;
1152+
const Optional<int> tip_height = locked_chain->getHeight();
1153+
now = tip_height ? locked_chain->getBlockMedianTimePast(*tip_height) : 0;
11521154
for (const UniValue& data : requests.getValues()) {
11531155
GetImportTimestamp(data, now);
11541156
}
11551157

11561158
const int64_t minimumTimestamp = 1;
11571159

1158-
if (fRescan && chainActive.Tip()) {
1159-
nLowestTimestamp = chainActive.Tip()->GetBlockTime();
1160+
if (fRescan && tip_height) {
1161+
nLowestTimestamp = locked_chain->getBlockTime(*tip_height);
11601162
} else {
11611163
fRescan = false;
11621164
}

0 commit comments

Comments
 (0)