cat_gateway/db/index/block/
certs.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
//! Index certs found in a transaction.

use std::{fmt::Debug, sync::Arc};

use cardano_chain_follower::MultiEraBlock;
use pallas::ledger::primitives::{alonzo, conway};
use scylla::{frame::value::MaybeUnset, SerializeRow, Session};
use tracing::error;

use crate::{
    db::index::{
        queries::{FallibleQueryTasks, PreparedQueries, PreparedQuery, SizedBatch},
        session::CassandraSession,
    },
    service::utilities::convert::from_saturating,
    settings::cassandra_db,
};

/// Insert TXI Query and Parameters
#[derive(SerializeRow)]
pub(crate) struct StakeRegistrationInsertQuery {
    /// Stake key hash
    stake_hash: Vec<u8>,
    /// Slot Number the cert is in.
    slot_no: num_bigint::BigInt,
    /// Transaction Index.
    txn: i16,
    /// Full Stake Public Key (32 byte Ed25519 Public key, not hashed).
    stake_address: MaybeUnset<Vec<u8>>,
    /// Is the stake address a script or not.
    script: bool,
    /// Is the Certificate Registered?
    register: MaybeUnset<bool>,
    /// Is the Certificate Deregistered?
    deregister: MaybeUnset<bool>,
    /// Pool Delegation Address
    pool_delegation: MaybeUnset<Vec<u8>>,
}

impl Debug for StakeRegistrationInsertQuery {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
        let stake_address = match self.stake_address {
            MaybeUnset::Unset => "UNSET",
            MaybeUnset::Set(ref v) => &hex::encode(v),
        };
        let register = match self.register {
            MaybeUnset::Unset => "UNSET",
            MaybeUnset::Set(v) => &format!("{v:?}"),
        };
        let deregister = match self.deregister {
            MaybeUnset::Unset => "UNSET",
            MaybeUnset::Set(v) => &format!("{v:?}"),
        };
        let pool_delegation = match self.pool_delegation {
            MaybeUnset::Unset => "UNSET",
            MaybeUnset::Set(ref v) => &hex::encode(v),
        };

        f.debug_struct("StakeRegistrationInsertQuery")
            .field("stake_hash", &hex::encode(hex::encode(&self.stake_hash)))
            .field("slot_no", &self.slot_no)
            .field("txn", &self.txn)
            .field("stake_address", &stake_address)
            .field("script", &self.script)
            .field("register", &register)
            .field("deregister", &deregister)
            .field("pool_delegation", &pool_delegation)
            .finish()
    }
}

/// TXI by Txn hash Index
const INSERT_STAKE_REGISTRATION_QUERY: &str = include_str!("./cql/insert_stake_registration.cql");

impl StakeRegistrationInsertQuery {
    /// Create a new Insert Query.
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        stake_hash: Vec<u8>, slot_no: u64, txn: i16, stake_address: Vec<u8>, script: bool,
        register: bool, deregister: bool, pool_delegation: Option<Vec<u8>>,
    ) -> Self {
        StakeRegistrationInsertQuery {
            stake_hash,
            slot_no: slot_no.into(),
            txn,
            stake_address: if stake_address.is_empty() {
                MaybeUnset::Unset
            } else {
                MaybeUnset::Set(stake_address)
            },
            script,
            register: if register {
                MaybeUnset::Set(true)
            } else {
                MaybeUnset::Unset
            },
            deregister: if deregister {
                MaybeUnset::Set(true)
            } else {
                MaybeUnset::Unset
            },
            pool_delegation: if let Some(pool_delegation) = pool_delegation {
                MaybeUnset::Set(pool_delegation)
            } else {
                MaybeUnset::Unset
            },
        }
    }

    /// Prepare Batch of Insert TXI Index Data Queries
    pub(crate) async fn prepare_batch(
        session: &Arc<Session>, cfg: &cassandra_db::EnvVars,
    ) -> anyhow::Result<SizedBatch> {
        let insert_queries = PreparedQueries::prepare_batch(
            session.clone(),
            INSERT_STAKE_REGISTRATION_QUERY,
            cfg,
            scylla::statement::Consistency::Any,
            true,
            false,
        )
        .await;

        if let Err(ref error) = insert_queries {
            error!(error=%error,"Failed to prepare Insert Stake Registration Query.");
        };

        insert_queries
    }
}

/// Insert Cert Queries
pub(crate) struct CertInsertQuery {
    /// Stake Registration Data captured during indexing.
    stake_reg_data: Vec<StakeRegistrationInsertQuery>,
}

impl CertInsertQuery {
    /// Create new data set for Cert Insert Query Batch.
    pub(crate) fn new() -> Self {
        CertInsertQuery {
            stake_reg_data: Vec::new(),
        }
    }

    /// Prepare Batch of Insert TXI Index Data Queries
    pub(crate) async fn prepare_batch(
        session: &Arc<Session>, cfg: &cassandra_db::EnvVars,
    ) -> anyhow::Result<SizedBatch> {
        // Note: for now we have one query, but there are many certs, and later we may have more
        // to add here.
        StakeRegistrationInsertQuery::prepare_batch(session, cfg).await
    }

