Skip to content

Commit

Permalink
fix features analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Coats committed Feb 28, 2024
1 parent 0e241e2 commit ceae6c0
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 100 deletions.
8 changes: 4 additions & 4 deletions src/analytics/influx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,11 +376,11 @@ impl Measurement for FeaturesMeasurement {
fn add_fields(&self, query: WriteQuery) -> WriteQuery {
query
.add_field("native_tokens_count", self.native_tokens.count as u64)
.add_field("native_tokens_amount", self.native_tokens.amount)
.add_field("block_issuer_key_count", self.block_issuer.count as u64)
.add_field("block_issuer_key_amount", self.block_issuer.amount)
.add_field("native_tokens_amount", self.native_tokens.amount.to_string())
.add_field("block_issuer_count", self.block_issuer.count as u64)
.add_field("block_issuer_amount", self.block_issuer.amount)
.add_field("staking_count", self.staking.count as u64)
.add_field("staking_amount", self.staking.amount)
.add_field("staked_amount", self.staking.staked_amount)
}
}

Expand Down
76 changes: 71 additions & 5 deletions src/analytics/ledger/features.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
// Copyright 2023 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use iota_sdk::types::block::{output::Feature, payload::SignedTransactionPayload};
use iota_sdk::{
types::block::{
output::{
feature::{NativeTokenFeature, StakingFeature},
Feature,
},
payload::SignedTransactionPayload,
},
utils::serde::string,
U256,
};
use serde::{Deserialize, Serialize};

