Skip to content

Commit c4979ca

Browse files
committed
fix(cat-gateway): incorporate chain root for role0 registration queries
1 parent 1c5190e commit c4979ca

File tree

5 files changed

+144
-9
lines changed

5 files changed

+144
-9
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
//! Chain Root For Role0 Key (RBAC 509 registrations) Queries used in purging data.
2+
use std::{fmt::Debug, sync::Arc};
3+
4+
use scylla::{
5+
prepared_statement::PreparedStatement, transport::iterator::TypedRowIterator, SerializeRow,
6+
Session,
7+
};
8+
use tracing::error;
9+
10+
use crate::{
11+
db::index::{
12+
queries::{
13+
purge::{PreparedDeleteQuery, PreparedQueries, PreparedSelectQuery},
14+
FallibleQueryResults, SizedBatch,
15+
},
16+
session::CassandraSession,
17+
},
18+
settings::cassandra_db,
19+
};
20+
21+
pub(crate) mod result {
22+
//! Return values for Chain Root For Role0 Key registration purge queries.
23+
24+
/// Primary Key Row
25+
pub(crate) type PrimaryKey = (Vec<u8>, num_bigint::BigInt, i16);
26+
}
27+
28+
/// Select primary keys for Chain Root For Role0 Key registration.
29+
const SELECT_QUERY: &str = include_str!("./cql/get_chain_root_for_role0_key.cql");
30+
31+
/// Primary Key Value.
32+
#[derive(SerializeRow)]
33+
pub(crate) struct Params {
34+
/// Role0 Key - Binary 16 bytes.
35+
pub(crate) role0_key: Vec<u8>,
36+
/// Block Slot Number
37+
pub(crate) slot_no: num_bigint::BigInt,
38+
/// Transaction Offset inside the block.
39+
pub(crate) txn: i16,
40+
}
41+
42+
impl Debug for Params {
43+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
44+
f.debug_struct("Params")
45+
.field("role0_key", &self.role0_key)
46+
.field("slot_no", &self.slot_no)
47+
.field("txn", &self.txn)
48+
.finish()
49+
}
50+
}
51+
52+
impl From<result::PrimaryKey> for Params {
53+
fn from(value: result::PrimaryKey) -> Self {
54+
Self {
55+
role0_key: value.0,
56+
slot_no: value.1,
57+
txn: value.2,
58+
}
59+
}
60+
}
61+
/// Get primary key for Chain Root For Role0 Key registration query.
62+
pub(crate) struct PrimaryKeyQuery;
63+
64+
impl PrimaryKeyQuery {
65+
/// Prepares a query to get all Chain Root For Role0 Key registration primary keys.
66+
pub(crate) async fn prepare(session: &Arc<Session>) -> anyhow::Result<PreparedStatement> {
67+
let select_primary_key = PreparedQueries::prepare(
68+
session.clone(),
69+
SELECT_QUERY,
70+
scylla::statement::Consistency::All,
71+
true,
72+
)
73+
.await;
74+
75+
if let Err(ref error) = select_primary_key {
76+
error!(error=%error, "Failed to prepare get Chain Root For Role0 Key registration primary key query");
77+
};
78+
79+
select_primary_key
80+
}
81+
82+
/// Executes a query to get all Chain Root For Role0 Key registration primary keys.
83+
pub(crate) async fn execute(
84+
session: &CassandraSession,
85+
) -> anyhow::Result<TypedRowIterator<result::PrimaryKey>> {
86+
let iter = session
87+
.purge_execute_iter(PreparedSelectQuery::ChainRootForRole0Key)
88+
.await?
89+
.into_typed::<result::PrimaryKey>();
90+
91+
Ok(iter)
92+
}
93+
}
94+
95+
/// Delete Chain Root For Role0 Key registration
96+
const DELETE_QUERY: &str = include_str!("./cql/delete_chain_root_for_role0_key.cql");
97+
98+
/// Delete Chain Root For Role0 Key registration Query
99+
pub(crate) struct DeleteQuery;
100+
101+
impl DeleteQuery {
102+
/// Prepare Batch of Delete Queries
103+
pub(crate) async fn prepare_batch(
104+
session: &Arc<Session>, cfg: &cassandra_db::EnvVars,
105+
) -> anyhow::Result<SizedBatch> {
106+
let delete_queries = PreparedQueries::prepare_batch(
107+
session.clone(),
108+
DELETE_QUERY,
109+
cfg,
110+
scylla::statement::Consistency::Any,
111+
true,
112+
false,
113+
)
114+
.await?;
115+
Ok(delete_queries)
116+
}
117+
118+
/// Executes a DELETE Query
119+
pub(crate) async fn execute(
120+
session: &CassandraSession, params: Vec<Params>,
121+
) -> FallibleQueryResults {
122+
let results = session
123+
.purge_execute_batch(PreparedDeleteQuery::ChainRootForRole0Key, params)
124+
.await?;
125+
126+
Ok(results)
127+
}
128+
}

