/__w/smoldot/smoldot/repo/lib/src/author/aura.rs
Line | Count | Source (jump to first uncovered line) |
1 | | // Smoldot |
2 | | // Copyright (C) 2019-2020 Parity Technologies (UK) Ltd. |
3 | | // SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 |
4 | | |
5 | | // This program is free software: you can redistribute it and/or modify |
6 | | // it under the terms of the GNU General Public License as published by |
7 | | // the Free Software Foundation, either version 3 of the License, or |
8 | | // (at your option) any later version. |
9 | | |
10 | | // This program is distributed in the hope that it will be useful, |
11 | | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | | // GNU General Public License for more details. |
14 | | |
15 | | // You should have received a copy of the GNU General Public License |
16 | | // along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 | | |
18 | | use crate::header; |
19 | | use core::{num::NonZeroU64, time::Duration}; |
20 | | |
21 | | /// Configuration for [`next_slot_claim`]. |
22 | | pub struct Config<'a, TLocAuth> { |
23 | | /// Time elapsed since [the Unix Epoch](https://en.wikipedia.org/wiki/Unix_time) (i.e. |
24 | | /// 00:00:00 UTC on 1 January 1970), ignoring leap seconds. |
25 | | pub now_from_unix_epoch: Duration, |
26 | | |
27 | | /// Duration, in milliseconds, of an Aura slot. |
28 | | pub slot_duration: NonZeroU64, |
29 | | |
30 | | /// List of the Aura authorities allowed to produce a block. This is either the same as the |
31 | | /// ones of the current best block, or a new list if the current best block contains an |
32 | | /// authorities list change digest item. |
33 | | pub current_authorities: header::AuraAuthoritiesIter<'a>, |
34 | | |
35 | | /// Iterator to the list of Sr25519 public keys available locally. |
36 | | /// |
37 | | /// Must implement `Iterator<Item = &[u8; 32]>`. |
38 | | pub local_authorities: TLocAuth, |
39 | | } |
40 | | |
41 | | /// Calculates the earliest one of the authorities in [`Config::local_authorities`] is allowed to |
42 | | /// produce a block. |
43 | | /// |
44 | | /// Returns `None` if none of the local authorities are allowed to produce blocks. |
45 | | /// |
46 | | /// The value returned by this function is entirely deterministic based on the [`Config`] and |
47 | | /// never changes until [`Config::now_from_unix_epoch`] gets past the value returned in |
48 | | /// [`SlotClaim::slot_end_from_unix_epoch`]. |
49 | | /// |
50 | | /// However, keep in mind that, as the best block changes, the list of authorities |
51 | | /// ([`Config::current_authorities`]) might change, in which case this function should be |
52 | | /// called again. |
53 | 0 | pub fn next_slot_claim<'a>( |
54 | 0 | config: Config<'a, impl Iterator<Item = &'a [u8; 32]>>, |
55 | 0 | ) -> Option<SlotClaim> { |
56 | 0 | let num_current_authorities = config.current_authorities.clone().count(); |
57 | 0 |
|
58 | 0 | // Note that this calculation (and some other calculations down below) can overflow in the |
59 | 0 | // very distant future. This is considered acceptable. |
60 | 0 | let current_slot = u64::try_from( |
61 | 0 | config.now_from_unix_epoch.as_millis() / u128::from(config.slot_duration.get()), |
62 | 0 | ) |
63 | 0 | .unwrap(); |
64 | | |
65 | 0 | let current_slot_index = |
66 | 0 | usize::try_from(current_slot.checked_rem(u64::try_from(num_current_authorities).unwrap())?) |
67 | 0 | .unwrap(); |
68 | 0 | debug_assert!(current_slot_index < num_current_authorities); |
69 | | |
70 | 0 | let mut claim = None; |
71 | | |
72 | 0 | for (pub_key_index, local_pub_key) in config.local_authorities.enumerate() { |
73 | | // TODO: O(n) complexity |
74 | 0 | let mut index = match config |
75 | 0 | .current_authorities |
76 | 0 | .clone() |
77 | 0 | .position(|pk| pk.public_key == local_pub_key) Unexecuted instantiation: _RNCINvNtNtCsN16ciHI6Qf_7smoldot6author4aura15next_slot_claimpE0B8_ Unexecuted instantiation: _RNCINvNtNtCseuYC0Zibziv_7smoldot6author4aura15next_slot_claimpE0B8_ |
78 | | { |
79 | 0 | Some(idx) => idx, |
80 | 0 | None => continue, |
81 | | }; |
82 | | |
83 | 0 | if index < current_slot_index { |
84 | 0 | index += num_current_authorities; |
85 | 0 | } |
86 | 0 | debug_assert!(index >= current_slot_index); |
87 | | |
88 | 0 | let claimable_slot = current_slot + u64::try_from(index - current_slot_index).unwrap(); |
89 | | |
90 | 0 | match claim { |
91 | 0 | Some((s, _)) if s <= claimable_slot => {} |
92 | 0 | _ => claim = Some((claimable_slot, pub_key_index)), |
93 | | } |
94 | | } |
95 | | |
96 | 0 | if let Some((slot_number, local_authorities_index)) = claim { |
97 | 0 | let slot_start_from_unix_epoch = |
98 | 0 | Duration::from_millis(slot_number.checked_mul(config.slot_duration.get()).unwrap()); |
99 | 0 | let slot_end_from_unix_epoch = |
100 | 0 | slot_start_from_unix_epoch + Duration::from_secs(config.slot_duration.get()); |
101 | 0 | debug_assert!(slot_end_from_unix_epoch > config.now_from_unix_epoch); |
102 | | |
103 | 0 | Some(SlotClaim { |
104 | 0 | slot_start_from_unix_epoch, |
105 | 0 | slot_end_from_unix_epoch, |
106 | 0 | slot_number, |
107 | 0 | local_authorities_index, |
108 | 0 | }) |
109 | | } else { |
110 | 0 | None |
111 | | } |
112 | 0 | } Unexecuted instantiation: _RINvNtNtCsN16ciHI6Qf_7smoldot6author4aura15next_slot_claimpEB6_ Unexecuted instantiation: _RINvNtNtCseuYC0Zibziv_7smoldot6author4aura15next_slot_claimpEB6_ |
113 | | |
114 | | /// Slot happening now or in the future and that can be attributed to one of the authorities in |
115 | | /// [`Config::local_authorities`]. |
116 | | /// |
117 | | /// See also [`next_slot_claim`]. |
118 | | #[derive(Debug, Copy, Clone, PartialEq, Eq)] |
119 | | pub struct SlotClaim { |
120 | | /// UNIX time when the slot starts. Can be inferior to the value passed to |
121 | | /// [`Config::now_from_unix_epoch`] if the slot has already started. |
122 | | pub slot_start_from_unix_epoch: Duration, |
123 | | /// UNIX time when the slot ends. Always superior to the value passed to |
124 | | /// [`Config::now_from_unix_epoch`]. |
125 | | pub slot_end_from_unix_epoch: Duration, |
126 | | /// Slot number of the claim. Used when building the block. |
127 | | pub slot_number: u64, |
128 | | /// Index within [`Config::local_authorities`] of the authority that can produce the block. |
129 | | pub local_authorities_index: usize, |
130 | | } |