From 5caafcb0a0e4f5da41e074cd3949dfe9a16e9ef4 Mon Sep 17 00:00:00 2001 From: Mike Greenberg Date: Thu, 27 Aug 2020 22:56:28 -0400 Subject: [PATCH] fix(chainwatch): Upsert on chain_economics This table may get multiple updates depending on how many blocks get added into a tipset. Each new block affects the tipset state, but the parent state root will remain the same. This allows the very latest value to be applied. --- cmd/lotus-chainwatch/syncer/sync.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/cmd/lotus-chainwatch/syncer/sync.go b/cmd/lotus-chainwatch/syncer/sync.go index 3027defc248..0455b3a292a 100644 --- a/cmd/lotus-chainwatch/syncer/sync.go +++ b/cmd/lotus-chainwatch/syncer/sync.go @@ -306,8 +306,10 @@ func (s *Syncer) storeCirculatingSupply(ctx context.Context, tipset *types.TipSe return err } - ceInsert := `insert into chain_economics (parent_state_root, circulating_fil, vested_fil, mined_fil, burnt_fil, locked_fil)` + - `values ('%s', '%s', '%s', '%s', '%s', '%s');` + ceInsert := `insert into chain_economics (parent_state_root, circulating_fil, vested_fil, mined_fil, burnt_fil, locked_fil) ` + + `values ('%s', '%s', '%s', '%s', '%s', '%s') on conflict on constraint chain_economics_pk do ` + + `update set (circulating_fil, vested_fil, mined_fil, burnt_fil, locked_fil) = ('%[2]s', '%[3]s', '%[4]s', '%[5]s', '%[6]s') ` + + `where parent_state_root = '%[1]s';` if _, err := s.db.Exec(fmt.Sprintf(ceInsert, tipset.ParentState().String(),