2024-01-03 10:24:52 +00:00
|
|
|
#![allow(clippy::field_reassign_with_default)]
|
|
|
|
|
|
|
|
use crate::ZgsConfig;
|
2024-04-11 03:52:03 +00:00
|
|
|
use ethereum_types::{H256, U256};
|
2024-08-14 03:35:48 +00:00
|
|
|
use ethers::prelude::{Http, Middleware, Provider};
|
2024-01-03 10:24:52 +00:00
|
|
|
use log_entry_sync::{CacheConfig, ContractAddress, LogSyncConfig};
|
2024-05-31 05:11:06 +00:00
|
|
|
use miner::MinerConfig;
|
2024-01-03 10:24:52 +00:00
|
|
|
use network::NetworkConfig;
|
2024-05-31 05:11:06 +00:00
|
|
|
use pruner::PrunerConfig;
|
2024-01-03 10:24:52 +00:00
|
|
|
use rpc::RPCConfig;
|
2024-08-29 01:55:24 +00:00
|
|
|
use shared_types::{NetworkIdentity, ProtocolVersion};
|
2024-07-09 09:39:14 +00:00
|
|
|
use std::net::IpAddr;
|
2024-05-31 05:11:06 +00:00
|
|
|
use std::time::Duration;
|
|
|
|
use storage::config::ShardConfig;
|
2024-01-03 10:24:52 +00:00
|
|
|
use storage::StorageConfig;
|
|
|
|
|
|
|
|
impl ZgsConfig {
|
2024-07-09 09:39:14 +00:00
|
|
|
pub async fn network_config(&self) -> Result<NetworkConfig, String> {
|
2024-01-03 10:24:52 +00:00
|
|
|
let mut network_config = NetworkConfig::default();
|
|
|
|
|
|
|
|
network_config.listen_address = self
|
|
|
|
.network_listen_address
|
|
|
|
.parse::<std::net::IpAddr>()
|
|
|
|
.map_err(|e| format!("Unable to parse network_listen_address: {:?}", e))?;
|
|
|
|
|
|
|
|
network_config.network_dir = self.network_dir.clone().into();
|
|
|
|
network_config.libp2p_port = self.network_libp2p_port;
|
|
|
|
network_config.disable_discovery = self.network_disable_discovery;
|
|
|
|
network_config.discovery_port = self.network_discovery_port;
|
2024-08-14 03:35:48 +00:00
|
|
|
let flow_address = self
|
|
|
|
.log_contract_address
|
|
|
|
.parse::<ContractAddress>()
|
|
|
|
.map_err(|e| format!("Unable to parse log_contract_address: {:?}", e))?;
|
|
|
|
let provider = Provider::<Http>::try_from(&self.blockchain_rpc_endpoint)
|
|
|
|
.map_err(|e| format!("Can not parse blockchain endpoint: {:?}", e))?;
|
|
|
|
let chain_id = provider
|
|
|
|
.get_chainid()
|
|
|
|
.await
|
|
|
|
.map_err(|e| format!("Unable to get chain id: {:?}", e))?
|
|
|
|
.as_u64();
|
|
|
|
network_config.network_id = NetworkIdentity {
|
|
|
|
chain_id,
|
|
|
|
flow_address,
|
2024-08-29 01:55:24 +00:00
|
|
|
p2p_protocol_version: ProtocolVersion {
|
|
|
|
major: network::PROTOCOL_VERSION[0],
|
|
|
|
minor: network::PROTOCOL_VERSION[1],
|
|
|
|
build: network::PROTOCOL_VERSION[2],
|
|
|
|
},
|
2024-08-14 03:35:48 +00:00
|
|
|
};
|
2024-06-27 06:47:33 +00:00
|
|
|
|
2024-07-09 09:39:14 +00:00
|
|
|
if !self.network_disable_discovery {
|
2024-06-27 06:47:33 +00:00
|
|
|
network_config.enr_tcp_port = Some(self.network_enr_tcp_port);
|
|
|
|
network_config.enr_udp_port = Some(self.network_enr_udp_port);
|
2024-07-09 09:39:14 +00:00
|
|
|
network_config.enr_address = match &self.network_enr_address {
|
|
|
|
Some(addr) => Some(addr.parse().unwrap()),
|
|
|
|
None => match public_ip::addr_v4().await {
|
|
|
|
Some(ipv4_addr) => {
|
|
|
|
info!(?ipv4_addr, "Auto detect public IP as ENR address");
|
|
|
|
Some(IpAddr::V4(ipv4_addr))
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
return Err(
|
|
|
|
"ENR address not configured and failed to detect public IP address"
|
|
|
|
.into(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
2024-06-27 06:47:33 +00:00
|
|
|
}
|
2024-01-03 10:24:52 +00:00
|
|
|
|
|
|
|
network_config.boot_nodes_multiaddr = self
|
|
|
|
.network_boot_nodes
|
|
|
|
.iter()
|
|
|
|
.map(|addr| addr.parse::<libp2p::Multiaddr>())
|
|
|
|
.collect::<Result<_, _>>()
|
|
|
|
.map_err(|e| format!("Unable to parse network_boot_nodes: {:?}", e))?;
|
|
|
|
|
|
|
|
network_config.libp2p_nodes = self
|
|
|
|
.network_libp2p_nodes
|
|
|
|
.iter()
|
|
|
|
.map(|addr| addr.parse::<libp2p::Multiaddr>())
|
|
|
|
.collect::<Result<_, _>>()
|
|
|
|
.map_err(|e| format!("Unable to parse network_libp2p_nodes: {:?}", e))?;
|
|
|
|
|
|
|
|
network_config.discv5_config.table_filter = |_| true;
|
2024-06-13 08:14:20 +00:00
|
|
|
network_config.discv5_config.request_timeout =
|
|
|
|
Duration::from_secs(self.discv5_request_timeout_secs);
|
|
|
|
network_config.discv5_config.query_peer_timeout =
|
|
|
|
Duration::from_secs(self.discv5_query_peer_timeout_secs);
|
|
|
|
network_config.discv5_config.request_retries = self.discv5_request_retries;
|
|
|
|
network_config.discv5_config.query_parallelism = self.discv5_query_parallelism;
|
|
|
|
network_config.discv5_config.report_discovered_peers = self.discv5_report_discovered_peers;
|
2024-06-14 10:04:35 +00:00
|
|
|
network_config.discv5_config.enable_packet_filter = !self.discv5_disable_packet_filter;
|
|
|
|
network_config.discv5_config.ip_limit = !self.discv5_disable_ip_limit;
|
2024-01-03 10:24:52 +00:00
|
|
|
|
|
|
|
network_config.target_peers = self.network_target_peers;
|
|
|
|
network_config.private = self.network_private;
|
|
|
|
|
|
|
|
Ok(network_config)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn storage_config(&self) -> Result<StorageConfig, String> {
|
|
|
|
Ok(StorageConfig {
|
|
|
|
db_dir: self.db_dir.clone().into(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn rpc_config(&self) -> Result<RPCConfig, String> {
|
|
|
|
let listen_address = self
|
|
|
|
.rpc_listen_address
|
|
|
|
.parse::<std::net::SocketAddr>()
|
|
|
|
.map_err(|e| format!("Unable to parse rpc_listen_address: {:?}", e))?;
|
|
|
|
|
2024-01-30 08:50:35 +00:00
|
|
|
let listen_address_admin = if self.rpc_listen_address_admin.is_empty() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(
|
|
|
|
self.rpc_listen_address_admin
|
|
|
|
.parse::<std::net::SocketAddr>()
|
|
|
|
.map_err(|e| format!("Unable to parse rpc_listen_address_admin: {:?}", e))?,
|
|
|
|
)
|
|
|
|
};
|
|
|
|
|
2024-01-03 10:24:52 +00:00
|
|
|
Ok(RPCConfig {
|
|
|
|
enabled: self.rpc_enabled,
|
|
|
|
listen_address,
|
2024-01-30 08:50:35 +00:00
|
|
|
listen_address_admin,
|
2024-02-06 09:51:31 +00:00
|
|
|
max_request_body_size: self.max_request_body_size,
|
2024-01-03 10:24:52 +00:00
|
|
|
chunks_per_segment: self.rpc_chunks_per_segment,
|
|
|
|
max_cache_file_size: self.rpc_max_cache_file_size,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn log_sync_config(&self) -> Result<LogSyncConfig, String> {
|
|
|
|
let contract_address = self
|
|
|
|
.log_contract_address
|
|
|
|
.parse::<ContractAddress>()
|
|
|
|
.map_err(|e| format!("Unable to parse log_contract_address: {:?}", e))?;
|
|
|
|
let cache_config = CacheConfig {
|
|
|
|
// 100 MB.
|
|
|
|
max_data_size: self.max_cache_data_size,
|
|
|
|
// This should be enough if we have about one Zgs tx per block.
|
|
|
|
tx_seq_ttl: self.cache_tx_seq_ttl,
|
|
|
|
};
|
|
|
|
Ok(LogSyncConfig::new(
|
|
|
|
self.blockchain_rpc_endpoint.clone(),
|
|
|
|
contract_address,
|
|
|
|
self.log_sync_start_block_number,
|
|
|
|
self.confirmation_block_count,
|
|
|
|
cache_config,
|
|
|
|
self.log_page_size,
|
|
|
|
self.rate_limit_retries,
|
|
|
|
self.timeout_retries,
|
|
|
|
self.initial_backoff,
|
|
|
|
self.recover_query_delay,
|
2024-03-27 05:54:06 +00:00
|
|
|
self.default_finalized_block_count,
|
|
|
|
self.remove_finalized_block_interval_minutes,
|
|
|
|
self.watch_loop_wait_time_ms,
|
2024-09-08 00:34:59 +00:00
|
|
|
self.force_log_sync_from_start_block_number,
|
2024-01-03 10:24:52 +00:00
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn mine_config(&self) -> Result<Option<MinerConfig>, String> {
|
|
|
|
let flow_address = self
|
|
|
|
.log_contract_address
|
|
|
|
.parse::<ContractAddress>()
|
|
|
|
.map_err(|e| format!("Unable to parse log_contract_address: {:?}", e))?;
|
|
|
|
let mine_address = self
|
|
|
|
.mine_contract_address
|
|
|
|
.parse::<ContractAddress>()
|
|
|
|
.map_err(|e| format!("Unable to parse mine_address: {:?}", e))?;
|
|
|
|
|
|
|
|
let miner_id = if let Some(ref miner_id) = self.miner_id {
|
|
|
|
Some(
|
|
|
|
miner_id
|
|
|
|
.parse::<H256>()
|
|
|
|
.map_err(|e| format!("Unable to parse miner_id: {:?}", e))?,
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
let miner_key = if let Some(ref miner_key) = self.miner_key {
|
|
|
|
Some(
|
|
|
|
miner_key
|
|
|
|
.parse::<H256>()
|
|
|
|
.map_err(|e| format!("Unable to parse miner_key: {:?}", e))?,
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2024-04-11 03:52:03 +00:00
|
|
|
let submission_gas = self.miner_submission_gas.map(U256::from);
|
2024-04-23 04:52:39 +00:00
|
|
|
let cpu_percentage = self.miner_cpu_percentage;
|
|
|
|
let iter_batch = self.mine_iter_batch_size;
|
2024-08-27 09:36:36 +00:00
|
|
|
let context_query_seconds = self.mine_context_query_seconds;
|
2024-04-30 06:34:44 +00:00
|
|
|
|
2024-06-07 18:50:36 +00:00
|
|
|
let shard_config = self.shard_config()?;
|
2024-04-30 06:34:44 +00:00
|
|
|
|
2024-01-03 10:24:52 +00:00
|
|
|
Ok(MinerConfig::new(
|
|
|
|
miner_id,
|
|
|
|
miner_key,
|
|
|
|
self.blockchain_rpc_endpoint.clone(),
|
|
|
|
mine_address,
|
|
|
|
flow_address,
|
2024-04-11 03:52:03 +00:00
|
|
|
submission_gas,
|
2024-04-23 04:52:39 +00:00
|
|
|
cpu_percentage,
|
|
|
|
iter_batch,
|
2024-08-27 09:36:36 +00:00
|
|
|
context_query_seconds,
|
2024-04-30 06:34:44 +00:00
|
|
|
shard_config,
|
2024-08-12 03:35:21 +00:00
|
|
|
self.rate_limit_retries,
|
|
|
|
self.timeout_retries,
|
|
|
|
self.initial_backoff,
|
2024-01-03 10:24:52 +00:00
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2024-06-07 18:50:36 +00:00
|
|
|
pub fn chunk_pool_config(&self) -> Result<chunk_pool::Config, String> {
|
|
|
|
Ok(chunk_pool::Config {
|
2024-01-03 10:24:52 +00:00
|
|
|
write_window_size: self.chunk_pool_write_window_size,
|
|
|
|
max_cached_chunks_all: self.chunk_pool_max_cached_chunks_all,
|
|
|
|
max_writings: self.chunk_pool_max_writings,
|
|
|
|
expiration_time_secs: self.chunk_pool_expiration_time_secs,
|
2024-06-07 18:50:36 +00:00
|
|
|
shard_config: self.shard_config()?,
|
|
|
|
})
|
2024-01-03 10:24:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn router_config(&self, network_config: &NetworkConfig) -> Result<router::Config, String> {
|
2024-07-08 10:45:55 +00:00
|
|
|
let mut router_config = self.router.clone();
|
2024-01-03 10:24:52 +00:00
|
|
|
router_config.libp2p_nodes = network_config.libp2p_nodes.to_vec();
|
|
|
|
Ok(router_config)
|
|
|
|
}
|
2024-05-31 05:11:06 +00:00
|
|
|
|
|
|
|
pub fn pruner_config(&self) -> Result<Option<PrunerConfig>, String> {
|
2024-07-29 14:31:19 +00:00
|
|
|
if let Some(max_num_sectors) = self.db_max_num_sectors {
|
2024-06-07 18:50:36 +00:00
|
|
|
let shard_config = self.shard_config()?;
|
2024-08-06 07:06:15 +00:00
|
|
|
let reward_address = self
|
|
|
|
.reward_contract_address
|
|
|
|
.parse::<ContractAddress>()
|
|
|
|
.map_err(|e| format!("Unable to parse reward_contract_address: {:?}", e))?;
|
2024-05-31 05:11:06 +00:00
|
|
|
Ok(Some(PrunerConfig {
|
|
|
|
shard_config,
|
|
|
|
db_path: self.db_dir.clone().into(),
|
2024-07-29 14:31:19 +00:00
|
|
|
max_num_sectors,
|
2024-05-31 05:11:06 +00:00
|
|
|
check_time: Duration::from_secs(self.prune_check_time_s),
|
|
|
|
batch_size: self.prune_batch_size,
|
|
|
|
batch_wait_time: Duration::from_millis(self.prune_batch_wait_time_ms),
|
2024-08-06 07:06:15 +00:00
|
|
|
rpc_endpoint_url: self.blockchain_rpc_endpoint.clone(),
|
|
|
|
reward_address,
|
2024-08-12 03:35:21 +00:00
|
|
|
rate_limit_retries: self.rate_limit_retries,
|
|
|
|
timeout_retries: self.timeout_retries,
|
|
|
|
initial_backoff: self.initial_backoff,
|
2024-05-31 05:11:06 +00:00
|
|
|
}))
|
|
|
|
} else {
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
}
|
2024-06-07 18:50:36 +00:00
|
|
|
|
|
|
|
fn shard_config(&self) -> Result<ShardConfig, String> {
|
2024-08-29 10:35:35 +00:00
|
|
|
self.shard_position.clone().try_into()
|
2024-06-07 18:50:36 +00:00
|
|
|
}
|
2024-01-03 10:24:52 +00:00
|
|
|
}
|