-
Notifications
You must be signed in to change notification settings - Fork 8
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 opcodes for msg.*
#8
Conversation
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.
Conflicts with #9 -- let's get that in first given it touches on the broader syscall interface. Left some thoughts.
erc20/src/lib.rs
Outdated
@@ -16,6 +16,9 @@ pub struct ERC20 { | |||
#[contract] | |||
impl ERC20 { | |||
pub fn balance_of(&self, owner: Address) -> u64 { | |||
if msg_data()[0] != 0x0 { |
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.
Should be abstracted into a #[payable]
attribute for functions?
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.
msg.data
is not used inside payable
afaik.
Do we want to add receive() external payable{}
to handle no-call-data txs?
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.
I think this can be removed now?
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.
Yeah sounds good.
erc20/src/lib.rs
Outdated
if msg_value() != U256::from(0) { | ||
revert(); | ||
} |
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.
same as above
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.
Good idea. Will add this in
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.
Done @gakonst
erc20/src/lib.rs
Outdated
if msg_sig() != [2, 0, 0, 0] { | ||
revert(); | ||
} |
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.
Should we enforce deterministic 4-byte discriminants here from the function signature, with the ability to override via a #[sig(0x12345678)
attribute?
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.
yep makes sense
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.
Yes, have an issue for it here: #7
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.
and this too?
eth-riscv-runtime/src/lib.rs
Outdated
let first: u64; | ||
let second: u64; | ||
let third: u64; | ||
let fourth: u64; | ||
unsafe { | ||
asm!("ecall", lateout("a0") first, lateout("a1") second, lateout("a2") third, lateout("a3") fourth, in("t0") u64::from(Syscall::CALLVALUE)); | ||
} | ||
U256::from_limbs([first, second, third, fourth]) |
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.
We should add an abstraction for U256 conversion stuff, I think -- not sure best way
eth-riscv-runtime/src/lib.rs
Outdated
let first: u64; | ||
let offset: u64 = 0; | ||
unsafe { | ||
asm!("ecall", lateout("a0") first, in("a0") offset, in("t0") u64::from(Syscall::CALLDATALOAD)); |
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.
So regarding calldata, currently we dont implement it as a host function, but rather inject it directly as a ROM for the interpreter which lets the contract read "for free". We need to decide which approach we want to use, but I quite like this one.
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.
Reading from ROM should be faster than syscalls
. Let's change it back to 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.
Yep sounds good
eth-riscv-syscalls/src/lib.rs
Outdated
@@ -60,4 +60,5 @@ syscalls!( | |||
(4, Revert, "revert"), | |||
(5, Caller, "caller"), | |||
(0x20, Keccak256, "keccak256"), | |||
(34, CALLVALUE, "callvalue"), |
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.
let's use hex here I think it's more common in EVM world
eth-riscv-runtime/src/lib.rs
Outdated
let length = unsafe { slice_from_raw_parts(CALLDATA_ADDRESS, 8) }; | ||
let length = u64::from_le_bytes([length[0], length[1], length[2], length[3], length[4], length[5], length[6], length[7]]) as usize; | ||
let calldata = unsafe { slice_from_raw_parts(CALLDATA_ADDRESS + 8, length) }; | ||
calldata.to_vec() |
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.
I think we could return a slice to avoid allocation?
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.
Ah yes!
@@ -44,7 +44,15 @@ pub fn contract(_attr: TokenStream, item: TokenStream) -> TokenStream { | |||
}).collect(); | |||
|
|||
let arg_names: Vec<_> = (0..method.sig.inputs.len() - 1).map(|i| format_ident!("arg{}", i)).collect(); | |||
|
|||
let checks = if !is_payable(&method) { |
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.
Nice!
Not much conflict with #9 now. I think we can proceed to merge this? |
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.
Awesome!
Related to #4
Adds
msg.value
.msg.sig
andmsg.data
with light checks in the contractAlso uses enum to pattern match syscalls.