#![forbid(
arithmetic_overflow,
mutable_transmutes,
no_mangle_const_items,
unknown_crate_types,
unsafe_code
)]
#![warn(
trivial_casts,
trivial_numeric_casts,
unreachable_pub,
unused_extern_crates,
unused_import_braces,
unused_qualifications,
clippy::unicode_not_nfc,
clippy::unwrap_used
)]
pub mod dbcs;
pub mod messaging;
pub mod network_knowledge;
pub mod types;
pub mod statemap;
#[macro_use]
extern crate tracing;
pub use network_knowledge::{elder_count, SectionAuthorityProvider};
const DEFAULT_DATA_COPY_COUNT: usize = 4;
const SN_DATA_COPY_COUNT: &str = "SN_DATA_COPY_COUNT";
pub fn max_num_faulty_elders() -> usize {
elder_count() / 3
}
pub fn max_num_faulty_elders_for_sap(sap: SectionAuthorityProvider) -> usize {
sap.elder_count() / 3
}
pub fn at_least_one_correct_elder() -> usize {
max_num_faulty_elders() + 1
}
pub fn data_copy_count() -> usize {
match std::env::var(SN_DATA_COPY_COUNT) {
Ok(count) => match count.parse() {
Ok(count) => {
warn!(
"data_copy_count countout set from env var SN_DATA_COPY_COUNT: {:?}",
SN_DATA_COPY_COUNT
);
count
}
Err(error) => {
warn!("There was an error parsing {:?} env var. DEFAULT_DATA_COPY_COUNT will be used: {:?}", SN_DATA_COPY_COUNT, error);
DEFAULT_DATA_COPY_COUNT
}
},
Err(_) => DEFAULT_DATA_COPY_COUNT,
}
}
use tracing_core::{Event, Subscriber};
use tracing_subscriber::{
fmt::{
format::Writer,
time::{FormatTime, SystemTime},
FmtContext, FormatEvent, FormatFields,
},
registry::LookupSpan,
};
#[derive(Default, Debug)]
pub struct LogFormatter;
impl<S, N> FormatEvent<S, N> for LogFormatter
where
S: Subscriber + for<'a> LookupSpan<'a>,
N: for<'a> FormatFields<'a> + 'static,
{
fn format_event(
&self,
ctx: &FmtContext<'_, S, N>,
mut writer: Writer,
event: &Event<'_>,
) -> std::fmt::Result {
let level = *event.metadata().level();
let module = event.metadata().module_path().unwrap_or("<unknown module>");
let time = SystemTime::default();
write!(writer, "[")?;
time.format_time(&mut writer)?;
write!(writer, " {level} {module}")?;
ctx.visit_spans(|span| write!(writer, "/{}", span.name()))?;
write!(writer, "] ")?;
ctx.field_format().format_fields(writer.by_ref(), event)?;
writeln!(writer)
}
}
#[cfg(feature = "test-utils")]
use std::sync::Once;
#[cfg(feature = "test-utils")]
static INIT: Once = Once::new();
#[cfg(feature = "test-utils")]
pub fn init_logger() {
INIT.call_once(|| {
tracing_subscriber::fmt::fmt()
.with_ansi(false)
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
.with_target(false)
.event_format(LogFormatter::default())
.try_init()
.unwrap_or_else(|_| println!("Error initializing logger"));
});
}
#[cfg(any(test, feature = "test-utils"))]
pub mod test_utils {
pub use crate::{
network_knowledge::{
section_authority_provider::test_utils::*, test_utils::*, test_utils_st::*,
},
types::keys::test_utils::*,
};
}