@@ -7,6 +7,7 @@ pub(crate) mod insert_rbac509_invalid;
77
88use std:: sync:: Arc ;
99
10+ use anyhow:: { bail, Context } ;
1011use 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.
201214async 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