From 85bce6c5a6d5207626ace06fe8f3b7b7697995fb Mon Sep 17 00:00:00 2001 From: Atris Date: Thu, 18 Jul 2024 14:58:12 +0200 Subject: [PATCH] feat: correctly define and insert blockdetail during indexing --- crates/bin/pindexer/src/block.rs | 2 +- crates/bin/pindexer/src/block/block.rs | 27 +++++++++++++++---- crates/bin/pindexer/src/lib.rs | 2 +- crates/bin/pindexer/src/main.rs | 3 +-- .../core/component/sct/src/component/tree.rs | 8 ++++-- crates/core/component/sct/src/event.rs | 2 +- 6 files changed, 32 insertions(+), 12 deletions(-) diff --git a/crates/bin/pindexer/src/block.rs b/crates/bin/pindexer/src/block.rs index 49bc7d4bf0..a863eaad24 100644 --- a/crates/bin/pindexer/src/block.rs +++ b/crates/bin/pindexer/src/block.rs @@ -1 +1 @@ -pub mod block; \ No newline at end of file +pub mod block; diff --git a/crates/bin/pindexer/src/block/block.rs b/crates/bin/pindexer/src/block/block.rs index 1532b12e4d..e65c02d731 100644 --- a/crates/bin/pindexer/src/block/block.rs +++ b/crates/bin/pindexer/src/block/block.rs @@ -1,23 +1,29 @@ use cometindex::{async_trait, sqlx, AppView, ContextualizedEvent, PgTransaction}; +use penumbra_proto::{core::component::sct::v1 as pb, event::ProtoEvent}; #[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> { + 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 BYTEA NOT NULL, - timestamp BYTEA NOT NULL + timestamp TIMESTAMPTZ NOT NULL ); ", ) - .execute(dbtx.as_mut()) - .await?; + .execute(dbtx.as_mut()) + .await?; Ok(()) } @@ -30,7 +36,18 @@ CREATE TABLE IF NOT EXISTS block_details ( dbtx: &mut PgTransaction, event: &ContextualizedEvent, ) -> Result<(), anyhow::Error> { - dbg!(event); + let pe = pb::EventBlockRoot::from_event(event.as_ref())?; + + sqlx::query( + " + INSERT INTO block_details (height, timestamp) + VALUES ($1, $2) + ", + ) + .bind(&pe.height) + .bind(&pe.timestamp) + .execute(dbtx.as_mu()) + .await?; Ok(()) } diff --git a/crates/bin/pindexer/src/lib.rs b/crates/bin/pindexer/src/lib.rs index e3eae33e85..e577d4daeb 100644 --- a/crates/bin/pindexer/src/lib.rs +++ b/crates/bin/pindexer/src/lib.rs @@ -3,6 +3,6 @@ pub use cometindex::{AppView, Indexer}; mod indexer_ext; pub use indexer_ext::IndexerExt; +pub mod block; pub mod shielded_pool; pub mod stake; -pub mod block; diff --git a/crates/bin/pindexer/src/main.rs b/crates/bin/pindexer/src/main.rs index f91dcf4e79..d7e7bdf4bf 100644 --- a/crates/bin/pindexer/src/main.rs +++ b/crates/bin/pindexer/src/main.rs @@ -1,10 +1,9 @@ use anyhow::Result; -use pindexer::{Indexer, IndexerExt as _}; use pindexer::block::block::Block; +use pindexer::{Indexer, IndexerExt as _}; #[tokio::main] async fn main() -> Result<()> { - dbg!("hello"); Indexer::new() .with_default_tracing() .with_default_penumbra_app_views() diff --git a/crates/core/component/sct/src/component/tree.rs b/crates/core/component/sct/src/component/tree.rs index 3bd4021c81..7773d2c65f 100644 --- a/crates/core/component/sct/src/component/tree.rs +++ b/crates/core/component/sct/src/component/tree.rs @@ -4,7 +4,7 @@ use cnidarium::{StateRead, StateWrite}; use penumbra_proto::{StateReadProto, StateWriteProto}; use penumbra_tct as tct; use tct::builder::{block, epoch}; -use tracing::{instrument}; +use tracing::instrument; use crate::{ component::clock::EpochRead, event, state_key, CommitmentSource, NullificationInfo, Nullifier, @@ -80,7 +80,11 @@ pub trait SctManager: StateWrite { self.put(state_key::tree::anchor_by_height(height), sct_anchor); self.record_proto(event::anchor(height, sct_anchor)); - self.record_proto(event::block_root(height, block_root, chrono::offset::Utc::now().timestamp())); + self.record_proto(event::block_root( + height, + block_root, + chrono::offset::Utc::now().timestamp(), + )); // Only record an epoch root event if we are ending the epoch. if let Some(epoch_root) = epoch_root { let index = self diff --git a/crates/core/component/sct/src/event.rs b/crates/core/component/sct/src/event.rs index 32410c040e..5584475b37 100644 --- a/crates/core/component/sct/src/event.rs +++ b/crates/core/component/sct/src/event.rs @@ -20,7 +20,7 @@ pub fn block_root(height: u64, root: block::Root, timestamp: i64) -> pb::EventBl timestamp: Some(Timestamp { seconds: timestamp, nanos: 0, - }) + }), } }