Skip to content

Commit 591c2c8

Browse files
committed
Remove uses of chainActive and mapBlockIndex in wallet code
This commit does not change behavior.
1 parent b545a6e commit 591c2c8

File tree

11 files changed

+445
-190
lines changed

11 files changed

+445
-190
lines changed

src/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ BITCOIN_CORE_H = \
145145
netbase.h \
146146
netmessagemaker.h \
147147
noui.h \
148+
optional.h \
148149
outputtype.h \
149150
policy/feerate.h \
150151
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: 5 additions & 5 deletions
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,13 +147,12 @@ void TestGUI()
146147
auto locked_chain = wallet->chain().lock();
147148
WalletRescanReserver reserver(wallet.get());
148149
reserver.reserve();
149-
const CBlockIndex* const null_block = nullptr;
150-
const CBlockIndex *stop_block, *failed_block;
150+
uint256 stop_block, failed_block;
151151
QCOMPARE(
152-
wallet->ScanForWalletTransactions(chainActive.Genesis(), nullptr, reserver, failed_block, stop_block, true /* fUpdate */),
152+
wallet->ScanForWalletTransactions(locked_chain->getBlockHash(0), {} /* stop block */, reserver, failed_block, stop_block, true /* fUpdate */),
153153
CWallet::ScanResult::SUCCESS);
154-
QCOMPARE(stop_block, chainActive.Tip());
155-
QCOMPARE(failed_block, null_block);
154+
QCOMPARE(stop_block, chainActive.Tip()->GetBlockHash());
155+
QVERIFY(failed_block.IsNull());
156156
}
157157
wallet->SetBroadcastTransactions(true);
158158

0 commit comments

Comments
 (0)