Skip to content

Commit 8411788

Browse files
author
Antoine Riard
committed
[wallet] Move methods from Chain::Lock interface to simple Chain
Remove findPruned and findFork, no more used after 17954.
1 parent 0a76287 commit 8411788

File tree

7 files changed

+97
-123
lines changed

7 files changed

+97
-123
lines changed

src/interfaces/chain.cpp

Lines changed: 34 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -55,58 +55,6 @@ bool FillBlock(const CBlockIndex* index, const FoundBlock& block, UniqueLock<Rec
5555

5656
class LockImpl : public Chain::Lock, public UniqueLock<RecursiveMutex>
5757
{
58-
bool haveBlockOnDisk(int height) override
59-
{
60-
LockAssertion lock(::cs_main);
61-
CBlockIndex* block = ::ChainActive()[height];
62-
return block && ((block->nStatus & BLOCK_HAVE_DATA) != 0) && block->nTx > 0;
63-
}
64-
Optional<int> findFirstBlockWithTimeAndHeight(int64_t time, int height, uint256* hash) override
65-
{
66-
LockAssertion lock(::cs_main);
67-
CBlockIndex* block = ::ChainActive().FindEarliestAtLeast(time, height);
68-
if (block) {
69-
if (hash) *hash = block->GetBlockHash();
70-
return block->nHeight;
71-
}
72-
return nullopt;
73-
}
74-
Optional<int> findFork(const uint256& hash, Optional<int>* height) override
75-
{
76-
LockAssertion lock(::cs_main);
77-
const CBlockIndex* block = LookupBlockIndex(hash);
78-
const CBlockIndex* fork = block ? ::ChainActive().FindFork(block) : nullptr;
79-
if (height) {
80-
if (block) {
81-
*height = block->nHeight;
82-
} else {
83-
height->reset();
84-
}
85-
}
86-
if (fork) {
87-
return fork->nHeight;
88-
}
89-
return nullopt;
90-
}
91-
CBlockLocator getTipLocator() override
92-
{
93-
LockAssertion lock(::cs_main);
94-
return ::ChainActive().GetLocator();
95-
}
96-
Optional<int> findLocatorFork(const CBlockLocator& locator) override
97-
{
98-
LockAssertion lock(::cs_main);
99-
if (CBlockIndex* fork = FindForkInGlobalIndex(::ChainActive(), locator)) {
100-
return fork->nHeight;
101-
}
102-
return nullopt;
103-
}
104-
bool checkFinalTx(const CTransaction& tx) override
105-
{
106-
LockAssertion lock(::cs_main);
107-
return CheckFinalTx(tx);
108-
}
109-
11058
using UniqueLock::UniqueLock;
11159
};
11260

@@ -234,6 +182,40 @@ class ChainImpl : public Chain
234182
assert(block);
235183
return block->GetBlockHash();
236184
}
185+
bool haveBlockOnDisk(int height) override
186+
{
187+
LOCK(cs_main);
188+
CBlockIndex* block = ::ChainActive()[height];
189+
return block && ((block->nStatus & BLOCK_HAVE_DATA) != 0) && block->nTx > 0;
190+
}
191+
Optional<int> findFirstBlockWithTimeAndHeight(int64_t time, int height, uint256* hash) override
192+
{
193+
LOCK(cs_main);
194+
CBlockIndex* block = ::ChainActive().FindEarliestAtLeast(time, height);
195+
if (block) {
196+
if (hash) *hash = block->GetBlockHash();
197+
return block->nHeight;
198+
}
199+
return nullopt;
200+
}
201+
CBlockLocator getTipLocator() override
202+
{
203+
LOCK(cs_main);
204+
return ::ChainActive().GetLocator();
205+
}
206+
bool checkFinalTx(const CTransaction& tx) override
207+
{
208+
LOCK(cs_main);
209+
return CheckFinalTx(tx);
210+
}
211+
Optional<int> findLocatorFork(const CBlockLocator& locator) override
212+
{
213+
LOCK(cs_main);
214+
if (CBlockIndex* fork = FindForkInGlobalIndex(::ChainActive(), locator)) {
215+
return fork->nHeight;
216+
}
217+
return nullopt;
218+
}
237219
bool findBlock(const uint256& hash, const FoundBlock& block) override
238220
{
239221
WAIT_LOCK(cs_main, lock);

src/interfaces/chain.h

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -86,36 +86,6 @@ class Chain
8686
{
8787
public:
8888
virtual ~Lock() {}
89-
90-
//! Check that the block is available on disk (i.e. has not been
91-
//! pruned), and contains transactions.
92-
virtual bool haveBlockOnDisk(int height) = 0;
93-
94-
//! Return height of the first block in the chain with timestamp equal
95-
//! or greater than the given time and height equal or greater than the
96-
//! given height, or nullopt if there is no block with a high enough
97-
//! timestamp and height. Also return the block hash as an optional output parameter
98-
//! (to avoid the cost of a second lookup in case this information is needed.)
99-
virtual Optional<int> findFirstBlockWithTimeAndHeight(int64_t time, int height, uint256* hash) = 0;
100-
101-
//! Return height of the specified block if it is on the chain, otherwise
102-
//! return the height of the highest block on chain that's an ancestor
103-
//! of the specified block, or nullopt if there is no common ancestor.
104-
//! Also return the height of the specified block as an optional output
105-
//! parameter (to avoid the cost of a second hash lookup in case this
106-
//! information is desired).
107-
virtual Optional<int> findFork(const uint256& hash, Optional<int>* height) = 0;
108-
109-
//! Get locator for the current chain tip.
110-
virtual CBlockLocator getTipLocator() = 0;
111-
112-
//! Return height of the highest block on chain in common with the locator,
113-
//! which will either be the original block used to create the locator,
114-
//! or one of its ancestors.
115-
virtual Optional<int> findLocatorFork(const CBlockLocator& locator) = 0;
116-
117-
//! Check if transaction will be final given chain height current time.
118-
virtual bool checkFinalTx(const CTransaction& tx) = 0;
11989
};
12090

12191
//! Return Lock interface. Chain is locked when this is called, and
@@ -135,6 +105,28 @@ class Chain
135105
//! Get block hash. Height must be valid or this function will abort.
136106
virtual uint256 getBlockHash(int height) = 0;
137107

108+
//! Check that the block is available on disk (i.e. has not been
109+
//! pruned), and contains transactions.
110+
virtual bool haveBlockOnDisk(int height) = 0;
111+
112+
//! Return height of the first block in the chain with timestamp equal
113+
//! or greater than the given time and height equal or greater than the
114+
//! given height, or nullopt if there is no block with a high enough
115+
//! timestamp and height. Also return the block hash as an optional output parameter
116+
//! (to avoid the cost of a second lookup in case this information is needed.)
117+
virtual Optional<int> findFirstBlockWithTimeAndHeight(int64_t time, int height, uint256* hash) = 0;
118+
119+
//! Get locator for the current chain tip.
120+
virtual CBlockLocator getTipLocator() = 0;
121+
122+
//! Return height of the highest block on chain in common with the locator,
123+
//! which will either be the original block used to create the locator,
124+
//! or one of its ancestors.
125+
virtual Optional<int> findLocatorFork(const CBlockLocator& locator) = 0;
126+
127+
//! Check if transaction will be final given chain height current time.
128+
virtual bool checkFinalTx(const CTransaction& tx) = 0;
129+
138130
//! Return whether node has the block and optionally return block metadata
139131
//! or contents.
140132
virtual bool findBlock(const uint256& hash, const FoundBlock& block={}) = 0;

src/interfaces/wallet.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,16 @@ WalletTx MakeWalletTx(CWallet& wallet, const CWalletTx& wtx)
6060
}
6161

