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