Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronc committed Sep 27, 2024
1 parent b23fcaf commit afd0de8
Show file tree
Hide file tree
Showing 32 changed files with 315 additions and 123 deletions.
2 changes: 2 additions & 0 deletions rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ members = [
"message_api",
"testing",
"util/simple_time",
"codecs/proto", "codecs/evm",
"vm/wasmtime", "vm/api", "vm/hypervisor",
]

[workspace.package]
Expand Down
14 changes: 14 additions & 0 deletions rust/codecs/evm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "ixc-codec-solidity-abi"
version = "0.0.1"
edition = "2021"
repository.workspace = true
license.workspace = true
publish = false

[dependencies]
ixc_schema = { path = "../../schema" }
alloy-sol-types = "0.8.5"

[lints]
workspace = true
1 change: 1 addition & 0 deletions rust/codecs/evm/src/decoder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
struct Decoder {}
31 changes: 31 additions & 0 deletions rust/codecs/evm/src/encoder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use alloy_sol_types::abi::encode;
use alloy_sol_types::SolValue;
use ixc_schema::encoder::EncodeError;
use ixc_schema::structs::StructEncodeVisitor;
use ixc_schema::value::Value;

struct Encoder {}

impl ixc_schema::encoder::Encoder for Encoder {
fn encode_u32(&mut self, x: u32) -> Result<(), EncodeError> {
let bz = <u32 as SolValue>::abi_encode(&x);
todo!()
}

fn encode_u128(&mut self, x: u128) -> Result<(), EncodeError> {
let bz = <u128 as SolValue>::abi_encode(&x);
todo!()
}

fn encode_str(&mut self, x: &str) -> Result<(), EncodeError> {
todo!()
}

fn encode_list_slice<'a, V: Value<'a>>(&mut self, xs: &[V]) -> Result<(), EncodeError> {
todo!()
}

fn encode_struct<V: StructEncodeVisitor>(&mut self, visitor: &V) -> Result<(), EncodeError> {
todo!()
}
}
20 changes: 20 additions & 0 deletions rust/codecs/evm/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
mod encoder;
mod decoder;

use ixc_schema::buffer::WriterFactory;
use ixc_schema::codec::Codec;
use ixc_schema::encoder::EncodeError;
use ixc_schema::mem::MemoryManager;
use ixc_schema::value::Value;

pub struct SolidityABICodec;

impl Codec for SolidityABICodec {
fn encode_value<'a, V: Value<'a>, F: WriterFactory>(value: &V, writer_factory: &F) -> Result<F::Output, EncodeError> {
todo!()
}

fn decode_value<'b, 'a: 'b, V: Value<'a>>(input: &'a [u8], memory_manager: &'b MemoryManager<'a, 'a>) -> Result<V, DecodeError> {
todo!()
}
}
13 changes: 13 additions & 0 deletions rust/codecs/proto/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "ixc-codec-proto"
version = "0.0.1"
edition = "2021"
repository.workspace = true
license.workspace = true
publish = false

[dependencies]
ixc_schema = { path = "../../schema" }

[lints]
workspace = true
1 change: 1 addition & 0 deletions rust/codecs/proto/src/decoder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
struct Decoder {}
1 change: 1 addition & 0 deletions rust/codecs/proto/src/encoder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
struct Encoder {}
21 changes: 21 additions & 0 deletions rust/codecs/proto/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
mod encoder;
mod decoder;

use ixc_schema::buffer::WriterFactory;
use ixc_schema::codec::Codec;
use ixc_schema::decoder::DecodeError;
use ixc_schema::encoder::EncodeError;
use ixc_schema::mem::MemoryManager;
use ixc_schema::value::Value;

pub struct ProtobufCodec;

impl Codec for ProtobufCodec {
fn encode_value<'a, V: Value<'a>, F: WriterFactory>(value: &V, writer_factory: &F) -> Result<F::Output, EncodeError> {
todo!()
}

fn decode_value<'b, 'a: 'b, V: Value<'a>>(input: &'a [u8], memory_manager: &'b MemoryManager<'a, 'a>) -> Result<V, DecodeError> {
todo!()
}
}
17 changes: 16 additions & 1 deletion rust/message_api/src/handler.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,37 @@
//! The raw handler and host backend interfaces.
use core::alloc::Layout;
use crate::code::Code;
use crate::packet::MessagePacket;

/// A handler for an account.
pub trait Handler {
/// Handle a message packet.
fn handle(&self, message_packet: &mut MessagePacket, callbacks: &dyn HostBackend) -> HandlerCode;
}

