Skip to content

Commit

Permalink
feat: add timestamp indexing wip (#4713)
Browse files Browse the repository at this point in the history
## Describe your changes

- builds on top of #4722
- consumes timestamps from raw events to create detailed_blocks in
processed events db

## Issue ticket number and link

fixes #4675
  • Loading branch information
vacekj authored Jul 19, 2024
1 parent e360740 commit 7142295
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 20 deletions.
5 changes: 5 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 18 additions & 17 deletions crates/bin/pindexer/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
[package]
name = "pindexer"
version = {workspace = true}
authors = {workspace = true}
edition = {workspace = true}
repository = {workspace = true}
homepage = {workspace = true}
license = {workspace = true}
version = { workspace = true }
authors = { workspace = true }
edition = { workspace = true }
repository = { workspace = true }
homepage = { workspace = true }
license = { workspace = true }
publish = false

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
cometindex = {workspace = true}
penumbra-shielded-pool = {workspace = true, default-features = false}
penumbra-stake = {workspace = true, default-features = false}
penumbra-app = {workspace = true, default-features = false}
penumbra-num = {workspace = true, default-features = false}
penumbra-asset = {workspace = true, default-features = false}
penumbra-proto = {workspace = true, default-features = false}
tokio = {workspace = true, features = ["full"]}
anyhow = {workspace = true}
serde_json = {workspace = true}
tracing = {workspace = true}
sqlx = { workspace = true, features = ["chrono"] }
cometindex = { workspace = true }
penumbra-shielded-pool = { workspace = true, default-features = false }
penumbra-stake = { workspace = true, default-features = false }
penumbra-app = { workspace = true, default-features = false }
penumbra-num = { workspace = true, default-features = false }
penumbra-asset = { workspace = true, default-features = false }
penumbra-proto = { workspace = true, default-features = false }
tokio = { workspace = true, features = ["full"] }
anyhow = { workspace = true }
serde_json = { workspace = true }
tracing = { workspace = true }
57 changes: 57 additions & 0 deletions crates/bin/pindexer/src/block.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use cometindex::{async_trait, sqlx, AppView, ContextualizedEvent, PgTransaction};
use penumbra_proto::{core::component::sct::v1 as pb, event::ProtoEvent};
use sqlx::types::chrono::DateTime;

#[derive(Debug)]
pub struct Block {}

#[async_trait]
impl AppView for Block {
async fn init_chain(
&self,
dbtx: &mut PgTransaction,
_: &serde_json::Value,
) -> Result<(), anyhow::Error> {
sqlx::query(
// table name is module path + struct name
"
CREATE TABLE IF NOT EXISTS block_details (
id SERIAL PRIMARY KEY,
root BYTEA NOT NULL,
height INT8 NOT NULL,
timestamp TIMESTAMPTZ NOT NULL
);
",
)
.execute(dbtx.as_mut())
.await?;
Ok(())
}

fn is_relevant(&self, type_str: &str) -> bool {
type_str == "penumbra.core.component.sct.v1.EventBlockRoot"
}

async fn index_event(
&self,
dbtx: &mut PgTransaction,
event: &ContextualizedEvent,
) -> Result<(), anyhow::Error> {
let pe = pb::EventBlockRoot::from_event(event.as_ref())?;
let timestamp = pe.timestamp.expect("Block has no timestamp");

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(pe.root.unwrap().inner)
.execute(dbtx.as_mut())
.await?;

Ok(())
}
}
3 changes: 1 addition & 2 deletions crates/bin/pindexer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ pub use cometindex::{AppView, Indexer};

mod indexer_ext;
pub use indexer_ext::IndexerExt;

pub mod block;
pub mod shielded_pool;

pub mod stake;
2 changes: 2 additions & 0 deletions crates/bin/pindexer/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use anyhow::Result;
use pindexer::block::Block;
use pindexer::{Indexer, IndexerExt as _};

#[tokio::main]
async fn main() -> Result<()> {
Indexer::new()
.with_default_tracing()
.with_default_penumbra_app_views()
.with_index(Block {})
.run()
.await?;

Expand Down
2 changes: 1 addition & 1 deletion crates/core/component/sct/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ serde = {workspace = true, features = ["derive"]}
tendermint = {workspace = true}
tonic = {workspace = true, optional = true}
tracing = {workspace = true}
chrono = { workspace = true, default-features = false, features = ["serde"] }
chrono = { workspace = true, default-features = false, features = ["serde", "now"] }

[dev-dependencies]
getrandom = {workspace = true}

0 comments on commit 7142295

Please sign in to comment.