Skip to content
This repository was archived by the owner on Jun 17, 2022. It is now read-only.

Commit a71f3ff

Browse files
committed
Patch Slots
1 parent 3b3246b commit a71f3ff

File tree

2 files changed

+36
-29
lines changed

2 files changed

+36
-29
lines changed

client/consensus/slots/src/slots.rs

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,47 @@
1-
// Copyright 2019-2020 Parity Technologies (UK) Ltd.
2-
// This file is part of Substrate.
1+
// Copyright 2020 Selendra.
2+
// This file is part of Indracore.
33

4-
// Substrate is free software: you can redistribute it and/or modify
4+
// Indracore is free software: you can redistribute it and/or modify
55
// it under the terms of the GNU General Public License as published by
66
// the Free Software Foundation, either version 3 of the License, or
77
// (at your option) any later version.
88

9-
// Substrate is distributed in the hope that it will be useful,
9+
// Indracore is distributed in the hope that it will be useful,
1010
// but WITHOUT ANY WARRANTY; without even the implied warranty of
1111
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1212
// GNU General Public License for more details.
1313

1414
// You should have received a copy of the GNU General Public License
15-
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
15+
// along with Indracore. If not, see <http://www.gnu.org/licenses/>.
1616

1717
//! Utility stream for yielding slots in a loop.
1818
//!
1919
//! This is used instead of `futures_timer::Interval` because it was unreliable.
2020
2121
use super::SlotCompatible;
22-
use sp_consensus::Error;
2322
use futures::{prelude::*, task::Context, task::Poll};
23+
use sp_consensus::Error;
2424
use sp_inherents::{InherentData, InherentDataProviders};
2525

26-
use std::{pin::Pin, time::{Duration, Instant}};
2726
use futures_timer::Delay;
27+
use std::{
28+
pin::Pin,
29+
time::{Duration, Instant},
30+
};
2831

2932
/// Returns current duration since unix epoch.
3033
pub fn duration_now() -> Duration {
3134
use std::time::SystemTime;
3235
let now = SystemTime::now();
33-
now.duration_since(SystemTime::UNIX_EPOCH).unwrap_or_else(|e| panic!(
34-
"Current time {:?} is before unix epoch. Something is wrong: {:?}",
35-
now,
36-
e,
37-
))
36+
now.duration_since(SystemTime::UNIX_EPOCH)
37+
.unwrap_or_else(|e| {
38+
panic!(
39+
"Current time {:?} is before unix epoch. Something is wrong: {:?}",
40+
now, e,
41+
)
42+
})
3843
}
3944

40-
4145
/// A `Duration` with a sign (before or after). Immutable.
4246
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
4347
pub struct SignedDuration {
@@ -48,7 +52,10 @@ pub struct SignedDuration {
4852
impl SignedDuration {
4953
/// Construct a `SignedDuration`
5054
pub fn new(offset: Duration, is_positive: bool) -> Self {
51-
Self { offset, is_positive }
55+
Self {
56+
offset,
57+
is_positive,
58+
}
5259
}
5360

5461
/// Get the slot for now. Panics if `slot_duration` is 0.
@@ -57,7 +64,9 @@ impl SignedDuration {
5764
duration_now() + self.offset
5865
} else {
5966
duration_now() - self.offset
60-
}.as_millis() as u64) / slot_duration
67+
}
68+
.as_millis() as u64)
69+
/ slot_duration
6170
}
6271
}
6372

@@ -137,14 +146,15 @@ impl<SC: SlotCompatible> Stream for Slots<SC> {
137146
Ok(id) => id,
138147
Err(err) => return Poll::Ready(Some(Err(sp_consensus::Error::InherentData(err)))),
139148
};
140-
let result = self.timestamp_extractor.extract_timestamp_and_slot(&inherent_data);
149+
let result = self
150+
.timestamp_extractor
151+
.extract_timestamp_and_slot(&inherent_data);
141152
let (timestamp, slot_num, offset) = match result {
142153
Ok(v) => v,
143154
Err(err) => return Poll::Ready(Some(Err(err))),
144155
};
145156
// reschedule delay for next slot.
146-
let ends_in = offset +
147-
time_until_next(Duration::from_millis(timestamp), slot_duration);
157+
let ends_in = offset + time_until_next(Duration::from_millis(timestamp), slot_duration);
148158
let ends_at = Instant::now() + ends_in;
149159
self.inner_delay = Some(Delay::new(ends_in));
150160

@@ -160,11 +170,10 @@ impl<SC: SlotCompatible> Stream for Slots<SC> {
160170
timestamp,
161171
ends_at,
162172
inherent_data,
163-
})))
173+
})));
164174
}
165175
}
166176
}
167177
}
168178

169-
impl<SC> Unpin for Slots<SC> {
170-
}
179+
impl<SC> Unpin for Slots<SC> {}

primitives/timestamp/src/lib.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
// This file is part of Substrate.
1+
// This file is part of Selendra.
22

3-
// Copyright (C) 2019-2020 Parity Technologies (UK) Ltd.
3+
// Copyright (C) 2020 Selendra.
44
// SPDX-License-Identifier: Apache-2.0
55

66
// Licensed under the Apache License, Version 2.0 (the "License");
@@ -19,12 +19,12 @@
1919
2020
#![cfg_attr(not(feature = "std"), no_std)]
2121

22-
use codec::Encode;
2322
#[cfg(feature = "std")]
2423
use codec::Decode;
24+
use codec::Encode;
2525
#[cfg(feature = "std")]
2626
use sp_inherents::ProvideInherentData;
27-
use sp_inherents::{InherentIdentifier, IsFatalError, InherentData};
27+
use sp_inherents::{InherentData, InherentIdentifier, IsFatalError};
2828

2929
use sp_runtime::RuntimeString;
3030

@@ -96,9 +96,8 @@ impl ProvideInherentData for InherentDataProvider {
9696

9797
let now = SystemTime::now();
9898
now.duration_since(SystemTime::UNIX_EPOCH)
99-
.map_err(|_| {
100-
"Current time is before unix epoch".into()
101-
}).and_then(|d| {
99+
.map_err(|_| "Current time is before unix epoch".into())
100+
.and_then(|d| {
102101
let duration: InherentType = d.as_millis() as u64;
103102
inherent_data.put_data(INHERENT_IDENTIFIER, &duration)
104103
})
@@ -109,7 +108,6 @@ impl ProvideInherentData for InherentDataProvider {
109108
}
110109
}
111110

112-
113111
/// A trait which is called when the timestamp is set.
114112
#[impl_trait_for_tuples::impl_for_tuples(30)]
115113
pub trait OnTimestampSet<Moment> {

0 commit comments

Comments
 (0)