Skip to content

Commit

Permalink
Improve clarity of the final portion of evm_runner::exec()
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Jung <[email protected]>
  • Loading branch information
AlexRamRam committed Oct 18, 2023
1 parent cba7cc3 commit cbf8884
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
32 changes: 20 additions & 12 deletions src/parsec/agent/runners/evm/impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -585,31 +585,39 @@ namespace cbdc::parsec::agent::runner {
m_log->trace("EVM output data:", out_buf.to_hex());

m_log->trace("Result status: ", result.status_code);
auto fn = [this, gas_left = result.gas_left]() {

// finalize_fn() makes the final set of state updates and invokes
// the main result callback with the full set of accumulated state
// updates
auto finalize_fn = [this, gas_left = result.gas_left]() {
auto gas_used = m_msg.gas - gas_left;
m_host->finalize(gas_left, gas_used);
auto state_updates = m_host->get_state_updates();
m_result_callback(state_updates);
};
lock_index_keys(fn);

const auto log_index_keys = m_host->get_log_index_keys();
if(log_index_keys.empty()) {
finalize_fn();
} else {
lock_index_keys_and_finalize(log_index_keys, finalize_fn);
}
}
}

void evm_runner::lock_index_keys(const std::function<void()>& callback) {
auto keys = m_host->get_log_index_keys();
if(keys.empty()) {
callback();
return;
}
void evm_runner::lock_index_keys_and_finalize(
const std::vector<cbdc::buffer>& keys,
const std::function<void()>& finalize_fn) {
auto acquired = std::make_shared<std::atomic<size_t>>();
for(auto& key : keys) {
for(const auto& key : keys) {
auto success = m_try_lock_callback(
key,
broker::lock_type::write,
[acquired, callback, key_count = keys.size()](
[acquired, finalize_fn, key_count = keys.size()](
const broker::interface::try_lock_return_type&) {
if(++(*acquired) == key_count) {
callback();
const bool is_last_key = ++(*acquired) == key_count;
if(is_last_key) {
finalize_fn();
}
});
if(!success) {
Expand Down
4 changes: 3 additions & 1 deletion src/parsec/agent/runners/evm/impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,9 @@ namespace cbdc::parsec::agent::runner {
const broker::interface::try_lock_return_type& res);

void lock_ticket_number_key_and_continue_exec();
void lock_index_keys(const std::function<void()>& callback);
void
lock_index_keys_and_finalize(const std::vector<cbdc::buffer>& keys,
const std::function<void()>& finalize_fn);
void schedule_exec();

void schedule(const std::function<void()>& fn);
Expand Down

0 comments on commit cbf8884

Please sign in to comment.