cat_gateway/db/index/queries/registrations/
get_invalid.rs

1//! Get invalid registrations for stake addr after given slot no.
2
3use std::sync::Arc;
4
5use scylla::{
6    prepared_statement::PreparedStatement, transport::iterator::TypedRowStream, DeserializeRow,
7    SerializeRow, Session,
8};
9use tracing::error;
10
11use crate::{
12    db::{
13        index::{
14            queries::{PreparedQueries, PreparedSelectQuery},
15            session::CassandraSession,
16        },
17        types::DbSlot,
18    },
19    service::common::types::cardano::slot_no::SlotNo,
20};
21
22/// Get invalid registrations from stake addr query.
23const GET_INVALID_REGISTRATIONS_FROM_STAKE_ADDR_QUERY: &str =
24    include_str!("../cql/get_invalid_registration_w_stake_addr.cql");
25
26/// Get registration
27#[derive(SerializeRow)]
28pub(crate) struct GetInvalidRegistrationParams {
29    /// Stake address.
30    pub stake_public_key: Vec<u8>,
31    /// Block Slot Number when spend occurred.
32    slot_no: DbSlot,
33}
34
35impl GetInvalidRegistrationParams {
36    /// Create a new instance of [`GetInvalidRegistrationParams`]
37    pub(crate) fn new(stake_public_key: Vec<u8>, slot_no: SlotNo) -> GetInvalidRegistrationParams {
38        Self {
39            stake_public_key,
40            slot_no: u64::from(slot_no).into(),
41        }
42    }
43}
44
45/// Get invalid registrations given stake address.
46#[derive(DeserializeRow)]
47pub(crate) struct GetInvalidRegistrationQuery {
48    /// Error report
49    pub problem_report: String,
50    /// Full Stake Address (not hashed, 32 byte ED25519 Public key).
51    pub stake_public_key: Vec<u8>,
52    /// Voting Public Key
53    pub vote_key: Vec<u8>,
54    /// Full Payment Address (not hashed, 32 byte ED25519 Public key).
55    pub payment_address: Vec<u8>,
56    /// Is the stake address a script or not.
57    pub is_payable: bool,
58    /// Is the Registration CIP36 format, or CIP15
59    pub cip36: bool,
60}
61
62impl GetInvalidRegistrationQuery {
63    /// Prepares a get invalid registration query.
64    pub(crate) async fn prepare(session: Arc<Session>) -> anyhow::Result<PreparedStatement> {
65        PreparedQueries::prepare(
66            session,
67            GET_INVALID_REGISTRATIONS_FROM_STAKE_ADDR_QUERY,
68            scylla::statement::Consistency::All,
69            true,
70        )
71        .await
72        .inspect_err(|error| error!(error=%error, "Failed to prepare get invalid registration from stake address query."))
73        .map_err(|error| {
74            anyhow::anyhow!("{error}\n--\n{GET_INVALID_REGISTRATIONS_FROM_STAKE_ADDR_QUERY}")
75        })
76    }
77
78    /// Executes get invalid registration info for given stake addr query.
79    pub(crate) async fn execute(
80        session: &CassandraSession, params: GetInvalidRegistrationParams,
81    ) -> anyhow::Result<TypedRowStream<GetInvalidRegistrationQuery>> {
82        let iter = session
83            .execute_iter(
84                PreparedSelectQuery::InvalidRegistrationsFromStakeAddr,
85                params,
86            )
87            .await?
88            .rows_stream::<GetInvalidRegistrationQuery>()?;
89
90        Ok(iter)
91    }
92}