Skip to content

Commit

Permalink
schema change: epoch start_height is nullable
Browse files Browse the repository at this point in the history
make epoch tracking parallelism-friendly

simplify db query; special-case end_height for current epoch

nit
  • Loading branch information
Andrew7234 committed May 4, 2023
1 parent 6b99c7b commit 7151647
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 21 deletions.
11 changes: 1 addition & 10 deletions analyzer/consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,20 +373,11 @@ func (m *Main) queueBlockInserts(batch *storage.QueryBatch, data *storage.Consen

func (m *Main) queueEpochInserts(batch *storage.QueryBatch, data *storage.ConsensusBlockData) error {
batch.Queue(
queries.ConsensusEpochInsert,
queries.ConsensusEpochUpsert,
data.Epoch,
data.BlockHeader.Height,
)

// Conclude previous epoch. Epochs start at index 0.
if data.Epoch > 0 {
batch.Queue(
queries.ConsensusEpochUpdate,
data.Epoch-1,
data.BlockHeader.Height,
)
}

return nil
}

Expand Down
16 changes: 7 additions & 9 deletions analyzer/queries/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,13 @@ const (
INSERT INTO chain.blocks (height, block_hash, time, num_txs, namespace, version, type, root_hash)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)`

ConsensusEpochInsert = `
INSERT INTO chain.epochs (id, start_height)
VALUES ($1, $2)
ON CONFLICT (id) DO NOTHING`

ConsensusEpochUpdate = `
UPDATE chain.epochs
SET end_height = $2
WHERE id = $1 AND end_height IS NULL`
ConsensusEpochUpsert = `
INSERT INTO chain.epochs AS epochs (id, start_height, end_height)
VALUES ($1, $2, $2)
ON CONFLICT (id) DO
UPDATE SET
start_height = LEAST(excluded.start_height, epochs.start_height),
end_height = GREATEST(excluded.end_height, epochs.end_height)`

ConsensusTransactionInsert = `
INSERT INTO chain.transactions (block, tx_hash, tx_index, nonce, fee_amount, max_gas, method, sender, body, module, code, message)
Expand Down
3 changes: 2 additions & 1 deletion storage/client/queries/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,8 @@ const (
OFFSET $3::bigint`

Epochs = `
SELECT id, start_height, end_height
SELECT id, start_height,
(CASE id WHEN (SELECT max(id) FROM chain.epochs) THEN NULL ELSE end_height END) AS end_height
FROM chain.epochs
ORDER BY id DESC
LIMIT $1::bigint
Expand Down
5 changes: 4 additions & 1 deletion storage/migrations/01_consensus.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,13 @@ CREATE INDEX ix_events_type ON chain.events (type, tx_block); -- tx_block is fo
CREATE TABLE chain.epochs
(
id UINT63 PRIMARY KEY,
-- Earliest known height that belongs to the epoch.
start_height UINT63 NOT NULL,
end_height UINT63 CHECK (end_height IS NULL OR end_height >= start_height),
-- Max known height that belongs to the epoch.
end_height UINT63 CHECK (end_height >= start_height),
UNIQUE (start_height, end_height)
);
CREATE INDEX ix_epochs_id ON chain.epochs (id);

-- Registry Backend Data
CREATE TABLE chain.entities
Expand Down

0 comments on commit 7151647

Please sign in to comment.