Skip to content

Commit e879e44

Browse files
Improve function docs, reorder imports.
1 parent aadda9d commit e879e44

File tree

1 file changed

+38
-18
lines changed
  • nomos-services/cryptarchia-consensus/src

1 file changed

+38
-18
lines changed

nomos-services/cryptarchia-consensus/src/lib.rs

+38-18
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,24 @@ mod states;
77
pub mod storage;
88
mod time;
99

10+
// std
11+
use core::fmt::Debug;
12+
use futures::StreamExt;
13+
use std::collections::BTreeSet;
14+
use std::hash::Hash;
15+
use std::path::PathBuf;
16+
// Crates
17+
use rand::{RngCore, SeedableRng};
18+
use serde::{de::DeserializeOwned, Deserialize, Serialize};
19+
use serde_with::serde_as;
20+
use thiserror::Error;
21+
pub use time::Config as TimeConfig;
22+
use tokio::sync::oneshot::Sender;
23+
use tokio::sync::{broadcast, oneshot};
24+
use tokio_stream::wrappers::IntervalStream;
25+
use tracing::{error, info, instrument, span, Level};
26+
use tracing_futures::Instrument;
27+
// Internal
1028
use crate::leadership::Leader;
1129
use crate::relays::CryptarchiaConsensusRelays;
1230
use crate::states::{
@@ -15,9 +33,7 @@ use crate::states::{
1533
};
1634
use crate::storage::adapters::StorageAdapter;
1735
use crate::storage::StorageAdapter as _;
18-
use core::fmt::Debug;
1936
use cryptarchia_engine::Slot;
20-
use futures::StreamExt;
2137
pub use leadership::LeaderConfig;
2238
use network::NetworkAdapter;
2339
use nomos_core::da::blob::{
@@ -41,22 +57,9 @@ use nomos_storage::{backends::StorageBackend, StorageMsg, StorageService};
4157
use overwatch_rs::services::relay::{OutboundRelay, Relay, RelayMessage};
4258
use overwatch_rs::services::{ServiceCore, ServiceData, ServiceId};
4359
use overwatch_rs::{DynError, OpaqueServiceStateHandle};
44-
use rand::{RngCore, SeedableRng};
45-
use serde::{de::DeserializeOwned, Deserialize, Serialize};
46-
use serde_with::serde_as;
4760
use services_utils::overwatch::lifecycle;
4861
use services_utils::overwatch::recovery::backends::FileBackendSettings;
4962
use services_utils::overwatch::{JsonFileBackend, RecoveryOperator};
50-
use std::collections::BTreeSet;
51-
use std::hash::Hash;
52-
use std::path::PathBuf;
53-
use thiserror::Error;
54-
pub use time::Config as TimeConfig;
55-
use tokio::sync::oneshot::Sender;
56-
use tokio::sync::{broadcast, oneshot};
57-
use tokio_stream::wrappers::IntervalStream;
58-
use tracing::{error, info, instrument, span, Level};
59-
use tracing_futures::Instrument;
6063

6164
type MempoolRelay<Payload, Item, Key> = OutboundRelay<MempoolMsg<HeaderId, Payload, Item, Key>>;
6265
type SamplingRelay<BlobId> = OutboundRelay<DaSamplingServiceMsg<BlobId>>;
@@ -853,13 +856,30 @@ where
853856
}
854857

855858
/// Retrieves the blocks in the range from `from` to `to` from the storage.
856-
/// Both `from` and `to` are included in the range and must be valid headers.
859+
/// Both `from` and `to` are included in the range.
860+
/// This is implemented here, and not as a method of `StorageAdapter`, to simplify the panic
861+
/// and error message handling.
862+
///
863+
/// # Panics
864+
///
865+
/// Panics if any of the blocks in the range are not found in the storage.
866+
///
867+
/// # Parameters
868+
///
869+
/// * `from` - The header id of the first block in the range. Must be a valid header.
870+
/// * `to` - The header id of the last block in the range. Must be a valid header.
871+
///
872+
/// # Returns
873+
///
874+
/// A vector of blocks in the range from `from` to `to`.
875+
/// If no blocks are found, returns an empty vector.
876+
/// If any of the HeaderId are invalid, returns an error with the first invalid header id.
857877
async fn get_blocks_in_range(
858878
from: HeaderId,
859879
to: HeaderId,
860880
storage_adapter: &StorageAdapter<Storage, TxS::Tx, BS::BlobId>,
861881
) -> Vec<Block<ClPool::Item, DaPool::Item>> {
862-
// `blocks` is in `to..from` order
882+
// Due to the blocks traversal order, this yields `to..from` order
863883
let blocks = futures::stream::unfold(to, |header_id| async move {
864884
if header_id == from {
865885
None
@@ -875,7 +895,7 @@ where
875895
}
876896
});
877897

878-
// To avoid confusion, the order is reversed so it fits in the natural `from..to` order
898+
// To avoid confusion, the order is reversed so it fits the natural `from..to` order
879899
blocks.collect::<Vec<_>>().await.into_iter().rev().collect()
880900
}
881901

0 commit comments

Comments
 (0)