rave_engine 0.8.0

A secure and efficient JSON Schema validation and Rhai script execution engine
Documentation
// todo:
// use crate::{
//     rhai_engine::RhaiEngine,
//     types::{ParkedSpendData, Payload, AllocationWtProof},
// };
// use hdi::prelude::{ActionHashB64, AgentPubKeyB64};
// use rmp_serde as msgpack;
// use serde_json::json;
// use std::str::FromStr;
// use zfuel::fuel::ZFuel;

// #[test]
// fn test_parse_record_error_handling() {
//     let invalid_record = json!({
//         "signed_action": {
//             "wrong_field": {}
//         }
//     });

//     let code = r#"
//         try {
//             parse_record_to_parked_amount_and_proof(spend_data[0]);
//         } catch(e) {
//             return #{
//                 "computed_values": #{
//                     "error": e
//                 }
//             };
//         }
//     "#;

//     let input = json!({
//         "spend_data": [invalid_record]
//     });
//     let result = RhaiEngine::execute(&input, msgpack::to_vec(code).unwrap()).unwrap();
//     // test partial string match
//     assert!(result
//         .computed_values
//         .unwrap()
//         .get("error")
//         .unwrap()
//         .to_string()
//         .contains("Failed to convert to Record"));
// }

// // todo: fix test
// #[test]
// fn test_parse_real_record_data() {
//     let tag1 = (ParkedSpendData {
//         allocation: vec![AllocationWtProof {
//             agent: AgentPubKeyB64::from_str(
//                 "uhCAkK5mA8l1XzHdlteX91kwTFBB4ct9Gnaypi3C1FvD0qM1knDR8",
//             )
//             .unwrap(),
//             amount: vec![ZFuel::from_str("1").unwrap()],
//         }],
//         new_balance: vec![ZFuel::from_str("0").unwrap()],
//         fees_owed: ZFuel::from_str("0").unwrap(),
//         parked_invoices: vec![],
//         note: Payload::None,
//         global_definition: ActionHashB64::from_str(
//             "uhCAkBi4aREMzPHnnNgh0N5hZj4Rs7P-9RdIAuv7bwKyVZq7CS9iq",
//         )
//         .unwrap()
//         .into(),
//     })
//     .link_tag()
//     .unwrap();

