-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resolve #88: Testing for get_code_hash
- Loading branch information
1 parent
f78cbaa
commit f7b206f
Showing
6 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); | ||
} | ||
}; | ||
|