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 opcodes for msg.* #8

Merged
merged 8 commits into from
Nov 14, 2024
Merged

Add opcodes for msg.* #8

merged 8 commits into from
Nov 14, 2024

Conversation

0xRampey
Copy link
Contributor

@0xRampey 0xRampey commented Nov 10, 2024

Related to #4

Adds msg.value. msg.sig and msg.data with light checks in the contract

Also uses enum to pattern match syscalls.

@0xRampey 0xRampey marked this pull request as draft November 10, 2024 00:29
@0xRampey 0xRampey marked this pull request as ready for review November 10, 2024 01:57
Copy link
Collaborator

@gakonst gakonst left a 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 {
Copy link
Collaborator

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?

Copy link
Contributor Author

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?

Copy link
Collaborator

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?

Copy link
Contributor Author

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
Comment on lines 42 to 44
if msg_value() != U256::from(0) {
revert();
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

same as above

Copy link
Contributor Author

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

Copy link
Contributor Author

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
Comment on lines 45 to 47
if msg_sig() != [2, 0, 0, 0] {
revert();
}
Copy link
Collaborator

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?

Copy link
Collaborator

Choose a reason for hiding this comment

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

yep makes sense

Copy link
Contributor Author

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

Copy link
Collaborator

Choose a reason for hiding this comment

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

and this too?

Comment on lines 122 to 129
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])
Copy link
Collaborator

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 Show resolved Hide resolved
let first: u64;
let offset: u64 = 0;
unsafe {
asm!("ecall", lateout("a0") first, in("a0") offset, in("t0") u64::from(Syscall::CALLDATALOAD));
Copy link
Collaborator

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.

Copy link
Contributor Author

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?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yep sounds good

@@ -60,4 +60,5 @@ syscalls!(
(4, Revert, "revert"),
(5, Caller, "caller"),
(0x20, Keccak256, "keccak256"),
(34, CALLVALUE, "callvalue"),
Copy link
Collaborator

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

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()
Copy link
Collaborator

@leonardoalt leonardoalt Nov 13, 2024

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?

Copy link
Contributor Author

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) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nice!

@0xRampey
Copy link
Contributor Author

Not much conflict with #9 now. I think we can proceed to merge this?

@0xRampey 0xRampey mentioned this pull request Nov 14, 2024
Copy link
Collaborator

@leonardoalt leonardoalt left a comment

Choose a reason for hiding this comment

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

Awesome!

@leonardoalt leonardoalt merged commit bc989b1 into r55-eth:main Nov 14, 2024
@0xRampey 0xRampey deleted the feat/msg branch November 14, 2024 19:54
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.

3 participants