From 9a4f4afcc7517a2a46bf09c50f666fad0b64c88f Mon Sep 17 00:00:00 2001 From: Matus Kysel Date: Thu, 9 Mar 2023 16:22:38 +0100 Subject: [PATCH] feat: remove duplicated columns from DB --- .../consensus/src/final_chain/final_chain.cpp | 21 +++++-------------- .../storage/include/storage/storage.hpp | 4 +--- libraries/core_libs/storage/src/storage.cpp | 12 +++++++++-- tests/final_chain_test.cpp | 8 +++---- tests/transaction_test.cpp | 2 -- 5 files changed, 19 insertions(+), 28 deletions(-) diff --git a/libraries/core_libs/consensus/src/final_chain/final_chain.cpp b/libraries/core_libs/consensus/src/final_chain/final_chain.cpp index 4d7c2e2a8a..5c4d2d8b34 100644 --- a/libraries/core_libs/consensus/src/final_chain/final_chain.cpp +++ b/libraries/core_libs/consensus/src/final_chain/final_chain.cpp @@ -309,16 +309,8 @@ class FinalChainImpl final : public FinalChain { chunk_to_alter[index % c_bloomIndexSize] |= log_bloom_for_index; db_->insert(batch, DB::Columns::final_chain_log_blooms_index, chunk_id, util::rlp_enc(rlp_strm, chunk_to_alter)); } - TransactionLocation tl{blk_header.number}; - for (auto const& trx : transactions) { - db_->insert(batch, DB::Columns::final_chain_transaction_location_by_hash, trx->getHash(), - util::rlp_enc(rlp_strm, tl)); - ++tl.index; - } db_->insert(batch, DB::Columns::final_chain_transaction_hashes_by_blk_number, blk_header.number, dev::rlp(hashes_from_transactions(transactions))); - db_->insert(batch, DB::Columns::final_chain_transaction_count_by_blk_number, blk_header.number, - transactions.size()); db_->insert(batch, DB::Columns::final_chain_blk_hash_by_number, blk_header.number, blk_header.hash); db_->insert(batch, DB::Columns::final_chain_blk_number_by_hash, blk_header.hash, blk_header.number); db_->insert(batch, DB::Columns::final_chain_meta, DBMetaKeys::LAST_NUMBER, blk_header.number); @@ -343,14 +335,12 @@ class FinalChainImpl final : public FinalChain { return block_headers_cache_.get(*n); } - std::optional transaction_location(h256 const& trx_hash) const override { - auto raw = db_->lookup(trx_hash, DB::Columns::final_chain_transaction_location_by_hash); - if (raw.empty()) { + std::optional transaction_location(const h256& trx_hash) const override { + const auto period = db_->getTransactionPeriod(trx_hash); + if (!period) { return {}; } - TransactionLocation ret; - ret.rlp(dev::RLP(raw)); - return ret; + return TransactionLocation{period->first, period->second}; } std::optional transaction_receipt(h256 const& trx_h) const override { @@ -364,8 +354,7 @@ class FinalChainImpl final : public FinalChain { } uint64_t transactionCount(std::optional n = {}) const override { - return db_->lookup_int(last_if_absent(n), DB::Columns::final_chain_transaction_count_by_blk_number) - .value_or(0); + return db_->getTransactionCount(last_if_absent(n)); } std::shared_ptr transaction_hashes(std::optional n = {}) const override { diff --git a/libraries/core_libs/storage/include/storage/storage.hpp b/libraries/core_libs/storage/include/storage/storage.hpp index 5f3ae3fbd1..7f78647eba 100644 --- a/libraries/core_libs/storage/include/storage/storage.hpp +++ b/libraries/core_libs/storage/include/storage/storage.hpp @@ -111,10 +111,7 @@ class DbStorage : public std::enable_shared_from_this { COLUMN(dag_block_period); COLUMN_W_COMP(proposal_period_levels_map, getIntComparator()); COLUMN(final_chain_meta); - COLUMN(final_chain_transaction_location_by_hash); - COLUMN(final_chain_replay_protection); COLUMN(final_chain_transaction_hashes_by_blk_number); - COLUMN(final_chain_transaction_count_by_blk_number); COLUMN(final_chain_blk_by_number); COLUMN(final_chain_blk_hash_by_number); COLUMN(final_chain_blk_number_by_hash); @@ -231,6 +228,7 @@ class DbStorage : public std::enable_shared_from_this { void addTransactionPeriodToBatch(Batch& write_batch, trx_hash_t const& trx, PbftPeriod period, uint32_t position); std::optional> getTransactionPeriod(trx_hash_t const& hash) const; std::unordered_map getAllTransactionPeriod(); + uint64_t getTransactionCount(PbftPeriod period) const; // PBFT manager uint32_t getPbftMgrField(PbftMgrField field); diff --git a/libraries/core_libs/storage/src/storage.cpp b/libraries/core_libs/storage/src/storage.cpp index 98b510f0ed..61cec78330 100644 --- a/libraries/core_libs/storage/src/storage.cpp +++ b/libraries/core_libs/storage/src/storage.cpp @@ -2,6 +2,7 @@ #include #include +#include #include #include "config/version.hpp" @@ -451,7 +452,6 @@ void DbStorage::clearPeriodDataHistory(PbftPeriod end_period) { auto hash = trx_hash_t(reinterpret_cast(trx_hashes_raw.data() + i * trx_hash_t::size), trx_hash_t::ConstructFromPointer); remove(write_batch, Columns::final_chain_receipt_by_trx_hash, hash); - remove(write_batch, Columns::final_chain_transaction_location_by_hash, hash); } remove(write_batch, Columns::final_chain_transaction_hashes_by_blk_number, EthBlockNumber(period)); if ((period - start_period + 1) % max_batch_delete == 0) { @@ -466,7 +466,6 @@ void DbStorage::clearPeriodDataHistory(PbftPeriod end_period) { // data in the database and free disk space db_->CompactRange({}, handle(Columns::period_data), &start_slice, &end_slice); db_->CompactRange({}, handle(Columns::final_chain_receipt_by_trx_hash), nullptr, nullptr); - db_->CompactRange({}, handle(Columns::final_chain_transaction_location_by_hash), nullptr, nullptr); db_->CompactRange({}, handle(Columns::final_chain_transaction_hashes_by_blk_number), nullptr, nullptr); } } @@ -608,6 +607,15 @@ std::shared_ptr DbStorage::getTransaction(trx_hash_t const& hash) { return nullptr; } +uint64_t DbStorage::getTransactionCount(PbftPeriod period) const { + auto period_data = getPeriodDataRaw(period); + if (period_data.size()) { + auto period_data_rlp = dev::RLP(period_data); + return period_data_rlp[TRANSACTIONS_POS_IN_PERIOD_DATA].itemCount(); + } + return 0; +} + std::pair, trx_hash_t> DbStorage::getFinalizedTransactions( std::vector const& trx_hashes) const { // Map of period to position of transactions within a period diff --git a/tests/final_chain_test.cpp b/tests/final_chain_test.cpp index 99d2fdd58f..daaa0b45b9 100644 --- a/tests/final_chain_test.cpp +++ b/tests/final_chain_test.cpp @@ -45,17 +45,16 @@ struct FinalChainTest : WithDataDir { SUT = nullptr; SUT = NewFinalChain(db, cfg); std::vector trx_hashes; - int pos = 0; + ++expected_blk_num; for (const auto& trx : trxs) { - db->saveTransactionPeriod(trx->getHash(), 1, pos++); trx_hashes.emplace_back(trx->getHash()); } DagBlock dag_blk({}, {}, {}, trx_hashes, {}, {}, secret_t::random()); db->saveDagBlock(dag_blk); std::vector reward_votes_hashes; auto pbft_block = - std::make_shared(kNullBlockHash, kNullBlockHash, kNullBlockHash, kNullBlockHash, 1, addr_t::random(), - dev::KeyPair::create().secret(), std::move(reward_votes_hashes)); + std::make_shared(kNullBlockHash, kNullBlockHash, kNullBlockHash, kNullBlockHash, expected_blk_num, + addr_t::random(), dev::KeyPair::create().secret(), std::move(reward_votes_hashes)); std::vector> votes; PeriodData period_data(pbft_block, votes); period_data.dag_blocks.push_back(dag_blk); @@ -67,7 +66,6 @@ struct FinalChainTest : WithDataDir { db->commitWriteBatch(batch); auto result = SUT->finalize(std::move(period_data), {dag_blk.getHash()}).get(); - ++expected_blk_num; const auto& blk_h = *result->final_chain_blk; EXPECT_EQ(util::rlp_enc(blk_h), util::rlp_enc(*SUT->block_header(blk_h.number))); EXPECT_EQ(util::rlp_enc(blk_h), util::rlp_enc(*SUT->block_header())); diff --git a/tests/transaction_test.cpp b/tests/transaction_test.cpp index f6e2ebfc94..53637d89a0 100644 --- a/tests/transaction_test.cpp +++ b/tests/transaction_test.cpp @@ -211,8 +211,6 @@ TEST_F(TransactionTest, transaction_low_nonce) { SharedTransactions trxs{trx_1, trx_2}; period_data.transactions = trxs; auto batch = db->createWriteBatch(); - db->saveTransactionPeriod(trx_1->getHash(), 1, 0); - db->saveTransactionPeriod(trx_2->getHash(), 1, 0); db->savePeriodData(period_data, batch); db->commitWriteBatch(batch); final_chain->finalize(std::move(period_data), {dag_blk.getHash()}).get();