    /// Get the stake address for a hash, return an empty address if one can not be found.
    #[allow(clippy::too_many_arguments)]
    fn stake_address(
        &mut self, cred: &alonzo::StakeCredential, slot_no: u64, txn: i16, register: bool,
        deregister: bool, delegation: Option<Vec<u8>>, block: &MultiEraBlock,
    ) {
        let default_addr = Vec::new();
        let (key_hash, pubkey, script) = match cred {
            pallas::ledger::primitives::conway::StakeCredential::AddrKeyhash(cred) => {
                let addr = block
                    .witness_for_tx(cred, from_saturating(txn))
                    .unwrap_or(default_addr);
                // Note: it is totally possible for the Registration Certificate to not be
                // witnessed.
                (cred.to_vec(), addr.clone(), false)
            },
            pallas::ledger::primitives::conway::StakeCredential::Scripthash(script) => {
                (script.to_vec(), default_addr, true)
            },
        };

        if pubkey.is_empty() && !script && deregister {
            error!(
                "Stake Deregistration Certificate {:?} is NOT Witnessed.",
                key_hash
            );
        }

        if pubkey.is_empty() && !script && delegation.is_some() {
            error!(
                "Stake Delegation Certificate {:?} is NOT Witnessed.",
                key_hash
            );
        }

        // This may not be witnessed, its normal but disappointing.
        self.stake_reg_data.push(StakeRegistrationInsertQuery::new(
            key_hash, slot_no, txn, pubkey, script, register, deregister, delegation,
        ));
    }

    /// Index an Alonzo Era certificate into the database.
    fn index_alonzo_cert(
        &mut self, cert: &alonzo::Certificate, slot_no: u64, txn: i16, block: &MultiEraBlock,
    ) {
        #[allow(clippy::match_same_arms)]
        match cert {
            pallas::ledger::primitives::alonzo::Certificate::StakeRegistration(cred) => {
                // This may not be witnessed, its normal but disappointing.
                self.stake_address(cred, slot_no, txn, true, false, None, block);
            },
            pallas::ledger::primitives::alonzo::Certificate::StakeDeregistration(cred) => {
                self.stake_address(cred, slot_no, txn, false, true, None, block);
            },
            pallas::ledger::primitives::alonzo::Certificate::StakeDelegation(cred, pool) => {
                self.stake_address(cred, slot_no, txn, false, false, Some(pool.to_vec()), block);
            },
            pallas::ledger::primitives::alonzo::Certificate::PoolRegistration { .. } => {},
            pallas::ledger::primitives::alonzo::Certificate::PoolRetirement(..) => {},
            pallas::ledger::primitives::alonzo::Certificate::GenesisKeyDelegation(..) => {},
            pallas::ledger::primitives::alonzo::Certificate::MoveInstantaneousRewardsCert(_) => {},
        }
    }

    /// Index a certificate from a conway transaction.
    fn index_conway_cert(
        &mut self, cert: &conway::Certificate, slot_no: u64, txn: i16, block: &MultiEraBlock,
    ) {
        #[allow(clippy::match_same_arms)]
        match cert {
            pallas::ledger::primitives::conway::Certificate::StakeRegistration(cred) => {
                // This may not be witnessed, its normal but disappointing.
                self.stake_address(cred, slot_no, txn, true, false, None, block);
            },
            pallas::ledger::primitives::conway::Certificate::StakeDeregistration(cred) => {
                self.stake_address(cred, slot_no, txn, false, true, None, block);
            },
            pallas::ledger::primitives::conway::Certificate::StakeDelegation(cred, pool) => {
                self.stake_address(cred, slot_no, txn, false, false, Some(pool.to_vec()), block);
            },
            pallas::ledger::primitives::conway::Certificate::PoolRegistration { .. } => {},
            pallas::ledger::primitives::conway::Certificate::PoolRetirement(..) => {},
            pallas::ledger::primitives::conway::Certificate::Reg(..) => {},
            pallas::ledger::primitives::conway::Certificate::UnReg(..) => {},
            pallas::ledger::primitives::conway::Certificate::VoteDeleg(..) => {},
            pallas::ledger::primitives::conway::Certificate::StakeVoteDeleg(..) => {},
            pallas::ledger::primitives::conway::Certificate::StakeRegDeleg(..) => {},
            pallas::ledger::primitives::conway::Certificate::VoteRegDeleg(..) => {},
            pallas::ledger::primitives::conway::Certificate::StakeVoteRegDeleg(..) => {},
            pallas::ledger::primitives::conway::Certificate::AuthCommitteeHot(..) => {},
            pallas::ledger::primitives::conway::Certificate::ResignCommitteeCold(..) => {},
            pallas::ledger::primitives::conway::Certificate::RegDRepCert(..) => {},
            pallas::ledger::primitives::conway::Certificate::UnRegDRepCert(..) => {},
            pallas::ledger::primitives::conway::Certificate::UpdateDRepCert(..) => {},
        }
    }

    /// Index the certificates in a transaction.
    pub(crate) fn index(
        &mut self, txs: &pallas::ledger::traverse::MultiEraTx<'_>, slot_no: u64, txn: i16,
        block: &MultiEraBlock,
    ) {
        #[allow(clippy::match_same_arms)]
        txs.certs().iter().for_each(|cert| {
            match cert {
                pallas::ledger::traverse::MultiEraCert::NotApplicable => {},
                pallas::ledger::traverse::MultiEraCert::AlonzoCompatible(cert) => {
                    self.index_alonzo_cert(cert, slot_no, txn, block);
                },
                pallas::ledger::traverse::MultiEraCert::Conway(cert) => {
                    self.index_conway_cert(cert, slot_no, txn, block);
                },
                _ => {},
            }
        });
    }

    /// Execute the Certificate Indexing Queries.
    ///
    /// Consumes the `self` and returns a vector of futures.
    pub(crate) fn execute(self, session: &Arc<CassandraSession>) -> FallibleQueryTasks {
        let mut query_handles: FallibleQueryTasks = Vec::new();

        let inner_session = session.clone();

        query_handles.push(tokio::spawn(async move {
            inner_session
                .execute_batch(
                    PreparedQuery::StakeRegistrationInsertQuery,
                    self.stake_reg_data,
                )
                .await
        }));

        query_handles
    }
}