-
Notifications
You must be signed in to change notification settings - Fork 237
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Gateway database modifications for different modes (#4868)
* Gateway db modifications for different modes * Add exit mixnet and replace whitespaces
- Loading branch information
Showing
6 changed files
with
235 additions
and
14 deletions.
There are no files selected for viewing
98 changes: 98 additions & 0 deletions
98
common/gateway-storage/migrations/20240910120000_generic_client_id.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
* Copyright 2024 - Nym Technologies SA <contact@nymtech.net> | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
CREATE TABLE clients ( | ||
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, | ||
client_type TEXT NOT NULL CHECK(client_type IN ('entry_mixnet', 'exit_mixnet', 'entry_wireguard', 'exit_wireguard')) | ||
); | ||
|
||
INSERT INTO clients (id, client_type) | ||
SELECT id, 'entry_mixnet' | ||
FROM shared_keys; | ||
|
||
CREATE TABLE shared_keys_tmp ( | ||
client_id INTEGER NOT NULL PRIMARY KEY REFERENCES clients(id), | ||
client_address_bs58 TEXT NOT NULL UNIQUE, | ||
derived_aes128_ctr_blake3_hmac_keys_bs58 TEXT NOT NULL | ||
); | ||
|
||
INSERT INTO shared_keys_tmp (client_id, client_address_bs58, derived_aes128_ctr_blake3_hmac_keys_bs58) | ||
SELECT id as client_id, client_address_bs58, derived_aes128_ctr_blake3_hmac_keys_bs58 FROM shared_keys; | ||
|
||
CREATE TABLE available_bandwidth_tmp ( | ||
client_id INTEGER NOT NULL PRIMARY KEY REFERENCES clients(id), | ||
available INTEGER NOT NULL, | ||
expiration TIMESTAMP WITHOUT TIME ZONE | ||
); | ||
|
||
INSERT INTO available_bandwidth_tmp | ||
SELECT * FROM available_bandwidth; | ||
|
||
DROP TABLE available_bandwidth; | ||
ALTER TABLE available_bandwidth_tmp RENAME TO available_bandwidth; | ||
|
||
CREATE TABLE received_ticket_tmp ( | ||
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, | ||
client_id INTEGER NOT NULL REFERENCES clients(id), | ||
received_at TIMESTAMP WITHOUT TIME ZONE NOT NULL, | ||
rejected BOOLEAN | ||
); | ||
|
||
INSERT INTO received_ticket_tmp | ||
SELECT * FROM received_ticket; | ||
|
||
DROP INDEX received_ticket_index; | ||
CREATE INDEX received_ticket_index ON received_ticket_tmp (received_at); | ||
|
||
-- received tickets that are in the process of verifying | ||
CREATE TABLE ticket_data_tmp ( | ||
ticket_id INTEGER NOT NULL PRIMARY KEY REFERENCES received_ticket_tmp(id), | ||
|
||
-- serial_number, alongside the entire row, will get purged after redemption is complete | ||
serial_number BLOB NOT NULL UNIQUE, | ||
|
||
-- data will get purged after 80% of signers verifies it | ||
data BLOB | ||
); | ||
|
||
INSERT INTO ticket_data_tmp | ||
SELECT * FROM ticket_data; | ||
|
||
DROP TABLE ticket_data; | ||
ALTER TABLE ticket_data_tmp RENAME TO ticket_data; | ||
|
||
-- result of a verification from a single signer (API) | ||
CREATE TABLE ticket_verification_tmp ( | ||
ticket_id INTEGER NOT NULL REFERENCES received_ticket_tmp(id), | ||
signer_id INTEGER NOT NULL, | ||
verified_at TIMESTAMP WITHOUT TIME ZONE NOT NULL, | ||
accepted BOOLEAN NOT NULL, | ||
|
||
PRIMARY KEY (ticket_id, signer_id) | ||
); | ||
|
||
DROP INDEX ticket_verification_index; | ||
CREATE INDEX ticket_verification_index ON ticket_verification_tmp (ticket_id); | ||
|
||
DROP TABLE ticket_verification; | ||
ALTER TABLE ticket_verification_tmp RENAME TO ticket_verification; | ||
|
||
-- verified tickets that are yet to be redeemed | ||
CREATE TABLE verified_tickets_tmp ( | ||
ticket_id INTEGER NOT NULL PRIMARY KEY REFERENCES received_ticket_tmp(id), | ||
proposal_id INTEGER REFERENCES redemption_proposals(proposal_id) | ||
); | ||
|
||
DROP INDEX verified_tickets_index; | ||
CREATE INDEX verified_tickets_index ON verified_tickets_tmp (proposal_id); | ||
|
||
DROP TABLE verified_tickets; | ||
ALTER TABLE verified_tickets_tmp RENAME TO verified_tickets; | ||
|
||
DROP TABLE received_ticket; | ||
ALTER TABLE received_ticket_tmp RENAME TO received_ticket; | ||
|
||
DROP TABLE shared_keys; | ||
ALTER TABLE shared_keys_tmp RENAME TO shared_keys; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// Copyright 2024 - Nym Technologies SA <contact@nymtech.net> | ||
// SPDX-License-Identifier: GPL-3.0-only | ||
|
||
use std::str::FromStr; | ||
|
||
use crate::models::Client; | ||
|
||
#[derive(Debug, PartialEq, sqlx::Type)] | ||
#[sqlx(type_name = "TEXT")] // SQLite TEXT type | ||
pub enum ClientType { | ||
EntryMixnet, | ||
ExitMixnet, | ||
EntryWireguard, | ||
ExitWireguard, | ||
} | ||
|
||
impl FromStr for ClientType { | ||
type Err = &'static str; | ||
|
||
fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
match s { | ||
"entry_mixnet" => Ok(ClientType::EntryMixnet), | ||
"exit_mixnet" => Ok(ClientType::ExitMixnet), | ||
"entry_wireguard" => Ok(ClientType::EntryWireguard), | ||
"exit_wireguard" => Ok(ClientType::ExitWireguard), | ||
_ => Err("Invalid client type"), | ||
} | ||
} | ||
} | ||
|
||
impl std::fmt::Display for ClientType { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
let s = match self { | ||
ClientType::EntryMixnet => "entry_mixnet", | ||
ClientType::ExitMixnet => "exit_mixnet", | ||
ClientType::EntryWireguard => "entry_wireguard", | ||
ClientType::ExitWireguard => "exit_wireguard", | ||
}; | ||
write!(f, "{}", s) | ||
} | ||
} | ||
|
||
#[derive(Clone)] | ||
pub(crate) struct ClientManager { | ||
connection_pool: sqlx::SqlitePool, | ||
} | ||
|
||
impl ClientManager { | ||
/// Creates new instance of the `ClientManager` with the provided sqlite connection pool. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `connection_pool`: database connection pool to use. | ||
pub(crate) fn new(connection_pool: sqlx::SqlitePool) -> Self { | ||
ClientManager { connection_pool } | ||
} | ||
|
||
/// Inserts new client to the storage, specifying its type. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `client_type`: Type of the client that gets inserted | ||
pub(crate) async fn insert_client(&self, client_type: ClientType) -> Result<i64, sqlx::Error> { | ||
let client_id = sqlx::query!("INSERT INTO clients(client_type) VALUES (?)", client_type) | ||
.execute(&self.connection_pool) | ||
.await? | ||
.last_insert_rowid(); | ||
Ok(client_id) | ||
} | ||
|
||
/// Tries to retrieve a particular client. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `id`: The client id | ||
pub(crate) async fn get_client(&self, id: i64) -> Result<Option<Client>, sqlx::Error> { | ||
sqlx::query_as!( | ||
Client, | ||
r#" | ||
SELECT id, client_type as "client_type: ClientType" | ||
FROM clients | ||
WHERE id = ? | ||
"#, | ||
id | ||
) | ||
.fetch_optional(&self.connection_pool) | ||
.await | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters