Skip to content

Commit

Permalink
refactor: UpdatableValidatorSigner (#12857)
Browse files Browse the repository at this point in the history
  • Loading branch information
staffik authored Feb 3, 2025
1 parent 4f20d61 commit 6c774bc
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 14 deletions.
6 changes: 4 additions & 2 deletions chain/client/src/config_updater.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use near_chain_configs::UpdatableClientConfig;
use near_dyn_configs::{UpdatableConfigLoaderError, UpdatableConfigs};
use near_dyn_configs::{UpdatableConfigLoaderError, UpdatableConfigs, UpdatableValidatorSigner};
use near_primitives::validator_signer::ValidatorSigner;
use std::sync::Arc;
use tokio::sync::broadcast::Receiver;
Expand Down Expand Up @@ -44,7 +44,9 @@ impl ConfigUpdater {
update_client_config_fn(client_config);
tracing::info!(target: "config", "Updated ClientConfig");
}
if let Some(validator_signer) = updatable_configs.validator_signer {
if let UpdatableValidatorSigner::MaybeKey(validator_signer) =
updatable_configs.validator_signer
{
update_result.validator_signer_updated |=
update_validator_signer_fn(validator_signer);
tracing::info!(target: "config", "Updated validator key");
Expand Down
4 changes: 1 addition & 3 deletions core/chain-configs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ pub use genesis_config::{
};
use near_primitives::types::{Balance, BlockHeightDelta, Gas, NumBlocks, NumSeats};
use num_rational::Rational32;
pub use updatable_config::{
MutableConfigValue, MutableValidatorSigner, UpdatableClientConfig, UpdatableValidatorSigner,
};
pub use updatable_config::{MutableConfigValue, MutableValidatorSigner, UpdatableClientConfig};

pub const GENESIS_CONFIG_FILENAME: &str = "genesis.json";

Expand Down
1 change: 0 additions & 1 deletion core/chain-configs/src/updatable_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,5 +110,4 @@ pub struct UpdatableClientConfig {
pub produce_chunk_add_transactions_time_limit: Option<Duration>,
}

pub type UpdatableValidatorSigner = Option<Arc<ValidatorSigner>>;
pub type MutableValidatorSigner = MutableConfigValue<Option<Arc<ValidatorSigner>>>;
16 changes: 12 additions & 4 deletions core/dyn-configs/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
#![doc = include_str!("../README.md")]

use near_chain_configs::{UpdatableClientConfig, UpdatableValidatorSigner};
use near_chain_configs::UpdatableClientConfig;
use near_o11y::log_config::LogConfig;
use near_primitives::validator_signer::ValidatorSigner;
use near_time::Clock;
use std::path::PathBuf;
use std::sync::Arc;
use tokio::sync::broadcast::Sender;

mod metrics;

#[derive(Clone, Default)]
pub enum UpdatableValidatorSigner {
/// Validator key existence could not be determined.
#[default]
KeyExistenceNotDetermined,
/// The new state of the validator key.
MaybeKey(Option<Arc<ValidatorSigner>>),
}

#[derive(Clone, Default)]
/// Contains the latest state of configs which can be updated at runtime.
pub struct UpdatableConfigs {
Expand All @@ -17,9 +27,7 @@ pub struct UpdatableConfigs {
/// Contents of the `config.json` corresponding to the mutable fields of `ClientConfig`.
pub client_config: Option<UpdatableClientConfig>,
/// Validator key hot loaded from file.
/// `None` means that the validator key existence could not be determined.
/// `Some(None)` means that it was determined that the validator key does not exist.
pub validator_signer: Option<UpdatableValidatorSigner>,
pub validator_signer: UpdatableValidatorSigner,
}

/// Pushes the updates to listeners.
Expand Down
8 changes: 4 additions & 4 deletions nearcore/src/dyn_config.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::config::Config;
use near_chain_configs::UpdatableClientConfig;
use near_dyn_configs::{UpdatableConfigLoaderError, UpdatableConfigs};
use near_dyn_configs::{UpdatableConfigLoaderError, UpdatableConfigs, UpdatableValidatorSigner};
use near_o11y::log_config::LogConfig;
use near_primitives::validator_signer::ValidatorSigner;
use serde::Deserialize;
Expand Down Expand Up @@ -35,14 +35,14 @@ pub fn read_updatable_configs(

let validator_signer = if let Some(config) = config {
match read_validator_key(home_dir, &config) {
Ok(validator_key) => Some(validator_key),
Ok(validator_key) => UpdatableValidatorSigner::MaybeKey(validator_key),
Err(err) => {
errs.push(err);
None
UpdatableValidatorSigner::KeyExistenceNotDetermined
}
}
} else {
None
UpdatableValidatorSigner::KeyExistenceNotDetermined
};

if errs.is_empty() {
Expand Down

0 comments on commit 6c774bc

Please sign in to comment.