Skip to content

Commit 0756e01

Browse files
authored
feat(athena): Handle rollback and roll-forward for block event in rbac-registration module (#546)
* feat: implement rollback rolforward block handler Signed-off-by: bkioshn <[email protected]> * fix: cleanup Signed-off-by: bkioshn <[email protected]> * fix: finalize Signed-off-by: bkioshn <[email protected]> --------- Signed-off-by: bkioshn <[email protected]>
1 parent 3780d0e commit 0756e01

File tree

14 files changed

+732
-262
lines changed

14 files changed

+732
-262
lines changed

hermes/apps/athena/modules/rbac-registration/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ crate-type = ["cdylib"]
1010
wit-bindgen = "0.43.0"
1111
anyhow = "1.0.98"
1212
serde_json = "1.0.142"
13+
strum = "0.27.2"
14+
strum_macros = "0.27.2"
1315

1416
cardano-blockchain-types = { version = "0.0.6", git = "https://github.com/input-output-hk/catalyst-libs", tag = "cardano-blockchain-types/v0.0.6" }
1517
rbac-registration = { version = "0.0.9", git = "https://github.com/input-output-hk/catalyst-libs", tag = "rbac-registration/v0.0.9" }
Lines changed: 45 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,55 @@
11
//! Create the database tables for RBAC registration.
22
3-
use crate::{hermes::sqlite::api::Sqlite, utils::log::log_error};
3+
use crate::{
4+
database::{
5+
operation::Operation, query_builder::QueryBuilder, statement::DatabaseStatement,
6+
RBAC_REGISTRATION_PERSISTENT_TABLE_NAME, RBAC_REGISTRATION_VOLATILE_TABLE_NAME,
7+
RBAC_STAKE_ADDRESS_PERSISTENT_TABLE_NAME, RBAC_STAKE_ADDRESS_VOLATILE_TABLE_NAME,
8+
},
9+
hermes::sqlite::api::Sqlite,
10+
};
411

5-
/// RBAC registration database schema.
6-
const RBAC_REGISTRATION_TABLE: &str = r#"
7-
CREATE TABLE IF NOT EXISTS rbac_registration (
8-
txn_id BLOB NOT NULL, -- 32 bytes of transaction ID (aka transaction hash)
9-
slot_no INTEGER NOT NULL, -- Slot number
10-
txn_idx INTEGER NOT NULL, -- Index of the transaction in the block
11-
prv_txn_id BLOB, -- 32 bytes of previous transaction ID (aka transaction hash)
12-
purpose TEXT, -- Registration purpose
13-
catalyst_id TEXT, -- Catalyst short ID - Exist only for Role0
14-
problem_report TEXT, -- Problem report
12+
/// Create a persistent `rbac_registration` and `rbac_stake_address` table.
13+
pub(crate) fn create_rbac_persistent_tables(sqlite: &Sqlite) {
14+
const FUNCTION_NAME: &str = "create_rbac_persistent_tables";
1515

16-
PRIMARY KEY (txn_id)
17-
18-
);
19-
-- Use for root lookup by catalyst_id
20-
CREATE INDEX IF NOT EXISTS idx_rbac_reg_cat_id ON rbac_registration (catalyst_id, slot_no, txn_idx);
21-
-- Child lookup
22-
CREATE INDEX IF NOT EXISTS idx_rbac_reg_prv_tx ON rbac_registration (prv_txn_id, slot_no, txn_idx);
23-
"#;
16+
if let Err(_) = DatabaseStatement::execute_statement(
17+
sqlite,
18+
&QueryBuilder::create_rbac_registration_table(RBAC_REGISTRATION_PERSISTENT_TABLE_NAME),
19+
Operation::Create,
20+
FUNCTION_NAME,
21+
) {
22+
return;
23+
}
2424

25-
/// RBAC registration stake address database schema.
26-
const RBAC_STAKE_ADDRESS_TABLE: &str = r#"
27-
CREATE TABLE IF NOT EXISTS rbac_stake_address (
28-
stake_address BLOB NOT NULL, -- 29 bytes of stake hash (CIP19)
29-
slot_no INTEGER NOT NULL, -- Slot number
30-
txn_idx INTEGER NOT NULL, -- Index of the transaction in the block
31-
catalyst_id TEXT, -- Catalyst short ID - Exist only for Role0
32-
txn_id BLOB NOT NULL, -- 32 bytes of transaction ID (aka transaction hash)
25+
if let Err(_) = DatabaseStatement::execute_statement(
26+
sqlite,
27+
&QueryBuilder::create_rbac_stake_address_table(RBAC_STAKE_ADDRESS_PERSISTENT_TABLE_NAME),
28+
Operation::Create,
29+
FUNCTION_NAME,
30+
) {
31+
return;
32+
}
33+
}
3334

34-
PRIMARY KEY (stake_address, txn_id)
35-
);
36-
-- Stake lookup (always want the newest registration first)
37-
CREATE INDEX IF NOT EXISTS idx_stake_addr ON rbac_stake_address (stake_address, slot_no DESC, txn_idx DESC);
38-
"#;
35+
/// Create a volatile `rbac_registration` and `rbac_stake_address` table.
36+
pub(crate) fn create_rbac_volatile_tables(sqlite: &Sqlite) {
37+
const FUNCTION_NAME: &str = "create_rbac_volatile_tables";
3938

40-
/// Create a `rbac_registration` and `rbac_stake_address` table.
41-
pub(crate) fn create_rbac_tables(sqlite: &Sqlite) {
42-
const FUNCTION_NAME: &str = "create_rbac_tables";
43-
if let Err(e) = sqlite.execute(RBAC_REGISTRATION_TABLE) {
44-
log_error(
45-
file!(),
46-
FUNCTION_NAME,
47-
"hermes::sqlite::api::execute",
48-
&format!("Failed to create rbac_registration table: {e}"),
49-
None,
50-
);
39+
if let Err(_) = DatabaseStatement::execute_statement(
40+
sqlite,
41+
&QueryBuilder::create_rbac_registration_table(RBAC_REGISTRATION_VOLATILE_TABLE_NAME),
42+
Operation::Create,
43+
FUNCTION_NAME,
44+
) {
45+
return;
5146
}
52-
if let Err(e) = sqlite.execute(RBAC_STAKE_ADDRESS_TABLE) {
53-
log_error(
54-
file!(),
55-
FUNCTION_NAME,
56-
"hermes::sqlite::api::execute",
57-
&format!("Failed to create rbac_stake_address table: {e}"),
58-
None,
59-
);
47+
if let Err(_) = DatabaseStatement::execute_statement(
48+
sqlite,
49+
&QueryBuilder::create_rbac_stake_address_table(RBAC_STAKE_ADDRESS_VOLATILE_TABLE_NAME),
50+
Operation::Create,
51+
FUNCTION_NAME,
52+
) {
53+
return;
6054
}
6155
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
//! Database DELETE operation.
2+
3+
pub(crate) mod roll_back;
4+
pub(crate) mod roll_forward;
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//! Handle block rollback from volatile table.
2+
3+
use crate::{
4+
database::{operation::Operation, query_builder::QueryBuilder, statement::DatabaseStatement},
5+
hermes::sqlite::api::{Sqlite, Statement},
6+
};
7+
8+
/// Prepare delete statement for deleting data when rollback happen from given volatile table.
9+
pub(crate) fn prepare_roll_back_delete_from_volatile(
10+
sqlite: &Sqlite,
11+
table: &str,
12+
) -> anyhow::Result<Statement> {
13+
const FUNCTION_NAME: &str = "prepare_roll_back_delete_from_volatile";
14+
DatabaseStatement::prepare_statement(
15+
sqlite,
16+
&QueryBuilder::delete_roll_back(table),
17+
Operation::Delete,
18+
FUNCTION_NAME,
19+
)
20+
.map_err(|e| anyhow::anyhow!(e))
21+
}
22+
23+
/// Delete data for handling rollback from volatile table.
24+
pub(crate) fn roll_back_delete_from_volatile(
25+
stmt: &Statement,
26+
slot_no: u64,
27+
) {
28+
const FUNCTION_NAME: &str = "roll_back_delete_from_volatile";
29+
if let Err(_) = DatabaseStatement::bind_slot(stmt, slot_no, FUNCTION_NAME) {
30+
return;
31+
}
32+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//! Handle block forward from volatile table.
2+
3+
use crate::{
4+
database::{operation::Operation, query_builder::QueryBuilder, statement::DatabaseStatement},
5+
hermes::sqlite::api::{Sqlite, Statement},
6+
};
7+
8+
/// Prepare delete statement for deleting data for roll forward from given volatile table.
9+
pub(crate) fn prepare_roll_forward_delete_from_volatile(
10+
sqlite: &Sqlite,
11+
table: &str,
12+
) -> anyhow::Result<Statement> {
13+
const FUNCTION_NAME: &str = "prepare_roll_forward_delete_from_volatile";
14+
DatabaseStatement::prepare_statement(
15+
sqlite,
16+
&QueryBuilder::delete_roll_forward(table),
17+
Operation::Delete,
18+
FUNCTION_NAME,
19+
)
20+
.map_err(|e| anyhow::anyhow!(e))
21+
}
22+
23+
/// Delete data for handling roll forward from volatile table.
24+
pub(crate) fn roll_forward_delete_from_volatile(
25+
stmt: &Statement,
26+
slot_no: u64,
27+
) {
28+
const FUNCTION_NAME: &str = "roll_forward_delete_from_volatile";
29+
30+
if let Err(_) = DatabaseStatement::bind_slot(stmt, slot_no, FUNCTION_NAME) {
31+
return;
32+
}
33+
}

hermes/apps/athena/modules/rbac-registration/src/database/insert.rs

Lines changed: 0 additions & 189 deletions
This file was deleted.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
//! Database INSERT operation.
2+
3+
pub(crate) mod rbac_table;
4+
pub(crate) mod stake_addr_table;

0 commit comments

Comments
 (0)