From 89f981a2a4336e7333bf6ca2825a37e2b6f6142b Mon Sep 17 00:00:00 2001 From: Serhii Volovyk Date: Wed, 20 Nov 2024 16:58:47 +0200 Subject: [PATCH] renamings --- chain-signatures/node/src/indexer.rs | 18 +++++++++--------- .../node/src/protocol/consensus.rs | 8 ++++---- chain-signatures/node/src/protocol/mod.rs | 16 ++++++++-------- .../node/src/protocol/presignature.rs | 6 +++--- chain-signatures/node/src/protocol/triple.rs | 6 +++--- .../node/src/storage/app_data_storage.rs | 14 ++++++-------- .../node/src/storage/presignature_storage.rs | 8 ++++---- .../node/src/storage/triple_storage.rs | 8 ++++---- chain-signatures/node/src/web/mod.rs | 2 +- integration-tests/chain-signatures/src/lib.rs | 8 ++------ 10 files changed, 44 insertions(+), 50 deletions(-) diff --git a/chain-signatures/node/src/indexer.rs b/chain-signatures/node/src/indexer.rs index 2001a901..bd15a3e7 100644 --- a/chain-signatures/node/src/indexer.rs +++ b/chain-signatures/node/src/indexer.rs @@ -1,5 +1,5 @@ use crate::protocol::{SignQueue, SignRequest}; -use crate::storage::app_data_storage::AppDataRedisStorage; +use crate::storage::app_data_storage::AppDataStorage; use crypto_shared::{derive_epsilon, ScalarExt}; use k256::Scalar; use near_account_id::AccountId; @@ -88,7 +88,7 @@ pub struct ContractSignRequest { #[derive(Clone)] pub struct Indexer { - app_data_storage: AppDataRedisStorage, + app_data_storage: AppDataStorage, last_updated_timestamp: Arc>, latest_block_timestamp_nanosec: Arc>>, running_threshold: Duration, @@ -96,7 +96,7 @@ pub struct Indexer { } impl Indexer { - fn new(app_data_storage: AppDataRedisStorage, options: &Options) -> Self { + fn new(app_data_storage: AppDataStorage, options: &Options) -> Self { Self { app_data_storage: app_data_storage.clone(), last_updated_timestamp: Arc::new(RwLock::new(Instant::now())), @@ -106,8 +106,8 @@ impl Indexer { } } - pub async fn get_last_processed_block(&self) -> Option { - match self.app_data_storage.get_last_processed_block().await { + pub async fn last_processed_block(&self) -> Option { + match self.app_data_storage.last_processed_block().await { Ok(Some(block_height)) => Some(block_height), Ok(None) => { tracing::warn!("no last processed block found"); @@ -293,7 +293,7 @@ pub fn run( mpc_contract_id: &AccountId, node_account_id: &AccountId, queue: &Arc>, - app_data_storage: AppDataRedisStorage, + app_data_storage: AppDataStorage, rpc_client: near_fetch::Client, ) -> anyhow::Result<(JoinHandle>, Indexer)> { tracing::info!( @@ -329,7 +329,7 @@ pub fn run( let Ok(lake) = rt.block_on(async { update_last_processed_block(rpc_client.clone(), app_data_storage.clone()).await?; - if let Some(latest) = context.indexer.get_last_processed_block().await { + if let Some(latest) = context.indexer.last_processed_block().await { if i > 0 { tracing::warn!("indexer latest height {latest}, restart count={i}"); } @@ -412,9 +412,9 @@ pub fn run( /// This function ensures we do not go back in time a lot when restarting the node async fn update_last_processed_block( rpc_client: near_fetch::Client, - app_data_storage: AppDataRedisStorage, + app_data_storage: AppDataStorage, ) -> anyhow::Result<()> { - let last_processed_block = match app_data_storage.get_last_processed_block().await { + let last_processed_block = match app_data_storage.last_processed_block().await { Ok(Some(block_height)) => block_height, Ok(None) => 0, Err(err) => { diff --git a/chain-signatures/node/src/protocol/consensus.rs b/chain-signatures/node/src/protocol/consensus.rs index 40f30e4a..f1a1a006 100644 --- a/chain-signatures/node/src/protocol/consensus.rs +++ b/chain-signatures/node/src/protocol/consensus.rs @@ -11,9 +11,9 @@ use crate::protocol::presignature::PresignatureManager; use crate::protocol::signature::SignatureManager; use crate::protocol::state::{GeneratingState, ResharingState}; use crate::protocol::triple::TripleManager; -use crate::storage::presignature_storage::PresignatureRedisStorage; +use crate::storage::presignature_storage::PresignatureStorage; use crate::storage::secret_storage::SecretNodeStorageBox; -use crate::storage::triple_storage::TripleRedisStorage; +use crate::storage::triple_storage::TripleStorage; use crate::types::{KeygenProtocol, ReshareProtocol, SecretKeyShare}; use crate::util::AffinePointExt; use crate::{http_client, rpc_client}; @@ -39,8 +39,8 @@ pub trait ConsensusCtx { fn my_address(&self) -> &Url; fn sign_queue(&self) -> Arc>; fn secret_storage(&self) -> &SecretNodeStorageBox; - fn triple_storage(&self) -> &TripleRedisStorage; - fn presignature_storage(&self) -> &PresignatureRedisStorage; + fn triple_storage(&self) -> &TripleStorage; + fn presignature_storage(&self) -> &PresignatureStorage; fn cfg(&self) -> &Config; fn message_options(&self) -> http_client::Options; } diff --git a/chain-signatures/node/src/protocol/mod.rs b/chain-signatures/node/src/protocol/mod.rs index 7109e7c4..c7ea2cd8 100644 --- a/chain-signatures/node/src/protocol/mod.rs +++ b/chain-signatures/node/src/protocol/mod.rs @@ -29,9 +29,9 @@ use crate::protocol::consensus::ConsensusProtocol; use crate::protocol::cryptography::CryptographicProtocol; use crate::protocol::message::{MessageHandler, MpcMessageQueue}; use crate::rpc_client; -use crate::storage::presignature_storage::PresignatureRedisStorage; +use crate::storage::presignature_storage::PresignatureStorage; use crate::storage::secret_storage::SecretNodeStorageBox; -use crate::storage::triple_storage::TripleRedisStorage; +use crate::storage::triple_storage::TripleStorage; use cait_sith::protocol::Participant; use near_account_id::AccountId; @@ -53,8 +53,8 @@ struct Ctx { http_client: reqwest::Client, sign_queue: Arc>, secret_storage: SecretNodeStorageBox, - triple_storage: TripleRedisStorage, - presignature_storage: PresignatureRedisStorage, + triple_storage: TripleStorage, + presignature_storage: PresignatureStorage, cfg: Config, mesh: Mesh, message_options: http_client::Options, @@ -97,11 +97,11 @@ impl ConsensusCtx for &mut MpcSignProtocol { &self.ctx.cfg } - fn triple_storage(&self) -> &TripleRedisStorage { + fn triple_storage(&self) -> &TripleStorage { &self.ctx.triple_storage } - fn presignature_storage(&self) -> &PresignatureRedisStorage { + fn presignature_storage(&self) -> &PresignatureStorage { &self.ctx.presignature_storage } @@ -177,8 +177,8 @@ impl MpcSignProtocol { receiver: mpsc::Receiver, sign_queue: Arc>, secret_storage: SecretNodeStorageBox, - triple_storage: TripleRedisStorage, - presignature_storage: PresignatureRedisStorage, + triple_storage: TripleStorage, + presignature_storage: PresignatureStorage, cfg: Config, mesh_options: mesh::Options, message_options: http_client::Options, diff --git a/chain-signatures/node/src/protocol/presignature.rs b/chain-signatures/node/src/protocol/presignature.rs index 6aecae80..865df519 100644 --- a/chain-signatures/node/src/protocol/presignature.rs +++ b/chain-signatures/node/src/protocol/presignature.rs @@ -1,7 +1,7 @@ use super::message::PresignatureMessage; use super::triple::{Triple, TripleId, TripleManager}; use crate::protocol::contract::primitives::Participants; -use crate::storage::presignature_storage::PresignatureRedisStorage; +use crate::storage::presignature_storage::PresignatureStorage; use crate::types::{PresignatureProtocol, SecretKeyShare}; use crate::util::AffinePointExt; @@ -150,7 +150,7 @@ pub enum GenerationError { /// Abstracts how triples are generated by providing a way to request a new triple that will be /// complete some time in the future and a way to take an already generated triple. pub struct PresignatureManager { - presignature_storage: PresignatureRedisStorage, + presignature_storage: PresignatureStorage, /// Ongoing presignature generation protocols. generators: HashMap, /// The set of presignatures that were introduced to the system by the current node. @@ -171,7 +171,7 @@ impl PresignatureManager { threshold: usize, epoch: u64, my_account_id: &AccountId, - storage: &PresignatureRedisStorage, + storage: &PresignatureStorage, ) -> Self { Self { presignature_storage: storage.clone(), diff --git a/chain-signatures/node/src/protocol/triple.rs b/chain-signatures/node/src/protocol/triple.rs index cb9ef8ba..2e80d338 100644 --- a/chain-signatures/node/src/protocol/triple.rs +++ b/chain-signatures/node/src/protocol/triple.rs @@ -2,7 +2,7 @@ use super::contract::primitives::Participants; use super::cryptography::CryptographicError; use super::message::TripleMessage; use super::presignature::GenerationError; -use crate::storage::triple_storage::TripleRedisStorage; +use crate::storage::triple_storage::TripleStorage; use crate::types::TripleProtocol; use crate::util::AffinePointExt; @@ -80,7 +80,7 @@ impl TripleGenerator { /// complete some time in the future and a way to take an already generated triple. pub struct TripleManager { /// Triple Storage - pub triple_storage: TripleRedisStorage, + pub triple_storage: TripleStorage, /// The pool of triple protocols that have yet to be completed. pub generators: HashMap, @@ -128,7 +128,7 @@ impl TripleManager { threshold: usize, epoch: u64, my_account_id: &AccountId, - storage: &TripleRedisStorage, + storage: &TripleStorage, ) -> Self { Self { generators: HashMap::new(), diff --git a/chain-signatures/node/src/storage/app_data_storage.rs b/chain-signatures/node/src/storage/app_data_storage.rs index a0d340a7..30041ef6 100644 --- a/chain-signatures/node/src/storage/app_data_storage.rs +++ b/chain-signatures/node/src/storage/app_data_storage.rs @@ -4,33 +4,31 @@ use near_primitives::types::BlockHeight; use near_sdk::AccountId; use redis::AsyncCommands; -type AppDataResult = std::result::Result; - const APP_DATA_PREFIX: &str = "app_data"; const APP_DATA_STORAGE_VERSION: &str = "v1"; -pub fn init(pool: &Pool, node_account_id: &AccountId) -> AppDataRedisStorage { - AppDataRedisStorage { +pub fn init(pool: &Pool, node_account_id: &AccountId) -> AppDataStorage { + AppDataStorage { redis_pool: pool.clone(), node_account_id: node_account_id.clone(), } } #[derive(Clone)] -pub struct AppDataRedisStorage { +pub struct AppDataStorage { redis_pool: Pool, node_account_id: AccountId, } -impl AppDataRedisStorage { - pub async fn set_last_processed_block(&self, height: BlockHeight) -> AppDataResult<()> { +impl AppDataStorage { + pub async fn set_last_processed_block(&self, height: BlockHeight) -> anyhow::Result<()> { let mut conn = self.redis_pool.get().await?; conn.set::<&str, BlockHeight, ()>(&self.last_block_key(), height) .await?; Ok(()) } - pub async fn get_last_processed_block(&self) -> AppDataResult> { + pub async fn last_processed_block(&self) -> anyhow::Result> { let mut conn = self.redis_pool.get().await?; let result: Option = conn.get(self.last_block_key()).await?; Ok(result) diff --git a/chain-signatures/node/src/storage/presignature_storage.rs b/chain-signatures/node/src/storage/presignature_storage.rs index 0f750185..0138d27f 100644 --- a/chain-signatures/node/src/storage/presignature_storage.rs +++ b/chain-signatures/node/src/storage/presignature_storage.rs @@ -10,20 +10,20 @@ type PresigResult = std::result::Result; // Can be used to "clear" redis storage in case of a breaking change const PRESIGNATURE_STORAGE_VERSION: &str = "v1"; -pub fn init(pool: &Pool, node_account_id: &AccountId) -> PresignatureRedisStorage { - PresignatureRedisStorage { +pub fn init(pool: &Pool, node_account_id: &AccountId) -> PresignatureStorage { + PresignatureStorage { redis_pool: pool.clone(), node_account_id: node_account_id.clone(), } } #[derive(Clone)] -pub struct PresignatureRedisStorage { +pub struct PresignatureStorage { redis_pool: Pool, node_account_id: AccountId, } -impl PresignatureRedisStorage { +impl PresignatureStorage { pub async fn insert(&self, presignature: Presignature) -> PresigResult<()> { let mut connection = self.redis_pool.get().await?; connection diff --git a/chain-signatures/node/src/storage/triple_storage.rs b/chain-signatures/node/src/storage/triple_storage.rs index 4a76c336..ff5e39fe 100644 --- a/chain-signatures/node/src/storage/triple_storage.rs +++ b/chain-signatures/node/src/storage/triple_storage.rs @@ -10,20 +10,20 @@ type TripleResult = std::result::Result; // Can be used to "clear" redis storage in case of a breaking change const TRIPLE_STORAGE_VERSION: &str = "v1"; -pub fn init(pool: &Pool, account_id: &AccountId) -> TripleRedisStorage { - TripleRedisStorage { +pub fn init(pool: &Pool, account_id: &AccountId) -> TripleStorage { + TripleStorage { redis_pool: pool.clone(), node_account_id: account_id.clone(), } } #[derive(Clone)] -pub struct TripleRedisStorage { +pub struct TripleStorage { redis_pool: Pool, node_account_id: AccountId, } -impl TripleRedisStorage { +impl TripleStorage { pub async fn insert(&self, triple: Triple) -> TripleResult<()> { let mut conn = self.redis_pool.get().await?; conn.hset::<&str, TripleId, Triple, ()>(&self.triple_key(), triple.id, triple) diff --git a/chain-signatures/node/src/web/mod.rs b/chain-signatures/node/src/web/mod.rs index c72ae972..63326751 100644 --- a/chain-signatures/node/src/web/mod.rs +++ b/chain-signatures/node/src/web/mod.rs @@ -131,7 +131,7 @@ pub enum StateView { async fn state(Extension(state): Extension>) -> Result> { tracing::debug!("fetching state"); // TODO: rename to last_processed_block when making other breaking changes - let latest_block_height = state.indexer.get_last_processed_block().await.unwrap_or(0); + let latest_block_height = state.indexer.last_processed_block().await.unwrap_or(0); let is_stable = state.indexer.is_stable().await; let protocol_state = state.protocol_state.read().await; diff --git a/integration-tests/chain-signatures/src/lib.rs b/integration-tests/chain-signatures/src/lib.rs index c79d8373..08e4fd54 100644 --- a/integration-tests/chain-signatures/src/lib.rs +++ b/integration-tests/chain-signatures/src/lib.rs @@ -19,7 +19,7 @@ use mpc_node::gcp::GcpService; use mpc_node::http_client; use mpc_node::mesh; use mpc_node::storage; -use mpc_node::storage::triple_storage::TripleRedisStorage; +use mpc_node::storage::triple_storage::TripleStorage; use near_crypto::KeyFile; use near_workspaces::network::{Sandbox, ValidatorKey}; use near_workspaces::types::{KeyType, SecretKey}; @@ -156,11 +156,7 @@ impl Nodes<'_> { Ok(()) } - pub async fn triple_storage( - &self, - redis_pool: &Pool, - account_id: &AccountId, - ) -> TripleRedisStorage { + pub async fn triple_storage(&self, redis_pool: &Pool, account_id: &AccountId) -> TripleStorage { storage::triple_storage::init(redis_pool, account_id) }