diff --git a/contracts/eosiolib/transaction.h b/contracts/eosiolib/transaction.h index db115ca27e1..d84e4dbd604 100644 --- a/contracts/eosiolib/transaction.h +++ b/contracts/eosiolib/transaction.h @@ -108,6 +108,13 @@ extern "C" { */ void get_action_sequence(uint64_t* seq); + /** + * Get the code_hash of the account + * @param name : account name + * @return : Return has code + */ + bool has_contract( account_name name); + /** * Get the producer's signature for the action * @param sig : Memory buffer diff --git a/libraries/chain/wasm_interface.cpp b/libraries/chain/wasm_interface.cpp index f63d836d377..ca896f3ef95 100644 --- a/libraries/chain/wasm_interface.cpp +++ b/libraries/chain/wasm_interface.cpp @@ -1406,6 +1406,12 @@ class context_free_transaction_api : public context_aware_api { seq = context.global_action_sequence; } + bool has_contract(account_name name){ + const auto accnt = context.db.find( name ); + EOS_ASSERT( accnt != nullptr, action_validate_exception, "account '${account}' does not exist", ("account", name) ); + return accnt->code.size() > 0; + } + int expiration() { return context.trx_context.trx.expiration.sec_since_epoch(); } @@ -1946,6 +1952,7 @@ REGISTER_INTRINSICS(context_free_transaction_api, (transaction_size, int() ) (get_transaction_id, void(int) ) (get_action_sequence, void(int) ) + (has_contract, int(int64_t) ) (expiration, int() ) (tapos_block_prefix, int() ) (tapos_block_num, int() ) diff --git a/unittests/actiondemo/actiondemo.abi b/unittests/actiondemo/actiondemo.abi index 172d180bfef..b2504bd4fd9 100644 --- a/unittests/actiondemo/actiondemo.abi +++ b/unittests/actiondemo/actiondemo.abi @@ -1,5 +1,5 @@ { - "____comment": "This file was generated by eosio-abigen. DO NOT EDIT - 2018-11-17T13:26:02", + "____comment": "This file was generated by eosio-abigen. DO NOT EDIT - 2018-12-18T07:34:45", "version": "eosio::abi/1.0", "types": [], "structs": [{ @@ -45,6 +45,22 @@ "name": "clear", "base": "", "fields": [] + },{ + "name": "args_name", + "base": "", + "fields": [{ + "name": "name", + "type": "name" + } + ] + },{ + "name": "printcode", + "base": "", + "fields": [{ + "name": "t", + "type": "args_name" + } + ] },{ "name": "args_inline", "base": "", @@ -74,6 +90,10 @@ "name": "clear", "type": "clear", "ricardian_contract": "" + },{ + "name": "printcode", + "type": "printcode", + "ricardian_contract": "" },{ "name": "inlineact", "type": "inlineact", diff --git a/unittests/actiondemo/actiondemo.cpp b/unittests/actiondemo/actiondemo.cpp index 3f8a3fcb6e0..3e596af6571 100644 --- a/unittests/actiondemo/actiondemo.cpp +++ b/unittests/actiondemo/actiondemo.cpp @@ -1,7 +1,7 @@ #include "actiondemo.hpp" #include "../../contracts/eosiolib/print.hpp" #include "../../contracts/eosiolib/types.hpp" -#include "../../contracts/eosiolib/transaction.hpp" +#include "../../contracts/eosiolib/transaction.h" namespace spaceaction { @@ -19,6 +19,9 @@ namespace spaceaction { case N(clear): clear(); return; + case N(printcode): + printcode(unpack_action_data()); + return; } } @@ -33,6 +36,8 @@ namespace spaceaction { } } + + std::string to_hex( const char* d, uint32_t s ) { std::string r; @@ -43,6 +48,11 @@ namespace spaceaction { return r; } + void actiondemo::printcode(const args_name& t){ + bool r = has_contract(t.name); + print_f("% code hash:%", name{t.name}.to_string(),r); + } + void actiondemo::generate(const args& t){ for (int i = 0; i < t.loop; ++i) { transaction_id_type txid; diff --git a/unittests/actiondemo/actiondemo.hpp b/unittests/actiondemo/actiondemo.hpp index e1d5031bfa5..626ff588b66 100644 --- a/unittests/actiondemo/actiondemo.hpp +++ b/unittests/actiondemo/actiondemo.hpp @@ -22,6 +22,12 @@ namespace spaceaction { //@abi action void clear(); + struct args_name{ + account_name name; + }; + //@abi action + void printcode(const args_name& t); + struct args_inline{ account_name payer; diff --git a/unittests/actiondemo/test.py b/unittests/actiondemo/test.py index 5ced2b4276c..a03fccaaa30 100644 --- a/unittests/actiondemo/test.py +++ b/unittests/actiondemo/test.py @@ -159,6 +159,13 @@ def stepGenerate(): run(args.cleos + 'get table %s %s seedobjs' %(args.contract2, args.contract2) ) print ("sleep 5") +def stepGetCode(): + print ("=========================== set stepGetCode ===========================" ) + run(args.cleos + 'push action %s printcode \'[{"name":"eosio.token"}]\' -p %s ' %(args.contract,args.contract)) + run(args.cleos + 'push action %s printcode \'[{"name":"eosio"}]\' -p %s ' %(args.contract,args.contract)) + run(args.cleos + 'push action %s printcode \'[{"name":"eosio.ram"}]\' -p %s ' %(args.contract,args.contract)) + print ("sleep 5") + parser = argparse.ArgumentParser() @@ -169,6 +176,7 @@ def stepGenerate(): ('i', 'init', stepInitCaee, True, "stepInitCaee"), ('c', 'clear', stepClear, True, "stepInitCaee"), ('g', 'generate', stepGenerate, True, "stepInitCaee"), + ('d', 'getcode', stepGetCode, True, "stepGetCode"), ] parser.add_argument('--public-key', metavar='', help="EOSIO Public Key", default='EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV', dest="public_key")