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

1//! Insert invalid 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, problem_report::ProblemReport, 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/// A RBAC registration indexing query.
19const QUERY: &str = include_str!("cql/insert_rbac509_invalid.cql");
20
21/// Insert an invalid RBAC registration query parameters.
22#[derive(SerializeRow)]
23pub(crate) struct Params {
24    /// A Catalyst short identifier.
25    catalyst_id: DbCatalystId,
26    /// A transaction hash of this registration.
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    /// A Hash of the previous transaction.
33    prv_txn_id: MaybeUnset<DbTransactionId>,
34    /// A registration purpose.
35    purpose: MaybeUnset<DbUuidV4>,
36    /// JSON encoded `ProblemReport`.
37    problem_report: String,
38}
39
40impl Params {
41    /// Create a new record for this transaction.
42    pub(crate) fn new(
43        catalyst_id: IdUri, txn_id: TransactionId, slot_no: Slot, txn_index: TxnIndex,
44        purpose: Option<UuidV4>, prv_txn_id: Option<TransactionId>, report: &ProblemReport,
45    ) -> Self {
46        let purpose = purpose.map_or(MaybeUnset::Unset, |v| MaybeUnset::Set(v.into()));
47        let prv_txn_id = prv_txn_id.map_or(MaybeUnset::Unset, |v| MaybeUnset::Set(v.into()));
48        let problem_report = serde_json::to_string(&report).unwrap_or_else(|e| {
49            error!("Failed to serialize problem report: {e:?}. Report = {report:?}");
50            String::new()
51        });
52
53        Self {
54            catalyst_id: catalyst_id.into(),
55            txn_id: txn_id.into(),
56            purpose,
57            slot_no: slot_no.into(),
58            txn_index: txn_index.into(),
59            prv_txn_id,
60            problem_report,
61        }
62    }
63
64    /// Prepare Batch of RBAC Registration Index Data Queries
65    pub(crate) async fn prepare_batch(
66        session: &Arc<Session>, cfg: &EnvVars,
67    ) -> anyhow::Result<SizedBatch> {
68        PreparedQueries::prepare_batch(
69            session.clone(),
70            QUERY,
71            cfg,
72            scylla::statement::Consistency::Any,
73            true,
74            false,
75        )
76        .await
77        .inspect_err(
78            |error| error!(error=%error,"Failed to prepare Insert Invalid RBAC 509 Registration Query."),
79        )
80        .map_err(|error| anyhow::anyhow!("{error}\n--\n{QUERY}"))
81    }
82}
83
84impl Debug for Params {
85    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
86        let prv_txn_id = match self.prv_txn_id {
87            MaybeUnset::Unset => "UNSET".to_owned(),
88            MaybeUnset::Set(v) => format!("{v}"),
89        };
90        let purpose = match self.purpose {
91            MaybeUnset::Unset => "UNSET".to_owned(),
92            MaybeUnset::Set(v) => format!("{}", UuidV4::from(v)),
93        };
94        f.debug_struct("Params")
95            .field("catalyst_id", &self.catalyst_id)
96            .field("txn_id", &self.txn_id)
97            .field("slot_no", &self.slot_no)
98            .field("txn_index", &self.txn_index)
99            .field("prv_txn_id", &prv_txn_id)
100            .field("purpose", &purpose)
101            .field("problem_report", &self.problem_report)
102            .finish()
103    }
104}