snarkos_node_bft/lib.rs
1// Copyright (c) 2019-2026 Provable Inc.
2// This file is part of the snarkOS library.
3
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at:
7
8// http://www.apache.org/licenses/LICENSE-2.0
9
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16#![forbid(unsafe_code)]
17#![allow(clippy::blocks_in_conditions)]
18#![allow(clippy::type_complexity)]
19
20#[macro_use]
21extern crate async_trait;
22#[macro_use]
23extern crate tracing;
24
25#[cfg(feature = "metrics")]
26extern crate snarkos_node_metrics as metrics;
27
28pub use snarkos_node_bft_events as events;
29pub use snarkos_node_bft_ledger_service as ledger_service;
30pub use snarkos_node_bft_storage_service as storage_service;
31
32use std::time::Duration;
33
34pub mod helpers;
35
36mod bft;
37pub use bft::*;
38
39mod gateway;
40pub use gateway::*;
41
42mod primary;
43pub use primary::*;
44
45mod sync;
46pub use sync::*;
47
48mod worker;
49pub use worker::*;
50
51pub const CONTEXT: &str = "[MemoryPool]";
52
53/// The port on which the memory pool listens for incoming connections.
54pub const MEMORY_POOL_PORT: u16 = 5000; // port
55
56/// The maximum time to wait before proposing a batch.
57pub const MAX_BATCH_DELAY: Duration = Duration::from_millis(2500);
58
59/// The minimum time that needs to elapse between two consecutive batch proposals.
60/// This creates a lower bound on the block interval, and ensures the network will not be overwhelmed with too many blocks/certificates.
61pub const MIN_BATCH_DELAY: Duration = Duration::from_secs(1);
62
63/// The time a primary waits between attempts to create a new batch (only relevant after `MIN_BATCH_DELAY` has passed).
64/// This only serves as a failsafe in case the task does not get woken up through other means.
65/// Lowering it too much would be wasteful.
66pub const CREATE_BATCH_INTERVAL: Duration = Duration::from_millis(250);
67
68/// The maximum time to wait before timing out on a fetch.
69/// TODO(kaimast): directy multiply by constant once the `const_trait_impl` feature is stable.
70pub const MAX_FETCH_TIMEOUT: Duration = Duration::from_millis(3 * (MAX_BATCH_DELAY.as_millis() as u64));
71
72/// The maximum time allowed for the leader to send their certificate.
73/// After this time, the node will consider the leader as failed and try to advance the round without it.
74pub const MAX_LEADER_CERTIFICATE_DELAY: Duration = Duration::from_millis(2 * (MAX_BATCH_DELAY.as_millis() as u64));
75
76/// The maximum difference allowed between our local time and a certificate's timestamp, for the node to sign the certificate.
77/// This prevents malicious actors from proposing certificates with timestamps that are too log or too far in the future)
78/// w
79pub const MAX_TIMESTAMP_DELTA: Duration = Duration::from_secs(10);
80
81/// The maximum number of workers that can be spawned.
82pub const MAX_WORKERS: u8 = 1; // worker(s)
83
84/// The interval at which each primary broadcasts a ping to every other node.
85/// Note: If this is updated, be sure to update `MAX_BLOCKS_BEHIND` to correspond properly.
86pub const PRIMARY_PING_INTERVAL: Duration = Duration::from_millis(2 * (MAX_BATCH_DELAY.as_millis() as u64));
87
88/// The interval at which each worker broadcasts a ping to every other node.
89pub const WORKER_PING_INTERVAL: Duration = Duration::from_millis(4 * (MAX_BATCH_DELAY.as_millis() as u64));
90
91/// A helper macro to spawn a blocking task.
92#[macro_export]
93macro_rules! spawn_blocking {
94 ($expr:expr) => {
95 match tokio::task::spawn_blocking(move || $expr).await {
96 Ok(value) => value,
97 Err(error) => Err(anyhow::anyhow!("[tokio::spawn_blocking] {error}")),
98 }
99 };
100}