Skip to content

Commit

Permalink
Add version check and new restriction test
Browse files Browse the repository at this point in the history
  • Loading branch information
mattdean-digicatapult committed Nov 15, 2023
1 parent 4fe84dc commit 8c5f5d0
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 9 deletions.
140 changes: 140 additions & 0 deletions pallets/process-validation/src/restrictions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -930,6 +930,146 @@ mod tests {
assert!(!result);
}

#[test]
fn input_restrict_metadata_type_succeeds() {
let roles = BTreeMap::from_iter(vec![(Default::default(), 1)]);
let inputs = vec![
ProcessIO {
id: 0u64,
roles: roles.clone(),
metadata: BTreeMap::new(),
},
ProcessIO {
id: 0u64,
roles: roles.clone(),
metadata: BTreeMap::from_iter(vec![(1, MetadataValue::A)]),
},
];
let result = validate_restriction::<u64, u64, u32, u32, MetadataValue, MetadataValueDisc>(
Restriction::FixedInputMetadataValueType {
index: 1,
metadata_key: 1,
metadata_value_type: MetadataValueDisc::AA,
},
&1u64,
&inputs,
&Vec::new(),
);
assert!(result);
}

#[test]
fn input_restrict_metadata_type_incorrect_type_fails() {
let roles = BTreeMap::from_iter(vec![(Default::default(), 1)]);
let inputs = vec![
ProcessIO {
id: 0u64,
roles: roles.clone(),
metadata: BTreeMap::new(),
},
ProcessIO {
id: 0u64,
roles: roles.clone(),
metadata: BTreeMap::from_iter(vec![(1, MetadataValue::A)]),
},
];
let result = validate_restriction::<u64, u64, u32, u32, MetadataValue, MetadataValueDisc>(
Restriction::FixedInputMetadataValueType {
index: 1,
metadata_key: 1,
metadata_value_type: MetadataValueDisc::BB,
},
&1u64,
&inputs,
&Vec::new(),
);
assert!(!result);
}

#[test]
fn input_restrict_metadata_type_incorrect_index_fails() {
let roles = BTreeMap::from_iter(vec![(Default::default(), 1)]);
let inputs = vec![
ProcessIO {
id: 0u64,
roles: roles.clone(),
metadata: BTreeMap::new(),
},
ProcessIO {
id: 0u64,
roles: roles.clone(),
metadata: BTreeMap::from_iter(vec![(1, MetadataValue::A)]),
},
];
let result = validate_restriction::<u64, u64, u32, u32, MetadataValue, MetadataValueDisc>(
Restriction::FixedInputMetadataValueType {
index: 0,
metadata_key: 1,
metadata_value_type: MetadataValueDisc::AA,
},
&1u64,
&inputs,
&Vec::new(),
);
assert!(!result);
}

#[test]
fn input_restrict_metadata_type_incorrect_key_fails() {
let roles = BTreeMap::from_iter(vec![(Default::default(), 1)]);
let inputs = vec![
ProcessIO {
id: 0u64,
roles: roles.clone(),
metadata: BTreeMap::new(),
},
ProcessIO {
id: 0u64,
roles: roles.clone(),
metadata: BTreeMap::from_iter(vec![(1, MetadataValue::A)]),
},
];
let result = validate_restriction::<u64, u64, u32, u32, MetadataValue, MetadataValueDisc>(
Restriction::FixedInputMetadataValueType {
index: 1,
metadata_key: 0,
metadata_value_type: MetadataValueDisc::AA,
},
&1u64,
&inputs,
&Vec::new(),
);
assert!(!result);
}

