Skip to content
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

Add support for chain.id, block.timestamp, other builtin attributes #208

Merged
merged 6 commits into from
Feb 1, 2021

Conversation

sbillig
Copy link
Collaborator

@sbillig sbillig commented Jan 28, 2021

What was wrong?

There are some missing builtins needed for the uniswap demo.

How was it fixed?

It hasn't been yet :)
I'll probably do block.timestamp, maybe more depending on whether they raise tricky issues.

Edit: I've added support for:
block.coinbase
block.difficulty
block.number
block.timestamp
chain.id
msg.value
tx.origin
tx.gas_price (aside: do we want to standardize on snake_case like .to_mem()?)

resolves #190

I changed the builtins to use enums, so the compiler can tell us when we aren't handling a case. I also stubbed out some probable builtins in those enums (based on the existing demo fe code, and vyper/solidity). I'm sure there are some things yet to be decided (eg is keccak256 a method on every value, or a standalone builtin function, or imported from the std library, or part of a builtin trait, or...?). I'm happy to change whatever as decisions are made, of course.

To-Do

  • OPTIONAL: Update Spec if applicable

  • Add entry to the release notes (may forgo for trivial changes)

  • Clean up commit history

@g-r-a-n-t
Copy link
Member

g-r-a-n-t commented Jan 28, 2021

The refactoring looks good.

is keccak256 a method on every value, or a standalone builtin function, or imported from the std library, or part of a builtin trait, or...?

For now I'm thinking keccak256 and the ABI encoding functions will be builtin to certain types as attribute functions, similar to to_mem and clone. Ideally these functions will be implemented in the standard library later on.

Also, we probably want to limit the use of keccak256 to bytes[n]. This way, it's clear what you're hashing. So anytime keccak256 is used, you would first encode the value and then hash (e.g. (42, "hello world").abiEncode().keccak256()).

@g-r-a-n-t
Copy link
Member

Here's the Yul reference btw.

@sbillig sbillig changed the title WIP: Add support for block.timestamp, others? Add support for chain.id, block.timestamp, other builtin attributes Jan 29, 2021
@sbillig sbillig marked this pull request as ready for review January 29, 2021 03:39
@codecov-io
Copy link

codecov-io commented Jan 29, 2021

Codecov Report

Merging #208 (669366b) into master (7443ac2) will increase coverage by 0.05%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #208      +/-   ##
==========================================
+ Coverage   94.28%   94.33%   +0.05%     
==========================================
  Files          49       49              
  Lines        3357     3356       -1     
==========================================
+ Hits         3165     3166       +1     
+ Misses        192      190       -2     
Impacted Files Coverage Δ
analyzer/src/traversal/expressions.rs 95.85% <ø> (+0.33%) ⬆️
compiler/src/yul/mappers/expressions.rs 97.16% <100.00%> (+0.83%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 7443ac2...c9f851c. Read the comment docs.

@sbillig
Copy link
Collaborator Author

sbillig commented Jan 29, 2021

Ok, this is probably enough for a first pass. msg.data and msg.sig aren't simple yul function calls, so I'm going to wait on those and maybe start looking at something more pressing.

The 'use refs in evm_contracts tests' changes are mostly superfluous. I hit a minor ownership issue and thought the nicest solution was for test_functions output arg to be Option<&Token> instead of Option<Token>, and ended up replacing all the vecs with &[] too. Happy to revert if such changes are annoying to anyone.

Ok(Object::Block) => todo!(),
Ok(Object::Chain) => todo!(),
Ok(Object::Msg) => todo!(), // TODO: error because msg has no methods?
Ok(Object::Tx) => todo!(),
Copy link
Collaborator

Choose a reason for hiding this comment

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

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

One of my favorite recent additions!

pub const MSG: &str = "msg";
pub const CLONE: &str = "clone";
pub const TO_MEM: &str = "to_mem";
use strum::EnumString;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Oh, that's cool! I didn't know about EnumString and yeah it makes total sense to use enums here for added guidance from the compiler 🎉

@sbillig sbillig force-pushed the builtins branch 2 times, most recently from 26c6f2e to 669366b Compare January 31, 2021 20:37
@sbillig
Copy link
Collaborator Author

sbillig commented Feb 1, 2021

Rebased on master; should be good now. If I add .abi_encode(), etc I'll do it in a new PR.

@@ -124,7 +123,7 @@ impl ContractHarness {
})
.collect::<Vec<_>>();

if !outputs_for_event.contains(&expected_output) {
if !outputs_for_event.iter().any(|v| &v == expected_output) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

No big deal but we generally try to avoid single letter variables. This could be named val instead.

@@ -224,12 +227,12 @@ fn bytes_token(s: &str) -> ethabi::Token {
ethabi::Token::FixedBytes(ethabi::FixedBytes::from(s))
}

fn u256_array_token(v: Vec<usize>) -> ethabi::Token {
ethabi::Token::FixedArray(v.into_iter().map(|n| uint_token(n)).collect())
fn u256_array_token(v: &[usize]) -> ethabi::Token {
Copy link
Collaborator

Choose a reason for hiding this comment

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

But we still have some leftover single letter variable names today...we should adjust all of them (but doesn't have to be in this PR)

Copy link
Collaborator

@cburgdorf cburgdorf left a comment

Choose a reason for hiding this comment

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

This looks great! All the Vec<T> to &[T]` changes seem to indicate that we aren't running clippy against the tests. Something that we should probably do.

@g-r-a-n-t
Copy link
Member

very nice!

@g-r-a-n-t g-r-a-n-t merged commit c665b27 into ethereum:master Feb 1, 2021
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.

chain id and block timestamp
4 participants