Skip to content

Commit

Permalink
pindexer: stake: use text for identity keys consistently
Browse files Browse the repository at this point in the history
This fixes a bug where only one of the tables was edited.
  • Loading branch information
cronokirby committed Aug 14, 2024
1 parent 21fcba6 commit 0d8323a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 24 deletions.
19 changes: 10 additions & 9 deletions crates/bin/pindexer/src/stake/delegation_txs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use anyhow::{anyhow, Result};
use cometindex::{async_trait, sqlx, AppView, ContextualizedEvent, PgPool, PgTransaction};
use penumbra_num::Amount;
use penumbra_proto::{core::component::stake::v1 as pb, event::ProtoEvent};
use penumbra_stake::IdentityKey;

#[derive(Debug)]
pub struct DelegationTxs {}
Expand All @@ -17,7 +18,7 @@ impl AppView for DelegationTxs {
sqlx::query(
"CREATE TABLE stake_delegation_txs (
id SERIAL PRIMARY KEY,
validator_ik BYTEA NOT NULL,
ik TEXT NOT NULL,
amount BIGINT NOT NULL,
height BIGINT NOT NULL,
tx_hash BYTEA NOT NULL
Expand All @@ -26,8 +27,8 @@ impl AppView for DelegationTxs {
.execute(dbtx.as_mut())
.await?;

// Create index on validator_ik
sqlx::query("CREATE INDEX idx_stake_delegation_txs_validator_ik ON stake_delegation_txs(validator_ik);")
// Create index on ik
sqlx::query("CREATE INDEX idx_stake_delegation_txs_ik ON stake_delegation_txs(ik);")
.execute(dbtx.as_mut())
.await?;

Expand All @@ -38,8 +39,8 @@ impl AppView for DelegationTxs {
.execute(dbtx.as_mut())
.await?;

// Create composite index on validator_ik and height (descending)
sqlx::query("CREATE INDEX idx_stake_delegation_txs_validator_ik_height ON stake_delegation_txs(validator_ik, height DESC);")
// Create composite index on ik and height (descending)
sqlx::query("CREATE INDEX idx_stake_delegation_txs_validator_ik_height ON stake_delegation_txs(ik, height DESC);")
.execute(dbtx.as_mut())
.await?;

Expand All @@ -58,20 +59,20 @@ impl AppView for DelegationTxs {
) -> Result<()> {
let pe = pb::EventDelegate::from_event(event.as_ref())?;

let ik_bytes = pe
let ik: IdentityKey = pe
.identity_key
.ok_or_else(|| anyhow::anyhow!("missing ik in event"))?
.ik;
.try_into()?;

let amount = Amount::try_from(
pe.amount
.ok_or_else(|| anyhow::anyhow!("missing amount in event"))?,
)?;

sqlx::query(
"INSERT INTO stake_delegation_txs (validator_ik, amount, height, tx_hash) VALUES ($1, $2, $3, $4)"
"INSERT INTO stake_delegation_txs (ik, amount, height, tx_hash) VALUES ($1, $2, $3, $4)"
)
.bind(&ik_bytes)
.bind(ik.to_string())
.bind(amount.value() as i64)
.bind(event.block_height as i64)
.bind(event.tx_hash.ok_or_else(|| anyhow!("missing tx hash in event"))?)
Expand Down
9 changes: 5 additions & 4 deletions crates/bin/pindexer/src/stake/missed_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use anyhow::Result;
use cometindex::{async_trait, sqlx, AppView, ContextualizedEvent, PgPool, PgTransaction};

use penumbra_proto::{core::component::stake::v1 as pb, event::ProtoEvent};
use penumbra_stake::IdentityKey;

#[derive(Debug)]
pub struct MissedBlocks {}
Expand All @@ -18,7 +19,7 @@ impl AppView for MissedBlocks {
"CREATE TABLE stake_missed_blocks (
id SERIAL PRIMARY KEY,
height BIGINT NOT NULL,
ik BYTEA NOT NULL
ik TEXT NOT NULL
);",
)
.execute(dbtx.as_mut())
Expand Down Expand Up @@ -55,16 +56,16 @@ impl AppView for MissedBlocks {
_src_db: &PgPool,
) -> Result<(), anyhow::Error> {
let pe = pb::EventValidatorMissedBlock::from_event(event.as_ref())?;
let ik_bytes = pe
let ik: IdentityKey = pe
.identity_key
.ok_or_else(|| anyhow::anyhow!("missing ik in event"))?
.ik;
.try_into()?;

let height = event.block_height;

sqlx::query("INSERT INTO stake_missed_blocks (height, ik) VALUES ($1, $2)")
.bind(height as i64)
.bind(ik_bytes)
.bind(ik.to_string())
.execute(dbtx.as_mut())
.await?;

Expand Down
4 changes: 2 additions & 2 deletions crates/bin/pindexer/src/stake/slashings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ impl AppView for Slashings {
"CREATE TABLE stake_slashings (
id SERIAL PRIMARY KEY,
height BIGINT NOT NULL,
ik BYTEA NOT NULL,
ik TEXT NOT NULL,
epoch_index BIGINT NOT NULL,
penalty TEXT NOT NULL
);",
Expand Down Expand Up @@ -73,7 +73,7 @@ impl AppView for Slashings {
VALUES ($1, $2, $3, $4)",
)
.bind(height as i64)
.bind(ik.to_bytes())
.bind(ik.to_string())
.bind(epoch_index as i64)
.bind(penalty_json)
.execute(dbtx.as_mut())
Expand Down
19 changes: 10 additions & 9 deletions crates/bin/pindexer/src/stake/undelegation_txs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use anyhow::{anyhow, Result};
use cometindex::{async_trait, sqlx, AppView, ContextualizedEvent, PgPool, PgTransaction};
use penumbra_num::Amount;
use penumbra_proto::{core::component::stake::v1 as pb, event::ProtoEvent};
use penumbra_stake::IdentityKey;

#[derive(Debug)]
pub struct UndelegationTxs {}
Expand All @@ -17,7 +18,7 @@ impl AppView for UndelegationTxs {
sqlx::query(
"CREATE TABLE stake_undelegation_txs (
id SERIAL PRIMARY KEY,
validator_ik BYTEA NOT NULL,
ik TEXT NOT NULL,
amount BIGINT NOT NULL,
height BIGINT NOT NULL,
tx_hash BYTEA NOT NULL
Expand All @@ -26,8 +27,8 @@ impl AppView for UndelegationTxs {
.execute(dbtx.as_mut())
.await?;

// Create index on validator_ik
sqlx::query("CREATE INDEX idx_stake_undelegation_txs_validator_ik ON stake_undelegation_txs(validator_ik);")
// Create index on ik
sqlx::query("CREATE INDEX idx_stake_undelegation_txs_ik ON stake_undelegation_txs(ik);")
.execute(dbtx.as_mut())
.await?;

Expand All @@ -38,8 +39,8 @@ impl AppView for UndelegationTxs {
.execute(dbtx.as_mut())
.await?;

// Create composite index on validator_ik and height (descending)
sqlx::query("CREATE INDEX idx_stake_undelegation_txs_validator_ik_height ON stake_undelegation_txs(validator_ik, height DESC);")
// Create composite index on ik and height (descending)
sqlx::query("CREATE INDEX idx_stake_undelegation_txs_ik_height ON stake_undelegation_txs(ik, height DESC);")
.execute(dbtx.as_mut())
.await?;

Expand All @@ -58,20 +59,20 @@ impl AppView for UndelegationTxs {
) -> Result<()> {
let pe = pb::EventUndelegate::from_event(event.as_ref())?;

let ik_bytes = pe
let ik: IdentityKey = pe
.identity_key
.ok_or_else(|| anyhow::anyhow!("missing ik in event"))?
.ik;
.try_into()?;

let amount = Amount::try_from(
pe.amount
.ok_or_else(|| anyhow::anyhow!("missing amount in event"))?,
)?;

sqlx::query(
"INSERT INTO stake_undelegation_txs (validator_ik, amount, height, tx_hash) VALUES ($1, $2, $3, $4)"
"INSERT INTO stake_undelegation_txs (ik, amount, height, tx_hash) VALUES ($1, $2, $3, $4)"
)
.bind(&ik_bytes)
.bind(ik.to_string())
.bind(amount.value() as i64)
.bind(event.block_height as i64)
.bind(event.tx_hash.ok_or_else(|| anyhow!("missing tx hash in event"))?)
Expand Down

0 comments on commit 0d8323a

Please sign in to comment.