Sitelet https://github.com/r55-eth/r55/commit/971f651bbc3fb142a0cd6782e9ea57656ceff40b
Skip to content

Commit 971f651

Browse files
committed
feat: add serde for Address type for msg.sender
1 parent 2820fed commit 971f651

3 files changed

Lines changed: 32 additions & 6 deletions

File tree

‎erc20/src/lib.rs‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use core::default::Default;
66
use contract_derive::contract;
77
use eth_riscv_runtime::types::Mapping;
88

9-
use alloy_core::primitives::Address;
9+
use alloy_core::primitives::{Address, address};
1010

1111
#[derive(Default)]
1212
pub struct ERC20 {
@@ -32,6 +32,11 @@ impl ERC20 {
3232
}
3333

3434
pub fn mint(&self, to: Address, value: u64) {
35+
let owner = msg_sender();
36+
if owner != address!("0000000000000000000000000000000000000007") {
37+
revert();
38+
}
39+
3540
let to_balance = self.balance.read(to);
3641
self.balance.write(to, to_balance + value);
3742
}

‎eth-riscv-runtime/src/lib.rs‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use core::arch::asm;
66
use core::panic::PanicInfo;
77
use core::slice;
88
pub use riscv_rt::entry;
9+
use alloy_core::primitives::Address;
910

1011
mod alloc;
1112
pub mod types;
@@ -72,12 +73,18 @@ pub fn revert() -> ! {
7273
unreachable!()
7374
}
7475

75-
pub fn msg_sender() -> u64 {
76-
let address: u64;
76+
pub fn msg_sender() -> Address {
77+
let first: u64;
78+
let second: u64;
79+
let third: u64;
7780
unsafe {
78-
asm!("ecall", lateout("a0") address, in("t0") u32::from(Syscall::Caller));
81+
asm!("ecall", lateout("a0") first, lateout("a1") second, lateout("a2") third, in("t0") u32::from(Syscall::Caller));
7982
}
80-
address
83+
let mut bytes = [0u8; 20];
84+
bytes[0..8].copy_from_slice(&first.to_be_bytes());
85+
bytes[8..16].copy_from_slice(&second.to_be_bytes());
86+
bytes[16..20].copy_from_slice(&third.to_be_bytes()[..4]);
87+
Address::from_slice(&bytes)
8188
}
8289

8390
#[allow(non_snake_case)]

‎r55/src/exec.rs‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ pub fn run_tx(db: &mut InMemoryDB, addr: &Address, calldata: Vec<u8>) {
4242
let mut evm = Evm::builder()
4343
.with_db(db)
4444
.modify_tx_env(|tx| {
45-
tx.caller = address!("0000000000000000000000000000000000000001");
45+
tx.caller = address!("0000000000000000000000000000000000000007");
4646
tx.transact_to = TransactTo::Call(*addr);
4747
tx.data = calldata.into();
4848
tx.value = U256::from(0);
@@ -242,6 +242,20 @@ fn execute_riscv(
242242
},
243243
};
244244
}
245+
5 => {
246+
// Syscall::Caller
247+
let caller = interpreter.contract.caller;
248+
// Break address into 3 u64s and write to registers
249+
let caller_bytes = caller.as_slice();
250+
let first_u64 = u64::from_be_bytes(caller_bytes[0..8].try_into().unwrap());
251+
emu.cpu.xregs.write(10, first_u64);
252+
let second_u64 = u64::from_be_bytes(caller_bytes[8..16].try_into().unwrap());
253+
emu.cpu.xregs.write(11, second_u64);
254+
let mut padded_bytes = [0u8; 8];
255+
padded_bytes[..4].copy_from_slice(&caller_bytes[16..20]);
256+
let third_u64 = u64::from_be_bytes(padded_bytes);
257+
emu.cpu.xregs.write(12, third_u64);
258+
}
245259
_ => {
246260
println!("Unhandled syscall: {:?}", t0);
247261
return return_revert(interpreter);

0 commit comments

Comments
 (0)