Skip to content

Commit de13363

Browse files
author
Antoine Riard
committed
[wallet] Move getBlockHeight from Chain::Lock interface to simple Chain
Add HaveChain to assert chain access for wallet-tool in LoadToWallet.
1 parent b855592 commit de13363

File tree

4 files changed

+20
-18
lines changed

4 files changed

+20
-18
lines changed

src/interfaces/chain.cpp

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

5656
class LockImpl : public Chain::Lock, public UniqueLock<RecursiveMutex>
5757
{
58-
Optional<int> getBlockHeight(const uint256& hash) override
59-
{
60-
LockAssertion lock(::cs_main);
61-
CBlockIndex* block = LookupBlockIndex(hash);
62-
if (block && ::ChainActive().Contains(block)) {
63-
return block->nHeight;
64-
}
65-
return nullopt;
66-
}
6758
uint256 getBlockHash(int height) override
6859
{
6960
LockAssertion lock(::cs_main);
@@ -234,6 +225,15 @@ class ChainImpl : public Chain
234225
}
235226
return nullopt;
236227
}
228+
Optional<int> getBlockHeight(const uint256& hash) override
229+
{
230+
LOCK(::cs_main);
231+
CBlockIndex* block = LookupBlockIndex(hash);
232+
if (block && ::ChainActive().Contains(block)) {
233+
return block->nHeight;
234+
}
235+
return nullopt;
236+
}
237237
bool findBlock(const uint256& hash, const FoundBlock& block) override
238238
{
239239
WAIT_LOCK(cs_main, lock);

src/interfaces/chain.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,6 @@ class Chain
8787
public:
8888
virtual ~Lock() {}
8989

90-
//! Get block height above genesis block. Returns 0 for genesis block,
91-
//! 1 for following block, and so on. Returns nullopt for a block not
92-
//! included in the current chain.
93-
virtual Optional<int> getBlockHeight(const uint256& hash) = 0;
94-
9590
//! Get block hash. Height must be valid or this function will abort.
9691
virtual uint256 getBlockHash(int height) = 0;
9792

@@ -135,6 +130,11 @@ class Chain
135130
//! any blocks)
136131
virtual Optional<int> getHeight() = 0;
137132

133+
//! Get block height above genesis block. Returns 0 for genesis block,
134+
//! 1 for following block, and so on. Returns nullopt for a block not
135+
//! included in the current chain.
136+
virtual Optional<int> getBlockHeight(const uint256& hash) = 0;
137+
138138
//! Return whether node has the block and optionally return block metadata
139139
//! or contents.
140140
virtual bool findBlock(const uint256& hash, const FoundBlock& block={}) = 0;

src/wallet/wallet.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -885,10 +885,9 @@ bool CWallet::AddToWallet(const CWalletTx& wtxIn, bool fFlushOnClose)
885885

886886
void CWallet::LoadToWallet(CWalletTx& wtxIn)
887887
{
888-
// If wallet doesn't have a chain (e.g bitcoin-wallet), lock can't be taken.
889-
auto locked_chain = LockChain();
890-
if (locked_chain) {
891-
Optional<int> block_height = locked_chain->getBlockHeight(wtxIn.m_confirm.hashBlock);
888+
// If wallet doesn't have a chain (e.g wallet-tool), don't bother to update txn.
889+
if (HaveChain()) {
890+
Optional<int> block_height = chain().getBlockHeight(wtxIn.m_confirm.hashBlock);
892891
if (block_height) {
893892
// Update cached block height variable since it not stored in the
894893
// serialized transaction.

src/wallet/wallet.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -778,6 +778,9 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
778778
/** Interface to assert chain access and if successful lock it */
779779
std::unique_ptr<interfaces::Chain::Lock> LockChain() { return m_chain ? m_chain->lock() : nullptr; }
780780

781+
/** Interface to assert chain access */
782+
bool HaveChain() const { return m_chain ? true : false; }
783+
781784
std::map<uint256, CWalletTx> mapWallet GUARDED_BY(cs_wallet);
782785

783786
typedef std::multimap<int64_t, CWalletTx*> TxItems;

0 commit comments

Comments
 (0)