Skip to content

Commit

Permalink
Tweak block indexing to use height as the primary key (#4757)
Browse files Browse the repository at this point in the history
Every block has a unique height, we almost always want to join with
other tables based on block height, the serial ID has no meaning: ergo,
we should get rid of the id, and just use height as the primary key.

There was no index on height either, which would have made joining on it
slow.

Also removed unwraps and expects and as in favor of actual errors.

## Checklist before requesting a review

- [x] If this code contains consensus-breaking changes, I have added the
"consensus-breaking" label. Otherwise, I declare my belief that there
are not consensus-breaking changes, for the following reason:

  > Indexer changes
  • Loading branch information
cronokirby authored Jul 24, 2024
1 parent 649cc22 commit 21b8d7f
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions crates/bin/pindexer/src/block.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use anyhow::anyhow;
use cometindex::{async_trait, sqlx, AppView, ContextualizedEvent, PgTransaction};
use penumbra_proto::{core::component::sct::v1 as pb, event::ProtoEvent};
use sqlx::{types::chrono::DateTime, PgPool};
Expand All @@ -16,9 +17,8 @@ impl AppView for Block {
// table name is module path + struct name
"
CREATE TABLE IF NOT EXISTS block_details (
id SERIAL PRIMARY KEY,
height BIGINT PRIMARY KEY,
root BYTEA NOT NULL,
height INT8 NOT NULL,
timestamp TIMESTAMPTZ NOT NULL
);
",
Expand All @@ -39,16 +39,21 @@ CREATE TABLE IF NOT EXISTS block_details (
_src_db: &PgPool,
) -> Result<(), anyhow::Error> {
let pe = pb::EventBlockRoot::from_event(event.as_ref())?;
let timestamp = pe.timestamp.expect("Block has no timestamp");
let timestamp = pe
.timestamp
.ok_or(anyhow!("block at height {} has no timestamp", pe.height))?;

sqlx::query(
"
INSERT INTO block_details (height, timestamp, root)
VALUES ($1, $2, $3)
",
)
.bind(pe.height as i64)
.bind(DateTime::from_timestamp(timestamp.seconds, timestamp.nanos as u32).unwrap())
.bind(i64::try_from(pe.height)?)
.bind(
DateTime::from_timestamp(timestamp.seconds, u32::try_from(timestamp.nanos)?)
.ok_or(anyhow!("failed to convert timestamp"))?,
)
.bind(pe.root.unwrap().inner)
.execute(dbtx.as_mut())
.await?;
Expand Down

0 comments on commit 21b8d7f

Please sign in to comment.