2026-07-14 13:10:32 +03:00
|
|
|
#![expect(
|
|
|
|
|
clippy::float_arithmetic,
|
|
|
|
|
reason = "One should expect floating point arithmetic in statistic calculations"
|
|
|
|
|
)]
|
|
|
|
|
#![expect(
|
|
|
|
|
clippy::cast_precision_loss,
|
|
|
|
|
reason = "Operated numbers is not big enough to have precision loss"
|
|
|
|
|
)]
|
|
|
|
|
|
2026-07-17 16:57:12 +03:00
|
|
|
use std::{collections::HashMap, path::Path, sync::Arc};
|
2026-07-08 14:18:52 +03:00
|
|
|
|
2026-07-14 13:10:32 +03:00
|
|
|
use anyhow::{Context as _, Result};
|
2026-07-24 13:46:45 +03:00
|
|
|
use common::{HashType, transaction::LeeTransaction};
|
2026-07-27 13:24:56 +03:00
|
|
|
use itertools::Itertools as _;
|
2026-07-17 16:57:12 +03:00
|
|
|
use lee_core::BlockId;
|
2026-07-14 13:10:32 +03:00
|
|
|
use sequencer_service_rpc::{RpcClient as _, SequencerClient, SequencerClientBuilder};
|
2026-07-07 15:14:49 +03:00
|
|
|
use serde::{Deserialize, Serialize};
|
2026-07-24 13:46:45 +03:00
|
|
|
use tokio::{sync::RwLock, task::JoinSet};
|
2026-07-08 14:18:52 +03:00
|
|
|
use url::Url;
|
2026-07-07 15:14:49 +03:00
|
|
|
|
2026-07-24 13:46:45 +03:00
|
|
|
use crate::{
|
|
|
|
|
ExecutionFailureKind,
|
|
|
|
|
config::{MultiSequencerClientConfig, SequencerConnectionData},
|
|
|
|
|
};
|
2026-07-07 15:14:49 +03:00
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
2026-07-17 16:57:12 +03:00
|
|
|
pub struct Statistics {
|
2026-07-08 14:18:52 +03:00
|
|
|
pub latency_avg: f32,
|
|
|
|
|
pub latency_var: f32,
|
|
|
|
|
pub sample_size: usize,
|
2026-07-17 16:57:12 +03:00
|
|
|
pub latest_block_id: BlockId,
|
2026-07-08 14:18:52 +03:00
|
|
|
pub errors: u64,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
2026-07-22 13:21:27 +03:00
|
|
|
pub enum StatisticsUpdate {
|
|
|
|
|
Success {
|
|
|
|
|
latency: f32,
|
|
|
|
|
new_latest_block_id: BlockId,
|
|
|
|
|
},
|
|
|
|
|
Failure,
|
2026-07-21 18:01:04 +03:00
|
|
|
}
|
|
|
|
|
|
2026-07-17 16:57:12 +03:00
|
|
|
impl Statistics {
|
|
|
|
|
pub fn apply_updates(&mut self, updates: &[StatisticsUpdate]) {
|
2026-07-13 15:37:25 +03:00
|
|
|
let CumulativeUpdates {
|
|
|
|
|
failure_count,
|
|
|
|
|
latest_block_id,
|
|
|
|
|
cumulative_latency,
|
|
|
|
|
cumulative_latency_squares,
|
|
|
|
|
additional_sample_size,
|
2026-07-14 13:10:32 +03:00
|
|
|
} = CumulativeUpdates::from_metric_updates(updates);
|
2026-07-13 15:37:25 +03:00
|
|
|
|
2026-07-14 13:10:32 +03:00
|
|
|
self.errors = self.errors.saturating_add(failure_count);
|
2026-07-13 15:37:25 +03:00
|
|
|
if let Some(latest_block_id) = latest_block_id {
|
|
|
|
|
self.latest_block_id = latest_block_id;
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-14 13:10:32 +03:00
|
|
|
#[expect(clippy::as_conversions, reason = "int to float conversion is safe")]
|
2026-07-13 15:37:25 +03:00
|
|
|
let orig_size_f = self.sample_size as f32;
|
2026-07-14 13:10:32 +03:00
|
|
|
#[expect(clippy::as_conversions, reason = "int to float conversion is safe")]
|
2026-07-13 15:37:25 +03:00
|
|
|
let mod_size_f = additional_sample_size as f32;
|
|
|
|
|
|
|
|
|
|
let latency_avg_old = self.latency_avg;
|
|
|
|
|
let latency_avg_new =
|
|
|
|
|
cumulative_avg(latency_avg_old, cumulative_latency, orig_size_f, mod_size_f);
|
|
|
|
|
|
|
|
|
|
let latency_var_new = cumulative_var(
|
|
|
|
|
latency_avg_old,
|
|
|
|
|
latency_avg_new,
|
|
|
|
|
self.latency_var,
|
|
|
|
|
cumulative_latency,
|
|
|
|
|
cumulative_latency_squares,
|
|
|
|
|
orig_size_f,
|
|
|
|
|
mod_size_f,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
self.latency_avg = latency_avg_new;
|
|
|
|
|
self.latency_var = latency_var_new;
|
2026-07-14 13:10:32 +03:00
|
|
|
self.sample_size = self.sample_size.saturating_add(additional_sample_size);
|
2026-07-13 15:37:25 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-07 15:14:49 +03:00
|
|
|
#[derive(Clone)]
|
|
|
|
|
pub struct MultiSequencerClient {
|
2026-07-20 17:10:01 +03:00
|
|
|
/// Ordered list of leaders, from best to worst.
|
2026-07-21 18:01:04 +03:00
|
|
|
leader_list: Vec<(SequencerClient, Url)>,
|
|
|
|
|
config: MultiSequencerClientConfig,
|
2026-07-17 16:57:12 +03:00
|
|
|
/// Wrapping statistic updates in Arc<RwLock> to not break interfaces too much.
|
|
|
|
|
///
|
|
|
|
|
/// It is assumed, that wallet methods can be accesed via immutable reference.
|
2026-07-20 17:10:01 +03:00
|
|
|
statistic_updates: Arc<RwLock<HashMap<Url, Vec<StatisticsUpdate>>>>,
|
2026-07-07 15:14:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl MultiSequencerClient {
|
2026-07-17 17:31:11 +03:00
|
|
|
async fn setup(
|
2026-07-08 14:18:52 +03:00
|
|
|
conn_data: &[SequencerConnectionData],
|
2026-07-17 16:57:12 +03:00
|
|
|
statistics: &mut HashMap<Url, Statistics>,
|
2026-07-20 17:10:01 +03:00
|
|
|
multi_sequencer_client_config: &MultiSequencerClientConfig,
|
|
|
|
|
) -> Result<Vec<(SequencerClient, Url)>> {
|
2026-07-27 13:24:56 +03:00
|
|
|
if !conn_data
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|conn| &conn.sequencer_addr)
|
|
|
|
|
.all_unique()
|
|
|
|
|
{
|
2026-07-28 07:55:43 +03:00
|
|
|
anyhow::bail!("All addresses must be unique");
|
2026-07-27 13:24:56 +03:00
|
|
|
}
|
|
|
|
|
|
2026-07-23 14:56:18 +03:00
|
|
|
let mut actualization_list = vec![];
|
|
|
|
|
let mut calibration_list = vec![];
|
|
|
|
|
let mut client_list = vec![];
|
2026-07-07 15:14:49 +03:00
|
|
|
|
|
|
|
|
for SequencerConnectionData {
|
|
|
|
|
sequencer_addr,
|
|
|
|
|
basic_auth,
|
|
|
|
|
} in conn_data
|
|
|
|
|
{
|
|
|
|
|
let sequencer_client = {
|
|
|
|
|
let mut builder = SequencerClientBuilder::default();
|
|
|
|
|
if let Some(basic_auth) = &basic_auth {
|
|
|
|
|
builder = builder.set_headers(
|
|
|
|
|
std::iter::once((
|
|
|
|
|
"Authorization".parse().expect("Header name is valid"),
|
|
|
|
|
format!("Basic {basic_auth}")
|
|
|
|
|
.parse()
|
|
|
|
|
.context("Invalid basic auth format")?,
|
|
|
|
|
))
|
|
|
|
|
.collect(),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
builder
|
|
|
|
|
.build(sequencer_addr)
|
|
|
|
|
.context("Failed to create sequencer client")?
|
|
|
|
|
};
|
|
|
|
|
|
2026-07-23 14:56:18 +03:00
|
|
|
if statistics.contains_key(sequencer_addr) {
|
|
|
|
|
actualization_list.push((sequencer_addr.clone(), sequencer_client.clone()));
|
|
|
|
|
} else {
|
|
|
|
|
calibration_list.push((sequencer_addr.clone(), sequencer_client.clone()));
|
|
|
|
|
}
|
2026-07-14 13:10:32 +03:00
|
|
|
|
2026-07-23 14:56:18 +03:00
|
|
|
client_list.push((sequencer_addr.clone(), sequencer_client));
|
|
|
|
|
}
|
2026-07-20 17:10:01 +03:00
|
|
|
|
2026-07-27 13:24:56 +03:00
|
|
|
// This actually starts in one thread, but each exact future produces more tasks.
|
2026-07-23 14:56:18 +03:00
|
|
|
let (actualization_res, callibration_res) = tokio::join!(
|
2026-07-24 13:46:45 +03:00
|
|
|
multi_actualize_clients(actualization_list),
|
2026-07-23 14:56:18 +03:00
|
|
|
multi_calibrate_clients(
|
2026-07-24 13:46:45 +03:00
|
|
|
calibration_list,
|
2026-07-23 14:56:18 +03:00
|
|
|
multi_sequencer_client_config.calibration_limit
|
2026-07-21 18:01:04 +03:00
|
|
|
)
|
2026-07-23 14:56:18 +03:00
|
|
|
);
|
|
|
|
|
|
2026-07-24 13:46:45 +03:00
|
|
|
for (addr, statistic_opt) in callibration_res {
|
|
|
|
|
if let Some(statistic) = statistic_opt {
|
|
|
|
|
statistics.insert(addr, statistic);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-23 14:56:18 +03:00
|
|
|
for (addr, statistic_update_opt) in actualization_res {
|
|
|
|
|
if let Some(statistic_mut) = statistics.get_mut(&addr)
|
|
|
|
|
&& let Some(statistic_update) = statistic_update_opt
|
2026-07-17 16:57:12 +03:00
|
|
|
{
|
2026-07-23 14:56:18 +03:00
|
|
|
statistic_mut.apply_updates(&[statistic_update]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-15 16:36:45 +03:00
|
|
|
let leader_list = choose_leaders(
|
2026-07-24 13:46:45 +03:00
|
|
|
client_list,
|
2026-07-20 17:10:01 +03:00
|
|
|
statistics,
|
2026-07-15 16:36:45 +03:00
|
|
|
multi_sequencer_client_config.distribution_limit,
|
|
|
|
|
)
|
|
|
|
|
.ok_or_else(|| anyhow::anyhow!("Failed to find leader"))?;
|
2026-07-14 13:10:32 +03:00
|
|
|
|
2026-07-24 08:02:03 +03:00
|
|
|
if leader_list.is_empty() {
|
|
|
|
|
anyhow::bail!(
|
|
|
|
|
"Leader search algorithm failure: Sorted out all clients during leader search"
|
|
|
|
|
);
|
|
|
|
|
}
|
2026-07-21 18:01:04 +03:00
|
|
|
|
2026-07-24 13:46:45 +03:00
|
|
|
log::info!(
|
|
|
|
|
"Chosen leaders is {:?}",
|
|
|
|
|
leader_list.iter().map(|(_, addr)| addr).collect::<Vec<_>>()
|
|
|
|
|
);
|
2026-07-08 14:18:52 +03:00
|
|
|
|
2026-07-20 17:10:01 +03:00
|
|
|
Ok(leader_list)
|
2026-07-08 14:18:52 +03:00
|
|
|
}
|
|
|
|
|
|
2026-07-17 17:31:11 +03:00
|
|
|
pub async fn new(
|
|
|
|
|
conn_data: &[SequencerConnectionData],
|
|
|
|
|
statistics: &mut HashMap<Url, Statistics>,
|
2026-07-20 17:10:01 +03:00
|
|
|
multi_sequencer_client_config: MultiSequencerClientConfig,
|
2026-07-17 17:31:11 +03:00
|
|
|
) -> Result<Self> {
|
2026-07-21 18:01:04 +03:00
|
|
|
let leader_list =
|
|
|
|
|
Self::setup(conn_data, statistics, &multi_sequencer_client_config).await?;
|
2026-07-08 14:18:52 +03:00
|
|
|
|
2026-07-17 16:57:12 +03:00
|
|
|
Ok(Self {
|
2026-07-20 17:10:01 +03:00
|
|
|
leader_list,
|
2026-07-21 18:01:04 +03:00
|
|
|
config: multi_sequencer_client_config,
|
2026-07-20 17:10:01 +03:00
|
|
|
statistic_updates: Arc::new(RwLock::new(HashMap::new())),
|
2026-07-17 16:57:12 +03:00
|
|
|
})
|
2026-07-08 14:18:52 +03:00
|
|
|
}
|
|
|
|
|
|
2026-07-17 17:31:11 +03:00
|
|
|
/// Re-choose leader, `statistic_updates` must be empty.
|
|
|
|
|
pub async fn rotate(
|
|
|
|
|
&mut self,
|
|
|
|
|
conn_data: &[SequencerConnectionData],
|
|
|
|
|
statistics: &mut HashMap<Url, Statistics>,
|
2026-07-20 17:10:01 +03:00
|
|
|
multi_sequencer_client_config: &MultiSequencerClientConfig,
|
2026-07-17 17:31:11 +03:00
|
|
|
) -> Result<()> {
|
2026-07-20 17:10:01 +03:00
|
|
|
let leader_list = Self::setup(conn_data, statistics, multi_sequencer_client_config).await?;
|
2026-07-17 17:31:11 +03:00
|
|
|
|
2026-07-24 13:46:45 +03:00
|
|
|
log::info!(
|
|
|
|
|
"Chosen leaders is {:?}",
|
|
|
|
|
leader_list.iter().map(|(_, addr)| addr).collect::<Vec<_>>()
|
|
|
|
|
);
|
2026-07-17 17:31:11 +03:00
|
|
|
|
2026-07-20 17:10:01 +03:00
|
|
|
self.leader_list = leader_list;
|
2026-07-17 17:31:11 +03:00
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-14 13:10:32 +03:00
|
|
|
#[must_use]
|
2026-07-20 17:10:01 +03:00
|
|
|
pub fn leaders(&self) -> &[(SequencerClient, Url)] {
|
2026-07-21 18:01:04 +03:00
|
|
|
self.leader_list.as_ref()
|
2026-07-08 14:18:52 +03:00
|
|
|
}
|
|
|
|
|
|
2026-07-14 13:10:32 +03:00
|
|
|
#[must_use]
|
2026-07-20 17:10:01 +03:00
|
|
|
pub fn helm(&self) -> &(SequencerClient, Url) {
|
2026-07-21 18:01:04 +03:00
|
|
|
self.leader_list
|
|
|
|
|
.first()
|
|
|
|
|
.expect("At least one leader must be set")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[must_use]
|
|
|
|
|
pub const fn config(&self) -> &MultiSequencerClientConfig {
|
|
|
|
|
&self.config
|
2026-07-08 14:18:52 +03:00
|
|
|
}
|
|
|
|
|
|
2026-07-22 10:13:01 +03:00
|
|
|
/// Helperfunction for the `metered_get`.
|
|
|
|
|
async fn metered_get_helper<R, E, I: AsyncFn(&SequencerClient) -> Result<R, E>>(
|
|
|
|
|
&self,
|
|
|
|
|
call: &I,
|
|
|
|
|
leader: &SequencerClient,
|
|
|
|
|
leader_url: &Url,
|
|
|
|
|
statistic_map: &mut HashMap<Url, Vec<StatisticsUpdate>>,
|
|
|
|
|
) -> Result<R, E> {
|
2026-07-23 14:56:18 +03:00
|
|
|
let (resp, statistics_update) =
|
|
|
|
|
tokio::join!(call(leader), actualize_client(leader.clone()));
|
2026-07-22 10:13:01 +03:00
|
|
|
|
|
|
|
|
log::debug!("Metered call for {leader_url:?}, statistic updates is {statistics_update:?}",);
|
|
|
|
|
|
|
|
|
|
statistic_map
|
|
|
|
|
.entry(leader_url.clone())
|
|
|
|
|
.or_default()
|
|
|
|
|
.push(statistics_update);
|
|
|
|
|
|
|
|
|
|
resp
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-20 17:10:01 +03:00
|
|
|
/// Metered call for main leader(helm), to get data, necessary for send call.
|
2026-07-21 18:01:04 +03:00
|
|
|
///
|
2026-07-23 08:30:05 +03:00
|
|
|
/// If current leader errors, we ask next one in list up to a `self.config.distribution_limit`.
|
2026-07-21 18:01:04 +03:00
|
|
|
pub async fn metered_get<R, E, I: AsyncFn(&SequencerClient) -> Result<R, E>>(
|
2026-07-08 14:18:52 +03:00
|
|
|
&self,
|
|
|
|
|
call: I,
|
2026-07-17 16:57:12 +03:00
|
|
|
) -> Result<R, E> {
|
2026-07-21 18:01:04 +03:00
|
|
|
// Collecting all statistics into one map to lock updates only once
|
|
|
|
|
let mut statistic_map: HashMap<Url, Vec<StatisticsUpdate>> = HashMap::new();
|
|
|
|
|
|
2026-07-22 10:47:37 +03:00
|
|
|
// We need helm response to avoid constructing guard Result<R, E>, which we can not do with
|
|
|
|
|
// generics.
|
2026-07-20 17:10:01 +03:00
|
|
|
let (helm, helm_url) = self.helm();
|
|
|
|
|
|
2026-07-22 10:13:01 +03:00
|
|
|
let mut resp = self
|
|
|
|
|
.metered_get_helper(&call, helm, helm_url, &mut statistic_map)
|
|
|
|
|
.await;
|
2026-07-21 18:01:04 +03:00
|
|
|
|
2026-07-22 10:13:01 +03:00
|
|
|
// Not the cleanest approach, but I am not sure how to have it both clean and async.
|
2026-07-21 18:01:04 +03:00
|
|
|
if resp.is_err() {
|
|
|
|
|
for (leader, leader_url) in self.leaders().iter().skip(1) {
|
2026-07-22 10:13:01 +03:00
|
|
|
resp = self
|
|
|
|
|
.metered_get_helper(&call, leader, leader_url, &mut statistic_map)
|
|
|
|
|
.await;
|
2026-07-21 18:01:04 +03:00
|
|
|
|
2026-07-23 08:30:05 +03:00
|
|
|
if resp.is_ok() {
|
2026-07-21 18:01:04 +03:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-07-14 13:10:32 +03:00
|
|
|
|
2026-07-17 16:57:12 +03:00
|
|
|
{
|
|
|
|
|
let mut statistic_updates_guard = self.statistic_updates.write().await;
|
2026-07-21 18:01:04 +03:00
|
|
|
|
2026-07-23 08:30:05 +03:00
|
|
|
#[expect(
|
|
|
|
|
clippy::iter_over_hash_type,
|
|
|
|
|
reason = "Ordering of map updates is not important"
|
|
|
|
|
)]
|
|
|
|
|
for (url, updates) in statistic_map {
|
|
|
|
|
statistic_updates_guard
|
|
|
|
|
.entry(url)
|
|
|
|
|
.or_insert_with(Vec::new)
|
|
|
|
|
.extend(updates);
|
|
|
|
|
}
|
2026-07-17 16:57:12 +03:00
|
|
|
}
|
|
|
|
|
|
2026-07-14 13:10:32 +03:00
|
|
|
resp
|
|
|
|
|
}
|
2026-07-17 16:57:12 +03:00
|
|
|
|
2026-07-24 13:46:45 +03:00
|
|
|
/// Metered `send_transaction` for `distribution_limit` amount of leaders.
|
|
|
|
|
///
|
2026-07-27 13:24:56 +03:00
|
|
|
/// Less abstract than it could be, clean way to implement it in a more general way is
|
2026-07-24 13:46:45 +03:00
|
|
|
/// "return-type notation".
|
|
|
|
|
///
|
|
|
|
|
/// `ToDo`: Return to it, when "return-type notation" is stable.
|
|
|
|
|
pub async fn metered_send_transaction(
|
2026-07-20 17:10:01 +03:00
|
|
|
&self,
|
2026-07-24 13:46:45 +03:00
|
|
|
tx: LeeTransaction,
|
|
|
|
|
) -> Vec<Result<HashType, ExecutionFailureKind>> {
|
2026-07-20 17:10:01 +03:00
|
|
|
// Collecting all statistics into one map to lock updates only once
|
|
|
|
|
let mut statistic_map: HashMap<Url, Vec<StatisticsUpdate>> = HashMap::new();
|
|
|
|
|
|
|
|
|
|
let mut results = vec![];
|
2026-07-24 13:46:45 +03:00
|
|
|
let mut join_set = JoinSet::new();
|
|
|
|
|
|
|
|
|
|
for (leader, leader_url) in self.leaders() {
|
|
|
|
|
let curr_leader = leader.clone();
|
|
|
|
|
let curr_tx = tx.clone();
|
|
|
|
|
let curr_url = leader_url.clone();
|
|
|
|
|
|
|
|
|
|
join_set.spawn(async move {
|
|
|
|
|
(
|
|
|
|
|
tokio::join!(
|
|
|
|
|
curr_leader.send_transaction(curr_tx),
|
|
|
|
|
actualize_client(curr_leader.clone())
|
|
|
|
|
),
|
|
|
|
|
curr_url,
|
|
|
|
|
)
|
|
|
|
|
});
|
|
|
|
|
}
|
2026-07-21 18:01:04 +03:00
|
|
|
|
2026-07-24 13:46:45 +03:00
|
|
|
while let Some(resp) = join_set.join_next().await {
|
|
|
|
|
let res = resp
|
2026-07-27 16:14:03 +03:00
|
|
|
.inspect_err(|j_err| log::warn!("Task failed with join error: {j_err:?}"))
|
2026-07-24 13:46:45 +03:00
|
|
|
.map_err(Into::into)
|
|
|
|
|
.and_then(|((resp, statistics_update), leader_url)| {
|
|
|
|
|
log::debug!(
|
|
|
|
|
"Metered call for {leader_url:?}, statistic updates is {statistics_update:?}",
|
|
|
|
|
);
|
2026-07-20 17:10:01 +03:00
|
|
|
|
2026-07-24 13:46:45 +03:00
|
|
|
statistic_map
|
|
|
|
|
.entry(leader_url)
|
|
|
|
|
.or_default()
|
|
|
|
|
.push(statistics_update);
|
2026-07-20 17:10:01 +03:00
|
|
|
|
2026-07-24 13:46:45 +03:00
|
|
|
resp.map_err(Into::into)
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
results.push(res);
|
2026-07-20 17:10:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
let mut statistic_updates_guard = self.statistic_updates.write().await;
|
|
|
|
|
|
2026-07-23 08:30:05 +03:00
|
|
|
#[expect(
|
|
|
|
|
clippy::iter_over_hash_type,
|
|
|
|
|
reason = "Ordering of map updates is not important"
|
|
|
|
|
)]
|
|
|
|
|
for (url, updates) in statistic_map {
|
|
|
|
|
statistic_updates_guard
|
|
|
|
|
.entry(url)
|
|
|
|
|
.or_insert_with(Vec::new)
|
|
|
|
|
.extend(updates);
|
|
|
|
|
}
|
2026-07-20 17:10:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
results
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Update statistics of a leader, clear statistic updates log.
|
2026-07-24 08:02:03 +03:00
|
|
|
pub async fn update_statistics(&self, statistics: &mut HashMap<Url, Statistics>) -> Result<()> {
|
2026-07-20 11:34:05 +03:00
|
|
|
let mut statistic_updates = self.statistic_updates.write().await;
|
2026-07-20 17:10:01 +03:00
|
|
|
|
2026-07-21 18:01:04 +03:00
|
|
|
#[expect(clippy::iter_over_hash_type, reason = "Ordering is unnecesary here")]
|
2026-07-20 17:10:01 +03:00
|
|
|
for (addr, statistic_updates_vec) in statistic_updates.iter() {
|
2026-07-21 18:01:04 +03:00
|
|
|
let leader_statistic = statistics
|
|
|
|
|
.get_mut(addr)
|
2026-07-24 08:02:03 +03:00
|
|
|
.ok_or_else(|| anyhow::anyhow!("Leader statistic must be present after setup"))?;
|
2026-07-20 17:10:01 +03:00
|
|
|
leader_statistic.apply_updates(statistic_updates_vec.as_slice());
|
|
|
|
|
}
|
2026-07-21 18:01:04 +03:00
|
|
|
|
2026-07-20 11:34:05 +03:00
|
|
|
statistic_updates.clear();
|
2026-07-24 08:02:03 +03:00
|
|
|
|
|
|
|
|
Ok(())
|
2026-07-17 16:57:12 +03:00
|
|
|
}
|
2026-07-14 13:10:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct CumulativeUpdates {
|
|
|
|
|
pub failure_count: u64,
|
2026-07-17 16:57:12 +03:00
|
|
|
pub latest_block_id: Option<BlockId>,
|
2026-07-14 13:10:32 +03:00
|
|
|
/// Necessary for cumulative average calculation.
|
|
|
|
|
pub cumulative_latency: f32,
|
|
|
|
|
/// Necessary for cumulative variance calculation.
|
|
|
|
|
pub cumulative_latency_squares: f32,
|
|
|
|
|
pub additional_sample_size: usize,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl CumulativeUpdates {
|
2026-07-17 16:57:12 +03:00
|
|
|
fn from_metric_updates(metric_updates: &[StatisticsUpdate]) -> Self {
|
2026-07-14 13:10:32 +03:00
|
|
|
let (failure_count, latest_block_id, cumulative_latency, cumulative_latency_squares) =
|
|
|
|
|
metric_updates
|
|
|
|
|
.iter()
|
2026-07-22 13:21:27 +03:00
|
|
|
.fold((0_u64, None, 0_f32, 0_f32), |mut acc, x| {
|
|
|
|
|
match x {
|
|
|
|
|
StatisticsUpdate::Success {
|
|
|
|
|
latency,
|
|
|
|
|
new_latest_block_id,
|
|
|
|
|
} => {
|
|
|
|
|
match acc.1 {
|
|
|
|
|
Some(val_old) => {
|
|
|
|
|
acc.1 = Some(std::cmp::max(val_old, *new_latest_block_id));
|
|
|
|
|
}
|
|
|
|
|
None => {
|
|
|
|
|
acc.1 = Some(*new_latest_block_id);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
acc.2 += latency;
|
|
|
|
|
acc.3 = latency.mul_add(*latency, acc.3);
|
|
|
|
|
}
|
|
|
|
|
StatisticsUpdate::Failure => {
|
|
|
|
|
acc.0 = acc.0.saturating_add(1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
acc
|
2026-07-14 13:10:32 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Self {
|
|
|
|
|
failure_count,
|
2026-07-22 13:21:27 +03:00
|
|
|
latest_block_id,
|
2026-07-14 13:10:32 +03:00
|
|
|
cumulative_latency,
|
|
|
|
|
cumulative_latency_squares,
|
|
|
|
|
additional_sample_size: metric_updates.len().saturating_sub(
|
|
|
|
|
usize::try_from(failure_count).expect("Sample size should fit usize"),
|
|
|
|
|
),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-17 16:57:12 +03:00
|
|
|
pub fn extract_statistics_from_path(
|
|
|
|
|
path: &Path,
|
|
|
|
|
) -> Result<HashMap<Url, Statistics>, anyhow::Error> {
|
2026-07-14 13:10:32 +03:00
|
|
|
match std::fs::File::open(path) {
|
|
|
|
|
Ok(file) => {
|
|
|
|
|
let reader = std::io::BufReader::new(file);
|
|
|
|
|
Ok(serde_json::from_reader(reader)?)
|
|
|
|
|
}
|
|
|
|
|
Err(err) if err.kind() == std::io::ErrorKind::NotFound => {
|
2026-07-17 16:57:12 +03:00
|
|
|
println!("Statistics not found, choosing empty");
|
2026-07-14 13:10:32 +03:00
|
|
|
Ok(HashMap::new())
|
|
|
|
|
}
|
|
|
|
|
Err(err) => Err(err).context("IO error"),
|
2026-07-07 15:14:49 +03:00
|
|
|
}
|
2026-07-08 14:18:52 +03:00
|
|
|
}
|
|
|
|
|
|
2026-07-17 16:57:12 +03:00
|
|
|
/// Measuring `get_last_block_id` as it should be the fastest request on sequencer.
|
|
|
|
|
async fn measure_request_duration(client: &SequencerClient) -> (u128, Option<BlockId>) {
|
|
|
|
|
let now = tokio::time::Instant::now();
|
|
|
|
|
let block_id = client.get_last_block_id().await.ok();
|
|
|
|
|
(
|
|
|
|
|
tokio::time::Instant::now().duration_since(now).as_millis(),
|
|
|
|
|
block_id,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-23 14:56:18 +03:00
|
|
|
/// Calibrate statistics for one client. Takes `client` by value deliberately, cloning
|
|
|
|
|
/// `SequencerClient` is cheap.
|
|
|
|
|
async fn calibrate_client(client: SequencerClient, calibration_limit: usize) -> Option<Statistics> {
|
2026-07-08 14:18:52 +03:00
|
|
|
let mut latencies = vec![];
|
|
|
|
|
let mut latest_block_id = 0;
|
2026-07-14 13:10:32 +03:00
|
|
|
let mut errors: u64 = 0;
|
2026-07-08 14:18:52 +03:00
|
|
|
|
2026-07-13 15:37:25 +03:00
|
|
|
// ToDo: Add some DDoS adaptation
|
2026-07-17 16:57:12 +03:00
|
|
|
for _ in 0..calibration_limit {
|
2026-07-23 14:56:18 +03:00
|
|
|
let (latency, block_id) = measure_request_duration(&client).await;
|
2026-07-08 14:18:52 +03:00
|
|
|
|
2026-07-17 16:57:12 +03:00
|
|
|
let Some(block_id) = block_id else {
|
2026-07-14 13:10:32 +03:00
|
|
|
errors = errors.saturating_add(1);
|
2026-07-08 14:18:52 +03:00
|
|
|
continue;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
latest_block_id = block_id;
|
|
|
|
|
latencies.push(latency);
|
2026-07-07 15:14:49 +03:00
|
|
|
}
|
|
|
|
|
|
2026-07-17 16:57:12 +03:00
|
|
|
// There is no point in guard numbers, exclude client if it fails all requests.
|
|
|
|
|
if latencies.is_empty() {
|
|
|
|
|
return None;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Precision loss is fine there
|
2026-07-08 14:18:52 +03:00
|
|
|
let sample_size = latencies.len();
|
2026-07-14 13:10:32 +03:00
|
|
|
#[expect(clippy::as_conversions, reason = "int to float conversion is safe")]
|
|
|
|
|
let latency_avg = (latencies.iter().sum::<u128>() as f32) / (sample_size as f32);
|
|
|
|
|
#[expect(clippy::as_conversions, reason = "int to float conversion is safe")]
|
|
|
|
|
let latency_var = latencies.iter().fold(0_f32, |acc, x| {
|
|
|
|
|
((*x as f32) - latency_avg).mul_add((*x as f32) - latency_avg, acc)
|
2026-07-10 16:21:03 +03:00
|
|
|
}) / (sample_size as f32);
|
2026-07-08 14:18:52 +03:00
|
|
|
|
2026-07-17 16:57:12 +03:00
|
|
|
Some(Statistics {
|
2026-07-08 14:18:52 +03:00
|
|
|
latency_avg,
|
|
|
|
|
latency_var,
|
|
|
|
|
sample_size,
|
|
|
|
|
latest_block_id,
|
|
|
|
|
errors,
|
2026-07-17 16:57:12 +03:00
|
|
|
})
|
2026-07-07 15:14:49 +03:00
|
|
|
}
|
2026-07-08 14:18:52 +03:00
|
|
|
|
2026-07-23 14:56:18 +03:00
|
|
|
/// Actualize statistics for one client. Takes `client` by value deliberately, cloning
|
|
|
|
|
/// `SequencerClient` is cheap.
|
|
|
|
|
async fn actualize_client(client: SequencerClient) -> StatisticsUpdate {
|
|
|
|
|
let (latency, block_id) = measure_request_duration(&client).await;
|
2026-07-13 15:37:25 +03:00
|
|
|
|
2026-07-14 13:10:32 +03:00
|
|
|
#[expect(clippy::as_conversions, reason = "int to float conversion is safe")]
|
2026-07-17 16:57:12 +03:00
|
|
|
let latency = latency as f32;
|
2026-07-13 15:37:25 +03:00
|
|
|
|
2026-07-22 13:21:27 +03:00
|
|
|
block_id.map_or(StatisticsUpdate::Failure, |new_latest_block_id| {
|
|
|
|
|
StatisticsUpdate::Success {
|
|
|
|
|
latency,
|
|
|
|
|
new_latest_block_id,
|
|
|
|
|
}
|
|
|
|
|
})
|
2026-07-13 15:37:25 +03:00
|
|
|
}
|
|
|
|
|
|
2026-07-23 14:56:18 +03:00
|
|
|
// Next 2 functions can be done in uniform way right now, but it will be incredibly cursed.
|
|
|
|
|
// ToDo: Return to it when "return-type notation" is stable
|
2026-07-22 16:19:40 +03:00
|
|
|
|
2026-07-27 13:24:56 +03:00
|
|
|
async fn multi_actualize_clients(
|
2026-07-24 13:46:45 +03:00
|
|
|
clients: Vec<(Url, SequencerClient)>,
|
2026-07-23 14:56:18 +03:00
|
|
|
) -> Vec<(Url, Option<StatisticsUpdate>)> {
|
|
|
|
|
let mut handle_map = HashMap::new();
|
2026-07-22 16:19:40 +03:00
|
|
|
|
2026-07-23 14:56:18 +03:00
|
|
|
for (url, client) in clients {
|
2026-07-24 13:46:45 +03:00
|
|
|
// `client` here must have 'static lifetime, so we can not use reference
|
|
|
|
|
let actualization_task = tokio::task::spawn(actualize_client(client));
|
|
|
|
|
handle_map.insert(url, actualization_task);
|
2026-07-23 14:56:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let mut statistic_updates = vec![];
|
|
|
|
|
|
|
|
|
|
// We anyhow need results of each one
|
|
|
|
|
#[expect(
|
|
|
|
|
clippy::iter_over_hash_type,
|
|
|
|
|
reason = "Ordering of map updates is not important"
|
|
|
|
|
)]
|
|
|
|
|
for (url, task) in handle_map {
|
|
|
|
|
let task_opt = task.await.ok();
|
|
|
|
|
statistic_updates.push((url, task_opt));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
statistic_updates
|
2026-07-22 16:19:40 +03:00
|
|
|
}
|
|
|
|
|
|
2026-07-27 13:24:56 +03:00
|
|
|
async fn multi_calibrate_clients(
|
2026-07-24 13:46:45 +03:00
|
|
|
clients: Vec<(Url, SequencerClient)>,
|
2026-07-23 14:56:18 +03:00
|
|
|
calibration_limit: usize,
|
|
|
|
|
) -> Vec<(Url, Option<Statistics>)> {
|
|
|
|
|
let mut handle_map = HashMap::new();
|
2026-07-22 16:19:40 +03:00
|
|
|
|
2026-07-23 14:56:18 +03:00
|
|
|
for (url, client) in clients {
|
2026-07-24 13:46:45 +03:00
|
|
|
// `client` here must have 'static lifetime, so we can not use reference
|
|
|
|
|
let calibration_task = tokio::task::spawn(calibrate_client(client, calibration_limit));
|
|
|
|
|
handle_map.insert(url, calibration_task);
|
2026-07-22 16:19:40 +03:00
|
|
|
}
|
|
|
|
|
|
2026-07-23 14:56:18 +03:00
|
|
|
let mut statistics = vec![];
|
2026-07-22 16:19:40 +03:00
|
|
|
|
2026-07-23 14:56:18 +03:00
|
|
|
// We anyhow need results of each one
|
|
|
|
|
#[expect(
|
|
|
|
|
clippy::iter_over_hash_type,
|
|
|
|
|
reason = "Ordering of map updates is not important"
|
|
|
|
|
)]
|
|
|
|
|
for (url, task) in handle_map {
|
|
|
|
|
let task_opt = task
|
|
|
|
|
.await
|
|
|
|
|
.ok()
|
|
|
|
|
.inspect(|val| {
|
|
|
|
|
if val.is_none() {
|
|
|
|
|
log::warn!(
|
|
|
|
|
"Client {url:?} failed all {calibration_limit} calibration attempts, it may be unhealthy.
|
|
|
|
|
\n Consider bumping calibration_limit or remove this client altogether"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.flatten();
|
|
|
|
|
statistics.push((url, task_opt));
|
2026-07-22 16:19:40 +03:00
|
|
|
}
|
|
|
|
|
|
2026-07-23 14:56:18 +03:00
|
|
|
statistics
|
2026-07-22 16:19:40 +03:00
|
|
|
}
|
|
|
|
|
|
2026-07-23 14:56:18 +03:00
|
|
|
/// Choosing leaders up to a `distribution_limit`.
|
|
|
|
|
///
|
|
|
|
|
/// Assumes that all clients have their statistics in `statistics`.
|
2026-07-27 16:14:03 +03:00
|
|
|
#[must_use]
|
2026-07-23 14:56:18 +03:00
|
|
|
fn choose_leaders(
|
2026-07-24 13:46:45 +03:00
|
|
|
client_vec: Vec<(Url, SequencerClient)>,
|
2026-07-17 16:57:12 +03:00
|
|
|
statistics: &HashMap<Url, Statistics>,
|
2026-07-15 16:36:45 +03:00
|
|
|
distribution_limit: usize,
|
|
|
|
|
) -> Option<Vec<(SequencerClient, Url)>> {
|
2026-07-09 15:58:29 +03:00
|
|
|
// Sort out all unmetered clients
|
2026-07-23 14:56:18 +03:00
|
|
|
let client_vec: Vec<_> = client_vec
|
2026-07-24 13:46:45 +03:00
|
|
|
.into_iter()
|
2026-07-23 14:56:18 +03:00
|
|
|
.filter(|item| statistics.contains_key(&item.0))
|
2026-07-14 13:10:32 +03:00
|
|
|
.collect();
|
2026-07-09 15:58:29 +03:00
|
|
|
|
|
|
|
|
if client_vec.is_empty() {
|
|
|
|
|
return None;
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-13 15:37:25 +03:00
|
|
|
// Considering the nature of our requests, the latest_block_id is the dominant characteristic
|
2026-07-23 14:56:18 +03:00
|
|
|
let max_block_id_addr = client_vec
|
|
|
|
|
.iter()
|
|
|
|
|
.fold(client_vec.first().unwrap(), |acc, x| {
|
|
|
|
|
let old_latest_block_id = statistics.get(&acc.0).unwrap().latest_block_id;
|
|
|
|
|
let new_latest_block_id = statistics.get(&x.0).unwrap().latest_block_id;
|
|
|
|
|
if new_latest_block_id > old_latest_block_id {
|
|
|
|
|
x
|
|
|
|
|
} else {
|
|
|
|
|
acc
|
|
|
|
|
}
|
|
|
|
|
});
|
2026-07-09 15:58:29 +03:00
|
|
|
|
2026-07-23 14:56:18 +03:00
|
|
|
let max_block_id = statistics
|
|
|
|
|
.get(&max_block_id_addr.0)
|
|
|
|
|
.unwrap()
|
|
|
|
|
.latest_block_id;
|
2026-07-09 15:58:29 +03:00
|
|
|
|
2026-07-13 15:37:25 +03:00
|
|
|
// Sort out all clients running late
|
2026-07-23 14:56:18 +03:00
|
|
|
let mut res_vec: Vec<_> = client_vec
|
2026-07-27 13:24:56 +03:00
|
|
|
.into_iter()
|
2026-07-24 13:46:45 +03:00
|
|
|
.filter(|x| {
|
2026-07-23 14:56:18 +03:00
|
|
|
let latest_block_id = statistics.get(&x.0).unwrap().latest_block_id;
|
2026-07-09 15:58:29 +03:00
|
|
|
|
2026-07-24 13:46:45 +03:00
|
|
|
latest_block_id == max_block_id
|
2026-07-09 15:58:29 +03:00
|
|
|
})
|
|
|
|
|
.collect();
|
|
|
|
|
|
2026-07-17 16:57:12 +03:00
|
|
|
// Get the clients with lesser or equal to average error ratio
|
2026-07-14 13:10:32 +03:00
|
|
|
#[expect(clippy::as_conversions, reason = "int to float conversion is safe")]
|
2026-07-23 14:56:18 +03:00
|
|
|
let avg_err_ratio = res_vec.iter().fold(0_f32, |acc, x| {
|
2026-07-17 16:57:12 +03:00
|
|
|
acc + error_ratio(
|
2026-07-23 14:56:18 +03:00
|
|
|
statistics.get(&x.0).unwrap().errors,
|
|
|
|
|
statistics.get(&x.0).unwrap().sample_size,
|
2026-07-17 16:57:12 +03:00
|
|
|
)
|
2026-07-23 14:56:18 +03:00
|
|
|
}) / (res_vec.len() as f32);
|
2026-07-13 15:37:25 +03:00
|
|
|
|
2026-07-23 14:56:18 +03:00
|
|
|
res_vec.sort_by(|a, b| {
|
2026-07-17 16:57:12 +03:00
|
|
|
let err_ratio_a = error_ratio(
|
2026-07-23 14:56:18 +03:00
|
|
|
statistics.get(&a.0).unwrap().errors,
|
|
|
|
|
statistics.get(&a.0).unwrap().sample_size,
|
2026-07-17 16:57:12 +03:00
|
|
|
);
|
|
|
|
|
let err_ratio_b = error_ratio(
|
2026-07-23 14:56:18 +03:00
|
|
|
statistics.get(&b.0).unwrap().errors,
|
|
|
|
|
statistics.get(&b.0).unwrap().sample_size,
|
2026-07-17 16:57:12 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
err_ratio_a
|
|
|
|
|
.partial_cmp(&err_ratio_b)
|
|
|
|
|
.expect("Ratios must be a valid numbers")
|
2026-07-09 15:58:29 +03:00
|
|
|
});
|
|
|
|
|
|
2026-07-28 07:55:43 +03:00
|
|
|
res_vec.truncate(
|
|
|
|
|
res_vec
|
|
|
|
|
.iter()
|
|
|
|
|
.position(|item| {
|
|
|
|
|
error_ratio(
|
|
|
|
|
statistics.get(&item.0).unwrap().errors,
|
|
|
|
|
statistics.get(&item.0).unwrap().sample_size,
|
|
|
|
|
) > avg_err_ratio
|
|
|
|
|
})
|
|
|
|
|
.unwrap_or(res_vec.len()),
|
|
|
|
|
);
|
2026-07-09 15:58:29 +03:00
|
|
|
|
|
|
|
|
// Choose clients with least latency and variance
|
2026-07-23 14:56:18 +03:00
|
|
|
res_vec.sort_by(|a, b| {
|
|
|
|
|
let left = statistics.get(&a.0).unwrap();
|
2026-07-15 16:36:45 +03:00
|
|
|
let (left_lat, left_var) = (left.latency_avg, left.latency_var);
|
2026-07-23 14:56:18 +03:00
|
|
|
let right = statistics.get(&b.0).unwrap();
|
2026-07-15 16:36:45 +03:00
|
|
|
let (right_lat, right_var) = (right.latency_avg, right.latency_var);
|
2026-07-09 15:58:29 +03:00
|
|
|
|
2026-07-15 16:36:45 +03:00
|
|
|
let right_std = right_var.sqrt();
|
|
|
|
|
let left_std = left_var.sqrt();
|
2026-07-10 16:21:03 +03:00
|
|
|
|
2026-07-27 13:24:56 +03:00
|
|
|
// Client is better if its average + std is lesser:
|
|
|
|
|
// [-right_std < right_lat < +left_std < +right_std]
|
|
|
|
|
(left_lat + left_std).total_cmp(&(right_lat + right_std))
|
2026-07-09 15:58:29 +03:00
|
|
|
});
|
|
|
|
|
|
2026-07-15 16:36:45 +03:00
|
|
|
Some(
|
2026-07-23 14:56:18 +03:00
|
|
|
res_vec
|
|
|
|
|
.into_iter()
|
2026-07-27 13:24:56 +03:00
|
|
|
.map(|(a, b)| (b, a))
|
2026-07-15 16:36:45 +03:00
|
|
|
.take(distribution_limit)
|
|
|
|
|
.collect(),
|
|
|
|
|
)
|
2026-07-08 14:18:52 +03:00
|
|
|
}
|
|
|
|
|
|
2026-07-14 13:10:32 +03:00
|
|
|
/// Helperfunction to calculate cumulative average.
|
2026-07-13 15:37:25 +03:00
|
|
|
///
|
|
|
|
|
/// Cumulative average calculation is the following problem:
|
|
|
|
|
///
|
|
|
|
|
/// We want to calculate avarage of a sample of size `N + N_1`
|
2026-07-14 13:10:32 +03:00
|
|
|
/// where average for `N` is known.
|
2026-07-13 15:37:25 +03:00
|
|
|
///
|
|
|
|
|
/// To do so we need:
|
|
|
|
|
/// - old average value
|
2026-07-14 13:10:32 +03:00
|
|
|
/// - sum_{`i=1}^{N_1}{n_i`}
|
|
|
|
|
/// - `N`
|
|
|
|
|
/// - `N_1`
|
2026-07-09 15:58:29 +03:00
|
|
|
fn cumulative_avg(
|
|
|
|
|
latency_avg_old: f32,
|
|
|
|
|
cumulative_latency: f32,
|
|
|
|
|
orig_size_f: f32,
|
|
|
|
|
mod_size_f: f32,
|
|
|
|
|
) -> f32 {
|
2026-07-14 13:10:32 +03:00
|
|
|
latency_avg_old.mul_add(orig_size_f, cumulative_latency) / (orig_size_f + mod_size_f)
|
2026-07-09 15:58:29 +03:00
|
|
|
}
|
|
|
|
|
|
2026-07-14 13:10:32 +03:00
|
|
|
/// Helperfunction to calculate cumulative variance.
|
2026-07-13 15:37:25 +03:00
|
|
|
///
|
|
|
|
|
/// Cumulative variance calculation is the following problem:
|
|
|
|
|
///
|
|
|
|
|
/// We want to calculate variance of a sample of size `N + N_1`
|
2026-07-14 13:10:32 +03:00
|
|
|
/// where average for `N` is known.
|
2026-07-13 15:37:25 +03:00
|
|
|
///
|
|
|
|
|
/// To do so we need:
|
|
|
|
|
/// - old average value
|
|
|
|
|
/// - new average value
|
|
|
|
|
/// - old variance
|
2026-07-14 13:10:32 +03:00
|
|
|
/// - sum_{`i=1}^{N_1}{n_i`}
|
|
|
|
|
/// - sum_{`i=1}^{N_1}{n_i^2`}
|
|
|
|
|
/// - `N`
|
|
|
|
|
/// - `N_1`
|
2026-07-09 15:58:29 +03:00
|
|
|
fn cumulative_var(
|
|
|
|
|
latency_avg_old: f32,
|
|
|
|
|
latency_avg_new: f32,
|
|
|
|
|
latency_var: f32,
|
2026-07-10 16:21:03 +03:00
|
|
|
cumulative_latency: f32,
|
|
|
|
|
cumulative_latency_squares: f32,
|
2026-07-09 15:58:29 +03:00
|
|
|
orig_size_f: f32,
|
|
|
|
|
mod_size_f: f32,
|
|
|
|
|
) -> f32 {
|
2026-07-14 13:10:32 +03:00
|
|
|
// The formula was atrocious before.
|
|
|
|
|
// `mul_add` function have less precision loss with drawback of being absolutely unreadable
|
|
|
|
|
((2_f32 * cumulative_latency).mul_add(
|
|
|
|
|
-latency_avg_new,
|
|
|
|
|
mod_size_f.mul_add(
|
|
|
|
|
latency_avg_new * latency_avg_new,
|
|
|
|
|
latency_var.mul_add(
|
|
|
|
|
orig_size_f,
|
|
|
|
|
(latency_avg_new - latency_avg_old)
|
|
|
|
|
* (latency_avg_new - latency_avg_old)
|
|
|
|
|
* orig_size_f,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
) + cumulative_latency_squares)
|
2026-07-10 16:21:03 +03:00
|
|
|
/ (orig_size_f + mod_size_f)
|
2026-07-09 15:58:29 +03:00
|
|
|
}
|
|
|
|
|
|
2026-07-17 16:57:12 +03:00
|
|
|
fn error_ratio(errors: u64, size: usize) -> f32 {
|
|
|
|
|
#[expect(clippy::as_conversions, reason = "int to float conversion is safe")]
|
|
|
|
|
let errors_f = errors as f32;
|
|
|
|
|
#[expect(clippy::as_conversions, reason = "int to float conversion is safe")]
|
|
|
|
|
let size_f = size as f32;
|
|
|
|
|
|
|
|
|
|
errors_f / (errors_f + size_f)
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-09 15:58:29 +03:00
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
2026-07-22 10:13:01 +03:00
|
|
|
use std::collections::{HashMap, HashSet};
|
2026-07-10 16:21:03 +03:00
|
|
|
|
2026-07-13 15:37:25 +03:00
|
|
|
use sequencer_service_rpc::{SequencerClient, SequencerClientBuilder};
|
2026-07-10 16:21:03 +03:00
|
|
|
use url::Url;
|
|
|
|
|
|
|
|
|
|
use crate::multi_client::{
|
2026-07-20 14:09:57 +03:00
|
|
|
CumulativeUpdates, Statistics, StatisticsUpdate, choose_leaders, cumulative_avg,
|
2026-07-17 16:57:12 +03:00
|
|
|
cumulative_var,
|
2026-07-10 16:21:03 +03:00
|
|
|
};
|
2026-07-09 15:58:29 +03:00
|
|
|
|
2026-07-17 16:57:12 +03:00
|
|
|
fn update_statistics(
|
|
|
|
|
statistics: &mut HashMap<Url, Statistics>,
|
2026-07-14 13:10:32 +03:00
|
|
|
leader_url: &Url,
|
2026-07-17 16:57:12 +03:00
|
|
|
metric_updates: &[StatisticsUpdate],
|
2026-07-14 13:10:32 +03:00
|
|
|
) -> Result<(), anyhow::Error> {
|
2026-07-17 16:57:12 +03:00
|
|
|
let leader_metric = statistics
|
2026-07-14 13:10:32 +03:00
|
|
|
.get_mut(leader_url)
|
2026-07-17 16:57:12 +03:00
|
|
|
.ok_or_else(|| anyhow::anyhow!("Leader URL is not present in statistics"))?;
|
2026-07-14 13:10:32 +03:00
|
|
|
|
|
|
|
|
leader_metric.apply_updates(metric_updates);
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-13 15:37:25 +03:00
|
|
|
fn client_from_url_unchecked(url: &Url) -> SequencerClient {
|
|
|
|
|
let builder = SequencerClientBuilder::default();
|
|
|
|
|
builder.build(url).unwrap()
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-23 14:56:18 +03:00
|
|
|
fn four_client_list() -> (Vec<(Url, SequencerClient)>, [Url; 4]) {
|
2026-07-22 10:13:01 +03:00
|
|
|
let addr_leader = Url::parse("http://127.0.0.1:3040").unwrap();
|
|
|
|
|
let addr_1 = Url::parse("http://127.0.0.1:3041").unwrap();
|
|
|
|
|
let addr_2 = Url::parse("http://127.0.0.1:3042").unwrap();
|
|
|
|
|
let addr_3 = Url::parse("http://127.0.0.1:3043").unwrap();
|
|
|
|
|
|
|
|
|
|
let leader = client_from_url_unchecked(&addr_leader);
|
|
|
|
|
let client_1 = client_from_url_unchecked(&addr_1);
|
|
|
|
|
let client_2 = client_from_url_unchecked(&addr_2);
|
|
|
|
|
let client_3 = client_from_url_unchecked(&addr_3);
|
|
|
|
|
|
2026-07-23 14:56:18 +03:00
|
|
|
let client_list = vec![
|
|
|
|
|
(addr_leader.clone(), leader),
|
|
|
|
|
(addr_1.clone(), client_1),
|
|
|
|
|
(addr_2.clone(), client_2),
|
|
|
|
|
(addr_3.clone(), client_3),
|
|
|
|
|
];
|
2026-07-22 10:13:01 +03:00
|
|
|
|
|
|
|
|
(client_list, [addr_leader, addr_1, addr_2, addr_3])
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-09 15:58:29 +03:00
|
|
|
#[test]
|
|
|
|
|
fn cumulative_updates_test() {
|
2026-07-17 16:57:12 +03:00
|
|
|
let statistics_updates_vec = vec![
|
2026-07-22 13:21:27 +03:00
|
|
|
StatisticsUpdate::Success {
|
2026-07-09 15:58:29 +03:00
|
|
|
latency: 100_f32,
|
2026-07-22 13:21:27 +03:00
|
|
|
new_latest_block_id: 15,
|
2026-07-09 15:58:29 +03:00
|
|
|
},
|
2026-07-22 13:21:27 +03:00
|
|
|
StatisticsUpdate::Success {
|
2026-07-09 15:58:29 +03:00
|
|
|
latency: 115_f32,
|
2026-07-22 13:21:27 +03:00
|
|
|
new_latest_block_id: 16,
|
2026-07-09 15:58:29 +03:00
|
|
|
},
|
2026-07-22 13:21:27 +03:00
|
|
|
StatisticsUpdate::Failure,
|
2026-07-09 15:58:29 +03:00
|
|
|
];
|
|
|
|
|
|
2026-07-10 16:21:03 +03:00
|
|
|
let CumulativeUpdates {
|
|
|
|
|
failure_count,
|
|
|
|
|
latest_block_id,
|
|
|
|
|
cumulative_latency,
|
|
|
|
|
cumulative_latency_squares,
|
|
|
|
|
additional_sample_size,
|
2026-07-17 16:57:12 +03:00
|
|
|
} = CumulativeUpdates::from_metric_updates(&statistics_updates_vec);
|
2026-07-09 15:58:29 +03:00
|
|
|
|
|
|
|
|
let epsilon = 0.01_f32;
|
|
|
|
|
|
2026-07-14 13:10:32 +03:00
|
|
|
let sum_squared_manual = 100_f32.mul_add(100_f32, 115_f32 * 115_f32);
|
2026-07-10 16:21:03 +03:00
|
|
|
|
|
|
|
|
assert_eq!(additional_sample_size, 2);
|
|
|
|
|
assert_eq!(failure_count, 1);
|
|
|
|
|
assert_eq!(latest_block_id, Some(16));
|
|
|
|
|
assert!((cumulative_latency - 215_f32).abs() < epsilon);
|
|
|
|
|
assert!((cumulative_latency_squares - sum_squared_manual).abs() < epsilon);
|
2026-07-09 15:58:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn cumulative_avg_test() {
|
2026-07-10 16:21:03 +03:00
|
|
|
let mut sample = vec![100_f32; 40];
|
2026-07-09 15:58:29 +03:00
|
|
|
|
2026-07-14 13:10:32 +03:00
|
|
|
#[expect(clippy::as_conversions, reason = "int to float conversion is safe")]
|
2026-07-09 15:58:29 +03:00
|
|
|
let old_sample_size_f = sample.len() as f32;
|
2026-07-14 13:10:32 +03:00
|
|
|
|
2026-07-09 15:58:29 +03:00
|
|
|
let old_avg = sample.iter().sum::<f32>() / old_sample_size_f;
|
|
|
|
|
|
2026-07-10 16:21:03 +03:00
|
|
|
let new_samples = vec![
|
|
|
|
|
101_f32, 110_f32, 112_f32, 97_f32, 78_f32, 25_f32, 75_f32, 189_f32, 120_f32, 50_f32,
|
|
|
|
|
];
|
2026-07-14 13:10:32 +03:00
|
|
|
|
|
|
|
|
#[expect(clippy::as_conversions, reason = "int to float conversion is safe")]
|
2026-07-10 16:21:03 +03:00
|
|
|
let mod_sample_size_f = new_samples.len() as f32;
|
2026-07-14 13:10:32 +03:00
|
|
|
|
2026-07-09 15:58:29 +03:00
|
|
|
let cumulative = new_samples.iter().sum();
|
|
|
|
|
|
|
|
|
|
sample.extend_from_slice(&new_samples);
|
2026-07-14 13:10:32 +03:00
|
|
|
|
|
|
|
|
#[expect(clippy::as_conversions, reason = "int to float conversion is safe")]
|
2026-07-09 15:58:29 +03:00
|
|
|
let new_sample_size_f = sample.len() as f32;
|
2026-07-14 13:10:32 +03:00
|
|
|
|
2026-07-09 15:58:29 +03:00
|
|
|
let new_avg_1 = sample.iter().sum::<f32>() / new_sample_size_f;
|
|
|
|
|
|
|
|
|
|
let new_avg_2 = cumulative_avg(old_avg, cumulative, old_sample_size_f, mod_sample_size_f);
|
|
|
|
|
|
|
|
|
|
let epsilon = 0.01_f32;
|
|
|
|
|
|
|
|
|
|
assert!((new_avg_1 - new_avg_2).abs() < epsilon);
|
|
|
|
|
}
|
2026-07-10 16:21:03 +03:00
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn cumulative_var_test() {
|
|
|
|
|
let mut sample = vec![100_f32; 40];
|
|
|
|
|
|
2026-07-14 13:10:32 +03:00
|
|
|
#[expect(clippy::as_conversions, reason = "int to float conversion is safe")]
|
2026-07-10 16:21:03 +03:00
|
|
|
let old_sample_size_f = sample.len() as f32;
|
|
|
|
|
let old_avg = sample.iter().sum::<f32>() / old_sample_size_f;
|
|
|
|
|
|
|
|
|
|
let old_var = sample
|
|
|
|
|
.iter()
|
2026-07-14 13:10:32 +03:00
|
|
|
.fold(0_f32, |acc, x| (x - old_avg).mul_add(x - old_avg, acc))
|
2026-07-10 16:21:03 +03:00
|
|
|
/ old_sample_size_f;
|
|
|
|
|
|
|
|
|
|
let new_samples = vec![
|
|
|
|
|
101_f32, 110_f32, 112_f32, 97_f32, 78_f32, 25_f32, 75_f32, 189_f32, 120_f32, 50_f32,
|
|
|
|
|
];
|
2026-07-14 13:10:32 +03:00
|
|
|
|
|
|
|
|
#[expect(clippy::as_conversions, reason = "int to float conversion is safe")]
|
2026-07-10 16:21:03 +03:00
|
|
|
let mod_sample_size_f = new_samples.len() as f32;
|
2026-07-14 13:10:32 +03:00
|
|
|
|
2026-07-10 16:21:03 +03:00
|
|
|
let cumulative = new_samples.iter().sum();
|
2026-07-14 13:10:32 +03:00
|
|
|
let cumulative_squares = new_samples
|
|
|
|
|
.iter()
|
|
|
|
|
.fold(0_f32, |acc, x| (*x).mul_add(*x, acc));
|
2026-07-10 16:21:03 +03:00
|
|
|
|
|
|
|
|
let new_avg = cumulative_avg(old_avg, cumulative, old_sample_size_f, mod_sample_size_f);
|
|
|
|
|
|
|
|
|
|
sample.extend_from_slice(&new_samples);
|
2026-07-14 13:10:32 +03:00
|
|
|
|
|
|
|
|
#[expect(clippy::as_conversions, reason = "int to float conversion is safe")]
|
2026-07-10 16:21:03 +03:00
|
|
|
let new_var_1 = sample
|
|
|
|
|
.iter()
|
2026-07-14 13:10:32 +03:00
|
|
|
.fold(0_f32, |acc, x| (x - new_avg).mul_add(x - new_avg, acc))
|
2026-07-10 16:21:03 +03:00
|
|
|
/ (sample.len() as f32);
|
|
|
|
|
|
|
|
|
|
let new_var_2 = cumulative_var(
|
|
|
|
|
old_avg,
|
|
|
|
|
new_avg,
|
|
|
|
|
old_var,
|
|
|
|
|
cumulative,
|
|
|
|
|
cumulative_squares,
|
|
|
|
|
old_sample_size_f,
|
|
|
|
|
mod_sample_size_f,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let epsilon = 0.01_f32;
|
|
|
|
|
|
|
|
|
|
assert!((new_var_1 - new_var_2).abs() < epsilon);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn metric_updates_correctness() {
|
2026-07-17 16:57:12 +03:00
|
|
|
let statistics_updates_vec = vec![
|
2026-07-22 13:21:27 +03:00
|
|
|
StatisticsUpdate::Success {
|
2026-07-10 16:21:03 +03:00
|
|
|
latency: 100_f32,
|
2026-07-22 13:21:27 +03:00
|
|
|
new_latest_block_id: 105,
|
2026-07-10 16:21:03 +03:00
|
|
|
},
|
2026-07-22 13:21:27 +03:00
|
|
|
StatisticsUpdate::Success {
|
2026-07-10 16:21:03 +03:00
|
|
|
latency: 115_f32,
|
2026-07-22 13:21:27 +03:00
|
|
|
new_latest_block_id: 106,
|
2026-07-10 16:21:03 +03:00
|
|
|
},
|
2026-07-22 13:21:27 +03:00
|
|
|
StatisticsUpdate::Failure,
|
2026-07-10 16:21:03 +03:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
let addr_leader = Url::parse("https://127.0.0.1:3040").unwrap();
|
|
|
|
|
|
2026-07-17 16:57:12 +03:00
|
|
|
let leader_statistics = Statistics {
|
2026-07-10 16:21:03 +03:00
|
|
|
latency_avg: 100_f32,
|
|
|
|
|
latency_var: 25_f32,
|
|
|
|
|
sample_size: 10,
|
|
|
|
|
latest_block_id: 100,
|
|
|
|
|
errors: 5,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let cumulative_latency = 100_f32 + 115_f32;
|
2026-07-14 13:10:32 +03:00
|
|
|
let cumulative_latency_squares = 100_f32.mul_add(100_f32, 115_f32 * 115_f32);
|
2026-07-10 16:21:03 +03:00
|
|
|
|
|
|
|
|
let avg_manual = cumulative_avg(
|
2026-07-17 16:57:12 +03:00
|
|
|
leader_statistics.latency_avg,
|
2026-07-10 16:21:03 +03:00
|
|
|
cumulative_latency,
|
|
|
|
|
10_f32,
|
|
|
|
|
2_f32,
|
|
|
|
|
);
|
|
|
|
|
let var_manual = cumulative_var(
|
2026-07-17 16:57:12 +03:00
|
|
|
leader_statistics.latency_avg,
|
2026-07-10 16:21:03 +03:00
|
|
|
avg_manual,
|
2026-07-17 16:57:12 +03:00
|
|
|
leader_statistics.latency_var,
|
2026-07-10 16:21:03 +03:00
|
|
|
cumulative_latency,
|
|
|
|
|
cumulative_latency_squares,
|
|
|
|
|
10_f32,
|
|
|
|
|
2_f32,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let mut metric_map = HashMap::new();
|
2026-07-17 16:57:12 +03:00
|
|
|
metric_map.insert(addr_leader.clone(), leader_statistics);
|
2026-07-10 16:21:03 +03:00
|
|
|
|
2026-07-17 16:57:12 +03:00
|
|
|
update_statistics(&mut metric_map, &addr_leader, &statistics_updates_vec).unwrap();
|
2026-07-10 16:21:03 +03:00
|
|
|
|
2026-07-17 16:57:12 +03:00
|
|
|
let Statistics {
|
2026-07-10 16:21:03 +03:00
|
|
|
latency_avg,
|
|
|
|
|
latency_var,
|
|
|
|
|
sample_size,
|
|
|
|
|
latest_block_id,
|
|
|
|
|
errors,
|
2026-07-14 13:10:32 +03:00
|
|
|
} = metric_map[&addr_leader];
|
2026-07-10 16:21:03 +03:00
|
|
|
|
|
|
|
|
let epsilon = 0.01_f32;
|
|
|
|
|
|
2026-07-14 13:10:32 +03:00
|
|
|
assert_eq!(errors, 6);
|
|
|
|
|
assert_eq!(latest_block_id, 106);
|
|
|
|
|
assert_eq!(sample_size, 12);
|
|
|
|
|
assert!((latency_avg - avg_manual).abs() < epsilon);
|
|
|
|
|
assert!((latency_var - var_manual).abs() < epsilon);
|
2026-07-10 16:21:03 +03:00
|
|
|
}
|
2026-07-13 15:37:25 +03:00
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn choose_leader_latest_block() {
|
2026-07-22 10:13:01 +03:00
|
|
|
let (client_list, [addr_leader, addr_1, addr_2, addr_3]) = four_client_list();
|
2026-07-13 15:37:25 +03:00
|
|
|
|
2026-07-17 16:57:12 +03:00
|
|
|
let mut statistics = HashMap::new();
|
2026-07-13 15:37:25 +03:00
|
|
|
|
2026-07-17 16:57:12 +03:00
|
|
|
statistics.insert(
|
2026-07-13 15:37:25 +03:00
|
|
|
addr_3,
|
2026-07-17 16:57:12 +03:00
|
|
|
Statistics {
|
2026-07-13 15:37:25 +03:00
|
|
|
latency_avg: 100_f32,
|
|
|
|
|
latency_var: 10_f32,
|
|
|
|
|
sample_size: 10,
|
|
|
|
|
latest_block_id: 97,
|
|
|
|
|
errors: 5,
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
2026-07-17 16:57:12 +03:00
|
|
|
statistics.insert(
|
2026-07-13 15:37:25 +03:00
|
|
|
addr_2,
|
2026-07-17 16:57:12 +03:00
|
|
|
Statistics {
|
2026-07-13 15:37:25 +03:00
|
|
|
latency_avg: 100_f32,
|
|
|
|
|
latency_var: 10_f32,
|
|
|
|
|
sample_size: 10,
|
|
|
|
|
latest_block_id: 98,
|
|
|
|
|
errors: 5,
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
2026-07-17 16:57:12 +03:00
|
|
|
statistics.insert(
|
2026-07-13 15:37:25 +03:00
|
|
|
addr_1,
|
2026-07-17 16:57:12 +03:00
|
|
|
Statistics {
|
2026-07-13 15:37:25 +03:00
|
|
|
latency_avg: 100_f32,
|
|
|
|
|
latency_var: 10_f32,
|
|
|
|
|
sample_size: 10,
|
|
|
|
|
latest_block_id: 99,
|
|
|
|
|
errors: 5,
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
2026-07-17 16:57:12 +03:00
|
|
|
statistics.insert(
|
2026-07-13 15:37:25 +03:00
|
|
|
addr_leader.clone(),
|
2026-07-17 16:57:12 +03:00
|
|
|
Statistics {
|
2026-07-13 15:37:25 +03:00
|
|
|
latency_avg: 100_f32,
|
|
|
|
|
latency_var: 10_f32,
|
|
|
|
|
sample_size: 10,
|
|
|
|
|
latest_block_id: 100,
|
|
|
|
|
errors: 5,
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
2026-07-24 13:46:45 +03:00
|
|
|
let leaders = choose_leaders(client_list, &statistics, 1).unwrap();
|
2026-07-13 15:37:25 +03:00
|
|
|
|
2026-07-21 18:01:04 +03:00
|
|
|
let (_, leader_url) = leaders.first().unwrap();
|
2026-07-20 17:10:01 +03:00
|
|
|
|
|
|
|
|
assert_eq!(leader_url, &addr_leader);
|
2026-07-13 15:37:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn choose_leader_least_errors() {
|
2026-07-22 10:13:01 +03:00
|
|
|
let (client_list, [addr_leader, addr_1, addr_2, addr_3]) = four_client_list();
|
2026-07-13 15:37:25 +03:00
|
|
|
|
2026-07-17 16:57:12 +03:00
|
|
|
let mut statistics = HashMap::new();
|
2026-07-13 15:37:25 +03:00
|
|
|
|
2026-07-17 16:57:12 +03:00
|
|
|
statistics.insert(
|
2026-07-13 15:37:25 +03:00
|
|
|
addr_3,
|
2026-07-17 16:57:12 +03:00
|
|
|
Statistics {
|
2026-07-13 15:37:25 +03:00
|
|
|
latency_avg: 100_f32,
|
|
|
|
|
latency_var: 10_f32,
|
|
|
|
|
sample_size: 10,
|
|
|
|
|
latest_block_id: 100,
|
|
|
|
|
errors: 5,
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
2026-07-17 16:57:12 +03:00
|
|
|
statistics.insert(
|
2026-07-13 15:37:25 +03:00
|
|
|
addr_2,
|
2026-07-17 16:57:12 +03:00
|
|
|
Statistics {
|
2026-07-13 15:37:25 +03:00
|
|
|
latency_avg: 100_f32,
|
|
|
|
|
latency_var: 10_f32,
|
|
|
|
|
sample_size: 10,
|
|
|
|
|
latest_block_id: 100,
|
|
|
|
|
errors: 4,
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
2026-07-17 16:57:12 +03:00
|
|
|
statistics.insert(
|
2026-07-13 15:37:25 +03:00
|
|
|
addr_1,
|
2026-07-17 16:57:12 +03:00
|
|
|
Statistics {
|
2026-07-13 15:37:25 +03:00
|
|
|
latency_avg: 100_f32,
|
|
|
|
|
latency_var: 10_f32,
|
|
|
|
|
sample_size: 10,
|
|
|
|
|
latest_block_id: 100,
|
|
|
|
|
errors: 3,
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
2026-07-17 16:57:12 +03:00
|
|
|
statistics.insert(
|
2026-07-13 15:37:25 +03:00
|
|
|
addr_leader.clone(),
|
2026-07-17 16:57:12 +03:00
|
|
|
Statistics {
|
2026-07-13 15:37:25 +03:00
|
|
|
latency_avg: 100_f32,
|
|
|
|
|
latency_var: 10_f32,
|
|
|
|
|
sample_size: 10,
|
|
|
|
|
latest_block_id: 100,
|
|
|
|
|
errors: 2,
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
2026-07-24 13:46:45 +03:00
|
|
|
let leaders = choose_leaders(client_list, &statistics, 1).unwrap();
|
2026-07-20 17:10:01 +03:00
|
|
|
|
2026-07-21 18:01:04 +03:00
|
|
|
let (_, leader_url) = leaders.first().unwrap();
|
2026-07-13 15:37:25 +03:00
|
|
|
|
2026-07-20 17:10:01 +03:00
|
|
|
assert_eq!(leader_url, &addr_leader);
|
2026-07-13 15:37:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn choose_leader_simple_latency_check() {
|
2026-07-22 10:13:01 +03:00
|
|
|
let (client_list, [addr_leader, addr_1, addr_2, addr_3]) = four_client_list();
|
2026-07-13 15:37:25 +03:00
|
|
|
|
2026-07-17 16:57:12 +03:00
|
|
|
let mut statistics = HashMap::new();
|
2026-07-13 15:37:25 +03:00
|
|
|
|
2026-07-17 16:57:12 +03:00
|
|
|
statistics.insert(
|
2026-07-13 15:37:25 +03:00
|
|
|
addr_3,
|
2026-07-17 16:57:12 +03:00
|
|
|
Statistics {
|
2026-07-13 15:37:25 +03:00
|
|
|
latency_avg: 103_f32,
|
|
|
|
|
latency_var: 10_f32,
|
|
|
|
|
sample_size: 10,
|
|
|
|
|
latest_block_id: 100,
|
|
|
|
|
errors: 5,
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
2026-07-17 16:57:12 +03:00
|
|
|
statistics.insert(
|
2026-07-13 15:37:25 +03:00
|
|
|
addr_2,
|
2026-07-17 16:57:12 +03:00
|
|
|
Statistics {
|
2026-07-13 15:37:25 +03:00
|
|
|
latency_avg: 102_f32,
|
|
|
|
|
latency_var: 10_f32,
|
|
|
|
|
sample_size: 10,
|
|
|
|
|
latest_block_id: 100,
|
|
|
|
|
errors: 5,
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
2026-07-17 16:57:12 +03:00
|
|
|
statistics.insert(
|
2026-07-13 15:37:25 +03:00
|
|
|
addr_1,
|
2026-07-17 16:57:12 +03:00
|
|
|
Statistics {
|
2026-07-13 15:37:25 +03:00
|
|
|
latency_avg: 101_f32,
|
|
|
|
|
latency_var: 10_f32,
|
|
|
|
|
sample_size: 10,
|
|
|
|
|
latest_block_id: 100,
|
|
|
|
|
errors: 5,
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
2026-07-17 16:57:12 +03:00
|
|
|
statistics.insert(
|
2026-07-13 15:37:25 +03:00
|
|
|
addr_leader.clone(),
|
2026-07-17 16:57:12 +03:00
|
|
|
Statistics {
|
2026-07-13 15:37:25 +03:00
|
|
|
latency_avg: 100_f32,
|
|
|
|
|
latency_var: 10_f32,
|
|
|
|
|
sample_size: 10,
|
|
|
|
|
latest_block_id: 100,
|
|
|
|
|
errors: 5,
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
2026-07-24 13:46:45 +03:00
|
|
|
let leaders = choose_leaders(client_list, &statistics, 1).unwrap();
|
2026-07-13 15:37:25 +03:00
|
|
|
|
2026-07-21 18:01:04 +03:00
|
|
|
let (_, leader_url) = leaders.first().unwrap();
|
2026-07-20 17:10:01 +03:00
|
|
|
|
|
|
|
|
assert_eq!(leader_url, &addr_leader);
|
2026-07-13 15:37:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn choose_leader_latency_var_check() {
|
2026-07-22 10:13:01 +03:00
|
|
|
let (client_list, [addr_leader, addr_1, addr_2, addr_3]) = four_client_list();
|
2026-07-13 15:37:25 +03:00
|
|
|
|
2026-07-17 16:57:12 +03:00
|
|
|
let mut statistics = HashMap::new();
|
2026-07-13 15:37:25 +03:00
|
|
|
|
2026-07-17 16:57:12 +03:00
|
|
|
statistics.insert(
|
2026-07-13 15:37:25 +03:00
|
|
|
addr_3,
|
2026-07-17 16:57:12 +03:00
|
|
|
Statistics {
|
2026-07-13 15:37:25 +03:00
|
|
|
latency_avg: 100_f32,
|
|
|
|
|
latency_var: 13_f32,
|
|
|
|
|
sample_size: 10,
|
|
|
|
|
latest_block_id: 100,
|
|
|
|
|
errors: 5,
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
2026-07-17 16:57:12 +03:00
|
|
|
statistics.insert(
|
2026-07-13 15:37:25 +03:00
|
|
|
addr_2,
|
2026-07-17 16:57:12 +03:00
|
|
|
Statistics {
|
2026-07-13 15:37:25 +03:00
|
|
|
latency_avg: 100_f32,
|
|
|
|
|
latency_var: 12_f32,
|
|
|
|
|
sample_size: 10,
|
|
|
|
|
latest_block_id: 100,
|
|
|
|
|
errors: 5,
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
2026-07-17 16:57:12 +03:00
|
|
|
statistics.insert(
|
2026-07-13 15:37:25 +03:00
|
|
|
addr_1,
|
2026-07-17 16:57:12 +03:00
|
|
|
Statistics {
|
2026-07-13 15:37:25 +03:00
|
|
|
latency_avg: 100_f32,
|
|
|
|
|
latency_var: 11_f32,
|
|
|
|
|
sample_size: 10,
|
|
|
|
|
latest_block_id: 100,
|
|
|
|
|
errors: 5,
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
2026-07-17 16:57:12 +03:00
|
|
|
statistics.insert(
|
2026-07-13 15:37:25 +03:00
|
|
|
addr_leader.clone(),
|
2026-07-17 16:57:12 +03:00
|
|
|
Statistics {
|
2026-07-13 15:37:25 +03:00
|
|
|
latency_avg: 100_f32,
|
|
|
|
|
latency_var: 10_f32,
|
|
|
|
|
sample_size: 10,
|
|
|
|
|
latest_block_id: 100,
|
|
|
|
|
errors: 5,
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
2026-07-24 13:46:45 +03:00
|
|
|
let leaders = choose_leaders(client_list, &statistics, 1).unwrap();
|
2026-07-20 17:10:01 +03:00
|
|
|
|
2026-07-21 18:01:04 +03:00
|
|
|
let (_, leader_url) = leaders.first().unwrap();
|
2026-07-13 15:37:25 +03:00
|
|
|
|
2026-07-20 17:10:01 +03:00
|
|
|
assert_eq!(leader_url, &addr_leader);
|
2026-07-13 15:37:25 +03:00
|
|
|
}
|
2026-07-22 10:13:01 +03:00
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn choose_multiple_leaders_latest_block() {
|
|
|
|
|
let (client_list, [addr_leader, addr_1, addr_2, addr_3]) = four_client_list();
|
|
|
|
|
|
|
|
|
|
let mut statistics = HashMap::new();
|
|
|
|
|
|
|
|
|
|
statistics.insert(
|
|
|
|
|
addr_3,
|
|
|
|
|
Statistics {
|
|
|
|
|
latency_avg: 100_f32,
|
|
|
|
|
latency_var: 10_f32,
|
|
|
|
|
sample_size: 10,
|
|
|
|
|
latest_block_id: 97,
|
|
|
|
|
errors: 5,
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
statistics.insert(
|
|
|
|
|
addr_2,
|
|
|
|
|
Statistics {
|
|
|
|
|
latency_avg: 100_f32,
|
|
|
|
|
latency_var: 10_f32,
|
|
|
|
|
sample_size: 10,
|
|
|
|
|
latest_block_id: 98,
|
|
|
|
|
errors: 5,
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
statistics.insert(
|
|
|
|
|
addr_1.clone(),
|
|
|
|
|
Statistics {
|
|
|
|
|
latency_avg: 100_f32,
|
|
|
|
|
latency_var: 10_f32,
|
|
|
|
|
sample_size: 10,
|
|
|
|
|
latest_block_id: 100,
|
|
|
|
|
errors: 5,
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
statistics.insert(
|
|
|
|
|
addr_leader.clone(),
|
|
|
|
|
Statistics {
|
|
|
|
|
latency_avg: 100_f32,
|
|
|
|
|
latency_var: 10_f32,
|
|
|
|
|
sample_size: 10,
|
|
|
|
|
latest_block_id: 100,
|
|
|
|
|
errors: 5,
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
2026-07-24 13:46:45 +03:00
|
|
|
let leaders = choose_leaders(client_list, &statistics, 2).unwrap();
|
2026-07-22 10:13:01 +03:00
|
|
|
|
|
|
|
|
let mut url_set_origin = HashSet::new();
|
|
|
|
|
let mut url_set_res = HashSet::new();
|
|
|
|
|
|
|
|
|
|
let (_, leader_url_first) = leaders[0].clone();
|
|
|
|
|
let (_, leader_url_second) = leaders[1].clone();
|
|
|
|
|
|
|
|
|
|
url_set_origin.insert(addr_leader);
|
|
|
|
|
url_set_origin.insert(addr_1);
|
|
|
|
|
|
|
|
|
|
url_set_res.insert(leader_url_first);
|
|
|
|
|
url_set_res.insert(leader_url_second);
|
|
|
|
|
|
|
|
|
|
assert_eq!(url_set_origin, url_set_res);
|
|
|
|
|
assert_eq!(leaders.len(), 2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn choose_multiple_leaders_latest_block_still_chooses_one_best() {
|
|
|
|
|
let (client_list, [addr_leader, addr_1, addr_2, addr_3]) = four_client_list();
|
|
|
|
|
|
|
|
|
|
let mut statistics = HashMap::new();
|
|
|
|
|
|
|
|
|
|
statistics.insert(
|
|
|
|
|
addr_3,
|
|
|
|
|
Statistics {
|
|
|
|
|
latency_avg: 100_f32,
|
|
|
|
|
latency_var: 10_f32,
|
|
|
|
|
sample_size: 10,
|
|
|
|
|
latest_block_id: 97,
|
|
|
|
|
errors: 5,
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
statistics.insert(
|
|
|
|
|
addr_2,
|
|
|
|
|
Statistics {
|
|
|
|
|
latency_avg: 100_f32,
|
|
|
|
|
latency_var: 10_f32,
|
|
|
|
|
sample_size: 10,
|
|
|
|
|
latest_block_id: 98,
|
|
|
|
|
errors: 5,
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
statistics.insert(
|
|
|
|
|
addr_1,
|
|
|
|
|
Statistics {
|
|
|
|
|
latency_avg: 100_f32,
|
|
|
|
|
latency_var: 10_f32,
|
|
|
|
|
sample_size: 10,
|
|
|
|
|
latest_block_id: 99,
|
|
|
|
|
errors: 5,
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
statistics.insert(
|
|
|
|
|
addr_leader.clone(),
|
|
|
|
|
Statistics {
|
|
|
|
|
latency_avg: 100_f32,
|
|
|
|
|
latency_var: 10_f32,
|
|
|
|
|
sample_size: 10,
|
|
|
|
|
latest_block_id: 100,
|
|
|
|
|
errors: 5,
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
2026-07-24 13:46:45 +03:00
|
|
|
let leaders = choose_leaders(client_list, &statistics, 2).unwrap();
|
2026-07-22 10:13:01 +03:00
|
|
|
|
|
|
|
|
let (_, helm) = leaders.first().unwrap();
|
|
|
|
|
|
|
|
|
|
assert_eq!(&addr_leader, helm);
|
|
|
|
|
assert_eq!(leaders.len(), 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn choose_multiple_leaders_least_errors() {
|
|
|
|
|
let (client_list, [addr_leader, addr_1, addr_2, addr_3]) = four_client_list();
|
|
|
|
|
|
|
|
|
|
let mut statistics = HashMap::new();
|
|
|
|
|
|
|
|
|
|
statistics.insert(
|
|
|
|
|
addr_3,
|
|
|
|
|
Statistics {
|
|
|
|
|
latency_avg: 100_f32,
|
|
|
|
|
latency_var: 10_f32,
|
|
|
|
|
sample_size: 10,
|
|
|
|
|
latest_block_id: 100,
|
|
|
|
|
errors: 6,
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
statistics.insert(
|
|
|
|
|
addr_2,
|
|
|
|
|
Statistics {
|
|
|
|
|
latency_avg: 100_f32,
|
|
|
|
|
latency_var: 10_f32,
|
|
|
|
|
sample_size: 10,
|
|
|
|
|
latest_block_id: 100,
|
|
|
|
|
errors: 6,
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
statistics.insert(
|
|
|
|
|
addr_1.clone(),
|
|
|
|
|
Statistics {
|
|
|
|
|
latency_avg: 100_f32,
|
|
|
|
|
latency_var: 10_f32,
|
|
|
|
|
sample_size: 10,
|
|
|
|
|
latest_block_id: 100,
|
|
|
|
|
errors: 3,
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
statistics.insert(
|
|
|
|
|
addr_leader.clone(),
|
|
|
|
|
Statistics {
|
|
|
|
|
latency_avg: 100_f32,
|
|
|
|
|
latency_var: 10_f32,
|
|
|
|
|
sample_size: 10,
|
|
|
|
|
latest_block_id: 100,
|
|
|
|
|
errors: 2,
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
2026-07-24 13:46:45 +03:00
|
|
|
let leaders = choose_leaders(client_list, &statistics, 2).unwrap();
|
2026-07-22 10:13:01 +03:00
|
|
|
|
|
|
|
|
let (_, leader_url_first) = leaders[0].clone();
|
|
|
|
|
let (_, leader_url_second) = leaders[1].clone();
|
|
|
|
|
|
|
|
|
|
assert_eq!(addr_leader, leader_url_first);
|
|
|
|
|
assert_eq!(addr_1, leader_url_second);
|
|
|
|
|
assert_eq!(leaders.len(), 2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn choose_multiple_leaders_simple_latency_check() {
|
|
|
|
|
let (client_list, [addr_leader, addr_1, addr_2, addr_3]) = four_client_list();
|
|
|
|
|
|
|
|
|
|
let mut statistics = HashMap::new();
|
|
|
|
|
|
|
|
|
|
statistics.insert(
|
|
|
|
|
addr_3,
|
|
|
|
|
Statistics {
|
|
|
|
|
latency_avg: 103_f32,
|
|
|
|
|
latency_var: 10_f32,
|
|
|
|
|
sample_size: 10,
|
|
|
|
|
latest_block_id: 100,
|
|
|
|
|
errors: 6,
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
statistics.insert(
|
|
|
|
|
addr_2,
|
|
|
|
|
Statistics {
|
|
|
|
|
latency_avg: 102_f32,
|
|
|
|
|
latency_var: 10_f32,
|
|
|
|
|
sample_size: 10,
|
|
|
|
|
latest_block_id: 100,
|
|
|
|
|
errors: 4,
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
statistics.insert(
|
|
|
|
|
addr_1.clone(),
|
|
|
|
|
Statistics {
|
|
|
|
|
latency_avg: 101_f32,
|
|
|
|
|
latency_var: 10_f32,
|
|
|
|
|
sample_size: 10,
|
|
|
|
|
latest_block_id: 100,
|
|
|
|
|
errors: 4,
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
statistics.insert(
|
|
|
|
|
addr_leader.clone(),
|
|
|
|
|
Statistics {
|
|
|
|
|
latency_avg: 100_f32,
|
|
|
|
|
latency_var: 10_f32,
|
|
|
|
|
sample_size: 10,
|
|
|
|
|
latest_block_id: 100,
|
|
|
|
|
errors: 4,
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
2026-07-24 13:46:45 +03:00
|
|
|
let leaders = choose_leaders(client_list, &statistics, 2).unwrap();
|
2026-07-22 10:13:01 +03:00
|
|
|
|
|
|
|
|
let (_, leader_url_first) = leaders[0].clone();
|
|
|
|
|
let (_, leader_url_second) = leaders[1].clone();
|
|
|
|
|
|
|
|
|
|
assert_eq!(addr_leader, leader_url_first);
|
|
|
|
|
assert_eq!(addr_1, leader_url_second);
|
|
|
|
|
assert_eq!(leaders.len(), 2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn choose_multiple_leaders_var_check() {
|
|
|
|
|
let (client_list, [addr_leader, addr_1, addr_2, addr_3]) = four_client_list();
|
|
|
|
|
|
|
|
|
|
let mut statistics = HashMap::new();
|
|
|
|
|
|
|
|
|
|
statistics.insert(
|
|
|
|
|
addr_3,
|
|
|
|
|
Statistics {
|
|
|
|
|
latency_avg: 103_f32,
|
|
|
|
|
latency_var: 10_f32,
|
|
|
|
|
sample_size: 10,
|
|
|
|
|
latest_block_id: 100,
|
|
|
|
|
errors: 6,
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
statistics.insert(
|
|
|
|
|
addr_2,
|
|
|
|
|
Statistics {
|
|
|
|
|
latency_avg: 100_f32,
|
|
|
|
|
latency_var: 12_f32,
|
|
|
|
|
sample_size: 10,
|
|
|
|
|
latest_block_id: 100,
|
|
|
|
|
errors: 4,
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
statistics.insert(
|
|
|
|
|
addr_1.clone(),
|
|
|
|
|
Statistics {
|
|
|
|
|
latency_avg: 100_f32,
|
|
|
|
|
latency_var: 11_f32,
|
|
|
|
|
sample_size: 10,
|
|
|
|
|
latest_block_id: 100,
|
|
|
|
|
errors: 4,
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
statistics.insert(
|
|
|
|
|
addr_leader.clone(),
|
|
|
|
|
Statistics {
|
|
|
|
|
latency_avg: 100_f32,
|
|
|
|
|
latency_var: 10_f32,
|
|
|
|
|
sample_size: 10,
|
|
|
|
|
latest_block_id: 100,
|
|
|
|
|
errors: 4,
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
2026-07-24 13:46:45 +03:00
|
|
|
let leaders = choose_leaders(client_list, &statistics, 2).unwrap();
|
2026-07-22 10:13:01 +03:00
|
|
|
|
|
|
|
|
let (_, leader_url_first) = leaders[0].clone();
|
|
|
|
|
let (_, leader_url_second) = leaders[1].clone();
|
|
|
|
|
|
|
|
|
|
assert_eq!(addr_leader, leader_url_first);
|
|
|
|
|
assert_eq!(addr_1, leader_url_second);
|
|
|
|
|
assert_eq!(leaders.len(), 2);
|
|
|
|
|
}
|
2026-07-09 15:58:29 +03:00
|
|
|
}
|