Skip to content

Commit

Permalink
clippy + bug fix + minor code cleanup + reduce mem usage
Browse files Browse the repository at this point in the history
  • Loading branch information
sdbondi committed Dec 12, 2024
1 parent 716d6aa commit 221ca6c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
70 changes: 28 additions & 42 deletions applications/tari_validator_node/src/dan_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions dan_layer/engine_types/src/published_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -38,8 +37,8 @@ impl PublishedTemplateAddress {
Ok(Self(BorTag::new(ObjectKey::from_hex(hex)?)))
}

pub fn as_hash(&self) -> Result<Hash, HashParseError> {
Hash::try_from_vec(self.0.inner().to_vec())
pub fn as_hash(&self) -> Hash {
Hash::from_array(self.as_object_key().into_array())
}
}

Expand Down

0 comments on commit 221ca6c

Please sign in to comment.