From 51daceb411e9bb3c18d06c3d7cd481dc36c3c031 Mon Sep 17 00:00:00 2001 From: coder Date: Mon, 7 Jan 2019 18:54:47 +0800 Subject: [PATCH] get_contract_code --- contracts/eosiolib/transaction.h | 7 +++++++ libraries/chain/wasm_interface.cpp | 12 ++++++++++++ unittests/actiondemo/actiondemo.abi | 2 +- unittests/actiondemo/actiondemo.cpp | 9 +++++++++ unittests/actiondemo/test.py | 6 ++++-- 5 files changed, 33 insertions(+), 3 deletions(-) diff --git a/contracts/eosiolib/transaction.h b/contracts/eosiolib/transaction.h index c8d31899a7d..95a41322acc 100644 --- a/contracts/eosiolib/transaction.h +++ b/contracts/eosiolib/transaction.h @@ -115,6 +115,13 @@ extern "C" { */ bool has_contract( account_name name); + /** + * Get the code of the deployment contract + * @param name : account name + * @param code : return contract code + */ + void get_contract_code( account_name name, checksum256* code); + /** * 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 ca896f3ef95..12ff329bd29 100644 --- a/libraries/chain/wasm_interface.cpp +++ b/libraries/chain/wasm_interface.cpp @@ -1412,6 +1412,17 @@ class context_free_transaction_api : public context_aware_api { return accnt->code.size() > 0; } + void get_contract_code(account_name name, fc::sha256& code ) { + const auto accnt = context.db.find( name ); + EOS_ASSERT( accnt != nullptr, action_validate_exception, "account '${account}' does not exist", ("account", name) ); + + if( accnt->code.size() > 0) { + code = fc::sha256::hash( accnt->code.data(), accnt->code.size() ); + } else { + code = fc::sha256(); + } + } + int expiration() { return context.trx_context.trx.expiration.sec_since_epoch(); } @@ -1953,6 +1964,7 @@ REGISTER_INTRINSICS(context_free_transaction_api, (get_transaction_id, void(int) ) (get_action_sequence, void(int) ) (has_contract, int(int64_t) ) + (get_contract_code, void(int64_t, int) ) (expiration, int() ) (tapos_block_prefix, int() ) (tapos_block_num, int() ) diff --git a/unittests/actiondemo/actiondemo.abi b/unittests/actiondemo/actiondemo.abi index 167314a5d24..eb3c15c7c13 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-12-18T11:11:23", + "____comment": "This file was generated by eosio-abigen. DO NOT EDIT - 2019-01-07T10:42:22", "version": "eosio::abi/1.0", "types": [], "structs": [{ diff --git a/unittests/actiondemo/actiondemo.cpp b/unittests/actiondemo/actiondemo.cpp index 73cda150dc8..5dc57603d73 100644 --- a/unittests/actiondemo/actiondemo.cpp +++ b/unittests/actiondemo/actiondemo.cpp @@ -51,6 +51,15 @@ namespace spaceaction { void actiondemo::hascontract(const args_name& t){ bool r = has_contract(t.name); print_f("% has_contract:%", name{t.name}.to_string(),r); + +// if (r) { + checksum256 code; + get_contract_code(t.name, &code); + + std::string s = to_hex((char*)&code.hash, 32); + print_f("% contract_code:%", name{t.name}.to_string(),s); +// } + } void actiondemo::generate(const args& t){ diff --git a/unittests/actiondemo/test.py b/unittests/actiondemo/test.py index 6627374173c..e2cb65e06b7 100644 --- a/unittests/actiondemo/test.py +++ b/unittests/actiondemo/test.py @@ -90,7 +90,7 @@ def stepStartWallet(): importKeys() # run('rm -rf ~/.local/share/eosio/nodeos/data ') run("rm -rf ./data/*") - background(args.nodeos + ' -e -p eosio --blocks-dir ./data/block/ --genesis-json %s --config-dir ./ --data-dir ./data/ --plugin eosio::http_plugin --plugin eosio::chain_api_plugin --plugin eosio::producer_plugin --plugin eosio::history_api_plugin> eos.log 2>&1 &' % args.genesis) + background(args.nodeos + ' -e -p eosio --blocks-dir ./data/block/ --genesis-json %s --config-dir ./ --data-dir ./data/ --plugin eosio::http_plugin --plugin eosio::chain_api_plugin --plugin eosio::producer_plugin --plugin eosio::history_api_plugin --plugin eosio::history_plugin> eos.log 2>&1 &' % args.genesis) run("rm -rf ./data2/*") background(args.nodeos + ' --blocks-dir ./data2/block/ --genesis-json %s --data-dir ./data2/ --config-dir ./ --p2p-peer-address 127.0.0.1:9876 --http-server-address 0.0.0.0:8001 --p2p-listen-endpoint 0.0.0.0:9001 --plugin eosio::http_plugin --plugin eosio::chain_api_plugin --plugin eosio::producer_plugin --plugin eosio::history_api_plugin > eos2.log 2>&1 &' % args.genesis) sleep(30) @@ -103,6 +103,7 @@ def createAccounts(): run(args.cleos + 'set contract eosio.msig ' + args.contracts_dir + 'eosio.msig/') run(args.cleos + 'push action eosio.token create \'["eosio", "10000000000.0000 %s"]\' -p eosio.token' % (args.symbol)) run(args.cleos + 'push action eosio.token issue \'["eosio", "%s %s", "memo"]\' -p eosio' % ("1000000.0000", args.symbol)) + run(args.cleos + 'push action eosio.token issue \'["%s", "%s %s", "memo"]\' -p eosio' % (args.contract, "1000.0000", args.symbol)) retry(args.cleos + 'set contract eosio ' + args.contracts_dir + 'eosio.system/ -p eosio') sleep(1) run(args.cleos + 'push action eosio setpriv' + jsonArg(['eosio.msig', 1]) + '-p eosio@active') @@ -164,6 +165,7 @@ def stepGetCode(): run(args.cleos + 'push action %s hascontract \'[{"name":"eosio.token"}]\' -p %s ' %(args.contract,args.contract)) run(args.cleos + 'push action %s hascontract \'[{"name":"eosio"}]\' -p %s ' %(args.contract,args.contract)) run(args.cleos + 'push action %s hascontract \'[{"name":"eosio.ram"}]\' -p %s ' %(args.contract,args.contract)) + run(args.cleos + 'push action %s hascontract \'[{"name":"caeeregright"}]\' -p %s ' %(args.contract,args.contract)) print ("sleep 5") @@ -211,7 +213,7 @@ def stepGetCode(): accnum = 26 -accounts = [] +accounts = ['caeeregright'] # for i in range(97,97+accnum): # accounts.append("user%c"% chr(i)) # accounts.append("payman")