-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip: experimenting with a packed transaction model
- Loading branch information
Showing
4 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
#[no_std] | ||
use mugraph_core::{types::Hash, util::BitSet8}; | ||
use smallvec::SmallVec; | ||
|
||
#[derive(Default)] | ||
pub struct Transaction { | ||
pub mask: BitSet8, | ||
pub inputs: Inputs<4>, | ||
pub outputs: Outputs<4>, | ||
pub data: SmallVec<[[u8; 256]; 8]>, | ||
} | ||
|
||
#[derive(Default)] | ||
pub struct Inputs<const N: usize> { | ||
pub parents: SmallVec<[Hash; N]>, | ||
pub indexes: SmallVec<[u8; N]>, | ||
pub asset_ids: SmallVec<[Hash; N]>, | ||
pub amounts: SmallVec<[u64; N]>, | ||
pub program_id: SmallVec<[Option<Hash>; N]>, | ||
pub data: SmallVec<[u32; N]>, | ||
} | ||
|
||
#[derive(Default)] | ||
pub struct Outputs<const N: usize> { | ||
pub asset_ids: SmallVec<[Hash; N]>, | ||
pub amounts: SmallVec<[u64; N]>, | ||
pub program_id: SmallVec<[Option<Hash>; N]>, | ||
pub data: SmallVec<[u32; N]>, | ||
} | ||
|
||
pub struct Seal { | ||
pub nullifiers: SmallVec<[Hash; 4]>, | ||
} | ||
|
||
fn test() { | ||
let transaction = Transaction::default(); | ||
|
||
let mut input_amounts = [0u64; 256]; | ||
let mut output_amounts = [0u64; 256]; | ||
|
||
for (asset_id, amount) in transaction | ||
.inputs | ||
.asset_ids | ||
.iter() | ||
.zip(transaction.inputs.amounts.iter()) | ||
{ | ||
let index = asset_id.as_bytes()[0] as usize; | ||
input_amounts[index] = input_amounts[index].wrapping_add(*amount); | ||
} | ||
|
||
for (asset_id, amount) in transaction | ||
.outputs | ||
.asset_ids | ||
.iter() | ||
.zip(transaction.outputs.amounts.iter()) | ||
{ | ||
let index = asset_id.as_bytes()[0] as usize; | ||
output_amounts[index] = output_amounts[index].wrapping_add(*amount); | ||
} | ||
|
||
let mut is_valid = true; | ||
|
||
for i in 0..256 { | ||
is_valid &= input_amounts[i] == output_amounts[i]; | ||
} | ||
|
||
assert!( | ||
is_valid, | ||
"Input and output amounts do not match for all asset IDs" | ||
); | ||
} | ||
|
||
iai::main!(test); |