From e7eb11c82dd1a2e13240c0d3f35e9581b9de48e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jochen=20G=C3=B6rtler?= Date: Fri, 30 Sep 2022 13:12:10 +0200 Subject: [PATCH] Minor improvements to rent structure computation (#761) * Minor improvements to rent structure computation * Further improve (#85) * Further improve * slight refactor Co-authored-by: /alex/ --- src/types/ledger/output_metadata.rs | 68 ++++++++++------------------- 1 file changed, 24 insertions(+), 44 deletions(-) diff --git a/src/types/ledger/output_metadata.rs b/src/types/ledger/output_metadata.rs index 8d36097cf..2c97d382c 100644 --- a/src/types/ledger/output_metadata.rs +++ b/src/types/ledger/output_metadata.rs @@ -52,36 +52,36 @@ pub struct LedgerSpent { pub spent_metadata: SpentMetadata, } +#[cfg(feature = "inx")] +// TODO: Write unit test cases for this function for each of the output types. +fn compute_rent_structure(output: &bee_block_stardust::output::Output) -> RentStructureBytes { + use bee_block_stardust::output::{Rent, RentStructureBuilder}; + + let rent_cost = |byte_cost, data_factor, key_factor| { + output.rent_cost( + &RentStructureBuilder::new() + .byte_cost(byte_cost) + .data_factor(data_factor) + .key_factor(key_factor) + .finish(), + ) + }; + + RentStructureBytes { + num_data_bytes: rent_cost(1, 1, 0), + num_key_bytes: rent_cost(1, 0, 1), + } +} + #[cfg(feature = "inx")] impl TryFrom for LedgerOutput { type Error = bee_inx::Error; fn try_from(value: bee_inx::LedgerOutput) -> Result { - use bee_block_stardust::output::{Rent, RentStructureBuilder}; let bee_output = value.output.inner_unverified()?; - let num_data_bytes = { - let config = RentStructureBuilder::new() - .byte_cost(1) - .data_factor(1) - .key_factor(0) - .finish(); - bee_output.rent_cost(&config) - }; - - let num_key_bytes = { - let config = RentStructureBuilder::new() - .byte_cost(1) - .data_factor(0) - .key_factor(1) - .finish(); - bee_output.rent_cost(&config) - }; Ok(Self { - rent_structure: RentStructureBytes { - num_data_bytes, - num_key_bytes, - }, + rent_structure: compute_rent_structure(&bee_output), output: Into::into(&bee_output), output_id: value.output_id.into(), block_id: value.block_id.into(), @@ -101,30 +101,10 @@ impl crate::types::context::TryFromWithContext for Ledger ctx: &bee_block_stardust::protocol::ProtocolParameters, value: bee_inx::LedgerOutput, ) -> Result { - use bee_block_stardust::output::{Rent, RentStructureBuilder}; let bee_output = value.output.inner(ctx)?; - let num_data_bytes = { - let config = RentStructureBuilder::new() - .byte_cost(1) - .data_factor(1) - .key_factor(0) - .finish(); - bee_output.rent_cost(&config) - }; - - let num_key_bytes = { - let config = RentStructureBuilder::new() - .byte_cost(1) - .data_factor(0) - .key_factor(1) - .finish(); - bee_output.rent_cost(&config) - }; + Ok(Self { - rent_structure: RentStructureBytes { - num_data_bytes, - num_key_bytes, - }, + rent_structure: compute_rent_structure(&bee_output), output: Into::into(&bee_output), output_id: value.output_id.into(), block_id: value.block_id.into(),