diff --git a/cmd/lotus-chainwatch/storage.go b/cmd/lotus-chainwatch/storage.go index dabf66f1d..77c0a078d 100644 --- a/cmd/lotus-chainwatch/storage.go +++ b/cmd/lotus-chainwatch/storage.go @@ -84,6 +84,15 @@ create table if not exists blocks create unique index if not exists block_cid_uindex on blocks (cid); +create materialized view if not exists state_heights + as select distinct height, parentstateroot from blocks; + +create unique index if not exists state_heights_uindex + on state_heights (height); + +create index if not exists state_heights_height_index + on state_heights (parentstateroot); + create table if not exists id_address_map ( id text not null, @@ -119,6 +128,22 @@ create index if not exists id_address_map_address_index create index if not exists id_address_map_id_index on id_address_map (id); +create or replace function actor_tips(epoch bigint) + returns table (id text, + code text, + head text, + nonce int, + balance text, + stateroot text, + height bigint, + parentstateroot text) as +$body$ + select distinct on (id) * from actors + inner join state_heights sh on sh.parentstateroot = stateroot + where height < $1 + order by id, height desc; +$body$ language sql; + create table if not exists actor_states ( head text not null, @@ -210,6 +235,31 @@ create table if not exists miner_heads primary key (head, addr) ); +create or replace function miner_tips(epoch bigint) + returns table (head text, + addr text, + stateroot text, + sectorset text, + setsize decimal, + provingset text, + provingsize decimal, + owner text, + worker text, + peerid text, + sectorsize bigint, + power decimal, + active bool, + ppe bigint, + slashed_at bigint, + height bigint, + parentstateroot text) as + $body$ + select distinct on (addr) * from miner_heads + inner join state_heights sh on sh.parentstateroot = stateroot + where height < $1 + order by addr, height desc; + $body$ language sql; + create table if not exists deals ( id int not null, @@ -832,6 +882,14 @@ func (st *storage) storeDeals(deals map[string]actors.OnChainDeal) error { return nil } +func (st *storage) refreshViews() error { + if _, err := st.db.Exec(`refresh materialized view state_heights`); err != nil { + return err + } + + return nil +} + func (st *storage) close() error { return st.db.Close() } diff --git a/cmd/lotus-chainwatch/sync.go b/cmd/lotus-chainwatch/sync.go index 33d6605f2..bf91c97d8 100644 --- a/cmd/lotus-chainwatch/sync.go +++ b/cmd/lotus-chainwatch/sync.go @@ -371,6 +371,13 @@ func syncHead(ctx context.Context, api api.FullNode, st *storage, ts *types.TipS return } + log.Infof("Refresh views") + + if err := st.refreshViews(); err != nil { + log.Error(err) + return + } + log.Infof("Sync done") }