Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Config for web3signer keep-alive #5007

Merged
merged 8 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions book/src/help_vc.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,4 +209,9 @@ OPTIONS:
--validators-dir <VALIDATORS_DIR>
The directory which contains the validator keystores, deposit data for each validator along with the common
slashing protection database and the validator_definitions.yml
--web3-signer-keep-alive-timeout <MILLIS>
Keep-alive timeout for each web3signer connection. Set to 'null' to never timeout [default: 90000]

--web3-signer-max-idle-connections <COUNT>
Maximum number of idle connections to maintain per web3signer host. Default is unlimited.
```
3 changes: 2 additions & 1 deletion testing/web3signer_tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,10 +301,12 @@ mod tests {
let log = environment::null_logger().unwrap();
let validator_dir = TempDir::new().unwrap();

let config = validator_client::Config::default();
let validator_definitions = ValidatorDefinitions::from(validator_definitions);
let initialized_validators = InitializedValidators::from_definitions(
validator_definitions,
validator_dir.path().into(),
config.clone(),
log.clone(),
)
.await
Expand All @@ -331,7 +333,6 @@ mod tests {

let slot_clock =
TestingSlotClock::new(Slot::new(0), Duration::from_secs(0), Duration::from_secs(1));
let config = validator_client::Config::default();

let validator_store = ValidatorStore::<_, E>::new(
initialized_validators,
Expand Down
20 changes: 20 additions & 0 deletions validator_client/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,4 +367,24 @@ pub fn cli_app<'a, 'b>() -> App<'a, 'b> {
constructed by builders, regardless of payload value.")
.takes_value(false),
)
/*
* Experimental/development options.
*/
.arg(
Arg::with_name("web3-signer-keep-alive-timeout")
.long("web3-signer-keep-alive-timeout")
.value_name("MILLIS")
.default_value("90000")
.help("Keep-alive timeout for each web3signer connection. Set to 'null' to never \
timeout")
.takes_value(true),
)
.arg(
Arg::with_name("web3-signer-max-idle-connections")
.long("web3-signer-max-idle-connections")
.value_name("COUNT")
.help("Maximum number of idle connections to maintain per web3signer host. Default \
is unlimited.")
.takes_value(true),
)
}
21 changes: 21 additions & 0 deletions validator_client/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use slog::{info, warn, Logger};
use std::fs;
use std::net::IpAddr;
use std::path::PathBuf;
use std::time::Duration;
use types::{Address, GRAFFITI_BYTES_LEN};

pub const DEFAULT_BEACON_NODE: &str = "http://localhost:5052/";
Expand Down Expand Up @@ -81,6 +82,8 @@ pub struct Config {
pub builder_boost_factor: Option<u64>,
/// If true, Lighthouse will prefer builder proposals, if available.
pub prefer_builder_proposals: bool,
pub web3_signer_keep_alive_timeout: Option<Duration>,
pub web3_signer_max_idle_connections: Option<usize>,
}

impl Default for Config {
Expand Down Expand Up @@ -124,6 +127,8 @@ impl Default for Config {
produce_block_v3: false,
builder_boost_factor: None,
prefer_builder_proposals: false,
web3_signer_keep_alive_timeout: Some(Duration::from_secs(90)),
web3_signer_max_idle_connections: None,
}
}
}
Expand Down Expand Up @@ -245,6 +250,22 @@ impl Config {
.collect::<Result<_, _>>()?;
}

/*
* Web3 signer
*/
if let Some(s) = parse_optional::<String>(cli_args, "web3-signer-keep-alive-timeout")? {
config.web3_signer_keep_alive_timeout = if s == "null" {
None
} else {
Some(Duration::from_millis(
s.parse().map_err(|_| "invalid timeout value".to_string())?,
))
}
}
if let Some(n) = parse_optional::<usize>(cli_args, "web3-signer-max-idle-connections")? {
config.web3_signer_max_idle_connections = Some(n);
}

/*
* Http API server
*/
Expand Down
1 change: 1 addition & 0 deletions validator_client/src/http_api/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ impl ApiTester {
let initialized_validators = InitializedValidators::from_definitions(
validator_defs,
validator_dir.path().into(),
Default::default(),
log.clone(),
)
.await
Expand Down
1 change: 1 addition & 0 deletions validator_client/src/http_api/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ impl ApiTester {
let initialized_validators = InitializedValidators::from_definitions(
validator_defs,
validator_dir.path().into(),
Config::default(),
log.clone(),
)
.await
Expand Down
18 changes: 17 additions & 1 deletion validator_client/src/initialized_validators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ use validator_dir::Builder as ValidatorDirBuilder;

use crate::key_cache;
use crate::key_cache::KeyCache;
use crate::Config;

/// Default timeout for a request to a remote signer for a signature.
///
Expand Down Expand Up @@ -208,6 +209,7 @@ impl InitializedValidator {
key_cache: &mut KeyCache,
key_stores: &mut HashMap<PathBuf, Keystore>,
web3_signer_client_map: &mut Option<HashMap<Web3SignerDefinition, Client>>,
config: &Config,
) -> Result<Self, Error> {
if !def.enabled {
return Err(Error::UnableToInitializeDisabledValidator);
Expand Down Expand Up @@ -311,6 +313,8 @@ impl InitializedValidator {
web3_signer.client_identity_path.clone(),
web3_signer.client_identity_password.clone(),
request_timeout,
config.web3_signer_keep_alive_timeout,
config.web3_signer_max_idle_connections,
)?;
client_map.insert(web3_signer, client.clone());
client
Expand All @@ -325,6 +329,8 @@ impl InitializedValidator {
web3_signer.client_identity_path.clone(),
web3_signer.client_identity_password.clone(),
request_timeout,
config.web3_signer_keep_alive_timeout,
config.web3_signer_max_idle_connections,
)?;
new_web3_signer_client_map.insert(web3_signer, client.clone());
*web3_signer_client_map = Some(new_web3_signer_client_map);
Expand Down Expand Up @@ -393,8 +399,13 @@ fn build_web3_signer_client(
client_identity_path: Option<PathBuf>,
client_identity_password: Option<String>,
request_timeout: Duration,
keep_alive_timeout: Option<Duration>,
max_idle_connections: Option<usize>,
) -> Result<Client, Error> {
let builder = Client::builder().timeout(request_timeout);
let builder = Client::builder()
.timeout(request_timeout)
.pool_idle_timeout(keep_alive_timeout)
.pool_max_idle_per_host(max_idle_connections.unwrap_or(usize::MAX));

let builder = if let Some(path) = root_certificate_path {
let certificate = load_pem_certificate(path)?;
Expand Down Expand Up @@ -475,20 +486,23 @@ pub struct InitializedValidators {
web3_signer_client_map: Option<HashMap<Web3SignerDefinition, Client>>,
/// For logging via `slog`.
log: Logger,
config: Config,
}

impl InitializedValidators {
/// Instantiates `Self`, initializing all validators in `definitions`.
pub async fn from_definitions(
definitions: ValidatorDefinitions,
validators_dir: PathBuf,
config: Config,
log: Logger,
) -> Result<Self, Error> {
let mut this = Self {
validators_dir,
definitions,
validators: HashMap::default(),
web3_signer_client_map: None,
config,
log,
};
this.update_validators().await?;
Expand Down Expand Up @@ -1234,6 +1248,7 @@ impl InitializedValidators {
&mut key_cache,
&mut key_stores,
&mut None,
&self.config,
)
.await
{
Expand Down Expand Up @@ -1284,6 +1299,7 @@ impl InitializedValidators {
&mut key_cache,
&mut key_stores,
&mut self.web3_signer_client_map,
&self.config,
)
.await
{
Expand Down
1 change: 1 addition & 0 deletions validator_client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ impl<T: EthSpec> ProductionValidatorClient<T> {
let validators = InitializedValidators::from_definitions(
validator_defs,
config.validator_dir.clone(),
config.clone(),
log.clone(),
)
.await
Expand Down
Loading