Skip to content

Commit 34e7f0e

Browse files
authored
A0-2097: refactor SessionBoundaries (#1028)
* refactor SessionBoundaries
1 parent d275c46 commit 34e7f0e

File tree

18 files changed

+209
-147
lines changed

18 files changed

+209
-147
lines changed

finality-aleph/src/abft/current.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
pub use aleph_primitives::CURRENT_FINALITY_VERSION as VERSION;
1+
pub use aleph_primitives::{BlockNumber, CURRENT_FINALITY_VERSION as VERSION};
22
use current_aleph_bft::{default_config, Config, LocalIO, Terminator};
33
use log::debug;
44
use sp_blockchain::HeaderBackend;
5-
use sp_runtime::traits::Block;
5+
use sp_runtime::traits::{Block, Header};
66

77
use crate::{
88
abft::{common::unit_creation_delay_fn, NetworkWrapper, SpawnHandleT},
@@ -17,11 +17,7 @@ use crate::{
1717
CurrentNetworkData, Hasher, Keychain, NodeIndex, SessionId, SignatureSet, UnitCreationDelay,
1818
};
1919

20-
pub fn run_member<
21-
B: Block,
22-
C: HeaderBackend<B> + Send + 'static,
23-
ADN: Network<CurrentNetworkData<B>> + 'static,
24-
>(
20+
pub fn run_member<B, C, ADN>(
2521
subtask_common: SubtaskCommon,
2622
multikeychain: Keychain,
2723
config: Config,
@@ -32,7 +28,13 @@ pub fn run_member<
3228
data_provider: impl current_aleph_bft::DataProvider<AlephData<B>> + Send + 'static,
3329
ordered_data_interpreter: OrderedDataInterpreter<B, C>,
3430
backup: ABFTBackup,
35-
) -> Task {
31+
) -> Task
32+
where
33+
B: Block,
34+
B::Header: Header<Number = BlockNumber>,
35+
C: HeaderBackend<B> + Send + 'static,
36+
ADN: Network<CurrentNetworkData<B>> + 'static,
37+
{
3638
let SubtaskCommon {
3739
spawn_handle,
3840
session_id,

finality-aleph/src/abft/legacy.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
pub use aleph_primitives::LEGACY_FINALITY_VERSION as VERSION;
1+
pub use aleph_primitives::{BlockNumber, LEGACY_FINALITY_VERSION as VERSION};
22
use legacy_aleph_bft::{default_config, Config, LocalIO, Terminator};
33
use log::debug;
44
use sp_blockchain::HeaderBackend;
5-
use sp_runtime::traits::Block;
5+
use sp_runtime::traits::{Block, Header};
66

77
use super::common::unit_creation_delay_fn;
88
use crate::{
@@ -17,19 +17,21 @@ use crate::{
1717
Keychain, LegacyNetworkData, NodeIndex, SessionId, UnitCreationDelay,
1818
};
1919

20-
pub fn run_member<
21-
B: Block,
22-
C: HeaderBackend<B> + Send + 'static,
23-
ADN: Network<LegacyNetworkData<B>> + 'static,
24-
>(
20+
pub fn run_member<B, C, ADN>(
2521
subtask_common: SubtaskCommon,
2622
multikeychain: Keychain,
2723
config: Config,
2824
network: NetworkWrapper<LegacyNetworkData<B>, ADN>,
2925
data_provider: impl legacy_aleph_bft::DataProvider<AlephData<B>> + Send + 'static,
3026
ordered_data_interpreter: OrderedDataInterpreter<B, C>,
3127
backup: ABFTBackup,
32-
) -> Task {
28+
) -> Task
29+
where
30+
B: Block,
31+
B::Header: Header<Number = BlockNumber>,
32+
C: HeaderBackend<B> + Send + 'static,
33+
ADN: Network<LegacyNetworkData<B>> + 'static,
34+
{
3335
let SubtaskCommon {
3436
spawn_handle,
3537
session_id,

finality-aleph/src/abft/traits.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
33
use std::{cmp::Ordering, fmt::Debug, hash::Hash as StdHash, marker::PhantomData, pin::Pin};
44

5+
use aleph_primitives::BlockNumber;
56
use codec::{Codec, Decode, Encode};
67
use futures::{channel::oneshot, Future, TryFutureExt};
78
use sc_service::SpawnTaskHandle;
8-
use sp_api::BlockT;
9+
use sp_api::{BlockT, HeaderT};
910
use sp_blockchain::HeaderBackend;
1011
use sp_runtime::traits::Hash as SpHash;
1112

@@ -30,16 +31,22 @@ impl<B: BlockT> legacy_aleph_bft::DataProvider<AlephData<B>> for DataProvider<B>
3031
}
3132
}
3233

33-
impl<B: BlockT, C: HeaderBackend<B> + Send + 'static>
34-
current_aleph_bft::FinalizationHandler<AlephData<B>> for OrderedDataInterpreter<B, C>
34+
impl<B, C> current_aleph_bft::FinalizationHandler<AlephData<B>> for OrderedDataInterpreter<B, C>
35+
where
36+
B: BlockT,
37+
B::Header: HeaderT<Number = BlockNumber>,
38+
C: HeaderBackend<B> + Send + 'static,
3539
{
3640
fn data_finalized(&mut self, data: AlephData<B>) {
3741
OrderedDataInterpreter::data_finalized(self, data)
3842
}
3943
}
4044

41-
impl<B: BlockT, C: HeaderBackend<B> + Send + 'static>
42-
legacy_aleph_bft::FinalizationHandler<AlephData<B>> for OrderedDataInterpreter<B, C>
45+
impl<B, C> legacy_aleph_bft::FinalizationHandler<AlephData<B>> for OrderedDataInterpreter<B, C>
46+
where
47+
B: BlockT,
48+
B::Header: HeaderT<Number = BlockNumber>,
49+
C: HeaderBackend<B> + Send + 'static,
4350
{
4451
fn data_finalized(&mut self, data: AlephData<B>) {
4552
OrderedDataInterpreter::data_finalized(self, data)

finality-aleph/src/data_io/chain_info.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
use std::sync::Arc;
22

3+
use aleph_primitives::BlockNumber;
34
use log::error;
45
use lru::LruCache;
56
use sc_client_api::HeaderBackend;
6-
use sp_runtime::traits::{Block as BlockT, Header as HeaderT, NumberFor, One};
7+
use sp_runtime::traits::{Block as BlockT, Header as HeaderT, NumberFor};
78

89
use crate::{data_io::ChainInfoCacheConfig, BlockHashNum};
910

@@ -20,6 +21,7 @@ pub trait ChainInfoProvider<B: BlockT> {
2021
impl<C, B> ChainInfoProvider<B> for Arc<C>
2122
where
2223
B: BlockT,
24+
B::Header: HeaderT<Number = BlockNumber>,
2325
C: HeaderBackend<B>,
2426
{
2527
fn is_block_imported(&mut self, block: &BlockHashNum<B>) -> bool {
@@ -98,6 +100,7 @@ where
98100
impl<B, CIP> ChainInfoProvider<B> for CachedChainInfoProvider<B, CIP>
99101
where
100102
B: BlockT,
103+
B::Header: HeaderT<Number = BlockNumber>,
101104
CIP: ChainInfoProvider<B>,
102105
{
103106
fn is_block_imported(&mut self, block: &BlockHashNum<B>) -> bool {
@@ -179,6 +182,7 @@ where
179182
impl<B, CIP> ChainInfoProvider<B> for AuxFinalizationChainInfoProvider<B, CIP>
180183
where
181184
B: BlockT,
185+
B::Header: HeaderT<Number = BlockNumber>,
182186
CIP: ChainInfoProvider<B>,
183187
{
184188
fn is_block_imported(&mut self, block: &BlockHashNum<B>) -> bool {
@@ -197,7 +201,7 @@ where
197201
let mut curr_block = self.aux_finalized.clone();
198202
while curr_block.num > num {
199203
let parent_hash = self.chain_info_provider.get_parent_hash(&curr_block)?;
200-
curr_block = (parent_hash, curr_block.num - NumberFor::<B>::one()).into();
204+
curr_block = (parent_hash, curr_block.num - 1).into();
201205
}
202206
Ok(curr_block)
203207
}

finality-aleph/src/data_io/data_interpreter.rs

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
use std::{default::Default, sync::Arc};
22

3+
use aleph_primitives::BlockNumber;
34
use futures::channel::mpsc;
45
use log::{debug, error, warn};
56
use sc_client_api::HeaderBackend;
6-
use sp_runtime::traits::{Block as BlockT, NumberFor, One, Zero};
7+
use sp_runtime::traits::{Block as BlockT, Header as HeaderT, NumberFor, Zero};
78

89
use crate::{
910
data_io::{
@@ -21,20 +22,30 @@ type InterpretersChainInfoProvider<B, C> =
2122
/// Takes as input ordered `AlephData` from `AlephBFT` and pushes blocks that should be finalized
2223
/// to an output channel. The other end of the channel is held by the aggregator whose goal is to
2324
/// create multisignatures under the finalized blocks.
24-
pub struct OrderedDataInterpreter<B: BlockT, C: HeaderBackend<B>> {
25+
pub struct OrderedDataInterpreter<B, C>
26+
where
27+
B: BlockT,
28+
B::Header: HeaderT<Number = BlockNumber>,
29+
C: HeaderBackend<B>,
30+
{
2531
blocks_to_finalize_tx: mpsc::UnboundedSender<BlockHashNum<B>>,
2632
chain_info_provider: InterpretersChainInfoProvider<B, C>,
2733
last_finalized_by_aleph: BlockHashNum<B>,
28-
session_boundaries: SessionBoundaries<B>,
34+
session_boundaries: SessionBoundaries,
2935
}
3036

31-
fn get_last_block_prev_session<B: BlockT, C: HeaderBackend<B>>(
32-
session_boundaries: SessionBoundaries<B>,
37+
fn get_last_block_prev_session<B, C>(
38+
session_boundaries: SessionBoundaries,
3339
mut client: Arc<C>,
34-
) -> BlockHashNum<B> {
35-
if session_boundaries.first_block() > NumberFor::<B>::zero() {
40+
) -> BlockHashNum<B>
41+
where
42+
B: BlockT,
43+
B::Header: HeaderT<Number = BlockNumber>,
44+
C: HeaderBackend<B>,
45+
{
46+
if session_boundaries.first_block() > 0 {
3647
// We are in session > 0, we take the last block of previous session.
37-
let last_prev_session_num = session_boundaries.first_block() - NumberFor::<B>::one();
48+
let last_prev_session_num = session_boundaries.first_block() - 1;
3849
client.get_finalized_at(last_prev_session_num).expect(
3950
"Last block of previous session must have been finalized before starting the current",
4051
)
@@ -46,11 +57,16 @@ fn get_last_block_prev_session<B: BlockT, C: HeaderBackend<B>>(
4657
}
4758
}
4859

49-
impl<B: BlockT, C: HeaderBackend<B>> OrderedDataInterpreter<B, C> {
60+
impl<B, C> OrderedDataInterpreter<B, C>
61+
where
62+
B: BlockT,
63+
B::Header: HeaderT<Number = BlockNumber>,
64+
C: HeaderBackend<B>,
65+
{
5066
pub fn new(
5167
blocks_to_finalize_tx: mpsc::UnboundedSender<BlockHashNum<B>>,
5268
client: Arc<C>,
53-
session_boundaries: SessionBoundaries<B>,
69+
session_boundaries: SessionBoundaries,
5470
) -> Self {
5571
let last_finalized_by_aleph =
5672
get_last_block_prev_session(session_boundaries.clone(), client.clone());

finality-aleph/src/data_io/data_provider.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
use std::{sync::Arc, time::Duration};
22

3+
use aleph_primitives::BlockNumber;
34
use futures::channel::oneshot;
45
use log::{debug, warn};
56
use parking_lot::Mutex;
67
use sc_client_api::HeaderBackend;
78
use sp_consensus::SelectChain;
89
use sp_runtime::{
9-
traits::{Block as BlockT, Header as HeaderT, NumberFor, One, Zero},
10+
traits::{Block as BlockT, Header as HeaderT, NumberFor, Zero},
1011
SaturatedConversion,
1112
};
1213

@@ -41,13 +42,14 @@ where
4142
pub fn get_parent<B, C>(client: &C, block: &BlockHashNum<B>) -> Option<BlockHashNum<B>>
4243
where
4344
B: BlockT,
45+
B::Header: HeaderT<Number = BlockNumber>,
4446
C: HeaderBackend<B>,
4547
{
4648
if block.num.is_zero() {
4749
return None;
4850
}
4951
if let Some(header) = client.header(block.hash).expect("client must respond") {
50-
Some((*header.parent_hash(), block.num - <NumberFor<B>>::one()).into())
52+
Some((*header.parent_hash(), block.num - 1).into())
5153
} else {
5254
warn!(target: "aleph-data-store", "Trying to fetch the parent of an unknown block {:?}.", block);
5355
None
@@ -61,6 +63,7 @@ pub fn get_proposal<B, C>(
6163
) -> Result<AlephData<B>, ()>
6264
where
6365
B: BlockT,
66+
B::Header: HeaderT<Number = BlockNumber>,
6467
C: HeaderBackend<B>,
6568
{
6669
let mut curr_block = best_block;
@@ -115,27 +118,29 @@ struct ChainInfo<B: BlockT> {
115118
pub struct ChainTracker<B, SC, C>
116119
where
117120
B: BlockT,
121+
B::Header: HeaderT<Number = BlockNumber>,
118122
C: HeaderBackend<B> + 'static,
119123
SC: SelectChain<B> + 'static,
120124
{
121125
select_chain: SC,
122126
client: Arc<C>,
123127
data_to_propose: Arc<Mutex<Option<AlephData<B>>>>,
124-
session_boundaries: SessionBoundaries<B>,
128+
session_boundaries: SessionBoundaries,
125129
prev_chain_info: Option<ChainInfo<B>>,
126130
config: ChainTrackerConfig,
127131
}
128132

129133
impl<B, SC, C> ChainTracker<B, SC, C>
130134
where
131135
B: BlockT,
136+
B::Header: HeaderT<Number = BlockNumber>,
132137
C: HeaderBackend<B> + 'static,
133138
SC: SelectChain<B> + 'static,
134139
{
135140
pub fn new(
136141
select_chain: SC,
137142
client: Arc<C>,
138-
session_boundaries: SessionBoundaries<B>,
143+
session_boundaries: SessionBoundaries,
139144
config: ChainTrackerConfig,
140145
metrics: Option<Metrics<<B::Header as HeaderT>::Hash>>,
141146
) -> (Self, DataProvider<B>) {
@@ -335,7 +340,7 @@ mod tests {
335340
client_chain_builder::ClientChainBuilder,
336341
mocks::{aleph_data_from_blocks, TBlock, TestClientBuilder, TestClientBuilderExt},
337342
},
338-
SessionBoundaries, SessionId, SessionPeriod,
343+
SessionBoundaryInfo, SessionId, SessionPeriod,
339344
};
340345

341346
const SESSION_LEN: u32 = 100;
@@ -354,7 +359,8 @@ mod tests {
354359

355360
let chain_builder =
356361
ClientChainBuilder::new(client.clone(), Arc::new(TestClientBuilder::new().build()));
357-
let session_boundaries = SessionBoundaries::new(SessionId(0), SessionPeriod(SESSION_LEN));
362+
let session_boundaries = SessionBoundaryInfo::new(SessionPeriod(SESSION_LEN))
363+
.boundaries_for_session(SessionId(0));
358364

359365
let config = ChainTrackerConfig {
360366
refresh_interval: REFRESH_INTERVAL,

finality-aleph/src/data_io/data_store.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::{
66
time::{self, Duration},
77
};
88

9+
use aleph_primitives::BlockNumber;
910
use futures::{
1011
channel::{
1112
mpsc::{self, UnboundedSender},
@@ -176,6 +177,7 @@ impl Default for DataStoreConfig {
176177
pub struct DataStore<B, C, RB, Message, R>
177178
where
178179
B: BlockT,
180+
B::Header: HeaderT<Number = BlockNumber>,
179181
C: HeaderBackend<B> + BlockchainEvents<B> + Send + Sync + 'static,
180182
RB: RequestBlocks<B> + 'static,
181183
Message:
@@ -192,7 +194,7 @@ where
192194
available_proposals_cache: LruCache<AlephProposal<B>, ProposalStatus<B>>,
193195
num_triggers_registered_since_last_pruning: usize,
194196
highest_finalized_num: NumberFor<B>,
195-
session_boundaries: SessionBoundaries<B>,
197+
session_boundaries: SessionBoundaries,
196198
client: Arc<C>,
197199
block_requester: RB,
198200
config: DataStoreConfig,
@@ -203,6 +205,7 @@ where
203205
impl<B, C, RB, Message, R> DataStore<B, C, RB, Message, R>
204206
where
205207
B: BlockT,
208+
B::Header: HeaderT<Number = BlockNumber>,
206209
C: HeaderBackend<B> + BlockchainEvents<B> + Send + Sync + 'static,
207210
RB: RequestBlocks<B> + 'static,
208211
Message:
@@ -211,7 +214,7 @@ where
211214
{
212215
/// Returns a struct to be run and a network that outputs messages filtered as appropriate
213216
pub fn new<N: ComponentNetwork<Message, R = R>>(
214-
session_boundaries: SessionBoundaries<B>,
217+
session_boundaries: SessionBoundaries,
215218
client: Arc<C>,
216219
block_requester: RB,
217220
config: DataStoreConfig,

0 commit comments

Comments
 (0)