Skip to content

Commit

Permalink
Resolve #88: Testing for get_code_hash
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanielhourt committed Feb 26, 2023
1 parent 7d17621 commit 2f9c71f
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 0 deletions.
5 changes: 5 additions & 0 deletions tests/integration/contracts.hpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,10 @@ namespace eosio::testing {

static std::vector<uint8_t> crypto_primitives_test_wasm() { return read_wasm("${CMAKE_BINARY_DIR}/../unit/test_contracts/crypto_primitives_tests.wasm"); }
static std::vector<char> crypto_primitives_test_abi() { return read_abi("${CMAKE_BINARY_DIR}/../unit/test_contracts/crypto_primitives_tests.abi"); }

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"); }
static std::vector<char> get_code_hash_write_test_abi() { return read_abi("${CMAKE_BINARY_DIR}/../unit/test_contracts/get_code_hash_write.abi"); }
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"); }
static std::vector<char> get_code_hash_read_test_abi() { return read_abi("${CMAKE_BINARY_DIR}/../unit/test_contracts/get_code_hash_read.abi"); }
};
} //ns eosio::testing
46 changes: 46 additions & 0 deletions tests/integration/get_code_hash_tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include <boost/test/unit_test.hpp>
#include <eosio/testing/tester.hpp>
#include <eosio/chain/abi_serializer.hpp>

#include <Runtime/Runtime.h>

#include <fc/variant_object.hpp>

#include <contracts.hpp>

using namespace eosio;
using namespace eosio::testing;
using namespace eosio::chain;
using namespace fc;

using mvo = fc::mutable_variant_object;

struct code_hash {
uint64_t id;
fc::sha256 hash;
uint64_t primary_key() const { return id; }
};
FC_REFLECT(code_hash, (id)(hash))

BOOST_AUTO_TEST_SUITE(get_code_hash_tests_suite)

BOOST_FIXTURE_TEST_CASE( get_code_hash_tests, tester ) try {
create_accounts( { "test"_n } );
produce_block();

set_code( "test"_n, contracts::get_code_hash_write_test_wasm() );
set_abi( "test"_n, contracts::get_code_hash_write_test_abi().data() );

produce_blocks();
push_action("test"_n, "theaction"_n, "test"_n, mvo());
code_hash entry;
get_table_entry(entry, "test"_n, "test"_n, "code.hash"_n, 0);
wdump((entry.hash));

set_code( "test"_n, contracts::get_code_hash_read_test_wasm() );
produce_blocks();

push_action("test"_n, "theaction"_n, "test"_n, mvo());
} FC_LOG_AND_RETHROW()

BOOST_AUTO_TEST_SUITE_END()
2 changes: 2 additions & 0 deletions tests/unit/test_contracts/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ add_contract(explicit_nested_tests explicit_nested_tests explicit_nested_tests.c
add_contract(transfer_contract transfer_contract transfer.cpp)
add_contract(minimal_tests minimal_tests minimal_tests.cpp)
add_contract(crypto_primitives_tests crypto_primitives_tests crypto_primitives_tests.cpp)
add_contract(get_code_hash_tests get_code_hash_write get_code_hash_write.cpp)
add_contract(get_code_hash_tests get_code_hash_read get_code_hash_read.cpp)
add_contract(capi_tests capi_tests capi/capi.c capi/action.c capi/chain.c capi/crypto.c capi/db.c capi/permission.c
capi/print.c capi/privileged.c capi/system.c capi/transaction.c)

Expand Down
27 changes: 27 additions & 0 deletions tests/unit/test_contracts/get_code_hash_read.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <eosio/eosio.hpp>
#include <eosio/action.hpp>
#include <eosio/name.hpp>

#include "get_code_hash_table.hpp"

class [[eosio::contract]] get_code_hash_tests : public contract {
public:
using contract::contract;

using hash_table = multi_index<name("code.hash"), code_hash>;

// Read the old code's hash from database and verify new code's hash differs
[[eosio::action]]
void theaction() {
require_auth(get_self());
hash_table hashes(get_self(), get_self().value);

auto hash = get_code_hash(get_self());
check(hash != checksum256(), "Code hash should not be null");

auto record = hashes.get(0, "Unable to find recorded hash");
check(hash != record.hash, "Code hash has not changed");
eosio::print("Old hash: ", record.hash, "; new hash: ", hash);
}
};

10 changes: 10 additions & 0 deletions tests/unit/test_contracts/get_code_hash_table.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once

using namespace eosio;

TABLE code_hash {
uint64_t id;
checksum256 hash;
uint64_t primary_key() const { return id; }
};

28 changes: 28 additions & 0 deletions tests/unit/test_contracts/get_code_hash_write.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <eosio/eosio.hpp>
#include <eosio/action.hpp>
#include <eosio/name.hpp>

#include "get_code_hash_table.hpp"

class [[eosio::contract]] get_code_hash_tests : public contract {
public:
using contract::contract;

using hash_table = multi_index<name("code.hash"), code_hash>;

// Write this code's hash to database
[[eosio::action]]
void theaction() {
require_auth(get_self());
hash_table hashes(get_self(), get_self().value);

auto hash = get_code_hash(get_self());
check(hash != checksum256(), "Code hash should not be null");

hashes.emplace(get_self(), [&hash](auto& t) {
t.id = 0;
t.hash = hash;
});
}
};

0 comments on commit 2f9c71f

Please sign in to comment.