@@ -1278,6 +1278,9 @@ void CWallet::BlockConnected(const std::shared_ptr<const CBlock>& pblock, const
12781278{
12791279 LOCK2 (cs_main, cs_wallet);
12801280
1281+ m_last_block_processed = pindex->GetBlockHash ();
1282+ m_last_block_processed_time = pindex->GetBlockTime ();
1283+ m_last_block_processed_height = pindex->nHeight ;
12811284 for (size_t i = 0 ; i < pblock->vtx .size (); i++) {
12821285 SyncTransaction (pblock->vtx [i], CWalletTx::Status::CONFIRMED, pindex, i);
12831286 TransactionRemovedFromMempool (pblock->vtx [i]);
@@ -1301,18 +1304,19 @@ void CWallet::BlockConnected(const std::shared_ptr<const CBlock>& pblock, const
13011304
13021305 // Sapling: Update cached incremental witnesses
13031306 ChainTipAdded (pindex, pblock.get (), oldSaplingTree);
1304-
1305- m_last_block_processed = pindex;
13061307}
13071308
1308- void CWallet::BlockDisconnected (const std::shared_ptr<const CBlock>& pblock, int nBlockHeight)
1309+ void CWallet::BlockDisconnected (const std::shared_ptr<const CBlock>& pblock, const uint256& blockHash, int nBlockHeight, int64_t blockTime )
13091310{
13101311 LOCK2 (cs_main, cs_wallet);
13111312
13121313 // At block disconnection, this will change an abandoned transaction to
13131314 // be unconfirmed, whether or not the transaction is added back to the mempool.
13141315 // User may have to call abandontransaction again. It may be addressed in the
13151316 // future with a stickier abandoned state or even removing abandontransaction call.
1317+ m_last_block_processed_height = nBlockHeight - 1 ;
1318+ m_last_block_processed_time = blockTime;
1319+ m_last_block_processed = blockHash;
13161320 for (const CTransactionRef& ptx : pblock->vtx ) {
13171321 SyncTransaction (ptx, CWalletTx::Status::UNCONFIRMED, nullptr , 0 );
13181322 }
@@ -1328,17 +1332,21 @@ void CWallet::BlockUntilSyncedToCurrentChain() {
13281332 AssertLockNotHeld (cs_main);
13291333 AssertLockNotHeld (cs_wallet);
13301334
1331- if (m_last_block_processed) {
1335+ {
13321336 // Skip the queue-draining stuff if we know we're caught up with
13331337 // chainActive.Tip()...
13341338 // We could also take cs_wallet here, and call m_last_block_processed
13351339 // protected by cs_wallet instead of cs_main, but as long as we need
13361340 // cs_main here anyway, its easier to just call it cs_main-protected.
1341+ uint256 last_block_hash = WITH_LOCK (cs_wallet, return m_last_block_processed);
13371342 LOCK (cs_main);
13381343 const CBlockIndex* initialChainTip = chainActive.Tip ();
13391344
1340- if (m_last_block_processed->GetAncestor (initialChainTip->nHeight ) == initialChainTip) {
1341- return ;
1345+ if (!last_block_hash.IsNull ()) {
1346+ auto it = mapBlockIndex.find (last_block_hash);
1347+ if (it == mapBlockIndex.end () || it->second ->GetAncestor (initialChainTip->nHeight ) == initialChainTip) {
1348+ return ;
1349+ }
13421350 }
13431351 }
13441352
@@ -4191,7 +4199,15 @@ CWallet* CWallet::CreateWalletFromFile(const std::string walletFile)
41914199 pindexRescan = FindForkInGlobalIndex (chainActive, locator);
41924200 }
41934201
4194- walletInstance->m_last_block_processed = chainActive.Tip ();
4202+ {
4203+ LOCK (walletInstance->cs_wallet );
4204+ const CBlockIndex* tip = chainActive.Tip ();
4205+ if (tip) {
4206+ walletInstance->m_last_block_processed = tip->GetBlockHash ();
4207+ walletInstance->m_last_block_processed_height = tip->nHeight ;
4208+ walletInstance->m_last_block_processed_time = tip->GetBlockTime ();
4209+ }
4210+ }
41954211 RegisterValidationInterface (walletInstance);
41964212
41974213 if (chainActive.Tip () && chainActive.Tip () != pindexRescan) {
0 commit comments