cat_gateway/db/index/queries/rbac/
get_registrations.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//! Get registrations by chain root.
use std::sync::Arc;

use scylla::{
    prepared_statement::PreparedStatement, transport::iterator::TypedRowStream, DeserializeRow,
    SerializeRow, Session,
};
use tracing::error;

use crate::db::index::{
    queries::{PreparedQueries, PreparedSelectQuery},
    session::CassandraSession,
};

/// Get get chain root by stake address query string.
const GET_REGISTRATIONS_BY_CHAIN_ROOT_CQL: &str =
    include_str!("../cql/get_registrations_by_chain_root.cql");

/// Get chain root by stake address query params.
#[derive(SerializeRow)]
pub(crate) struct GetRegistrationsByChainRootQueryParams {
    /// Chain root to get registrations for.
    pub(crate) chain_root: Vec<u8>,
}

/// Get chain root by stake address query.
#[derive(DeserializeRow)]
pub(crate) struct GetRegistrationsByChainRootQuery {
    /// Registration transaction id.
    pub(crate) transaction_id: Vec<u8>,
}

impl GetRegistrationsByChainRootQuery {
    /// Prepares a get registrations by chain root query.
    pub(crate) async fn prepare(session: Arc<Session>) -> anyhow::Result<PreparedStatement> {
        let get_registrations_by_chain_root_query = PreparedQueries::prepare(
            session,
            GET_REGISTRATIONS_BY_CHAIN_ROOT_CQL,
            scylla::statement::Consistency::All,
            true,
        )
        .await;

        if let Err(ref error) = get_registrations_by_chain_root_query {
            error!(error=%error, "Failed to prepare get registrations by chain root query");
        };

        get_registrations_by_chain_root_query
    }

    /// Executes a get registrations by chain root query.
    pub(crate) async fn execute(
        session: &CassandraSession, params: GetRegistrationsByChainRootQueryParams,
    ) -> anyhow::Result<TypedRowStream<GetRegistrationsByChainRootQuery>> {
        let iter = session
            .execute_iter(PreparedSelectQuery::RegistrationsByChainRoot, params)
            .await?
            .rows_stream::<GetRegistrationsByChainRootQuery>()?;

        Ok(iter)
    }
}