Skip to content

Commit 2a0f6ac

Browse files
fix(cat-gateway): Additionally query a Catalyst ID in the persistent dabase when indexing volatile RBAC registrations (#2231)
* Additionally query a Catalyst ID in the persistent dabase when indexing volatile RBAC registrations * Update catalyst-gateway/bin/src/db/index/block/rbac509/mod.rs Co-authored-by: bkioshn <[email protected]> * Update catalyst-gateway/bin/src/db/index/block/mod.rs Co-authored-by: bkioshn <[email protected]> * fix --------- Co-authored-by: bkioshn <[email protected]>
1 parent 927f42b commit 2a0f6ac

File tree

2 files changed

+57
-21
lines changed

2 files changed

+57
-21
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use super::{queries::FallibleQueryTasks, session::CassandraSession};
2323
pub(crate) async fn index_block(block: &MultiEraBlock) -> anyhow::Result<()> {
2424
// Get the session. This should never fail.
2525
let Some(session) = CassandraSession::get(block.is_immutable()) else {
26-
anyhow::bail!("Failed to get Index DB Session. Can not index block.");
26+
anyhow::bail!("Failed to get Index DB Session. Cannot index block.");
2727
};
2828

2929
let mut cert_index = CertInsertQuery::new();

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

Lines changed: 56 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ pub(crate) mod insert_rbac509_invalid;
77

88
use std::sync::Arc;
99

10+
use anyhow::{bail, Context};
1011
use cardano_blockchain_types::{
1112
Cip0134Uri, MultiEraBlock, Slot, StakeAddress, TransactionId, TxnIndex,
1213
};
@@ -99,9 +100,21 @@ impl Rbac509InsertQuery {
99100
);
100101
}
101102

102-
let Some(catalyst_id) = catalyst_id(session, &cip509, txn_hash, slot, index).await else {
103-
error!("Unable to determine Catalyst id for registration: slot = {slot:?}, index = {index:?}, txn_hash = {txn_hash:?}");
104-
return;
103+
let catalyst_id = match catalyst_id(
104+
session,
105+
&cip509,
106+
txn_hash,
107+
slot,
108+
index,
109+
block.is_immutable(),
110+
)
111+
.await
112+
{
113+
Ok(v) => v,
114+
Err(e) => {
115+
error!("Unable to determine Catalyst id for registration: slot = {slot:?}, index = {index:?}, txn_hash = {txn_hash:?}: {e:?}");
116+
return;
117+
},
105118
};
106119

107120
let previous_transaction = cip509.previous_transaction();
@@ -199,29 +212,52 @@ impl Rbac509InsertQuery {
199212

200213
/// Returns a Catalyst ID of the given registration.
201214
async fn catalyst_id(
202-
session: &Arc<CassandraSession>, cip509: &Cip509, txn_hash: TransactionId, slot: Slot,
203-
index: TxnIndex,
204-
) -> Option<IdUri> {
205-
use crate::db::index::queries::rbac::get_catalyst_id_from_transaction_id::{
206-
cache_for_transaction_id, Query,
207-
};
215+
session: &Arc<CassandraSession>, cip509: &Cip509, txn_id: TransactionId, slot: Slot,
216+
index: TxnIndex, is_immutable: bool,
217+
) -> anyhow::Result<IdUri> {
218+
use crate::db::index::queries::rbac::get_catalyst_id_from_transaction_id::cache_for_transaction_id;
208219

209220
let id = match cip509.previous_transaction() {
210-
Some(previous) => {
211-
Query::get_latest(session, previous.into())
212-
.await
213-
.inspect_err(|e| error!("{e:?}"))
214-
.ok()
215-
.flatten()?
216-
.catalyst_id
217-
.into()
221+
Some(previous) => query_catalyst_id(session, previous, is_immutable).await?,
222+
None => {
223+
cip509
224+
.catalyst_id()
225+
.context("Empty Catalyst ID in root RBAC registration")?
226+
.as_short_id()
218227
},
219-
None => cip509.catalyst_id()?.as_short_id(),
220228
};
221229

222-
cache_for_transaction_id(txn_hash, id.clone(), slot, index);
230+
cache_for_transaction_id(txn_id, id.clone(), slot, index);
231+
232+
Ok(id)
233+
}
234+
235+
/// Queries a Catalyst ID from the database by the given transaction ID.
236+
async fn query_catalyst_id(
237+
session: &Arc<CassandraSession>, txn_id: TransactionId, is_immutable: bool,
238+
) -> anyhow::Result<IdUri> {
239+
use crate::db::index::queries::rbac::get_catalyst_id_from_transaction_id::Query;
223240

224-
Some(id)
241+
if let Some(q) = Query::get_latest(session, txn_id.into())
242+
.await
243+
.context("Failed to query Catalyst ID from transaction ID")?
244+
{
245+
Ok(q.catalyst_id.into())
246+
} else {
247+
if is_immutable {
248+
bail!("Unable to find Catalyst ID in the persistent DB");
249+
}
250+
251+
// If this block is a volatile/mutable one then try to look up a Catalyst ID in the
252+
// persistent database.
253+
let persistent_session =
254+
CassandraSession::get(true).context("Failed to get persistent DB session")?;
255+
Query::get_latest(&persistent_session, txn_id.into())
256+
.await
257+
.transpose()
258+
.context("Unable to find Catalyst ID in the persistent DB")?
259+
.map(|q| q.catalyst_id.into())
260+
}
225261
}
226262

227263
/// Returns stake addresses of the role 0.

0 commit comments

Comments
 (0)