implement address and msg.sender

Signed-off-by: Cyrill Leutwiler <bigcyrill@hotmail.com>
This commit is contained in:
Cyrill Leutwiler
2024-05-08 16:09:23 +02:00
parent f80a96059d
commit ea63991617
14 changed files with 114 additions and 11 deletions
+1 -1
View File
@@ -1,7 +1,7 @@
{
"Baseline": 3917,
"Computation": 7363,
"ERC20": 50944,
"ERC20": 53193,
"Fibonacci": 5965,
"Flipper": 4336
}
+1 -1
View File
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
pragma solidity ^0.8;
contract Baseline {
function baseline() public payable {}
+1 -1
View File
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
pragma solidity ^0.8;
contract Block {
function timestamp() public view returns (uint ret) {
+1 -1
View File
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
pragma solidity ^0.8;
contract Computation {
function triangle_number(int64 n) public pure returns (int64 sum) {
+13
View File
@@ -0,0 +1,13 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8;
contract Context {
function address_this() public view returns (address ret) {
ret = address(this);
}
function caller() public view returns (address ret) {
ret = msg.sender;
}
}
+1 -1
View File
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
pragma solidity ^0.8;
// https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v3.0.0/contracts/token/ERC20/IERC20.sol
interface IERC20 {
+1 -1
View File
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.24;
pragma solidity ^0.8;
// https://medium.com/coinmonks/fibonacci-in-solidity-8477d907e22a
+1 -1
View File
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
pragma solidity ^0.8;
contract MSize {
uint[] public data;
+1 -1
View File
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
pragma solidity ^0.8;
contract Value {
function value() public payable returns (uint ret) {
+1 -1
View File
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
pragma solidity ^0.8;
contract Flipper {
bool coin;
+1 -1
View File
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
pragma solidity ^0.8;
contract MStore8 {
function mStore8(uint value) public pure returns (uint256 word) {
+30
View File
@@ -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)]
+44
View File
@@ -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
}
+17 -1
View File
@@ -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);
}