From 5f9dcee62303058573d0eab73d4124e4c5c0ab68 Mon Sep 17 00:00:00 2001 From: Christopher Tarry Date: Wed, 11 Sep 2024 13:55:03 -0400 Subject: [PATCH] store siafund pool in metrics --- explorer/types.go | 2 ++ explorer/update.go | 3 ++- persist/sqlite/consensus.go | 3 ++- persist/sqlite/init.sql | 1 + persist/sqlite/metrics.go | 2 +- 5 files changed, 8 insertions(+), 3 deletions(-) diff --git a/explorer/types.go b/explorer/types.go index bcc697b0..223742a6 100644 --- a/explorer/types.go +++ b/explorer/types.go @@ -144,6 +144,8 @@ type Metrics struct { Index types.ChainIndex `json:"index"` // Current difficulty Difficulty consensus.Work `json:"difficulty"` + // Siafund pool value + SiafundPool types.Currency `json:"siafundPool"` // Total announced hosts TotalHosts uint64 `json:"totalHosts"` // Number of active contracts diff --git a/explorer/update.go b/explorer/update.go index c9faca73..e9f1bc2d 100644 --- a/explorer/update.go +++ b/explorer/update.go @@ -171,8 +171,9 @@ func applyChainUpdate(tx UpdateTx, cau chain.ApplyUpdate) error { if err != nil { return err } - state.Metrics.Difficulty = cau.State.Difficulty state.Metrics.Index = cau.State.Index + state.Metrics.Difficulty = cau.State.Difficulty + state.Metrics.SiafundPool = cau.State.SiafundPool return tx.ApplyIndex(state) } diff --git a/persist/sqlite/consensus.go b/persist/sqlite/consensus.go index 24324a6f..d6ee1f7e 100644 --- a/persist/sqlite/consensus.go +++ b/persist/sqlite/consensus.go @@ -910,10 +910,11 @@ func updateFileContractElements(tx *txn, revert bool, b types.Block, fces []expl } func addMetrics(tx *txn, s explorer.UpdateState) error { - _, err := tx.Exec(`INSERT INTO network_metrics(block_id, height, difficulty, 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, 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), s.Metrics.TotalHosts, s.Metrics.ActiveContracts, s.Metrics.FailedContracts, diff --git a/persist/sqlite/init.sql b/persist/sqlite/init.sql index 87d9f1ab..93947ced 100644 --- a/persist/sqlite/init.sql +++ b/persist/sqlite/init.sql @@ -17,6 +17,7 @@ CREATE TABLE network_metrics ( height INTEGER NOT NULL, difficulty BLOB NOT NULL, + siafund_pool BLOB NOT NULL, total_hosts INTEGER NOT NULL, active_contracts INTEGER NOT NULL, failed_contracts INTEGER NOT NULL, diff --git a/persist/sqlite/metrics.go b/persist/sqlite/metrics.go index c8b5d0d2..8a0103af 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, 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), &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, 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)) if err != nil { return fmt.Errorf("failed to get metrics: %w", err) }