//     let tag2 = (ParkedSpendData {
//         allocation: vec![AllocationWtProof {
//             agent: AgentPubKeyB64::from_str(
//                 "uhCAkK5mA8l1XzHdlteX91kwTFBB4ct9Gnaypi3C1FvD0qM1knDR8",
//             )
//             .unwrap(),
//             amount: vec![ZFuel::from_str("2").unwrap()],
//         }],
//         new_balance: vec![ZFuel::from_str("0").unwrap()],
//         fees_owed: ZFuel::from_str("0").unwrap(),
//         parked_invoices: vec![],
//         note: Payload::None,
//         global_definition: ActionHashB64::from_str(
//             "uhCAkBi4aREMzPHnnNgh0N5hZj4Rs7P-9RdIAuv7bwKyVZq7CS9iq",
//         )
//         .unwrap()
//         .into(),
//     })
//     .link_tag()
//     .unwrap();
//     let tag3 = (ParkedSpendData {
//         allocation: vec![AllocationWtProof {
//             agent: AgentPubKeyB64::from_str(
//                 "uhCAkK5mA8l1XzHdlteX91kwTFBB4ct9Gnaypi3C1FvD0qM1knDR8",
//             )
//             .unwrap(),
//             amount: vec![ZFuel::from_str("3").unwrap()],
//         }],
//         new_balance: vec![ZFuel::from_str("0").unwrap()],
//         fees_owed: ZFuel::from_str("0").unwrap(),
//         parked_invoices: vec![],
//         note: Payload::None,
//         global_definition: ActionHashB64::from_str(
//             "uhCAkBi4aREMzPHnnNgh0N5hZj4Rs7P-9RdIAuv7bwKyVZq7CS9iq",
//         )
//         .unwrap()
//         .into(),
//     })
//     .link_tag()
//     .unwrap();
//     let records = json!([
//         {
//             "entry": "NA",
//             "signed_action": {
//                 "hashed": {
//                     "content": {
//                         "action_seq": 6,
//                         "author": [132, 32, 36, 75, 46, 203, 166, 172, 44, 182, 83, 45, 200, 112, 177, 138, 238, 247, 223, 245, 14, 141, 24, 181, 152, 15, 83, 242, 37, 6, 119, 244, 240, 191, 217, 149, 60, 140, 88],
//                         "base_address": [132, 41, 36, 194, 20, 187, 158, 254, 102, 117, 143, 242, 211, 48, 251, 187, 176, 255, 162, 112, 218, 223, 144, 188, 111, 126, 61, 156, 238, 126, 243, 135, 218, 113, 81, 127, 136, 95, 226],
//                         "link_type": 9,
//                         "prev_action": [132, 41, 36, 150, 232, 195, 198, 213, 253, 172, 16, 34, 39, 221, 185, 251, 56, 51, 146, 74, 169, 214, 189, 224, 107, 219, 180, 177, 219, 13, 174, 129, 32, 33, 104, 103, 63, 233, 115],
//                         "tag": tag1,
//                         "target_address": [132, 33, 36, 105, 142, 178, 58, 236, 235, 183, 45, 136, 26, 254, 233, 247, 139, 108, 110, 153, 81, 169, 4, 80, 63, 6, 111, 178, 114, 157, 202, 31, 11, 135, 68, 255, 93, 103, 176],
//                         "timestamp": 1740084031450828 as i64,
//                         "type": "CreateLink",
//                         "weight": {"bucket_id": 255, "units": 0},
//                         "zome_index": 0
//                     },
//                     "hash": [132, 41, 36, 51, 161, 72, 22, 190, 86, 15, 16, 114, 229, 168, 10, 8, 219, 101, 76, 20, 47, 10, 48, 3, 163, 203, 100, 70, 85, 104, 174, 22, 46, 43, 160, 125, 117, 16, 178]
//                 },
//                 "signature": [143, 130, 54, 12, 129, 93, 199, 232, 236, 66, 77, 168, 180, 134, 99, 51, 151, 95, 111, 232, 166, 17, 232, 109, 175, 34, 39, 81, 45, 229, 103, 202, 24, 75, 235, 33, 177, 50, 254, 19, 189, 109, 173, 61, 203, 93, 142, 234, 253, 175, 14, 129, 211, 240, 195, 99, 213, 178, 17, 189, 213, 0, 48, 10]
//             }
//         },
//         {
//             "entry": "NA",
//             "signed_action": {
//                 "hashed": {
//                     "content": {
//                         "action_seq": 9,
//                         "author": [132, 32, 36, 75, 46, 203, 166, 172, 44, 182, 83, 45, 200, 112, 177, 138, 238, 247, 223, 245, 14, 141, 24, 181, 152, 15, 83, 242, 37, 6, 119, 244, 240, 191, 217, 149, 60, 140, 88],
//                         "base_address": [132, 41, 36, 194, 20, 187, 158, 254, 102, 117, 143, 242, 211, 48, 251, 187, 176, 255, 162, 112, 218, 223, 144, 188, 111, 126, 61, 156, 238, 126, 243, 135, 218, 113, 81, 127, 136, 95, 226],
//                         "link_type": 9,
//                         "prev_action": [132, 41, 36, 60, 192, 241, 163, 53, 74, 26, 26, 31, 35, 6, 5, 122, 102, 103, 16, 0, 219, 35, 160, 145, 169, 245, 231, 12, 198, 101, 177, 65, 235, 180, 196, 104, 165, 181, 69],
//                         "tag": tag2,
//                         "target_address": [132, 33, 36, 105, 142, 178, 58, 236, 235, 183, 45, 136, 26, 254, 233, 247, 139, 108, 110, 153, 81, 169, 4, 80, 63, 6, 111, 178, 114, 157, 202, 31, 11, 135, 68, 255, 93, 103, 176],
//                         "timestamp": 1740084031635860 as i64,
//                         "type": "CreateLink",
//                         "weight": {"bucket_id": 255, "units": 0},
//                         "zome_index": 0
//                     },
//                     "hash": [132, 41, 36, 187, 221, 133, 240, 177, 49, 37, 100, 130, 59, 190, 54, 31, 90, 112, 67, 227, 153, 35, 169, 97, 194, 62, 220, 214, 190, 0, 87, 3, 115, 188, 86, 17, 152, 205, 9],
//                 },
//                 "signature": [155, 224, 106, 77, 143, 44, 123, 198, 66, 37, 66, 237, 140, 146, 231, 41, 27, 46, 1, 120, 13, 144, 74, 96, 43, 116, 86, 93, 187, 53, 45, 236, 152, 161, 120, 128, 176, 217, 74, 22, 54, 237, 212, 55, 204, 112, 234, 57, 12, 138, 149, 121, 22, 2, 148, 254, 21, 115, 54, 196, 249, 157, 31, 3]
//             }
//         },
//         {
//             "entry": "NA",
//             "signed_action": {
//                 "hashed": {
//                     "content": {
//                         "action_seq": 12,
//                         "author": [132, 32, 36, 75, 46, 203, 166, 172, 44, 182, 83, 45, 200, 112, 177, 138, 238, 247, 223, 245, 14, 141, 24, 181, 152, 15, 83, 242, 37, 6, 119, 244, 240, 191, 217, 149, 60, 140, 88],
//                         "base_address": [132, 41, 36, 194, 20, 187, 158, 254, 102, 117, 143, 242, 211, 48, 251, 187, 176, 255, 162, 112, 218, 223, 144, 188, 111, 126, 61, 156, 238, 126, 243, 135, 218, 113, 81, 127, 136, 95, 226],
//                         "link_type": 9,
//                         "prev_action": [132, 41, 36, 44, 194, 131, 40, 139, 14, 165, 3, 79, 120, 118, 106, 144, 219, 112, 251, 152, 190, 126, 233, 185, 61, 179, 65, 133, 7, 113, 186, 96, 214, 68, 68, 254, 54, 26, 71],
//                         "tag": tag3,
//                         "target_address": [132, 33, 36, 105, 142, 178, 58, 236, 235, 183, 45, 136, 26, 254, 233, 247, 139, 108, 110, 153, 81, 169, 4, 80, 63, 6, 111, 178, 114, 157, 202, 31, 11, 135, 68, 255, 93, 103, 176],
//                         "timestamp": 1740084031867703 as i64,
//                         "type": "CreateLink",
//                         "weight": {"bucket_id": 255, "units": 0},
//                         "zome_index": 0
//                     },
//                     "hash": [132, 41, 36, 161, 105, 252, 61, 148, 231, 65, 186, 79, 96, 151, 120, 153, 247, 202, 183, 2, 214, 183, 56, 25, 69, 94, 113, 99, 186, 183, 72, 227, 90, 164, 249, 251, 208, 97, 220],
//                 },
//                 "signature": [30, 58, 229, 124, 64, 196, 51, 89, 169, 18, 85, 175, 91, 21, 172, 240, 216, 137, 32, 116, 35, 42, 32, 88, 253, 218, 201, 254, 98, 131, 114, 25, 207, 101, 122, 20, 227, 89, 177, 34, 176, 46, 64, 162, 16, 141, 107, 173, 31, 127, 101, 107, 134, 192, 34, 255, 197, 220, 204, 62, 140, 7, 113, 8]
//             }
//         }
//     ]);

//     let code = r#"
//         let total_amount = 0;
//         let merged_allocations = [];
//         let errors = [];

//         for record in spend_data {
//             try {
//                 let allocations = parse_record_to_parked_amount_and_proof(record);
//                 for allocation in allocations {
//                     merged_allocations.push(allocation);
//                     total_amount += parse_int(allocation.amount[0]);
//                 }
//             } catch(e) {
//                 errors.push(e);
//             }
//         }

//         return #{
//             "unyt_allocation": merged_allocations,
//             "computed_values": #{
//                 "total_amount": total_amount.to_string(),
//                 "errors": errors
//             }
//         };
//     "#
//     .to_string();

//     let input = json!({
//         "spend_data": records
//     });

//     let result = RhaiEngine::execute(&input, msgpack::to_vec(&code).unwrap()).unwrap();
//     let computed = result.computed_values.unwrap();
//     println!("computed {:?}", computed);
//     assert_eq!(computed.get("total_amount").unwrap(), "6");
//     assert!(computed
//         .get("errors")
//         .unwrap()
//         .as_array()
//         .unwrap()
//         .is_empty());
// }