From adc96f48ad16c23b7deb80984b43163cb5077877 Mon Sep 17 00:00:00 2001 From: Alexander Schmidt Date: Thu, 18 Apr 2024 16:14:27 +0200 Subject: [PATCH 1/2] analytics: ensure finalized transactions --- src/analytics/mod.rs | 89 +++++++++++++++++++++++--------------------- 1 file changed, 47 insertions(+), 42 deletions(-) diff --git a/src/analytics/mod.rs b/src/analytics/mod.rs index 465bd7ddd..a58dff0eb 100644 --- a/src/analytics/mod.rs +++ b/src/analytics/mod.rs @@ -4,12 +4,15 @@ //! Various analytics that give insight into the usage of the tangle. use futures::{prelude::stream::StreamExt, TryStreamExt}; -use iota_sdk::types::block::{ - output::OutputId, - payload::SignedTransactionPayload, - protocol::ProtocolParameters, - slot::{EpochIndex, SlotCommitment, SlotIndex}, - Block, +use iota_sdk::types::{ + api::core::TransactionState, + block::{ + output::OutputId, + payload::SignedTransactionPayload, + protocol::ProtocolParameters, + slot::{EpochIndex, SlotCommitment, SlotIndex}, + Block, + }, }; use thiserror::Error; @@ -343,42 +346,44 @@ impl<'a, I: InputSource> Slot<'a, I> { metadata: &TransactionMetadata, ctx: &BasicContext<'_>, ) -> eyre::Result<()> { - let consumed = payload - .transaction() - .inputs() - .iter() - .map(|input| input.as_utxo().output_id()) - .map(|output_id| { - Ok(self - .ledger_updates() - .get_consumed(output_id) - .ok_or(AnalyticsError::MissingLedgerSpent { - output_id: *output_id, - slot_index: metadata.transaction_id.slot_index(), - })? - .clone()) - }) - .collect::>>()?; - let created = payload - .transaction() - .outputs() - .iter() - .enumerate() - .map(|(index, _)| { - let output_id = metadata.transaction_id.into_output_id(index as _); - Ok(self - .ledger_updates() - .get_created(&output_id) - .ok_or(AnalyticsError::MissingLedgerOutput { - output_id, - slot_index: metadata.transaction_id.slot_index(), - })? - .clone()) - }) - .collect::>>()?; - analytics - .handle_transaction(payload, metadata, &consumed, &created, ctx) - .await?; + if let Some(TransactionState::Finalized) = metadata.transaction_state { + let consumed = payload + .transaction() + .inputs() + .iter() + .map(|input| input.as_utxo().output_id()) + .map(|output_id| { + Ok(self + .ledger_updates() + .get_consumed(output_id) + .ok_or(AnalyticsError::MissingLedgerSpent { + output_id: *output_id, + slot_index: metadata.transaction_id.slot_index(), + })? + .clone()) + }) + .collect::>>()?; + let created = payload + .transaction() + .outputs() + .iter() + .enumerate() + .map(|(index, _)| { + let output_id = metadata.transaction_id.into_output_id(index as _); + Ok(self + .ledger_updates() + .get_created(&output_id) + .ok_or(AnalyticsError::MissingLedgerOutput { + output_id, + slot_index: metadata.transaction_id.slot_index(), + })? + .clone()) + }) + .collect::>>()?; + analytics + .handle_transaction(payload, metadata, &consumed, &created, ctx) + .await?; + } Ok(()) } From 8860b3515f3c0ffad7791c94fb0f833919fa0680 Mon Sep 17 00:00:00 2001 From: Alexander Schmidt Date: Mon, 22 Apr 2024 15:34:21 +0200 Subject: [PATCH 2/2] suggestions --- src/analytics/mod.rs | 78 ++++++++++++++++++++++---------------------- 1 file changed, 39 insertions(+), 39 deletions(-) diff --git a/src/analytics/mod.rs b/src/analytics/mod.rs index a58dff0eb..77db0b333 100644 --- a/src/analytics/mod.rs +++ b/src/analytics/mod.rs @@ -327,7 +327,9 @@ impl<'a, I: InputSource> Slot<'a, I> { .and_then(|p| p.as_signed_transaction_opt()) .zip(data.transaction) { - self.handle_transaction(analytics, payload, &metadata, &ctx).await?; + if metadata.transaction_state == Some(TransactionState::Finalized) { + self.handle_transaction(analytics, payload, &metadata, &ctx).await?; + } } self.handle_block(analytics, &data.block, &ctx).await?; } @@ -346,44 +348,42 @@ impl<'a, I: InputSource> Slot<'a, I> { metadata: &TransactionMetadata, ctx: &BasicContext<'_>, ) -> eyre::Result<()> { - if let Some(TransactionState::Finalized) = metadata.transaction_state { - let consumed = payload - .transaction() - .inputs() - .iter() - .map(|input| input.as_utxo().output_id()) - .map(|output_id| { - Ok(self - .ledger_updates() - .get_consumed(output_id) - .ok_or(AnalyticsError::MissingLedgerSpent { - output_id: *output_id, - slot_index: metadata.transaction_id.slot_index(), - })? - .clone()) - }) - .collect::>>()?; - let created = payload - .transaction() - .outputs() - .iter() - .enumerate() - .map(|(index, _)| { - let output_id = metadata.transaction_id.into_output_id(index as _); - Ok(self - .ledger_updates() - .get_created(&output_id) - .ok_or(AnalyticsError::MissingLedgerOutput { - output_id, - slot_index: metadata.transaction_id.slot_index(), - })? - .clone()) - }) - .collect::>>()?; - analytics - .handle_transaction(payload, metadata, &consumed, &created, ctx) - .await?; - } + let consumed = payload + .transaction() + .inputs() + .iter() + .map(|input| input.as_utxo().output_id()) + .map(|output_id| { + Ok(self + .ledger_updates() + .get_consumed(output_id) + .ok_or(AnalyticsError::MissingLedgerSpent { + output_id: *output_id, + slot_index: metadata.transaction_id.slot_index(), + })? + .clone()) + }) + .collect::>>()?; + let created = payload + .transaction() + .outputs() + .iter() + .enumerate() + .map(|(index, _)| { + let output_id = metadata.transaction_id.into_output_id(index as _); + Ok(self + .ledger_updates() + .get_created(&output_id) + .ok_or(AnalyticsError::MissingLedgerOutput { + output_id, + slot_index: metadata.transaction_id.slot_index(), + })? + .clone()) + }) + .collect::>>()?; + analytics + .handle_transaction(payload, metadata, &consumed, &created, ctx) + .await?; Ok(()) }