Skip to content

Move blockhash from block.blockhash to global level.#3646

Merged
axic merged 4 commits intodevelopfrom
blockhash-global
Mar 27, 2018
Merged

Move blockhash from block.blockhash to global level.#3646
axic merged 4 commits intodevelopfrom
blockhash-global

Conversation

@erak
Copy link
Collaborator

@erak erak commented Mar 5, 2018

Closes #2970

@erak erak requested review from axic, chriseth and ekpyron March 5, 2018 15:44
- ``assert(bool condition)``: abort execution and revert state changes if condition is ``false`` (use for internal error)
- ``require(bool condition)``: abort execution and revert state changes if condition is ``false`` (use for malformed input or error in external component)
- ``revert()``: abort execution and revert state changes
- ``blockhash(uint blockNumber) returns (bytes32)``: hash of the given block - only works for 256 most recent blocks
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think all these values should be sorted somehow...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have a strong opinion on how they should be sorted? Otherwise I'd probably sort them by variable in alphabetical order.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, alphabetically, but that can be a separate PR.

Global Variables
================

- ``block.blockhash(uint blockNumber) returns (bytes32)``: hash of the given block - only works for 256 most recent blocks
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please keep the old one and mark it as deprecated.

Block and Transaction Properties
--------------------------------

- ``block.blockhash(uint blockNumber) returns (bytes32)``: hash of the given block - only works for 256 most recent blocks excluding current
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Were the two leading -- here on purpose?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you check if they have any effect?


.. note::
Do not rely on ``block.timestamp``, ``now`` and ``block.blockhash`` as a source of randomness,
Do not rely on ``block.timestamp`` and ``now`` as a source of randomness,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please keep blockhash.

but the only guarantee is that it will be somewhere between the timestamps of two
consecutive blocks in the canonical chain.

.. note::
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It probably does not hurt to have all the warnings in both places.

break;
case Type::Category::Magic:
// we can ignore the kind of magic and only look at the name of the member
if (member != "data" && member != "sig" && member != "blockhash")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please keep the old one.

return MemberList::MemberMap({
{"coinbase", make_shared<IntegerType>(160, IntegerType::Modifier::Address)},
{"timestamp", make_shared<IntegerType>(256)},
{"blockhash", make_shared<FunctionType>(strings{"uint"}, strings{"bytes32"}, FunctionType::Kind::BlockHash, false, StateMutability::View)},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please only remove it if pragma experimental "v0.5.0" is set.

}
)";
compileAndRun(sourceCode);
BOOST_CHECK_EQUAL(callContractFunction("a()").empty(), false);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use ABI_CHECK(callContractFunction(...), encodeArgs(0))

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah sorry, I misunderstood this. I think it is easier to read if you just use
BOOST_CHECK(!callContractFunction("a()").empty()), or perhaps even BOOST_CHECK_EQUAL(callContractFunction("a()").length(), 32);

char const* text = R"(
contract test {
function blockhash() pure public returns (bytes32) {}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add another function to the test contract that calls the local blockhash and checks whether the compiler resolves it correctly, e.g. by using a return type that is incompatible to the return type of global blackhash and assigning the result of the function call to a variable?

Copy link
Contributor

@chriseth chriseth Mar 5, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better to have a function with identical signature (including return type) and use an EndToEnd test to check.

@erak erak force-pushed the blockhash-global branch from 60c7413 to 92a8ad9 Compare March 5, 2018 18:32
@erak
Copy link
Collaborator Author

erak commented Mar 5, 2018

Updated

@chriseth
Copy link
Contributor

chriseth commented Mar 5, 2018

Tests are failing: blockhash_not_available_in_block and blockhash_shadow

@chriseth
Copy link
Contributor

chriseth commented Mar 5, 2018

Sorry, merged the other one first ;)
Please rebase!

