From 9b303f3451064c8a0bc7060528b4c993ccc43461 Mon Sep 17 00:00:00 2001 From: Alexander Jung <104335080+AlexRamRam@users.noreply.github.com> Date: Tue, 17 Oct 2023 21:20:10 -0400 Subject: [PATCH] Improve clarity of the final portion of evm_runner::exec() Signed-off-by: Alexander Jung <104335080+AlexRamRam@users.noreply.github.com> --- src/parsec/agent/runners/evm/impl.cpp | 32 +++++++++++++++++---------- src/parsec/agent/runners/evm/impl.hpp | 4 +++- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/src/parsec/agent/runners/evm/impl.cpp b/src/parsec/agent/runners/evm/impl.cpp index 3669ea219..39e260128 100644 --- a/src/parsec/agent/runners/evm/impl.cpp +++ b/src/parsec/agent/runners/evm/impl.cpp @@ -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& 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& keys, + const std::function& finalize_fn) { auto acquired = std::make_shared>(); - 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) { diff --git a/src/parsec/agent/runners/evm/impl.hpp b/src/parsec/agent/runners/evm/impl.hpp index c75ea463b..ec6290dc4 100644 --- a/src/parsec/agent/runners/evm/impl.hpp +++ b/src/parsec/agent/runners/evm/impl.hpp @@ -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& callback); + void + lock_index_keys_and_finalize(const std::vector& keys, + const std::function& finalize_fn); void schedule_exec(); void schedule(const std::function& fn);