Skip to content

Commit

Permalink
feat: correctly define and insert blockdetail during indexing
Browse files Browse the repository at this point in the history
  • Loading branch information
vacekj committed Jul 18, 2024
1 parent a802d8d commit 85bce6c
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 12 deletions.
2 changes: 1 addition & 1 deletion crates/bin/pindexer/src/block.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pub mod block;
pub mod block;
27 changes: 22 additions & 5 deletions crates/bin/pindexer/src/block/block.rs
Original file line number Diff line number Diff line change
@@ -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(())
}

Expand All @@ -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(())
}
Expand Down
2 changes: 1 addition & 1 deletion crates/bin/pindexer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
3 changes: 1 addition & 2 deletions crates/bin/pindexer/src/main.rs
Original file line number Diff line number Diff line change
@@ -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()
Expand Down
8 changes: 6 additions & 2 deletions crates/core/component/sct/src/component/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion crates/core/component/sct/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub fn block_root(height: u64, root: block::Root, timestamp: i64) -> pb::EventBl
timestamp: Some(Timestamp {
seconds: timestamp,
nanos: 0,
})
}),
}
}

Expand Down

0 comments on commit 85bce6c

Please sign in to comment.