Skip to content

Commit 176365e

Browse files
committed
Merge branch 'dapsnet' of https://gitlab.com/credits_bc/core/node into dev
2 parents 06b4787 + 14b89b2 commit 176365e

9 files changed

Lines changed: 88 additions & 1 deletion

File tree

csdb/include/csdb/database.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class Database {
4747

4848
virtual bool putToTransIndex(const cs::Bytes& key, const cs::Bytes& value) = 0;
4949
virtual bool getFromTransIndex(const cs::Bytes& key, cs::Bytes* value) = 0;
50+
virtual bool removeLastFromTrxIndex(const cs::Bytes& key) = 0;
5051
virtual void truncateTransIndex() = 0;
5152

5253
virtual bool updateContractData(const cs::Bytes& key, const cs::Bytes& data) = 0;

csdb/include/csdb/database_berkeleydb.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class DatabaseBerkeleyDB : public Database {
4040

4141
bool putToTransIndex(const cs::Bytes& key, const cs::Bytes& value) override final;
4242
bool getFromTransIndex(const cs::Bytes& key, cs::Bytes* value) override final;
43+
bool removeLastFromTrxIndex(const cs::Bytes& key) override final;
4344
void truncateTransIndex() override final;
4445

4546
bool updateContractData(const cs::Bytes& key, const cs::Bytes& data) override;

csdb/include/csdb/storage.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ class Storage final {
227227
// And now for something completely different
228228
cs::Sequence get_previous_transaction_block(const Address&, cs::Sequence) const;
229229
void set_previous_transaction_block(const Address&, cs::Sequence currTransBlock, cs::Sequence prevTransBlock);
230+
void remove_last_from_trx_index(const Address&, cs::Sequence lastIndexed);
230231
void truncate_trxs_index();
231232

232233
/**

csdb/src/database_berkeleydb.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,24 @@ bool DatabaseBerkeleyDB::putToTransIndex(const cs::Bytes &key, const cs::Bytes &
497497
return true;
498498
}
499499

500+
bool DatabaseBerkeleyDB::removeLastFromTrxIndex(const cs::Bytes &key) {
501+
if (!db_trans_idx_) {
502+
set_last_error(NotOpen);
503+
return false;
504+
}
505+
506+
Dbt_copy<cs::Bytes> db_key(key);
507+
508+
int status = db_trans_idx_->del(nullptr, &db_key, 0);
509+
if (status != 0) {
510+
set_last_error_from_berkeleydb(status);
511+
return false;
512+
}
513+
514+
set_last_error();
515+
return true;
516+
}
517+
500518
bool DatabaseBerkeleyDB::getFromTransIndex(const cs::Bytes &key, cs::Bytes *value) {
501519
if (!db_trans_idx_) {
502520
set_last_error(NotOpen);

csdb/src/storage.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -871,6 +871,10 @@ void Storage::set_previous_transaction_block(const Address& addr, cs::Sequence c
871871
d->db->putToTransIndex(key, os.buffer());
872872
}
873873

874+
void Storage::remove_last_from_trx_index(const Address& addr, cs::Sequence lastIndexed) {
875+
d->db->removeLastFromTrxIndex(get_trans_index_key(addr, lastIndexed));
876+
}
877+
874878
void Storage::truncate_trxs_index() {
875879
d->db->truncateTransIndex();
876880
}

csnode/include/csnode/blockchain.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ class BlockChain {
115115
}
116116

117117
void removeWalletsInPoolFromCache(const csdb::Pool& pool);
118+
void removeLastBlockFromTrxIndex(const csdb::Pool&);
118119
void removeLastBlock();
119120

120121
// updates fees in every transaction

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: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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+
465513
void BlockChain::removeWalletsInPoolFromCache(const csdb::Pool& pool) {
466514
try {
467515
std::lock_guard lock(cacheMutex_);

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)