Skip to content

Commit 710ccfc

Browse files
authored
Use traits to encapsulate sync committe config (#81)
1 parent 84cea2d commit 710ccfc

File tree

16 files changed

+165
-180
lines changed

16 files changed

+165
-180
lines changed

parachain/modules/consensus/sync-committee/primitives/Cargo.toml

+1-2
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,4 @@ std = [
3232
"primitive-types/std",
3333
"serde"
3434
]
35-
mainnet = []
36-
sepolia = []
35+

parachain/modules/consensus/sync-committee/primitives/src/constants.rs

+65-45
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ pub const MAX_EXTRA_DATA_BYTES: usize = 32;
5757

5858
pub const DEPOSIT_PROOF_LENGTH: usize = 33;
5959

60-
pub const ETH1_DATA_VOTES_BOUND: usize = (EPOCHS_PER_ETH1_VOTING_PERIOD * SLOTS_PER_EPOCH) as usize;
6160
pub const DOMAIN_SYNC_COMMITTEE: DomainType = DomainType::SyncCommittee;
6261
pub const FINALIZED_ROOT_INDEX: u64 = 52;
6362
pub const EXECUTION_PAYLOAD_STATE_ROOT_INDEX: u64 = 18;
@@ -73,62 +72,83 @@ pub const EXECUTION_PAYLOAD_INDEX_LOG2: u64 = 5;
7372
pub const NEXT_SYNC_COMMITTEE_INDEX_LOG2: u64 = 5;
7473
pub const BLOCK_ROOTS_INDEX_LOG2: u64 = 5;
7574
pub const HISTORICAL_ROOTS_INDEX_LOG2: u64 = 5;
76-
77-
#[cfg(feature = "sepolia")]
78-
pub use sepolia::*;
79-
80-
#[cfg(feature = "mainnet")]
81-
pub use mainnet::*;
75+
pub const ETH1_DATA_VOTES_BOUND: usize = (EPOCHS_PER_ETH1_VOTING_PERIOD * 32) as usize;
76+
77+
pub trait Config {
78+
const SLOTS_PER_EPOCH: Slot;
79+
const GENESIS_VALIDATORS_ROOT: [u8; 32];
80+
const BELLATRIX_FORK_VERSION: Version;
81+
const ALTAIR_FORK_VERSION: Version;
82+
const GENESIS_FORK_VERSION: Version;
83+
const ALTAIR_FORK_EPOCH: Epoch;
84+
const BELLATRIX_FORK_EPOCH: Epoch;
85+
const CAPELLA_FORK_EPOCH: Epoch;
86+
const CAPELLA_FORK_VERSION: Version;
87+
const EPOCHS_PER_SYNC_COMMITTEE_PERIOD: Epoch;
88+
}
8289

8390
use crate::ssz::ByteVector;
84-
#[cfg(all(not(feature = "mainnet"), not(feature = "sepolia")))]
85-
pub use devnet::*;
8691

87-
#[cfg(feature = "sepolia")]
8892
pub mod sepolia {
8993
use super::*;
90-
pub const SLOTS_PER_EPOCH: Slot = 32;
91-
pub const GENESIS_VALIDATORS_ROOT: [u8; 32] =
92-
hex_literal::hex!("d8ea171f3c94aea21ebc42a1ed61052acf3f9209c00e4efbaaddac09ed9b8078");
93-
pub const BELLATRIX_FORK_VERSION: Version = hex_literal::hex!("90000071");
94-
pub const ALTAIR_FORK_VERSION: Version = hex_literal::hex!("90000070");
95-
pub const GENESIS_FORK_VERSION: Version = hex_literal::hex!("90000069");
96-
pub const ALTAIR_FORK_EPOCH: Epoch = 50;
97-
pub const BELLATRIX_FORK_EPOCH: Epoch = 100;
98-
pub const CAPELLA_FORK_EPOCH: Epoch = 56832;
99-
pub const CAPELLA_FORK_VERSION: Version = hex_literal::hex!("90000072");
100-
pub const EPOCHS_PER_SYNC_COMMITTEE_PERIOD: Epoch = 256;
94+
95+
#[derive(Default)]
96+
pub struct Sepolia;
97+
98+
impl Config for Sepolia {
99+
const SLOTS_PER_EPOCH: Slot = 32;
100+
const GENESIS_VALIDATORS_ROOT: [u8; 32] =
101+
hex_literal::hex!("d8ea171f3c94aea21ebc42a1ed61052acf3f9209c00e4efbaaddac09ed9b8078");
102+
const BELLATRIX_FORK_VERSION: Version = hex_literal::hex!("90000071");
103+
const ALTAIR_FORK_VERSION: Version = hex_literal::hex!("90000070");
104+
const GENESIS_FORK_VERSION: Version = hex_literal::hex!("90000069");
105+
const ALTAIR_FORK_EPOCH: Epoch = 50;
106+
const BELLATRIX_FORK_EPOCH: Epoch = 100;
107+
const CAPELLA_FORK_EPOCH: Epoch = 56832;
108+
const CAPELLA_FORK_VERSION: Version = hex_literal::hex!("90000072");
109+
const EPOCHS_PER_SYNC_COMMITTEE_PERIOD: Epoch = 256;
110+
}
101111
}
102112

103-
#[cfg(feature = "mainnet")]
104113
pub mod mainnet {
105114
use super::*;
106-
pub const SLOTS_PER_EPOCH: Slot = 32;
107-
pub const GENESIS_VALIDATORS_ROOT: [u8; 32] =
108-
hex_literal::hex!("4b363db94e286120d76eb905340fdd4e54bfe9f06bf33ff6cf5ad27f511bfe95");
109-
pub const BELLATRIX_FORK_VERSION: Version = hex_literal::hex!("02000000");
110-
pub const ALTAIR_FORK_VERSION: Version = hex_literal::hex!("01000000");
111-
pub const GENESIS_FORK_VERSION: Version = hex_literal::hex!("00000000");
112-
pub const ALTAIR_FORK_EPOCH: Epoch = 74240;
113-
pub const BELLATRIX_FORK_EPOCH: Epoch = 144896;
114-
pub const CAPELLA_FORK_EPOCH: Epoch = 194048;
115-
pub const CAPELLA_FORK_VERSION: Version = hex_literal::hex!("03000000");
116-
pub const EPOCHS_PER_SYNC_COMMITTEE_PERIOD: Epoch = 256;
115+
116+
#[derive(Default)]
117+
pub struct Mainnet;
118+
119+
impl Config for Mainnet {
120+
const SLOTS_PER_EPOCH: Slot = 32;
121+
const GENESIS_VALIDATORS_ROOT: [u8; 32] =
122+
hex_literal::hex!("4b363db94e286120d76eb905340fdd4e54bfe9f06bf33ff6cf5ad27f511bfe95");
123+
const BELLATRIX_FORK_VERSION: Version = hex_literal::hex!("02000000");
124+
const ALTAIR_FORK_VERSION: Version = hex_literal::hex!("01000000");
125+
const GENESIS_FORK_VERSION: Version = hex_literal::hex!("00000000");
126+
const ALTAIR_FORK_EPOCH: Epoch = 74240;
127+
const BELLATRIX_FORK_EPOCH: Epoch = 144896;
128+
const CAPELLA_FORK_EPOCH: Epoch = 194048;
129+
const CAPELLA_FORK_VERSION: Version = hex_literal::hex!("03000000");
130+
const EPOCHS_PER_SYNC_COMMITTEE_PERIOD: Epoch = 256;
131+
}
117132
}
118133

119-
#[cfg(all(not(feature = "mainnet"), not(feature = "sepolia")))]
120134
pub mod devnet {
121135
use super::*;
122136
use hex_literal::hex;
123-
pub const SLOTS_PER_EPOCH: Slot = 6;
124-
pub const GENESIS_VALIDATORS_ROOT: [u8; 32] =
125-
hex_literal::hex!("83431ec7fcf92cfc44947fc0418e831c25e1d0806590231c439830db7ad54fda");
126-
pub const BELLATRIX_FORK_VERSION: Version = hex!("52525502");
127-
pub const ALTAIR_FORK_VERSION: Version = hex!("52525501");
128-
pub const GENESIS_FORK_VERSION: Version = hex!("52525500");
129-
pub const ALTAIR_FORK_EPOCH: Epoch = 0;
130-
pub const BELLATRIX_FORK_EPOCH: Epoch = 0;
131-
pub const CAPELLA_FORK_EPOCH: Epoch = 2;
132-
pub const CAPELLA_FORK_VERSION: Version = hex!("52525503");
133-
pub const EPOCHS_PER_SYNC_COMMITTEE_PERIOD: Epoch = 4;
137+
138+
#[derive(Default)]
139+
pub struct Devnet;
140+
141+
impl Config for Devnet {
142+
const SLOTS_PER_EPOCH: Slot = 6;
143+
const GENESIS_VALIDATORS_ROOT: [u8; 32] =
144+
hex_literal::hex!("83431ec7fcf92cfc44947fc0418e831c25e1d0806590231c439830db7ad54fda");
145+
const BELLATRIX_FORK_VERSION: Version = hex!("52525502");
146+
const ALTAIR_FORK_VERSION: Version = hex!("52525501");
147+
const GENESIS_FORK_VERSION: Version = hex!("52525500");
148+
const ALTAIR_FORK_EPOCH: Epoch = 0;
149+
const BELLATRIX_FORK_EPOCH: Epoch = 0;
150+
const CAPELLA_FORK_EPOCH: Epoch = 2;
151+
const CAPELLA_FORK_VERSION: Version = hex!("52525503");
152+
const EPOCHS_PER_SYNC_COMMITTEE_PERIOD: Epoch = 4;
153+
}
134154
}

parachain/modules/consensus/sync-committee/primitives/src/util.rs

+15-20
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
use crate::{
22
consensus_types::ForkData,
3-
constants::{
4-
Domain, Root, Version, ALTAIR_FORK_EPOCH, ALTAIR_FORK_VERSION, BELLATRIX_FORK_EPOCH,
5-
BELLATRIX_FORK_VERSION, CAPELLA_FORK_EPOCH, CAPELLA_FORK_VERSION,
6-
EPOCHS_PER_SYNC_COMMITTEE_PERIOD, GENESIS_FORK_VERSION, SLOTS_PER_EPOCH,
7-
},
3+
constants::{Config, Domain, Root, Version},
84
domains::DomainType,
95
};
106
use alloc::{vec, vec::Vec};
@@ -17,26 +13,25 @@ pub fn should_have_sync_committee_update(state_period: u64, signature_period: u6
1713
}
1814

1915
/// Return the sync committee period at the given ``epoch``
20-
pub fn compute_sync_committee_period(epoch: u64) -> u64 {
21-
epoch / EPOCHS_PER_SYNC_COMMITTEE_PERIOD
16+
pub fn compute_sync_committee_period<C: Config>(epoch: u64) -> u64 {
17+
epoch / C::EPOCHS_PER_SYNC_COMMITTEE_PERIOD
2218
}
2319

2420
/// Return the epoch number at ``slot``.
25-
pub fn compute_epoch_at_slot(slot: u64) -> u64 {
26-
slot / SLOTS_PER_EPOCH
21+
pub fn compute_epoch_at_slot<C: Config>(slot: u64) -> u64 {
22+
slot / C::SLOTS_PER_EPOCH
2723
}
2824

29-
#[cfg(not(feature = "testing"))]
3025
/// Return the fork version at the given ``epoch``.
31-
pub fn compute_fork_version(epoch: u64) -> [u8; 4] {
32-
if epoch >= CAPELLA_FORK_EPOCH {
33-
CAPELLA_FORK_VERSION
34-
} else if epoch >= BELLATRIX_FORK_EPOCH {
35-
BELLATRIX_FORK_VERSION
36-
} else if epoch >= ALTAIR_FORK_EPOCH {
37-
ALTAIR_FORK_VERSION
26+
pub fn compute_fork_version<C: Config>(epoch: u64) -> [u8; 4] {
27+
if epoch >= C::CAPELLA_FORK_EPOCH {
28+
C::CAPELLA_FORK_VERSION
29+
} else if epoch >= C::BELLATRIX_FORK_EPOCH {
30+
C::BELLATRIX_FORK_VERSION
31+
} else if epoch >= C::ALTAIR_FORK_EPOCH {
32+
C::ALTAIR_FORK_VERSION
3833
} else {
39-
GENESIS_FORK_VERSION
34+
C::GENESIS_FORK_VERSION
4035
}
4136
}
4237

@@ -81,6 +76,6 @@ pub fn compute_fork_data_root(
8176
}
8277

8378
/// Return the sync committee period at ``slot``
84-
pub fn compute_sync_committee_period_at_slot(slot: u64) -> u64 {
85-
compute_sync_committee_period(compute_epoch_at_slot(slot))
79+
pub fn compute_sync_committee_period_at_slot<C: Config>(slot: u64) -> u64 {
80+
compute_sync_committee_period::<C>(compute_epoch_at_slot::<C>(slot))
8681
}

parachain/modules/consensus/sync-committee/prover/Cargo.toml

-2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,4 @@ std = [
4646
"bls_on_arkworks/std",
4747
"ark-bls12-381/std"
4848
]
49-
sepolia = ["sync-committee-primitives/sepolia", "sync-committee-verifier/sepolia"]
50-
mainnet = ["sync-committee-primitives/mainnet", "sync-committee-verifier/mainnet"]
5149

0 commit comments

Comments
 (0)