Skip to content

Commit

Permalink
feat(evm): add dyn types in sol-abi
Browse files Browse the repository at this point in the history
  • Loading branch information
clearloop committed Feb 26, 2024
1 parent b58839f commit 677c35a
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 5 deletions.
16 changes: 16 additions & 0 deletions codegen/src/codegen/dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ impl Dispatcher {
// 1. drop selector.
// 2. load calldata to stack.
// 3. jump to the callee function.
//
// TODO: Parse bytes from the selector.
fn process(&mut self, len: usize, last: bool) -> Result<bool> {
let len = len as u8;
if last && len == 0 {
Expand All @@ -151,6 +153,20 @@ impl Dispatcher {
}

if len > 0 {
// FIXME: Using the length of parameters here
// is incorrect once we have params have length
// over than 4 bytes.
//
// 1. decode the abi from signature, if contains
// bytes type, use `calldatacopy` to load the data
// on stack.
//
// 2. if the present param is a 4 bytes value, use
// `calldataload[n]` directly.
//
// Actually 1. is more closed to the common cases,
// what 4 bytes for in EVM?

// [ ret, callee ] -> [ param * len, ret, callee ]
for p in (0..len).rev() {
let offset = 4 + p * 32;
Expand Down
2 changes: 2 additions & 0 deletions codegen/src/visitor/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ impl Function {
pub fn _local_get(&mut self, local_index: u32) -> Result<()> {
let local_index = local_index as usize;
if self.is_main && local_index < self.ty.params().len() {
// Parsing data from selector.
self._local_get_calldata(local_index)
} else {
// Passing data between local functions.
self._local_get_var(local_index)
}
}
Expand Down
6 changes: 5 additions & 1 deletion evm/abi/README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
# Solidity ABI

An implementation of solidity ABI in rust.
An straightforward solidity ABI implementation in rust.

Currently only used by the zink language, so the provided features
is syncing with the development of zink.

If you are looking for a complete implementation of solidity ABI
in rust, plz check [alloy-core](https://github.com/alloy-rs/core).


## LICENSE

GPL-3.0
23 changes: 19 additions & 4 deletions evm/abi/src/arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,17 @@ pub enum Param {
UInt32,
/// A 64-bit unsigned integer.
UInt64,
/// An unknown type.
/// An EVM address.
Address,
/// A boolean type.
Bool,
/// A byte array.
#[default]
Unknown,
Bytes,
/// A string type.
String,
/// An unknown type.
Unknown(String),
}

impl From<&str> for Param {
Expand All @@ -41,7 +49,10 @@ impl From<&str> for Param {
"i64" | "int64" => Param::Int64,
"u32" | "uint32" => Param::UInt32,
"usize" | "u64" | "uint64" => Param::UInt64,
_ => Param::Unknown,
"Address" => Param::Address,
"Bytes" | "Vec<u8>" => Param::Bytes,
"String" => Param::String,
_ => Param::Unknown(s.to_string()),
}
}
}
Expand All @@ -61,7 +72,11 @@ impl AsRef<str> for Param {
Param::Int64 => "int64",
Param::UInt32 => "uint32",
Param::UInt64 => "uint64",
Param::Unknown => "unknown",
Param::Address => "address",
Param::Bool => "bool",
Param::Bytes => "bytes",
Param::String => "string",
Param::Unknown(ty) => ty.as_ref(),
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions zink/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ mod storage;
pub use self::{asm::Asm, event::Event, storage::Storage};
pub use zink_codegen::{constructor, external, storage, Event};

/// EVM address in rust.
pub type Address = [u8; 20];

// Panic hook implementation
#[cfg(target_arch = "wasm32")]
#[panic_handler]
Expand Down

0 comments on commit 677c35a

Please sign in to comment.