From 92f67c1fe8a6fcc47fcde199cce26019c1bab604 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joaqu=C3=ADn=20Rosales?= Date: Wed, 13 Nov 2024 19:52:32 -0600 Subject: [PATCH] fix(cat-gateway): incorporate chain root for role0 registration queries --- .../queries/purge/chain_root_for_role0_key.rs | 128 ++++++++++++++++++ .../cql/delete_chain_root_for_role0_key.cql | 2 +- .../cql/get_chain_root_for_role0_key.cql | 2 +- .../bin/src/db/index/queries/purge/mod.rs | 17 ++- .../schema/cql/chain_root_for_role0_key.cql | 4 +- 5 files changed, 144 insertions(+), 9 deletions(-) create mode 100644 catalyst-gateway/bin/src/db/index/queries/purge/chain_root_for_role0_key.rs diff --git a/catalyst-gateway/bin/src/db/index/queries/purge/chain_root_for_role0_key.rs b/catalyst-gateway/bin/src/db/index/queries/purge/chain_root_for_role0_key.rs new file mode 100644 index 00000000000..2d3e40d5ba7 --- /dev/null +++ b/catalyst-gateway/bin/src/db/index/queries/purge/chain_root_for_role0_key.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_role0_key.cql b/catalyst-gateway/bin/src/db/index/queries/purge/cql/delete_chain_root_for_role0_key.cql index 63ef2aedba8..5285b565f7c 100644 --- a/catalyst-gateway/bin/src/db/index/queries/purge/cql/delete_chain_root_for_role0_key.cql +++ b/catalyst-gateway/bin/src/db/index/queries/purge/cql/delete_chain_root_for_role0_key.cql @@ -1,4 +1,4 @@ --- Delete all the chain roots for a role0 key +-- Delete Chain Root For Role0 Key. RBAC 509 registrations. DELETE FROM chain_root_for_role0_key WHERE role0_key = :role0_key AND slot_no = :slot_no diff --git a/catalyst-gateway/bin/src/db/index/queries/purge/cql/get_chain_root_for_role0_key.cql b/catalyst-gateway/bin/src/db/index/queries/purge/cql/get_chain_root_for_role0_key.cql index 7987c2da104..b817f18bc79 100644 --- a/catalyst-gateway/bin/src/db/index/queries/purge/cql/get_chain_root_for_role0_key.cql +++ b/catalyst-gateway/bin/src/db/index/queries/purge/cql/get_chain_root_for_role0_key.cql @@ -1,4 +1,4 @@ --- Get all primary keys from chain roots for a role0 key. +-- Get all primary keys from Chain Root For Role0 Key. RBAC 509 registrations. SELECT role0_key, slot_no, 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 6341330941d..ef594a2ce0a 100644 --- a/catalyst-gateway/bin/src/db/index/queries/purge/mod.rs +++ b/catalyst-gateway/bin/src/db/index/queries/purge/mod.rs @@ -1,5 +1,6 @@ //! Queries for purging volatile data. +pub(crate) mod chain_root_for_role0_key; pub(crate) mod cip36_registration; pub(crate) mod cip36_registration_for_vote_key; pub(crate) mod cip36_registration_invalid; @@ -129,18 +130,18 @@ pub(crate) struct PreparedQueries { delete_cip36_registration_for_vote_key: SizedBatch, /// RBAC 509 Registrations Primary Key Query. select_rbac509_registration: PreparedStatement, - /// Chain Root for TX ID Primary Key Query.. - select_chain_root_for_txn_id: PreparedStatement, - /// Chain Root for Role 0 Key Primary Key Query.. - select_chain_root_for_role0_key: PreparedStatement, - /// Chain Root for Stake Address Primary Key Query.. - select_chain_root_for_stake_address: PreparedStatement, /// RBAC 509 Registrations Delete Query. delete_rbac509_registration: SizedBatch, + /// Chain Root for TX ID Primary Key Query.. + select_chain_root_for_txn_id: PreparedStatement, /// Chain Root for TX ID Delete Query.. delete_chain_root_for_txn_id: SizedBatch, + /// Chain Root for Role 0 Key Primary Key Query.. + select_chain_root_for_role0_key: PreparedStatement, /// Chain Root for Role 0 Key Delete Query.. delete_chain_root_for_role0_key: SizedBatch, + /// Chain Root for Stake Address Primary Key Query.. + select_chain_root_for_stake_address: PreparedStatement, /// Chain Root for Stake Address Delete Query.. delete_chain_root_for_stake_address: SizedBatch, } @@ -185,6 +186,10 @@ impl PreparedQueries { 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"); } diff --git a/catalyst-gateway/bin/src/db/index/schema/cql/chain_root_for_role0_key.cql b/catalyst-gateway/bin/src/db/index/schema/cql/chain_root_for_role0_key.cql index 6d5ff854621..fb34c2a4131 100644 --- a/catalyst-gateway/bin/src/db/index/schema/cql/chain_root_for_role0_key.cql +++ b/catalyst-gateway/bin/src/db/index/schema/cql/chain_root_for_role0_key.cql @@ -1,9 +1,11 @@ --- Index of Chain Root For Role0 Key. RBAC 509 registrations. +-- Index of Chain Root For Role0 Key. RBAC 509 registrations. CREATE TABLE IF NOT EXISTS chain_root_for_role0_key ( -- Primary Key Data role0_key blob, -- 16 Bytes of Role0 Key. 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 (role0_key, slot_no, txn)