#[non_exhaustive]
/// A host backend for the handler.
pub trait HostBackend {
/// Invoke a message packet.
fn invoke(&self, message_packet: &mut MessagePacket) -> Code;
/// Allocate memory for a message response.
/// The memory management expectation of handlers is that the caller
/// deallocates both the memory it allocated and any memory allocated
/// for the response by the callee.
/// The alloc function in the host backend should return a pointer to
/// memory that the caller knows how to free and such allocated
/// memory should be referenced in the message packet's out pointers.
unsafe fn alloc(&self, layout: Layout) -> Result<*mut u8, AllocError>;
}

/// An allocation error.
#[derive(Debug)]
pub struct AllocError;

/// A code that a handler can return.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum HandlerCode {
/// The handler completed successfully.
Ok,
/// The handler encountered an error.
HandlerError(u32),
}
2 changes: 1 addition & 1 deletion rust/schema/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ license.workspace = true
workspace = true

[dependencies]
ixc_message_api = { path = "../message_api", version = "0.0.2" }
ixc_message_api = { path = "../message_api" }
simple_time = { path = "../util/simple_time", version = "0.0.1" }
bump-scope = "0.9.0"
allocator-api2 = "0.2.18"
Expand Down
4 changes: 2 additions & 2 deletions rust/schema/src/binary/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use bump_scope::{BumpScope, BumpString};
use crate::decoder::{decode, DecodeError};
use crate::list::ListVisitor;
use crate::mem::MemoryManager;
use crate::r#struct::StructDecodeVisitor;
use crate::structs::StructDecodeVisitor;
use crate::value::Value;

