Implement CODESIZE

Signed-off-by: xermicus <cyrill@parity.io>
This commit is contained in:
xermicus
2024-06-06 15:10:21 +02:00
parent 39d78179d4
commit 10c7045e15
10 changed files with 73 additions and 24 deletions
+14
View File
@@ -126,6 +126,8 @@ sol!(
sol!(
contract ExtCode {
function ExtCodeSize(address who) public view returns (uint ret);
function CodeSize() public pure returns (uint ret);
}
);
@@ -461,6 +463,18 @@ impl Contract {
}
}
pub fn code_size() -> Self {
let code = include_str!("../contracts/ExtCode.sol");
let name = "ExtCode";
Self {
name,
evm_runtime: crate::compile_evm_bin_runtime(name, code),
pvm_runtime: crate::compile_blob(name, code),
calldata: ExtCode::CodeSizeCall::new(()).abi_encode(),
}
}
pub fn memcpy(payload: Vec<u8>) -> Self {
let code = include_str!("../contracts/MCopy.sol");
let name = "MCopy";
+13 -7
View File
@@ -884,20 +884,26 @@ fn link_host_functions(engine: &Engine) -> Linker<Transaction> {
|caller: Caller<Transaction>, address_ptr: u32| {
let (caller, transaction) = caller.split();
let bytes = caller.read_memory_into_vec(address_ptr, 32)?;
let word = U256::from_le_slice(&bytes);
let address = Address::from_word(word.into());
let address = if address_ptr == u32::MAX {
transaction.top_frame().callee
} else {
let bytes = caller.read_memory_into_vec(address_ptr, 32)?;
let word = U256::from_le_slice(&bytes);
Address::from_word(word.into())
};
log::info!("{}", address);
Ok(transaction
let code_size = transaction
.state
.accounts
.get(&address)
.and_then(|account| account.contract)
.and_then(|blob_hash| transaction.state.blobs.get(&blob_hash))
.map(|code| code.len())
.unwrap_or_default() as u32)
.unwrap_or_default() as u32;
log::info!("code size of {address} = {code_size}");
Ok(code_size)
},
)
.unwrap();
+9
View File
@@ -525,6 +525,15 @@ fn ext_code_size() {
assert_eq!(received, expected);
}
#[test]
fn code_size() {
let contract = Contract::code_size();
let (_, output) = assert_success(&contract, false);
let expected = U256::from(contract.pvm_runtime.len());
let received = U256::from_be_slice(&output.data);
assert_eq!(expected, received);
}
#[test]
fn value_transfer() {
// Succeeds in remix (shanghai) but traps the interpreter