char const* text = R"(
contract Test {
function a() public returns (bytes32) {
return block.blockhash(0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should only fail if v050 mode is selected.

vector<string> view{
"block.coinbase",
"block.timestamp",
"block.blockhash(7)",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These shouldn't be removed (in v050 mode).

@ekpyron
Copy link
Collaborator

ekpyron commented Mar 5, 2018

It may be worth to consider, whether these changes could/should be replaced by a change to the static analyzer instead; similarly to preferring the solution of #3652 over the solution of #3643.

@erak
Copy link
Collaborator Author

erak commented Mar 7, 2018

@ekpyron I will have a look into your suggestion regarding deprecation of block.blockhash().

@chriseth
Copy link
Contributor

chriseth commented Mar 7, 2018

The list of commits looks a little bit messed up :)

@erak erak force-pushed the blockhash-global branch from 0be0c6e to 43a75e4 Compare March 7, 2018 11:03
@erak
Copy link
Collaborator Author

erak commented Mar 7, 2018

@chriseth Yep, I accidentally messed it up. Should be fine now :) There's just this one comment about deprecating block.blockhash() left, I have to look into. Will let you know when this PR is final...

@erak
Copy link
Collaborator Author

erak commented Mar 7, 2018

Ready for review.

@chriseth
Copy link
Contributor

chriseth commented Mar 9, 2018

There are still two consecutive commits with the same description.

================

- ``block.blockhash(uint blockNumber) returns (bytes32)``: hash of the given block - only works for 256 most recent blocks
- ``block.blockhash(uint blockNumber) returns (bytes32)``: hash of the given block - only works for 256 most recent blocks - deprecated in version 0.4.21 and to be replaced by ``blockhash(uint blockNumber)``.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update the version to 0.4.22

@erak erak force-pushed the blockhash-global branch from 96769b4 to 4a449c1 Compare March 9, 2018 14:46
@erak
Copy link
Collaborator Author

erak commented Mar 9, 2018

@chriseth I've change the version number mentioned in your last comment and also checked the additional - in the docs: it probably slipped in by accident and can be removed safely since it's ignored while rendering the bullet list.

@erak erak force-pushed the blockhash-global branch from 4a449c1 to ef85910 Compare March 12, 2018 10:30
{"number", make_shared<IntegerType>(256)},
{"gaslimit", make_shared<IntegerType>(256)}
});
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case-statements the { usually have the same indentation level as the case keyword itself.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you actually change anything here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. In the end I did not change anything. These braces are leftovers from a previous commit and I'll remove them.

Copy link
Contributor

@chriseth chriseth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only the small formatting issue left in my opinion.

@erak erak force-pushed the blockhash-global branch from ef85910 to 0b25f48 Compare March 12, 2018 13:52
@erak
Copy link
Collaborator Author

erak commented Mar 12, 2018

@chriseth Fixed the formatting

@chriseth
Copy link
Contributor

@axic please merge if you are fine with this PR.

@axic
Copy link
Contributor

axic commented Mar 14, 2018

Sorting out the rebase.

@axic axic force-pushed the blockhash-global branch from 0b25f48 to c4dbed9 Compare March 15, 2018 01:19
@chriseth
Copy link
Contributor

Tests are passing. Can be merged in my opinion.

Block and Transaction Properties
--------------------------------

- ``block.blockhash(uint blockNumber) returns (bytes32)``: hash of the given block - only works for 256 most recent blocks excluding current
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't comment inline, but the note below still uses block.blockhash, should be updated, since the copy of that note in miscellaneous does the same.

================

- ``block.blockhash(uint blockNumber) returns (bytes32)``: hash of the given block - only works for 256 most recent blocks
- ``block.blockhash(uint blockNumber) returns (bytes32)``: hash of the given block - only works for 256 most recent blocks - deprecated in version 0.4.22 and to be replaced by ``blockhash(uint blockNumber)``.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd remove to be.

@erak
Copy link
Collaborator Author

erak commented Mar 23, 2018

Improved documentation. Should be ready to merge.

@axic axic force-pushed the blockhash-global branch 2 times, most recently from e4b7644 to c5b6a2e Compare March 27, 2018 02:29
@axic axic force-pushed the blockhash-global branch from c5b6a2e to 2c56e53 Compare March 27, 2018 02:30
@axic axic merged commit 32f0898 into develop Mar 27, 2018
@axic axic deleted the blockhash-global branch March 27, 2018 10:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

Comments