pub fn decode_value<'b, 'a: 'b, V: Value<'a>>(input: &'a [u8], memory_manager: &'b MemoryManager<'a, 'a>) -> Result<V, DecodeError> {
Expand Down Expand Up @@ -129,7 +129,7 @@ mod tests {
use crate::encoder::{EncodeError, Encoder};
use crate::field::Field;
use crate::mem::MemoryManager;
use crate::r#struct::{StructDecodeVisitor, StructEncodeVisitor, StructSchema};
use crate::structs::{StructDecodeVisitor, StructEncodeVisitor, StructSchema};
use crate::types::{to_field, StrT, StructT, UIntNT};
use crate::value::Value;

Expand Down
2 changes: 1 addition & 1 deletion rust/schema/src/binary/encoder.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use bump_scope::BumpScope;
use crate::encoder::{EncodeError};
use crate::r#struct::{StructDecodeVisitor, StructEncodeVisitor};
use crate::structs::{StructDecodeVisitor, StructEncodeVisitor};
use crate::value::Value;
use crate::buffer::{ReverseWriter, WriterFactory};

Expand Down
2 changes: 2 additions & 0 deletions rust/schema/src/binary/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//! Defines a codec for the native binary format.
use crate::binary::encoder::encode_value;
use crate::buffer::WriterFactory;
use crate::codec::Codec;
Expand All @@ -9,6 +10,7 @@ use crate::value::Value;
mod encoder;
mod decoder;

/// A codec for encoding and decoding values using the native binary format.
pub struct NativeBinaryCodec;

impl Codec for NativeBinaryCodec {
Expand Down
114 changes: 17 additions & 97 deletions rust/schema/src/buffer.rs
Original file line number Diff line number Diff line change
@@ -1,97 +1,30 @@
//! Buffer utilities for encoding and decoding.
use bump_scope::{BumpScope, BumpBox};
use crate::encoder::EncodeError;

// pub trait WriterFactory {
// type Writer: Writer;
// fn new(&self, size: Option<usize>) -> Self::Writer;
// }
//
// pub trait Writer {
// fn new(size: Option<usize>) -> Self;
// // type Output;
// fn write(&mut self, bytes: &[u8]) -> Result<(), EncodeError>;
// // fn finish(self) -> Result<Self::Output, EncodeError>;
// }

// pub struct BumpWriterFactory<'a> {
// scope: &'a mut BumpScope<'a>,
// }
//
// impl<'a> BumpWriterFactory<'a> {
// pub fn new(scope: &'a mut BumpScope<'a>) -> BumpWriterFactory<'a> {
// BumpWriterFactory {
// scope,
// }
// }
// }
//
//
// pub struct SliceWriter<'a> {
// buf: &'a mut [u8],
// pos: usize,
// }
//
// impl<'a> SliceWriter<'a> {
// pub fn new(buf: &'a mut [u8]) -> SliceWriter<'a> {
// SliceWriter {
// buf,
// pos: 0,
// }
// }
//
// pub fn written(&self) -> usize {
// self.pos
// }
// }
//
// impl<'a> Writer for SliceWriter<'a> {
// type Output = &'a [u8];
//
// fn write(&mut self, bytes: &[u8]) -> Result<(), EncodeError> {
// if self.pos + bytes.len() > self.buf.len() {
// return Err(EncodeError::OutOfSpace);
// }
// self.buf[self.pos..self.pos + bytes.len()].copy_from_slice(bytes);
// self.pos += bytes.len();
// Ok(())
// }
//
// fn finish(self) -> Result<Self::Output, EncodeError> {
// Ok(&self.buf[0..self.pos])
// }
// }

// #[cfg(feature = "std")]
// impl Writer for alloc::vec::Vec<u8> {
// fn new(size: Option<usize>) -> Self {
// match size {
// Some(size) => alloc::vec::Vec::with_capacity(size),
// None => alloc::vec::Vec::new(),
// }
// }
//
// fn write(&mut self, bytes: &[u8]) -> Result<(), EncodeError> {
// self.extend_from_slice(bytes);
// Ok(())
// }
// }

/// A factory for creating writers.
pub trait WriterFactory {
/// The type of output produced by the writer.
type Output;
fn new_reverse(&self, size: usize) -> impl ReverseWriter<Self::Output>;
/// Create a new reverse writer.
fn new_reverse(&self, size: usize) -> impl ReverseWriter<Output=Self::Output>;
}

/// A writer that writes bytes slices starting from the end of a buffer.
pub trait ReverseWriter {
/// The type of output produced by the writer.
type Output;
/// Write bytes to the end of the buffer.
fn write(&mut self, bytes: &[u8]) -> Result<(), EncodeError>;
/// Get the current position in the buffer.
fn pos(&self) -> usize;
fn finish(self) -> Result<Output, EncodeError>;
/// Finish writing and return the output.
fn finish(self) -> Result<Self::Output, EncodeError>;
}

pub trait

impl<'a> WriterFactory for BumpScope<'a> {
type Output = &'a [u8];
fn new_reverse(&self, size: usize) -> impl ReverseWriter<Self::Output> {
fn new_reverse(&self, size: usize) -> impl ReverseWriter<Output=Self::Output> {
let b = self.alloc_slice_fill(size, 0);
ReverseSliceWriter {
buf: b.into_mut(),
Expand All @@ -101,12 +34,14 @@ impl<'a> WriterFactory for BumpScope<'a> {
}


pub struct ReverseSliceWriter<'a> {
struct ReverseSliceWriter<'a> {
buf: &'a mut [u8],
pos: usize,
}

impl<'a> ReverseWriter<&'a [u8]> for ReverseSliceWriter<'a> {
impl<'a> ReverseWriter for ReverseSliceWriter<'a> {
type Output = &'a [u8];

fn write(&mut self, bytes: &[u8]) -> Result<(), EncodeError> {
if self.pos < bytes.len() {
return Err(EncodeError::OutOfSpace);
Expand All @@ -123,19 +58,4 @@ impl<'a> ReverseWriter<&'a [u8]> for ReverseSliceWriter<'a> {
fn finish(self) -> Result<&'a [u8], EncodeError> {
Ok(&self.buf[self.pos..])
}
}

impl<'a> ReverseWriter<&'a [u8]> for ReverseSliceWriter<'a> {
fn write(&mut self, bytes: &[u8]) -> Result<(), EncodeError> {
if self.pos < bytes.len() {
return Err(EncodeError::OutOfSpace);
}
self.pos -= bytes.len();
self.buf[self.pos..self.pos + bytes.len()].copy_from_slice(bytes);
Ok(())
}

fn pos(&self) -> usize {
self.pos
}
}
13 changes: 7 additions & 6 deletions rust/schema/src/codec.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
use bump_scope::{Bump, BumpString, BumpVec};
//! The codec trait.
use crate::buffer::{ReverseWriter, WriterFactory};
use crate::decoder::DecodeError;
use crate::encoder::EncodeError;
use crate::mem::MemoryManager;
use crate::value::Value;
use bump_scope::{Bump, BumpVec};

/// Trait implemented by encoding protocols.
pub trait Codec {
/// Encode a value.
fn encode_value<'a, V: Value<'a>, F: WriterFactory>(value: &V, writer_factory: &F) -> Result<F::Output, EncodeError>;
/// Decode a value.
fn decode_value<'b, 'a: 'b, V: Value<'a>>(input: &'a [u8], memory_manager: &'b MemoryManager<'a, 'a>) -> Result<V, DecodeError>;
}

#[cfg(test)]
mod tests {
use alloc::boxed::Box;
use super::*;
use alloc::string::String;
use alloc::vec;
use core::any::Any;
use bump_scope::{bump_vec, Bump, BumpBox, BumpScope, BumpVec};
use core::ptr::NonNull;
use bump_scope::{bump_vec, mut_bump_vec, Bump, BumpBox, BumpScope, BumpVec, MutBumpVec};
use super::*;
extern crate std;

struct HasString {
Expand Down
Loading

0 comments on commit afd0de8

Please sign in to comment.