Skip to content

Commit

Permalink
GH-260 Add is_code_cached method to wasm_interface and use in api_tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
heifner committed Dec 28, 2022
1 parent 7c534bf commit cafceaa
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 9 deletions.
3 changes: 3 additions & 0 deletions libraries/chain/include/eosio/chain/wasm_interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ namespace eosio { namespace chain {
//Immediately exits currently running wasm. UB is called when no wasm running
void exit();

//Returns true if the code is cached
size_t is_code_cached(const digest_type& code_hash, const uint8_t& vm_type, const uint8_t& vm_version) const;

// If substitute_apply is set, then apply calls it before doing anything else. If substitute_apply returns true,
// then apply returns immediately.
std::function<bool(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ namespace eosio { namespace chain {
});
}

size_t is_code_cached(const digest_type& code_hash, const uint8_t& vm_type, const uint8_t& vm_version) const {
wasm_cache_index::iterator it = wasm_instantiation_cache.find( boost::make_tuple(code_hash, vm_type, vm_version) );
return it != wasm_instantiation_cache.end();
}

std::vector<uint8_t> parse_initial_memory(const Module& module) {
std::vector<uint8_t> mem_image;

Expand Down
4 changes: 4 additions & 0 deletions libraries/chain/wasm_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ namespace eosio { namespace chain {
my->runtime_interface->immediately_exit_currently_running_module();
}

size_t wasm_interface::is_code_cached(const digest_type& code_hash, const uint8_t& vm_type, const uint8_t& vm_version) const {
return my->is_code_cached(code_hash, vm_type, vm_version);
}

wasm_instantiated_module_interface::~wasm_instantiated_module_interface() {}
wasm_runtime_interface::~wasm_runtime_interface() {}

Expand Down
2 changes: 2 additions & 0 deletions libraries/testing/include/eosio/testing/tester.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,8 @@ namespace eosio { namespace testing {
void set_code( account_name name, const vector<uint8_t> wasm, const private_key_type* signer = nullptr );
void set_abi( account_name name, const char* abi_json, const private_key_type* signer = nullptr );

bool is_code_cached( account_name name ) const;

bool chain_has_transaction( const transaction_id_type& txid ) const;
const transaction_receipt& get_transaction_receipt( const transaction_id_type& txid ) const;

Expand Down
7 changes: 7 additions & 0 deletions libraries/testing/tester.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -950,6 +950,13 @@ namespace eosio { namespace testing {
push_transaction( trx );
}

bool base_tester::is_code_cached( eosio::chain::account_name name ) const {
const auto& db = control->db();
const account_metadata_object* receiver_account = &db.template get<account_metadata_object,by_name>( name );
if ( receiver_account->code_hash == digest_type() ) return false;
return control->get_wasm_interface().is_code_cached( receiver_account->code_hash, receiver_account->vm_type, receiver_account->vm_version );
}


bool base_tester::chain_has_transaction( const transaction_id_type& txid ) const {
return chain_transactions.count(txid) != 0;
Expand Down
18 changes: 9 additions & 9 deletions unittests/api_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1264,14 +1264,14 @@ BOOST_FIXTURE_TEST_CASE(checktime_intrinsic, TESTER) { try {
set_code( "testapi"_n, ss.str().c_str() );
produce_blocks(1);

BOOST_TEST( !is_code_cached("testapi"_n) );

//initialize cache
BOOST_CHECK_EXCEPTION( call_test( *this, test_api_action<TEST_METHOD("doesn't matter", "doesn't matter")>{},
5000, 10, 10 ),
deadline_exception, is_deadline_exception );

// https://github.com/AntelopeIO/leap/issues/260 was created to track this TODO.
// Remove those comments after the issue is resolved.
// #warning TODO validate that the contract was successfully cached
BOOST_TEST( is_code_cached("testapi"_n) );

//it will always call
BOOST_CHECK_EXCEPTION( call_test( *this, test_api_action<TEST_METHOD("doesn't matter", "doesn't matter")>{},
Expand Down Expand Up @@ -1303,14 +1303,14 @@ BOOST_FIXTURE_TEST_CASE(checktime_grow_memory, TESTER) { try {
set_code( "testapi"_n, ss.str().c_str() );
produce_blocks(1);

BOOST_TEST( !is_code_cached("testapi"_n) );

//initialize cache
BOOST_CHECK_EXCEPTION( call_test( *this, test_api_action<TEST_METHOD("doesn't matter", "doesn't matter")>{},
5000, 10, 10 ),
deadline_exception, is_deadline_exception );

// https://github.com/AntelopeIO/leap/issues/260 was created to track this TODO.
// Remove those comments after the issue is resolved.
//#warning TODO validate that the contract was successfully cached
BOOST_TEST( is_code_cached("testapi"_n) );

//it will always call
BOOST_CHECK_EXCEPTION( call_test( *this, test_api_action<TEST_METHOD("doesn't matter", "doesn't matter")>{},
Expand All @@ -1325,14 +1325,14 @@ BOOST_FIXTURE_TEST_CASE(checktime_hashing_fail, TESTER) { try {
set_code( "testapi"_n, contracts::test_api_wasm() );
produce_blocks(1);

BOOST_TEST( !is_code_cached("testapi"_n) );

//hit deadline exception, but cache the contract
BOOST_CHECK_EXCEPTION( call_test( *this, test_api_action<TEST_METHOD("test_checktime", "checktime_sha1_failure")>{},
5000, 3, 3 ),
deadline_exception, is_deadline_exception );

// https://github.com/AntelopeIO/leap/issues/260 was created to track this TODO.
// Remove those comments after the issue is resolved.
//#warning TODO validate that the contract was successfully cached
BOOST_TEST( is_code_cached("testapi"_n) );

//the contract should be cached, now we should get deadline_exception because of calls to checktime() from hashing function
BOOST_CHECK_EXCEPTION( call_test( *this, test_api_action<TEST_METHOD("test_checktime", "checktime_sha1_failure")>{},
Expand Down

0 comments on commit cafceaa

Please sign in to comment.