Skip to content

Commit 99ab3a7

Browse files
committed
Merge #15931: Remove GetDepthInMainChain dependency on locked chain interface
36b68de Remove getBlockDepth method from Chain::interface (Antoine Riard) b66c429 Remove locked_chain from GetDepthInMainChain and its callers (Antoine Riard) 0ff0387 Use CWallet::m_last_block_processed_height in GetDepthInMainChain (Antoine Riard) f77b1de Only return early from BlockUntilSyncedToCurrentChain if current tip is exact match (Antoine Riard) 769ff05 Refactor some importprunedfunds checks with guard clause (Antoine Riard) 5971d38 Add block_height field in struct Confirmation (Antoine Riard) 9700fcb Replace CWalletTx::SetConf by Confirmation initialization list (Antoine Riard) 5aacc3e Add m_last_block_processed_height field in CWallet (Antoine Riard) 10b4729 Pass block height in Chain::BlockConnected/Chain::BlockDisconnected (Antoine Riard) Pull request description: Work starter to remove Chain::Lock interface by adding m_last_block_processed_height in CWallet and m_block_height in CMerkleTx to avoid GetDepthInMainChain having to keep a lock . Once this one done, it should ease work to wipe out more cs_main locks from wallet code. I think it's ready for a first round of review before to get further. - `BlockUntilSyncedToCurrent` : restrain isPotentialTip to isTip because we want to be sure that wallet see BlockDisconnected callbacks if its height differs from the Chain one. It means during a reorg, an RPC could return before the BlockDisconnected callback had been triggered. This could cause a tx that had been included in the disconnected block to be displayed as confirmed, for example. ~~- `AbandonTransaction` : in case of conflicted tx (nIndex = -1), we set its m_block_height to the one of conflicting blocks, but if this height is superior to CWallet::m_last_block_processed_height, that means tx isn't conflicted anymore so we return 0 as tx is again unconfirmed~~ After #16624, we instead rely on Confirmation. ~~- `AddToWalletIfInvolvingMe`: in case of block disconnected, transactions are added to mempool again, so we need to replace old txn in `mapWallet` with a height set to zero so we remove check on block_hash.IsNull~~ Already done in #16624 ACKs for top commit: jnewbery: @jkczyz you've ACKed an intermediate commit (github annoyingly orders commits in date order, not commit order). Did you mean to ACK the final commit in this branch (36b68de). jkczyz: > @jkczyz you've ACKed an intermediate commit (github annoyingly orders commits in date order, not commit order). Did you mean to ACK the final commit in this branch ([36b68de](bitcoin/bitcoin@36b68de)). meshcollider: utACK 36b68de ryanofsky: Code review ACK 36b68de. Changes since last review: new jkczyz refactor importprunedfunds commit, changed BlockUntilSyncedToCurrentChainChanges commit title and description, changed Confirmation struct field order and line-wrapped comment jnewbery: utACK 36b68de promag: Code review ACK 36b68de. Tree-SHA512: 08b89a0bcc39f67c82a6cb6aee195e6a11697770c788ba737b90986b4893f44e90d1ab9ef87239ea3766508b7e24ea882b7199df41173ab27a3d000328c14644
2 parents 2fb6140 + 36b68de commit 99ab3a7

16 files changed

+254
-201
lines changed

