Skip to content

Commit

Permalink
Make polling config less frequent (#760)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChaoticTempest authored Jul 30, 2024
1 parent 6dc7704 commit 401ba83
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 16 deletions.
12 changes: 12 additions & 0 deletions chain-signatures/node/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::str::FromStr;

use mpc_contract::config::ProtocolConfig;
use mpc_keys::hpke;
use near_account_id::AccountId;
use serde::{Deserialize, Serialize};
use serde_json::Value;

Expand Down Expand Up @@ -47,6 +48,17 @@ impl Config {
local: original.local.clone(),
})
}

/// Fetches the latest config from the contract and set the config inplace. The old config
/// is returned when swap is completed.
pub async fn fetch_inplace(
&mut self,
rpc_client: &near_fetch::Client,
contract_id: &AccountId,
) -> anyhow::Result<Self> {
let new_config = crate::rpc_client::fetch_mpc_config(rpc_client, contract_id, self).await?;
Ok(std::mem::replace(self, new_config))
}
}

/// All the local configurations on a node that are not accessible by anyone else
Expand Down
41 changes: 25 additions & 16 deletions chain-signatures/node/src/protocol/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,19 @@ impl MpcSignProtocol {
.set(node_version());
let mut queue = MpcMessageQueue::default();
let mut last_state_update = Instant::now();
let mut last_config_update = Instant::now();
let mut last_pinged = Instant::now();

// Sets the latest configurations from the contract:
if let Err(err) = self
.ctx
.cfg
.fetch_inplace(&self.ctx.rpc_client, &self.ctx.mpc_contract_id)
.await
{
tracing::warn!("could not fetch contract's config on startup: {err:?}");
}

loop {
let protocol_time = Instant::now();
tracing::debug!("trying to advance chain signatures protocol");
Expand Down Expand Up @@ -237,22 +249,6 @@ impl MpcSignProtocol {
};
tracing::debug!(?contract_state);

// Sets the latest configurations from the contract:
self.ctx.cfg = match rpc_client::fetch_mpc_config(
&self.ctx.rpc_client,
&self.ctx.mpc_contract_id,
&self.ctx.cfg,
)
.await
{
Ok(config) => config,
Err(e) => {
tracing::error!("could not fetch contract's config: {e}");
tokio::time::sleep(Duration::from_secs(1)).await;
continue;
}
};

// Establish the participants for this current iteration of the protocol loop. This will
// set which participants are currently active in the protocol and determines who will be
// receiving messages.
Expand All @@ -264,6 +260,19 @@ impl MpcSignProtocol {
None
};

if last_config_update.elapsed() > Duration::from_secs(5 * 60) {
// Sets the latest configurations from the contract:
if let Err(err) = self
.ctx
.cfg
.fetch_inplace(&self.ctx.rpc_client, &self.ctx.mpc_contract_id)
.await
{
tracing::warn!("could not fetch contract's config: {err:?}");
}
last_config_update = Instant::now();
}

if last_pinged.elapsed() > Duration::from_millis(300) {
self.ctx.mesh.ping().await;
last_pinged = Instant::now();
Expand Down

0 comments on commit 401ba83

Please sign in to comment.