#[test]
fn input_restrict_metadata_type_bad_index_fails() {
let roles = BTreeMap::from_iter(vec![(Default::default(), 1)]);
let inputs = vec![
ProcessIO {
id: 0u64,
roles: roles.clone(),
metadata: BTreeMap::new(),
},
ProcessIO {
id: 0u64,
roles: roles.clone(),
metadata: BTreeMap::from_iter(vec![(1, MetadataValue::A)]),
},
];
let result = validate_restriction::<u64, u64, u32, u32, MetadataValue, MetadataValueDisc>(
Restriction::FixedInputMetadataValueType {
index: 2,
metadata_key: 0,
metadata_value_type: MetadataValueDisc::AA,
},
&1u64,
&inputs,
&Vec::new(),
);
assert!(!result);
}

#[test]
fn sender_has_input_role_succeeds() {
let roles = BTreeMap::from_iter(vec![(Default::default(), 1)]);
Expand Down
1 change: 1 addition & 0 deletions tools/lang/src/compiler/constants.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub const ORIGINAL_ID_KEY: &[u8] = "@original_id".as_bytes();
pub const TYPE_KEY: &[u8] = "@type".as_bytes();
pub const VERSION_KEY: &[u8] = "@version".as_bytes();
37 changes: 28 additions & 9 deletions tools/lang/src/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub use condition_transform::transform_condition_to_program;
mod helper;
use helper::to_bounded_vec;

use self::constants::TYPE_KEY;
use self::constants::{TYPE_KEY, VERSION_KEY};

#[derive(Serialize)]
pub struct Process {
Expand Down Expand Up @@ -68,22 +68,32 @@ fn make_process_restrictions(

// get restrictions for input arg types
let token_type_key = TokenMetadataKey::try_from(TYPE_KEY.to_vec()).unwrap();
let version_type_key = TokenMetadataKey::try_from(VERSION_KEY.to_vec()).unwrap();
let input_arg_conditions = fn_decl
.inputs
.value
.iter()
.enumerate()
.map(|(index, input)| {
Ok(vec![BooleanExpressionSymbol::Restriction(
dscp_runtime_types::Restriction::FixedInputMetadataValue {
Ok(vec![
BooleanExpressionSymbol::Restriction(dscp_runtime_types::Restriction::FixedInputMetadataValue {
index: index as u32,
metadata_key: token_type_key.clone(),
metadata_value: TokenMetadataValue::Literal(to_bounded_vec(AstNode {
value: input.value.token_type.value.as_bytes().to_owned(),
span: input.value.token_type.span,
})?),
},
)])
}),
BooleanExpressionSymbol::Op(BooleanOperator::And),
BooleanExpressionSymbol::Restriction(dscp_runtime_types::Restriction::FixedInputMetadataValue {
index: index as u32,
metadata_key: version_type_key.clone(),
metadata_value: TokenMetadataValue::Literal(to_bounded_vec(AstNode {
value: "1".as_bytes().to_owned(),
span: input.value.token_type.span,
})?),
}),
])
})
.collect::<Result<Vec<_>, _>>()?;

Expand All @@ -94,16 +104,25 @@ fn make_process_restrictions(
.iter()
.enumerate()
.map(|(index, output)| {
Ok(vec![BooleanExpressionSymbol::Restriction(
dscp_runtime_types::Restriction::FixedOutputMetadataValue {
Ok(vec![
BooleanExpressionSymbol::Restriction(dscp_runtime_types::Restriction::FixedOutputMetadataValue {
index: index as u32,
metadata_key: token_type_key.clone(),
metadata_value: TokenMetadataValue::Literal(to_bounded_vec(AstNode {
value: output.value.token_type.value.as_bytes().to_owned(),
span: output.value.token_type.span,
})?),
},
)])
}),
BooleanExpressionSymbol::Op(BooleanOperator::And),
BooleanExpressionSymbol::Restriction(dscp_runtime_types::Restriction::FixedOutputMetadataValue {
index: index as u32,
metadata_key: version_type_key.clone(),
metadata_value: TokenMetadataValue::Literal(to_bounded_vec(AstNode {
value: "1".as_bytes().to_owned(),
span: output.value.token_type.span,
})?),
}),
])
})
.collect::<Result<Vec<_>, _>>()?;

Expand Down

0 comments on commit 8c5f5d0

Please sign in to comment.