Skip to content

Commit

Permalink
fix: insert into db correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
vacekj committed Jul 18, 2024
1 parent 3ee08c6 commit 7cb7bf5
Show file tree
Hide file tree
Showing 7 changed files with 554 additions and 307 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 }
10 changes: 6 additions & 4 deletions crates/bin/pindexer/src/block/block.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
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 {}
Expand Down Expand Up @@ -37,17 +38,18 @@ CREATE TABLE IF NOT EXISTS block_details (
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)
VALUES ($1, $2)
",
)
.bind(&pe.height)
.bind(&pe.timestamp)
.execute(dbtx.as_mu())
.await?;
.bind(&pe.height)
.bind(DateTime::from_timestamp(timestamp.seconds, timestamp.nanos as u32).unwrap())
.execute(dbtx.as_mut())
.await?;

Ok(())
}
Expand Down
6 changes: 2 additions & 4 deletions crates/core/component/sct/src/component/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,8 @@ pub trait SctManager: StateWrite {
// TODO: can we move this out to NV storage?
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));
dbg!("BLOCK");
self.record_proto(event::block_timestamp(height, chrono::offset::Utc::now().timestamp()));
self.record_proto(event::anchor(height, sct_anchor, block_timestamp));
self.record_proto(event::block_root(height, block_root, block_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
8 changes: 1 addition & 7 deletions crates/core/component/sct/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,10 @@ pub fn anchor(height: u64, anchor: tct::Root, timestamp: i64) -> pb::EventAnchor
}
}

pub fn block_root(height: u64, root: block::Root) -> pb::EventBlockRoot {
pub fn block_root(height: u64, root: block::Root, timestamp: i64) -> pb::EventBlockRoot {
pb::EventBlockRoot {
height,
root: Some(root.into()),
}
}

pub fn block_timestamp(height: u64, timestamp: i64) -> pb::EventBlockTimestamp {
pb::EventBlockTimestamp {
height,
timestamp: Some(Timestamp {
seconds: timestamp,
nanos: 0,
Expand Down
Loading

0 comments on commit 7cb7bf5

Please sign in to comment.