@@ -10,8 +10,8 @@ pub use riscv_rt::entry;
1010
1111mod alloc;
1212pub mod block;
13- pub mod tx;
1413pub mod types;
14+ pub mod tx;
1515
1616pub mod log;
1717pub use log:: { emit_log, Event } ;
@@ -22,8 +22,8 @@ pub use call::call_contract;
2222const CALLDATA_ADDRESS : usize = 0x8000_0000 ;
2323
2424pub trait Contract {
25- fn call ( & self ) ;
26- fn call_with_data ( & self , calldata : & [ u8 ] ) ;
25+ fn call ( & mut self ) ;
26+ fn call_with_data ( & mut self , calldata : & [ u8 ] ) ;
2727}
2828
2929pub unsafe fn slice_from_raw_parts ( address : usize , length : usize ) -> & ' static [ u8 ] {
@@ -56,21 +56,30 @@ pub fn return_riscv(addr: u64, offset: u64) -> ! {
5656 unreachable ! ( )
5757}
5858
59- pub fn sload ( key : u64 ) -> U256 {
60- let first: u64 ;
61- let second: u64 ;
62- let third: u64 ;
63- let fourth: u64 ;
59+ pub fn sload ( key : U256 ) -> U256 {
60+ let key = key. as_limbs ( ) ;
61+ let ( val0, val1, val2, val3) : ( u64 , u64 , u64 , u64 ) ;
6462 unsafe {
65- asm ! ( "ecall" , lateout( "a0" ) first, lateout( "a1" ) second, lateout( "a2" ) third, lateout( "a3" ) fourth, in( "a0" ) key, in( "t0" ) u8 :: from( Syscall :: SLoad ) ) ;
63+ asm ! (
64+ "ecall" ,
65+ lateout( "a0" ) val0, lateout( "a1" ) val1, lateout( "a2" ) val2, lateout( "a3" ) val3,
66+ in( "a0" ) key[ 0 ] , in( "a1" ) key[ 1 ] , in( "a2" ) key[ 2 ] , in( "a3" ) key[ 3 ] ,
67+ in( "t0" ) u8 :: from( Syscall :: SLoad ) ) ;
6668 }
67- U256 :: from_limbs ( [ first , second , third , fourth ] )
69+ U256 :: from_limbs ( [ val0 , val1 , val2 , val3 ] )
6870}
6971
70- pub fn sstore ( key : u64 , value : U256 ) {
71- let limbs = value. as_limbs ( ) ;
72+ pub fn sstore ( key : U256 , value : U256 ) {
73+ let key = key. as_limbs ( ) ;
74+ let value = value. as_limbs ( ) ;
75+
7276 unsafe {
73- asm ! ( "ecall" , in( "a0" ) key, in( "a1" ) limbs[ 0 ] , in( "a2" ) limbs[ 1 ] , in( "a3" ) limbs[ 2 ] , in( "a4" ) limbs[ 3 ] , in( "t0" ) u8 :: from( Syscall :: SStore ) ) ;
77+ asm ! (
78+ "ecall" ,
79+ in( "a0" ) key[ 0 ] , in( "a1" ) key[ 1 ] , in( "a2" ) key[ 2 ] , in( "a3" ) key[ 3 ] ,
80+ in( "a4" ) value[ 0 ] , in( "a5" ) value[ 1 ] , in( "a6" ) value[ 2 ] , in( "a7" ) value[ 3 ] ,
81+ in( "t0" ) u8 :: from( Syscall :: SStore )
82+ ) ;
7483 }
7584}
7685
@@ -107,12 +116,8 @@ pub fn revert() -> ! {
107116 unreachable ! ( )
108117}
109118
110- pub fn keccak256 ( offset : u64 , size : u64 ) -> B256 {
111- let first: u64 ;
112- let second: u64 ;
113- let third: u64 ;
114- let fourth: u64 ;
115-
119+ pub fn keccak256 ( offset : u64 , size : u64 ) -> U256 {
120+ let ( first, second, third, fourth) : ( u64 , u64 , u64 , u64 ) ;
116121 unsafe {
117122 asm ! (
118123 "ecall" ,
@@ -125,21 +130,11 @@ pub fn keccak256(offset: u64, size: u64) -> B256 {
125130 in( "t0" ) u8 :: from( Syscall :: Keccak256 )
126131 ) ;
127132 }
128-
129- let mut bytes = [ 0u8 ; 32 ] ;
130-
131- bytes[ 0 ..8 ] . copy_from_slice ( & first. to_be_bytes ( ) ) ;
132- bytes[ 8 ..16 ] . copy_from_slice ( & second. to_be_bytes ( ) ) ;
133- bytes[ 16 ..24 ] . copy_from_slice ( & third. to_be_bytes ( ) ) ;
134- bytes[ 24 ..32 ] . copy_from_slice ( & fourth. to_be_bytes ( ) ) ;
135-
136- B256 :: from_slice ( & bytes)
133+ U256 :: from_limbs ( [ first, second, third, fourth] )
137134}
138135
139136pub fn msg_sender ( ) -> Address {
140- let first: u64 ;
141- let second: u64 ;
142- let third: u64 ;
137+ let ( first, second, third) : ( u64 , u64 , u64 ) ;
143138 unsafe {
144139 asm ! ( "ecall" , lateout( "a0" ) first, lateout( "a1" ) second, lateout( "a2" ) third, in( "t0" ) u8 :: from( Syscall :: Caller ) ) ;
145140 }
@@ -151,10 +146,7 @@ pub fn msg_sender() -> Address {
151146}
152147
153148pub fn msg_value ( ) -> U256 {
154- let first: u64 ;
155- let second: u64 ;
156- let third: u64 ;
157- let fourth: u64 ;
149+ let ( first, second, third, fourth) : ( u64 , u64 , u64 , u64 ) ;
158150 unsafe {
159151 asm ! ( "ecall" , lateout( "a0" ) first, lateout( "a1" ) second, lateout( "a2" ) third, lateout( "a3" ) fourth, in( "t0" ) u8 :: from( Syscall :: CallValue ) ) ;
160152 }
0 commit comments