use super::CountAndAmount;
Expand All @@ -13,9 +23,9 @@ use crate::{
#[derive(Copy, Clone, Debug, Default, Serialize, Deserialize)]
#[allow(missing_docs)]
pub(crate) struct FeaturesMeasurement {
pub(crate) native_tokens: CountAndAmount,
pub(crate) native_tokens: NativeTokensCountAndAmount,
pub(crate) block_issuer: CountAndAmount,
pub(crate) staking: CountAndAmount,
pub(crate) staking: StakingCountAndAmount,
}

impl FeaturesMeasurement {
Expand All @@ -38,9 +48,9 @@ impl FeaturesMeasurement {
if let Some(features) = output.output().features() {
for feature in features.iter() {
match feature {
Feature::NativeToken(_) => measurement.native_tokens.add_output(output),
Feature::NativeToken(nt) => measurement.native_tokens.add_native_token(nt),
Feature::BlockIssuer(_) => measurement.block_issuer.add_output(output),
Feature::Staking(_) => measurement.staking.add_output(output),
Feature::Staking(staking) => measurement.staking.add_staking(staking),
_ => (),
}
}
Expand Down Expand Up @@ -71,3 +81,59 @@ impl Analytics for FeaturesMeasurement {
*self
}
}

#[derive(Copy, Clone, Debug, Default, Serialize, Deserialize)]
pub(crate) struct NativeTokensCountAndAmount {
pub(crate) count: usize,
#[serde(with = "string")]
pub(crate) amount: U256,
}

impl NativeTokensCountAndAmount {
fn wrapping_add(&mut self, rhs: Self) {
*self = Self {
count: self.count.wrapping_add(rhs.count),
amount: self.amount.overflowing_add(rhs.amount).0,
}
}

fn wrapping_sub(&mut self, rhs: Self) {
*self = Self {
count: self.count.wrapping_sub(rhs.count),
amount: self.amount.overflowing_sub(rhs.amount).0,
}
}

fn add_native_token(&mut self, nt: &NativeTokenFeature) {
self.count += 1;
self.amount += nt.amount();
}
}

#[derive(Copy, Clone, Debug, Default, Serialize, Deserialize)]
pub(crate) struct StakingCountAndAmount {
pub(crate) count: usize,
#[serde(with = "string")]
pub(crate) staked_amount: u64,
}

impl StakingCountAndAmount {
fn wrapping_add(&mut self, rhs: Self) {
*self = Self {
count: self.count.wrapping_add(rhs.count),
staked_amount: self.staked_amount.wrapping_add(rhs.staked_amount),
}
}

fn wrapping_sub(&mut self, rhs: Self) {
*self = Self {
count: self.count.wrapping_sub(rhs.count),
staked_amount: self.staked_amount.wrapping_sub(rhs.staked_amount),
}
}

fn add_staking(&mut self, staking: &StakingFeature) {
self.count += 1;
self.staked_amount += staking.staked_amount();
}
}
94 changes: 3 additions & 91 deletions src/inx/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,17 @@

use futures::stream::{Stream, StreamExt};
use inx::{client::InxClient, proto};
use iota_sdk::types::block::{self as iota, output::OutputId, slot::SlotIndex, BlockId};
use packable::PackableExt;
use iota_sdk::types::block::slot::SlotIndex;

use super::{
convert::TryConvertTo,
ledger::{AcceptedTransaction, LedgerUpdate, UnspentOutput},
ledger::{LedgerUpdate, UnspentOutput},
request::SlotRangeRequest,
responses::{Block, Output},
InxError,
};
use crate::model::{
block_metadata::{BlockMetadata, BlockWithMetadata},
block_metadata::BlockWithMetadata,
node::{NodeConfiguration, NodeStatus},
raw::Raw,
slot::Commitment,
};

Expand Down Expand Up @@ -47,17 +44,6 @@ impl Inx {
.try_convert()
}

/// Get a commitment from a slot index.
pub async fn get_commitment(&mut self, slot_index: SlotIndex) -> Result<Commitment, InxError> {
self.inx
.read_commitment(proto::CommitmentRequest {
commitment_slot: slot_index.0,
commitment_id: None,
})
.await?
.try_convert()
}

/// Get a stream of committed slots.
pub async fn get_committed_slots(
&mut self,
Expand All @@ -71,58 +57,6 @@ impl Inx {
.map(|msg| TryConvertTo::try_convert(msg?)))
}

/// Get a block using a block id.
pub async fn get_block(&mut self, block_id: BlockId) -> Result<Raw<iota::Block>, InxError> {
Ok(self
.inx
.read_block(proto::BlockId { id: block_id.to_vec() })
.await?
.into_inner()
.try_into()?)
}

/// Get a block's metadata using a block id.
pub async fn get_block_metadata(&mut self, block_id: BlockId) -> Result<BlockMetadata, InxError> {
self.inx
.read_block_metadata(proto::BlockId { id: block_id.to_vec() })
.await?
.try_convert()
}

/// Convenience wrapper that gets all blocks.
pub async fn get_blocks(&mut self) -> Result<impl Stream<Item = Result<Block, InxError>>, InxError> {
Ok(self
.inx
.listen_to_blocks(proto::NoParams {})
.await?
.into_inner()
.map(|msg| TryConvertTo::try_convert(msg?)))
}

/// Convenience wrapper that gets accepted blocks.
pub async fn get_accepted_blocks(
&mut self,
) -> Result<impl Stream<Item = Result<BlockMetadata, InxError>>, InxError> {
Ok(self
.inx
.listen_to_accepted_blocks(proto::NoParams {})
.await?
.into_inner()
.map(|msg| TryConvertTo::try_convert(msg?)))
}

/// Convenience wrapper that gets confirmed blocks.
pub async fn get_confirmed_blocks(
&mut self,
) -> Result<impl Stream<Item = Result<BlockMetadata, InxError>>, InxError> {
Ok(self
.inx
.listen_to_confirmed_blocks(proto::NoParams {})
.await?
.into_inner()
.map(|msg| TryConvertTo::try_convert(msg?)))
}

/// Convenience wrapper that gets accepted blocks for a given slot.
pub async fn get_accepted_blocks_for_slot(
&mut self,
Expand Down Expand Up @@ -160,26 +94,4 @@ impl Inx {
.into_inner()
.map(|msg| TryConvertTo::try_convert(msg?)))
}

/// Convenience wrapper that listen to accepted transactions.
pub async fn get_accepted_transactions(
&mut self,
) -> Result<impl Stream<Item = Result<AcceptedTransaction, InxError>>, InxError> {
Ok(self
.inx
.listen_to_accepted_transactions(proto::NoParams {})
.await?
.into_inner()
.map(|msg| TryConvertTo::try_convert(msg?)))
}

/// Get an output using an output id.
pub async fn get_output(&mut self, output_id: OutputId) -> Result<Output, InxError> {
self.inx
.read_output(proto::OutputId {
id: output_id.pack_to_vec(),
})
.await?
.try_convert()
}
}

0 comments on commit ceae6c0

Please sign in to comment.