Skip to content

Commit

Permalink
Added basic encoding tests for rgb/schema/script.rs
Browse files Browse the repository at this point in the history
Minor changes by cargo fmt
  • Loading branch information
rajarshimaitra committed Oct 1, 2020
1 parent 607172b commit 1954acf
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 10 deletions.
16 changes: 9 additions & 7 deletions derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,15 +312,15 @@ fn lnp_api_inner_enum(
let mut get_type = vec![];
let mut get_payload = vec![];
for v in &data.variants {
let meta = attr_list(&v.attrs, "lnp_api", example)?
.ok_or(Error::new(
v.span(),
format!(
let meta =
attr_list(&v.attrs, "lnp_api", example)?.ok_or(Error::new(
v.span(),
format!(
"Attribute macro canonical form `{}` violation: {}",
example,
"`lnp_api` attribute is required for each message enum case",
),
))?;
))?;

let type_lit: Lit =
attr_nested_one_named_value(meta.into_iter(), "type", example)?.lit;
Expand Down Expand Up @@ -358,11 +358,13 @@ fn lnp_api_inner_enum(
};

match &v.fields {
Fields::Named(_) => return proc_macro_err!(
Fields::Named(_) => {
return proc_macro_err!(
v,
"LNP API does not support requests represented by named enums",
example
),
)
}
Fields::Unnamed(args) => {
let fields = &args.unnamed;
if fields.len() > 1 {
Expand Down
64 changes: 64 additions & 0 deletions src/rgb/schema/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,68 @@ mod strict_encoding {
})
}
}

#[cfg(test)]
mod test {
use super::*;
use crate::strict_encoding::strict_encode;

#[test]
fn test_basics() {
// Test Actions and Standard procedures
test_enum_u8_exhaustive!(AssignmentAction; AssignmentAction::Validate => 0);
test_enum_u8_exhaustive!(TransitionAction; TransitionAction::GenerateBlank => 0);
test_enum_u8_exhaustive!(StandardProcedure; StandardProcedure::ConfidentialAmount => 1,
StandardProcedure::IssueControl => 2,
StandardProcedure::Prunning => 3);

// Test Procedures
assert_eq!(
vec![0xFF, 0x01],
strict_encode(&Procedure::Standard(
StandardProcedure::ConfidentialAmount
))
.unwrap()
);
assert_eq!(
vec![0xFF, 0x02],
strict_encode(&Procedure::Standard(
StandardProcedure::IssueControl
))
.unwrap()
);
assert_eq!(
vec![0xFF, 0x03],
strict_encode(&Procedure::Standard(
StandardProcedure::Prunning
))
.unwrap()
);
assert_eq!(
vec![0x00, 0x58, 0x00, 0x00, 0x00],
strict_encode(&Procedure::Simplicity { offset: 88 }).unwrap()
);

// Test Transition and Assignment ABI
let mut trans_abi = TransitionAbi::new();
trans_abi.insert(
TransitionAction::GenerateBlank,
Procedure::Standard(StandardProcedure::ConfidentialAmount),
);
assert_eq!(
vec![0x01, 0x00, 0x00, 0xff, 0x01],
strict_encode(&trans_abi).unwrap()
);

let mut assignment_abi = AssignmentAbi::new();
assignment_abi.insert(
AssignmentAction::Validate,
Procedure::Simplicity { offset: 45 },
);
assert_eq!(
vec![0x01, 0x00, 0x00, 0x00, 0x2d, 0x00, 0x00, 0x00],
strict_encode(&assignment_abi).unwrap()
);
}
}
}
6 changes: 3 additions & 3 deletions src/rgb/stash/consignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ pub(crate) mod test {
141, 136, 241, 106, 62, 52, 132, 32, //
85, 122, 53, 167, 151, 135, 178, 70, //
187, 68, 10, 209, 232, 38, 153, 97, //
//
//
// * Chain
0x6f, 0xe2, 0x8c, 0xa, 0xb6, 0xf1, 0xb3, 0x72, 0xc1, 0xa6, 0xa2,
0x46, 0xae, 0x63, 0xf7, 0x4f, 0x93, 0x1e, 0x83, 0x65, 0xe1, 0x5a,
Expand All @@ -112,7 +112,7 @@ pub(crate) mod test {
0xa6, 0xa2, 0x46, 0xae, 0x63, 0xf7, 0x4f, 0x93, 0x1e, 0x83, 0x65,
0xe1, 0x5a, 0x8, 0x9c, 0x68, 0xd6, 0x19, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x1, // Bitcoin mainnet
//
//
// * Network - replaced with Chain
// 0x43, 0x49, 0x7f, 0xd7, 0xf8, 0x26, 0x95, 0x71, 0x08, 0xf4,
// 0xa3, 0x0f, 0xd9, 0xce, 0xc3, 0xae, 0xba, 0x79,
Expand Down Expand Up @@ -158,7 +158,7 @@ pub(crate) mod test {
1, 0, // No of values
11, // Value type: I64
129, 58, 36, 95, 0, 0, 0, 0, // Value
//
//
// * Assignments
2, 0, // No of assignments
1, 0, 1, 3, 1, 0, 1, 0, 172, 198, 241, 181, 165, 20, 213, 54, 40,
Expand Down

0 comments on commit 1954acf

Please sign in to comment.