Skip to content

Commit

Permalink
hacking
Browse files Browse the repository at this point in the history
  • Loading branch information
Grant Wuerker committed Mar 14, 2023
1 parent c25d233 commit d694664
Showing 1 changed file with 75 additions and 24 deletions.
99 changes: 75 additions & 24 deletions crates/library/std/src/abi.fe
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// use ingot::evm
use std::evm
use std::traits::Max

enum ReadLocation {
Calldata
Expand Down Expand Up @@ -65,38 +66,33 @@ struct WriteBuffer {
}
}

// pub fn decode_array<T: Abi, const LEN: u256>(buffer: ReadBuffer<T::ABI_SIZE * LEN>) -> Array<T, LEN> {}
pub fn decode_array<T: Abi>(buffer: ReadBuffer) -> u256 {
return 0
}

trait Abi {
fn decode(mut buffer: ReadBuffer) -> Self;
fn encode(self, mut buffer: WriteBuffer);
fn decode(mut buf: ReadBuffer) -> Self;
fn encode(self, mut buf: WriteBuffer);
}

impl Abi for u256 {
fn decode(mut buffer: ReadBuffer) -> Self {
return buffer.read_word()
fn decode(mut buf: ReadBuffer) -> Self {
return buf.read_word()
}

fn encode(self, mut buffer: WriteBuffer) {
return buffer.write_word(value: self)
fn encode(self, mut buf: WriteBuffer) {
return buf.write_word(value: self)
}
}

impl Abi for u128 {
fn decode(mut buffer: ReadBuffer) -> Self {
let value: u256 = buffer.read_word()
fn decode(mut buf: ReadBuffer) -> Self {
let value: u256 = buf.read_word()
if value > u128::max() {
revert
} else {
return u128(value)
}
}

fn encode(self, mut buffer: WriteBuffer) {
return buffer.write_word(value: u256(self))
fn encode(self, mut buf: WriteBuffer) {
return buf.write_word(value: u256(self))
}
}

Expand All @@ -109,39 +105,94 @@ unsafe fn u256_decode() {
evm::mstore(offset: HASH_SCRATCH_SPACE_START, value: 26)
evm::mstore(offset: HASH_SCRATCH_SPACE_START + 32, value: 42)

let mut buffer: ReadBuffer = ReadBuffer::new_mem(
let mut buf: ReadBuffer = ReadBuffer::new_mem(
start: HASH_SCRATCH_SPACE_START,
end: HASH_SCRATCH_SPACE_START + HASH_SCRATCH_SPACE_SIZE
)

assert u256::decode(buffer) == 26
assert u256::decode(buffer) == 42
assert u256::decode(buf) == 26
assert u256::decode(buf) == 42
}

#test
unsafe fn u256_encode() {
let mut buffer: WriteBuffer = WriteBuffer::new(
let mut buf: WriteBuffer = WriteBuffer::new(
start: HASH_SCRATCH_SPACE_START,
end: HASH_SCRATCH_SPACE_START + HASH_SCRATCH_SPACE_SIZE
)

26.encode(buffer)
42.encode(buffer)
26.encode(buf)
42.encode(buf)

assert evm::mload(offset: HASH_SCRATCH_SPACE_START) == 26
assert evm::mload(offset: HASH_SCRATCH_SPACE_START + 32) == 42
}







// example derived array and tuple implementations

impl Abi for Array<u256, 2> {
fn encode(self, mut buf: WriteBuffer) {
self[0].encode(buf)
self[1].encode(buf)
}

fn decode(mut buf: ReadBuffer) -> Self {
return [u256::decode(buf), u256::decode(buf)]
}
}

impl Abi for (u256, u256) {
fn encode(self, mut buf: WriteBuffer) {
self.item0.encode(buf)
self.item1.encode(buf)
}

fn decode(mut buf: ReadBuffer) -> Self {
// panics
// return (u256::decode(buf), u256::decode(buf)
return (u256::decode(buf), u256::decode(buf))
}
}

type MyArray = Array<u256, 2>
type MyTuple = (u256, u256)

#test
unsafe fn u256_array_decode() {
unsafe fn array_decode() {
evm::mstore(offset: HASH_SCRATCH_SPACE_START, value: 26)
evm::mstore(offset: HASH_SCRATCH_SPACE_START + 32, value: 42)

let mut buffer: ReadBuffer = ReadBuffer::new_mem(
let mut buf: ReadBuffer = ReadBuffer::new_mem(
start: HASH_SCRATCH_SPACE_START,
end: HASH_SCRATCH_SPACE_START + HASH_SCRATCH_SPACE_SIZE
)

// let my_array: Array<u256, 2> = decode_array(buffer)
// parser panics
// let my_array: = Array<u256, 2>::decode(buf)
let my_array: MyArray = MyArray::decode(buf)
assert my_array[0] == 26
assert my_array[1] == 42
}

#test
unsafe fn tuple_decode() {
evm::mstore(offset: HASH_SCRATCH_SPACE_START, value: 26)
evm::mstore(offset: HASH_SCRATCH_SPACE_START + 32, value: 42)

let mut buf: ReadBuffer = ReadBuffer::new_mem(
start: HASH_SCRATCH_SPACE_START,
end: HASH_SCRATCH_SPACE_START + HASH_SCRATCH_SPACE_SIZE
)

// parser panics
// let my_tuple: (u256, u256) = (u256, u256)::decode(buf)
let my_tuple: MyTuple = MyTuple::decode(buf)
assert my_tuple.item0 == 26
assert my_tuple.item1 == 42
}

0 comments on commit d694664

Please sign in to comment.