Skip to content

Commit f7b8fa9

Browse files
committed
wip(cat-gateway): implement query trait for batched queries
1 parent 95d62fe commit f7b8fa9

17 files changed

+124
-229
lines changed

catalyst-gateway/bin/src/db/index/block/certs.rs

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,12 @@ use tracing::error;
1111
use crate::{
1212
db::{
1313
index::{
14-
queries::{
15-
FallibleQueryTasks, PreparedQueries, PreparedQuery, Query, QueryKind, SizedBatch,
16-
},
14+
queries::{FallibleQueryTasks, PreparedQueries, PreparedQuery, SizedBatch},
1715
session::CassandraSession,
1816
},
1917
types::{DbPublicKey, DbSlot, DbStakeAddress, DbTxnIndex},
2018
},
19+
impl_query_batch,
2120
settings::cassandra_db,
2221
};
2322

@@ -44,17 +43,6 @@ pub(crate) struct StakeRegistrationInsertQuery {
4443
pool_delegation: MaybeUnset<Vec<u8>>,
4544
}
4645

47-
impl Query for StakeRegistrationInsertQuery {
48-
/// Prepare Batch of Insert TXI Index Data Queries
49-
async fn prepare_query(
50-
session: &Arc<Session>, cfg: &cassandra_db::EnvVars,
51-
) -> anyhow::Result<QueryKind> {
52-
StakeRegistrationInsertQuery::prepare_batch(session, cfg)
53-
.await
54-
.map(QueryKind::Batch)
55-
}
56-
}
57-
5846
impl fmt::Debug for StakeRegistrationInsertQuery {
5947
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
6048
let stake_public_key = hex::encode(self.stake_public_key.as_ref());
@@ -92,11 +80,10 @@ impl fmt::Debug for StakeRegistrationInsertQuery {
9280
/// Insert stake registration
9381
const INSERT_STAKE_REGISTRATION_QUERY: &str = include_str!("./cql/insert_stake_registration.cql");
9482

95-
impl fmt::Display for StakeRegistrationInsertQuery {
96-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
97-
write!(f, "{INSERT_STAKE_REGISTRATION_QUERY}")
98-
}
99-
}
83+
impl_query_batch!(
84+
StakeRegistrationInsertQuery,
85+
INSERT_STAKE_REGISTRATION_QUERY
86+
);
10087

10188
impl StakeRegistrationInsertQuery {
10289
/// Create a new Insert Query.

catalyst-gateway/bin/src/db/index/block/cip36/insert_cip36.rs

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ use tracing::error;
88

99
use crate::{
1010
db::{
11-
index::queries::{PreparedQueries, Query, QueryKind, SizedBatch},
11+
index::queries::{PreparedQueries, SizedBatch},
1212
types::{DbSlot, DbTxnIndex},
1313
},
14+
impl_query_batch,
1415
settings::cassandra_db,
1516
};
1617

@@ -40,16 +41,7 @@ pub(crate) struct Cip36Insert {
4041
cip36: bool,
4142
}
4243

43-
impl Query for Cip36Insert {
44-
/// Prepare Batch of Insert TXI Index Data Queries
45-
async fn prepare_query(
46-
session: &Arc<Session>, cfg: &cassandra_db::EnvVars,
47-
) -> anyhow::Result<QueryKind> {
48-
Self::prepare_batch(session, cfg)
49-
.await
50-
.map(QueryKind::Batch)
51-
}
52-
}
44+
impl_query_batch!(Cip36Insert, INSERT_CIP36_REGISTRATION_QUERY);
5345

5446
impl fmt::Debug for Cip36Insert {
5547
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
@@ -71,12 +63,6 @@ impl fmt::Debug for Cip36Insert {
7163
}
7264
}
7365

74-
impl fmt::Display for Cip36Insert {
75-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
76-
write!(f, "{INSERT_CIP36_REGISTRATION_QUERY}")
77-
}
78-
}
79-
8066
impl Cip36Insert {
8167
/// Create a new Insert Query.
8268
pub fn new(vote_key: &VotingPubKey, slot_no: Slot, txn_index: TxnIndex, cip36: &Cip36) -> Self {

catalyst-gateway/bin/src/db/index/block/cip36/insert_cip36_for_vote_key.rs

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
//! Insert CIP36 Registration Query
22
3-
use std::{fmt, sync::Arc};
3+
use std::sync::Arc;
44

55
use cardano_blockchain_types::{Cip36, Slot, TxnIndex, VotingPubKey};
66
use scylla::{SerializeRow, Session};
77
use tracing::error;
88

99
use crate::{
1010
db::{
11-
index::queries::{PreparedQueries, Query, QueryKind, SizedBatch},
11+
index::queries::{PreparedQueries, SizedBatch},
1212
types::{DbSlot, DbTxnIndex},
1313
},
14+
impl_query_batch,
1415
settings::cassandra_db,
1516
};
1617

@@ -33,22 +34,10 @@ pub(crate) struct Cip36ForVoteKeyInsert {
3334
valid: bool,
3435
}
3536

36-
impl Query for Cip36ForVoteKeyInsert {
37-
/// Prepare Batch of Insert TXI Index Data Queries
38-
async fn prepare_query(
39-
session: &Arc<Session>, cfg: &cassandra_db::EnvVars,
40-
) -> anyhow::Result<QueryKind> {
41-
Self::prepare_batch(session, cfg)
42-
.await
43-
.map(QueryKind::Batch)
44-
}
45-
}
46-
47-
impl fmt::Display for Cip36ForVoteKeyInsert {
48-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
49-
write!(f, "{INSERT_CIP36_REGISTRATION_FOR_VOTE_KEY_QUERY}")
50-
}
51-
}
37+
impl_query_batch!(
38+
Cip36ForVoteKeyInsert,
39+
INSERT_CIP36_REGISTRATION_FOR_VOTE_KEY_QUERY
40+
);
5241

5342
impl Cip36ForVoteKeyInsert {
5443
/// Create a new Insert Query.

catalyst-gateway/bin/src/db/index/block/cip36/insert_cip36_invalid.rs

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ use tracing::error;
99

1010
use crate::{
1111
db::{
12-
index::queries::{PreparedQueries, Query, QueryKind, SizedBatch},
12+
index::queries::{PreparedQueries, SizedBatch},
1313
types::{DbSlot, DbTxnIndex},
1414
},
15+
impl_query_batch,
1516
settings::cassandra_db,
1617
};
1718

@@ -46,16 +47,7 @@ pub(crate) struct Cip36InvalidInsert {
4647
problem_report: String,
4748
}
4849

49-
impl Query for Cip36InvalidInsert {
50-
/// Prepare Batch of Insert TXI Index Data Queries
51-
async fn prepare_query(
52-
session: &Arc<Session>, cfg: &cassandra_db::EnvVars,
53-
) -> anyhow::Result<QueryKind> {
54-
Self::prepare_batch(session, cfg)
55-
.await
56-
.map(QueryKind::Batch)
57-
}
58-
}
50+
impl_query_batch!(Cip36InvalidInsert, INSERT_CIP36_REGISTRATION_INVALID_QUERY);
5951

6052
impl fmt::Debug for Cip36InvalidInsert {
6153
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
@@ -79,12 +71,6 @@ impl fmt::Debug for Cip36InvalidInsert {
7971
}
8072
}
8173

82-
impl fmt::Display for Cip36InvalidInsert {
83-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
84-
write!(f, "{INSERT_CIP36_REGISTRATION_INVALID_QUERY}")
85-
}
86-
}
87-
8874
impl Cip36InvalidInsert {
8975
/// Create a new Insert Query.
9076
pub fn new(

catalyst-gateway/bin/src/db/index/block/rbac509/insert_catalyst_id_for_stake_address.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use crate::{
1212
index::queries::{PreparedQueries, SizedBatch},
1313
types::{DbCatalystId, DbSlot, DbStakeAddress},
1414
},
15+
impl_query_batch,
1516
settings::cassandra_db::EnvVars,
1617
};
1718

@@ -20,7 +21,7 @@ const QUERY: &str = include_str!("cql/insert_catalyst_id_for_stake_address.cql")
2021

2122
/// Insert Catalyst ID For Stake Address Query Parameters
2223
#[derive(SerializeRow)]
23-
pub(crate) struct Params {
24+
pub(crate) struct CatalystIdForStakeAddressInsert {
2425
/// A stake address.
2526
stake_address: DbStakeAddress,
2627
/// A Catalyst short identifier.
@@ -29,20 +30,22 @@ pub(crate) struct Params {
2930
slot_no: DbSlot,
3031
}
3132

32-
impl Debug for Params {
33+
impl_query_batch!(CatalystIdForStakeAddressInsert, QUERY);
34+
35+
impl Debug for CatalystIdForStakeAddressInsert {
3336
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
34-
f.debug_struct("Params")
37+
f.debug_struct("CatalystIdForStakeAddressInsert")
3538
.field("stake_address", &self.stake_address)
3639
.field("catalyst_id", &self.catalyst_id)
3740
.field("slot_no", &self.slot_no)
3841
.finish()
3942
}
4043
}
4144

42-
impl Params {
45+
impl CatalystIdForStakeAddressInsert {
4346
/// Create a new record for this transaction.
4447
pub(crate) fn new(stake_address: StakeAddress, slot_no: Slot, catalyst_id: CatalystId) -> Self {
45-
Params {
48+
CatalystIdForStakeAddressInsert {
4649
stake_address: stake_address.into(),
4750
catalyst_id: catalyst_id.into(),
4851
slot_no: slot_no.into(),

catalyst-gateway/bin/src/db/index/block/rbac509/insert_catalyst_id_for_txn_id.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use crate::{
1212
index::queries::{PreparedQueries, SizedBatch},
1313
types::{DbCatalystId, DbTransactionId},
1414
},
15+
impl_query_batch,
1516
settings::cassandra_db::EnvVars,
1617
};
1718

@@ -20,26 +21,28 @@ const QUERY: &str = include_str!("cql/insert_catalyst_id_for_txn_id.cql");
2021

2122
/// Insert Catalyst ID For Transaction ID Query Parameters
2223
#[derive(SerializeRow)]
23-
pub(crate) struct Params {
24+
pub(crate) struct CatalystIdForTxnIdInsert {
2425
/// A transaction hash.
2526
txn_id: DbTransactionId,
2627
/// A Catalyst short identifier.
2728
catalyst_id: DbCatalystId,
2829
}
2930

30-
impl Debug for Params {
31+
impl_query_batch!(CatalystIdForTxnIdInsert, QUERY);
32+
33+
impl Debug for CatalystIdForTxnIdInsert {
3134
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
32-
f.debug_struct("Params")
35+
f.debug_struct("CatalystIdForTxnIdInsert")
3336
.field("txn_id", &self.txn_id)
3437
.field("catalyst_id", &self.catalyst_id)
3538
.finish()
3639
}
3740
}
3841

39-
impl Params {
42+
impl CatalystIdForTxnIdInsert {
4043
/// Creates a new record for this transaction.
4144
pub(crate) fn new(catalyst_id: CatalystId, txn_id: TransactionId) -> Self {
42-
Params {
45+
CatalystIdForTxnIdInsert {
4346
txn_id: txn_id.into(),
4447
catalyst_id: catalyst_id.into(),
4548
}

catalyst-gateway/bin/src/db/index/block/rbac509/insert_rbac509.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Insert RBAC 509 Registration Query.
22
3-
use std::{fmt::Debug, sync::Arc};
3+
use std::{fmt, sync::Arc};
44

55
use cardano_blockchain_types::{Slot, TransactionId, TxnIndex};
66
use catalyst_types::{catalyst_id::CatalystId, uuid::UuidV4};
@@ -12,6 +12,7 @@ use crate::{
1212
index::queries::{PreparedQueries, SizedBatch},
1313
types::{DbCatalystId, DbSlot, DbTransactionId, DbTxnIndex, DbUuidV4},
1414
},
15+
impl_query_batch,
1516
settings::cassandra_db::EnvVars,
1617
};
1718

@@ -20,7 +21,7 @@ const QUERY: &str = include_str!("cql/insert_rbac509.cql");
2021

2122
/// Insert RBAC Registration Query Parameters
2223
#[derive(SerializeRow)]
23-
pub(crate) struct Params {
24+
pub(crate) struct Rbac509Insert {
2425
/// A Catalyst short identifier.
2526
catalyst_id: DbCatalystId,
2627
/// A transaction hash
@@ -35,13 +36,15 @@ pub(crate) struct Params {
3536
purpose: DbUuidV4,
3637
}
3738

38-
impl Debug for Params {
39-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
39+
impl_query_batch!(Rbac509Insert, QUERY);
40+
41+
impl fmt::Debug for Rbac509Insert {
42+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4043
let prv_txn_id = match self.prv_txn_id {
4144
MaybeUnset::Unset => "UNSET".to_owned(),
4245
MaybeUnset::Set(ref v) => format!("{v:?}"),
4346
};
44-
f.debug_struct("Params")
47+
f.debug_struct("Rbac509Insert")
4548
.field("catalyst_id", &self.catalyst_id)
4649
.field("txn_id", &self.txn_id)
4750
.field("slot_no", &self.slot_no)
@@ -52,7 +55,7 @@ impl Debug for Params {
5255
}
5356
}
5457

55-
impl Params {
58+
impl Rbac509Insert {
5659
/// Create a new record for this transaction.
5760
pub(crate) fn new(
5861
catalyst_id: CatalystId, txn_id: TransactionId, slot_no: Slot, txn_index: TxnIndex,

catalyst-gateway/bin/src/db/index/block/rbac509/insert_rbac509_invalid.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Insert invalid RBAC 509 Registration Query.
22
3-
use std::{fmt::Debug, sync::Arc};
3+
use std::{fmt, sync::Arc};
44

55
use cardano_blockchain_types::{Slot, TransactionId, TxnIndex};
66
use catalyst_types::{catalyst_id::CatalystId, problem_report::ProblemReport, uuid::UuidV4};
@@ -12,6 +12,7 @@ use crate::{
1212
index::queries::{PreparedQueries, SizedBatch},
1313
types::{DbCatalystId, DbSlot, DbTransactionId, DbTxnIndex, DbUuidV4},
1414
},
15+
impl_query_batch,
1516
settings::cassandra_db::EnvVars,
1617
};
1718

@@ -20,7 +21,7 @@ const QUERY: &str = include_str!("cql/insert_rbac509_invalid.cql");
2021

2122
/// Insert an invalid RBAC registration query parameters.
2223
#[derive(SerializeRow)]
23-
pub(crate) struct Params {
24+
pub(crate) struct Rbac509InvalidInsert {
2425
/// A Catalyst short identifier.
2526
catalyst_id: DbCatalystId,
2627
/// A transaction hash of this registration.
@@ -37,7 +38,9 @@ pub(crate) struct Params {
3738
problem_report: String,
3839
}
3940

40-
impl Params {
41+
impl_query_batch!(Rbac509InvalidInsert, QUERY);
42+
43+
impl Rbac509InvalidInsert {
4144
/// Create a new record for this transaction.
4245
pub(crate) fn new(
4346
catalyst_id: CatalystId, txn_id: TransactionId, slot_no: Slot, txn_index: TxnIndex,
@@ -81,8 +84,8 @@ impl Params {
8184
}
8285
}
8386

84-
impl Debug for Params {
85-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
87+
impl fmt::Debug for Rbac509InvalidInsert {
88+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
8689
let prv_txn_id = match self.prv_txn_id {
8790
MaybeUnset::Unset => "UNSET".to_owned(),
8891
MaybeUnset::Set(v) => format!("{v}"),
@@ -91,7 +94,7 @@ impl Debug for Params {
9194
MaybeUnset::Unset => "UNSET".to_owned(),
9295
MaybeUnset::Set(v) => format!("{}", UuidV4::from(v)),
9396
};
94-
f.debug_struct("Params")
97+
f.debug_struct("Rbac509InvalidInsert")
9598
.field("catalyst_id", &self.catalyst_id)
9699
.field("txn_id", &self.txn_id)
97100
.field("slot_no", &self.slot_no)

0 commit comments

Comments
 (0)