Skip to content

Commit

Permalink
CalldataReader ethereum#900
Browse files Browse the repository at this point in the history
Implement CallDataReader, as per the tasks of issue ethereum#900
  • Loading branch information
saifkatoutatcom authored and saifalkatout committed Sep 28, 2023
1 parent 76483a2 commit fbf0431
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 5 deletions.
78 changes: 76 additions & 2 deletions crates/library/std/src/buf.fe
Original file line number Diff line number Diff line change
Expand Up @@ -309,11 +309,85 @@ pub struct RawCallBuffer {
return self.buf.offset()
}

pub fn reader(self) -> MemoryBufferReader {
return self.buf.reader()
pub fn reader(self) -> RawCallBufferReader {
return RawCallBufferReader::new(buf: self)
}

pub fn writer(mut self) -> MemoryBufferWriter {
return self.buf.writer()
}
}

pub struct RawCallBufferReader {
buf: RawCallBuffer
/// Returns a new reader for the given buffer.
pub fn new(buf: RawCallBuffer) -> Self {
return RawCallBufferReader(buf)
}

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

fn read_n(mut self, len: u256) -> u256 {
let offset: u256 = self.read_offset(len)
unsafe {
let value: u256 = evm::mload(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
}
}

// pub fn read_buf(mut self, len: u256) -> RawCallBuffer {
// let mut buf: RawCallBuffer = RawCallBuffer::new(len)
// let mut writer: MemoryBufferWriter = buf.writer()
//
// while true {
// let bytes_remaining: u256 = writer.remainder()
//
// if bytes_remaining >= 32 {
// writer.write(value: self.read_u256())
// } else if bytes_remaining == 0 {
// break
// } else {
// writer.write(value: self.read_u8())
// }
// }
//
// return buf
// }

// `T` has not been defined
// pub fn read<T: MemoryBufferRead>(mut self) -> T {
// T::read_buf(writer: self)
// }


}
8 changes: 5 additions & 3 deletions crates/tests/fixtures/files/raw_call.fe
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::buf::{
RawCallBuffer,
MemoryBufferReader,
MemoryBufferWriter,
MemoryBuffer
MemoryBuffer,
RawCallBufferReader
}
use std::evm

Expand Down Expand Up @@ -34,7 +34,9 @@ fn test_raw_call(mut ctx: Context) {

writer.write(value: 42)
assert ctx.raw_call(addr: address(foo), value: 0, buf)
let mut reader: MemoryBufferReader = buf.reader()
let mut reader: RawCallBufferReader = RawCallBufferReader::new(
buf
)
assert reader.read_u256() == 26

assert not ctx.raw_call(addr: address(foo), value: 0, buf)
Expand Down

0 comments on commit fbf0431

Please sign in to comment.