catalyst-gateway/bin/src/db/index/queries/purge/cql/delete_chain_root_for_role0_key.cql

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
-- Delete all the chain roots for a role0 key
1+
-- Delete Chain Root For Role0 Key. RBAC 509 registrations.
22
DELETE FROM chain_root_for_role0_key
33
WHERE role0_key = :role0_key
44
AND slot_no = :slot_no

catalyst-gateway/bin/src/db/index/queries/purge/cql/get_chain_root_for_role0_key.cql

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
-- Get all primary keys from chain roots for a role0 key.
1+
-- Get all primary keys from Chain Root For Role0 Key. RBAC 509 registrations.
22
SELECT
33
role0_key,
44
slot_no,

catalyst-gateway/bin/src/db/index/queries/purge/mod.rs

+11-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Queries for purging volatile data.
22
3+
pub(crate) mod chain_root_for_role0_key;
34
pub(crate) mod cip36_registration;
45
pub(crate) mod cip36_registration_for_vote_key;
56
pub(crate) mod cip36_registration_invalid;
@@ -129,18 +130,18 @@ pub(crate) struct PreparedQueries {
129130
delete_cip36_registration_for_vote_key: SizedBatch,
130131
/// RBAC 509 Registrations Primary Key Query.
131132
select_rbac509_registration: PreparedStatement,
132-
/// Chain Root for TX ID Primary Key Query..
133-
select_chain_root_for_txn_id: PreparedStatement,
134-
/// Chain Root for Role 0 Key Primary Key Query..
135-
select_chain_root_for_role0_key: PreparedStatement,
136-
/// Chain Root for Stake Address Primary Key Query..
137-
select_chain_root_for_stake_address: PreparedStatement,
138133
/// RBAC 509 Registrations Delete Query.
139134
delete_rbac509_registration: SizedBatch,
135+
/// Chain Root for TX ID Primary Key Query..
136+
select_chain_root_for_txn_id: PreparedStatement,
140137
/// Chain Root for TX ID Delete Query..
141138
delete_chain_root_for_txn_id: SizedBatch,
139+
/// Chain Root for Role 0 Key Primary Key Query..
140+
select_chain_root_for_role0_key: PreparedStatement,
142141
/// Chain Root for Role 0 Key Delete Query..
143142
delete_chain_root_for_role0_key: SizedBatch,
143+
/// Chain Root for Stake Address Primary Key Query..
144+
select_chain_root_for_stake_address: PreparedStatement,
144145
/// Chain Root for Stake Address Delete Query..
145146
delete_chain_root_for_stake_address: SizedBatch,
146147
}
@@ -185,6 +186,10 @@ impl PreparedQueries {
185186
rbac509_registration::PrimaryKeyQuery::prepare(&session).await?;
186187
let delete_rbac509_registration =
187188
rbac509_registration::DeleteQuery::prepare_batch(&session, cfg).await?;
189+
let select_chain_root_for_role0_key =
190+
chain_root_for_role0_key::PrimaryKeyQuery::prepare(&session).await?;
191+
let delete_chain_root_for_role0_key =
192+
chain_root_for_role0_key::DeleteQuery::prepare_batch(&session, cfg).await?;
188193

189194
todo!("WIP");
190195
}

catalyst-gateway/bin/src/db/index/schema/cql/chain_root_for_role0_key.cql

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
-- Index of Chain Root For Role0 Key. RBAC 509 registrations.
1+
-- Index of Chain Root For Role0 Key. RBAC 509 registrations.
22
CREATE TABLE IF NOT EXISTS chain_root_for_role0_key (
33
-- Primary Key Data
44
role0_key blob, -- 16 Bytes of Role0 Key.
55
slot_no varint, -- slot number when the key_was_registered.
66
txn smallint, -- Index of the TX which holds the registration data.
7+
8+
-- Non-primary Key Data
79
chain_root blob, -- 32 Bytes of Chain Root.
810

911
PRIMARY KEY (role0_key, slot_no, txn)

0 commit comments

Comments
 (0)