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

Commit

Permalink
refactor data access from wasm_interface to handler context
Browse files Browse the repository at this point in the history
  • Loading branch information
bytemaster committed Jul 10, 2017
1 parent 84b7518 commit ba22200
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 118 deletions.
35 changes: 0 additions & 35 deletions libraries/chain/chain_controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,41 +57,6 @@
namespace eos { namespace chain {


String apply_context::get( String key )const {
/*
const auto& obj = db.get<key_value_object,by_scope_key>( 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<key_value_object,by_scope_key>( 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<key_value_object>( [&](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<key_value_object,by_scope_key>( 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool> used_authorizations;
};
Expand All @@ -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<eos::chain::generated_transaction> applied; ///< sync calls made
std::deque<eos::chain::generated_transaction> generated; ///< async calls requested
Expand Down
57 changes: 57 additions & 0 deletions libraries/chain/message_handling_contexts.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <eos/chain/message_handling_contexts.hpp>
#include <eos/chain/permission_object.hpp>
#include <eos/chain/exceptions.hpp>
#include <eos/chain/key_value_object.hpp>

#include <boost/algorithm/cxx11/all_of.hpp>
#include <boost/range/algorithm/find_if.hpp>
Expand Down Expand Up @@ -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<key_value_object,by_scope_key>( boost::make_tuple(
AccountName(scope),
AccountName(code),
AccountName(table),
AccountName(key) ) );
if( obj == nullptr ) { return -1; }
auto copylen = std::min<size_t>(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<key_value_object,by_scope_key>( 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<key_value_object,by_scope_key>( 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<key_value_object>( [&](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
86 changes: 6 additions & 80 deletions libraries/chain/wasm_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -38,108 +34,38 @@ 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<char>( mem, valueptr, valuelen);

//idump((Name(scope))(Name(code))(Name(table))(Name(key))(valuelen) );
const auto* obj = db.find<key_value_object,by_scope_key>( 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<key_value_object>( [&](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) );

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<char>( mem, valueptr, valuelen);


const auto* obj = db.find<key_value_object,by_scope_key>( boost::make_tuple(
AccountName(scope),
AccountName(code),
AccountName(table),
AccountName(key) ) );
if( obj == nullptr ) {
wlog( "not found" );
return -1;
}
auto copylen = std::min<size_t>(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<const char>( 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<char>( mem, keyptr, keylen);
string keystr( key, key+keylen);
const auto* obj = db.find<key_value_object,by_scope_key>( 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) {
Expand Down

0 comments on commit ba22200

Please sign in to comment.