Skip to content

Commit

Permalink
store num leaves in metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
chris124567 committed Oct 21, 2024
1 parent 3fcc51b commit 8a9aa97
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 3 deletions.
2 changes: 2 additions & 0 deletions explorer/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions explorer/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
3 changes: 2 additions & 1 deletion persist/sqlite/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions persist/sqlite/init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion persist/sqlite/merkle.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion persist/sqlite/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 8a9aa97

Please sign in to comment.