Skip to content
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions bin/ream/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,15 +160,19 @@ pub async fn run_lean_node(config: LeanNodeConfig, executor: ReamExecutor) {

// Start the services concurrently.
let chain_future = executor.spawn(async move {
chain_service.start().await;
if let Err(err) = chain_service.start().await {
panic!("Chain service exited with error: {err}");
}
});
let network_future = executor.spawn(async move {
if let Err(err) = network_service.start().await {
panic!("Network service exited with error: {err}");
}
});
let validator_future = executor.spawn(async move {
validator_service.start().await;
if let Err(err) = validator_service.start().await {
panic!("Validator service exited with error: {err}");
}
});

tokio::select! {
Expand Down
23 changes: 23 additions & 0 deletions crates/common/chain/lean/src/clock.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use std::time::{Duration, SystemTime, UNIX_EPOCH};

use anyhow::anyhow;
use ream_consensus_misc::constants::lean::INTERVALS_PER_SLOT;
use ream_network_spec::networks::lean_network_spec;
use tokio::time::{Instant, Interval, MissedTickBehavior, interval_at};

pub fn create_lean_clock_interval() -> anyhow::Result<Interval> {
let genesis_instant = UNIX_EPOCH + Duration::from_secs(lean_network_spec().genesis_time);

let interval_start = Instant::now()
+ genesis_instant
.duration_since(SystemTime::now())
.map_err(|err| anyhow!("Genesis time is in the past: {err}"))?;

let mut interval = interval_at(
interval_start,
Duration::from_secs(lean_network_spec().seconds_per_slot / INTERVALS_PER_SLOT),
);
interval.set_missed_tick_behavior(MissedTickBehavior::Burst);

Ok(interval)
}
1 change: 1 addition & 0 deletions crates/common/chain/lean/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod clock;
pub mod genesis;
pub mod lean_chain;
pub mod service;
Expand Down
47 changes: 12 additions & 35 deletions crates/common/chain/lean/src/service.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
use std::{
collections::HashMap,
sync::Arc,
time::{Duration, SystemTime, UNIX_EPOCH},
};
use std::{collections::HashMap, sync::Arc};

use alloy_primitives::B256;
use anyhow::anyhow;
use ream_consensus_lean::{QueueItem, VoteItem, block::Block, process_block};
use ream_consensus_misc::constants::lean::INTERVALS_PER_SLOT;
use ream_network_spec::networks::lean_network_spec;
use tokio::{
sync::{RwLock, mpsc},
time::{Instant, MissedTickBehavior, interval_at},
};
use tokio::sync::{RwLock, mpsc};
use tracing::info;
use tree_hash::TreeHash;

use crate::{lean_chain::LeanChain, slot::get_current_slot};
use crate::{clock::create_lean_clock_interval, lean_chain::LeanChain, slot::get_current_slot};

#[derive(Debug, Clone)]
pub struct LeanChainServiceMessage {
Expand Down Expand Up @@ -50,32 +43,16 @@ impl LeanChainService {
}
}

pub async fn start(mut self) {
// TODO: Duplicate clock logic from ValidatorService. May need to refactor later.

// Get the Lean network specification.
let network_spec = lean_network_spec();
let seconds_per_slot = network_spec.seconds_per_slot;
let genesis_time = network_spec.genesis_time;

info!("LeanChainService started with genesis_time={genesis_time}");

// Calculate the genesis instant from the genesis time (in seconds).
let genesis_instant = UNIX_EPOCH + Duration::from_secs(genesis_time);

// Assume genesis time is "always" in the future,
// as we don't support syncing features yet.
let interval_start = Instant::now()
+ genesis_instant
.duration_since(SystemTime::now())
.expect("Genesis time is in the past");
pub async fn start(mut self) -> anyhow::Result<()> {
info!(
"LeanChainService started with genesis_time: {}",
lean_network_spec().genesis_time
);

let mut tick_count = 0u64;
let mut interval = interval_at(
interval_start,
Duration::from_secs(seconds_per_slot / INTERVALS_PER_SLOT),
);
interval.set_missed_tick_behavior(MissedTickBehavior::Burst);

let mut interval = create_lean_clock_interval()
.map_err(|err| anyhow!("Failed to create clock interval: {err}"))?;

loop {
tokio::select! {
Expand Down
1 change: 1 addition & 0 deletions crates/common/validator/lean/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ rust-version.workspace = true
version.workspace = true

[dependencies]
anyhow.workspace = true
tokio.workspace = true
tracing = { workspace = true, features = ["log"] }

Expand Down
47 changes: 12 additions & 35 deletions crates/common/validator/lean/src/service.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
use std::{
sync::Arc,
time::{Duration, SystemTime, UNIX_EPOCH},
};
use std::sync::Arc;

use anyhow::anyhow;
use ream_chain_lean::{
lean_chain::LeanChain, service::LeanChainServiceMessage, slot::get_current_slot,
clock::create_lean_clock_interval, lean_chain::LeanChain, service::LeanChainServiceMessage,
slot::get_current_slot,
};
use ream_consensus_lean::{QueueItem, VoteItem};
use ream_consensus_misc::constants::lean::INTERVALS_PER_SLOT;
use ream_network_spec::networks::lean_network_spec;
use tokio::{
sync::{RwLock, mpsc},
time::{Instant, MissedTickBehavior, interval_at},
};
use tokio::sync::{RwLock, mpsc};
use tracing::info;

// TODO: We need to replace this after PQC integration.
Expand Down Expand Up @@ -60,35 +55,17 @@ impl ValidatorService {
}
}

pub async fn start(self) {
// TODO: Duplicate clock logic from LeanChainService. May need to refactor later.

// Get the Lean network specification.
let network_spec = lean_network_spec();
let seconds_per_slot = network_spec.seconds_per_slot;
let genesis_time = network_spec.genesis_time;

pub async fn start(self) -> anyhow::Result<()> {
info!(
"ValidatorService started with {} validator(s), genesis_time={genesis_time}",
self.keystores.len()
"ValidatorService started with {} validator(s), genesis_time: {}",
self.keystores.len(),
lean_network_spec().genesis_time
);

// Calculate the genesis instant from the genesis time (in seconds).
let genesis_instant = UNIX_EPOCH + Duration::from_secs(genesis_time);

// Assume genesis time is "always" in the future,
// as we don't support syncing features yet.
let interval_start = Instant::now()
+ genesis_instant
.duration_since(SystemTime::now())
.expect("Genesis time is in the past");

let mut tick_count = 0u64;
let mut interval = interval_at(
interval_start,
Duration::from_secs(seconds_per_slot / INTERVALS_PER_SLOT),
);
interval.set_missed_tick_behavior(MissedTickBehavior::Burst);

let mut interval = create_lean_clock_interval()
.map_err(|err| anyhow!("Failed to create clock interval: {err}"))?;

loop {
tokio::select! {
Expand Down