From 221ca6c70d3da32d8cf91e26ae78c0e8d2a084bd Mon Sep 17 00:00:00 2001 From: Stan Bondi Date: Thu, 12 Dec 2024 10:30:21 +0400 Subject: [PATCH] clippy + bug fix + minor code cleanup + reduce mem usage --- .../src/template_manager/interface/handle.rs | 2 +- .../tari_validator_node/src/dan_node.rs | 70 ++++++++----------- .../engine_types/src/published_template.rs | 5 +- 3 files changed, 31 insertions(+), 46 deletions(-) diff --git a/applications/tari_dan_app_utilities/src/template_manager/interface/handle.rs b/applications/tari_dan_app_utilities/src/template_manager/interface/handle.rs index d709adef4..06d06bd1e 100644 --- a/applications/tari_dan_app_utilities/src/template_manager/interface/handle.rs +++ b/applications/tari_dan_app_utilities/src/template_manager/interface/handle.rs @@ -67,7 +67,7 @@ impl TemplateManagerHandle { pub async fn add_template( &self, author_public_key: PublicKey, - template_address: tari_engine_types::TemplateAddress, + template_address: TemplateAddress, template: TemplateExecutable, ) -> Result<(), TemplateManagerError> { let (tx, rx) = oneshot::channel(); diff --git a/applications/tari_validator_node/src/dan_node.rs b/applications/tari_validator_node/src/dan_node.rs index b5be9a392..ee3213ed0 100644 --- a/applications/tari_validator_node/src/dan_node.rs +++ b/applications/tari_validator_node/src/dan_node.rs @@ -21,7 +21,6 @@ // USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. use log::*; -use tari_common_types::types::PublicKey; use tari_consensus::hotstuff::HotstuffEvent; use tari_dan_app_utilities::template_manager::interface::TemplateExecutable; use tari_dan_storage::{ @@ -30,9 +29,7 @@ use tari_dan_storage::{ }; use tari_engine_types::{ commit_result::TransactionResult, - instruction::Instruction, - substate::{Substate, SubstateDiff, SubstateId, SubstateValue}, - TemplateAddress, + substate::{SubstateId, SubstateValue}, }; use tari_epoch_manager::{EpochManagerEvent, EpochManagerReader}; use tari_networking::NetworkingService; @@ -100,51 +97,40 @@ impl DanNode { /// from the given block. async fn handle_template_publishes(&self, block: &Block) -> Result<(), anyhow::Error> { // add wasm templates to template manager if available in any of the new block's transactions - let mut template_counter = 0; - let templates: Vec<(PublicKey, TemplateAddress, TemplateExecutable)> = self + let templates = self .services .state_store .with_read_tx(|tx| block.get_transactions(tx))? - .iter() - .filter(|record| { - if let Some(decision) = record.final_decision { - if decision == Decision::Commit { - return true; - } - } - false - }) + .into_iter() + .filter(|record| matches!(record.final_decision, Some(Decision::Commit))) .filter_map(|record| { - let tx_signature = record.transaction.signatures().first(); - if let Some(tx_signature) = tx_signature { - let signer_pub_key = tx_signature.public_key(); - return match &record.execution_result { - Some(result) => { - if let TransactionResult::Accept(diff) = &result.finalize.result { - for (substate_id, substate) in diff.up_iter() { - if let SubstateId::Template(template_address) = substate_id { - if let Ok(template_address_hash) = template_address.as_hash() { - if let SubstateValue::Template(template) = substate.substate_value() { - return Some(( - signer_pub_key.clone(), - TemplateAddress::from(template_address_hash), - TemplateExecutable::CompiledWasm(template.binary.clone()), - )); - } - } - } - } - } - None - }, - None => None, - }; - } - None + let result = record.execution_result?; + let TransactionResult::Accept(diff) = result.finalize.result else { + return None; + }; + let tx_signature = record.transaction.signatures().first()?; + let signer_pub_key = tx_signature.public_key().clone(); + Some((signer_pub_key, diff)) }) - .collect(); + .flat_map(|(signer_pub_key, diff)| { + let mut templates = vec![]; + for (substate_id, substate) in diff.into_up_iter() { + if let SubstateId::Template(template_address) = substate_id { + let template_address_hash = template_address.as_hash(); + if let SubstateValue::Template(template) = substate.into_substate_value() { + templates.push(( + signer_pub_key.clone(), + template_address_hash, + TemplateExecutable::CompiledWasm(template.binary), + )); + } + } + } + templates + }); // adding templates to template manager + let mut template_counter = 0; for (author_pub_key, template_address, template) in templates { self.services .template_manager diff --git a/dan_layer/engine_types/src/published_template.rs b/dan_layer/engine_types/src/published_template.rs index aee92d185..408ef5165 100644 --- a/dan_layer/engine_types/src/published_template.rs +++ b/dan_layer/engine_types/src/published_template.rs @@ -11,7 +11,6 @@ use tari_bor::{BorTag, Deserialize, Serialize}; use tari_template_lib::{ models::{BinaryTag, KeyParseError, ObjectKey}, Hash, - HashParseError, }; const TAG: u64 = BinaryTag::TemplateAddress.as_u64(); @@ -38,8 +37,8 @@ impl PublishedTemplateAddress { Ok(Self(BorTag::new(ObjectKey::from_hex(hex)?))) } - pub fn as_hash(&self) -> Result { - Hash::try_from_vec(self.0.inner().to_vec()) + pub fn as_hash(&self) -> Hash { + Hash::from_array(self.as_object_key().into_array()) } }