From 07376276e28d66ad7d817b3c152f9431c84c2117 Mon Sep 17 00:00:00 2001 From: Kevin Heifner Date: Tue, 27 Jul 2021 11:03:24 -0500 Subject: [PATCH 1/2] Test rodeos filter current_time --- libraries/rodeos/include/b1/rodeos/filter.hpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/libraries/rodeos/include/b1/rodeos/filter.hpp b/libraries/rodeos/include/b1/rodeos/filter.hpp index 19ce94e50f6..11929e22040 100644 --- a/libraries/rodeos/include/b1/rodeos/filter.hpp +++ b/libraries/rodeos/include/b1/rodeos/filter.hpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #ifdef EOSIO_EOS_VM_OC_RUNTIME_ENABLED # include @@ -45,7 +46,7 @@ struct eosvmoc_tier { }; #endif -struct filter_state : b1::rodeos::data_state, b1::rodeos::console_state, b1::rodeos::filter_callback_state { +struct filter_state : b1::rodeos::data_state, b1::rodeos::console_state, b1::rodeos::filter_callback_state, b1::rodeos::query_state { eosio::vm::wasm_allocator wa = {}; #ifdef EOSIO_EOS_VM_OC_RUNTIME_ENABLED std::optional eosvmoc_tierup; @@ -64,7 +65,8 @@ struct callbacks : b1::rodeos::chaindb_callbacks, b1::rodeos::filter_callbacks, b1::rodeos::memory_callbacks, b1::rodeos::unimplemented_callbacks, - b1::rodeos::unimplemented_filter_callbacks { + b1::rodeos::query_callbacks { +// b1::rodeos::unimplemented_filter_callbacks { filter::filter_state& filter_state; b1::rodeos::chaindb_state& chaindb_state; b1::rodeos::db_view_state& db_view_state; @@ -90,7 +92,8 @@ inline void register_callbacks() { b1::rodeos::filter_callbacks::register_callbacks(); b1::rodeos::memory_callbacks::register_callbacks(); b1::rodeos::unimplemented_callbacks::register_callbacks(); - b1::rodeos::unimplemented_filter_callbacks::register_callbacks(); + b1::rodeos::query_callbacks::register_callbacks(); +// b1::rodeos::unimplemented_filter_callbacks::register_callbacks(); } } // namespace b1::rodeos::filter From c802f527dee25fc1541f765a0a592891803835cd Mon Sep 17 00:00:00 2001 From: Kevin Heifner Date: Tue, 27 Jul 2021 17:06:46 -0500 Subject: [PATCH 2/2] Add current_time support to filters --- .../include/b1/rodeos/callbacks/system.hpp | 40 +++++++++++++++++++ .../rodeos/callbacks/unimplemented_filter.hpp | 8 ---- libraries/rodeos/include/b1/rodeos/filter.hpp | 10 ++--- 3 files changed, 44 insertions(+), 14 deletions(-) create mode 100644 libraries/rodeos/include/b1/rodeos/callbacks/system.hpp diff --git a/libraries/rodeos/include/b1/rodeos/callbacks/system.hpp b/libraries/rodeos/include/b1/rodeos/callbacks/system.hpp new file mode 100644 index 00000000000..ca66a8798b3 --- /dev/null +++ b/libraries/rodeos/include/b1/rodeos/callbacks/system.hpp @@ -0,0 +1,40 @@ +#pragma once + +#include +#include + +namespace b1::rodeos { + +// for filter callback. query callback for current_time is in query.hpp + +template +struct system_callbacks { + Derived& derived() { return static_cast(*this); } + + auto load_block_info() { + auto& state = derived().get_state(); + + block_info_kv table{ { derived().get_db_view_state() } }; + if (table.primary_index.begin() != table.primary_index.end()) { + return table.primary_index.begin().value(); + } + throw std::runtime_error("system callback database is missing block_info_v0"); + } + + int64_t current_time() { + auto block_info = load_block_info(); + return std::visit( + [](auto& b) { // + return b.timestamp.to_time_point().time_since_epoch().count(); + }, + block_info); + } + + template + static void register_callbacks() { + // todo: preconditions + RODEOS_REGISTER_CALLBACK(Rft, Derived, current_time); + } +}; // system_callbacks + +} // namespace b1::rodeos diff --git a/libraries/rodeos/include/b1/rodeos/callbacks/unimplemented_filter.hpp b/libraries/rodeos/include/b1/rodeos/callbacks/unimplemented_filter.hpp index e1339306db3..c23719635ba 100644 --- a/libraries/rodeos/include/b1/rodeos/callbacks/unimplemented_filter.hpp +++ b/libraries/rodeos/include/b1/rodeos/callbacks/unimplemented_filter.hpp @@ -11,14 +11,6 @@ struct unimplemented_filter_callbacks { throw std::runtime_error("wasm called " + std::string(name) + ", which is unimplemented"); } - // system_api - int64_t current_time() { return unimplemented("current_time"); } - - template - static void register_callbacks() { - // todo: preconditions - RODEOS_REGISTER_CALLBACK(Rft, Derived, current_time); - } }; // unimplemented_filter_callbacks } // namespace b1::rodeos diff --git a/libraries/rodeos/include/b1/rodeos/filter.hpp b/libraries/rodeos/include/b1/rodeos/filter.hpp index 11929e22040..b139ac7ee2f 100644 --- a/libraries/rodeos/include/b1/rodeos/filter.hpp +++ b/libraries/rodeos/include/b1/rodeos/filter.hpp @@ -7,8 +7,8 @@ #include #include #include +#include #include -#include #include #ifdef EOSIO_EOS_VM_OC_RUNTIME_ENABLED # include @@ -46,7 +46,7 @@ struct eosvmoc_tier { }; #endif -struct filter_state : b1::rodeos::data_state, b1::rodeos::console_state, b1::rodeos::filter_callback_state, b1::rodeos::query_state { +struct filter_state : b1::rodeos::data_state, b1::rodeos::console_state, b1::rodeos::filter_callback_state { eosio::vm::wasm_allocator wa = {}; #ifdef EOSIO_EOS_VM_OC_RUNTIME_ENABLED std::optional eosvmoc_tierup; @@ -65,8 +65,7 @@ struct callbacks : b1::rodeos::chaindb_callbacks, b1::rodeos::filter_callbacks, b1::rodeos::memory_callbacks, b1::rodeos::unimplemented_callbacks, - b1::rodeos::query_callbacks { -// b1::rodeos::unimplemented_filter_callbacks { + b1::rodeos::system_callbacks { filter::filter_state& filter_state; b1::rodeos::chaindb_state& chaindb_state; b1::rodeos::db_view_state& db_view_state; @@ -91,9 +90,8 @@ inline void register_callbacks() { b1::rodeos::db_callbacks::register_callbacks(); b1::rodeos::filter_callbacks::register_callbacks(); b1::rodeos::memory_callbacks::register_callbacks(); + b1::rodeos::system_callbacks::register_callbacks(); b1::rodeos::unimplemented_callbacks::register_callbacks(); - b1::rodeos::query_callbacks::register_callbacks(); -// b1::rodeos::unimplemented_filter_callbacks::register_callbacks(); } } // namespace b1::rodeos::filter