Skip to content

Commit 3ed23f0

Browse files
Resolve #88: Testing for get_code_hash
1 parent 23b70db commit 3ed23f0

File tree

6 files changed

+118
-0
lines changed

6 files changed

+118
-0
lines changed

tests/integration/contracts.hpp.in

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,10 @@ namespace eosio::testing {
2222

2323
static std::vector<uint8_t> crypto_primitives_test_wasm() { return read_wasm("${CMAKE_BINARY_DIR}/../unit/test_contracts/crypto_primitives_tests.wasm"); }
2424
static std::vector<char> crypto_primitives_test_abi() { return read_abi("${CMAKE_BINARY_DIR}/../unit/test_contracts/crypto_primitives_tests.abi"); }
25+
26+
static std::vector<uint8_t> get_code_hash_write_test_wasm() { return read_wasm("${CMAKE_BINARY_DIR}/../unit/test_contracts/get_code_hash_write.wasm"); }
27+
static std::vector<char> get_code_hash_write_test_abi() { return read_abi("${CMAKE_BINARY_DIR}/../unit/test_contracts/get_code_hash_write.abi"); }
28+
static std::vector<uint8_t> get_code_hash_read_test_wasm() { return read_wasm("${CMAKE_BINARY_DIR}/../unit/test_contracts/get_code_hash_read.wasm"); }
29+
static std::vector<char> get_code_hash_read_test_abi() { return read_abi("${CMAKE_BINARY_DIR}/../unit/test_contracts/get_code_hash_read.abi"); }
2530
};
2631
} //ns eosio::testing
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include <boost/test/unit_test.hpp>
2+
#include <eosio/testing/tester.hpp>
3+
#include <eosio/chain/abi_serializer.hpp>
4+
5+
#include <Runtime/Runtime.h>
6+
7+
#include <fc/variant_object.hpp>
8+
9+
#include <contracts.hpp>
10+
11+
using namespace eosio;
12+
using namespace eosio::testing;
13+
using namespace eosio::chain;
14+
using namespace fc;
15+
16+
using mvo = fc::mutable_variant_object;
17+
18+
struct code_hash {
19+
uint64_t id;
20+
fc::sha256 hash;
21+
uint64_t primary_key() const { return id; }
22+
};
23+
FC_REFLECT(code_hash, (id)(hash))
24+
25+
BOOST_AUTO_TEST_SUITE(get_code_hash_tests_suite)
26+
27+
BOOST_FIXTURE_TEST_CASE( get_code_hash_tests, tester ) try {
28+
create_accounts( { "test"_n } );
29+
produce_block();
30+
31+
set_code( "test"_n, contracts::get_code_hash_write_test_wasm() );
32+
set_abi( "test"_n, contracts::get_code_hash_write_test_abi().data() );
33+
34+
produce_blocks();
35+
push_action("test"_n, "theaction"_n, "test"_n, mvo());
36+
code_hash entry;
37+
get_table_entry(entry, "test"_n, "test"_n, "code.hash"_n, 0);
38+
wdump((entry.hash));
39+
40+
set_code( "test"_n, contracts::get_code_hash_read_test_wasm() );
41+
produce_blocks();
42+
43+
push_action("test"_n, "theaction"_n, "test"_n, mvo());
44+
} FC_LOG_AND_RETHROW()
45+
46+
BOOST_AUTO_TEST_SUITE_END()

tests/unit/test_contracts/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ add_contract(explicit_nested_tests explicit_nested_tests explicit_nested_tests.c
77
add_contract(transfer_contract transfer_contract transfer.cpp)
88
add_contract(minimal_tests minimal_tests minimal_tests.cpp)
99
add_contract(crypto_primitives_tests crypto_primitives_tests crypto_primitives_tests.cpp)
10+
add_contract(get_code_hash_tests get_code_hash_write get_code_hash_write.cpp)
11+
add_contract(get_code_hash_tests get_code_hash_read get_code_hash_read.cpp)
1012
add_contract(capi_tests capi_tests capi/capi.c capi/action.c capi/chain.c capi/crypto.c capi/db.c capi/permission.c
1113
capi/print.c capi/privileged.c capi/system.c capi/transaction.c)
1214

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <eosio/eosio.hpp>
2+
#include <eosio/action.hpp>
3+
#include <eosio/name.hpp>
4+
5+
#include "get_code_hash_table.hpp"
6+
7+
class [[eosio::contract]] get_code_hash_tests : public contract {
8+
public:
9+
using contract::contract;
10+
11+
using hash_table = multi_index<name("code.hash"), code_hash>;
12+
13+
// Read the old code's hash from database and verify new code's hash differs
14+
[[eosio::action]]
15+
void theaction() {
16+
require_auth(get_self());
17+
hash_table hashes(get_self(), get_self().value);
18+
19+
auto hash = get_code_hash(get_self());
20+
check(hash != checksum256(), "Code hash should not be null");
21+
22+
auto record = hashes.get(0, "Unable to find recorded hash");
23+
check(hash != record.hash, "Code hash has not changed");
24+
eosio::print("Old hash: ", record.hash, "; new hash: ", hash);
25+
}
26+
};
27+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#pragma once
2+
3+
using namespace eosio;
4+
5+
TABLE code_hash {
6+
uint64_t id;
7+
checksum256 hash;
8+
uint64_t primary_key() const { return id; }
9+
};
10+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <eosio/eosio.hpp>
2+
#include <eosio/action.hpp>
3+
#include <eosio/name.hpp>
4+
5+
#include "get_code_hash_table.hpp"
6+
7+
class [[eosio::contract]] get_code_hash_tests : public contract {
8+
public:
9+
using contract::contract;
10+
11+
using hash_table = multi_index<name("code.hash"), code_hash>;
12+
13+
// Write this code's hash to database
14+
[[eosio::action]]
15+
void theaction() {
16+
require_auth(get_self());
17+
hash_table hashes(get_self(), get_self().value);
18+
19+
auto hash = get_code_hash(get_self());
20+
check(hash != checksum256(), "Code hash should not be null");
21+
22+
hashes.emplace(get_self(), [&hash](auto& t) {
23+
t.id = 0;
24+
t.hash = hash;
25+
});
26+
}
27+
};
28+

0 commit comments

Comments
 (0)