Skip to content

Commit

Permalink
fix(analytics): account for outputs with amount less than min deposit (
Browse files Browse the repository at this point in the history
…#1334)

* fix(analytics): account for outputs with amount less than min deposit in ledger size analytics

* dependencies

* fix address balance analytics
  • Loading branch information
DaughterOfMars authored Feb 22, 2024
1 parent 8668d4f commit d7ad6dd
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 151 deletions.
216 changes: 112 additions & 104 deletions Cargo.lock

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions src/analytics/ledger/active_addresses.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 IOTA Stiftung
// Copyright 2024 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use std::collections::HashSet;
Expand Down Expand Up @@ -43,15 +43,15 @@ impl IntervalAnalytics for AddressActivityMeasurement {
impl Analytics for AddressActivityAnalytics {
type Measurement = AddressActivityMeasurement;

fn handle_transaction(&mut self, consumed: &[LedgerSpent], created: &[LedgerOutput], ctx: &dyn AnalyticsContext) {
fn handle_transaction(&mut self, consumed: &[LedgerSpent], created: &[LedgerOutput], _ctx: &dyn AnalyticsContext) {
for output in consumed {
if let Some(a) = output.owning_address() {
self.addresses.insert(*a);
}
}

for output in created {
if let Some(a) = output.output.owning_address(ctx.at().milestone_timestamp) {
if let Some(a) = output.owning_address() {
self.addresses.insert(*a);
}
}
Expand Down
18 changes: 6 additions & 12 deletions src/analytics/ledger/address_balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
use std::collections::HashMap;

use super::*;
use crate::model::{
payload::milestone::MilestoneTimestamp,
utxo::{Address, TokenAmount},
};
use crate::model::utxo::{Address, TokenAmount};

Check warning on line 7 in src/analytics/ledger/address_balance.rs

View workflow job for this annotation

GitHub Actions / docs

the item `TokenAmount` is imported redundantly

Check warning on line 7 in src/analytics/ledger/address_balance.rs

View workflow job for this annotation

GitHub Actions / udeps

the item `TokenAmount` is imported redundantly

Check warning on line 7 in src/analytics/ledger/address_balance.rs

View workflow job for this annotation

GitHub Actions / udeps

the item `TokenAmount` is imported redundantly

Check warning on line 7 in src/analytics/ledger/address_balance.rs

View workflow job for this annotation

GitHub Actions / udeps

the item `TokenAmount` is imported redundantly

Check warning on line 7 in src/analytics/ledger/address_balance.rs

View workflow job for this annotation

GitHub Actions / udeps

the item `TokenAmount` is imported redundantly

Check failure on line 7 in src/analytics/ledger/address_balance.rs

View workflow job for this annotation

GitHub Actions / clippy / ubuntu-latest, beta

the item `TokenAmount` is imported redundantly

Check warning on line 7 in src/analytics/ledger/address_balance.rs

View workflow job for this annotation

GitHub Actions / check and test / ubuntu-latest, beta

the item `TokenAmount` is imported redundantly

Check warning on line 7 in src/analytics/ledger/address_balance.rs

View workflow job for this annotation

GitHub Actions / check and test / ubuntu-latest, beta

the item `TokenAmount` is imported redundantly

Check warning on line 7 in src/analytics/ledger/address_balance.rs

View workflow job for this annotation

GitHub Actions / check and test / ubuntu-latest, beta

the item `TokenAmount` is imported redundantly

Check warning on line 7 in src/analytics/ledger/address_balance.rs

View workflow job for this annotation

GitHub Actions / check and test / windows-latest, beta

the item `TokenAmount` is imported redundantly

Check warning on line 7 in src/analytics/ledger/address_balance.rs

View workflow job for this annotation

GitHub Actions / check and test / windows-latest, beta

the item `TokenAmount` is imported redundantly

Check warning on line 7 in src/analytics/ledger/address_balance.rs

View workflow job for this annotation

GitHub Actions / check and test / windows-latest, beta

the item `TokenAmount` is imported redundantly

Check warning on line 7 in src/analytics/ledger/address_balance.rs

View workflow job for this annotation

GitHub Actions / udeps

the item `TokenAmount` is imported redundantly

Check warning on line 7 in src/analytics/ledger/address_balance.rs

View workflow job for this annotation

GitHub Actions / check and test / macos-latest, beta

the item `TokenAmount` is imported redundantly

Check warning on line 7 in src/analytics/ledger/address_balance.rs

View workflow job for this annotation

GitHub Actions / check and test / macos-latest, beta

the item `TokenAmount` is imported redundantly

Check warning on line 7 in src/analytics/ledger/address_balance.rs

View workflow job for this annotation

GitHub Actions / check and test / macos-latest, beta

the item `TokenAmount` is imported redundantly

Check failure on line 7 in src/analytics/ledger/address_balance.rs

View workflow job for this annotation

GitHub Actions / clippy / ubuntu-latest, beta

the item `TokenAmount` is imported redundantly

Check warning on line 7 in src/analytics/ledger/address_balance.rs

View workflow job for this annotation

GitHub Actions / check and test / ubuntu-latest, beta

the item `TokenAmount` is imported redundantly

Check warning on line 7 in src/analytics/ledger/address_balance.rs

View workflow job for this annotation

GitHub Actions / check and test / ubuntu-latest, beta

the item `TokenAmount` is imported redundantly

Check warning on line 7 in src/analytics/ledger/address_balance.rs

View workflow job for this annotation

GitHub Actions / check and test / ubuntu-latest, beta

the item `TokenAmount` is imported redundantly

Check warning on line 7 in src/analytics/ledger/address_balance.rs

View workflow job for this annotation

GitHub Actions / check and test / macos-latest, beta

the item `TokenAmount` is imported redundantly

Check warning on line 7 in src/analytics/ledger/address_balance.rs

View workflow job for this annotation

GitHub Actions / check and test / macos-latest, beta

the item `TokenAmount` is imported redundantly

Check warning on line 7 in src/analytics/ledger/address_balance.rs

View workflow job for this annotation

GitHub Actions / check and test / macos-latest, beta

the item `TokenAmount` is imported redundantly

Check warning on line 7 in src/analytics/ledger/address_balance.rs

View workflow job for this annotation

GitHub Actions / udeps

the item `TokenAmount` is imported redundantly

Check warning on line 7 in src/analytics/ledger/address_balance.rs

View workflow job for this annotation

GitHub Actions / check and test / windows-latest, beta

the item `TokenAmount` is imported redundantly

Check warning on line 7 in src/analytics/ledger/address_balance.rs

View workflow job for this annotation

GitHub Actions / check and test / windows-latest, beta

the item `TokenAmount` is imported redundantly

Check warning on line 7 in src/analytics/ledger/address_balance.rs

View workflow job for this annotation

GitHub Actions / check and test / windows-latest, beta

the item `TokenAmount` is imported redundantly

#[derive(Debug)]
pub(crate) struct AddressBalanceMeasurement {
Expand All @@ -32,13 +29,10 @@ pub(crate) struct AddressBalancesAnalytics {

impl AddressBalancesAnalytics {
/// Initialize the analytics by reading the current ledger state.
pub(crate) fn init<'a>(
unspent_outputs: impl IntoIterator<Item = &'a LedgerOutput>,
milestone_timestamp: MilestoneTimestamp,
) -> Self {
pub(crate) fn init<'a>(unspent_outputs: impl IntoIterator<Item = &'a LedgerOutput>) -> Self {
let mut balances = HashMap::new();
for output in unspent_outputs {
if let Some(&a) = output.output.owning_address(milestone_timestamp) {
if let Some(&a) = output.owning_address() {
*balances.entry(a).or_default() += output.amount();
}
}
Expand All @@ -49,9 +43,9 @@ impl AddressBalancesAnalytics {
impl Analytics for AddressBalancesAnalytics {
type Measurement = AddressBalanceMeasurement;

fn handle_transaction(&mut self, consumed: &[LedgerSpent], created: &[LedgerOutput], ctx: &dyn AnalyticsContext) {
fn handle_transaction(&mut self, consumed: &[LedgerSpent], created: &[LedgerOutput], _ctx: &dyn AnalyticsContext) {
for output in consumed {
if let Some(a) = output.owning_address() {
if let Some(a) = output.output.owning_address() {
// All inputs should be present in `addresses`. If not, we skip it's value.
if let Some(amount) = self.balances.get_mut(a) {
*amount -= output.amount();
Expand All @@ -63,7 +57,7 @@ impl Analytics for AddressBalancesAnalytics {
}

for output in created {
if let Some(&a) = output.output.owning_address(ctx.at().milestone_timestamp) {
if let Some(&a) = output.owning_address() {
// All inputs should be present in `addresses`. If not, we skip it's value.
*self.balances.entry(a).or_default() += output.amount();
}
Expand Down
4 changes: 3 additions & 1 deletion src/analytics/ledger/ledger_size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ impl LedgerSize for Output {
iota_sdk::types::block::output::Output::try_from_with_context(&protocol_params, self.clone()).unwrap();
let rent_bytes = RentStructureBytes::compute(&output);
LedgerSizeMeasurement {
total_storage_deposit_amount: Rent::rent_cost(&output, protocol_params.rent_structure()).into(),
total_storage_deposit_amount: Rent::rent_cost(&output, protocol_params.rent_structure())
.min(output.amount())
.into(),
total_key_bytes: rent_bytes.num_key_bytes,
total_data_bytes: rent_bytes.num_data_bytes,
}
Expand Down
12 changes: 4 additions & 8 deletions src/analytics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use crate::{
model::{
ledger::{LedgerOutput, LedgerSpent},
metadata::LedgerInclusionState,
payload::{milestone::MilestoneTimestamp, Payload, TransactionEssence},
payload::{Payload, TransactionEssence},
protocol::ProtocolParameters,
tangle::{MilestoneIndex, MilestoneIndexTimestamp},
utxo::Input,
Expand Down Expand Up @@ -152,12 +152,9 @@ impl Analytic {
choice: &AnalyticsChoice,
protocol_params: &ProtocolParameters,
unspent_outputs: impl IntoIterator<Item = &'a LedgerOutput>,
milestone_timestamp: MilestoneTimestamp,
) -> Self {
Self(match choice {
AnalyticsChoice::AddressBalance => {
Box::new(AddressBalancesAnalytics::init(unspent_outputs, milestone_timestamp)) as _
}
AnalyticsChoice::AddressBalance => Box::new(AddressBalancesAnalytics::init(unspent_outputs)) as _,
AnalyticsChoice::BaseTokenActivity => Box::<BaseTokenActivityMeasurement>::default() as _,
AnalyticsChoice::BlockActivity => Box::<BlockActivityMeasurement>::default() as _,
AnalyticsChoice::ActiveAddresses => Box::<AddressActivityAnalytics>::default() as _,
Expand Down Expand Up @@ -399,7 +396,7 @@ mod test {
ledger::{LedgerOutput, LedgerSpent},
metadata::BlockMetadata,
node::NodeConfiguration,
payload::{milestone::MilestoneTimestamp, MilestoneId, MilestonePayload},
payload::{MilestoneId, MilestonePayload},
protocol::ProtocolParameters,
tangle::{MilestoneIndex, MilestoneIndexTimestamp},
},
Expand Down Expand Up @@ -447,11 +444,10 @@ mod test {
fn init<'a>(
protocol_params: ProtocolParameters,
unspent_outputs: impl IntoIterator<Item = &'a LedgerOutput> + Copy,
milestone_timestamp: MilestoneTimestamp,
) -> Self {
Self {
active_addresses: Default::default(),
address_balance: AddressBalancesAnalytics::init(unspent_outputs, milestone_timestamp),
address_balance: AddressBalancesAnalytics::init(unspent_outputs),
base_tokens: Default::default(),
ledger_outputs: LedgerOutputMeasurement::init(unspent_outputs),
ledger_size: LedgerSizeAnalytics::init(protocol_params, unspent_outputs),
Expand Down
5 changes: 0 additions & 5 deletions src/bin/inx-chronicle/api/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@ pub type ApiResult<T> = Result<T, ApiError>;
pub trait ErrorStatus: std::error::Error {
/// Gets the HTTP status code associated with this error.
fn status(&self) -> StatusCode;

/// Gets the u16 status code representation associated with this error.
fn code(&self) -> u16 {
self.status().as_u16()
}
}

#[derive(Debug, Error)]
Expand Down
15 changes: 5 additions & 10 deletions src/bin/inx-chronicle/cli/analytics.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 IOTA Stiftung
// Copyright 2024 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use std::collections::HashSet;
Expand All @@ -23,7 +23,9 @@ use tracing::{debug, info};

use crate::config::ChronicleConfig;

/// This command accepts both milestone index and date ranges. The following rules apply:
/// This command accepts both milestone index and date ranges.
///
/// The following rules apply:
///
/// - If both milestone and date are specified, the date will be used for interval analytics
/// while the milestone will be used for per-milestone analytics.
Expand Down Expand Up @@ -264,14 +266,7 @@ pub async fn fill_analytics<I: 'static + InputSource + Clone>(

let analytics = analytics_choices
.iter()
.map(|choice| {
Analytic::init(
choice,
&milestone.protocol_params,
&ledger_state,
milestone.at.milestone_timestamp,
)
})
.map(|choice| Analytic::init(choice, &milestone.protocol_params, &ledger_state))
.collect::<Vec<_>>();
state = Some(AnalyticsState {
analytics,
Expand Down
9 changes: 1 addition & 8 deletions src/bin/inx-chronicle/inx/influx/analytics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,7 @@ impl InxWorker {

let analytics = analytics_choices
.iter()
.map(|choice| {
Analytic::init(
choice,
&milestone.protocol_params,
&ledger_state,
milestone.at.milestone_timestamp,
)
})
.map(|choice| Analytic::init(choice, &milestone.protocol_params, &ledger_state))
.collect::<Vec<_>>();
*state = Some(AnalyticsState {
analytics,
Expand Down

0 comments on commit d7ad6dd

Please sign in to comment.