6262
//! Construct wallet tx status struct.
63-
WalletTxStatus MakeWalletTxStatus(interfaces::Chain::Lock& locked_chain, const CWalletTx& wtx)
63+
WalletTxStatus MakeWalletTxStatus(CWallet& wallet, const CWalletTx& wtx)
6464
{
6565
WalletTxStatus result;
6666
result.block_height = wtx.m_confirm.block_height > 0 ? wtx.m_confirm.block_height : std::numeric_limits<int>::max();
6767
result.blocks_to_maturity = wtx.GetBlocksToMaturity();
6868
result.depth_in_main_chain = wtx.GetDepthInMainChain();
6969
result.time_received = wtx.nTimeReceived;
7070
result.lock_time = wtx.tx->nLockTime;
71-
result.is_final = locked_chain.checkFinalTx(*wtx.tx);
72-
result.is_trusted = wtx.IsTrusted(locked_chain);
71+
result.is_final = wallet.chain().checkFinalTx(*wtx.tx);
72+
result.is_trusted = wtx.IsTrusted();
7373
result.is_abandoned = wtx.isAbandoned();
7474
result.is_coinbase = wtx.IsCoinBase();
7575
result.is_in_main_chain = wtx.IsInMainChain();
@@ -322,7 +322,7 @@ class WalletImpl : public Wallet
322322
num_blocks = m_wallet->GetLastBlockHeight();
323323
block_time = -1;
324324
CHECK_NONFATAL(m_wallet->chain().findBlock(m_wallet->GetLastBlockHash(), FoundBlock().time(block_time)));
325-
tx_status = MakeWalletTxStatus(*locked_chain, mi->second);
325+
tx_status = MakeWalletTxStatus(*m_wallet, mi->second);
326326
return true;
327327
}
328328
WalletTx getWalletTxDetails(const uint256& txid,
@@ -338,7 +338,7 @@ class WalletImpl : public Wallet
338338
num_blocks = m_wallet->GetLastBlockHeight();
339339
in_mempool = mi->second.InMempool();
340340
order_form = mi->second.vOrderForm;
341-
tx_status = MakeWalletTxStatus(*locked_chain, mi->second);
341+
tx_status = MakeWalletTxStatus(*m_wallet, mi->second);
342342
return MakeWalletTx(*m_wallet, mi->second);
343343
}
344344
return {};
@@ -413,7 +413,7 @@ class WalletImpl : public Wallet
413413
auto locked_chain = m_wallet->chain().lock();
414414
LOCK(m_wallet->cs_wallet);
415415
CoinsList result;
416-
for (const auto& entry : m_wallet->ListCoins(*locked_chain)) {
416+
for (const auto& entry : m_wallet->ListCoins()) {
417417
auto& group = result[entry.first];
418418
for (const auto& coin : entry.second) {
419419
group.emplace_back(COutPoint(coin.tx->GetHash(), coin.i),

src/wallet/rpcwallet.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ static void WalletTxToJSON(interfaces::Chain& chain, interfaces::Chain::Lock& lo
148148
CHECK_NONFATAL(chain.findBlock(wtx.m_confirm.hashBlock, FoundBlock().time(block_time)));
149149
entry.pushKV("blocktime", block_time);
150150
} else {
151-
entry.pushKV("trusted", wtx.IsTrusted(locked_chain));
151+
entry.pushKV("trusted", wtx.IsTrusted());
152152
}
153153
uint256 hash = wtx.GetHash();
154154
entry.pushKV("txid", hash.GetHex());
@@ -572,7 +572,7 @@ static UniValue signmessage(const JSONRPCRequest& request)
572572
return signature;
573573
}
574574

575-
static CAmount GetReceived(interfaces::Chain::Lock& locked_chain, const CWallet& wallet, const UniValue& params, bool by_label) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet)
575+
static CAmount GetReceived(const CWallet& wallet, const UniValue& params, bool by_label) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet)
576576
{
577577
std::set<CTxDestination> address_set;
578578

@@ -602,7 +602,7 @@ static CAmount GetReceived(interfaces::Chain::Lock& locked_chain, const CWallet&
602602
CAmount amount = 0;
603603
for (const std::pair<const uint256, CWalletTx>& wtx_pair : wallet.mapWallet) {
604604
const CWalletTx& wtx = wtx_pair.second;
605-
if (wtx.IsCoinBase() || !locked_chain.checkFinalTx(*wtx.tx) || wtx.GetDepthInMainChain() < min_depth) {
605+
if (wtx.IsCoinBase() || !wallet.chain().checkFinalTx(*wtx.tx) || wtx.GetDepthInMainChain() < min_depth) {
606606
continue;
607607
}
608608

@@ -655,7 +655,7 @@ static UniValue getreceivedbyaddress(const JSONRPCRequest& request)
655655
auto locked_chain = pwallet->chain().lock();
656656
LOCK(pwallet->cs_wallet);
657657

658-
return ValueFromAmount(GetReceived(*locked_chain, *pwallet, request.params, /* by_label */ false));
658+
return ValueFromAmount(GetReceived(*pwallet, request.params, /* by_label */ false));
659659
}
660660

661661

@@ -696,7 +696,7 @@ static UniValue getreceivedbylabel(const JSONRPCRequest& request)
696696
auto locked_chain = pwallet->chain().lock();
697697
LOCK(pwallet->cs_wallet);
698698

699-
return ValueFromAmount(GetReceived(*locked_chain, *pwallet, request.params, /* by_label */ true));
699+
return ValueFromAmount(GetReceived(*pwallet, request.params, /* by_label */ true));
700700
}
701701

702702

@@ -1049,7 +1049,7 @@ static UniValue ListReceived(interfaces::Chain::Lock& locked_chain, const CWalle
10491049
for (const std::pair<const uint256, CWalletTx>& pairWtx : pwallet->mapWallet) {
10501050
const CWalletTx& wtx = pairWtx.second;
10511051

1052-
if (wtx.IsCoinBase() || !locked_chain.checkFinalTx(*wtx.tx)) {
1052+
if (wtx.IsCoinBase() || !pwallet->chain().checkFinalTx(*wtx.tx)) {
10531053
continue;
10541054
}
10551055

@@ -2942,7 +2942,7 @@ static UniValue listunspent(const JSONRPCRequest& request)
29422942
cctl.m_max_depth = nMaxDepth;
29432943
auto locked_chain = pwallet->chain().lock();
29442944
LOCK(pwallet->cs_wallet);
2945-
pwallet->AvailableCoins(*locked_chain, vecOutputs, !include_unsafe, &cctl, nMinimumAmount, nMaximumAmount, nMinimumSumAmount, nMaximumCount);
2945+
pwallet->AvailableCoins(vecOutputs, !include_unsafe, &cctl, nMinimumAmount, nMaximumAmount, nMinimumSumAmount, nMaximumCount);
29462946
}
29472947

29482948
LOCK(pwallet->cs_wallet);

src/wallet/test/wallet_tests.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ BOOST_FIXTURE_TEST_CASE(ListCoins, ListCoinsTestingSetup)
556556
{
557557
auto locked_chain = m_chain->lock();
558558
LOCK(wallet->cs_wallet);
559-
list = wallet->ListCoins(*locked_chain);
559+
list = wallet->ListCoins();
560560
}
561561
BOOST_CHECK_EQUAL(list.size(), 1U);
562562
BOOST_CHECK_EQUAL(boost::get<PKHash>(list.begin()->first).ToString(), coinbaseAddress);
@@ -573,7 +573,7 @@ BOOST_FIXTURE_TEST_CASE(ListCoins, ListCoinsTestingSetup)
573573
{
574574
auto locked_chain = m_chain->lock();
575575
LOCK(wallet->cs_wallet);
576-
list = wallet->ListCoins(*locked_chain);
576+
list = wallet->ListCoins();
577577
}
578578
BOOST_CHECK_EQUAL(list.size(), 1U);
579579
BOOST_CHECK_EQUAL(boost::get<PKHash>(list.begin()->first).ToString(), coinbaseAddress);
@@ -584,7 +584,7 @@ BOOST_FIXTURE_TEST_CASE(ListCoins, ListCoinsTestingSetup)
584584
auto locked_chain = m_chain->lock();
585585
LOCK(wallet->cs_wallet);
586586
std::vector<COutput> available;
587-
wallet->AvailableCoins(*locked_chain, available);
587+
wallet->AvailableCoins(available);
588588
BOOST_CHECK_EQUAL(available.size(), 2U);
589589
}
590590
for (const auto& group : list) {
@@ -597,15 +597,15 @@ BOOST_FIXTURE_TEST_CASE(ListCoins, ListCoinsTestingSetup)
597597
auto locked_chain = m_chain->lock();
598598
LOCK(wallet->cs_wallet);
599599
std::vector<COutput> available;
600-
wallet->AvailableCoins(*locked_chain, available);
600+
wallet->AvailableCoins(available);
601601
BOOST_CHECK_EQUAL(available.size(), 0U);
602602
}
603603
// Confirm ListCoins still returns same result as before, despite coins
604604
// being locked.
605605
{
606606
auto locked_chain = m_chain->lock();
607607
LOCK(wallet->cs_wallet);
608-
list = wallet->ListCoins(*locked_chain);
608+
list = wallet->ListCoins();
609609
}
610610
BOOST_CHECK_EQUAL(list.size(), 1U);
611611
BOOST_CHECK_EQUAL(boost::get<PKHash>(list.begin()->first).ToString(), coinbaseAddress);

0 commit comments

Comments
 (0)