Skip to content

Commit

Permalink
fix(cat-gateway): incorporate cip36 registration for votekey queries
Browse files Browse the repository at this point in the history
  • Loading branch information
saibatizoku committed Nov 13, 2024
1 parent 52d786b commit 85b532f
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
//! CIP-36 registration by Vote Key Queries used in purging data.
use std::{fmt::Debug, sync::Arc};

use scylla::{
prepared_statement::PreparedStatement, transport::iterator::TypedRowIterator, SerializeRow,
Session,
};
use tracing::error;

use crate::{
db::index::{
queries::{
purge::{PreparedDeleteQuery, PreparedQueries, PreparedSelectQuery},
FallibleQueryResults, SizedBatch,
},
session::CassandraSession,
},
settings::cassandra_db,
};

pub(crate) mod result {
//! Return values for CIP-36 registration by Vote key purge queries.
/// Primary Key Row
pub(crate) type PrimaryKey = (Vec<u8>, Vec<u8>, num_bigint::BigInt, i16, bool);
}

/// Select primary keys for CIP-36 registration.
const SELECT_QUERY: &str = include_str!("./cql/get_cip36_registration_for_vote_key.cql");

/// Primary Key Value.
#[derive(SerializeRow)]
pub(crate) struct Params {
/// Vote key - Binary 28 bytes.
pub(crate) vote_key: Vec<u8>,
/// Stake Address - Binary 28 bytes. 0 bytes = not staked.
pub(crate) stake_address: Vec<u8>,
/// Block Slot Number
pub(crate) slot_no: num_bigint::BigInt,
/// Transaction Offset inside the block.
pub(crate) txn: i16,
/// True if registration is valid.
pub(crate) valid: bool,
}

impl Debug for Params {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Params")
.field("vote_key", &self.vote_key)
.field("stake_address", &self.stake_address)
.field("slot_no", &self.slot_no)
.field("txn", &self.txn)
.field("valid", &self.valid)
.finish()
}
}

impl From<result::PrimaryKey> for Params {
fn from(value: result::PrimaryKey) -> Self {
Self {
vote_key: value.0,
stake_address: value.1,
slot_no: value.2,
txn: value.3,
valid: value.4,
}
}
}
/// Get primary key for CIP-36 registration by Vote key query.
pub(crate) struct PrimaryKeyQuery;

impl PrimaryKeyQuery {
/// Prepares a query to get all CIP-36 registration by Vote key primary keys.
pub(crate) async fn prepare(session: &Arc<Session>) -> anyhow::Result<PreparedStatement> {
let select_primary_key = PreparedQueries::prepare(
session.clone(),
SELECT_QUERY,
scylla::statement::Consistency::All,
true,
)
.await;

if let Err(ref error) = select_primary_key {
error!(error=%error, "Failed to prepare get CIP-36 registration by Vote key primary key query");
};

select_primary_key
}

/// Executes a query to get all CIP-36 registration by Vote key primary keys.
pub(crate) async fn execute(
session: &CassandraSession,
) -> anyhow::Result<TypedRowIterator<result::PrimaryKey>> {
let iter = session
.purge_execute_iter(PreparedSelectQuery::TxoAda)
.await?
.into_typed::<result::PrimaryKey>();

Ok(iter)
}
}

/// Delete CIP-36 registration
const DELETE_QUERY: &str = include_str!("./cql/delete_cip36_registration_for_vote_key.cql");

/// Delete CIP-36 registration by Vote key Query
pub(crate) struct DeleteQuery;

impl DeleteQuery {
/// Prepare Batch of Delete Queries
pub(crate) async fn prepare_batch(
session: &Arc<Session>, cfg: &cassandra_db::EnvVars,
) -> anyhow::Result<SizedBatch> {
let delete_queries = PreparedQueries::prepare_batch(
session.clone(),
DELETE_QUERY,
cfg,
scylla::statement::Consistency::Any,
true,
false,
)
.await?;
Ok(delete_queries)
}

/// Executes a DELETE Query
pub(crate) async fn execute(
session: &CassandraSession, params: Vec<Params>,
) -> FallibleQueryResults {
let results = session
.purge_execute_batch(PreparedDeleteQuery::TxoAda, params)
.await?;

Ok(results)
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
-- Get all primary keys from CIP-36 registration by Stake Address.
-- Get all primary keys from CIP-36 registration by Vote Key.
SELECT
vote_key,
stake_address,
Expand Down
5 changes: 5 additions & 0 deletions catalyst-gateway/bin/src/db/index/queries/purge/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Queries for purging volatile data.
pub(crate) mod cip36_registration;
pub(crate) mod cip36_registration_for_vote_key;
pub(crate) mod stake_registration;
pub(crate) mod txi_by_hash;
pub(crate) mod txo_ada;
Expand Down Expand Up @@ -170,6 +171,10 @@ impl PreparedQueries {
cip36_registration::PrimaryKeyQuery::prepare(&session).await?;
let delete_cip36_registration =
cip36_registration::DeleteQuery::prepare_batch(&session, cfg).await?;
let select_cip36_registration =
cip36_registration::PrimaryKeyQuery::prepare(&session).await?;
let delete_cip36_registration =
cip36_registration::DeleteQuery::prepare_batch(&session, cfg).await?;

todo!("WIP");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-- Index of CIP-36 registrations searchable by Stake Address.
-- Full registration data needs to be queried from the man cip36 registration tables.
-- Index of CIP-36 registrations searchable by Vote Key.
-- Full registration data needs to be queried from the main CIP-36 registration tables.
-- Includes both Valid and Invalid registrations.
CREATE TABLE IF NOT EXISTS cip36_registration_for_vote_key (
-- Primary Key Data
Expand Down

0 comments on commit 85b532f

Please sign in to comment.