From 8a9aa9753250d38cc7c7a4834ed87bd1ed347104 Mon Sep 17 00:00:00 2001 From: Christopher Tarry Date: Mon, 21 Oct 2024 16:39:53 -0400 Subject: [PATCH] store num leaves in metrics --- explorer/types.go | 2 ++ explorer/update.go | 1 + persist/sqlite/consensus.go | 3 ++- persist/sqlite/init.sql | 1 + persist/sqlite/merkle.go | 2 +- persist/sqlite/metrics.go | 2 +- 6 files changed, 8 insertions(+), 3 deletions(-) diff --git a/explorer/types.go b/explorer/types.go index 798432be..4f859320 100644 --- a/explorer/types.go +++ b/explorer/types.go @@ -208,6 +208,8 @@ type Metrics struct { SiafundPool types.Currency `json:"siafundPool"` // Total announced hosts TotalHosts uint64 `json:"totalHosts"` + // Number of leaves in the accumulator + NumLeaves uint64 `json:"numLeaves"` // Number of active contracts ActiveContracts uint64 `json:"activeContracts"` // Number of failed contracts diff --git a/explorer/update.go b/explorer/update.go index f5447503..0db1701f 100644 --- a/explorer/update.go +++ b/explorer/update.go @@ -199,6 +199,7 @@ func applyChainUpdate(tx UpdateTx, cau chain.ApplyUpdate) error { state.Metrics.Index = cau.State.Index state.Metrics.Difficulty = cau.State.Difficulty state.Metrics.SiafundPool = cau.State.SiafundPool + state.Metrics.NumLeaves = cau.State.Elements.NumLeaves return tx.ApplyIndex(state) } diff --git a/persist/sqlite/consensus.go b/persist/sqlite/consensus.go index 65fec026..a66d3f08 100644 --- a/persist/sqlite/consensus.go +++ b/persist/sqlite/consensus.go @@ -1006,11 +1006,12 @@ func updateFileContractIndices(tx *txn, revert bool, index types.ChainIndex, fce } func addMetrics(tx *txn, s explorer.UpdateState) error { - _, err := tx.Exec(`INSERT INTO network_metrics(block_id, height, difficulty, siafund_pool, total_hosts, active_contracts, failed_contracts, successful_contracts, storage_utilization, circulating_supply, contract_revenue) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, + _, err := tx.Exec(`INSERT INTO network_metrics(block_id, height, difficulty, siafund_pool, num_leaves, total_hosts, active_contracts, failed_contracts, successful_contracts, storage_utilization, circulating_supply, contract_revenue) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, encode(s.Metrics.Index.ID), s.Metrics.Index.Height, encode(s.Metrics.Difficulty), encode(s.Metrics.SiafundPool), + encode(s.Metrics.NumLeaves), s.Metrics.TotalHosts, s.Metrics.ActiveContracts, s.Metrics.FailedContracts, diff --git a/persist/sqlite/init.sql b/persist/sqlite/init.sql index ee993c97..446d9498 100644 --- a/persist/sqlite/init.sql +++ b/persist/sqlite/init.sql @@ -21,6 +21,7 @@ CREATE TABLE network_metrics ( height INTEGER NOT NULL, difficulty BLOB NOT NULL, siafund_pool BLOB NOT NULL, + num_leaves BLOB NOT NULL, total_hosts INTEGER NOT NULL, active_contracts INTEGER NOT NULL, failed_contracts INTEGER NOT NULL, diff --git a/persist/sqlite/merkle.go b/persist/sqlite/merkle.go index ff4fd103..bcbd47a8 100644 --- a/persist/sqlite/merkle.go +++ b/persist/sqlite/merkle.go @@ -10,7 +10,7 @@ import ( func (s *Store) MerkleProof(leafIndex uint64) (proof []types.Hash256, err error) { err = s.transaction(func(tx *txn) error { var numLeaves uint64 - if err := tx.QueryRow("SELECT COUNT(*) FROM state_tree WHERE row = 0").Scan(&numLeaves); err != nil { + if err := tx.QueryRow("SELECT num_leaves FROM network_metrics ORDER BY height DESC LIMIT 1").Scan(decode(&numLeaves)); err != nil { return err } diff --git a/persist/sqlite/metrics.go b/persist/sqlite/metrics.go index 8a0103af..6258e54f 100644 --- a/persist/sqlite/metrics.go +++ b/persist/sqlite/metrics.go @@ -11,7 +11,7 @@ import ( // Metrics implements explorer.Store func (s *Store) Metrics(id types.BlockID) (result explorer.Metrics, err error) { err = s.transaction(func(tx *txn) error { - err = tx.QueryRow(`SELECT block_id, height, difficulty, siafund_pool, total_hosts, active_contracts, failed_contracts, successful_contracts, storage_utilization, circulating_supply, contract_revenue FROM network_metrics WHERE block_id = ?`, encode(id)).Scan(decode(&result.Index.ID), &result.Index.Height, decode(&result.Difficulty), decode(&result.SiafundPool), &result.TotalHosts, &result.ActiveContracts, &result.FailedContracts, &result.SuccessfulContracts, &result.StorageUtilization, decode(&result.CirculatingSupply), decode(&result.ContractRevenue)) + err = tx.QueryRow(`SELECT block_id, height, difficulty, siafund_pool, num_leaves, total_hosts, active_contracts, failed_contracts, successful_contracts, storage_utilization, circulating_supply, contract_revenue FROM network_metrics WHERE block_id = ?`, encode(id)).Scan(decode(&result.Index.ID), &result.Index.Height, decode(&result.Difficulty), decode(&result.SiafundPool), decode(&result.NumLeaves), &result.TotalHosts, &result.ActiveContracts, &result.FailedContracts, &result.SuccessfulContracts, &result.StorageUtilization, decode(&result.CirculatingSupply), decode(&result.ContractRevenue)) if err != nil { return fmt.Errorf("failed to get metrics: %w", err) }