Merge pull request #2910 from filecoin-project/feat/chainwatch/capture-circulating-fil

feat(chainwatch): capture circulating fil per tipset metric
This commit is contained in:
Łukasz Magiera 2020-08-08 01:51:07 +02:00 committed by GitHub
commit 40ac52a862
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -4,6 +4,7 @@ import (
"container/list" "container/list"
"context" "context"
"database/sql" "database/sql"
"fmt"
"sync" "sync"
"time" "time"
@ -40,6 +41,14 @@ func (s *Syncer) setupSchemas() error {
} }
if _, err := tx.Exec(` if _, err := tx.Exec(`
/* tracks circulating fil available on the network at each tipset */
create table if not exists chain_economics
(
parent_state_root text not null
constraint chain_economics_pk primary key,
circulating_fil text not null
);
create table if not exists block_cids create table if not exists block_cids
( (
cid text not null cid text not null
@ -173,6 +182,10 @@ func (s *Syncer) Start(ctx context.Context) {
log.Errorw("failed to gather unsynced blocks", "error", err) log.Errorw("failed to gather unsynced blocks", "error", err)
} }
if err := s.storeCirculatingSupply(ctx, change.Val); err != nil {
log.Errorw("failed to store circulating supply", "error", err)
}
if len(unsynced) == 0 { if len(unsynced) == 0 {
continue continue
} }
@ -264,6 +277,24 @@ func (s *Syncer) syncedBlocks(timestamp time.Time) (map[cid.Cid]struct{}, error)
return out, nil return out, nil
} }
func (s *Syncer) storeCirculatingSupply(ctx context.Context, tipset *types.TipSet) error {
supply, err := s.node.StateCirculatingSupply(ctx, tipset.Key())
if err != nil {
return err
}
ceInsert := `insert into chain_economics (parent_state_root, circulating_fil) values ('%s', '%s');`
if _, err := s.db.Exec(fmt.Sprintf(ceInsert,
tipset.ParentState().String(),
supply.String(),
)); err != nil {
return xerrors.Errorf("insert circulating supply for tipset (%s): %w", tipset.Key().String(), err)
}
return nil
}
func (s *Syncer) storeHeaders(bhs map[cid.Cid]*types.BlockHeader, sync bool, timestamp time.Time) error { func (s *Syncer) storeHeaders(bhs map[cid.Cid]*types.BlockHeader, sync bool, timestamp time.Time) error {
s.headerLk.Lock() s.headerLk.Lock()
defer s.headerLk.Unlock() defer s.headerLk.Unlock()