From c845935b2bf3f2134f1eb4084997647466439b42 Mon Sep 17 00:00:00 2001 From: Xiangyi Zheng Date: Wed, 27 Mar 2024 21:56:48 -0700 Subject: [PATCH 1/5] in progress --- node/src/protocol/message.rs | 3 +++ node/src/protocol/presignature.rs | 20 +++++++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/node/src/protocol/message.rs b/node/src/protocol/message.rs index f2ce62911..e45bf1feb 100644 --- a/node/src/protocol/message.rs +++ b/node/src/protocol/message.rs @@ -267,6 +267,9 @@ impl MessageHandler for RunningState { Err(presignature::GenerationError::AlreadyGenerated) => { tracing::info!(id, "presignature already generated, nothing left to do") } + Err(presignature::GenerationError::Timeout) => { + tracing::info!(id, "presignature msg already timed out") + } Err(presignature::GenerationError::TripleIsMissing(_)) => { // Store the message until we are ready to process it leftover_messages.push(message) diff --git a/node/src/protocol/presignature.rs b/node/src/protocol/presignature.rs index 6a482ce07..158c8d546 100644 --- a/node/src/protocol/presignature.rs +++ b/node/src/protocol/presignature.rs @@ -12,6 +12,7 @@ use near_lake_primitives::AccountId; use std::collections::hash_map::Entry; use std::collections::{HashMap, HashSet, VecDeque}; use std::time::Instant; +use std::time::Duration; /// Unique number used to identify a specific ongoing presignature generation protocol. /// Without `PresignatureId` it would be unclear where to route incoming cait-sith presignature @@ -86,6 +87,8 @@ pub enum GenerationError { CaitSithInitializationError(#[from] InitializationError), #[error("datastore storage error: {0}")] DatastoreStorageError(#[from] DatastoreStorageError), + #[error("presignature message timed out")] + Timeout } /// Abstracts how triples are generated by providing a way to request a new triple that will be @@ -102,6 +105,7 @@ pub struct PresignatureManager { /// The set of presignatures that were already taken. This will be maintained for at most /// presignature timeout period just so messages are cycled through the system. taken: HashMap, + requested: HashMap, me: Participant, threshold: usize, @@ -124,6 +128,7 @@ impl PresignatureManager { mine: VecDeque::new(), introduced: HashSet::new(), taken: HashMap::new(), + requested: HashMap::new(), me, threshold, epoch, @@ -158,6 +163,11 @@ impl PresignatureManager { .retain(|_, instant| instant.elapsed() < crate::types::TAKEN_TIMEOUT); } + pub fn clear_requested(&mut self) { + self.requested + .retain(|_, instant| instant.elapsed() < crate::types::TAKEN_TIMEOUT); + } + #[allow(clippy::too_many_arguments)] fn generate_internal( participants: &Participants, @@ -305,11 +315,19 @@ impl PresignatureManager { ) -> Result<&mut PresignatureProtocol, GenerationError> { if self.presignatures.contains_key(&id) || self.taken.contains_key(&id) { Err(GenerationError::AlreadyGenerated) + } else if self.requested.contains_key(&id) && self.requested.get(&id).unwrap().elapsed() > Duration::from_secs(300) { + Err(GenerationError::Timeout) } else { match self.generators.entry(id) { Entry::Vacant(entry) => { tracing::info!(id, "joining protocol to generate a new presignature"); - let (triple0, triple1) = match triple_manager.take_two(triple0, triple1).await { + if !self.requested.contains_key(&id) { + self.requested.insert(id, Instant::now()); + } + let (triple0, triple1) = match triple_manager + .take_two(triple0, triple1) + .await + { Ok(result) => result, Err(error) => { tracing::warn!( From 3e44db17335bd4dfb45c951a55ec163179839ef3 Mon Sep 17 00:00:00 2001 From: Xiangyi Zheng Date: Wed, 3 Apr 2024 15:33:42 -0700 Subject: [PATCH 2/5] fmt and timeout --- node/src/protocol/message.rs | 1 + node/src/protocol/presignature.rs | 21 +++++++++------------ node/src/types.rs | 3 +++ 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/node/src/protocol/message.rs b/node/src/protocol/message.rs index e45bf1feb..3afacb8cb 100644 --- a/node/src/protocol/message.rs +++ b/node/src/protocol/message.rs @@ -347,6 +347,7 @@ impl MessageHandler for RunningState { triple_manager.clear_failed_triples(); triple_manager.clear_taken(); presignature_manager.clear_taken(); + presignature_manager.clear_requested(); Ok(()) } } diff --git a/node/src/protocol/presignature.rs b/node/src/protocol/presignature.rs index 158c8d546..bf4413404 100644 --- a/node/src/protocol/presignature.rs +++ b/node/src/protocol/presignature.rs @@ -12,7 +12,6 @@ use near_lake_primitives::AccountId; use std::collections::hash_map::Entry; use std::collections::{HashMap, HashSet, VecDeque}; use std::time::Instant; -use std::time::Duration; /// Unique number used to identify a specific ongoing presignature generation protocol. /// Without `PresignatureId` it would be unclear where to route incoming cait-sith presignature @@ -88,7 +87,7 @@ pub enum GenerationError { #[error("datastore storage error: {0}")] DatastoreStorageError(#[from] DatastoreStorageError), #[error("presignature message timed out")] - Timeout + Timeout, } /// Abstracts how triples are generated by providing a way to request a new triple that will be @@ -105,8 +104,8 @@ pub struct PresignatureManager { /// The set of presignatures that were already taken. This will be maintained for at most /// presignature timeout period just so messages are cycled through the system. taken: HashMap, + /// The set of presignatures that were already requested to be generated. . requested: HashMap, - me: Participant, threshold: usize, epoch: u64, @@ -165,7 +164,7 @@ impl PresignatureManager { pub fn clear_requested(&mut self) { self.requested - .retain(|_, instant| instant.elapsed() < crate::types::TAKEN_TIMEOUT); + .retain(|_, instant| instant.elapsed() < crate::types::REQUESTED_TIMEOUT); } #[allow(clippy::too_many_arguments)] @@ -315,19 +314,17 @@ impl PresignatureManager { ) -> Result<&mut PresignatureProtocol, GenerationError> { if self.presignatures.contains_key(&id) || self.taken.contains_key(&id) { Err(GenerationError::AlreadyGenerated) - } else if self.requested.contains_key(&id) && self.requested.get(&id).unwrap().elapsed() > Duration::from_secs(300) { + } else if self.requested.contains_key(&id) + && self.requested.get(&id).unwrap().elapsed() > crate::types::PROTOCOL_PRESIG_TIMEOUT + { + tracing::info!(id, "presignature message timed out"); Err(GenerationError::Timeout) } else { match self.generators.entry(id) { Entry::Vacant(entry) => { tracing::info!(id, "joining protocol to generate a new presignature"); - if !self.requested.contains_key(&id) { - self.requested.insert(id, Instant::now()); - } - let (triple0, triple1) = match triple_manager - .take_two(triple0, triple1) - .await - { + self.requested.entry(id).or_insert_with(Instant::now); + let (triple0, triple1) = match triple_manager.take_two(triple0, triple1).await { Ok(result) => result, Err(error) => { tracing::warn!( diff --git a/node/src/types.rs b/node/src/types.rs index 4e8548e89..e1f920b1a 100644 --- a/node/src/types.rs +++ b/node/src/types.rs @@ -29,6 +29,9 @@ pub const FAILED_TRIPLES_TIMEOUT: Duration = Duration::from_secs(120 * 60); /// Default invalidation time for taken triples and presignatures. 120 mins pub const TAKEN_TIMEOUT: Duration = Duration::from_secs(120 * 60); +/// Default invalidation time for requested presignatures. 120 mins +pub const REQUESTED_TIMEOUT: Duration = Duration::from_secs(120 * 60); + pub type SecretKeyShare = ::Scalar; pub type PublicKey = ::AffinePoint; pub type TripleProtocol = From 6db3e6a14561ed03dd0ec7d82081a7d4186a1fde Mon Sep 17 00:00:00 2001 From: Xiangyi Zheng Date: Mon, 15 Apr 2024 16:12:31 -0700 Subject: [PATCH 3/5] add timestamp to messages --- Cargo.lock | 1 + node/Cargo.toml | 1 + node/src/protocol/message.rs | 24 +++++++++++--- node/src/protocol/presignature.rs | 55 +++++++++++++++++-------------- node/src/protocol/signature.rs | 3 ++ node/src/protocol/triple.rs | 15 +++++++-- node/src/util.rs | 13 ++++++++ 7 files changed, 82 insertions(+), 30 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a8c1fe7b6..5e82c9603 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4187,6 +4187,7 @@ dependencies = [ "axum", "axum-extra", "cait-sith", + "chrono", "clap 4.4.18", "google-datastore1", "google-secretmanager1", diff --git a/node/Cargo.toml b/node/Cargo.toml index 7889655d9..9d20eccda 100644 --- a/node/Cargo.toml +++ b/node/Cargo.toml @@ -19,6 +19,7 @@ cait-sith = { git = "https://github.com/LIT-Protocol/cait-sith.git", features = "k256", ], rev = "8ad2316"} clap = { version = "4.2", features = ["derive", "env"] } +chrono = "0.4.24" google-datastore1 = "5" google-secretmanager1 = "5" hex = "0.4.3" diff --git a/node/src/protocol/message.rs b/node/src/protocol/message.rs index 3afacb8cb..8bb52523b 100644 --- a/node/src/protocol/message.rs +++ b/node/src/protocol/message.rs @@ -5,6 +5,7 @@ use super::triple::TripleId; use crate::gcp::error::SecretStorageError; use crate::http_client::SendError; use crate::mesh::Mesh; +use crate::util; use async_trait::async_trait; use cait_sith::protocol::{InitializationError, MessageData, Participant, ProtocolError}; @@ -43,6 +44,8 @@ pub struct TripleMessage { pub epoch: u64, pub from: Participant, pub data: MessageData, + // UNIX timestamp as seconds since the epoch + pub timestamp: u64 } #[derive(Serialize, Deserialize, Debug, PartialEq, Eq)] @@ -53,6 +56,8 @@ pub struct PresignatureMessage { pub epoch: u64, pub from: Participant, pub data: MessageData, + // UNIX timestamp as seconds since the epoch + pub timestamp: u64 } #[derive(Serialize, Deserialize, Debug, PartialEq, Eq)] @@ -66,6 +71,8 @@ pub struct SignatureMessage { pub epoch: u64, pub from: Participant, pub data: MessageData, + // UNIX timestamp as seconds since the epoch + pub timestamp: u64 } #[derive(Serialize, Deserialize, Debug, PartialEq, Eq)] @@ -251,6 +258,11 @@ impl MessageHandler for RunningState { for (id, queue) in queue.presignature_bins.entry(self.epoch).or_default() { let mut leftover_messages = Vec::new(); while let Some(message) = queue.pop_front() { + // Skip message if it already timed out + if util::is_elapsed_longer_than_timeout(message.timestamp, crate::types::PROTOCOL_PRESIG_TIMEOUT) { + continue; + } + match presignature_manager .get_or_generate( participants, @@ -267,11 +279,12 @@ impl MessageHandler for RunningState { Err(presignature::GenerationError::AlreadyGenerated) => { tracing::info!(id, "presignature already generated, nothing left to do") } - Err(presignature::GenerationError::Timeout) => { - tracing::info!(id, "presignature msg already timed out") + Err(presignature::GenerationError::TripleIsGenerating(_)) => { + // Store the message until triple gets generated + leftover_messages.push(message) } Err(presignature::GenerationError::TripleIsMissing(_)) => { - // Store the message until we are ready to process it + // Store the message until triple is ready leftover_messages.push(message) } Err(presignature::GenerationError::CaitSithInitializationError(error)) => { @@ -296,6 +309,10 @@ impl MessageHandler for RunningState { for (receipt_id, queue) in queue.signature_bins.entry(self.epoch).or_default() { let mut leftover_messages = Vec::new(); while let Some(message) = queue.pop_front() { + // Skip message if it already timed out + if util::is_elapsed_longer_than_timeout(message.timestamp, crate::types::PROTOCOL_SIGNATURE_TIMEOUT) { + continue; + } tracing::info!( presignature_id = message.presignature_id, "new signature message" @@ -347,7 +364,6 @@ impl MessageHandler for RunningState { triple_manager.clear_failed_triples(); triple_manager.clear_taken(); presignature_manager.clear_taken(); - presignature_manager.clear_requested(); Ok(()) } } diff --git a/node/src/protocol/presignature.rs b/node/src/protocol/presignature.rs index bf4413404..3cce5c077 100644 --- a/node/src/protocol/presignature.rs +++ b/node/src/protocol/presignature.rs @@ -12,6 +12,7 @@ use near_lake_primitives::AccountId; use std::collections::hash_map::Entry; use std::collections::{HashMap, HashSet, VecDeque}; use std::time::Instant; +use chrono::Utc; /// Unique number used to identify a specific ongoing presignature generation protocol. /// Without `PresignatureId` it would be unclear where to route incoming cait-sith presignature @@ -86,8 +87,8 @@ pub enum GenerationError { CaitSithInitializationError(#[from] InitializationError), #[error("datastore storage error: {0}")] DatastoreStorageError(#[from] DatastoreStorageError), - #[error("presignature message timed out")] - Timeout, + #[error("triple {0} is generating")] + TripleIsGenerating(TripleId), } /// Abstracts how triples are generated by providing a way to request a new triple that will be @@ -104,8 +105,6 @@ pub struct PresignatureManager { /// The set of presignatures that were already taken. This will be maintained for at most /// presignature timeout period just so messages are cycled through the system. taken: HashMap, - /// The set of presignatures that were already requested to be generated. . - requested: HashMap, me: Participant, threshold: usize, epoch: u64, @@ -127,7 +126,6 @@ impl PresignatureManager { mine: VecDeque::new(), introduced: HashSet::new(), taken: HashMap::new(), - requested: HashMap::new(), me, threshold, epoch, @@ -162,11 +160,6 @@ impl PresignatureManager { .retain(|_, instant| instant.elapsed() < crate::types::TAKEN_TIMEOUT); } - pub fn clear_requested(&mut self) { - self.requested - .retain(|_, instant| instant.elapsed() < crate::types::REQUESTED_TIMEOUT); - } - #[allow(clippy::too_many_arguments)] fn generate_internal( participants: &Participants, @@ -314,27 +307,39 @@ impl PresignatureManager { ) -> Result<&mut PresignatureProtocol, GenerationError> { if self.presignatures.contains_key(&id) || self.taken.contains_key(&id) { Err(GenerationError::AlreadyGenerated) - } else if self.requested.contains_key(&id) - && self.requested.get(&id).unwrap().elapsed() > crate::types::PROTOCOL_PRESIG_TIMEOUT - { - tracing::info!(id, "presignature message timed out"); - Err(GenerationError::Timeout) } else { match self.generators.entry(id) { Entry::Vacant(entry) => { tracing::info!(id, "joining protocol to generate a new presignature"); - self.requested.entry(id).or_insert_with(Instant::now); let (triple0, triple1) = match triple_manager.take_two(triple0, triple1).await { Ok(result) => result, Err(error) => { - tracing::warn!( - ?error, - id, - triple0, - triple1, - "could not initiate non-introduced presignature: triple might not have completed for this node yet" - ); - return Err(error); + match error { + GenerationError::TripleIsGenerating(_) => { + tracing::warn!( + ?error, + id, + triple0, + triple1, + "could not initiate non-introduced presignature: one triple is generating" + ); + return Err(error); + } + GenerationError::TripleIsMissing(_) => { + tracing::warn!( + ?error, + id, + triple0, + triple1, + "could not initiate non-introduced presignature: one triple is missing" + ); + return Err(error); + } + _ => { + return Err(error); + } + } + } }; let generator = Self::generate_internal( @@ -411,6 +416,7 @@ impl PresignatureManager { epoch: self.epoch, from: self.me, data: data.clone(), + timestamp: Utc::now().timestamp() as u64 }, )) } @@ -424,6 +430,7 @@ impl PresignatureManager { epoch: self.epoch, from: self.me, data, + timestamp: Utc::now().timestamp() as u64 }, )), Action::Return(output) => { diff --git a/node/src/protocol/signature.rs b/node/src/protocol/signature.rs index 50118c5b5..9269c1732 100644 --- a/node/src/protocol/signature.rs +++ b/node/src/protocol/signature.rs @@ -18,6 +18,7 @@ use rand::SeedableRng; use std::collections::hash_map::Entry; use std::collections::{HashMap, VecDeque}; use std::time::{Duration, Instant}; +use chrono::Utc; /// Duration for which completed signatures are retained. pub const COMPLETION_EXISTENCE_TIMEOUT: Duration = Duration::from_secs(120 * 60); @@ -386,6 +387,7 @@ impl SignatureManager { epoch: self.epoch, from: self.me, data: data.clone(), + timestamp: Utc::now().timestamp() as u64 }, )) } @@ -402,6 +404,7 @@ impl SignatureManager { epoch: self.epoch, from: self.me, data, + timestamp: Utc::now().timestamp() as u64 }, )), Action::Return(output) => { diff --git a/node/src/protocol/triple.rs b/node/src/protocol/triple.rs index 37bbb8f75..0c858f654 100644 --- a/node/src/protocol/triple.rs +++ b/node/src/protocol/triple.rs @@ -17,6 +17,7 @@ use serde::{Deserialize, Serialize}; use std::collections::hash_map::Entry; use std::collections::{HashMap, HashSet, VecDeque}; use std::time::{Duration, Instant}; +use chrono::Utc; /// Unique number used to identify a specific ongoing triple generation protocol. /// Without `TripleId` it would be unclear where to route incoming cait-sith triple generation @@ -245,9 +246,17 @@ impl TripleManager { id1: TripleId, ) -> Result<(Triple, Triple), GenerationError> { if !self.triples.contains_key(&id0) { - Err(GenerationError::TripleIsMissing(id0)) + if self.generators.contains_key(&id0) { + Err(GenerationError::TripleIsGenerating(id0)) + } else { + Err(GenerationError::TripleIsMissing(id0)) + } } else if !self.triples.contains_key(&id1) { - Err(GenerationError::TripleIsMissing(id1)) + if self.generators.contains_key(&id1) { + Err(GenerationError::TripleIsGenerating(id1)) + } else { + Err(GenerationError::TripleIsMissing(id1)) + } } else { self.delete_triple_from_storage(id0).await?; self.delete_triple_from_storage(id1).await?; @@ -405,6 +414,7 @@ impl TripleManager { epoch: self.epoch, from: self.me, data: data.clone(), + timestamp: Utc::now().timestamp() as u64 }, )) } @@ -416,6 +426,7 @@ impl TripleManager { epoch: self.epoch, from: self.me, data, + timestamp: Utc::now().timestamp() as u64 }, )), Action::Return(output) => { diff --git a/node/src/util.rs b/node/src/util.rs index f8c870e81..867315f5d 100644 --- a/node/src/util.rs +++ b/node/src/util.rs @@ -4,6 +4,7 @@ use k256::elliptic_curve::sec1::{FromEncodedPoint, ToEncodedPoint}; use k256::{AffinePoint, EncodedPoint, Scalar, U256}; use std::env; use std::time::Duration; +use chrono::{DateTime, LocalResult, TimeZone, Utc}; pub trait NearPublicKeyExt { fn into_affine_point(self) -> PublicKey; @@ -83,3 +84,15 @@ pub fn get_triple_timeout() -> Duration { .unwrap_or_default() .unwrap_or(crate::types::PROTOCOL_TRIPLE_TIMEOUT) } + +pub fn is_elapsed_longer_than_timeout(timestamp_sec: u64, timeout: Duration) -> bool { + if let LocalResult::Single(msg_timestamp) = Utc.timestamp_opt(timestamp_sec as i64, 0) { + let now_datetime: DateTime = Utc::now(); + // Calculate the difference in seconds + let elapsed_duration = now_datetime.signed_duration_since(msg_timestamp); + let timeout = chrono::Duration::seconds(timeout.as_secs() as i64) + chrono::Duration::nanoseconds(timeout.subsec_nanos() as i64); + elapsed_duration > timeout + } else { + false + } +} From 566e3eb629cee156e7859e69a87b9b6fce424415 Mon Sep 17 00:00:00 2001 From: Xiangyi Zheng Date: Mon, 15 Apr 2024 16:15:13 -0700 Subject: [PATCH 4/5] fmt --- node/src/protocol/message.rs | 18 ++++++++++++------ node/src/protocol/presignature.rs | 29 +++++++++++++---------------- node/src/protocol/signature.rs | 2 +- node/src/protocol/triple.rs | 6 +++--- node/src/util.rs | 5 +++-- 5 files changed, 32 insertions(+), 28 deletions(-) diff --git a/node/src/protocol/message.rs b/node/src/protocol/message.rs index 8bb52523b..8460021c5 100644 --- a/node/src/protocol/message.rs +++ b/node/src/protocol/message.rs @@ -45,7 +45,7 @@ pub struct TripleMessage { pub from: Participant, pub data: MessageData, // UNIX timestamp as seconds since the epoch - pub timestamp: u64 + pub timestamp: u64, } #[derive(Serialize, Deserialize, Debug, PartialEq, Eq)] @@ -57,7 +57,7 @@ pub struct PresignatureMessage { pub from: Participant, pub data: MessageData, // UNIX timestamp as seconds since the epoch - pub timestamp: u64 + pub timestamp: u64, } #[derive(Serialize, Deserialize, Debug, PartialEq, Eq)] @@ -72,7 +72,7 @@ pub struct SignatureMessage { pub from: Participant, pub data: MessageData, // UNIX timestamp as seconds since the epoch - pub timestamp: u64 + pub timestamp: u64, } #[derive(Serialize, Deserialize, Debug, PartialEq, Eq)] @@ -259,10 +259,13 @@ impl MessageHandler for RunningState { let mut leftover_messages = Vec::new(); while let Some(message) = queue.pop_front() { // Skip message if it already timed out - if util::is_elapsed_longer_than_timeout(message.timestamp, crate::types::PROTOCOL_PRESIG_TIMEOUT) { + if util::is_elapsed_longer_than_timeout( + message.timestamp, + crate::types::PROTOCOL_PRESIG_TIMEOUT, + ) { continue; } - + match presignature_manager .get_or_generate( participants, @@ -310,7 +313,10 @@ impl MessageHandler for RunningState { let mut leftover_messages = Vec::new(); while let Some(message) = queue.pop_front() { // Skip message if it already timed out - if util::is_elapsed_longer_than_timeout(message.timestamp, crate::types::PROTOCOL_SIGNATURE_TIMEOUT) { + if util::is_elapsed_longer_than_timeout( + message.timestamp, + crate::types::PROTOCOL_SIGNATURE_TIMEOUT, + ) { continue; } tracing::info!( diff --git a/node/src/protocol/presignature.rs b/node/src/protocol/presignature.rs index 3cce5c077..f36fc575f 100644 --- a/node/src/protocol/presignature.rs +++ b/node/src/protocol/presignature.rs @@ -7,12 +7,12 @@ use crate::types::{PresignatureProtocol, PublicKey, SecretKeyShare}; use crate::util::AffinePointExt; use cait_sith::protocol::{Action, InitializationError, Participant, ProtocolError}; use cait_sith::{KeygenOutput, PresignArguments, PresignOutput}; +use chrono::Utc; use k256::Secp256k1; use near_lake_primitives::AccountId; use std::collections::hash_map::Entry; use std::collections::{HashMap, HashSet, VecDeque}; use std::time::Instant; -use chrono::Utc; /// Unique number used to identify a specific ongoing presignature generation protocol. /// Without `PresignatureId` it would be unclear where to route incoming cait-sith presignature @@ -313,34 +313,31 @@ impl PresignatureManager { tracing::info!(id, "joining protocol to generate a new presignature"); let (triple0, triple1) = match triple_manager.take_two(triple0, triple1).await { Ok(result) => result, - Err(error) => { - match error { - GenerationError::TripleIsGenerating(_) => { - tracing::warn!( + Err(error) => match error { + GenerationError::TripleIsGenerating(_) => { + tracing::warn!( ?error, id, triple0, triple1, "could not initiate non-introduced presignature: one triple is generating" ); - return Err(error); - } - GenerationError::TripleIsMissing(_) => { - tracing::warn!( + return Err(error); + } + GenerationError::TripleIsMissing(_) => { + tracing::warn!( ?error, id, triple0, triple1, "could not initiate non-introduced presignature: one triple is missing" ); - return Err(error); - } - _ => { - return Err(error); - } + return Err(error); } - - } + _ => { + return Err(error); + } + }, }; let generator = Self::generate_internal( participants, diff --git a/node/src/protocol/signature.rs b/node/src/protocol/signature.rs index 9269c1732..9fad671ec 100644 --- a/node/src/protocol/signature.rs +++ b/node/src/protocol/signature.rs @@ -6,6 +6,7 @@ use crate::types::{PublicKey, SignatureProtocol}; use crate::util::{AffinePointExt, ScalarExt}; use cait_sith::protocol::{Action, InitializationError, Participant, ProtocolError}; use cait_sith::{FullSignature, PresignOutput}; +use chrono::Utc; use k256::{Scalar, Secp256k1}; use near_crypto::Signer; use near_fetch::signer::ExposeAccountId; @@ -18,7 +19,6 @@ use rand::SeedableRng; use std::collections::hash_map::Entry; use std::collections::{HashMap, VecDeque}; use std::time::{Duration, Instant}; -use chrono::Utc; /// Duration for which completed signatures are retained. pub const COMPLETION_EXISTENCE_TIMEOUT: Duration = Duration::from_secs(120 * 60); diff --git a/node/src/protocol/triple.rs b/node/src/protocol/triple.rs index 0c858f654..57c42b940 100644 --- a/node/src/protocol/triple.rs +++ b/node/src/protocol/triple.rs @@ -9,6 +9,7 @@ use crate::types::TripleProtocol; use crate::util::AffinePointExt; use cait_sith::protocol::{Action, InitializationError, Participant, ProtocolError}; use cait_sith::triples::{TripleGenerationOutput, TriplePub, TripleShare}; +use chrono::Utc; use highway::{HighwayHash, HighwayHasher}; use k256::elliptic_curve::group::GroupEncoding; use k256::Secp256k1; @@ -17,7 +18,6 @@ use serde::{Deserialize, Serialize}; use std::collections::hash_map::Entry; use std::collections::{HashMap, HashSet, VecDeque}; use std::time::{Duration, Instant}; -use chrono::Utc; /// Unique number used to identify a specific ongoing triple generation protocol. /// Without `TripleId` it would be unclear where to route incoming cait-sith triple generation @@ -414,7 +414,7 @@ impl TripleManager { epoch: self.epoch, from: self.me, data: data.clone(), - timestamp: Utc::now().timestamp() as u64 + timestamp: Utc::now().timestamp() as u64, }, )) } @@ -426,7 +426,7 @@ impl TripleManager { epoch: self.epoch, from: self.me, data, - timestamp: Utc::now().timestamp() as u64 + timestamp: Utc::now().timestamp() as u64, }, )), Action::Return(output) => { diff --git a/node/src/util.rs b/node/src/util.rs index 867315f5d..bfbf27ee2 100644 --- a/node/src/util.rs +++ b/node/src/util.rs @@ -1,10 +1,10 @@ use crate::types::PublicKey; +use chrono::{DateTime, LocalResult, TimeZone, Utc}; use k256::elliptic_curve::scalar::FromUintUnchecked; use k256::elliptic_curve::sec1::{FromEncodedPoint, ToEncodedPoint}; use k256::{AffinePoint, EncodedPoint, Scalar, U256}; use std::env; use std::time::Duration; -use chrono::{DateTime, LocalResult, TimeZone, Utc}; pub trait NearPublicKeyExt { fn into_affine_point(self) -> PublicKey; @@ -90,7 +90,8 @@ pub fn is_elapsed_longer_than_timeout(timestamp_sec: u64, timeout: Duration) -> let now_datetime: DateTime = Utc::now(); // Calculate the difference in seconds let elapsed_duration = now_datetime.signed_duration_since(msg_timestamp); - let timeout = chrono::Duration::seconds(timeout.as_secs() as i64) + chrono::Duration::nanoseconds(timeout.subsec_nanos() as i64); + let timeout = chrono::Duration::seconds(timeout.as_secs() as i64) + + chrono::Duration::nanoseconds(timeout.subsec_nanos() as i64); elapsed_duration > timeout } else { false From c94a5e06fcb4a466c6392228da8151dd874f6ba8 Mon Sep 17 00:00:00 2001 From: Xiangyi Zheng Date: Mon, 15 Apr 2024 17:18:34 -0700 Subject: [PATCH 5/5] remove unnecessary code --- node/src/types.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/node/src/types.rs b/node/src/types.rs index e1f920b1a..4e8548e89 100644 --- a/node/src/types.rs +++ b/node/src/types.rs @@ -29,9 +29,6 @@ pub const FAILED_TRIPLES_TIMEOUT: Duration = Duration::from_secs(120 * 60); /// Default invalidation time for taken triples and presignatures. 120 mins pub const TAKEN_TIMEOUT: Duration = Duration::from_secs(120 * 60); -/// Default invalidation time for requested presignatures. 120 mins -pub const REQUESTED_TIMEOUT: Duration = Duration::from_secs(120 * 60); - pub type SecretKeyShare = ::Scalar; pub type PublicKey = ::AffinePoint; pub type TripleProtocol =