Skip to content

Commit

Permalink
feat(client): dump spends creation_reason statistics
Browse files Browse the repository at this point in the history
  • Loading branch information
maqi committed May 1, 2024
1 parent 3a64df3 commit cd567d7
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/memcheck.yml
Original file line number Diff line number Diff line change
Expand Up @@ -281,9 +281,9 @@ jobs:

- name: Audit from genesis to collect entire spend DAG and dump to a dot file
run: |
cargo run --bin safe --release -- --log-output-dest=data-dir wallet audit --dot > spend_dag.dot
cargo run --bin safe --release -- --log-output-dest=data-dir wallet audit --dot > spend_dag_and_statistics.txt
echo "=============================================================================="
cat spend_dag.dot
cat spend_dag_and_statistics.txt
env:
SN_LOG: "all"
timeout-minutes: 60
Expand Down Expand Up @@ -446,10 +446,10 @@ jobs:
continue-on-error: true
if: always()

- name: Upload spend DAG
- name: Upload spend DAG and statistics
uses: actions/upload-artifact@main
with:
name: memory_check_spend_dag
path: spend_dag.dot
name: memory_check_spend_dag_and_statistics
path: spend_dag_and_statistics.txt
continue-on-error: true
if: always()
7 changes: 7 additions & 0 deletions sn_cli/src/bin/subcommands/wallet/audit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,14 @@ async fn gather_spend_dag(client: &Client, root_dir: &Path) -> Result<SpendDag>
pub async fn audit(client: &Client, to_dot: bool, royalties: bool, root_dir: &Path) -> Result<()> {
if to_dot {
let dag = gather_spend_dag(client, root_dir).await?;
println!(
"========================== spends DAG diagraph ============================="
);
println!("{}", dag.dump_dot_format());
println!(
"======================= spends purpose statistics =========================="
);
println!("{}", dag.dump_creation_reasons_statistics());
} else if royalties {
let dag = gather_spend_dag(client, root_dir).await?;
let royalties = dag.all_royalties()?;
Expand Down
27 changes: 27 additions & 0 deletions sn_client/src/audit/spend_dag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ impl DagEntry {
DagEntry::NotGatheredYet(_) => vec![],
}
}

fn amount(&self) -> NanoTokens {
if let Some(signed_spend) = self.spends().first() {
signed_spend.spend.amount
} else {
NanoTokens::zero()
}
}
}

/// The result of a get operation on the DAG
Expand Down Expand Up @@ -338,6 +346,25 @@ impl SpendDag {
format!("{:?}", Dot::with_config(&self.dag, &[]))
}

pub fn dump_creation_reasons_statistics(&self) -> String {
let mut statistics: BTreeMap<String, Vec<NanoTokens>> = Default::default();
for (spend_addr, reason) in self.creation_reasons.iter() {
let holders = statistics.entry(reason.clone()).or_default();
if let Some(spend_dag_entry) = self.spends.get(spend_addr) {
holders.push(spend_dag_entry.amount());
}
}
let mut content = "Purpose,Times,Amount".to_string();
for (purpose, payments) in statistics.iter() {
let total_amount: u64 = payments
.iter()
.map(|nano_tokens| nano_tokens.as_nano())
.sum();
content = format!("{content}\n{purpose},{},{total_amount}", payments.len());
}
content
}

/// Merges the given dag into ours
pub fn merge(&mut self, sub_dag: SpendDag) -> Result<(), DagError> {
let source = self.source();
Expand Down

0 comments on commit cd567d7

Please sign in to comment.