Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CODEOWNERS
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
* @71104
* @caiiiycuk
2 changes: 2 additions & 0 deletions Cargo.lock

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

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ tonic-prost = "0.14.2"
tower = "0.5.3"
x509-parser = "0.18.1"
zeroize = "1.8.2"
sha3 = "0.10.8"
Copy link
Member

@71104 71104 Feb 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor nitpick: did you use cargo add to add this dependency? cargo should keep the dependency list alphabetized, which I prefer.

Copy link
Collaborator Author

@caiiiycuk caiiiycuk Feb 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Never used it)) Good to know, thanks)


[dev-dependencies]
hex = "0.4"

[build-dependencies]
tonic-prost-build = "*"
5 changes: 4 additions & 1 deletion build.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
fn main() -> Result<(), Box<dyn std::error::Error>> {
tonic_prost_build::compile_protos("proto/libernet.proto")?;
tonic_prost_build::configure().compile_protos(
&["proto/libernet.proto", "proto/libernet_wasm.proto"],
&["proto"],
)?;
Ok(())
}
2 changes: 1 addition & 1 deletion proto
69 changes: 69 additions & 0 deletions src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,47 @@ impl AccountProof {
}
}

#[derive(Debug, Default)]
#[cfg_attr(test, derive(PartialEq))]
pub struct Program {
module: Option<libernet::wasm::ProgramModule>,
hash: Scalar,
}

impl Program {
pub fn new(module: libernet::wasm::ProgramModule) -> Self {
let hash = module.as_scalar();
Self {
module: Some(module),
hash,
}
}
}

impl AsScalar for Program {
fn as_scalar(&self) -> Scalar {
self.hash
}
}

impl proto::EncodeToAny for Program {
fn encode_to_any(&self) -> Result<prost_types::Any> {
prost_types::Any::from_msg(self.module.as_ref().context("missing program module")?)
.map_err(|e| anyhow!(e.to_string()))
}
}

impl proto::DecodeFromAny for Program {
fn decode_from_any(proto: &prost_types::Any) -> Result<Self> {
let proto = proto.to_msg::<libernet::wasm::ProgramModule>()?;
let hash = proto.as_scalar();
Ok(Self {
module: Some(proto),
hash,
})
}
}

#[derive(Debug, Clone, PartialEq)]
pub struct Transaction {
payload: prost_types::Any,
Expand Down Expand Up @@ -499,6 +540,7 @@ mod tests {
use super::*;
use crate::proto::{DecodeFromAny, EncodeToAny};
use crate::testing::parse_scalar;
use core::hash;
use crypto::utils;
use std::time::Duration;

Expand Down Expand Up @@ -873,4 +915,31 @@ mod tests {
assert_eq!(storage.get_u8(0x1234567eu32), 0);
assert_eq!(storage.get_u8(0x1234567fu32), 0);
}

#[test]
fn test_encode_decode_empty_program() {
let module = libernet::wasm::ProgramModule {
protocol_version: Some(1),
version: Some(libernet::wasm::Version {
number: Some(1),
encoding: Some(libernet::wasm::Encoding::Module as i32),
}),
..Default::default()
};
let hash = module.as_scalar();
let program = Program {
module: Some(module),
hash,
};
let proto = program.encode_to_any().unwrap();
let decoded = Program::decode_from_any(&proto).unwrap();
assert_eq!(program, decoded);
let proto_module = proto.to_msg::<libernet::wasm::ProgramModule>().unwrap();
assert_eq!(proto_module.protocol_version, Some(1));
assert_eq!(proto_module.version.as_ref().unwrap().number, Some(1));
assert_eq!(
proto_module.version.as_ref().unwrap().encoding,
Some(libernet::wasm::Encoding::Module as i32)
);
}
}
5 changes: 5 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ mod constants;
mod data;
mod db;
mod net;
mod program;
mod proto;
mod service;
mod ssl;
Expand All @@ -35,6 +36,10 @@ mod testing;

pub mod libernet {
tonic::include_proto!("libernet");

pub mod wasm {
tonic::include_proto!("libernet.wasm");
}
}

#[derive(Parser, Debug)]
Expand Down
Loading