Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion category/execution/ethereum/db/db_cache.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ class DbCache final : public Db
StorageCache storage_{10'000'000};
Proposals proposals_;

uint64_t num_account_touched_{0};
uint64_t num_account_changed_{0};
uint64_t num_storage_touched_{0};
uint64_t num_storage_changed_{0};

public:
DbCache(Db &db)
: db_{db}
Expand Down Expand Up @@ -222,34 +227,59 @@ class DbCache final : public Db
virtual std::string print_stats() override
{
return db_.print_stats() + ",ac=" + accounts_.print_stats() +
",sc=" + storage_.print_stats();
",sc=" + storage_.print_stats() + print_read_write_stats();
}

virtual uint64_t get_block_number() const override
{
return db_.get_block_number();
}

std::string print_read_write_stats() const
{
std::string str = std::format(
",at={},am={},st={},sm={}",
num_account_touched_,
num_account_changed_,
num_storage_touched_,
num_storage_changed_);
return str;
}

private:
void insert_in_lru_caches(StateDeltas const &state_deltas)
{
num_account_touched_ = 0;
num_account_changed_ = 0;
num_storage_touched_ = 0;
num_storage_changed_ = 0;
for (auto it = state_deltas.cbegin(); it != state_deltas.cend(); ++it) {
auto const &address = it->first;
auto const &account_delta = it->second.account;
accounts_.insert(address, account_delta.second);
auto const &storage = it->second.storage;
auto const &account = account_delta.second;
num_account_touched_++;
bool account_changed = account_delta.first != account;
if (account.has_value()) {
for (auto it2 = storage.cbegin(); it2 != storage.cend();
++it2) {
auto const &key = it2->first;
auto const &storage_delta = it2->second;
auto const incarnation = account->incarnation;
num_storage_touched_++;
if (storage_delta.first != storage_delta.second) {
num_storage_changed_++;
account_changed = true;
}
storage_.insert(
StorageKey(address, incarnation, key),
storage_delta.second);
}
}
if (account_changed) {
num_account_changed_++;
}
}
}
};
Expand Down
27 changes: 25 additions & 2 deletions category/execution/ethereum/state2/block_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ BlockState::BlockState(Db &db, vm::VM &monad_vm)
: db_{db}
, vm_{monad_vm}
, state_(std::make_unique<StateDeltas>())
, final_state_(std::make_unique<StateDeltas>())
{
}

Expand Down Expand Up @@ -211,13 +212,26 @@ void BlockState::merge(State const &state)
}

MONAD_ASSERT(state_);
MONAD_ASSERT(final_state_);
for (auto const &[address, stack] : current) {
auto const &account_state = stack.recent();
auto const &account = account_state.account_;
auto const &storage = account_state.storage_;
StateDeltas::accessor it{};
MONAD_ASSERT(state_->find(it, address));
it->second.account.second = account;
StateDeltas::accessor final_it{};
if (final_state_->find(final_it, address)) {
MONAD_ASSERT(
final_it->second.account.first == it->second.account.first);
final_it->second.account.second = account;
}
else {
final_state_->emplace(
final_it,
address,
StateDelta{.account = it->second.account, .storage = {}});
}
if (account.has_value()) {
for (auto const &[key, value] : storage) {
StorageDeltas::accessor it2{};
Expand All @@ -226,12 +240,20 @@ void BlockState::merge(State const &state)
}
else {
it->second.storage.emplace(
key, std::make_pair(bytes32_t{}, value));
it2, key, std::make_pair(bytes32_t{}, value));
}
StorageDeltas::accessor final_it2{};
if (final_it->second.storage.find(final_it2, key)) {
final_it2->second.second = value;
}
else {
final_it->second.storage.emplace(key, it2->second);
}
}
}
else {
it->second.storage.clear();
final_it->second.storage.clear();
}
}
}
Expand All @@ -246,7 +268,7 @@ void BlockState::commit(
std::optional<std::vector<Withdrawal>> const &withdrawals)
{
db_.commit(
std::move(state_),
std::move(final_state_),
code_,
block_id,
header,
Expand All @@ -262,6 +284,7 @@ void BlockState::log_debug()
{
MONAD_ASSERT(state_);
LOG_DEBUG("State Deltas: {}", *state_);
LOG_DEBUG("Final States: {}", *final_state_);
LOG_DEBUG("Code Deltas: {}", code_);
}

Expand Down
1 change: 1 addition & 0 deletions category/execution/ethereum/state2/block_state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class BlockState final
Db &db_;
vm::VM &vm_;
std::unique_ptr<StateDeltas> state_;
std::unique_ptr<StateDeltas> final_state_;
Code code_;

public:
Expand Down
2 changes: 1 addition & 1 deletion cmd/monad/runloop_ethereum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ Result<void> process_ethereum_block(
fiber::PriorityPool &priority_pool, Block &block, bytes32_t const &block_id,
bytes32_t const &parent_block_id, bool const enable_tracing)
{
[[maybe_unused]] auto const block_start = std::chrono::system_clock::now();
[[maybe_unused]] auto const block_start = std::chrono::steady_clock::now();
auto const block_begin = std::chrono::steady_clock::now();

// Block input validation
Expand Down
2 changes: 1 addition & 1 deletion cmd/monad/runloop_monad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ Result<BlockExecOutput> propose_block(
vm::VM &vm, fiber::PriorityPool &priority_pool, bool const is_first_block,
bool const enable_tracing, BlockCache &block_cache)
{
[[maybe_unused]] auto const block_start = std::chrono::system_clock::now();
[[maybe_unused]] auto const block_start = std::chrono::steady_clock::now();
auto const block_begin = std::chrono::steady_clock::now();
auto const &block_hash_buffer =
block_hash_chain.find_chain(consensus_header.parent_id());
Expand Down
Loading