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 timestamp indexing wip #4713

Merged
merged 8 commits into from
Jul 19, 2024
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
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}
Loading