Skip to content

Commit

Permalink
feat: Add heaviest chain materialized view (#97)
Browse files Browse the repository at this point in the history
* feat: add heaviest chain materialized view

* Change migration number

* fix: Update view name for consistency with derived tables

* chore: I like capital SQL

Co-authored-by: Mike Greenberg <[email protected]>
  • Loading branch information
iand and placer14 authored Oct 14, 2020
1 parent f1d9277 commit 6aa5107
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
42 changes: 42 additions & 0 deletions storage/migrations/12_derived_consensus_chain.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package migrations

import (
"github.com/go-pg/migrations/v8"
)

// Schema version 8 adds view over derived_conensus_chain_view

func init() {
up := batch(`
CREATE MATERIALIZED VIEW IF NOT EXISTS derived_conensus_chain_view AS
WITH RECURSIVE consensus_chain AS (
SELECT
b.cid,
b.height,
b.miner,
b.timestamp,
b.parent_state_root,
b.win_count
FROM block_headers b
WHERE b.parent_state_root = (SELECT parent_state_root FROM block_headers ORDER BY height desc, parent_weight DESC LIMIT 1)
UNION
SELECT
p.cid,
p.height,
p.miner,
p.timestamp,
p.parent_state_root,
p.win_count
FROM block_headers p
INNER JOIN block_parents pb ON p.cid = pb.parent
INNER JOIN consensus_chain c ON c.cid = pb.block
) SELECT * FROM consensus_chain
WITH NO DATA;
`)

down := batch(`
DROP MATERIALIZED VIEW IF EXISTS derived_conensus_chain_view;
`)

migrations.MustRegisterTx(up, down)
}
1 change: 1 addition & 0 deletions tasks/views/chainvis.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var chainVisViews = []string{
"chain_visualizer_blocks_with_parents_view",
"chain_visualizer_chain_data_view",
"chain_visualizer_orphans_view",
"heaviest_chain_view",
}

func NewChainVisRefresher(d *storage.Database, refreshRate time.Duration) *ChainVisRefresher {
Expand Down

0 comments on commit 6aa5107

Please sign in to comment.