Skip to content

Commit

Permalink
add wormhole-io (#37)
Browse files Browse the repository at this point in the history
* add wormhole-io

* add wormhole-io as dependency

---------

Co-authored-by: A5 Pickle <[email protected]>
  • Loading branch information
a5-pickle and a5-pickle authored Aug 22, 2023
1 parent f32d564 commit e94f18c
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 13 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ anchor-lang = "0.28.0"
serde = "1.0.171"
serde_json = "1.0.103"

wormhole-io = { path = "crates/io" }
wormhole-vaas = { path = "crates/vaas" }
wormhole-explorer-client = { path = "crates/explorer-client" }
18 changes: 18 additions & 0 deletions crates/io/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "wormhole-io"
description = "Traits for Wormhole payload serialization/deserialization"

version.workspace = true
edition.workspace = true
authors.workspace = true
license.workspace = true
homepage.workspace = true
repository.workspace = true

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
alloy-primitives = { workspace = true, optional = true }

[features]
alloy = ["dep:alloy-primitives"]
5 changes: 5 additions & 0 deletions crates/io/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mod payload;
pub use payload::TypePrefixedPayload;

mod read_write;
pub use read_write::{Readable, Writeable};
55 changes: 55 additions & 0 deletions crates/io/src/payload.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use std::io;

use crate::{Readable, Writeable};

/// Trait to capture common payload behavior. We do not recommend overwriting
/// any trait methods. Simply set the type constant and implement [`Readable`]
/// and [`Writeable`].
pub trait TypePrefixedPayload: Readable + Writeable + Clone + std::fmt::Debug {
const TYPE: Option<u8>;

/// Read the payload, including the type prefix.
fn read_typed<R: io::Read>(reader: &mut R) -> Result<Self, io::Error> {
let payload_type = u8::read(reader)?;
if payload_type == Self::TYPE.expect("Called write_typed on untyped payload") {
Self::read(reader)
} else {
Err(io::Error::new(
io::ErrorKind::InvalidData,
"Invalid payload type",
))
}
}

/// Write the payload, including the type prefix.
fn write_typed<W: io::Write>(&self, writer: &mut W) -> Result<(), io::Error> {
Self::TYPE
.expect("Called write_typed on untyped payload")
.write(writer)?;
Writeable::write(self, writer)
}

/// Read the payload, including the type prefix if applicable.
fn read_payload<R: io::Read>(reader: &mut R) -> Result<Self, io::Error> {
match Self::TYPE {
Some(_) => Self::read_typed(reader),
None => Readable::read(reader),
}
}

/// Write the payload, including the type prefix if applicable.
fn write_payload<W: io::Write>(&self, writer: &mut W) -> Result<(), io::Error> {
match Self::TYPE {
Some(_) => self.write_typed(writer),
None => Writeable::write(self, writer),
}
}

/// Returns the size of the payload, including the type prefix.
fn payload_written_size(&self) -> usize {
match Self::TYPE {
Some(_) => self.written_size() + 1,
None => self.written_size(),
}
}
}
26 changes: 15 additions & 11 deletions crates/vaas/src/read_write.rs → crates/io/src/read_write.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use std::io;

use alloy_primitives::{Address, FixedBytes, Uint};

pub trait Readable {
const SIZE: Option<usize>;

Expand Down Expand Up @@ -154,7 +152,8 @@ impl<const N: usize> Writeable for [u8; N] {
}
}

impl<const N: usize> Readable for FixedBytes<N> {
#[cfg(feature = "alloy")]
impl<const N: usize> Readable for alloy_primitives::FixedBytes<N> {
const SIZE: Option<usize> = Some(N);

fn read<R>(reader: &mut R) -> io::Result<Self>
Expand All @@ -166,7 +165,8 @@ impl<const N: usize> Readable for FixedBytes<N> {
}
}

impl<const N: usize> Writeable for FixedBytes<N> {
#[cfg(feature = "alloy")]
impl<const N: usize> Writeable for alloy_primitives::FixedBytes<N> {
fn written_size(&self) -> usize {
<Self as Readable>::SIZE.unwrap()
}
Expand All @@ -179,22 +179,24 @@ impl<const N: usize> Writeable for FixedBytes<N> {
}
}

impl<const BITS: usize, const LIMBS: usize> Readable for Uint<BITS, LIMBS> {
#[cfg(feature = "alloy")]
impl<const BITS: usize, const LIMBS: usize> Readable for alloy_primitives::Uint<BITS, LIMBS> {
const SIZE: Option<usize> = { Some(BITS * 8) };

fn read<R>(reader: &mut R) -> io::Result<Self>
where
Self: Sized,
R: io::Read,
{
let mut buf = Uint::<BITS, LIMBS>::default().to_be_bytes_vec();
let mut buf = alloy_primitives::Uint::<BITS, LIMBS>::default().to_be_bytes_vec();
reader.read_exact(buf.as_mut_slice())?;

Ok(Uint::try_from_be_slice(buf.as_slice()).unwrap())
Ok(alloy_primitives::Uint::try_from_be_slice(buf.as_slice()).unwrap())
}
}

impl<const BITS: usize, const LIMBS: usize> Writeable for Uint<BITS, LIMBS> {
#[cfg(feature = "alloy")]
impl<const BITS: usize, const LIMBS: usize> Writeable for alloy_primitives::Uint<BITS, LIMBS> {
fn written_size(&self) -> usize {
<Self as Readable>::SIZE.unwrap()
}
Expand All @@ -207,19 +209,21 @@ impl<const BITS: usize, const LIMBS: usize> Writeable for Uint<BITS, LIMBS> {
}
}

impl Readable for Address {
#[cfg(feature = "alloy")]
impl Readable for alloy_primitives::Address {
const SIZE: Option<usize> = Some(20);

fn read<R>(reader: &mut R) -> io::Result<Self>
where
Self: Sized,
R: io::Read,
{
FixedBytes::<20>::read(reader).map(Self)
alloy_primitives::FixedBytes::<20>::read(reader).map(Self)
}
}

impl Writeable for Address {
#[cfg(feature = "alloy")]
impl Writeable for alloy_primitives::Address {
fn written_size(&self) -> usize {
<Self as Readable>::SIZE.unwrap()
}
Expand Down
2 changes: 2 additions & 0 deletions crates/vaas/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ homepage.workspace = true
repository.workspace = true

[dependencies]
wormhole-io = { workspace = true, features = ["alloy"] }

alloy-primitives.workspace = true
hex-literal.workspace = true

Expand Down
3 changes: 1 addition & 2 deletions crates/vaas/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
mod read_write;
pub use read_write::{Readable, Writeable};
pub use wormhole_io::{Readable, Writeable};

pub mod utils;
pub use utils::{keccak256, quorum};
Expand Down

0 comments on commit e94f18c

Please sign in to comment.