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

Commit

Permalink
Merge pull request #134 from iamaprotoss/master
Browse files Browse the repository at this point in the history
merged apply, precondition and validate contexts into apply_context
  • Loading branch information
bytemaster authored Aug 3, 2017
2 parents c35b0bd + 4e7655e commit 7d9acda
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 93 deletions.
102 changes: 34 additions & 68 deletions libraries/chain/include/eos/chain/message_handling_contexts.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,98 +9,64 @@ namespace chainbase { class database; }
namespace eos { namespace chain {

class chain_controller;
class message_validate_context {
public:
explicit message_validate_context(const chain_controller& control,
const chainbase::database& d,
const chain::Transaction& t,
const chain::Message& m,
types::AccountName c,
TransactionAuthorizationChecker* authChecker)
:controller(control),db(d),trx(t),msg(m),code(c),authChecker(authChecker){}

/**
* @brief Require @ref account to have approved of this message
* @param account The account whose approval is required
*
* This method will check that @ref account is listed in the message's declared authorizations, and marks the
* authorization as used. Note that all authorizations on a message must be used, or the message is invalid.
*
* @throws tx_missing_auth If no sufficient permission was found
*/
void require_authorization(const types::AccountName& account);
void require_scope(const types::AccountName& account)const;

const chain_controller& controller;
const chainbase::database& db; ///< database where state is stored
const chain::Transaction& trx; ///< used to gather the valid read/write scopes
const chain::Message& msg; ///< message being applied
types::AccountName code; ///< the code that is currently running

TransactionAuthorizationChecker* authChecker;


int32_t load_i64( Name scope, Name code, Name table, Name Key, char* data, uint32_t maxlen );


int32_t front_primary_i128i128( Name scope, Name code, Name table,
uint128_t* primary, uint128_t* secondary, char* data, uint32_t maxlen );
int32_t back_primary_i128i128( Name scope, Name code, Name table,
uint128_t* primary, uint128_t* secondary, char* data, uint32_t maxlen );
int32_t front_secondary_i128i128( Name scope, Name code, Name table,
uint128_t* primary, uint128_t* secondary, char* data, uint32_t maxlen );
int32_t back_secondary_i128i128( Name scope, Name code, Name table,
uint128_t* primary, uint128_t* secondary, char* data, uint32_t maxlen );
int32_t load_primary_i128i128( Name scope, Name code, Name table,
uint128_t* primary, uint128_t* secondary, char* data, uint32_t maxlen );
int32_t load_secondary_i128i128( Name scope, Name code, Name table,
uint128_t* primary, uint128_t* secondary, char* data, uint32_t maxlen );
int32_t lowerbound_primary_i128i128( Name scope, Name code, Name table,
uint128_t* primary, uint128_t* secondary, char* data, uint32_t maxlen );
int32_t lowerbound_secondary_i128i128( Name scope, Name code, Name table,
uint128_t* primary, uint128_t* secondary, char* data, uint32_t maxlen );
};

class precondition_validate_context : public message_validate_context {
public:
precondition_validate_context(const chain_controller& con,
const chainbase::database& db,
const chain::Transaction& t,
const chain::Message& m,
const types::AccountName& code,
TransactionAuthorizationChecker* authChecker)
:message_validate_context(con, db, t, m, code, authChecker){}
};

class apply_context : public precondition_validate_context {
class apply_context {
public:
apply_context(chain_controller& con,
chainbase::database& db,
const chain::Transaction& t,
const chain::Message& m,
const types::AccountName& code,
TransactionAuthorizationChecker* authChecker)
:precondition_validate_context(con,db,t,m,code,authChecker),mutable_controller(con),mutable_db(db){}
:controller(con),db(db),trx(t),msg(m),code(code),mutable_controller(con),mutable_db(db),authChecker(authChecker){}

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 );
int32_t remove_i128i128( Name scope, Name table, uint128_t primary, uint128_t secondary );
int32_t store_i128i128( Name scope, Name table, uint128_t primary, uint128_t secondary,
const char* data, uint32_t len );

int32_t load_i64( Name scope, Name code, Name table, Name Key, char* data, uint32_t maxlen );

int32_t front_primary_i128i128( Name scope, Name code, Name table,
uint128_t* primary, uint128_t* secondary, char* data, uint32_t maxlen );
int32_t back_primary_i128i128( Name scope, Name code, Name table,
uint128_t* primary, uint128_t* secondary, char* data, uint32_t maxlen );
int32_t front_secondary_i128i128( Name scope, Name code, Name table,
uint128_t* primary, uint128_t* secondary, char* data, uint32_t maxlen );
int32_t back_secondary_i128i128( Name scope, Name code, Name table,
uint128_t* primary, uint128_t* secondary, char* data, uint32_t maxlen );
int32_t load_primary_i128i128( Name scope, Name code, Name table,
uint128_t* primary, uint128_t* secondary, char* data, uint32_t maxlen );
int32_t load_secondary_i128i128( Name scope, Name code, Name table,
uint128_t* primary, uint128_t* secondary, char* data, uint32_t maxlen );
int32_t lowerbound_primary_i128i128( Name scope, Name code, Name table,
uint128_t* primary, uint128_t* secondary, char* data, uint32_t maxlen );
int32_t lowerbound_secondary_i128i128( Name scope, Name code, Name table,
uint128_t* primary, uint128_t* secondary, char* data, uint32_t maxlen );

void require_authorization(const types::AccountName& account);
void require_scope(const types::AccountName& account)const;
bool has_recipient( const types::AccountName& account )const;
void require_recipient(const types::AccountName& account);

const chain_controller& controller;
const chainbase::database& db; ///< database where state is stored
const chain::Transaction& trx; ///< used to gather the valid read/write scopes
const chain::Message& msg; ///< message being applied
types::AccountName code; ///< the code that is currently running

chain_controller& mutable_controller;
chainbase::database& mutable_db;

TransactionAuthorizationChecker* authChecker;

std::deque<AccountName> notified;
std::deque<ProcessedTransaction> sync_transactions; ///< sync calls made
std::deque<GeneratedTransaction> async_transactions; ///< async calls requested

chain_controller& mutable_controller;
chainbase::database& mutable_db;
};

using message_validate_handler = std::function<void(message_validate_context&)>;
using precondition_validate_handler = std::function<void(precondition_validate_context&)>;
using apply_handler = std::function<void(apply_context&)>;

} } // namespace eos::chain
12 changes: 6 additions & 6 deletions libraries/chain/include/eos/chain/wasm_interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
namespace eos { namespace chain {

class chain_controller;
typedef int32_t (message_validate_context::*load_i128i128_fnc)(Name, Name, Name, uint128_t* , uint128_t*, char* , uint32_t);
typedef int32_t (apply_context::*load_i128i128_fnc)(Name, Name, Name, uint128_t* , uint128_t*, char* , uint32_t);

/**
* @class wasm_interface
Expand All @@ -33,14 +33,14 @@ class wasm_interface {

void init( apply_context& c );
void apply( apply_context& c );
void validate( message_validate_context& c );
void precondition( precondition_validate_context& c );
void validate( apply_context& c );
void precondition( apply_context& c );

int64_t current_execution_time();

apply_context* current_apply_context = nullptr;
message_validate_context* current_validate_context = nullptr;
precondition_validate_context* current_precondition_context = nullptr;
apply_context* current_apply_context = nullptr;
apply_context* current_validate_context = nullptr;
apply_context* current_precondition_context = nullptr;

Runtime::MemoryInstance* current_memory = nullptr;
Runtime::ModuleInstance* current_module = nullptr;
Expand Down
22 changes: 11 additions & 11 deletions libraries/chain/message_handling_contexts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@

namespace eos { namespace chain {

void message_validate_context::require_authorization(const types::AccountName& account) {
void apply_context::require_authorization(const types::AccountName& account) {
#warning TODO: Look up the permission_object that account has specified to use for this message type
if (authChecker)
EOS_ASSERT(authChecker->requirePermission({account, "active"}), tx_missing_auth,
"Transaction does not declare required authority '${auth}'", ("auth", account));
}

void message_validate_context::require_scope(const types::AccountName& account)const {
void apply_context::require_scope(const types::AccountName& account)const {
auto itr = boost::find_if(trx.scope, [&account](const auto& scope) {
return scope == account;
});
Expand All @@ -43,7 +43,7 @@ void apply_context::require_recipient(const types::AccountName& account) {
}
}

int32_t message_validate_context::load_i64( Name scope, Name code, Name table, Name key, char* value, uint32_t valuelen ) {
int32_t apply_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(
Expand All @@ -59,7 +59,7 @@ int32_t message_validate_context::load_i64( Name scope, Name code, Name table, N
return copylen;
}

int32_t message_validate_context::back_primary_i128i128( Name scope, Name code, Name table,
int32_t apply_context::back_primary_i128i128( Name scope, Name code, Name table,
uint128_t* primary, uint128_t* secondary, char* value, uint32_t valuelen ) {

return -1;
Expand Down Expand Up @@ -92,7 +92,7 @@ int32_t message_validate_context::back_primary_i128i128( Name scope, Name code,
*/
}

int32_t message_validate_context::back_secondary_i128i128( Name scope, Name code, Name table,
int32_t apply_context::back_secondary_i128i128( Name scope, Name code, Name table,
uint128_t* primary, uint128_t* secondary, char* value, uint32_t valuelen ) {

require_scope( scope );
Expand Down Expand Up @@ -123,7 +123,7 @@ int32_t message_validate_context::back_secondary_i128i128( Name scope, Name code
}


int32_t message_validate_context::front_primary_i128i128( Name scope, Name code, Name table,
int32_t apply_context::front_primary_i128i128( Name scope, Name code, Name table,
uint128_t* primary, uint128_t* secondary, char* value, uint32_t valuelen ) {

require_scope( scope );
Expand Down Expand Up @@ -152,7 +152,7 @@ int32_t message_validate_context::front_primary_i128i128( Name scope, Name code,
}
return copylen;
}
int32_t message_validate_context::front_secondary_i128i128( Name scope, Name code, Name table,
int32_t apply_context::front_secondary_i128i128( Name scope, Name code, Name table,
uint128_t* primary, uint128_t* secondary, char* value, uint32_t valuelen ) {

require_scope( scope );
Expand Down Expand Up @@ -186,7 +186,7 @@ int32_t message_validate_context::front_secondary_i128i128( Name scope, Name cod
}


int32_t message_validate_context::load_primary_i128i128( Name scope, Name code, Name table,
int32_t apply_context::load_primary_i128i128( Name scope, Name code, Name table,
uint128_t* primary, uint128_t* secondary, char* value, uint32_t valuelen ) {

require_scope( scope );
Expand All @@ -211,7 +211,7 @@ int32_t message_validate_context::load_primary_i128i128( Name scope, Name code,
return copylen;
}

int32_t message_validate_context::load_secondary_i128i128( Name scope, Name code, Name table,
int32_t apply_context::load_secondary_i128i128( Name scope, Name code, Name table,
uint128_t* primary, uint128_t* secondary, char* value, uint32_t valuelen ) {

require_scope( scope );
Expand All @@ -236,7 +236,7 @@ int32_t message_validate_context::load_secondary_i128i128( Name scope, Name code
return copylen;
}

int32_t message_validate_context::lowerbound_primary_i128i128( Name scope, Name code, Name table,
int32_t apply_context::lowerbound_primary_i128i128( Name scope, Name code, Name table,
uint128_t* primary, uint128_t* secondary, char* value, uint32_t valuelen ) {

require_scope( scope );
Expand All @@ -261,7 +261,7 @@ int32_t message_validate_context::lowerbound_primary_i128i128( Name scope, Name
return copylen;
}

int32_t message_validate_context::lowerbound_secondary_i128i128( Name scope, Name code, Name table,
int32_t apply_context::lowerbound_secondary_i128i128( Name scope, Name code, Name table,
uint128_t* primary, uint128_t* secondary, char* value, uint32_t valuelen ) {

require_scope( scope );
Expand Down
16 changes: 8 additions & 8 deletions libraries/chain/wasm_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,27 +139,27 @@ DEFINE_INTRINSIC_FUNCTION3(env,remove_i128i128,remove_i128i128,i32,i64,scope,i64
}

DEFINE_INTRINSIC_FUNCTION5(env,load_primary_i128i128,load_primary_i128i128,i32,i64,scope,i64,code,i64,table,i32,valueptr,i32,valuelen) {
return load_i128i128_object(scope, code, table, valueptr, valuelen, &message_validate_context::load_primary_i128i128);
return load_i128i128_object(scope, code, table, valueptr, valuelen, &apply_context::load_primary_i128i128);
}

DEFINE_INTRINSIC_FUNCTION5(env,load_secondary_i128i128,load_secondary_i128i128,i32,i64,scope,i64,code,i64,table,i32,valueptr,i32,valuelen) {
return load_i128i128_object(scope, code, table, valueptr, valuelen, &message_validate_context::load_secondary_i128i128);
return load_i128i128_object(scope, code, table, valueptr, valuelen, &apply_context::load_secondary_i128i128);
}

DEFINE_INTRINSIC_FUNCTION5(env,back_primary_i128i128,back_primary_i128i128,i32,i64,scope,i64,code,i64,table,i32,valueptr,i32,valuelen) {
return load_i128i128_object(scope, code, table, valueptr, valuelen, &message_validate_context::back_primary_i128i128);
return load_i128i128_object(scope, code, table, valueptr, valuelen, &apply_context::back_primary_i128i128);
}

DEFINE_INTRINSIC_FUNCTION5(env,front_primary_i128i128,front_primary_i128i128,i32,i64,scope,i64,code,i64,table,i32,valueptr,i32,valuelen) {
return load_i128i128_object(scope, code, table, valueptr, valuelen, &message_validate_context::front_primary_i128i128);
return load_i128i128_object(scope, code, table, valueptr, valuelen, &apply_context::front_primary_i128i128);
}

DEFINE_INTRINSIC_FUNCTION5(env,back_secondary_i128i128,back_secondary_i128i128,i32,i64,scope,i64,code,i64,table,i32,valueptr,i32,valuelen) {
return load_i128i128_object(scope, code, table, valueptr, valuelen, &message_validate_context::back_secondary_i128i128);
return load_i128i128_object(scope, code, table, valueptr, valuelen, &apply_context::back_secondary_i128i128);
}

DEFINE_INTRINSIC_FUNCTION5(env,front_secondary_i128i128,front_secondary_i128i128,i32,i64,scope,i64,code,i64,table,i32,valueptr,i32,valuelen) {
return load_i128i128_object(scope, code, table, valueptr, valuelen, &message_validate_context::front_secondary_i128i128);
return load_i128i128_object(scope, code, table, valueptr, valuelen, &apply_context::front_secondary_i128i128);
}

DEFINE_INTRINSIC_FUNCTION0(env,currentCode,currentCode,i64) {
Expand Down Expand Up @@ -458,7 +458,7 @@ DEFINE_INTRINSIC_FUNCTION1(env,free,free,none,i32,ptr) {
}
} FC_CAPTURE_AND_RETHROW() }

void wasm_interface::validate( message_validate_context& c ) {
void wasm_interface::validate( apply_context& c ) {
/*
current_validate_context = &c;
current_precondition_context = nullptr;
Expand All @@ -468,7 +468,7 @@ DEFINE_INTRINSIC_FUNCTION1(env,free,free,none,i32,ptr) {
vm_validate();
*/
}
void wasm_interface::precondition( precondition_validate_context& c ) {
void wasm_interface::precondition( apply_context& c ) {
try {

/*
Expand Down

0 comments on commit 7d9acda

Please sign in to comment.