Skip to content

Commit

Permalink
feat: rewrite programs using new packed transaction repr
Browse files Browse the repository at this point in the history
  • Loading branch information
cfcosta committed Aug 4, 2024
1 parent 1ddd080 commit 2065e37
Show file tree
Hide file tree
Showing 22 changed files with 179 additions and 510 deletions.
36 changes: 0 additions & 36 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ serde_bytes = "0.11.15"

proptest = { version = "1.5.0", optional = true }
test-strategy = { version = "0.4.0", optional = true }
smallvec = { version = "1.13.2", features = ["serde"] }
smallvec = { version = "1.13.2", features = ["serde"], default-features = false }

[features]
default = ["proptest"]
Expand Down
2 changes: 1 addition & 1 deletion core/programs/guest/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ edition = "2021"

[dependencies]
mugraph-core = { path = "../..", default-features = false }
risc0-zkvm = { version = "1.0.5", features = ["std"] }
risc0-zkvm = { version = "1.0.5", default-features = false }
53 changes: 53 additions & 0 deletions core/programs/guest/benches/validate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#![no_std]
#![feature(test)]
extern crate test;

use core::ops::Deref;

use test::Bencher;

#[derive(Debug, Clone, Copy)]
#[repr(transparent)]
pub struct Hash([u8; 32]);

impl Deref for Hash {
type Target = [u8; 32];

fn deref(&self) -> &Self::Target {
&self.0
}
}

#[bench]
fn bench_validate(b: &mut Bencher) {
let transaction = Transaction {
manifest: Manifest {
programs: ProgramSet {
validate: VALIDATE_ID.into(),
},
},
inputs: Inputs {
parents: [[0; 32].into(); 4],
indexes: [0, 1, 2, 3],
asset_ids: [1, 2, 3, 1],
amounts: [100, 200, 300, 400],
program_id: [[0; 32].into(); 4],
data: [u32::MAX; 4],
},
outputs: Outputs {
asset_ids: [1, 2, 3, 1],
amounts: [150, 200, 300, 350],
program_id: [[0; 32].into(); 4],
data: [u32::MAX; 4],
},
data: [0; 256 * 8],
assets: [
[1; 32].into(),
[2; 32].into(),
[3; 32].into(),
[0; 32].into(),
],
};

b.iter(|| validate(&transaction));
}
8 changes: 0 additions & 8 deletions core/programs/guest/src/bin/apply.rs

This file was deleted.

8 changes: 0 additions & 8 deletions core/programs/guest/src/bin/compose.rs

This file was deleted.

9 changes: 9 additions & 0 deletions core/programs/guest/src/bin/validate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#![no_std]

use mugraph_core::programs::validate;
use risc0_zkvm::guest::env;

fn main() {
validate(&env::read());
env::write(&env::cycle_count());
}
1 change: 0 additions & 1 deletion core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@ pub mod error;
pub mod programs;
pub mod types;
pub mod util;
pub mod wallet;
73 changes: 0 additions & 73 deletions core/src/programs/apply.rs

This file was deleted.

30 changes: 0 additions & 30 deletions core/src/programs/compose.rs

This file was deleted.

5 changes: 2 additions & 3 deletions core/src/programs/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
mod apply;
mod compose;
mod validate;

pub use self::{apply::*, compose::*};
pub use self::validate::*;
35 changes: 35 additions & 0 deletions core/src/programs/validate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use crate::types::*;

#[inline(always)]
#[no_mangle]
pub fn validate(transaction: &Transaction) {
let mut amounts = [0u64; 4];

for i in 0..4 {
assert_ne!(transaction.inputs.amounts[i], 0);

let index = transaction
.assets
.iter()
.position(|a| a[0] == transaction.inputs.asset_ids[i])
.unwrap_or(0);

amounts[index] = amounts[index]
.checked_add(transaction.inputs.amounts[i])
.unwrap();
}

for i in 0..4 {
let index = transaction
.assets
.iter()
.position(|a| a[0] == transaction.outputs.asset_ids[i])
.unwrap_or(0);

amounts[index] = amounts[index]
.checked_sub(transaction.outputs.amounts[i])
.unwrap();
}

assert_eq!(amounts, amounts);
}
4 changes: 1 addition & 3 deletions core/src/types/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ use crate::types::*;
#[cfg_attr(feature = "proptest", derive(test_strategy::Arbitrary))]
pub struct ProgramSet {
#[n(0)]
pub apply: Hash,
#[n(1)]
pub compose: Hash,
pub validate: Hash,
}

#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode, Serialize, Deserialize)]
Expand Down
47 changes: 40 additions & 7 deletions core/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,51 @@ use serde::{Deserialize, Serialize};

mod hash;
mod manifest;
mod note;
mod operation;
mod reaction;
mod sealed;

pub use self::{hash::*, manifest::*, note::*, operation::*, reaction::*, sealed::*};
pub use self::{hash::*, manifest::*};

#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode, Serialize, Deserialize)]
#[cfg_attr(feature = "proptest", derive(test_strategy::Arbitrary))]
pub struct Request<T> {
pub struct Transaction {
#[n(0)]
pub manifest: Manifest,
#[n(1)]
pub data: T,
pub inputs: Inputs,
#[n(2)]
pub outputs: Outputs,
#[n(3)]
#[serde(with = "serde_bytes")]
pub data: [u8; 256 * 8],
#[n(4)]
pub assets: [Hash; 4],
}

#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode, Serialize, Deserialize)]
#[cfg_attr(feature = "proptest", derive(test_strategy::Arbitrary))]
pub struct Inputs {
#[n(0)]
pub parents: [Hash; 4],
#[n(1)]
pub indexes: [u8; 4],
#[n(2)]
pub asset_ids: [u8; 4],
#[n(3)]
pub amounts: [u64; 4],
#[n(4)]
pub program_id: [Hash; 4],
#[n(5)]
pub data: [u8; 4],
}

#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode, Serialize, Deserialize)]
#[cfg_attr(feature = "proptest", derive(test_strategy::Arbitrary))]
pub struct Outputs {
#[n(0)]
pub asset_ids: [u8; 4],
#[n(1)]
pub amounts: [u64; 4],
#[n(2)]
pub program_id: [Hash; 4],
#[n(3)]
pub data: [u8; 4],
}
Loading

0 comments on commit 2065e37

Please sign in to comment.