From 18b69fd8d9b803b467b9863de61b5ec13b5c5bf2 Mon Sep 17 00:00:00 2001 From: /alex/ Date: Mon, 6 May 2024 16:06:39 +0200 Subject: [PATCH] fix: `commitments/by-index/:index/blocks` route should return only finalized blocks (#1385) * filter out non-finalized blocks * remove block state analytics * update dashboard * much better --------- Co-authored-by: DaughterOfMars --- .../dashboards/analytics_dashboard.json | 84 ------------------- src/analytics/influx.rs | 6 -- src/analytics/tangle/block_activity.rs | 24 ++---- src/tangle/slot_stream.rs | 48 ++++++----- 4 files changed, 34 insertions(+), 128 deletions(-) diff --git a/docker/assets/grafana/dashboards/analytics_dashboard.json b/docker/assets/grafana/dashboards/analytics_dashboard.json index 89e7f2a20..0722db138 100644 --- a/docker/assets/grafana/dashboards/analytics_dashboard.json +++ b/docker/assets/grafana/dashboards/analytics_dashboard.json @@ -281,48 +281,6 @@ "refId": "A", "resultFormat": "time_series", "select": [ - [ - { - "params": ["block_pending_count"], - "type": "field" - }, - { - "params": [], - "type": "sum" - }, - { - "params": ["Pending"], - "type": "alias" - } - ], - [ - { - "params": ["block_accepted_count"], - "type": "field" - }, - { - "params": [], - "type": "sum" - }, - { - "params": ["Accepted"], - "type": "alias" - } - ], - [ - { - "params": ["block_confirmed_count"], - "type": "field" - }, - { - "params": [], - "type": "sum" - }, - { - "params": ["Confirmed"], - "type": "alias" - } - ], [ { "params": ["block_finalized_count"], @@ -336,48 +294,6 @@ "params": ["Finalized"], "type": "alias" } - ], - [ - { - "params": ["block_rejected_count"], - "type": "field" - }, - { - "params": [], - "type": "sum" - }, - { - "params": ["Rejected"], - "type": "alias" - } - ], - [ - { - "params": ["block_failed_count"], - "type": "field" - }, - { - "params": [], - "type": "sum" - }, - { - "params": ["Failed"], - "type": "alias" - } - ], - [ - { - "params": ["block_unknown_count"], - "type": "field" - }, - { - "params": [], - "type": "sum" - }, - { - "params": ["Unknown"], - "type": "alias" - } ] ], "tags": [] diff --git a/src/analytics/influx.rs b/src/analytics/influx.rs index 4e80e2902..a5f1a6479 100644 --- a/src/analytics/influx.rs +++ b/src/analytics/influx.rs @@ -178,13 +178,7 @@ impl Measurement for BlockActivityMeasurement { .add_field("tagged_data_count", self.tagged_data_count as u64) .add_field("candidacy_announcement_count", self.candidacy_announcement_count as u64) .add_field("no_payload_count", self.no_payload_count as u64) - .add_field("block_pending_count", self.block_pending_count as u64) - .add_field("block_accepted_count", self.block_accepted_count as u64) - .add_field("block_confirmed_count", self.block_confirmed_count as u64) .add_field("block_finalized_count", self.block_finalized_count as u64) - .add_field("block_dropped_count", self.block_dropped_count as u64) - .add_field("block_orphaned_count", self.block_orphaned_count as u64) - .add_field("block_unknown_count", self.block_unknown_count as u64) .add_field("txn_pending_count", self.txn_pending_count as u64) .add_field("txn_accepted_count", self.txn_accepted_count as u64) .add_field("txn_committed_count", self.txn_committed_count as u64) diff --git a/src/analytics/tangle/block_activity.rs b/src/analytics/tangle/block_activity.rs index a7d3695b1..5d6118775 100644 --- a/src/analytics/tangle/block_activity.rs +++ b/src/analytics/tangle/block_activity.rs @@ -2,7 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 use iota_sdk::types::{ - api::core::{BlockState, TransactionState}, + api::core::TransactionState, block::{ payload::{Payload, SignedTransactionPayload}, Block, BlockBody, @@ -26,13 +26,7 @@ pub(crate) struct BlockActivityMeasurement { pub(crate) tagged_data_count: usize, pub(crate) transaction_count: usize, pub(crate) candidacy_announcement_count: usize, - pub(crate) block_pending_count: usize, - pub(crate) block_accepted_count: usize, - pub(crate) block_confirmed_count: usize, pub(crate) block_finalized_count: usize, - pub(crate) block_dropped_count: usize, - pub(crate) block_orphaned_count: usize, - pub(crate) block_unknown_count: usize, pub(crate) txn_pending_count: usize, pub(crate) txn_accepted_count: usize, pub(crate) txn_committed_count: usize, @@ -47,7 +41,7 @@ impl Analytics for BlockActivityMeasurement { async fn handle_block( &mut self, block: &Block, - block_metadata: &BlockMetadata, + _block_metadata: &BlockMetadata, _ctx: &dyn AnalyticsContext, ) -> eyre::Result<()> { match block.body() { @@ -62,17 +56,9 @@ impl Analytics for BlockActivityMeasurement { } BlockBody::Validation(_) => self.validation_count += 1, } - match &block_metadata.block_state { - Some(state) => match state { - BlockState::Pending => self.block_pending_count += 1, - BlockState::Accepted => self.block_accepted_count += 1, - BlockState::Confirmed => self.block_confirmed_count += 1, - BlockState::Finalized => self.block_finalized_count += 1, - BlockState::Dropped => self.block_dropped_count += 1, - BlockState::Orphaned => self.block_orphaned_count += 1, - }, - None => self.block_unknown_count += 1, - } + + // non-finalized blocks, or blocks without a block state have been filtered out. + self.block_finalized_count += 1; Ok(()) } diff --git a/src/tangle/slot_stream.rs b/src/tangle/slot_stream.rs index 70c6b236c..5a18ac6a5 100644 --- a/src/tangle/slot_stream.rs +++ b/src/tangle/slot_stream.rs @@ -7,7 +7,10 @@ use std::{ }; use futures::{stream::BoxStream, Stream, TryStreamExt}; -use iota_sdk::types::block::slot::{SlotCommitment, SlotCommitmentId, SlotIndex}; +use iota_sdk::types::{ + api::core::BlockState, + block::slot::{SlotCommitment, SlotCommitmentId, SlotIndex}, +}; use super::InputSource; use crate::model::{ @@ -43,25 +46,32 @@ impl<'a, I: InputSource> Slot<'a, I> { pub async fn accepted_block_stream( &self, ) -> Result> + '_, I::Error> { - Ok(self.source.accepted_blocks(self.index()).await?.and_then(|res| async { - let transaction = if let Some(transaction_id) = res - .block - .inner() - .body() - .as_basic_opt() - .and_then(|body| body.payload()) - .and_then(|p| p.as_signed_transaction_opt()) - .map(|txn| txn.transaction().id()) - { - Some(self.source.transaction_metadata(transaction_id).await?) - } else { - None - }; - Ok(BlockWithTransactionMetadata { - transaction, - block: res, + Ok(self + .source + .accepted_blocks(self.index()) + .await? + .try_filter(|block_with_metadata| { + futures::future::ready(block_with_metadata.metadata.block_state == Some(BlockState::Finalized)) }) - })) + .and_then(|res| async { + let transaction = if let Some(transaction_id) = res + .block + .inner() + .body() + .as_basic_opt() + .and_then(|body| body.payload()) + .and_then(|p| p.as_signed_transaction_opt()) + .map(|txn| txn.transaction().id()) + { + Some(self.source.transaction_metadata(transaction_id).await?) + } else { + None + }; + Ok(BlockWithTransactionMetadata { + transaction, + block: res, + }) + })) } /// Returns the ledger update store.