Skip to content

Commit b6728c1

Browse files
committed
Add chainspec and genesis validators root to eth2_libp2p
1 parent d0f63e0 commit b6728c1

File tree

5 files changed

+35
-11
lines changed

5 files changed

+35
-11
lines changed

beacon_node/eth2_libp2p/src/behaviour/mod.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ use std::{
4343
sync::Arc,
4444
task::{Context, Poll},
4545
};
46-
use types::{ChainSpec, EnrForkId, EthSpec, SignedBeaconBlock, Slot, SubnetId};
46+
use types::{ChainSpec, EnrForkId, EthSpec, Hash256, SignedBeaconBlock, Slot, SubnetId};
4747

4848
mod gossipsub_scoring_parameters;
4949
mod handler;
@@ -136,6 +136,11 @@ pub struct Behaviour<TSpec: EthSpec> {
136136

137137
score_settings: PeerScoreSettings<TSpec>,
138138

139+
spec: ChainSpec,
140+
141+
/// The genesis root for the eth2 network
142+
genesis_validators_root: Hash256,
143+
139144
/// The interval for updating gossipsub scores
140145
update_gossipsub_scores: tokio::time::Interval,
141146
}
@@ -147,6 +152,7 @@ impl<TSpec: EthSpec> Behaviour<TSpec> {
147152
net_conf: &NetworkConfig,
148153
network_globals: Arc<NetworkGlobals<TSpec>>,
149154
log: &slog::Logger,
155+
genesis_validators_root: Hash256,
150156
chain_spec: &ChainSpec,
151157
) -> error::Result<Self> {
152158
let behaviour_log = log.new(o!());
@@ -232,7 +238,9 @@ impl<TSpec: EthSpec> Behaviour<TSpec> {
232238
network_dir: net_conf.network_dir.clone(),
233239
log: behaviour_log,
234240
score_settings,
241+
spec: chain_spec.clone(),
235242
update_gossipsub_scores,
243+
genesis_validators_root,
236244
})
237245
}
238246

beacon_node/eth2_libp2p/src/service.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use std::io::prelude::*;
2525
use std::pin::Pin;
2626
use std::sync::Arc;
2727
use std::time::Duration;
28-
use types::{ChainSpec, EnrForkId, EthSpec};
28+
use types::{ChainSpec, EnrForkId, EthSpec, Hash256};
2929

3030
pub const NETWORK_KEY_FILENAME: &str = "key";
3131
/// The maximum simultaneous libp2p connections per peer.
@@ -64,6 +64,7 @@ impl<TSpec: EthSpec> Service<TSpec> {
6464
config: &NetworkConfig,
6565
enr_fork_id: EnrForkId,
6666
log: &Logger,
67+
genesis_validators_root: Hash256,
6768
chain_spec: &ChainSpec,
6869
) -> error::Result<(Arc<NetworkGlobals<TSpec>>, Self)> {
6970
let log = log.new(o!("service"=> "libp2p"));
@@ -113,6 +114,7 @@ impl<TSpec: EthSpec> Service<TSpec> {
113114
config,
114115
network_globals.clone(),
115116
&log,
117+
genesis_validators_root,
116118
chain_spec,
117119
)
118120
.await?;

beacon_node/network/src/service.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ impl<T: BeaconChainTypes> NetworkService<T> {
165165
config,
166166
enr_fork_id,
167167
&network_log,
168+
beacon_chain.genesis_validators_root,
168169
&beacon_chain.spec,
169170
)
170171
.await?;

boot_node/src/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ impl<T: EthSpec> TryFrom<&ArgMatches<'_>> for BootNodeConfig<T> {
111111

112112
// If we know of the ENR field, add it to the initial construction
113113
if let Some(enr_fork_bytes) = enr_fork {
114-
builder.add_value("eth2", &enr_fork_bytes);
114+
builder.add_value("eth2", enr_fork_bytes.as_slice());
115115
}
116116
builder
117117
.build(&local_key)

consensus/types/src/chain_spec.rs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -157,20 +157,33 @@ impl ChainSpec {
157157
}
158158

159159
/// Returns an `EnrForkId` for the given `slot`.
160-
///
161-
/// Presently, we don't have any forks so we just ignore the slot. In the future this function
162-
/// may return something different based upon the slot.
163-
pub fn enr_fork_id(&self, _slot: Slot, genesis_validators_root: Hash256) -> EnrForkId {
160+
pub fn enr_fork_id(&self, slot: Slot, genesis_validators_root: Hash256) -> EnrForkId {
164161
EnrForkId {
165-
fork_digest: Self::compute_fork_digest(
166-
self.genesis_fork_version,
167-
genesis_validators_root,
168-
),
162+
fork_digest: self.fork_digest(slot, genesis_validators_root),
169163
next_fork_version: self.genesis_fork_version,
170164
next_fork_epoch: self.far_future_epoch,
171165
}
172166
}
173167

168+
/// Returns the `ForkDigest` for the given slot.
169+
///
170+
/// Add additional if else branches with additional forks.
171+
pub fn fork_digest(&self, slot: Slot, genesis_validators_root: Hash256) -> [u8; 4] {
172+
if slot >= self.altair_fork_slot {
173+
Self::compute_fork_digest(self.altair_fork_version, genesis_validators_root)
174+
} else {
175+
Self::compute_fork_digest(self.genesis_fork_version, genesis_validators_root)
176+
}
177+
}
178+
179+
pub fn genesis_fork_digest(&self, genesis_validators_root: Hash256) -> [u8; 4] {
180+
Self::compute_fork_digest(self.genesis_fork_version, genesis_validators_root)
181+
}
182+
183+
pub fn altair_fork_digest(&self, genesis_validators_root: Hash256) -> [u8; 4] {
184+
Self::compute_fork_digest(self.altair_fork_version, genesis_validators_root)
185+
}
186+
174187
/// Returns the epoch of the next scheduled change in the `fork.current_version`.
175188
///
176189
/// There are no future forks scheduled so this function always returns `None`. This may not

0 commit comments

Comments
 (0)