mirror of
https://github.com/pezkuwichain/revive.git
synced 2026-06-17 05:21:06 +00:00
implement address and msg.sender
Signed-off-by: Cyrill Leutwiler <bigcyrill@hotmail.com>
This commit is contained in:
@@ -75,6 +75,14 @@ sol!(
|
||||
}
|
||||
);
|
||||
|
||||
sol!(
|
||||
contract Context {
|
||||
function address_this() public view returns (address);
|
||||
|
||||
function caller() public pure returns (address);
|
||||
}
|
||||
);
|
||||
|
||||
impl Contract {
|
||||
pub fn baseline() -> Self {
|
||||
let code = include_str!("../contracts/Baseline.sol");
|
||||
@@ -196,6 +204,28 @@ impl Contract {
|
||||
calldata: Block::timestampCall::new(()).abi_encode(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn context_address() -> Self {
|
||||
let code = include_str!("../contracts/Context.sol");
|
||||
let name = "Context";
|
||||
|
||||
Self {
|
||||
evm_runtime: crate::compile_evm_bin_runtime(name, code),
|
||||
pvm_runtime: crate::compile_blob(name, code),
|
||||
calldata: Context::address_thisCall::new(()).abi_encode(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn context_caller() -> Self {
|
||||
let code = include_str!("../contracts/Context.sol");
|
||||
let name = "Context";
|
||||
|
||||
Self {
|
||||
evm_runtime: crate::compile_evm_bin_runtime(name, code),
|
||||
pvm_runtime: crate::compile_blob(name, code),
|
||||
calldata: Context::callerCall::new(()).abi_encode(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -33,8 +33,10 @@ impl Default for CallOutput {
|
||||
}
|
||||
|
||||
impl State {
|
||||
pub const ADDRESS: [u8; 20] = [1; 20];
|
||||
pub const BLOCK_NUMBER: u64 = 123;
|
||||
pub const BLOCK_TIMESTAMP: u64 = 456;
|
||||
pub const CALLER: [u8; 20] = [2; 20];
|
||||
|
||||
pub fn new(input: Vec<u8>) -> Self {
|
||||
Self {
|
||||
@@ -253,6 +255,48 @@ fn link_host_functions(engine: &Engine) -> Linker<State> {
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
linker
|
||||
.func_wrap(
|
||||
runtime_api::ADDRESS,
|
||||
|caller: Caller<State>, out_ptr: u32, out_len_ptr: u32| {
|
||||
let (mut caller, _) = caller.split();
|
||||
|
||||
let out_len = caller.read_u32(out_len_ptr)? as usize;
|
||||
assert_eq!(
|
||||
out_len,
|
||||
revive_common::BYTE_LENGTH_WORD,
|
||||
"spurious output buffer size: {out_len}"
|
||||
);
|
||||
|
||||
caller.write_memory(out_ptr, &State::ADDRESS)?;
|
||||
caller.write_memory(out_len_ptr, &(State::ADDRESS.len() as u32).to_le_bytes())?;
|
||||
|
||||
Ok(())
|
||||
},
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
linker
|
||||
.func_wrap(
|
||||
runtime_api::CALLER,
|
||||
|caller: Caller<State>, out_ptr: u32, out_len_ptr: u32| {
|
||||
let (mut caller, _) = caller.split();
|
||||
|
||||
let out_len = caller.read_u32(out_len_ptr)? as usize;
|
||||
assert_eq!(
|
||||
out_len,
|
||||
revive_common::BYTE_LENGTH_WORD,
|
||||
"spurious output buffer size: {out_len}"
|
||||
);
|
||||
|
||||
caller.write_memory(out_ptr, &State::CALLER)?;
|
||||
caller.write_memory(out_len_ptr, &(State::CALLER.len() as u32).to_le_bytes())?;
|
||||
|
||||
Ok(())
|
||||
},
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
linker
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use alloy_primitives::{FixedBytes, Keccak256, I256, U256};
|
||||
use alloy_primitives::{Address, FixedBytes, Keccak256, I256, U256};
|
||||
use alloy_sol_types::{sol, SolCall};
|
||||
use sha1::Digest;
|
||||
|
||||
@@ -280,3 +280,19 @@ fn block_timestamp() {
|
||||
let expected = U256::from(mock_runtime::State::BLOCK_TIMESTAMP);
|
||||
assert_eq!(received, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn address() {
|
||||
let state = assert_success(Contract::context_address(), true);
|
||||
let received = Address::from_slice(&state.output.data[12..]);
|
||||
let expected = Address::from(&mock_runtime::State::ADDRESS);
|
||||
assert_eq!(received, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn caller() {
|
||||
let state = assert_success(Contract::context_caller(), true);
|
||||
let received = Address::from_slice(&state.output.data[12..]);
|
||||
let expected = Address::from(&mock_runtime::State::CALLER);
|
||||
assert_eq!(received, expected);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user