|
1 | 1 | //! Create the database tables for RBAC registration.
|
2 | 2 |
|
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 | +}; |
4 | 11 |
|
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"; |
15 | 15 |
|
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 | + } |
24 | 24 |
|
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 | +} |
33 | 34 |
|
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"; |
39 | 38 |
|
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; |
51 | 46 | }
|
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; |
60 | 54 | }
|
61 | 55 | }
|
0 commit comments