@@ -444,6 +444,7 @@ void BlockChain::removeLastBlock() {
444444 --lastSequence_;
445445 total_transactions_count_ -= pool.transactions ().size ();
446446 removeWalletsInPoolFromCache (pool);
447+ removeLastBlockFromTrxIndex (pool);
447448
448449 emit removeBlockEvent (pool.sequence ());
449450
@@ -461,7 +462,54 @@ csdb::Address BlockChain::getAddressFromKey(const std::string& key) {
461462 return res;
462463 }
463464}
464-
465+
466+ void BlockChain::removeLastBlockFromTrxIndex (const csdb::Pool& pool) {
467+ std::set<csdb::Address> uniqueAddresses;
468+ std::vector<std::pair<cs::PublicKey, csdb::TransactionID>> updates;
469+
470+ auto lbd = [&updates, &uniqueAddresses, this ](const csdb::Address& addr, cs::Sequence sq) {
471+ auto key = getAddressByType (addr, AddressType::PublicKey);
472+
473+ if (uniqueAddresses.insert (key).second ) {
474+ auto it = TransactionsIterator (*this , addr);
475+ it.next ();
476+ bool found = false ;
477+
478+ for (; it.isValid (); it.next ()) {
479+ if (it->id ().pool_seq () < sq) {
480+ updates.push_back (std::make_pair (key.public_key (), it->id ()));
481+ found = true ;
482+ break ;
483+ }
484+ }
485+ if (!found) {
486+ updates.push_back (std::make_pair (key.public_key (),
487+ csdb::TransactionID (kWrongSequence , kWrongSequence )));
488+ }
489+
490+ std::lock_guard<decltype (dbLock_)> l (dbLock_);
491+ storage_.remove_last_from_trx_index (key, sq);
492+ }
493+ };
494+
495+ for (const auto & t : pool.transactions ()) {
496+ lbd (t.source (), lastIndexedPool);
497+ lbd (t.target (), lastIndexedPool);
498+ }
499+ --lastIndexedPool;
500+ updateLastIndFile ();
501+
502+ if (lastNonEmptyBlock_.poolSeq == pool.sequence ()) {
503+ lastNonEmptyBlock_ = previousNonEmpty_[lastNonEmptyBlock_.poolSeq ];
504+ previousNonEmpty_.erase (pool.sequence ());
505+ }
506+
507+ if (updates.size ()) {
508+ std::lock_guard l (cacheMutex_);
509+ walletsCacheUpdater_->updateLastTransactions (updates);
510+ }
511+ }
512+
465513void BlockChain::removeWalletsInPoolFromCache (const csdb::Pool& pool) {
466514 try {
467515 std::lock_guard lock (cacheMutex_);
0 commit comments