From 4960a606edf1718ae028976e1d2e9163e7bd3bc3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joaqu=C3=ADn=20Rosales?= Date: Tue, 24 Sep 2024 22:10:03 -0700 Subject: [PATCH] wip: index rbac registrations --- catalyst-gateway/bin/Cargo.toml | 5 ++- .../bin/src/db/index/block/mod.rs | 2 +- .../db/index/block/rbac509/insert_rbac509.rs | 37 +++++++++++++++---- .../bin/src/db/index/block/rbac509/mod.rs | 28 +++++++++----- 4 files changed, 52 insertions(+), 20 deletions(-) diff --git a/catalyst-gateway/bin/Cargo.toml b/catalyst-gateway/bin/Cargo.toml index aed66bbb86..7989c01d41 100644 --- a/catalyst-gateway/bin/Cargo.toml +++ b/catalyst-gateway/bin/Cargo.toml @@ -15,7 +15,8 @@ repository.workspace = true workspace = true [dependencies] -cardano-chain-follower = { version = "0.0.2", git = "https://github.com/input-output-hk/catalyst-libs.git", tag = "v0.0.2" } +cardano-chain-follower = { version = "0.0.2", git = "https://github.com/input-output-hk/catalyst-libs.git", branch = "main" } +#cardano-chain-follower = { version = "0.0.2", git = "https://github.com/input-output-hk/catalyst-libs.git", tag = "v0.0.2" } pallas = { version = "0.30.1", git = "https://github.com/input-output-hk/catalyst-pallas.git", rev = "9b5183c8b90b90fe2cc319d986e933e9518957b3" } pallas-traverse = { version = "0.30.1", git = "https://github.com/input-output-hk/catalyst-pallas.git", rev = "9b5183c8b90b90fe2cc319d986e933e9518957b3" } @@ -92,4 +93,4 @@ base64 = "0.22.1" dashmap = "6.0.1" [build-dependencies] -build-info-build = "0.0.38" \ No newline at end of file +build-info-build = "0.0.38" diff --git a/catalyst-gateway/bin/src/db/index/block/mod.rs b/catalyst-gateway/bin/src/db/index/block/mod.rs index b37fa71968..0d488c671e 100644 --- a/catalyst-gateway/bin/src/db/index/block/mod.rs +++ b/catalyst-gateway/bin/src/db/index/block/mod.rs @@ -57,7 +57,7 @@ pub(crate) async fn index_block(block: &MultiEraBlock) -> anyhow::Result<()> { txo_index.index(txs, slot_no, &txn_hash, txn); // Index RBAC 509 inside the transaction. - rbac509_index.index(txn_index, txn, slot_no, block); + rbac509_index.index(&txn_hash, txn_index, txn, slot_no, block); } // We then execute each batch of data from the block. diff --git a/catalyst-gateway/bin/src/db/index/block/rbac509/insert_rbac509.rs b/catalyst-gateway/bin/src/db/index/block/rbac509/insert_rbac509.rs index b01a14a193..d6c77e4946 100644 --- a/catalyst-gateway/bin/src/db/index/block/rbac509/insert_rbac509.rs +++ b/catalyst-gateway/bin/src/db/index/block/rbac509/insert_rbac509.rs @@ -2,9 +2,10 @@ //! //! Note, there are multiple ways TXO Data is indexed and they all happen in here. -use std::sync::Arc; +use std::{fmt::Debug, sync::Arc}; -use scylla::{SerializeRow, Session}; +use cardano_chain_follower::Metadata::cip509::Cip509; +use scylla::{frame::value::MaybeUnset, SerializeRow, Session}; use tracing::error; use crate::{ @@ -17,7 +18,7 @@ use crate::{ const INSERT_RBAC509_QUERY: &str = include_str!("./cql/insert_rbac509.cql"); /// Insert RBAC Registration Query Parameters -#[derive(SerializeRow, Debug)] +#[derive(SerializeRow)] pub(super) struct Params { /// Chain Root Hash. 32 bytes. chain_root: Vec, @@ -30,23 +31,43 @@ pub(super) struct Params { /// Transaction Offset inside the block. txn: i16, /// Hash of Previous Transaction. Is `None` for the first registration. 32 Bytes. - prv_txn_id: Option>, + prv_txn_id: MaybeUnset>, +} + +impl Debug for Params { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let prv_txn_id = match self.prv_txn_id { + MaybeUnset::Unset => "UNSET", + MaybeUnset::Set(ref v) => &hex::encode(v), + }; + f.debug_struct("Params") + .field("chain_root", &self.chain_root) + .field("transaction_id", &self.transaction_id) + .field("purpose", &self.purpose) + .field("slot_no", &self.slot_no) + .field("txn", &self.txn) + .field("prv_txn_id", &prv_txn_id) + .finish() + } } #[allow(clippy::todo, dead_code, clippy::unused_async)] impl Params { /// Create a new record for this transaction. pub(super) fn new( - chain_root: Vec, transaction_id: Vec, purpose: Vec, slot_no: u64, txn: i16, - prv_txn_id: Option>, + chain_root: Vec, transaction_id: Vec, slot_no: u64, txn: i16, cip509: &Cip509, ) -> Self { Params { chain_root, transaction_id, - purpose, + purpose: cip509.purpose.to_vec(), slot_no: num_bigint::BigInt::from(slot_no), txn, - prv_txn_id, + prv_txn_id: if let Some(tx_id) = cip509.prv_tx_id { + MaybeUnset::Set(tx_id.to_vec()) + } else { + MaybeUnset::Unset + }, } } diff --git a/catalyst-gateway/bin/src/db/index/block/rbac509/mod.rs b/catalyst-gateway/bin/src/db/index/block/rbac509/mod.rs index c91bc36465..3d05673e04 100644 --- a/catalyst-gateway/bin/src/db/index/block/rbac509/mod.rs +++ b/catalyst-gateway/bin/src/db/index/block/rbac509/mod.rs @@ -1,11 +1,4 @@ //! Index Role-Based Access Control (RBAC) Registration. -#![allow( - unused_imports, - clippy::todo, - dead_code, - unused_variables, - clippy::unused_async -)] mod insert_rbac509; @@ -44,10 +37,27 @@ impl Rbac509InsertQuery { } /// Index the RBAC 509 registrations in a transaction. + #[allow(clippy::todo, unused_variables)] pub(crate) fn index( - &mut self, txn: usize, txn_index: i16, slot_no: u64, block: &MultiEraBlock, + &mut self, txn_hash: &[u8], txn: usize, txn_index: i16, slot_no: u64, block: &MultiEraBlock, ) { - todo!(); + if let Some(decoded_metadata) = block.txn_metadata(txn, Metadata::cip509::LABEL) { + #[allow(irrefutable_let_patterns)] + if let Metadata::DecodedMetadataValues::Cip509(rbac) = &decoded_metadata.value { + if let Some(tx_id) = rbac.prv_tx_id { + // WIP: fetch chain_root from cache or DB + } else { + let chain_root = txn_hash.to_vec(); + self.registrations.push(insert_rbac509::Params::new( + chain_root, + txn_hash.to_vec(), + slot_no, + txn_index, + rbac, + )); + } + } + } } /// Execute the RBAC 509 Registration Indexing Queries.