Skip to content

Commit

Permalink
Add chainspec and genesis validators root to eth2_libp2p
Browse files Browse the repository at this point in the history
  • Loading branch information
pawanjay176 committed Apr 27, 2021
1 parent d0f63e0 commit b6728c1
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 11 deletions.
10 changes: 9 additions & 1 deletion beacon_node/eth2_libp2p/src/behaviour/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ use std::{
sync::Arc,
task::{Context, Poll},
};
use types::{ChainSpec, EnrForkId, EthSpec, SignedBeaconBlock, Slot, SubnetId};
use types::{ChainSpec, EnrForkId, EthSpec, Hash256, SignedBeaconBlock, Slot, SubnetId};

mod gossipsub_scoring_parameters;
mod handler;
Expand Down Expand Up @@ -136,6 +136,11 @@ pub struct Behaviour<TSpec: EthSpec> {

score_settings: PeerScoreSettings<TSpec>,

spec: ChainSpec,

/// The genesis root for the eth2 network
genesis_validators_root: Hash256,

/// The interval for updating gossipsub scores
update_gossipsub_scores: tokio::time::Interval,
}
Expand All @@ -147,6 +152,7 @@ impl<TSpec: EthSpec> Behaviour<TSpec> {
net_conf: &NetworkConfig,
network_globals: Arc<NetworkGlobals<TSpec>>,
log: &slog::Logger,
genesis_validators_root: Hash256,
chain_spec: &ChainSpec,
) -> error::Result<Self> {
let behaviour_log = log.new(o!());
Expand Down Expand Up @@ -232,7 +238,9 @@ impl<TSpec: EthSpec> Behaviour<TSpec> {
network_dir: net_conf.network_dir.clone(),
log: behaviour_log,
score_settings,
spec: chain_spec.clone(),
update_gossipsub_scores,
genesis_validators_root,
})
}

Expand Down
4 changes: 3 additions & 1 deletion beacon_node/eth2_libp2p/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use std::io::prelude::*;
use std::pin::Pin;
use std::sync::Arc;
use std::time::Duration;
use types::{ChainSpec, EnrForkId, EthSpec};
use types::{ChainSpec, EnrForkId, EthSpec, Hash256};

pub const NETWORK_KEY_FILENAME: &str = "key";
/// The maximum simultaneous libp2p connections per peer.
Expand Down Expand Up @@ -64,6 +64,7 @@ impl<TSpec: EthSpec> Service<TSpec> {
config: &NetworkConfig,
enr_fork_id: EnrForkId,
log: &Logger,
genesis_validators_root: Hash256,
chain_spec: &ChainSpec,
) -> error::Result<(Arc<NetworkGlobals<TSpec>>, Self)> {
let log = log.new(o!("service"=> "libp2p"));
Expand Down Expand Up @@ -113,6 +114,7 @@ impl<TSpec: EthSpec> Service<TSpec> {
config,
network_globals.clone(),
&log,
genesis_validators_root,
chain_spec,
)
.await?;
Expand Down
1 change: 1 addition & 0 deletions beacon_node/network/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ impl<T: BeaconChainTypes> NetworkService<T> {
config,
enr_fork_id,
&network_log,
beacon_chain.genesis_validators_root,
&beacon_chain.spec,
)
.await?;
Expand Down
2 changes: 1 addition & 1 deletion boot_node/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ impl<T: EthSpec> TryFrom<&ArgMatches<'_>> for BootNodeConfig<T> {

// If we know of the ENR field, add it to the initial construction
if let Some(enr_fork_bytes) = enr_fork {
builder.add_value("eth2", &enr_fork_bytes);
builder.add_value("eth2", enr_fork_bytes.as_slice());
}
builder
.build(&local_key)
Expand Down
29 changes: 21 additions & 8 deletions consensus/types/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,20 +157,33 @@ impl ChainSpec {
}

/// Returns an `EnrForkId` for the given `slot`.
///
/// Presently, we don't have any forks so we just ignore the slot. In the future this function
/// may return something different based upon the slot.
pub fn enr_fork_id(&self, _slot: Slot, genesis_validators_root: Hash256) -> EnrForkId {
pub fn enr_fork_id(&self, slot: Slot, genesis_validators_root: Hash256) -> EnrForkId {
EnrForkId {
fork_digest: Self::compute_fork_digest(
self.genesis_fork_version,
genesis_validators_root,
),
fork_digest: self.fork_digest(slot, genesis_validators_root),
next_fork_version: self.genesis_fork_version,
next_fork_epoch: self.far_future_epoch,
}
}

/// Returns the `ForkDigest` for the given slot.
///
/// Add additional if else branches with additional forks.
pub fn fork_digest(&self, slot: Slot, genesis_validators_root: Hash256) -> [u8; 4] {
if slot >= self.altair_fork_slot {
Self::compute_fork_digest(self.altair_fork_version, genesis_validators_root)
} else {
Self::compute_fork_digest(self.genesis_fork_version, genesis_validators_root)
}
}

pub fn genesis_fork_digest(&self, genesis_validators_root: Hash256) -> [u8; 4] {
Self::compute_fork_digest(self.genesis_fork_version, genesis_validators_root)
}

pub fn altair_fork_digest(&self, genesis_validators_root: Hash256) -> [u8; 4] {
Self::compute_fork_digest(self.altair_fork_version, genesis_validators_root)
}

/// Returns the epoch of the next scheduled change in the `fork.current_version`.
///
/// There are no future forks scheduled so this function always returns `None`. This may not
Expand Down

0 comments on commit b6728c1

Please sign in to comment.