Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Rodeos filter current_time() 📦 #10582

Merged
merged 2 commits into from
Jul 28, 2021
Merged
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
40 changes: 40 additions & 0 deletions libraries/rodeos/include/b1/rodeos/callbacks/system.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#pragma once

#include <b1/rodeos/callbacks/basic.hpp>
#include <b1/rodeos/get_state_row.hpp>

namespace b1::rodeos {

// for filter callback. query callback for current_time is in query.hpp

template <typename Derived>
struct system_callbacks {
Derived& derived() { return static_cast<Derived&>(*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 <typename Rft>
static void register_callbacks() {
// todo: preconditions
RODEOS_REGISTER_CALLBACK(Rft, Derived, current_time);
}
}; // system_callbacks

} // namespace b1::rodeos
Original file line number Diff line number Diff line change
Expand Up @@ -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<int64_t>("current_time"); }

template <typename Rft>
static void register_callbacks() {
// todo: preconditions
RODEOS_REGISTER_CALLBACK(Rft, Derived, current_time);
}
}; // unimplemented_filter_callbacks

} // namespace b1::rodeos
5 changes: 3 additions & 2 deletions libraries/rodeos/include/b1/rodeos/filter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <b1/rodeos/callbacks/crypto.hpp>
#include <b1/rodeos/callbacks/filter.hpp>
#include <b1/rodeos/callbacks/memory.hpp>
#include <b1/rodeos/callbacks/system.hpp>
#include <b1/rodeos/callbacks/unimplemented.hpp>
#include <b1/rodeos/callbacks/unimplemented_filter.hpp>
#ifdef EOSIO_EOS_VM_OC_RUNTIME_ENABLED
Expand Down Expand Up @@ -64,7 +65,7 @@ struct callbacks : b1::rodeos::chaindb_callbacks<callbacks>,
b1::rodeos::filter_callbacks<callbacks>,
b1::rodeos::memory_callbacks<callbacks>,
b1::rodeos::unimplemented_callbacks<callbacks>,
b1::rodeos::unimplemented_filter_callbacks<callbacks> {
b1::rodeos::system_callbacks<callbacks> {
filter::filter_state& filter_state;
b1::rodeos::chaindb_state& chaindb_state;
b1::rodeos::db_view_state& db_view_state;
Expand All @@ -89,8 +90,8 @@ inline void register_callbacks() {
b1::rodeos::db_callbacks<callbacks>::register_callbacks<rhf_t>();
b1::rodeos::filter_callbacks<callbacks>::register_callbacks<rhf_t>();
b1::rodeos::memory_callbacks<callbacks>::register_callbacks<rhf_t>();
b1::rodeos::system_callbacks<callbacks>::register_callbacks<rhf_t>();
b1::rodeos::unimplemented_callbacks<callbacks>::register_callbacks<rhf_t>();
b1::rodeos::unimplemented_filter_callbacks<callbacks>::register_callbacks<rhf_t>();
}

} // namespace b1::rodeos::filter