-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Resolve #88 #109
Merged
Merged
Resolve #88 #109
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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; | ||
}); | ||
} | ||
}; | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure which one is more efficient but we have a common pattern to execute intrinsic where first we execute it with zero size first, it returns with return structure size and then we execute it again. example is
get_action
:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, well I'm not sure how the overheads stack up in the WASM VM, but calling an intrinsic is roughly equivalent in principle to a syscall to kernel space from userland, and those are notoriously slow.
alloca
, on the other hand, is merely ticking a register, which is pretty much the fastest instruction imaginable.There's no substitute for benchmarking, but I would expect making an extra call out of the VM to be at least a couple of orders of magnitude slower than
alloca
. I think it would only make sense to make the second call if there was a possibility that the struct would be unpredictably large (especially, structs containingstring
orvector
, etc), but even in this case, just allocating a very large heap buffer is probably much faster, just more wasteful of space. That's a hard decision for a library to make, since different clients have different constraints, but in this case the buffer's size has a tight upper bound so we don't need to concern ourselves. =)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a way to get microseconds or CPU ticks within a contract? I'd like to run some benchmarks, including the cost of calling an intrinsic, as I'm pretty certain it falls in the category of obnoxiously slow, and it might be worth my effort to go through and replace all of these "when you call the intrinsic once, call it twice" instances with something way faster. CPU time is scarce on the blockchain, so the stdlib should make an effort to conserve it for the contract.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like that would be a good addition to: eosnetworkfoundation/product#134
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nathanielhourt I agree with your point but the easiest solution looks to me just to make it inline with other stuff. Anyway there are not a lot. If you can spend extra and tell the difference, we can go that way and redo this (probably as part of other PR).
@larryk85 any thought on that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since the packed size will always be the same unless we somehow manage to make it to 128 versions of the struct, which seems incredibly unlikely since each bump would be a consensus change, it does seem rather wasteful to make multiple calls. It seems unlikely to even need to
*2
the size here.I might suggest checking the return value of
internal_use_do_not_use::get_code_hash()
, maybe even in lieu of the version check (the possibility that the host function could in the future return a version different than the one asked for would seem to be devastating to existing contracts -- why would we ever do that).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hahaha yeah, the version check is abysmally pessimistic, but I like to make such checks where cheap. Bugs love to hide at API boundaries and deucedly so at versioned ones! =)
The
*2
size is definitely overkill, and I would be amenable to reducing it. I also agree that checking the return value is prudent. Eventually, a general template could probably be improvised to do an optimistic fast path (alloca
and single call) with a slow path fallback (malloc
and second call) if the first call didn't allocate adequate space, and even give motivated clients the option to pass a size hint to optimize the fast path for their case, and make that template reusable for all the intrinsics. That would be nice. But for now, I can just go for a retval check and fallback slow path, and maybe dress it up to a template in a future PReq.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, check out the new version. Also, I just noticed that the
get_action
implementation has a memory leak in the greater than 512 byte case.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nathanielhourt PR has been approved. Thanks. Do you want to add something or just letting enf dev merge by themselves? normally developer pushes their changes so you are good to go if you are done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I don't have privs to merge it even after approval. I figured someone else would do it. =)