Skip to content

Commit 23b70db

Browse files
Ref #88: Implement wrapper for get_code_hash
1 parent d1b5588 commit 23b70db

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

libraries/eosiolib/capi/eosio/action.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,16 @@ uint64_t publication_time( void );
168168
__attribute__((eosio_wasm_import))
169169
capi_name current_receiver( void );
170170

171+
/**
172+
* Get the hash of the code currently published on the given account
173+
* @param account Name of the account to hash the code of
174+
* @param struct_version Version specifying the desired format of the returned struct
175+
* @param result_buffer Buffer wherein the result should be written
176+
* @param buffer_size Size in bytes of result_buffer
177+
*/
178+
__attribute__((eosio_wasm_import))
179+
uint32_t get_code_hash( uint64_t account, uint32_t struct_version, char* result_buffer, size_t buffer_size );
180+
171181
/**
172182
* Set the action return value which will be included in the action_receipt
173183
* @brief Set the action return value

libraries/eosiolib/contracts/eosio/action.hpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "../../core/eosio/serialize.hpp"
99
#include "../../core/eosio/datastream.hpp"
1010
#include "../../core/eosio/name.hpp"
11+
#include "../../core/eosio/fixed_bytes.hpp"
1112
#include "../../core/eosio/ignore.hpp"
1213
#include "../../core/eosio/time.hpp"
1314

@@ -52,9 +53,23 @@ namespace eosio {
5253

5354
__attribute__((eosio_wasm_import))
5455
uint64_t current_receiver();
56+
57+
__attribute__((eosio_wasm_import))
58+
uint32_t get_code_hash( uint64_t account, uint32_t struct_version, char* result_buffer, size_t buffer_size );
5559
}
5660
};
5761

62+
struct code_hash_result {
63+
unsigned_int struct_version;
64+
uint64_t code_sequence;
65+
checksum256 code_hash;
66+
uint8_t vm_type;
67+
uint8_t vm_version;
68+
69+
CDT_REFLECT(struct_version, code_sequence, code_hash, vm_type, vm_version);
70+
EOSLIB_SERIALIZE(code_hash_result, (struct_version)(code_sequence)(code_hash)(vm_type)(vm_version));
71+
};
72+
5873
/**
5974
* @defgroup action Action
6075
* @ingroup contracts
@@ -150,6 +165,30 @@ namespace eosio {
150165
return name{internal_use_do_not_use::current_receiver()};
151166
}
152167

168+
/**
169+
* Get the hash of the code currently published on the given account
170+
* @param account Name of the account to hash the code of
171+
* @param full_result Optional: If a full result struct is desired, a pointer to the struct to populate
172+
* @return The SHA256 hash of the specified account's code
173+
*/
174+
inline checksum256 get_code_hash( name account, code_hash_result* full_result = nullptr ) {
175+
if (full_result == nullptr)
176+
full_result = (code_hash_result*)alloca(sizeof(code_hash_result));
177+
178+
// Packed size is dynamic, so we don't know what it'll be. Try to have plenty of space.
179+
auto struct_buffer_size = sizeof(code_hash_result)*2;
180+
char* struct_buffer = (char*)alloca(struct_buffer_size);
181+
182+
using VersionType = decltype(code_hash_result::struct_version);
183+
const VersionType STRUCT_VERSION = 0;
184+
internal_use_do_not_use::get_code_hash(account.value, STRUCT_VERSION, struct_buffer, struct_buffer_size);
185+
check(unpack<VersionType>(struct_buffer, struct_buffer_size) == STRUCT_VERSION,
186+
"Hypervisor returned unexpected code hash struct version");
187+
unpack(*full_result, struct_buffer, struct_buffer_size);
188+
189+
return full_result->code_hash;
190+
}
191+
153192
/**
154193
* Copy up to length bytes of current action data to the specified location
155194
*

tests/unit/test_contracts/capi/action.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ void test_action( void ) {
1313
send_context_free_inline(NULL, 0);
1414
publication_time();
1515
current_receiver();
16+
get_code_hash(0, 0, NULL, 0);
1617
set_action_return_value(NULL, 0);
1718
}

0 commit comments

Comments
 (0)