Skip to content

Commit

Permalink
hacking
Browse files Browse the repository at this point in the history
  • Loading branch information
Grant Wuerker committed Mar 15, 2023
1 parent 616b58a commit 8a72457
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 135 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

161 changes: 46 additions & 115 deletions crates/library/std/src/abi.fe
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
// use ingot::evm
use std::evm
use std::traits::Max
use std::buf::{ReadBuffer, WriteBuffer}
use ingot::evm
use ingot::traits::Max
use ingot::buf::{MemoryBuffer, ReadBuffer, WriteBuffer}

// does not compile
// trait Abi {
// // fn decode<R: buf::ReadBuffer>(mut buf: R) -> Self;
// fn decode<R: ReadBuffer>(mut buf: R) -> Self;
// fn encode<W: WriteBuffer>(self, mut buf: W);
// }

trait Abi {
// fn decode<R: buf::ReadBuffer>(mut buf: R) -> Self;
fn decode<R: ReadBuffer>(mut buf: R) -> Self;
fn encode<W: WriteBuffer>(self, mut buf: W);
pub trait Abi {
fn decode(mut buf: MemoryBuffer) -> Self;
fn encode(self, mut buf: MemoryBuffer);
}

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

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

// impl Abi for u128 {
// fn decode(mut buf: ReadBuffer) -> Self {
// fn decode(mut buf: MemoryBuffer) -> Self {
// let value: u256 = buf.read_word()
// if value > u128::max() {
// revert
Expand All @@ -29,108 +34,34 @@ trait Abi {
// }
// }

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


// const HASH_SCRATCH_SPACE_START: u256 = 0x00
// const HASH_SCRATCH_SPACE_SIZE: u256 = 64

// #test
// 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 buf: ReadBuffer = ReadBuffer::new_mem(
// start: HASH_SCRATCH_SPACE_START,
// end: HASH_SCRATCH_SPACE_START + HASH_SCRATCH_SPACE_SIZE
// )

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

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

// 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 array and tuple derived 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 array_decode() {
// evm::mstore(offset: HASH_SCRATCH_SPACE_START, value: 26)
// evm::mstore(offset: HASH_SCRATCH_SPACE_START + 32, value: 42)
// example array implementation
impl Abi for Array<u256, 2> {
fn encode(self, mut buf: MemoryBuffer) {
self[0].encode(buf)
self[1].encode(buf)
}

// 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_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
// )
fn decode(mut buf: MemoryBuffer) -> Self {
return [u256::decode(buf), u256::decode(buf)]
}
}

// // 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
// }
// example tuple implementation
impl Abi for (u256, u256) {
fn encode(self, mut buf: MemoryBuffer) {
self.item0.encode(buf)
self.item1.encode(buf)
}

fn decode(mut buf: MemoryBuffer) -> Self {
// panics
// return (u256::decode(buf), u256::decode(buf)
return (u256::decode(buf), u256::decode(buf))
}
}
28 changes: 12 additions & 16 deletions crates/library/std/src/buf.fe
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::evm
use ingot::evm

trait WriteBuffer {
fn write_word(mut self, offset: u256, value: u256);
pub trait WriteBuffer {
fn write_word(mut self, value: u256);
}

trait ReadBuffer {
fn read_word(mut self, offset: u256) -> u256;
pub trait ReadBuffer {
fn read_word(mut self) -> u256;
}

pub struct CalldataBuffer {
Expand All @@ -32,33 +32,29 @@ pub struct MemoryBuffer {
impl ReadBuffer for CalldataBuffer {
fn read_word(mut self) -> u256 {
unsafe {
let offset = self.curr_offset
let offset: u256 = self.curr_offset
self.curr_offset += 32
return evm::call_data_load(offset)
}
}
}

impl ReadBuffer for MemoryBuffer {
fn read_word(self, offset: u256) -> u256 {
fn read_word(mut self) -> u256 {
unsafe {
let offset: u256 = self.curr_offset
self.curr_offset += 32
return evm::mload(offset: self.start + offset)
}
}
}

impl WriteBuffer for MemoryBuffer {
fn write_word(self, offset: u256, value: u256) {
fn write_word(mut self, value: u256) {
unsafe {
let offset: u256 = self.curr_offset
self.curr_offset += 32
evm::mstore(offset: self.start + offset, value)
}
}
}

const HASH_SCRATCH_SPACE_START: u256 = 0x00
const HASH_SCRATCH_SPACE_SIZE: u256 = 64

#test
unsafe fn buf_read() {

}
1 change: 1 addition & 0 deletions crates/tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ repository = "https://github.com/ethereum/fe"
[dependencies]
fe-test-runner = {path = "../test-runner", version = "^0.21.0-alpha"}
fe-driver = {path = "../driver", version = "^0.21.0-alpha"}
fe-common = {path = "../common", version = "^0.21.0-alpha"}
dir-test = "^0.1"

[features]
Expand Down
73 changes: 73 additions & 0 deletions crates/tests/fixtures/files/std_lib.fe
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// error: cannot glob import from ingot
// use std::{abi::Abi, evm}

use std::abi::{Abi, MemoryBuffer}
use std::evm

const HASH_SCRATCH_SPACE_START: u256 = 0x00
const HASH_SCRATCH_SPACE_SIZE: u256 = 64

#test
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 buf: MemoryBuffer = MemoryBuffer::new(
start: HASH_SCRATCH_SPACE_START,
end: HASH_SCRATCH_SPACE_START + HASH_SCRATCH_SPACE_SIZE
)

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

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

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
}

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

#test
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 buf: MemoryBuffer = MemoryBuffer::new(
start: HASH_SCRATCH_SPACE_START,
end: HASH_SCRATCH_SPACE_START + HASH_SCRATCH_SPACE_SIZE
)

// 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: MemoryBuffer = MemoryBuffer::new(
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
}
19 changes: 15 additions & 4 deletions crates/tests/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
#![cfg(feature = "solc-backend")]
#![allow(dead_code)]
use dir_test::{dir_test, Fixture};
use fe_common::diagnostics::print_diagnostics;
use fe_test_runner::TestSink;

#[dir_test(dir: "$CARGO_MANIFEST_DIR/fixtures/files", glob: "*.fe")]
fn single_file_test_run(fixture: Fixture<&str>) {
let mut db = fe_driver::Db::default();
let tests =
fe_driver::compile_single_file_tests(&mut db, fixture.path(), fixture.content(), true)
.expect("failed to compile tests")
.1;

let tests = match fe_driver::compile_single_file_tests(
&mut db,
fixture.path(),
fixture.content(),
true,
) {
Ok((_, tests)) => tests,
Err(error) => {
eprintln!("Unable to compile {}.", fixture.path());
print_diagnostics(&db, &error.0);
panic!("failed to compile tests")
}
};

let mut test_sink = TestSink::default();

Expand Down

0 comments on commit 8a72457

Please sign in to comment.