From ba222004b61508975cd721d1858feca8177e3b62 Mon Sep 17 00:00:00 2001 From: Daniel Larimer Date: Mon, 10 Jul 2017 17:35:01 -0400 Subject: [PATCH] refactor data access from wasm_interface to handler context --- libraries/chain/chain_controller.cpp | 35 -------- .../eos/chain/message_handling_contexts.hpp | 8 +- libraries/chain/message_handling_contexts.cpp | 57 ++++++++++++ libraries/chain/wasm_interface.cpp | 86 ++----------------- 4 files changed, 68 insertions(+), 118 deletions(-) diff --git a/libraries/chain/chain_controller.cpp b/libraries/chain/chain_controller.cpp index 30c0d26ce25..d63f3e118e4 100644 --- a/libraries/chain/chain_controller.cpp +++ b/libraries/chain/chain_controller.cpp @@ -57,41 +57,6 @@ namespace eos { namespace chain { -String apply_context::get( String key )const { - /* - const auto& obj = db.get( boost::make_tuple(scope, key) ); - return String(obj.value.begin(),obj.value.end()); - */ - return String(); -} -void apply_context::set( String key, String value ) { - /* - const auto* obj = db.find( boost::make_tuple(scope, key) ); - if( obj ) { - mutable_db.modify( *obj, [&]( auto& o ) { - o.value.resize( value.size() ); - // memcpy( o.value.data(), value.data(), value.size() ); - }); - } else { - mutable_db.create( [&](auto& o) { - o.scope = scope; - o.key.insert( 0, key.data(), key.size() ); - o.value.insert( 0, value.data(), value.size() ); - }); - } - */ -} -void apply_context::remove( String key ) { - /* - const auto* obj = db.find( boost::make_tuple(scope, key) ); - if( obj ) { - mutable_db.remove( *obj ); - } - */ -} - - - bool chain_controller::is_known_block(const block_id_type& id)const { return _fork_db.is_known_block(id) || _block_log.read_block_by_id(id); diff --git a/libraries/chain/include/eos/chain/message_handling_contexts.hpp b/libraries/chain/include/eos/chain/message_handling_contexts.hpp index 87eca7eeb86..b51b07763bf 100644 --- a/libraries/chain/include/eos/chain/message_handling_contexts.hpp +++ b/libraries/chain/include/eos/chain/message_handling_contexts.hpp @@ -35,6 +35,9 @@ class message_validate_context { const chain::Message& msg; ///< message being applied types::AccountName code; ///< the code that is currently running + + int32_t load_i64( Name scope, Name code, Name table, Name Key, char* data, uint32_t maxlen ); + ///< Parallel to msg.authorization; tracks which permissions have been used while processing the message vector used_authorizations; }; @@ -56,9 +59,8 @@ class apply_context : public precondition_validate_context { const types::AccountName& code) :precondition_validate_context(db,t,m,code),mutable_db(db){} - types::String get(types::String key)const; - void set(types::String key, types::String value); - void remove(types::String key); + int32_t store_i64( Name scope, Name table, Name key, const char* data, uint32_t len); + int32_t remove_i64( Name scope, Name table, Name key ); std::deque applied; ///< sync calls made std::deque generated; ///< async calls requested diff --git a/libraries/chain/message_handling_contexts.cpp b/libraries/chain/message_handling_contexts.cpp index d8843eb6bcd..be853caf73f 100644 --- a/libraries/chain/message_handling_contexts.cpp +++ b/libraries/chain/message_handling_contexts.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include #include @@ -41,4 +42,60 @@ bool message_validate_context::all_authorizations_used() const { return boost::algorithm::all_of_equal(used_authorizations, true); } +int32_t message_validate_context::load_i64( Name scope, Name code, Name table, Name key, char* value, uint32_t valuelen ) { + require_scope( scope ); + + const auto* obj = db.find( boost::make_tuple( + AccountName(scope), + AccountName(code), + AccountName(table), + AccountName(key) ) ); + if( obj == nullptr ) { return -1; } + auto copylen = std::min(obj->value.size(),valuelen); + if( copylen ) { + obj->value.copy(value, copylen); + } + return copylen; +} + +int32_t apply_context::remove_i64( Name scope, Name table, Name key ) { + require_scope( scope ); + + const auto* obj = db.find( boost::make_tuple( + AccountName(scope), + AccountName(code), + AccountName(table), + AccountName(key) ) ); + if( obj ) { + mutable_db.remove( *obj ); + return 1; + } + return 0; +} + +int32_t apply_context::store_i64( Name scope, Name table, Name key, const char* value, uint32_t valuelen ) { + require_scope( scope ); + + const auto* obj = db.find( boost::make_tuple( + AccountName(scope), + AccountName(code), + AccountName(table), + AccountName(key) ) ); + if( obj ) { + mutable_db.modify( *obj, [&]( auto& o ) { + o.value.assign(value, valuelen); + }); + return 0; + } else { + mutable_db.create( [&](auto& o) { + o.scope = scope; + o.code = code; + o.table = table; + o.key = key; + o.value.insert( 0, value, valuelen ); + }); + return 1; + } +} + } } // namespace eos::chain diff --git a/libraries/chain/wasm_interface.cpp b/libraries/chain/wasm_interface.cpp index efbf955a09d..dabbb733171 100644 --- a/libraries/chain/wasm_interface.cpp +++ b/libraries/chain/wasm_interface.cpp @@ -23,11 +23,7 @@ DEFINE_INTRINSIC_FUNCTION0(env,currentCode,currentCode,i64) { } DEFINE_INTRINSIC_FUNCTION1(env,requireAuth,requireAuth,none,i64,account) { - auto& wasm = wasm_interface::get(); - const auto& msg = wasm.current_validate_context->msg; - for( const auto& perm : msg.authorization ) - if( perm.account == Name(account) ) return; - FC_ASSERT( !"missing authorization", "expected authorization ${auth}", ("auth",Name(account)) ); + wasm_interface::get().current_validate_context->require_authorization( Name(account) ); } DEFINE_INTRINSIC_FUNCTION1(env,requireNotice,requireNotice,none,i64,account) { @@ -38,48 +34,19 @@ DEFINE_INTRINSIC_FUNCTION1(env,requireScope,requireScope,none,i64,scope) { wasm_interface::get().current_validate_context->require_scope( scope ); } -DEFINE_INTRINSIC_FUNCTION3(env,remove_i64,remove_i64,i32,i64,scope,i64,table,i64,key) -{ - return 0; -} - DEFINE_INTRINSIC_FUNCTION5(env,store_i64,store_i64,i32,i64,scope,i64,table,i64,key,i32,valueptr,i32,valuelen) { - FC_ASSERT( valuelen >= 0 ); - auto& wasm = wasm_interface::get(); - wasm.current_validate_context->require_scope( scope ); - FC_ASSERT( wasm.current_apply_context, "no apply context found" ); - auto& db = wasm.current_apply_context->mutable_db; - auto& code = wasm.current_apply_context->code; - auto mem = wasm.current_memory; char* value = memoryArrayPtr( mem, valueptr, valuelen); //idump((Name(scope))(Name(code))(Name(table))(Name(key))(valuelen) ); - const auto* obj = db.find( boost::make_tuple( - AccountName(scope), - AccountName(code), - AccountName(table), - AccountName(key) ) ); - if( obj ) { - db.modify( *obj, [&]( auto& o ) { - o.value.assign(value, valuelen); - }); - } else { - db.create( [&](auto& o) { - o.scope = scope; - o.code = code; - o.table = table; - o.key = key; - o.value.insert( 0, value, valuelen ); - }); - } - return 0; + return wasm.current_apply_context->store_i64( scope, table, key, value, valuelen ); } + DEFINE_INTRINSIC_FUNCTION6(env,load_i64,load_i64,i32,i64,scope,i64,code,i64,table,i64,key,i32,valueptr,i32,valuelen) { //idump((Name(scope))(Name(code))(Name(table))(Name(key))(valuelen) ); @@ -87,59 +54,18 @@ DEFINE_INTRINSIC_FUNCTION6(env,load_i64,load_i64,i32,i64,scope,i64,code,i64,tabl FC_ASSERT( valuelen >= 0 ); auto& wasm = wasm_interface::get(); - FC_ASSERT( wasm.current_validate_context, "no apply context found" ); - auto& db = wasm.current_apply_context->mutable_db; auto mem = wasm.current_memory; char* value = memoryArrayPtr( mem, valueptr, valuelen); - - const auto* obj = db.find( boost::make_tuple( - AccountName(scope), - AccountName(code), - AccountName(table), - AccountName(key) ) ); - if( obj == nullptr ) { - wlog( "not found" ); - return -1; - } - auto copylen = std::min(obj->value.size(),valuelen); - if( copylen ) { - obj->value.copy(value, copylen); - } - return copylen; + return wasm.current_validate_context->load_i64( scope, code, table, key, value, valuelen ); } - -DEFINE_INTRINSIC_FUNCTION1(env,name_to_int64,name_to_int64,i64,i32,cstr) { +DEFINE_INTRINSIC_FUNCTION3(env,remove_i64,remove_i64,i32,i64,scope,i64,table,i64,key) { auto& wasm = wasm_interface::get(); - auto mem = wasm.current_memory; - const char* str = memoryArrayPtr( mem, cstr, 13); - return Name(str).value; -} - -DEFINE_INTRINSIC_FUNCTION2(env,remove,remove,i32,i32,keyptr,i32,keylen) { - /* - FC_ASSERT( keylen > 0 ); - - auto& wasm = wasm_interface::get(); - FC_ASSERT( wasm.current_apply_context, "no apply context found" ); - - auto& db = wasm.current_apply_context->mutable_db; - auto& scope = wasm.current_apply_context->scope; - auto mem = wasm.current_memory; - char* key = memoryArrayPtr( mem, keyptr, keylen); - string keystr( key, key+keylen); - - const auto* obj = db.find( boost::make_tuple(scope, keystr) ); - if( obj ) { - db.remove( *obj ); - return true; - } - */ - return false; + return wasm.current_apply_context->remove_i64( scope, table, key ); } DEFINE_INTRINSIC_FUNCTION3(env,memcpy,memcpy,i32,i32,dstp,i32,srcp,i32,len) {