src/interfaces/chain.cpp

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,6 @@ class LockImpl : public Chain::Lock, public UniqueLock<CCriticalSection>
5858
}
5959
return nullopt;
6060
}
61-
int getBlockDepth(const uint256& hash) override
62-
{
63-
const Optional<int> tip_height = getHeight();
64-
const Optional<int> height = getBlockHeight(hash);
65-
return tip_height && height ? *tip_height - *height + 1 : 0;
66-
}
6761
uint256 getBlockHash(int height) override
6862
{
6963
LockAssertion lock(::cs_main);
@@ -182,11 +176,11 @@ class NotificationsHandlerImpl : public Handler, CValidationInterface
182176
const CBlockIndex* index,
183177
const std::vector<CTransactionRef>& tx_conflicted) override
184178
{
185-
m_notifications->BlockConnected(*block, tx_conflicted);
179+
m_notifications->BlockConnected(*block, tx_conflicted, index->nHeight);
186180
}
187-
void BlockDisconnected(const std::shared_ptr<const CBlock>& block) override
181+
void BlockDisconnected(const std::shared_ptr<const CBlock>& block, const CBlockIndex* index) override
188182
{
189-
m_notifications->BlockDisconnected(*block);
183+
m_notifications->BlockDisconnected(*block, index->nHeight);
190184
}
191185
void UpdatedBlockTip(const CBlockIndex* index, const CBlockIndex* fork_index, bool is_ibd) override
192186
{
@@ -353,13 +347,11 @@ class ChainImpl : public Chain
353347
{
354348
return MakeUnique<NotificationsHandlerImpl>(*this, notifications);
355349
}
356-
void waitForNotificationsIfNewBlocksConnected(const uint256& old_tip) override
350+
void waitForNotificationsIfTipChanged(const uint256& old_tip) override
357351
{
358352
if (!old_tip.IsNull()) {
359353
LOCK(::cs_main);
360354
if (old_tip == ::ChainActive().Tip()->GetBlockHash()) return;
361-
CBlockIndex* block = LookupBlockIndex(old_tip);
362-
if (block && block->GetAncestor(::ChainActive().Height()) == ::ChainActive().Tip()) return;
363355
}
364356
SyncWithValidationInterfaceQueue();
365357
}

src/interfaces/chain.h

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,6 @@ class Chain
7676
//! included in the current chain.
7777
virtual Optional<int> getBlockHeight(const uint256& hash) = 0;
7878

79-
//! Get block depth. Returns 1 for chain tip, 2 for preceding block, and
80-
//! so on. Returns 0 for a block not included in the current chain.
81-
virtual int getBlockDepth(const uint256& hash) = 0;
82-
8379
//! Get block hash. Height must be valid or this function will abort.
8480
virtual uint256 getBlockHash(int height) = 0;
8581

@@ -226,8 +222,8 @@ class Chain
226222
virtual ~Notifications() {}
227223
virtual void TransactionAddedToMempool(const CTransactionRef& tx) {}
228224
virtual void TransactionRemovedFromMempool(const CTransactionRef& ptx) {}
229-
virtual void BlockConnected(const CBlock& block, const std::vector<CTransactionRef>& tx_conflicted) {}
230-
virtual void BlockDisconnected(const CBlock& block) {}
225+
virtual void BlockConnected(const CBlock& block, const std::vector<CTransactionRef>& tx_conflicted, int height) {}
226+
virtual void BlockDisconnected(const CBlock& block, int height) {}
231227
virtual void UpdatedBlockTip() {}
232228
virtual void ChainStateFlushed(const CBlockLocator& locator) {}
233229
};
@@ -236,9 +232,8 @@ class Chain
236232
virtual std::unique_ptr<Handler> handleNotifications(Notifications& notifications) = 0;
237233

238234
//! Wait for pending notifications to be processed unless block hash points to the current
239-
//! chain tip, or to a possible descendant of the current chain tip that isn't currently
240-
//! connected.
241-
virtual void waitForNotificationsIfNewBlocksConnected(const uint256& old_tip) = 0;
235+
//! chain tip.
236+
virtual void waitForNotificationsIfTipChanged(const uint256& old_tip) = 0;
242237

243238
//! Register handler for RPC. Command is not copied, so reference
244239
//! needs to remain valid until Handler is disconnected.

