Skip to content

Commit

Permalink
all: ouch
Browse files Browse the repository at this point in the history
  • Loading branch information
n8maninger committed Nov 5, 2024
1 parent 0bf8f9a commit 88e5a0a
Show file tree
Hide file tree
Showing 24 changed files with 1,237 additions and 1,642 deletions.
12 changes: 11 additions & 1 deletion Cargo.lock

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

42 changes: 6 additions & 36 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,36 +1,6 @@
[package]
name = "sia_core"
version = "0.0.1"
edition = "2021"
repository = "https://github.com/SiaFoundation/core-rs"
license = "MIT"
description = "Low-level SDK for interacting with the Sia decentralized storage network"
authors = ["The Sia Foundation"]
categories = ["cryptography::cryptocurrencies"]
keywords = ["sia", "decentralized", "blockchain", "depin", "storage"]

[lib]
name = "sia_core"
path = "src/lib.rs"

[dependencies]
base64 = "0.22.1"
bip39 = "2.1.0"
blake2b_simd = "1.0.2"
ed25519-dalek = "2.1.1"
hex = "0.4.3"
rayon = "1.10.0"
serde = { version = "1.0.214", features = ["derive"] }
serde-big-array = "0.5.1"
serde_json = "1.0.132"
sha2 = "0.10.8"
thiserror = "1.0.65"
time = {version = "0.3.36", features = ["serde"] }

[dev-dependencies]
rand = "0.8.5"
criterion = { version = "0.5" }

[[bench]]
name = "merkle_root"
harness = false
[workspace]
resolver = "2"
members = [
"sia",
"sia-derive",
]
12 changes: 12 additions & 0 deletions sia-derive/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "sia-derive"
version = "0.1.0"
edition = "2021"

[lib]
proc-macro = true

[dependencies]
syn = "2.0"
quote = "1.0"
proc-macro2 = "1.0"
184 changes: 184 additions & 0 deletions sia-derive/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, Data, DeriveInput, Fields};

