Skip to content

Commit 14b89b2

Browse files
committed
wallets cache, bc : update last transactions in wallets cache when remove last block from trx index
1 parent 5cd3f98 commit 14b89b2

3 files changed

Lines changed: 37 additions & 1 deletion

File tree

csnode/include/csnode/walletscache.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <list>
55
#include <memory>
66
#include <unordered_map>
7+
#include <vector>
78

89
#include <cscrypto/cscrypto.hpp>
910
#include <csdb/address.hpp>
@@ -107,6 +108,8 @@ class WalletsCache::Updater {
107108
void rollbackExceededTimeoutContract(const csdb::Transaction&, const WalletsCache::RefContractCall&, const csdb::Amount& execFee = 0);
108109
void smartSourceTransactionReleased(const csdb::Transaction& smartSourceTrx, const csdb::Transaction& initTrx);
109110

111+
void updateLastTransactions(const std::vector<std::pair<PublicKey, csdb::TransactionID>>&);
112+
110113
PublicKey toPublicKey(const csdb::Address&) const;
111114

112115
private:

csnode/src/blockchain.cpp

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,10 +465,28 @@ csdb::Address BlockChain::getAddressFromKey(const std::string& key) {
465465

466466
void BlockChain::removeLastBlockFromTrxIndex(const csdb::Pool& pool) {
467467
std::set<csdb::Address> uniqueAddresses;
468+
std::vector<std::pair<cs::PublicKey, csdb::TransactionID>> updates;
468469

469-
auto lbd = [&uniqueAddresses, this](const csdb::Address& addr, cs::Sequence sq) {
470+
auto lbd = [&updates, &uniqueAddresses, this](const csdb::Address& addr, cs::Sequence sq) {
470471
auto key = getAddressByType(addr, AddressType::PublicKey);
472+
471473
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+
472490
std::lock_guard<decltype(dbLock_)> l(dbLock_);
473491
storage_.remove_last_from_trx_index(key, sq);
474492
}
@@ -485,6 +503,11 @@ void BlockChain::removeLastBlockFromTrxIndex(const csdb::Pool& pool) {
485503
lastNonEmptyBlock_ = previousNonEmpty_[lastNonEmptyBlock_.poolSeq];
486504
previousNonEmpty_.erase(pool.sequence());
487505
}
506+
507+
if (updates.size()) {
508+
std::lock_guard l(cacheMutex_);
509+
walletsCacheUpdater_->updateLastTransactions(updates);
510+
}
488511
}
489512

490513
void BlockChain::removeWalletsInPoolFromCache(const csdb::Pool& pool) {

csnode/src/walletscache.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,16 @@ void WalletsCache::Updater::loadTrxForTarget(const csdb::Transaction& tr) {
360360
wallData.lastTransaction_ = tr.id();
361361
}
362362

363+
void WalletsCache::Updater::updateLastTransactions(const std::vector<std::pair<PublicKey, csdb::TransactionID>>& updates) {
364+
for (const auto& u : updates) {
365+
auto it = std::find_if(data_.wallets_.begin(), data_.wallets_.end(), [&u](auto& wall) {
366+
return wall.first == u.first; });
367+
if (it != data_.wallets_.end()) {
368+
it->second.lastTransaction_ = u.second;
369+
}
370+
}
371+
}
372+
363373
void WalletsCache::iterateOverWallets(const std::function<bool(const PublicKey&, const WalletData&)> func) {
364374
for (const auto& wallet : wallets_) {
365375
if (!func(wallet.first, wallet.second)) {

0 commit comments

Comments
 (0)