forked from sigp/lighthouse
/
eth_spec.rs
365 lines (326 loc) · 12.7 KB
/
eth_spec.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
use crate::*;
use safe_arith::SafeArith;
use serde_derive::{Deserialize, Serialize};
use ssz_types::typenum::{
bit::B0, UInt, Unsigned, U0, U1024, U1048576, U1073741824, U1099511627776, U128, U16,
U16777216, U2, U2048, U256, U32, U4, U4096, U512, U625, U64, U65536, U8, U8192,
};
use std::fmt::{self, Debug};
use std::str::FromStr;
pub type U5000 = UInt<UInt<UInt<U625, B0>, B0>, B0>; // 625 * 8 = 5000
const MAINNET: &str = "mainnet";
const MINIMAL: &str = "minimal";
pub const GNOSIS: &str = "gnosis";
/// Used to identify one of the `EthSpec` instances defined here.
#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum EthSpecId {
Mainnet,
Minimal,
Gnosis,
}
impl FromStr for EthSpecId {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
MAINNET => Ok(EthSpecId::Mainnet),
MINIMAL => Ok(EthSpecId::Minimal),
GNOSIS => Ok(EthSpecId::Gnosis),
_ => Err(format!("Unknown eth spec: {}", s)),
}
}
}
impl fmt::Display for EthSpecId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let s = match self {
EthSpecId::Mainnet => MAINNET,
EthSpecId::Minimal => MINIMAL,
EthSpecId::Gnosis => GNOSIS,
};
write!(f, "{}", s)
}
}
pub trait EthSpec: 'static + Default + Sync + Send + Clone + Debug + PartialEq + Eq {
/*
* Constants
*/
type GenesisEpoch: Unsigned + Clone + Sync + Send + Debug + PartialEq;
type JustificationBitsLength: Unsigned + Clone + Sync + Send + Debug + PartialEq + Default;
type SubnetBitfieldLength: Unsigned + Clone + Sync + Send + Debug + PartialEq + Default;
/*
* Misc
*/
type MaxValidatorsPerCommittee: Unsigned + Clone + Sync + Send + Debug + PartialEq + Eq;
/*
* Time parameters
*/
type SlotsPerEpoch: Unsigned + Clone + Sync + Send + Debug + PartialEq;
type EpochsPerEth1VotingPeriod: Unsigned + Clone + Sync + Send + Debug + PartialEq;
type SlotsPerHistoricalRoot: Unsigned + Clone + Sync + Send + Debug + PartialEq;
/*
* State list lengths
*/
type EpochsPerHistoricalVector: Unsigned + Clone + Sync + Send + Debug + PartialEq;
type EpochsPerSlashingsVector: Unsigned + Clone + Sync + Send + Debug + PartialEq;
type HistoricalRootsLimit: Unsigned + Clone + Sync + Send + Debug + PartialEq;
type ValidatorRegistryLimit: Unsigned + Clone + Sync + Send + Debug + PartialEq;
/*
* Max operations per block
*/
type MaxProposerSlashings: Unsigned + Clone + Sync + Send + Debug + PartialEq;
type MaxAttesterSlashings: Unsigned + Clone + Sync + Send + Debug + PartialEq;
type MaxAttestations: Unsigned + Clone + Sync + Send + Debug + PartialEq;
type MaxDeposits: Unsigned + Clone + Sync + Send + Debug + PartialEq;
type MaxVoluntaryExits: Unsigned + Clone + Sync + Send + Debug + PartialEq;
/*
* New in Altair
*/
type SyncCommitteeSize: Unsigned + Clone + Sync + Send + Debug + PartialEq;
/// The number of `sync_committee` subnets.
type SyncCommitteeSubnetCount: Unsigned + Clone + Sync + Send + Debug + PartialEq;
/*
* New in Merge
*/
type MaxBytesPerTransaction: Unsigned + Clone + Sync + Send + Debug + PartialEq;
type MaxTransactionsPerPayload: Unsigned + Clone + Sync + Send + Debug + PartialEq;
type BytesPerLogsBloom: Unsigned + Clone + Sync + Send + Debug + PartialEq;
type GasLimitDenominator: Unsigned + Clone + Sync + Send + Debug + PartialEq;
type MinGasLimit: Unsigned + Clone + Sync + Send + Debug + PartialEq;
type MaxExtraDataBytes: Unsigned + Clone + Sync + Send + Debug + PartialEq;
/*
* Derived values (set these CAREFULLY)
*/
/// The length of the `{previous,current}_epoch_attestations` lists.
///
/// Must be set to `MaxAttestations * SlotsPerEpoch`
// NOTE: we could safely instantiate these by using type-level arithmetic, but doing
// so adds ~25s to the time required to type-check this crate
type MaxPendingAttestations: Unsigned + Clone + Sync + Send + Debug + PartialEq;
/// The length of `eth1_data_votes`.
///
/// Must be set to `EpochsPerEth1VotingPeriod * SlotsPerEpoch`
type SlotsPerEth1VotingPeriod: Unsigned + Clone + Sync + Send + Debug + PartialEq;
/// The size of `sync_subcommittees`.
///
/// Must be set to `SyncCommitteeSize / SyncCommitteeSubnetCount`.
type SyncSubcommitteeSize: Unsigned + Clone + Sync + Send + Debug + PartialEq;
fn default_spec() -> ChainSpec;
fn spec_name() -> EthSpecId;
fn genesis_epoch() -> Epoch {
Epoch::new(Self::GenesisEpoch::to_u64())
}
/// Return the number of committees per slot.
///
/// Note: the number of committees per slot is constant in each epoch, and depends only on
/// the `active_validator_count` during the slot's epoch.
///
/// Spec v0.12.1
fn get_committee_count_per_slot(
active_validator_count: usize,
spec: &ChainSpec,
) -> Result<usize, Error> {
Self::get_committee_count_per_slot_with(
active_validator_count,
spec.max_committees_per_slot,
spec.target_committee_size,
)
}
fn get_committee_count_per_slot_with(
active_validator_count: usize,
max_committees_per_slot: usize,
target_committee_size: usize,
) -> Result<usize, Error> {
let slots_per_epoch = Self::SlotsPerEpoch::to_usize();
Ok(std::cmp::max(
1,
std::cmp::min(
max_committees_per_slot,
active_validator_count
.safe_div(slots_per_epoch)?
.safe_div(target_committee_size)?,
),
))
}
/// Returns the minimum number of validators required for this spec.
///
/// This is the _absolute_ minimum, the number required to make the chain operate in the most
/// basic sense. This count is not required to provide any security guarantees regarding
/// decentralization, entropy, etc.
fn minimum_validator_count() -> usize {
Self::SlotsPerEpoch::to_usize()
}
/// Returns the `SLOTS_PER_EPOCH` constant for this specification.
///
/// Spec v0.12.1
fn slots_per_epoch() -> u64 {
Self::SlotsPerEpoch::to_u64()
}
/// Returns the `SLOTS_PER_HISTORICAL_ROOT` constant for this specification.
///
/// Spec v0.12.1
fn slots_per_historical_root() -> usize {
Self::SlotsPerHistoricalRoot::to_usize()
}
/// Returns the `EPOCHS_PER_HISTORICAL_VECTOR` constant for this specification.
///
/// Spec v0.12.1
fn epochs_per_historical_vector() -> usize {
Self::EpochsPerHistoricalVector::to_usize()
}
/// Returns the `SLOTS_PER_ETH1_VOTING_PERIOD` constant for this specification.
///
/// Spec v0.12.1
fn slots_per_eth1_voting_period() -> usize {
Self::SlotsPerEth1VotingPeriod::to_usize()
}
/// Returns the `SYNC_COMMITTEE_SIZE` constant for this specification.
fn sync_committee_size() -> usize {
Self::SyncCommitteeSize::to_usize()
}
/// Returns the `SYNC_COMMITTEE_SIZE / SyncCommitteeSubnetCount`.
fn sync_subcommittee_size() -> usize {
Self::SyncSubcommitteeSize::to_usize()
}
/// Returns the `MAX_BYTES_PER_TRANSACTION` constant for this specification.
fn max_bytes_per_transaction() -> usize {
Self::MaxBytesPerTransaction::to_usize()
}
/// Returns the `MAX_TRANSACTIONS_PER_PAYLOAD` constant for this specification.
fn max_transactions_per_payload() -> usize {
Self::MaxTransactionsPerPayload::to_usize()
}
/// Returns the `MAX_EXTRA_DATA_BYTES` constant for this specification.
fn max_extra_data_bytes() -> usize {
Self::MaxExtraDataBytes::to_usize()
}
/// Returns the `BYTES_PER_LOGS_BLOOM` constant for this specification.
fn bytes_per_logs_bloom() -> usize {
Self::BytesPerLogsBloom::to_usize()
}
}
/// Macro to inherit some type values from another EthSpec.
#[macro_export]
macro_rules! params_from_eth_spec {
($spec_ty:ty { $($ty_name:ident),+ }) => {
$(type $ty_name = <$spec_ty as EthSpec>::$ty_name;)+
}
}
/// Ethereum Foundation specifications.
#[cfg_attr(feature = "arbitrary-fuzz", derive(arbitrary::Arbitrary))]
#[derive(Clone, PartialEq, Eq, Debug, Default, Serialize, Deserialize)]
pub struct MainnetEthSpec;
impl EthSpec for MainnetEthSpec {
type JustificationBitsLength = U4;
type SubnetBitfieldLength = U64;
type MaxValidatorsPerCommittee = U2048;
type GenesisEpoch = U0;
type SlotsPerEpoch = U32;
type EpochsPerEth1VotingPeriod = U64;
type SlotsPerHistoricalRoot = U8192;
type EpochsPerHistoricalVector = U65536;
type EpochsPerSlashingsVector = U8192;
type HistoricalRootsLimit = U16777216;
type ValidatorRegistryLimit = U1099511627776;
type MaxProposerSlashings = U16;
type MaxAttesterSlashings = U2;
type MaxAttestations = U128;
type MaxDeposits = U16;
type MaxVoluntaryExits = U16;
type SyncCommitteeSize = U512;
type SyncCommitteeSubnetCount = U4;
type MaxBytesPerTransaction = U1073741824; // 1,073,741,824
type MaxTransactionsPerPayload = U1048576; // 1,048,576
type BytesPerLogsBloom = U256;
type GasLimitDenominator = U1024;
type MinGasLimit = U5000;
type MaxExtraDataBytes = U32;
type SyncSubcommitteeSize = U128; // 512 committee size / 4 sync committee subnet count
type MaxPendingAttestations = U4096; // 128 max attestations * 32 slots per epoch
type SlotsPerEth1VotingPeriod = U2048; // 64 epochs * 32 slots per epoch
fn default_spec() -> ChainSpec {
ChainSpec::mainnet()
}
fn spec_name() -> EthSpecId {
EthSpecId::Mainnet
}
}
/// Ethereum Foundation minimal spec, as defined in the eth2.0-specs repo.
#[cfg_attr(feature = "arbitrary-fuzz", derive(arbitrary::Arbitrary))]
#[derive(Clone, PartialEq, Eq, Debug, Default, Serialize, Deserialize)]
pub struct MinimalEthSpec;
impl EthSpec for MinimalEthSpec {
type SlotsPerEpoch = U8;
type EpochsPerEth1VotingPeriod = U4;
type SlotsPerHistoricalRoot = U64;
type EpochsPerHistoricalVector = U64;
type EpochsPerSlashingsVector = U64;
type SyncCommitteeSize = U32;
type SyncSubcommitteeSize = U8; // 32 committee size / 4 sync committee subnet count
type MaxPendingAttestations = U1024; // 128 max attestations * 8 slots per epoch
type SlotsPerEth1VotingPeriod = U32; // 4 epochs * 8 slots per epoch
params_from_eth_spec!(MainnetEthSpec {
JustificationBitsLength,
SubnetBitfieldLength,
SyncCommitteeSubnetCount,
MaxValidatorsPerCommittee,
GenesisEpoch,
HistoricalRootsLimit,
ValidatorRegistryLimit,
MaxProposerSlashings,
MaxAttesterSlashings,
MaxAttestations,
MaxDeposits,
MaxVoluntaryExits,
MaxBytesPerTransaction,
MaxTransactionsPerPayload,
BytesPerLogsBloom,
GasLimitDenominator,
MinGasLimit,
MaxExtraDataBytes
});
fn default_spec() -> ChainSpec {
ChainSpec::minimal()
}
fn spec_name() -> EthSpecId {
EthSpecId::Minimal
}
}
/// Gnosis Beacon Chain specifications.
#[cfg_attr(feature = "arbitrary-fuzz", derive(arbitrary::Arbitrary))]
#[derive(Clone, PartialEq, Eq, Debug, Default, Serialize, Deserialize)]
pub struct GnosisEthSpec;
impl EthSpec for GnosisEthSpec {
type JustificationBitsLength = U4;
type SubnetBitfieldLength = U64;
type MaxValidatorsPerCommittee = U2048;
type GenesisEpoch = U0;
type SlotsPerEpoch = U16;
type EpochsPerEth1VotingPeriod = U64;
type SlotsPerHistoricalRoot = U8192;
type EpochsPerHistoricalVector = U65536;
type EpochsPerSlashingsVector = U8192;
type HistoricalRootsLimit = U16777216;
type ValidatorRegistryLimit = U1099511627776;
type MaxProposerSlashings = U16;
type MaxAttesterSlashings = U2;
type MaxAttestations = U128;
type MaxDeposits = U16;
type MaxVoluntaryExits = U16;
type SyncCommitteeSize = U512;
type SyncCommitteeSubnetCount = U4;
type MaxBytesPerTransaction = U1073741824; // 1,073,741,824
type MaxTransactionsPerPayload = U1048576; // 1,048,576
type BytesPerLogsBloom = U256;
type GasLimitDenominator = U1024;
type MinGasLimit = U5000;
type MaxExtraDataBytes = U32;
type SyncSubcommitteeSize = U128; // 512 committee size / 4 sync committee subnet count
type MaxPendingAttestations = U2048; // 128 max attestations * 16 slots per epoch
type SlotsPerEth1VotingPeriod = U1024; // 64 epochs * 16 slots per epoch
fn default_spec() -> ChainSpec {
ChainSpec::gnosis()
}
fn spec_name() -> EthSpecId {
EthSpecId::Gnosis
}
}