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_blockchain_types::{Slot, TransactionId, TxnIndex};
6use catalyst_types::{id_uri::IdUri, uuid::UuidV4};
7use scylla::{frame::value::MaybeUnset, SerializeRow, Session};
8use tracing::error;
9
10use crate::{
11    db::{
12        index::queries::{PreparedQueries, SizedBatch},
13        types::{DbCatalystId, DbSlot, DbTransactionId, DbTxnIndex, DbUuidV4},
14    },
15    settings::cassandra_db::EnvVars,
16};
17
18/// RBAC Registration Indexing query
19const QUERY: &str = include_str!("cql/insert_rbac509.cql");
20
21/// Insert RBAC Registration Query Parameters
22#[derive(SerializeRow)]
23pub(crate) struct Params {
24    /// A Catalyst short identifier.
25    catalyst_id: DbCatalystId,
26    /// A transaction hash
27    txn_id: DbTransactionId,
28    /// A block slot number.
29    slot_no: DbSlot,
30    /// A transaction offset inside the block.
31    txn_index: DbTxnIndex,
32    /// Hash of Previous Transaction. Is `None` for the first registration. 32 Bytes.
33    prv_txn_id: MaybeUnset<DbTransactionId>,
34    /// Purpose.`UUIDv4`. 16 bytes.
35    purpose: DbUuidV4,
36}
37
38impl Debug for Params {
39    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
40        let prv_txn_id = match self.prv_txn_id {
41            MaybeUnset::Unset => "UNSET".to_owned(),
42            MaybeUnset::Set(ref v) => format!("{v:?}"),
43        };
44        f.debug_struct("Params")
45            .field("catalyst_id", &self.catalyst_id)
46            .field("txn_id", &self.txn_id)
47            .field("slot_no", &self.slot_no)
48            .field("txn_index", &self.txn_index)
49            .field("prv_txn_id", &prv_txn_id)
50            .field("purpose", &self.purpose)
51            .finish()
52    }
53}
54
55impl Params {
56    /// Create a new record for this transaction.
57    pub(crate) fn new(
58        catalyst_id: IdUri, txn_id: TransactionId, slot_no: Slot, txn_index: TxnIndex,
59        purpose: UuidV4, prv_txn_id: Option<TransactionId>,
60    ) -> Self {
61        let prv_txn_id = prv_txn_id.map_or(MaybeUnset::Unset, |v| MaybeUnset::Set(v.into()));
62
63        Self {
64            catalyst_id: catalyst_id.into(),
65            txn_id: txn_id.into(),
66            purpose: purpose.into(),
67            slot_no: slot_no.into(),
68            txn_index: txn_index.into(),
69            prv_txn_id,
70        }
71    }
72
73    /// Prepare Batch of RBAC Registration Index Data Queries
74    pub(crate) async fn prepare_batch(
75        session: &Arc<Session>, cfg: &EnvVars,
76    ) -> anyhow::Result<SizedBatch> {
77        PreparedQueries::prepare_batch(
78            session.clone(),
79            QUERY,
80            cfg,
81            scylla::statement::Consistency::Any,
82            true,
83            false,
84        )
85        .await
86        .inspect_err(
87            |error| error!(error=%error,"Failed to prepare Insert RBAC 509 Registration Query."),
88        )
89        .map_err(|error| anyhow::anyhow!("{error}\n--\n{QUERY}"))
90    }
91}