Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add heaviest chain materialized view #97

Merged
merged 5 commits into from
Oct 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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