Skip to content

Commit

Permalink
feat: remove duplicated columns from DB
Browse files Browse the repository at this point in the history
  • Loading branch information
MatusKysel committed Mar 9, 2023
1 parent c7e0b90 commit 9a4f4af
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 28 deletions.
21 changes: 5 additions & 16 deletions libraries/core_libs/consensus/src/final_chain/final_chain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -343,14 +335,12 @@ class FinalChainImpl final : public FinalChain {
return block_headers_cache_.get(*n);
}

std::optional<TransactionLocation> 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<TransactionLocation> 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<TransactionReceipt> transaction_receipt(h256 const& trx_h) const override {
Expand All @@ -364,8 +354,7 @@ class FinalChainImpl final : public FinalChain {
}

uint64_t transactionCount(std::optional<EthBlockNumber> n = {}) const override {
return db_->lookup_int<uint64_t>(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<const TransactionHashes> transaction_hashes(std::optional<EthBlockNumber> n = {}) const override {
Expand Down
4 changes: 1 addition & 3 deletions libraries/core_libs/storage/include/storage/storage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,7 @@ class DbStorage : public std::enable_shared_from_this<DbStorage> {
COLUMN(dag_block_period);
COLUMN_W_COMP(proposal_period_levels_map, getIntComparator<uint64_t>());
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);
Expand Down Expand Up @@ -231,6 +228,7 @@ class DbStorage : public std::enable_shared_from_this<DbStorage> {
void addTransactionPeriodToBatch(Batch& write_batch, trx_hash_t const& trx, PbftPeriod period, uint32_t position);
std::optional<std::pair<PbftPeriod, uint32_t>> getTransactionPeriod(trx_hash_t const& hash) const;
std::unordered_map<trx_hash_t, PbftPeriod> getAllTransactionPeriod();
uint64_t getTransactionCount(PbftPeriod period) const;

// PBFT manager
uint32_t getPbftMgrField(PbftMgrField field);
Expand Down
12 changes: 10 additions & 2 deletions libraries/core_libs/storage/src/storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/split.hpp>
#include <cstdint>
#include <memory>

#include "config/version.hpp"
Expand Down Expand Up @@ -451,7 +452,6 @@ void DbStorage::clearPeriodDataHistory(PbftPeriod end_period) {
auto hash = trx_hash_t(reinterpret_cast<uint8_t*>(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) {
Expand All @@ -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);
}
}
Expand Down Expand Up @@ -608,6 +607,15 @@ std::shared_ptr<Transaction> 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<std::optional<SharedTransactions>, trx_hash_t> DbStorage::getFinalizedTransactions(
std::vector<trx_hash_t> const& trx_hashes) const {
// Map of period to position of transactions within a period
Expand Down
8 changes: 3 additions & 5 deletions tests/final_chain_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,16 @@ struct FinalChainTest : WithDataDir {
SUT = nullptr;
SUT = NewFinalChain(db, cfg);
std::vector<h256> 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<vote_hash_t> reward_votes_hashes;
auto pbft_block =
std::make_shared<PbftBlock>(kNullBlockHash, kNullBlockHash, kNullBlockHash, kNullBlockHash, 1, addr_t::random(),
dev::KeyPair::create().secret(), std::move(reward_votes_hashes));
std::make_shared<PbftBlock>(kNullBlockHash, kNullBlockHash, kNullBlockHash, kNullBlockHash, expected_blk_num,
addr_t::random(), dev::KeyPair::create().secret(), std::move(reward_votes_hashes));
std::vector<std::shared_ptr<Vote>> votes;
PeriodData period_data(pbft_block, votes);
period_data.dag_blocks.push_back(dag_blk);
Expand All @@ -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()));
Expand Down
2 changes: 0 additions & 2 deletions tests/transaction_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 9a4f4af

Please sign in to comment.