cat_gateway/db/index/block/rbac509/
insert_rbac509.rs

1//! Insert RBAC 509 Registration Query.
2
3use std::{fmt::Debug, sync::Arc};
4
5use cardano_chain_follower::{Slot, TxnIndex, hashes::TransactionId};
6use catalyst_types::{catalyst_id::CatalystId, uuid::UuidV4};
7use scylla::{SerializeRow, client::session::Session, value::MaybeUnset};
8use tracing::error;
9
10use crate::{
11    db::{
12        index::{
13            queries::{FallibleQueryResults, PreparedQueries, PreparedQuery, SizedBatch},
14            session::CassandraSession,
15        },
16        types::{DbCatalystId, DbSlot, DbTransactionId, DbTxnIndex, DbUuidV4},
17    },
18    settings::cassandra_db::EnvVars,
19};
20
21/// RBAC Registration Indexing query
22const QUERY: &str = include_str!("cql/insert_rbac509.cql");
23
24/// Insert RBAC Registration Query Parameters
25#[derive(SerializeRow)]
26pub(crate) struct Params {
27    /// A Catalyst short identifier.
28    catalyst_id: DbCatalystId,
29    /// A block slot number.
30    slot_no: DbSlot,
31    /// A transaction offset inside the block.
32    txn_index: DbTxnIndex,
33    /// A transaction hash
34    txn_id: DbTransactionId,
35    /// Hash of Previous Transaction. Is `None` for the first registration. 32 Bytes.
36    prv_txn_id: MaybeUnset<DbTransactionId>,
37    /// A registration purpose.
38    ///
39    /// The value of purpose is `None` if the chain is modified by the registration
40    /// belonging to another chain (a stake address has been removed).
41    purpose: MaybeUnset<DbUuidV4>,
42}
43
44impl Debug for Params {
45    fn fmt(
46        &self,
47        f: &mut std::fmt::Formatter<'_>,
48    ) -> std::fmt::Result {
49        let prv_txn_id = match self.prv_txn_id {
50            MaybeUnset::Unset => "UNSET".to_owned(),
51            MaybeUnset::Set(ref v) => format!("{v:?}"),
52        };
53        f.debug_struct("Params")
54            .field("catalyst_id", &self.catalyst_id)
55            .field("txn_id", &self.txn_id)
56            .field("slot_no", &self.slot_no)
57            .field("txn_index", &self.txn_index)
58            .field("prv_txn_id", &prv_txn_id)
59            .field("purpose", &self.purpose)
60            .finish()
61    }
62}
63
64impl Params {
65    /// Create a new record for this transaction.
66    pub(crate) fn new(
67        catalyst_id: CatalystId,
68        txn_id: TransactionId,
69        slot_no: Slot,
70        txn_index: TxnIndex,
71        prv_txn_id: Option<TransactionId>,
72        purpose: Option<UuidV4>,
73    ) -> Self {
74        let prv_txn_id = prv_txn_id.map_or(MaybeUnset::Unset, |v| MaybeUnset::Set(v.into()));
75        let purpose = purpose.map_or(MaybeUnset::Unset, |v| MaybeUnset::Set(v.into()));
76
77        Self {
78            catalyst_id: catalyst_id.into(),
79            txn_id: txn_id.into(),
80            slot_no: slot_no.into(),
81            txn_index: txn_index.into(),
82            prv_txn_id,
83            purpose,
84        }
85    }
86
87    /// Executes prepared queries as a batch.
88    pub(crate) async fn execute_batch(
89        session: &Arc<CassandraSession>,
90        queries: Vec<Self>,
91    ) -> FallibleQueryResults {
92        session
93            .execute_batch(PreparedQuery::Rbac509InsertQuery, queries)
94            .await
95    }
96
97    /// Prepare Batch of RBAC Registration Index Data Queries
98    pub(crate) async fn prepare_batch(
99        session: &Arc<Session>,
100        cfg: &EnvVars,
101    ) -> anyhow::Result<SizedBatch> {
102        PreparedQueries::prepare_batch(
103            session.clone(),
104            QUERY,
105            cfg,
106            scylla::statement::Consistency::Any,
107            true,
108            false,
109        )
110        .await
111        .inspect_err(
112            |error| error!(error=%error,"Failed to prepare Insert RBAC 509 Registration Query."),
113        )
114        .map_err(|error| anyhow::anyhow!("{error}\n--\n{QUERY}"))
115    }
116}