src/interfaces/wallet.cpp

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ namespace interfaces {
3131
namespace {
3232

3333
//! Construct wallet tx struct.
34-
WalletTx MakeWalletTx(interfaces::Chain::Lock& locked_chain, CWallet& wallet, const CWalletTx& wtx)
34+
WalletTx MakeWalletTx(CWallet& wallet, const CWalletTx& wtx)
3535
{
3636
WalletTx result;
3737
result.tx = wtx.tx;
@@ -49,7 +49,7 @@ WalletTx MakeWalletTx(interfaces::Chain::Lock& locked_chain, CWallet& wallet, co
4949
wallet.IsMine(result.txout_address.back()) :
5050
ISMINE_NO);
5151
}
52-
result.credit = wtx.GetCredit(locked_chain, ISMINE_ALL);
52+
result.credit = wtx.GetCredit(ISMINE_ALL);
5353
result.debit = wtx.GetDebit(ISMINE_ALL);
5454
result.change = wtx.GetChange();
5555
result.time = wtx.GetTxTime();
@@ -63,21 +63,20 @@ WalletTxStatus MakeWalletTxStatus(interfaces::Chain::Lock& locked_chain, const C
6363
{
6464
WalletTxStatus result;
6565
result.block_height = locked_chain.getBlockHeight(wtx.m_confirm.hashBlock).get_value_or(std::numeric_limits<int>::max());
66-
result.blocks_to_maturity = wtx.GetBlocksToMaturity(locked_chain);
67-
result.depth_in_main_chain = wtx.GetDepthInMainChain(locked_chain);
66+
result.blocks_to_maturity = wtx.GetBlocksToMaturity();
67+
result.depth_in_main_chain = wtx.GetDepthInMainChain();
6868
result.time_received = wtx.nTimeReceived;
6969
result.lock_time = wtx.tx->nLockTime;
7070
result.is_final = locked_chain.checkFinalTx(*wtx.tx);
7171
result.is_trusted = wtx.IsTrusted(locked_chain);
7272
result.is_abandoned = wtx.isAbandoned();
7373
result.is_coinbase = wtx.IsCoinBase();
74-
result.is_in_main_chain = wtx.IsInMainChain(locked_chain);
74+
result.is_in_main_chain = wtx.IsInMainChain();
7575
return result;
7676
}
7777

7878
//! Construct wallet TxOut struct.
79-
WalletTxOut MakeWalletTxOut(interfaces::Chain::Lock& locked_chain,
80-
CWallet& wallet,
79+
WalletTxOut MakeWalletTxOut(CWallet& wallet,
8180
const CWalletTx& wtx,
8281
int n,
8382
int depth) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet)
@@ -86,7 +85,7 @@ WalletTxOut MakeWalletTxOut(interfaces::Chain::Lock& locked_chain,
8685
result.txout = wtx.tx->vout[n];
8786
result.time = wtx.GetTxTime();
8887
result.depth_in_main_chain = depth;
89-
result.is_spent = wallet.IsSpent(locked_chain, wtx.GetHash(), n);
88+
result.is_spent = wallet.IsSpent(wtx.GetHash(), n);
9089
return result;
9190
}
9291

@@ -235,7 +234,7 @@ class WalletImpl : public Wallet
235234
{
236235
auto locked_chain = m_wallet->chain().lock();
237236
LOCK(m_wallet->cs_wallet);
238-
return m_wallet->AbandonTransaction(*locked_chain, txid);
237+
return m_wallet->AbandonTransaction(txid);
239238
}
240239
bool transactionCanBeBumped(const uint256& txid) override
241240
{
@@ -282,7 +281,7 @@ class WalletImpl : public Wallet
282281
LOCK(m_wallet->cs_wallet);
283282
auto mi = m_wallet->mapWallet.find(txid);
284283
if (mi != m_wallet->mapWallet.end()) {
285-
return MakeWalletTx(*locked_chain, *m_wallet, mi->second);
284+
return MakeWalletTx(*m_wallet, mi->second);
286285
}
287286
return {};
288287
}
@@ -293,7 +292,7 @@ class WalletImpl : public Wallet
293292
std::vector<WalletTx> result;
294293
result.reserve(m_wallet->mapWallet.size());
295294
for (const auto& entry : m_wallet->mapWallet) {
296-
result.emplace_back(MakeWalletTx(*locked_chain, *m_wallet, entry.second));
295+
result.emplace_back(MakeWalletTx(*m_wallet, entry.second));
297296
}
298297
return result;
299298
}
@@ -338,7 +337,7 @@ class WalletImpl : public Wallet
338337
in_mempool = mi->second.InMempool();
339338
order_form = mi->second.vOrderForm;
340339
tx_status = MakeWalletTxStatus(*locked_chain, mi->second);
341-
return MakeWalletTx(*locked_chain, *m_wallet, mi->second);
340+
return MakeWalletTx(*m_wallet, mi->second);
342341
}
343342
return {};
344343
}
@@ -407,7 +406,7 @@ class WalletImpl : public Wallet
407406
auto& group = result[entry.first];
408407
for (const auto& coin : entry.second) {
409408
group.emplace_back(COutPoint(coin.tx->GetHash(), coin.i),
410-
MakeWalletTxOut(*locked_chain, *m_wallet, *coin.tx, coin.i, coin.nDepth));
409+
MakeWalletTxOut(*m_wallet, *coin.tx, coin.i, coin.nDepth));
411410
}
412411
}
413412
return result;
@@ -422,9 +421,9 @@ class WalletImpl : public Wallet
422421
result.emplace_back();
423422
auto it = m_wallet->mapWallet.find(output.hash);
424423
if (it != m_wallet->mapWallet.end()) {
425-
int depth = it->second.GetDepthInMainChain(*locked_chain);
424+
int depth = it->second.GetDepthInMainChain();
426425
if (depth >= 0) {
427-
result.back() = MakeWalletTxOut(*locked_chain, *m_wallet, it->second, output.n, depth);
426+
result.back() = MakeWalletTxOut(*m_wallet, it->second, output.n, depth);
428427
}
429428
}
430429
}

