Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

std::abi #856

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
223 changes: 223 additions & 0 deletions crates/library/std/src/abi.fe
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
use ingot::evm
use ingot::context::CalldataReader
use ingot::traits::Max
use ingot::buf::{MemoryBuffer, MemoryBufferReader, MemoryBufferWriter}

pub trait AbiDecode {
fn decode(mut reader: CalldataReader) -> Self;
}

pub trait AbiEncode {
fn encode(self, mut writer: MemoryBufferWriter);
}


// unsigned integers

impl AbiDecode for u256 {
fn decode(mut reader: CalldataReader) -> Self {
return reader.read_u256()
}
}

impl AbiEncode for u256 {
fn encode(self, mut writer: MemoryBufferWriter) {
return writer.write(value: self)
}
}

impl AbiDecode for u128 {
fn decode(mut reader: CalldataReader) -> Self {
let value: u256 = reader.read_u256()
if value > u128::max() {
revert
} else {
return u128(value)
}
}
}

impl AbiEncode for u128 {
fn encode(self, mut writer: MemoryBufferWriter) {
return writer.write(value: u256(self))
}
}

impl AbiDecode for u64 {
fn decode(mut reader: CalldataReader) -> Self {
let value: u256 = reader.read_u256()
if value > u64::max() {
revert
} else {
return u64(value)
}
}
}

impl AbiEncode for u64 {
fn encode(self, mut writer: MemoryBufferWriter) {
return writer.write(value: u256(self))
}
}

impl AbiDecode for u32 {
fn decode(mut reader: CalldataReader) -> Self {
let value: u256 = reader.read_u256()
if value > u32::max() {
revert
} else {
return u32(value)
}
}
}

impl AbiEncode for u32 {
fn encode(self, mut writer: MemoryBufferWriter) {
return writer.write(value: u256(self))
}
}

impl AbiDecode for u16 {
fn decode(mut reader: CalldataReader) -> Self {
let value: u256 = reader.read_u256()
if value > u16::max() {
revert
} else {
return u16(value)
}
}
}

impl AbiEncode for u16 {
fn encode(self, mut writer: MemoryBufferWriter) {
return writer.write(value: u256(self))
}
}

impl AbiDecode for u8 {
fn decode(mut reader: CalldataReader) -> Self {
let value: u256 = reader.read_u256()
if value > u8::max() {
revert
} else {
return u8(value)
}
}
}

impl AbiEncode for u8 {
fn encode(self, mut writer: MemoryBufferWriter) {
return writer.write(value: u256(self))
}
}

// signed integers

// todo

// misc types

impl AbiDecode for bool {
fn decode(mut reader: CalldataReader) -> Self {
let value: u256 = reader.read_u256()

if value == 0 {
return false
} else if value == 1 {
return true
} else {
revert
}
}
}

impl AbiEncode for bool {
fn encode(self, mut writer: MemoryBufferWriter) {
if self {
writer.write(value: 1)
} else {
writer.write(value: 0)
}
}
}

impl AbiDecode for address {
fn decode(mut reader: CalldataReader) -> Self {
let value: u256 = reader.read_u256()

if value > 2 ^ 160 {
revert
} else {
return address(value)
}
}
}

impl AbiEncode for address {
fn encode(self, mut writer: MemoryBufferWriter) {
writer.write(value: u256(self))
}
}

// // generic impl of array encoding
// impl AbiEncode for Array<T, N>
// where
// T: AbiEncode
// {
// fn encode(self, mut writer: MemoryBufferWriter) {
// let i: usize = 0

// while i < N {
// self[i].encode(buf)
// i += 1
// }
// }
// }

// // generic impl of array decoding
// impl AbiDecode for Array<T, N>
// where
// T: AbiDecode
// {
// fn decode(mut reader: CalldataReader) -> Self {
// let decoded_array: Self
// let i: usize = 0

// while i < N {
// decoded_array[i] = T::decode(buf)
// i += 1
// }

// return decoded_array
// }
// }

// // tuple encode derivation
// impl AbiEncode for (T1, T2, ...)
// where
// T1: AbiEncode
// T2: AbiEncode
// ...
// {
// fn encode(self, mut writer: MemoryBufferWriter) {
// self.item0.encode(writer)
// self.item1.encode(writer)
// ...
// }
// }

// // tuple decode derivation
// impl AbiDecode for (T1, T2, ...)
// where
// T1: AbiDecode
// T1: AbiDecode
// ...
// {
// fn decode(mut reader: CalldataReader) -> Self {
// return (
// u256::decode(reader),
// u256::decode(reader)
// ...
// )
// }
// }
45 changes: 45 additions & 0 deletions crates/library/std/src/buf.fe
Original file line number Diff line number Diff line change
Expand Up @@ -297,3 +297,48 @@ pub struct RawCallBuffer {
return self.buf.writer()
}
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delete this

pub struct CalldataReader {
cur: Cursor

fn read_offset(mut self, len: u256) -> u256 {
return self.cur.advance(len)
}

fn read_n(mut self, len: u256) -> u256 {
let offset: u256 = self.read_offset(len)
unsafe {
let value: u256 = evm::call_data_load(offset)
return evm::shr(bits: 256 - len * 8, value)
}
}

pub fn read_u8(mut self) -> u8 {
return u8(self.read_n(len: 1))
}

pub fn read_u16(mut self) -> u16 {
return u16(self.read_n(len: 2))
}

pub fn read_u32(mut self) -> u32 {
return u32(self.read_n(len: 4))
}

pub fn read_u64(mut self) -> u64 {
return u64(self.read_n(len: 8))
}

pub fn read_u128(mut self) -> u128 {
return u128(self.read_n(len: 16))
}

pub fn read_u256(mut self) -> u256 {
let offset: u256 = self.read_offset(len: 32)
unsafe {
let value: u256 = evm::mload(offset)
return value
}
}

}
47 changes: 47 additions & 0 deletions crates/library/std/src/context.fe
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,46 @@ pub trait Emittable {
fn emit(self, _ val: OutOfReachMarker);
}

pub struct CalldataReader {
cur_offset: u256
len: u256

pub unsafe fn new(len: u256) -> CalldataReader {
return CalldataReader(cur_offset: 0, len)
}

pub fn remainder(self) -> u256 {
return self.len - self.cur_offset
}

pub fn read_u256(mut self) -> u256 {
unsafe {
let value: u256 = evm::call_data_load(offset: self.cur_offset)
self.cur_offset += 32
assert self.cur_offset <= self.len
return value
}
}

pub fn read_u16(mut self) -> u16 {
unsafe {
let value: u256 = evm::call_data_load(offset: self.cur_offset)
self.cur_offset += 2
assert self.cur_offset <= self.len
return u16(evm::shr(bits: 240, value))
}
}

pub fn read_u32(mut self) -> u32 {
unsafe {
let value: u256 = evm::call_data_load(offset: self.cur_offset)
self.cur_offset += 4
assert self.cur_offset <= self.len
return u32(evm::shr(bits: 224, value))
}
}
}

pub struct Context {
pub fn base_fee(self) -> u256 {
unsafe { return evm::base_fee() }
Expand Down Expand Up @@ -107,6 +147,13 @@ pub struct Context {
}
}

pub fn calldata_reader(self) -> CalldataReader {
unsafe {
let len: u256 = evm::call_data_size()
return CalldataReader::new(len)
}
}

pub fn emit<T: Emittable>(mut self, _ val: T) {
val.emit(OutOfReachMarker())
}
Expand Down
Loading