Skip to content

Commit cbfadfd

Browse files
committed
Move epoch offset constants to the Epoch type
+ All offset computation that can lead to negative values must now use the safe `Epoch::offset_by` method
1 parent 0399fbd commit cbfadfd

File tree

3 files changed

+23
-103
lines changed

3 files changed

+23
-103
lines changed

mithril-aggregator/src/multi_signer.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -726,8 +726,9 @@ mod tests {
726726
ProtocolParametersStore,
727727
};
728728
use mithril_common::{
729-
crypto_helper::tests_setup::*, fake_data, store::adapter::MemoryAdapter, store::StakeStore,
730-
SIGNER_EPOCH_RECORDING_OFFSET, SIGNER_EPOCH_RETRIEVAL_OFFSET,
729+
crypto_helper::tests_setup::*,
730+
fake_data,
731+
store::{adapter::MemoryAdapter, StakeStore},
731732
};
732733
use std::{collections::HashMap, sync::Arc};
733734

@@ -843,7 +844,7 @@ mod tests {
843844

844845
offset_epoch(
845846
&mut multi_signer,
846-
SIGNER_EPOCH_RECORDING_OFFSET - SIGNER_EPOCH_RETRIEVAL_OFFSET,
847+
Epoch::SIGNER_RECORDING_OFFSET as i64 - Epoch::SIGNER_RETRIEVAL_OFFSET,
847848
)
848849
.await;
849850

@@ -879,7 +880,7 @@ mod tests {
879880

880881
offset_epoch(
881882
&mut multi_signer,
882-
SIGNER_EPOCH_RECORDING_OFFSET - SIGNER_EPOCH_RETRIEVAL_OFFSET,
883+
Epoch::SIGNER_RECORDING_OFFSET as i64 - Epoch::SIGNER_RETRIEVAL_OFFSET,
883884
)
884885
.await;
885886

@@ -944,7 +945,7 @@ mod tests {
944945

945946
offset_epoch(
946947
&mut multi_signer,
947-
SIGNER_EPOCH_RECORDING_OFFSET - SIGNER_EPOCH_RETRIEVAL_OFFSET,
948+
Epoch::SIGNER_RECORDING_OFFSET as i64 - Epoch::SIGNER_RETRIEVAL_OFFSET,
948949
)
949950
.await;
950951
// We have to update the current message AFTER we reached the epoch for
Lines changed: 17 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
use crate::{
2-
NEXT_SIGNER_EPOCH_RETRIEVAL_OFFSET, SIGNER_EPOCH_RECORDING_OFFSET,
3-
SIGNER_EPOCH_RETRIEVAL_OFFSET,
4-
};
51
use serde::{Deserialize, Serialize};
62
use std::{
73
fmt::{Display, Formatter},
@@ -16,9 +12,19 @@ use thiserror::Error;
1612
pub struct Epoch(pub u64);
1713

1814
impl Epoch {
15+
/// The epoch offset used for signers stake distribution and verification keys retrieval.
16+
pub const SIGNER_RETRIEVAL_OFFSET: i64 = -1;
17+
18+
/// The epoch offset used to retrieve the signers stake distribution and verification keys that's
19+
/// currently being signed so it can be used in the next epoch.
20+
pub const NEXT_SIGNER_RETRIEVAL_OFFSET: u64 = 0;
21+
22+
/// The epoch offset used for signers stake distribution and verification keys recording.
23+
pub const SIGNER_RECORDING_OFFSET: u64 = 1;
24+
1925
/// Computes a new Epoch by applying an epoch offset.
2026
///
21-
/// Will fails if the computed epoch is negative.
27+
/// Will fail if the computed epoch is negative.
2228
pub fn offset_by(&self, epoch_offset: i64) -> Result<Self, EpochError> {
2329
let epoch_new = self.0 as i64 + epoch_offset;
2430
if epoch_new < 0 {
@@ -27,19 +33,19 @@ impl Epoch {
2733
Ok(Epoch(epoch_new as u64))
2834
}
2935

30-
/// Apply the [SIGNER_EPOCH_RETRIEVAL_OFFSET] to this epoch
36+
/// Apply the [Self::SIGNER_EPOCH_RETRIEVAL_OFFSET] to this epoch
3137
pub fn offset_to_signer_retrieval_epoch(&self) -> Result<Self, EpochError> {
32-
self.offset_by(SIGNER_EPOCH_RETRIEVAL_OFFSET)
38+
self.offset_by(Self::SIGNER_RETRIEVAL_OFFSET)
3339
}
3440

35-
/// Apply the [NEXT_SIGNER_EPOCH_RETRIEVAL_OFFSET] to this epoch
41+
/// Apply the [Self::NEXT_SIGNER_EPOCH_RETRIEVAL_OFFSET] to this epoch
3642
pub fn offset_to_next_signer_retrieval_epoch(&self) -> Self {
37-
*self + NEXT_SIGNER_EPOCH_RETRIEVAL_OFFSET
43+
*self + Self::NEXT_SIGNER_RETRIEVAL_OFFSET
3844
}
3945

40-
/// Apply the [SIGNER_EPOCH_RECORDING_OFFSET] to this epoch
46+
/// Apply the [Self::SIGNER_EPOCH_RECORDING_OFFSET] to this epoch
4147
pub fn offset_to_recording_epoch(&self) -> Self {
42-
*self + SIGNER_EPOCH_RECORDING_OFFSET
48+
*self + Self::SIGNER_RECORDING_OFFSET
4349
}
4450
}
4551

@@ -59,22 +65,6 @@ impl Add<u64> for Epoch {
5965
}
6066
}
6167

62-
impl Add<i32> for Epoch {
63-
type Output = Self;
64-
65-
fn add(self, rhs: i32) -> Self::Output {
66-
Self(self.0 + rhs as u64)
67-
}
68-
}
69-
70-
impl Add<i64> for Epoch {
71-
type Output = Self;
72-
73-
fn add(self, rhs: i64) -> Self::Output {
74-
Self(self.0 + rhs as u64)
75-
}
76-
}
77-
7868
impl AddAssign for Epoch {
7969
fn add_assign(&mut self, rhs: Self) {
8070
*self = self.add(rhs);
@@ -87,18 +77,6 @@ impl AddAssign<u64> for Epoch {
8777
}
8878
}
8979

90-
impl AddAssign<i32> for Epoch {
91-
fn add_assign(&mut self, rhs: i32) {
92-
*self = self.add(rhs);
93-
}
94-
}
95-
96-
impl AddAssign<i64> for Epoch {
97-
fn add_assign(&mut self, rhs: i64) {
98-
*self = self.add(rhs);
99-
}
100-
}
101-
10280
impl Sub for Epoch {
10381
type Output = Self;
10482

@@ -115,22 +93,6 @@ impl Sub<u64> for Epoch {
11593
}
11694
}
11795

118-
impl Sub<i32> for Epoch {
119-
type Output = Self;
120-
121-
fn sub(self, rhs: i32) -> Self::Output {
122-
Self(self.0 - rhs as u64)
123-
}
124-
}
125-
126-
impl Sub<i64> for Epoch {
127-
type Output = Self;
128-
129-
fn sub(self, rhs: i64) -> Self::Output {
130-
Self(self.0 - rhs as u64)
131-
}
132-
}
133-
13496
impl SubAssign for Epoch {
13597
fn sub_assign(&mut self, rhs: Self) {
13698
*self = self.sub(rhs);
@@ -143,18 +105,6 @@ impl SubAssign<u64> for Epoch {
143105
}
144106
}
145107

146-
impl SubAssign<i32> for Epoch {
147-
fn sub_assign(&mut self, rhs: i32) {
148-
*self = self.sub(rhs);
149-
}
150-
}
151-
152-
impl SubAssign<i64> for Epoch {
153-
fn sub_assign(&mut self, rhs: i64) {
154-
*self = self.sub(rhs);
155-
}
156-
}
157-
158108
impl PartialEq<u64> for Epoch {
159109
fn eq(&self, other: &u64) -> bool {
160110
self.0.eq(other)
@@ -189,8 +139,6 @@ mod tests {
189139
fn test_add() {
190140
assert_eq!(Epoch(4), Epoch(1) + Epoch(3));
191141
assert_eq!(Epoch(4), Epoch(1) + 3_u64);
192-
assert_eq!(Epoch(4), Epoch(1) + 3_i32);
193-
assert_eq!(Epoch(4), Epoch(1) + 3_i64);
194142

195143
let mut epoch = Epoch(1);
196144
epoch += Epoch(3);
@@ -199,22 +147,12 @@ mod tests {
199147
let mut epoch = Epoch(1);
200148
epoch += 3_u64;
201149
assert_eq!(Epoch(4), epoch);
202-
203-
let mut epoch = Epoch(1);
204-
epoch += 3_i32;
205-
assert_eq!(Epoch(4), epoch);
206-
207-
let mut epoch = Epoch(1);
208-
epoch += 3_i64;
209-
assert_eq!(Epoch(4), epoch);
210150
}
211151

212152
#[test]
213153
fn test_sub() {
214154
assert_eq!(Epoch(8), Epoch(14) - Epoch(6));
215155
assert_eq!(Epoch(8), Epoch(14) - 6_u64);
216-
assert_eq!(Epoch(8), Epoch(14) - 6_i32);
217-
assert_eq!(Epoch(8), Epoch(14) - 6_i64);
218156

219157
let mut epoch = Epoch(14);
220158
epoch -= Epoch(6);
@@ -223,13 +161,5 @@ mod tests {
223161
let mut epoch = Epoch(14);
224162
epoch -= 6_u64;
225163
assert_eq!(Epoch(8), epoch);
226-
227-
let mut epoch = Epoch(14);
228-
epoch -= 6_i32;
229-
assert_eq!(Epoch(8), epoch);
230-
231-
let mut epoch = Epoch(14);
232-
epoch -= 6_i64;
233-
assert_eq!(Epoch(8), epoch);
234164
}
235165
}

mithril-common/src/lib.rs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,6 @@ pub mod store;
2727
pub use beacon_provider::{BeaconProvider, BeaconProviderError, BeaconProviderImpl};
2828
pub use entities::{CardanoNetwork, MagicId};
2929

30-
// TODO: Investigate as why signers can't sign until epoch 3 (in the e2e) when set to -1
31-
/// The epoch offset used for signers stake distribution and verification keys retrieval.
32-
pub const SIGNER_EPOCH_RETRIEVAL_OFFSET: i64 = -1;
33-
34-
/// The epoch offset used to retrieve the signers stake distribution and verification keys that's
35-
/// currently being signed so it can be used in the next epoch.
36-
pub const NEXT_SIGNER_EPOCH_RETRIEVAL_OFFSET: i64 = 0;
37-
38-
/// The epoch offset used for signers stake distribution and verification keys recording.
39-
pub const SIGNER_EPOCH_RECORDING_OFFSET: i64 = 1;
40-
4130
/// Mithril API protocol version
4231
/// this is the same as the one in openapi.yml file.
4332
/// If you want to update this version to reflect changes in the protocol,

0 commit comments

Comments
 (0)