diff --git a/catalyst-gateway/bin/src/db/index/queries/purge/chain_root_for_stake_address.rs b/catalyst-gateway/bin/src/db/index/queries/purge/chain_root_for_stake_address.rs new file mode 100644 index 0000000000..2d3e40d5ba --- /dev/null +++ b/catalyst-gateway/bin/src/db/index/queries/purge/chain_root_for_stake_address.rs @@ -0,0 +1,128 @@ +//! Chain Root For Role0 Key (RBAC 509 registrations) 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 Chain Root For Role0 Key registration purge queries. + + /// Primary Key Row + pub(crate) type PrimaryKey = (Vec, num_bigint::BigInt, i16); +} + +/// Select primary keys for Chain Root For Role0 Key registration. +const SELECT_QUERY: &str = include_str!("./cql/get_chain_root_for_role0_key.cql"); + +/// Primary Key Value. +#[derive(SerializeRow)] +pub(crate) struct Params { + /// Role0 Key - Binary 16 bytes. + pub(crate) role0_key: Vec, + /// Block Slot Number + pub(crate) slot_no: num_bigint::BigInt, + /// Transaction Offset inside the block. + pub(crate) txn: i16, +} + +impl Debug for Params { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("Params") + .field("role0_key", &self.role0_key) + .field("slot_no", &self.slot_no) + .field("txn", &self.txn) + .finish() + } +} + +impl From for Params { + fn from(value: result::PrimaryKey) -> Self { + Self { + role0_key: value.0, + slot_no: value.1, + txn: value.2, + } + } +} +/// Get primary key for Chain Root For Role0 Key registration query. +pub(crate) struct PrimaryKeyQuery; + +impl PrimaryKeyQuery { + /// Prepares a query to get all Chain Root For Role0 Key registration primary keys. + pub(crate) async fn prepare(session: &Arc) -> anyhow::Result { + 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 Chain Root For Role0 Key registration primary key query"); + }; + + select_primary_key + } + + /// Executes a query to get all Chain Root For Role0 Key registration primary keys. + pub(crate) async fn execute( + session: &CassandraSession, + ) -> anyhow::Result> { + let iter = session + .purge_execute_iter(PreparedSelectQuery::ChainRootForRole0Key) + .await? + .into_typed::(); + + Ok(iter) + } +} + +/// Delete Chain Root For Role0 Key registration +const DELETE_QUERY: &str = include_str!("./cql/delete_chain_root_for_role0_key.cql"); + +/// Delete Chain Root For Role0 Key registration Query +pub(crate) struct DeleteQuery; + +impl DeleteQuery { + /// Prepare Batch of Delete Queries + pub(crate) async fn prepare_batch( + session: &Arc, cfg: &cassandra_db::EnvVars, + ) -> anyhow::Result { + 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, + ) -> FallibleQueryResults { + let results = session + .purge_execute_batch(PreparedDeleteQuery::ChainRootForRole0Key, params) + .await?; + + Ok(results) + } +} diff --git a/catalyst-gateway/bin/src/db/index/queries/purge/chain_root_for_txn_id.rs b/catalyst-gateway/bin/src/db/index/queries/purge/chain_root_for_txn_id.rs new file mode 100644 index 0000000000..2d3e40d5ba --- /dev/null +++ b/catalyst-gateway/bin/src/db/index/queries/purge/chain_root_for_txn_id.rs @@ -0,0 +1,128 @@ +//! Chain Root For Role0 Key (RBAC 509 registrations) 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 Chain Root For Role0 Key registration purge queries. + + /// Primary Key Row + pub(crate) type PrimaryKey = (Vec, num_bigint::BigInt, i16); +} + +/// Select primary keys for Chain Root For Role0 Key registration. +const SELECT_QUERY: &str = include_str!("./cql/get_chain_root_for_role0_key.cql"); + +/// Primary Key Value. +#[derive(SerializeRow)] +pub(crate) struct Params { + /// Role0 Key - Binary 16 bytes. + pub(crate) role0_key: Vec, + /// Block Slot Number + pub(crate) slot_no: num_bigint::BigInt, + /// Transaction Offset inside the block. + pub(crate) txn: i16, +} + +impl Debug for Params { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("Params") + .field("role0_key", &self.role0_key) + .field("slot_no", &self.slot_no) + .field("txn", &self.txn) + .finish() + } +} + +impl From for Params { + fn from(value: result::PrimaryKey) -> Self { + Self { + role0_key: value.0, + slot_no: value.1, + txn: value.2, + } + } +} +/// Get primary key for Chain Root For Role0 Key registration query. +pub(crate) struct PrimaryKeyQuery; + +impl PrimaryKeyQuery { + /// Prepares a query to get all Chain Root For Role0 Key registration primary keys. + pub(crate) async fn prepare(session: &Arc) -> anyhow::Result { + 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 Chain Root For Role0 Key registration primary key query"); + }; + + select_primary_key + } + + /// Executes a query to get all Chain Root For Role0 Key registration primary keys. + pub(crate) async fn execute( + session: &CassandraSession, + ) -> anyhow::Result> { + let iter = session + .purge_execute_iter(PreparedSelectQuery::ChainRootForRole0Key) + .await? + .into_typed::(); + + Ok(iter) + } +} + +/// Delete Chain Root For Role0 Key registration +const DELETE_QUERY: &str = include_str!("./cql/delete_chain_root_for_role0_key.cql"); + +/// Delete Chain Root For Role0 Key registration Query +pub(crate) struct DeleteQuery; + +impl DeleteQuery { + /// Prepare Batch of Delete Queries + pub(crate) async fn prepare_batch( + session: &Arc, cfg: &cassandra_db::EnvVars, + ) -> anyhow::Result { + 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, + ) -> FallibleQueryResults { + let results = session + .purge_execute_batch(PreparedDeleteQuery::ChainRootForRole0Key, params) + .await?; + + Ok(results) + } +} diff --git a/catalyst-gateway/bin/src/db/index/queries/purge/cql/delete_chain_root_for_stake_addr.cql b/catalyst-gateway/bin/src/db/index/queries/purge/cql/delete_chain_root_for_stake_addr.cql index 41a776e4ef..d964bb1181 100644 --- a/catalyst-gateway/bin/src/db/index/queries/purge/cql/delete_chain_root_for_stake_addr.cql +++ b/catalyst-gateway/bin/src/db/index/queries/purge/cql/delete_chain_root_for_stake_addr.cql @@ -1,3 +1,5 @@ --- Delete Chain Root For TX ID. RBAC 509 registrations. -DELETE FROM chain_root_for_txn_id -WHERE transaction_id = :transaction_id +-- Delete Chain Root For Stake Address (RBAC 509 registrations). +DELETE FROM chain_root_for_stake_addr +WHERE stake_addr = :stake_addr + AND slot_no = :slot_no + AND txn = :txn diff --git a/catalyst-gateway/bin/src/db/index/queries/purge/cql/delete_chain_root_for_txn_id.cql b/catalyst-gateway/bin/src/db/index/queries/purge/cql/delete_chain_root_for_txn_id.cql index 2a992be601..5a4ca4d773 100644 --- a/catalyst-gateway/bin/src/db/index/queries/purge/cql/delete_chain_root_for_txn_id.cql +++ b/catalyst-gateway/bin/src/db/index/queries/purge/cql/delete_chain_root_for_txn_id.cql @@ -1,5 +1,3 @@ --- Delete all the chain roots for a stake address -DELETE FROM chain_root_for_stake_addr -WHERE stake_addr = :stake_addr - AND slot_no = :slot_no - AND txn = :txn +-- Delete Chain Root For TX ID (RBAC 509 registrations). +DELETE FROM chain_root_for_txn_id +WHERE transaction_id = :transaction_id diff --git a/catalyst-gateway/bin/src/db/index/queries/purge/cql/get_chain_root_for_stake_addr.cql b/catalyst-gateway/bin/src/db/index/queries/purge/cql/get_chain_root_for_stake_addr.cql index f3f9d0e218..90dad7264b 100644 --- a/catalyst-gateway/bin/src/db/index/queries/purge/cql/get_chain_root_for_stake_addr.cql +++ b/catalyst-gateway/bin/src/db/index/queries/purge/cql/get_chain_root_for_stake_addr.cql @@ -1,3 +1,6 @@ --- Get all primary keys from Chain Root For TX ID. RBAC 509 Registrations. -SELECT transaction_id -FROM chain_root_for_txn_id +-- Get all primary keys from Chain Root For Stake Address (RBAC 509 registrations). +SELECT + role0_key, + slot_no, + txn +FROM chain_root_for_role0_key diff --git a/catalyst-gateway/bin/src/db/index/queries/purge/cql/get_chain_root_for_txn_id.cql b/catalyst-gateway/bin/src/db/index/queries/purge/cql/get_chain_root_for_txn_id.cql index 84bb782303..176a29975f 100644 --- a/catalyst-gateway/bin/src/db/index/queries/purge/cql/get_chain_root_for_txn_id.cql +++ b/catalyst-gateway/bin/src/db/index/queries/purge/cql/get_chain_root_for_txn_id.cql @@ -1,6 +1,3 @@ --- Get all primary keys from Chain Roots for a Role0 Key. -SELECT - role0_key, - slot_no, - txn -FROM chain_root_for_role0_key +-- Get all primary keys from Chain Root For TX ID (RBAC 509 Registrations). +SELECT transaction_id +FROM chain_root_for_txn_id diff --git a/catalyst-gateway/bin/src/db/index/queries/purge/mod.rs b/catalyst-gateway/bin/src/db/index/queries/purge/mod.rs index ef594a2ce0..978a81d796 100644 --- a/catalyst-gateway/bin/src/db/index/queries/purge/mod.rs +++ b/catalyst-gateway/bin/src/db/index/queries/purge/mod.rs @@ -1,6 +1,8 @@ //! Queries for purging volatile data. pub(crate) mod chain_root_for_role0_key; +pub(crate) mod chain_root_for_stake_address; +pub(crate) mod chain_root_for_txn_id; pub(crate) mod cip36_registration; pub(crate) mod cip36_registration_for_vote_key; pub(crate) mod cip36_registration_invalid; @@ -27,7 +29,6 @@ const NO_PARAMS: () = (); /// All prepared DELETE query statements (purge DB table rows). #[derive(strum_macros::Display)] -#[allow(dead_code)] pub(crate) enum PreparedDeleteQuery { /// TXO Delete query. TxoAda, @@ -59,7 +60,6 @@ pub(crate) enum PreparedDeleteQuery { /// All prepared SELECT query statements (primary keys from table). #[derive(strum_macros::Display)] -#[allow(dead_code)] pub(crate) enum PreparedSelectQuery { /// TXO Select query. TxoAda, @@ -148,50 +148,71 @@ pub(crate) struct PreparedQueries { impl PreparedQueries { /// Create new prepared queries for a given session. - #[allow(clippy::todo, unused_variables)] pub(crate) async fn new( session: Arc, cfg: &cassandra_db::EnvVars, ) -> anyhow::Result { // We initialize like this, so that all errors preparing querys get shown before aborting. - let select_txo_ada = txo_ada::PrimaryKeyQuery::prepare(&session).await?; - let delete_txo_ada = txo_ada::DeleteQuery::prepare_batch(&session, cfg).await?; - let select_txo_assets = txo_assets::PrimaryKeyQuery::prepare(&session).await?; - let delete_txo_assets = txo_assets::DeleteQuery::prepare_batch(&session, cfg).await?; - let select_unstaked_txo_ada = unstaked_txo_ada::PrimaryKeyQuery::prepare(&session).await?; - let delete_unstaked_txo_ada = - unstaked_txo_ada::DeleteQuery::prepare_batch(&session, cfg).await?; - let select_unstaked_txo_assets = - unstaked_txo_assets::PrimaryKeyQuery::prepare(&session).await?; - let delete_unstaked_txo_assets = - unstaked_txo_assets::DeleteQuery::prepare_batch(&session, cfg).await?; - let select_txi_by_hash = txi_by_hash::PrimaryKeyQuery::prepare(&session).await?; - let delete_txi_by_hash = txi_by_hash::DeleteQuery::prepare_batch(&session, cfg).await?; - let select_stake_registration = - stake_registration::PrimaryKeyQuery::prepare(&session).await?; - let delete_stake_registration = - stake_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?; - let select_cip36_registration_invalid = - cip36_registration_invalid::PrimaryKeyQuery::prepare(&session).await?; - let delete_cip36_registration_invalid = - cip36_registration_invalid::DeleteQuery::prepare_batch(&session, cfg).await?; - let select_cip36_registration_for_vote_key = - cip36_registration_for_vote_key::PrimaryKeyQuery::prepare(&session).await?; - let delete_cip36_registration_for_vote_key = - cip36_registration_for_vote_key::DeleteQuery::prepare_batch(&session, cfg).await?; - let select_rbac509_registration = - rbac509_registration::PrimaryKeyQuery::prepare(&session).await?; - let delete_rbac509_registration = - rbac509_registration::DeleteQuery::prepare_batch(&session, cfg).await?; - let select_chain_root_for_role0_key = - chain_root_for_role0_key::PrimaryKeyQuery::prepare(&session).await?; - let delete_chain_root_for_role0_key = - chain_root_for_role0_key::DeleteQuery::prepare_batch(&session, cfg).await?; - - todo!("WIP"); + Ok(Self { + select_txo_ada: txo_ada::PrimaryKeyQuery::prepare(&session).await?, + delete_txo_ada: txo_ada::DeleteQuery::prepare_batch(&session, cfg).await?, + select_txo_assets: txo_assets::PrimaryKeyQuery::prepare(&session).await?, + delete_txo_assets: txo_assets::DeleteQuery::prepare_batch(&session, cfg).await?, + select_unstaked_txo_ada: unstaked_txo_ada::PrimaryKeyQuery::prepare(&session).await?, + delete_unstaked_txo_ada: unstaked_txo_ada::DeleteQuery::prepare_batch(&session, cfg) + .await?, + select_unstaked_txo_assets: unstaked_txo_assets::PrimaryKeyQuery::prepare(&session) + .await?, + delete_unstaked_txo_assets: unstaked_txo_assets::DeleteQuery::prepare_batch( + &session, cfg, + ) + .await?, + select_txi_by_hash: txi_by_hash::PrimaryKeyQuery::prepare(&session).await?, + delete_txi_by_hash: txi_by_hash::DeleteQuery::prepare_batch(&session, cfg).await?, + select_stake_registration: stake_registration::PrimaryKeyQuery::prepare(&session) + .await?, + delete_stake_registration: stake_registration::DeleteQuery::prepare_batch( + &session, cfg, + ) + .await?, + select_cip36_registration: cip36_registration::PrimaryKeyQuery::prepare(&session) + .await?, + delete_cip36_registration: cip36_registration::DeleteQuery::prepare_batch( + &session, cfg, + ) + .await?, + select_cip36_registration_invalid: + cip36_registration_invalid::PrimaryKeyQuery::prepare(&session).await?, + delete_cip36_registration_invalid: + cip36_registration_invalid::DeleteQuery::prepare_batch(&session, cfg).await?, + select_cip36_registration_for_vote_key: + cip36_registration_for_vote_key::PrimaryKeyQuery::prepare(&session).await?, + delete_cip36_registration_for_vote_key: + cip36_registration_for_vote_key::DeleteQuery::prepare_batch(&session, cfg).await?, + select_rbac509_registration: rbac509_registration::PrimaryKeyQuery::prepare(&session) + .await?, + delete_rbac509_registration: rbac509_registration::DeleteQuery::prepare_batch( + &session, cfg, + ) + .await?, + select_chain_root_for_role0_key: chain_root_for_role0_key::PrimaryKeyQuery::prepare( + &session, + ) + .await?, + delete_chain_root_for_role0_key: chain_root_for_role0_key::DeleteQuery::prepare_batch( + &session, cfg, + ) + .await?, + select_chain_root_for_txn_id: chain_root_for_txn_id::PrimaryKeyQuery::prepare(&session) + .await?, + delete_chain_root_for_txn_id: chain_root_for_txn_id::DeleteQuery::prepare_batch( + &session, cfg, + ) + .await?, + select_chain_root_for_stake_address: + chain_root_for_stake_address::PrimaryKeyQuery::prepare(&session).await?, + delete_chain_root_for_stake_address: + chain_root_for_stake_address::DeleteQuery::prepare_batch(&session, cfg).await?, + }) } /// Prepares a statement. diff --git a/catalyst-gateway/bin/src/db/index/queries/purge/txo_ada.rs b/catalyst-gateway/bin/src/db/index/queries/purge/txo_ada.rs index dcb842ac4a..4ad61f27ff 100644 --- a/catalyst-gateway/bin/src/db/index/queries/purge/txo_ada.rs +++ b/catalyst-gateway/bin/src/db/index/queries/purge/txo_ada.rs @@ -84,7 +84,6 @@ impl PrimaryKeyQuery { } /// Executes a query to get all TXO by stake address primary keys. - #[allow(dead_code)] pub(crate) async fn execute( session: &CassandraSession, ) -> anyhow::Result> { @@ -121,7 +120,6 @@ impl DeleteQuery { } /// Executes a DELETE Query - #[allow(dead_code)] pub(crate) async fn execute( session: &CassandraSession, params: Vec, ) -> FallibleQueryResults { diff --git a/catalyst-gateway/bin/src/db/index/schema/cql/chain_root_for_stake_addr.cql b/catalyst-gateway/bin/src/db/index/schema/cql/chain_root_for_stake_addr.cql index 0ccaf81cf0..e1f23ac0b8 100644 --- a/catalyst-gateway/bin/src/db/index/schema/cql/chain_root_for_stake_addr.cql +++ b/catalyst-gateway/bin/src/db/index/schema/cql/chain_root_for_stake_addr.cql @@ -4,6 +4,8 @@ CREATE TABLE IF NOT EXISTS chain_root_for_stake_addr ( stake_addr blob, -- 32 Bytes of Stake Address. slot_no varint, -- slot number when the key_was_registered. txn smallint, -- Index of the TX which holds the registration data. + + -- Non-primary Key Data chain_root blob, -- 32 Bytes of Chain Root. PRIMARY KEY (stake_addr, slot_no, txn) diff --git a/catalyst-gateway/bin/src/db/index/schema/cql/chain_root_for_txn_id.cql b/catalyst-gateway/bin/src/db/index/schema/cql/chain_root_for_txn_id.cql index 4c8324b088..a051b76d76 100644 --- a/catalyst-gateway/bin/src/db/index/schema/cql/chain_root_for_txn_id.cql +++ b/catalyst-gateway/bin/src/db/index/schema/cql/chain_root_for_txn_id.cql @@ -1,4 +1,4 @@ --- Index of Chain Root For TX ID. RBAC 509 registrations. +-- Index of Chain Root For TX ID (RBAC 509 registrations). CREATE TABLE IF NOT EXISTS chain_root_for_txn_id ( -- Primary Key Data transaction_id blob, -- 32 Bytes of Transaction Hash.