From ce07c8c673fa811f50f1ed13b33dee0ab8a324c8 Mon Sep 17 00:00:00 2001 From: sara Date: Mon, 8 Apr 2024 17:15:59 -0300 Subject: [PATCH 1/2] feat: byron validations added --- napi-pallas/src/validations/byron.rs | 135 ++++++++++++++++++++++++++- 1 file changed, 132 insertions(+), 3 deletions(-) diff --git a/napi-pallas/src/validations/byron.rs b/napi-pallas/src/validations/byron.rs index d7bbe94..bc64a16 100644 --- a/napi-pallas/src/validations/byron.rs +++ b/napi-pallas/src/validations/byron.rs @@ -1,6 +1,135 @@ -use crate::Validations; -use pallas::ledger::primitives::byron::MintedTxPayload; +use crate::{Validation, Validations}; +use pallas::{ + applying::{ + byron::{ + check_fees, check_ins_in_utxos, check_ins_not_empty, check_outs_have_lovelace, + check_outs_not_empty, check_size, check_witnesses, + }, + utils::{ByronProtParams, FeePolicy}, + UTxOs, + }, + codec::minicbor::encode, + ledger::primitives::byron::{MintedTxPayload, Tx}, +}; + +use super::validate::set_description; + +fn get_tx_size(tx: &Tx) -> u64 { + let mut buff: Vec = Vec::new(); + if encode(tx, &mut buff).is_ok() { + return buff.len() as u64; + } else { + return 0; + } +} + +// & The following validation requires the size and the protocol parameters +fn validate_byron_size(size: &u64, prot_pps: &ByronProtParams) -> Validation { + let res = check_size(&size, &prot_pps); + let description = set_description( + &res, + "The transaction size does not exceed the protocol limit.".to_string(), + ); + return Validation::new() + .with_name("Transaction size".to_string()) + .with_value(res.is_ok()) + .with_description(description); +} + +// & The following validations require the transaction +fn validate_byron_ins_not_empty(tx: &Tx) -> Validation { + let res = check_ins_not_empty(tx); + let description = set_description( + &res, + "The set of transaction inputs is not empty.".to_string(), + ); + return Validation::new() + .with_name("Non empty inputs".to_string()) + .with_value(res.is_ok()) + .with_description(description); +} + +fn validate_byron_outs_not_empty(tx: &Tx) -> Validation { + let res = check_outs_not_empty(tx); + let description = set_description( + &res, + "The set of transaction outputs is not empty.".to_string(), + ); + return Validation::new() + .with_name("Non empty outputs".to_string()) + .with_value(res.is_ok()) + .with_description(description); +} + +fn validate_byron_outs_have_lovelace(tx: &Tx) -> Validation { + let res = check_outs_have_lovelace(tx); + let description = set_description( + &res, + "All transaction outputs contain non-null Lovelace values.".to_string(), + ); + return Validation::new() + .with_name("Outputs have lovelace".to_string()) + .with_value(res.is_ok()) + .with_description(description); +} + +// & The following validations require the transaction and the UTXOs +fn validate_byron_ins_in_utxos(tx: &Tx, utxos: &UTxOs) -> Validation { + let res = check_ins_in_utxos(tx, utxos); + let description = set_description( + &res, + "All transaction inputs are in the set of (yet) unspent transaction outputs.".to_string(), + ); + return Validation::new() + .with_name("Inputs in UTXOs".to_string()) + .with_value(res.is_ok()) + .with_description(description); +} + +// & The following validations require the transaction, the UTXOs and the protocol magic +fn validate_byron_witnesses(tx: &MintedTxPayload, utxos: &UTxOs, prot_magic: u32) -> Validation { + let res = check_witnesses(&tx, &utxos, &prot_magic); + let description = set_description(&res, "All transaction witnesses are valid.".to_string()); + return Validation::new() + .with_name("Witnesses".to_string()) + .with_value(res.is_ok()) + .with_description(description); +} + +// & The following validations require the transaction, the size, the UTXOs and the protocol parameters +fn validate_byron_fees( + tx: &Tx, + size: &u64, + utxos: &UTxOs, + prot_pps: &ByronProtParams, +) -> Validation { + let res = check_fees(&tx, &size, &utxos, &prot_pps); + let description = set_description( + &res, + "Fees are not less than what is determined by the protocol.".to_string(), + ); + return Validation::new() + .with_name("Fees".to_string()) + .with_value(res.is_ok()) + .with_description(description); +} + pub fn validate_byron(mtxp: &MintedTxPayload) -> Validations { - let out = Validations::new().with_era("Byron".to_string()); + let tx: &Tx = &mtxp.transaction; + let size: &u64 = &get_tx_size(&tx); + let prot_pps: ByronProtParams = ByronProtParams { + fee_policy: FeePolicy { + summand: 155381, + multiplier: 44, + }, + max_tx_size: 16384, + }; + + let out = Validations::new() + .with_era("Byron".to_string()) + .add_new_validation(validate_byron_size(&size, &prot_pps)) + .add_new_validation(validate_byron_ins_not_empty(&tx)) + .add_new_validation(validate_byron_outs_not_empty(&tx)) + .add_new_validation(validate_byron_outs_have_lovelace(&tx)); out } From 791446870749ebe193c3397f4ab23e6a01e97479 Mon Sep 17 00:00:00 2001 From: sara Date: Tue, 9 Apr 2024 14:40:31 -0300 Subject: [PATCH 2/2] fix: tx size validation handle and button fix --- napi-pallas/src/validations/byron.rs | 6 ++++ web/app/components.tsx | 43 ++++++++++++++++------------ 2 files changed, 31 insertions(+), 18 deletions(-) diff --git a/napi-pallas/src/validations/byron.rs b/napi-pallas/src/validations/byron.rs index bc64a16..1af97d9 100644 --- a/napi-pallas/src/validations/byron.rs +++ b/napi-pallas/src/validations/byron.rs @@ -25,6 +25,12 @@ fn get_tx_size(tx: &Tx) -> u64 { // & The following validation requires the size and the protocol parameters fn validate_byron_size(size: &u64, prot_pps: &ByronProtParams) -> Validation { + if size == &0 { + return Validation::new() + .with_name("Transaction size".to_string()) + .with_value(false) + .with_description("The transaction size could not be obtained.".to_string()); + } let res = check_size(&size, &prot_pps); let description = set_description( &res, diff --git a/web/app/components.tsx b/web/app/components.tsx index cb73df9..9ee674e 100644 --- a/web/app/components.tsx +++ b/web/app/components.tsx @@ -222,25 +222,32 @@ export function AccordionItem({ validation }: { validation: IValidation }) { onClick={handleClick} > {validation.value ? "✔" : "✘"}  {validation.name} - -
- + -
+ > + + + + -
- {open && ( -

{validation.description}

- )} +
+

{validation.description}

); @@ -250,7 +257,7 @@ export function ValidationAccordion(props: { validations: IValidation[] }) { return (
{props.validations.map((v) => (