src/qt/test/wallettests.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,12 @@ void TestGUI(interfaces::Node& node)
139139
wallet->LoadWallet(firstRun);
140140
{
141141
auto spk_man = wallet->GetLegacyScriptPubKeyMan();
142+
auto locked_chain = wallet->chain().lock();
142143
LOCK(wallet->cs_wallet);
143144
AssertLockHeld(spk_man->cs_wallet);
144145
wallet->SetAddressBook(GetDestinationForKey(test.coinbaseKey.GetPubKey(), wallet->m_default_address_type), "", "receive");
145146
spk_man->AddKeyPubKey(test.coinbaseKey, test.coinbaseKey.GetPubKey());
147+
wallet->SetLastBlockProcessed(105, ::ChainActive().Tip()->GetBlockHash());
146148
}
147149
{
148150
auto locked_chain = wallet->chain().lock();

src/test/validation_block_tests.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,10 @@ struct TestSubscriber : public CValidationInterface {
4040
m_expected_tip = block->GetHash();
4141
}
4242

43-
void BlockDisconnected(const std::shared_ptr<const CBlock>& block) override
43+
void BlockDisconnected(const std::shared_ptr<const CBlock>& block, const CBlockIndex* pindex) override
4444
{
4545
BOOST_CHECK_EQUAL(m_expected_tip, block->GetHash());
46+
BOOST_CHECK_EQUAL(m_expected_tip, pindex->GetBlockHash());
4647

4748
m_expected_tip = block->hashPrevBlock;
4849
}

src/validation.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2458,7 +2458,7 @@ bool CChainState::DisconnectTip(BlockValidationState& state, const CChainParams&
24582458
UpdateTip(pindexDelete->pprev, chainparams);
24592459
// Let wallets know transactions went from 1-confirmed to
24602460
// 0-confirmed or conflicted:
2461-
GetMainSignals().BlockDisconnected(pblock);
2461+
GetMainSignals().BlockDisconnected(pblock, pindexDelete);
24622462
return true;
24632463
}
24642464

src/validationinterface.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ struct MainSignalsInstance {
2929
boost::signals2::signal<void (const CBlockIndex *, const CBlockIndex *, bool fInitialDownload)> UpdatedBlockTip;
3030
boost::signals2::signal<void (const CTransactionRef &)> TransactionAddedToMempool;
3131
boost::signals2::signal<void (const std::shared_ptr<const CBlock> &, const CBlockIndex *pindex, const std::vector<CTransactionRef>&)> BlockConnected;
32-
boost::signals2::signal<void (const std::shared_ptr<const CBlock> &)> BlockDisconnected;
32+
boost::signals2::signal<void (const std::shared_ptr<const CBlock>&, const CBlockIndex* pindex)> BlockDisconnected;
3333
boost::signals2::signal<void (const CTransactionRef &)> TransactionRemovedFromMempool;
3434
boost::signals2::signal<void (const CBlockLocator &)> ChainStateFlushed;
3535
boost::signals2::signal<void (const CBlock&, const BlockValidationState&)> BlockChecked;
@@ -92,7 +92,7 @@ void RegisterValidationInterface(CValidationInterface* pwalletIn) {
9292
conns.UpdatedBlockTip = g_signals.m_internals->UpdatedBlockTip.connect(std::bind(&CValidationInterface::UpdatedBlockTip, pwalletIn, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
9393
conns.TransactionAddedToMempool = g_signals.m_internals->TransactionAddedToMempool.connect(std::bind(&CValidationInterface::TransactionAddedToMempool, pwalletIn, std::placeholders::_1));
9494
conns.BlockConnected = g_signals.m_internals->BlockConnected.connect(std::bind(&CValidationInterface::BlockConnected, pwalletIn, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
95-
conns.BlockDisconnected = g_signals.m_internals->BlockDisconnected.connect(std::bind(&CValidationInterface::BlockDisconnected, pwalletIn, std::placeholders::_1));
95+
conns.BlockDisconnected = g_signals.m_internals->BlockDisconnected.connect(std::bind(&CValidationInterface::BlockDisconnected, pwalletIn, std::placeholders::_1, std::placeholders::_2));
9696
conns.TransactionRemovedFromMempool = g_signals.m_internals->TransactionRemovedFromMempool.connect(std::bind(&CValidationInterface::TransactionRemovedFromMempool, pwalletIn, std::placeholders::_1));
9797
conns.ChainStateFlushed = g_signals.m_internals->ChainStateFlushed.connect(std::bind(&CValidationInterface::ChainStateFlushed, pwalletIn, std::placeholders::_1));
9898
conns.BlockChecked = g_signals.m_internals->BlockChecked.connect(std::bind(&CValidationInterface::BlockChecked, pwalletIn, std::placeholders::_1, std::placeholders::_2));
@@ -156,9 +156,10 @@ void CMainSignals::BlockConnected(const std::shared_ptr<const CBlock> &pblock, c
156156
});
157157
}
158158

159-
void CMainSignals::BlockDisconnected(const std::shared_ptr<const CBlock> &pblock) {
160-
m_internals->m_schedulerClient.AddToProcessQueue([pblock, this] {
161-
m_internals->BlockDisconnected(pblock);
159+
void CMainSignals::BlockDisconnected(const std::shared_ptr<const CBlock>& pblock, const CBlockIndex* pindex)
160+
{
161+
m_internals->m_schedulerClient.AddToProcessQueue([pblock, pindex, this] {
162+
m_internals->BlockDisconnected(pblock, pindex);
162163
});
163164
}
164165

src/validationinterface.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ class CValidationInterface {
114114
*
115115
* Called on a background thread.
116116
*/
117-
virtual void BlockDisconnected(const std::shared_ptr<const CBlock> &block) {}
117+
virtual void BlockDisconnected(const std::shared_ptr<const CBlock> &block, const CBlockIndex* pindex) {}
118118
/**
119119
* Notifies listeners of the new active block chain on-disk.
120120
*
@@ -178,7 +178,7 @@ class CMainSignals {
178178
void UpdatedBlockTip(const CBlockIndex *, const CBlockIndex *, bool fInitialDownload);
179179
void TransactionAddedToMempool(const CTransactionRef &);
180180
void BlockConnected(const std::shared_ptr<const CBlock> &, const CBlockIndex *pindex, const std::shared_ptr<const std::vector<CTransactionRef>> &);
181-
void BlockDisconnected(const std::shared_ptr<const CBlock> &);
181+
void BlockDisconnected(const std::shared_ptr<const CBlock> &, const CBlockIndex* pindex);
182182
void ChainStateFlushed(const CBlockLocator &);
183183
void BlockChecked(const CBlock&, const BlockValidationState&);
184184
void NewPoWValidBlock(const CBlockIndex *, const std::shared_ptr<const CBlock>&);

src/wallet/feebumper.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
//! Check whether transaction has descendant in wallet or mempool, or has been
1818
//! mined, or conflicts with a mined transaction. Return a feebumper::Result.
19-
static feebumper::Result PreconditionChecks(interfaces::Chain::Lock& locked_chain, const CWallet& wallet, const CWalletTx& wtx, std::vector<std::string>& errors) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet)
19+
static feebumper::Result PreconditionChecks(const CWallet& wallet, const CWalletTx& wtx, std::vector<std::string>& errors) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet)
2020
{
2121
if (wallet.HasWalletSpend(wtx.GetHash())) {
2222
errors.push_back("Transaction has descendants in the wallet");
@@ -30,7 +30,7 @@ static feebumper::Result PreconditionChecks(interfaces::Chain::Lock& locked_chai
3030
}
3131
}
3232

33-
if (wtx.GetDepthInMainChain(locked_chain) != 0) {
33+
if (wtx.GetDepthInMainChain() != 0) {
3434
errors.push_back("Transaction has been mined, or is conflicted with a mined transaction");
3535
return feebumper::Result::WALLET_ERROR;
3636
}
@@ -146,7 +146,7 @@ bool TransactionCanBeBumped(const CWallet& wallet, const uint256& txid)
146146
if (wtx == nullptr) return false;
147147

148148
std::vector<std::string> errors_dummy;
149-
feebumper::Result res = PreconditionChecks(*locked_chain, wallet, *wtx, errors_dummy);
149+
feebumper::Result res = PreconditionChecks(wallet, *wtx, errors_dummy);
150150
return res == feebumper::Result::OK;
151151
}
152152

@@ -165,7 +165,7 @@ Result CreateTotalBumpTransaction(const CWallet* wallet, const uint256& txid, co
165165
}
166166
const CWalletTx& wtx = it->second;
167167

168-
Result result = PreconditionChecks(*locked_chain, *wallet, wtx, errors);
168+
Result result = PreconditionChecks(*wallet, wtx, errors);
169169
if (result != Result::OK) {
170170
return result;
171171
}
@@ -291,7 +291,7 @@ Result CreateRateBumpTransaction(CWallet& wallet, const uint256& txid, const CCo
291291
}
292292
const CWalletTx& wtx = it->second;
293293

294-
Result result = PreconditionChecks(*locked_chain, wallet, wtx, errors);
294+
Result result = PreconditionChecks(wallet, wtx, errors);
295295
if (result != Result::OK) {
296296
return result;
297297
}
@@ -382,7 +382,7 @@ Result CommitTransaction(CWallet& wallet, const uint256& txid, CMutableTransacti
382382
CWalletTx& oldWtx = it->second;
383383

384384
// make sure the transaction still has no descendants and hasn't been mined in the meantime
385-
Result result = PreconditionChecks(*locked_chain, wallet, oldWtx, errors);
385+
Result result = PreconditionChecks(wallet, oldWtx, errors);
386386
if (result != Result::OK) {
387387
return result;
388388
}

src/wallet/rpcdump.cpp

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ UniValue importaddress(const JSONRPCRequest& request)
316316
{
317317
auto locked_chain = pwallet->chain().lock();
318318
LOCK(pwallet->cs_wallet);
319-
pwallet->ReacceptWalletTransactions(*locked_chain);
319+
pwallet->ReacceptWalletTransactions();
320320
}
321321
}
322322

@@ -354,28 +354,26 @@ UniValue importprunedfunds(const JSONRPCRequest& request)
354354
//Search partial merkle tree in proof for our transaction and index in valid block
355355
std::vector<uint256> vMatch;
356356
std::vector<unsigned int> vIndex;
357-
unsigned int txnIndex = 0;
358-
if (merkleBlock.txn.ExtractMatches(vMatch, vIndex) == merkleBlock.header.hashMerkleRoot) {
359-
360-
auto locked_chain = pwallet->chain().lock();
361-
if (locked_chain->getBlockHeight(merkleBlock.header.GetHash()) == nullopt) {
362-
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found in chain");
363-
}
364-
365-
std::vector<uint256>::const_iterator it;
366-
if ((it = std::find(vMatch.begin(), vMatch.end(), hashTx))==vMatch.end()) {
367-
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Transaction given doesn't exist in proof");
368-
}
357+
if (merkleBlock.txn.ExtractMatches(vMatch, vIndex) != merkleBlock.header.hashMerkleRoot) {
358+
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Something wrong with merkleblock");
359+
}
369360

370-
txnIndex = vIndex[it - vMatch.begin()];
361+
auto locked_chain = pwallet->chain().lock();
362+
Optional<int> height = locked_chain->getBlockHeight(merkleBlock.header.GetHash());
363+
if (height == nullopt) {
364+
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found in chain");
371365
}
372-
else {
373-
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Something wrong with merkleblock");
366+
367+
std::vector<uint256>::const_iterator it;
368+
if ((it = std::find(vMatch.begin(), vMatch.end(), hashTx)) == vMatch.end()) {
369+
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Transaction given doesn't exist in proof");
374370
}
375371

376-
wtx.SetConf(CWalletTx::Status::CONFIRMED, merkleBlock.header.GetHash(), txnIndex);
372+
unsigned int txnIndex = vIndex[it - vMatch.begin()];
373+
374+
CWalletTx::Confirmation confirm(CWalletTx::Status::CONFIRMED, *height, merkleBlock.header.GetHash(), txnIndex);
375+
wtx.m_confirm = confirm;
377376

378-
auto locked_chain = pwallet->chain().lock();
379377
LOCK(pwallet->cs_wallet);
380378

381379
if (pwallet->IsMine(*wtx.tx)) {
@@ -507,7 +505,7 @@ UniValue importpubkey(const JSONRPCRequest& request)
507505
{
508506
auto locked_chain = pwallet->chain().lock();
509507
LOCK(pwallet->cs_wallet);
510-
pwallet->ReacceptWalletTransactions(*locked_chain);
508+
pwallet->ReacceptWalletTransactions();
511509
}
512510
}
513511

@@ -1406,7 +1404,7 @@ UniValue importmulti(const JSONRPCRequest& mainRequest)
14061404
{
14071405
auto locked_chain = pwallet->chain().lock();
14081406
LOCK(pwallet->cs_wallet);
1409-
pwallet->ReacceptWalletTransactions(*locked_chain);
1407+
pwallet->ReacceptWalletTransactions();
14101408
}
14111409

14121410
if (pwallet->IsAbortingRescan()) {

0 commit comments

Comments
 (0)