#[proc_macro_derive(SiaEncode)]
pub fn derive_sia_encode(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
let name = &input.ident;

let encode_impl = match &input.data {
Data::Struct(data) => {
let fields = match &data.fields {
Fields::Named(fields) => {
let encodes = fields.named.iter().map(|f| {
let name = &f.ident;
quote! { self.#name.encode(w)?; }
});
quote! { #(#encodes)* }
}
Fields::Unnamed(fields) => {
let encodes = fields.unnamed.iter().enumerate().map(|(i, _)| {
let index = syn::Index::from(i);
quote! { self.#index.encode(w)?; }
});
quote! { #(#encodes)* }
}
Fields::Unit => quote! {},
};
quote! {
#fields
Ok(())
}
}
Data::Enum(_) => panic!("enums not supported"),
Data::Union(_) => panic!("unions not supported"),
};

let expanded = quote! {
impl SiaEncodable for #name {
fn encode<W: std::io::Write>(&self, w: &mut W) -> crate::encoding::Result<()> {
#encode_impl
}
}
};

TokenStream::from(expanded)
}

#[proc_macro_derive(SiaDecode)]
pub fn derive_sia_decode(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
let name = &input.ident;

let decode_impl = match &input.data {
Data::Struct(data) => {
let fields = match &data.fields {
Fields::Named(fields) => {
let decodes = fields.named.iter().map(|f| {
let name = &f.ident;
let ty = &f.ty;
quote! { #name: <#ty>::decode(r)?, }
});
quote! {
Ok(Self {
#(#decodes)*
})
}
}
Fields::Unnamed(fields) => {
let decodes = fields.unnamed.iter().map(|f| {
let ty = &f.ty;
quote! { <#ty>::decode(r)?, }
});
quote! {
Ok(Self(#(#decodes)*))
}
}
Fields::Unit => quote! { Ok(Self) },
};
fields
}
Data::Enum(_) => panic!("enums not supported"),
Data::Union(_) => panic!("unions not supported"),
};

let expanded = quote! {
impl SiaDecodable for #name {
fn decode<R: std::io::Read>(r: &mut R) -> crate::encoding::Result<Self> {
#decode_impl
}
}
};
TokenStream::from(expanded)
}

#[proc_macro_derive(V1SiaEncode)]
pub fn derive_v1_sia_encode(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
let name = &input.ident;

let encode_impl = match &input.data {
Data::Struct(data) => {
let fields = match &data.fields {
Fields::Named(fields) => {
let encodes = fields.named.iter().map(|f| {
let name = &f.ident;
quote! { self.#name.encode_v1(enc)?; }
});
quote! { #(#encodes)* }
}
Fields::Unnamed(fields) => {
let encodes = fields.unnamed.iter().enumerate().map(|(i, _)| {
let index = syn::Index::from(i);
quote! { self.#index.encode_v1(enc)?; }
});
quote! { #(#encodes)* }
}
Fields::Unit => quote! {},
};
quote! {
#fields
Ok(())
}
}
Data::Enum(_) => panic!("enums not supported"),
Data::Union(_) => panic!("unions not supported"),
};

let expanded = quote! {
impl V1SiaEncodable for #name {
fn encode_v1<W: std::io::Write>(&self, enc: &mut W) -> crate::encoding::Result<()> {
#encode_impl
}
}
};
TokenStream::from(expanded)
}

#[proc_macro_derive(V1SiaDecode)]
pub fn derive_v1_sia_decode(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
let name = &input.ident;

let decode_impl = match &input.data {
Data::Struct(data) => {
let fields = match &data.fields {
Fields::Named(fields) => {
let decodes = fields.named.iter().map(|f| {
let name = &f.ident;
let ty = &f.ty;
quote! { #name: <#ty>::decode_v1(r)?, }
});
quote! {
Ok(Self {
#(#decodes)*
})
}
}
Fields::Unnamed(fields) => {
let decodes = fields.unnamed.iter().map(|f| {
let ty = &f.ty;
quote! { <#ty>::decode_v1(r)?, }
});
quote! {
Ok(Self(#(#decodes)*))
}
}
Fields::Unit => quote! { Ok(Self) },
};
fields
}
Data::Enum(_) => panic!("enums not supported"),
Data::Union(_) => panic!("unions not supported"),
};

let expanded = quote! {
impl V1SiaDecodable for #name {
fn decode_v1<R: std::io::Read>(r: &mut R) -> crate::encoding::Result<Self> {
#decode_impl
}
}
};
TokenStream::from(expanded)
}
37 changes: 37 additions & 0 deletions sia/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
[package]
name = "sia"
version = "0.0.1"
edition = "2021"
repository = "https://github.com/SiaFoundation/sia-rs"
license = "MIT"
description = "Low-level SDK for interacting with the Sia decentralized storage network"
authors = ["The Sia Foundation"]
categories = ["cryptography::cryptocurrencies"]
keywords = ["sia", "decentralized", "blockchain", "depin", "storage"]

[lib]
name = "sia"
path = "src/lib.rs"

[dependencies]
base64 = "0.22.1"
bip39 = "2.1.0"
blake2b_simd = "1.0.2"
ed25519-dalek = "2.1.1"
hex = "0.4.3"
rayon = "1.10.0"
serde = { version = "1.0.213", features = ["derive"] }
serde-big-array = "0.5.1"
serde_json = "1.0.132"
sha2 = "0.10.8"
thiserror = "1.0.65"
time = {version = "0.3.36", features = ["serde"] }
sia-derive = { path = "../sia-derive" }

[dev-dependencies]
rand = "0.8.5"
criterion = { version = "0.5" }

[[bench]]
name = "merkle_root"
harness = false
2 changes: 1 addition & 1 deletion benches/merkle_root.rs → sia/benches/merkle_root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::hint::black_box;
fn criterion_benchmark(c: &mut Criterion) {
let sector = [0u8; 1 << 22];
c.bench_function("sector_root", |b| {
b.iter(|| sia_core::rhp::sector_root(black_box(&sector)))
b.iter(|| sia::rhp::sector_root(black_box(&sector)))
});
}

Expand Down
Loading

0 comments on commit 88e5a0a

Please sign in to comment.