Skip to content

Commit

Permalink
chore: remove actions module
Browse files Browse the repository at this point in the history
  • Loading branch information
cfcosta committed Aug 3, 2024
1 parent 5859d4b commit f502706
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 95 deletions.
90 changes: 0 additions & 90 deletions core/programs/guest/src/actions/mod.rs

This file was deleted.

2 changes: 1 addition & 1 deletion core/programs/guest/src/bin/apply.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use mugraph_core::{error::Result, types::Operation};
use mugraph_core_programs_guest::actions::verify;
use mugraph_core_programs_guest::verify;
use risc0_zkvm::guest::env;

fn main() -> Result<()> {
Expand Down
92 changes: 88 additions & 4 deletions core/programs/guest/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,90 @@
pub mod actions;
use mugraph_core::{error::Result, types::*};
use risc0_zkvm::guest::env;

#[test]
pub fn test_transaction() {
assert_eq!(1, 1);
pub fn compose(operations: Vec<Operation>) -> Result<Reaction> {
let mut nullifiers = Vec::new();

for operation in operations.iter() {
verify(operation)?;

match operation {
Operation::UNSAFE_Mint { .. } => {
// Do nothing.
}
Operation::Consume { input, .. } => {
nullifiers.push(input.hash()?);
}
Operation::Fission { input, .. } => {
nullifiers.push(input.hash()?);
}
Operation::Fusion { inputs, .. } => {
for input in inputs {
nullifiers.push(input.hash()?);
}
}
}
}

Ok(Reaction { nullifiers })
}

pub fn verify(operation: &Operation) -> Result<()> {
match operation {
Operation::UNSAFE_Mint { .. } => {
// Do nothing.
}
Operation::Fission { input, outputs } => {
assert_ne!(outputs.len(), 0);

if let Some(program_id) = input.data.program_id {
env::verify(program_id, &operation.to_bytes()?)
.expect("Failed to run input program.");
}

let mut output_total = 0;

for output in outputs {
assert_eq!(output.asset_id, input.data.asset_id);
assert_ne!(output.amount, 0);

output_total += output.amount;
}

assert_eq!(input.data.amount, output_total);
}
Operation::Fusion { inputs, output } => {
assert_ne!(inputs.len(), 0);

let asset_id = inputs[0].data.asset_id;
assert_eq!(asset_id, output.asset_id);

let mut input_total = 0;

for input in inputs {
assert_eq!(asset_id, input.data.asset_id);
assert_ne!(0, input.data.amount);

if let Some(program_id) = input.data.program_id {
env::verify(program_id, &operation.to_bytes()?)
.expect("Failed to run input program.");
}

input_total += input.data.amount;
}

assert_eq!(input_total, output.amount);
}
Operation::Consume { input, output } => {
assert_eq!(input.data.asset_id, output.asset_id);
assert_ne!(0, input.data.amount);
assert_eq!(input.data.amount, output.amount);

if let Some(program_id) = input.data.program_id {
env::verify(program_id, &operation.to_bytes()?)
.expect("Failed to run input program.");
}
}
}

Ok(())
}

0 comments on commit f502706

Please sign in to comment.