Skip to content

Commit 1817d88

Browse files
committed
making nym-api aware of the changes
1 parent d579b39 commit 1817d88

File tree

6 files changed

+140
-122
lines changed

6 files changed

+140
-122
lines changed

common/client-libs/validator-client/src/nyxd/contract_traits/dkg_query_client.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,18 @@ use crate::nyxd::error::NyxdError;
77
use crate::nyxd::CosmWasmClient;
88
use async_trait::async_trait;
99
use cosmrs::AccountId;
10-
use nym_coconut_dkg_common::dealer::{
10+
use serde::Deserialize;
11+
12+
pub use nym_coconut_dkg_common::dealer::{
1113
DealerDetailsResponse, DealingResponse, DealingStatusResponse, PagedDealerResponse,
1214
PagedDealingsResponse,
1315
};
14-
use nym_coconut_dkg_common::msg::QueryMsg as DkgQueryMsg;
15-
use nym_coconut_dkg_common::types::{
16-
DealerDetails, DealingIndex, Epoch, EpochId, InitialReplacementData, PartialContractDealing,
17-
State,
16+
pub use nym_coconut_dkg_common::msg::QueryMsg as DkgQueryMsg;
17+
pub use nym_coconut_dkg_common::types::{
18+
DealerDetails, DealingIndex, Epoch, EpochId, EpochState, InitialReplacementData,
19+
PartialContractDealing, State,
1820
};
19-
use nym_coconut_dkg_common::verification_key::{ContractVKShare, PagedVKSharesResponse};
20-
use serde::Deserialize;
21+
pub use nym_coconut_dkg_common::verification_key::{ContractVKShare, PagedVKSharesResponse};
2122

2223
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
2324
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]

common/client-libs/validator-client/src/nyxd/contract_traits/mod.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,26 @@ use std::str::FromStr;
88
// TODO: all of those could/should be derived via a macro
99

1010
// query clients
11-
mod coconut_bandwidth_query_client;
12-
mod dkg_query_client;
13-
mod ephemera_query_client;
14-
mod group_query_client;
15-
mod mixnet_query_client;
16-
mod multisig_query_client;
17-
mod name_service_query_client;
18-
mod sp_directory_query_client;
19-
mod vesting_query_client;
11+
pub mod coconut_bandwidth_query_client;
12+
pub mod dkg_query_client;
13+
pub mod ephemera_query_client;
14+
pub mod group_query_client;
15+
pub mod mixnet_query_client;
16+
pub mod multisig_query_client;
17+
pub mod name_service_query_client;
18+
pub mod sp_directory_query_client;
19+
pub mod vesting_query_client;
2020

2121
// signing clients
22-
mod coconut_bandwidth_signing_client;
23-
mod dkg_signing_client;
24-
mod ephemera_signing_client;
25-
mod group_signing_client;
26-
mod mixnet_signing_client;
27-
mod multisig_signing_client;
28-
mod name_service_signing_client;
29-
mod sp_directory_signing_client;
30-
mod vesting_signing_client;
22+
pub mod coconut_bandwidth_signing_client;
23+
pub mod dkg_signing_client;
24+
pub mod ephemera_signing_client;
25+
pub mod group_signing_client;
26+
pub mod mixnet_signing_client;
27+
pub mod multisig_signing_client;
28+
pub mod name_service_signing_client;
29+
pub mod sp_directory_signing_client;
30+
pub mod vesting_signing_client;
3131

3232
// re-export query traits
3333
pub use coconut_bandwidth_query_client::{

common/credential-utils/src/utils.rs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ use nym_bandwidth_controller::acquire::state::State;
55
use nym_client_core::config::disk_persistence::CommonClientPaths;
66
use nym_config::DEFAULT_DATA_DIR;
77
use nym_credential_storage::persistent_storage::PersistentStorage;
8-
use nym_validator_client::nyxd::contract_traits::{CoconutBandwidthSigningClient, DkgQueryClient};
8+
use nym_validator_client::nyxd::contract_traits::{
9+
dkg_query_client::EpochState, CoconutBandwidthSigningClient, DkgQueryClient,
10+
};
911
use nym_validator_client::nyxd::Coin;
1012
use std::path::PathBuf;
1113
use std::process::exit;
@@ -87,21 +89,29 @@ where
8789
.duration_since(SystemTime::UNIX_EPOCH)
8890
.expect("the system clock is set to 01/01/1970 (or earlier)")
8991
.as_secs();
92+
9093
if epoch.state.is_final() {
91-
if current_timestamp_secs + SAFETY_BUFFER_SECS >= epoch.finish_timestamp.seconds() {
92-
info!("In the next {} minute(s), a transition will take place in the coconut system. Deposits should be halted in this time for safety reasons.", SAFETY_BUFFER_SECS / 60);
93-
exit(0);
94+
if let Some(finish_timestamp) = epoch.finish_timestamp {
95+
if current_timestamp_secs + SAFETY_BUFFER_SECS >= finish_timestamp.seconds() {
96+
info!("In the next {} minute(s), a transition will take place in the coconut system. Deposits should be halted in this time for safety reasons.", SAFETY_BUFFER_SECS / 60);
97+
exit(0);
98+
}
9499
}
95100

96101
break;
97-
} else {
102+
} else if let Some(final_timestamp) = epoch.final_timestamp_secs() {
98103
// Use 1 additional second to not start the next iteration immediately and spam get_current_epoch queries
99-
let secs_until_final = epoch
100-
.final_timestamp_secs()
101-
.saturating_sub(current_timestamp_secs)
102-
+ 1;
104+
let secs_until_final = final_timestamp.saturating_sub(current_timestamp_secs) + 1;
103105
info!("Approximately {} seconds until coconut is available. Sleeping until then. You can safely kill the process at any moment.", secs_until_final);
104106
tokio::time::sleep(Duration::from_secs(secs_until_final)).await;
107+
} else if matches!(epoch.state, EpochState::WaitingInitialisation) {
108+
info!("dkg hasn't been initialised yet and it is not known when it will be. Going to check again later");
109+
tokio::time::sleep(Duration::from_secs(60 * 5)).await;
110+
} else {
111+
// this should never be the case since the only case where final timestamp is unknown is when it's waiting for initialisation,
112+
// but let's guard ourselves against future changes
113+
info!("it is unknown when coconut will be come available. Going to check again later");
114+
tokio::time::sleep(Duration::from_secs(60 * 5)).await;
105115
}
106116
}
107117

nym-api/src/coconut/comm.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,18 @@ impl CachedEpoch {
4242

4343
async fn update(&mut self, epoch: Epoch) -> Result<()> {
4444
let now = OffsetDateTime::now_utc();
45-
let state_end =
46-
OffsetDateTime::from_unix_timestamp(epoch.finish_timestamp.seconds() as i64).unwrap();
47-
let until_epoch_state_end = state_end - now;
4845

49-
// make it valid until the next epoch transition or next 5min, whichever is smaller
50-
self.valid_until = now + min(until_epoch_state_end, 5 * time::Duration::MINUTE);
46+
let validity_duration = if let Some(epoch_finish) = epoch.finish_timestamp {
47+
let state_end =
48+
OffsetDateTime::from_unix_timestamp(epoch_finish.seconds() as i64).unwrap();
49+
let until_epoch_state_end = state_end - now;
50+
// make it valid until the next epoch transition or next 5min, whichever is smaller
51+
min(until_epoch_state_end, 5 * time::Duration::MINUTE)
52+
} else {
53+
5 * time::Duration::MINUTE
54+
};
55+
56+
self.valid_until = now + validity_duration;
5157
self.current_epoch_id = epoch.epoch_id;
5258

5359
Ok(())

nym-api/src/coconut/dkg/controller.rs

Lines changed: 83 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -105,89 +105,89 @@ impl<R: RngCore + CryptoRng + Clone> DkgController<R> {
105105
}
106106

107107
pub(crate) async fn handle_epoch_state(&mut self) {
108-
match self.dkg_client.get_current_epoch().await {
109-
Err(err) => warn!("Could not get current epoch state {err}"),
110-
Ok(epoch) => {
111-
if self
112-
.dkg_client
113-
.group_member()
114-
.await
115-
.map(|resp| resp.weight.is_none())
116-
.unwrap_or(true)
117-
{
118-
debug!("Not a member of the group, DKG won't be run");
119-
return;
120-
}
121-
if let Err(err) = self.state.is_consistent(epoch.state).await {
122-
debug!("Epoch state is corrupted - {err}. Awaiting for a DKG restart.");
123-
} else {
124-
let ret = match epoch.state {
125-
EpochState::PublicKeySubmission { resharing } => {
126-
public_key_submission(&self.dkg_client, &mut self.state, resharing)
127-
.await
128-
}
129-
EpochState::DealingExchange { resharing } => {
130-
dealing_exchange(
131-
&self.dkg_client,
132-
&mut self.state,
133-
self.rng.clone(),
134-
resharing,
135-
)
136-
.await
137-
}
138-
EpochState::VerificationKeySubmission { resharing } => {
139-
let keypair_path = nym_pemstore::KeyPairPath::new(
140-
self.secret_key_path.clone(),
141-
self.verification_key_path.clone(),
142-
);
143-
verification_key_submission(
144-
&self.dkg_client,
145-
&mut self.state,
146-
epoch.epoch_id,
147-
&keypair_path,
148-
resharing,
149-
)
150-
.await
151-
}
152-
EpochState::VerificationKeyValidation { resharing } => {
153-
verification_key_validation(
154-
&self.dkg_client,
155-
&mut self.state,
156-
resharing,
157-
)
158-
.await
159-
}
160-
EpochState::VerificationKeyFinalization { resharing } => {
161-
verification_key_finalization(
162-
&self.dkg_client,
163-
&mut self.state,
164-
resharing,
165-
)
166-
.await
167-
}
168-
// Just wait, in case we need to redo dkg at some point
169-
EpochState::InProgress => {
170-
self.state.set_was_in_progress();
171-
// We're dumping state here so that we don't do it uselessly during the
172-
// long InProgress state
173-
self.dump_persistent_state().await;
174-
Ok(())
175-
}
176-
};
177-
if let Err(err) = ret {
178-
warn!("Could not handle this iteration for the epoch state: {err}");
179-
} else if epoch.state != EpochState::InProgress {
180-
self.dump_persistent_state().await;
181-
}
182-
}
183-
if let Ok(current_timestamp) =
184-
SystemTime::now().duration_since(SystemTime::UNIX_EPOCH)
185-
{
186-
if current_timestamp.as_secs() >= epoch.finish_timestamp.seconds() {
187-
// We try advancing the epoch state, on a best-effort basis
188-
info!("DKG: Trying to advance the epoch");
189-
self.dkg_client.advance_epoch_state().await.ok();
190-
}
108+
let epoch = match self.dkg_client.get_current_epoch().await {
109+
Err(err) => {
110+
warn!("Could not get current epoch state {err}");
111+
return;
112+
}
113+
Ok(epoch) => epoch,
114+
};
115+
116+
if self
117+
.dkg_client
118+
.group_member()
119+
.await
120+
.map(|resp| resp.weight.is_none())
121+
.unwrap_or(true)
122+
{
123+
debug!("Not a member of the group, DKG won't be run");
124+
return;
125+
}
126+
if let Err(err) = self.state.is_consistent(epoch.state).await {
127+
warn!("Epoch state is corrupted - {err}. Awaiting for a DKG restart.");
128+
return;
129+
}
130+
131+
let ret = match epoch.state {
132+
EpochState::WaitingInitialisation => {
133+
info!("DKG hasn't been initialised yet");
134+
return;
135+
}
136+
EpochState::PublicKeySubmission { resharing } => {
137+
public_key_submission(&self.dkg_client, &mut self.state, resharing).await
138+
}
139+
EpochState::DealingExchange { resharing } => {
140+
dealing_exchange(
141+
&self.dkg_client,
142+
&mut self.state,
143+
self.rng.clone(),
144+
resharing,
145+
)
146+
.await
147+
}
148+
EpochState::VerificationKeySubmission { resharing } => {
149+
let keypair_path = nym_pemstore::KeyPairPath::new(
150+
self.secret_key_path.clone(),
151+
self.verification_key_path.clone(),
152+
);
153+
verification_key_submission(
154+
&self.dkg_client,
155+
&mut self.state,
156+
epoch.epoch_id,
157+
&keypair_path,
158+
resharing,
159+
)
160+
.await
161+
}
162+
EpochState::VerificationKeyValidation { resharing } => {
163+
verification_key_validation(&self.dkg_client, &mut self.state, resharing).await
164+
}
165+
EpochState::VerificationKeyFinalization { resharing } => {
166+
verification_key_finalization(&self.dkg_client, &mut self.state, resharing).await
167+
}
168+
// Just wait, in case we need to redo dkg at some point
169+
EpochState::InProgress => {
170+
self.state.set_was_in_progress();
171+
// We're dumping state here so that we don't do it uselessly during the
172+
// long InProgress state
173+
self.dump_persistent_state().await;
174+
Ok(())
175+
}
176+
};
177+
if let Err(err) = ret {
178+
warn!("Could not handle this iteration for the epoch state: {err}");
179+
} else if epoch.state != EpochState::InProgress
180+
&& epoch.state != EpochState::WaitingInitialisation
181+
{
182+
self.dump_persistent_state().await;
183+
}
184+
185+
if let Ok(current_timestamp) = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH) {
186+
if let Some(epoch_finish) = epoch.finish_timestamp {
187+
if current_timestamp.as_secs() >= epoch_finish.seconds() {
188+
// We try advancing the epoch state, on a best-effort basis
189+
info!("DKG: Trying to advance the epoch");
190+
self.dkg_client.advance_epoch_state().await.ok();
191191
}
192192
}
193193
}

nym-api/src/coconut/dkg/state.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ pub(crate) trait ConsistentState {
7373
fn proposal_id_value(&self) -> Result<u64, CoconutError>;
7474
async fn is_consistent(&self, epoch_state: EpochState) -> Result<(), CoconutError> {
7575
match epoch_state {
76+
EpochState::WaitingInitialisation => {}
7677
EpochState::PublicKeySubmission { .. } => {}
7778
EpochState::DealingExchange { .. } => {
7879
self.node_index_value()?;

0 commit comments

Comments
 (0)