Skip to content

Commit

Permalink
renamings
Browse files Browse the repository at this point in the history
  • Loading branch information
volovyks committed Nov 20, 2024
1 parent 00bbd96 commit 89f981a
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 50 deletions.
18 changes: 9 additions & 9 deletions chain-signatures/node/src/indexer.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -88,15 +88,15 @@ pub struct ContractSignRequest {

#[derive(Clone)]
pub struct Indexer {
app_data_storage: AppDataRedisStorage,
app_data_storage: AppDataStorage,
last_updated_timestamp: Arc<RwLock<Instant>>,
latest_block_timestamp_nanosec: Arc<RwLock<Option<u64>>>,
running_threshold: Duration,
behind_threshold: Duration,
}

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())),
Expand All @@ -106,8 +106,8 @@ impl Indexer {
}
}

pub async fn get_last_processed_block(&self) -> Option<BlockHeight> {
match self.app_data_storage.get_last_processed_block().await {
pub async fn last_processed_block(&self) -> Option<BlockHeight> {
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");
Expand Down Expand Up @@ -293,7 +293,7 @@ pub fn run(
mpc_contract_id: &AccountId,
node_account_id: &AccountId,
queue: &Arc<RwLock<SignQueue>>,
app_data_storage: AppDataRedisStorage,
app_data_storage: AppDataStorage,
rpc_client: near_fetch::Client,
) -> anyhow::Result<(JoinHandle<anyhow::Result<()>>, Indexer)> {
tracing::info!(
Expand Down Expand Up @@ -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}");
}
Expand Down Expand Up @@ -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) => {
Expand Down
8 changes: 4 additions & 4 deletions chain-signatures/node/src/protocol/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -39,8 +39,8 @@ pub trait ConsensusCtx {
fn my_address(&self) -> &Url;
fn sign_queue(&self) -> Arc<RwLock<SignQueue>>;
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;
}
Expand Down
16 changes: 8 additions & 8 deletions chain-signatures/node/src/protocol/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -53,8 +53,8 @@ struct Ctx {
http_client: reqwest::Client,
sign_queue: Arc<RwLock<SignQueue>>,
secret_storage: SecretNodeStorageBox,
triple_storage: TripleRedisStorage,
presignature_storage: PresignatureRedisStorage,
triple_storage: TripleStorage,
presignature_storage: PresignatureStorage,
cfg: Config,
mesh: Mesh,
message_options: http_client::Options,
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -177,8 +177,8 @@ impl MpcSignProtocol {
receiver: mpsc::Receiver<MpcMessage>,
sign_queue: Arc<RwLock<SignQueue>>,
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,
Expand Down
6 changes: 3 additions & 3 deletions chain-signatures/node/src/protocol/presignature.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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<PresignatureId, PresignatureGenerator>,
/// The set of presignatures that were introduced to the system by the current node.
Expand All @@ -171,7 +171,7 @@ impl PresignatureManager {
threshold: usize,
epoch: u64,
my_account_id: &AccountId,
storage: &PresignatureRedisStorage,
storage: &PresignatureStorage,
) -> Self {
Self {
presignature_storage: storage.clone(),
Expand Down
6 changes: 3 additions & 3 deletions chain-signatures/node/src/protocol/triple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<TripleId, TripleGenerator>,
Expand Down Expand Up @@ -128,7 +128,7 @@ impl TripleManager {
threshold: usize,
epoch: u64,
my_account_id: &AccountId,
storage: &TripleRedisStorage,
storage: &TripleStorage,
) -> Self {
Self {
generators: HashMap::new(),
Expand Down
14 changes: 6 additions & 8 deletions chain-signatures/node/src/storage/app_data_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,31 @@ use near_primitives::types::BlockHeight;
use near_sdk::AccountId;
use redis::AsyncCommands;

type AppDataResult<T> = std::result::Result<T, anyhow::Error>;

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<Option<BlockHeight>> {
pub async fn last_processed_block(&self) -> anyhow::Result<Option<BlockHeight>> {
let mut conn = self.redis_pool.get().await?;
let result: Option<BlockHeight> = conn.get(self.last_block_key()).await?;
Ok(result)
Expand Down
8 changes: 4 additions & 4 deletions chain-signatures/node/src/storage/presignature_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@ type PresigResult<T> = std::result::Result<T, anyhow::Error>;
// 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
Expand Down
8 changes: 4 additions & 4 deletions chain-signatures/node/src/storage/triple_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@ type TripleResult<T> = std::result::Result<T, anyhow::Error>;
// 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)
Expand Down
2 changes: 1 addition & 1 deletion chain-signatures/node/src/web/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ pub enum StateView {
async fn state(Extension(state): Extension<Arc<AxumState>>) -> Result<Json<StateView>> {
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;

Expand Down
8 changes: 2 additions & 6 deletions integration-tests/chain-signatures/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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)
}

Expand Down

0 comments on commit 89f981a

Please sign in to comment.