-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
415 additions
and
285 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
use super::Label; | ||
use crate::evm::{calldata::CallData, element::Element, U256, VAL_4, VAL_131072}; | ||
use std::error; | ||
|
||
#[derive(Clone)] | ||
pub(super) struct CallDataImpl { | ||
pub selector: [u8; 4], | ||
} | ||
|
||
impl CallData<Label> for CallDataImpl { | ||
fn load32(&self, offset: U256) -> Element<Label> { | ||
let mut data = [0; 32]; | ||
if offset < VAL_4 { | ||
let off = usize::try_from(offset).expect("len checked"); | ||
data[..4 - off].copy_from_slice(&self.selector()[off..]); | ||
} | ||
Element { | ||
data, | ||
label: Some(Label::CallData), | ||
} | ||
} | ||
|
||
fn load( | ||
&self, | ||
offset: U256, | ||
size: U256, | ||
) -> Result<(Vec<u8>, Option<Label>), Box<dyn error::Error>> { | ||
let sz = u16::try_from(size)?; | ||
if sz > 512 { | ||
return Err("unsupported size".into()); | ||
} | ||
let mut data = vec![0; sz as usize]; | ||
if offset < VAL_4 { | ||
let off = usize::try_from(offset).expect("len checked"); | ||
let nlen = std::cmp::min(data.len(), 4 - off); | ||
data[..nlen].copy_from_slice(&self.selector()[off..off + nlen]); | ||
} | ||
Ok((data, Some(Label::CallData))) | ||
} | ||
|
||
fn selector(&self) -> [u8; 4] { | ||
self.selector | ||
} | ||
|
||
fn len(&self) -> U256 { | ||
VAL_131072 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
use super::{element::Element, U256}; | ||
use std::error; | ||
|
||
pub trait CallData<T> { | ||
fn load32(&self, offset: U256) -> Element<T>; | ||
fn load(&self, offset: U256, size: U256) | ||
-> Result<(Vec<u8>, Option<T>), Box<dyn error::Error>>; | ||
fn len(&self) -> U256; | ||
fn selector(&self) -> [u8; 4]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
use crate::evm::U256; | ||
|
||
#[derive(Clone, Debug, Eq, PartialEq)] | ||
pub struct Element<T> { | ||
pub data: [u8; 32], | ||
pub label: Option<T>, | ||
} | ||
|
||
macro_rules! impl_tryfrom_element { | ||
($t:ty) => { | ||
impl<T> TryFrom<Element<T>> for $t { | ||
type Error = alloy_primitives::ruint::FromUintError<$t>; | ||
|
||
fn try_from(val: Element<T>) -> Result<Self, Self::Error> { | ||
U256::from_be_bytes(val.data).try_into() | ||
} | ||
} | ||
|
||
impl<T> TryFrom<&Element<T>> for $t { | ||
type Error = alloy_primitives::ruint::FromUintError<$t>; | ||
|
||
fn try_from(val: &Element<T>) -> Result<Self, Self::Error> { | ||
U256::from_be_bytes(val.data).try_into() | ||
} | ||
} | ||
}; | ||
} | ||
impl_tryfrom_element!(u8); | ||
impl_tryfrom_element!(u32); | ||
impl_tryfrom_element!(usize); | ||
|
||
impl<T> From<Element<T>> for U256 { | ||
fn from(val: Element<T>) -> Self { | ||
U256::from_be_bytes(val.data) | ||
} | ||
} | ||
|
||
impl<T> From<&Element<T>> for U256 { | ||
fn from(val: &Element<T>) -> Self { | ||
U256::from_be_bytes(val.data) | ||
} | ||
} |
Oops, something went wrong.