Skip to content

Commit

Permalink
Update eth2_libp2p
Browse files Browse the repository at this point in the history
  • Loading branch information
povi committed Aug 16, 2024
1 parent 1b0700e commit 2687c17
Show file tree
Hide file tree
Showing 9 changed files with 159 additions and 6 deletions.
17 changes: 16 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ members = [
'deposit_tree',
'directories',
'eip_2335',
'eip_7594',
'eth1',
'eth1_api',
'eth2_cache_utils',
Expand Down Expand Up @@ -439,6 +440,7 @@ database = { path = 'database' }
deposit_tree = { path = 'deposit_tree' }
directories = { path = 'directories' }
eip_2335 = { path = 'eip_2335' }
eip_7594 = { path = 'eip_7594' }
eth1 = { path = 'eth1' }
eth1_api = { path = 'eth1_api' }
eth2_cache_utils = { path = 'eth2_cache_utils' }
Expand Down
20 changes: 20 additions & 0 deletions eip_7594/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
name = 'eip_7594'
edition = { workspace = true }
authors = ["Grandine <info@grandine.io>"]

[lints]
workspace = true

[dependencies]
num-traits = { workspace = true }
ssz = { workspace = true}
sha2 = { workspace = true }
typenum = { workspace = true }
types = { workspace = true }

[dev-dependencies]
duplicate = { workspace = true }
serde = { workspace = true }
spec_test_utils = { workspace = true }
test-generator = { workspace = true }
101 changes: 101 additions & 0 deletions eip_7594/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
use num_traits::One as _;
use sha2::{Digest as _, Sha256};
use ssz::Uint256;
use typenum::Unsigned as _;
use types::{
eip7594::{ColumnIndex, NumberOfColumns, DATA_COLUMN_SIDECAR_SUBNET_COUNT},
phase0::primitives::NodeId,
};

pub fn get_custody_columns(node_id: NodeId, custody_subnet_count: u64) -> Vec<ColumnIndex> {
assert!(custody_subnet_count <= DATA_COLUMN_SIDECAR_SUBNET_COUNT);

let mut subnet_ids = vec![];
let mut current_id = node_id;

while (subnet_ids.len() as u64) < custody_subnet_count {
let mut hasher = Sha256::new();
let mut bytes: [u8; 32] = [0; 32];

current_id.into_raw().to_little_endian(&mut bytes);

hasher.update(&bytes);
bytes = hasher.finalize().into();

let output_prefix = [
bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6], bytes[7],
];

let output_prefix_u64 = u64::from_le_bytes(output_prefix);
let subnet_id = output_prefix_u64 % DATA_COLUMN_SIDECAR_SUBNET_COUNT;

if !subnet_ids.contains(&subnet_id) {
subnet_ids.push(subnet_id);
}

if current_id == Uint256::MAX {
current_id = Uint256::ZERO;
}

current_id = current_id + Uint256::one();
}

let columns_per_subnet = NumberOfColumns::U64 / DATA_COLUMN_SIDECAR_SUBNET_COUNT;
let mut result = Vec::new();
for i in 0..columns_per_subnet {
for &subnet_id in &subnet_ids {
result.push(
(DATA_COLUMN_SIDECAR_SUBNET_COUNT * i + subnet_id)
.try_into()
.unwrap(),
);
}
}

result.sort();
result
}

#[cfg(test)]
mod tests {
use duplicate::duplicate_item;
use serde::Deserialize;
use spec_test_utils::Case;
use test_generator::test_resources;
use types::{
phase0::primitives::NodeId,
preset::{Mainnet, Minimal, Preset},
};

use crate::{get_custody_columns, ColumnIndex};

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct Meta {
description: Option<String>,
node_id: NodeId,
custody_subnet_count: u64,
result: Vec<ColumnIndex>,
}

#[duplicate_item(
glob function_name preset;
["consensus-spec-tests/tests/mainnet/eip7594/networking/get_custody_columns/*/*"] [get_custody_columns_mainnet] [Mainnet];
["consensus-spec-tests/tests/minimal/eip7594/networking/get_custody_columns/*/*"] [get_custody_columns_minimal] [Minimal];
)]
#[test_resources(glob)]
fn function_name(case: Case) {
run_case::<preset>(case);
}

fn run_case<P: Preset>(case: Case) {
let Meta {
description: _description,
node_id,
custody_subnet_count,
result,
} = case.yaml::<Meta>("meta");

assert_eq!(get_custody_columns(node_id, custody_subnet_count), result);
}
}
14 changes: 11 additions & 3 deletions p2p/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ impl<P: Preset> Network<P> {
metrics: Option<Arc<Metrics>>,
libp2p_registry: Option<&mut Registry>,
) -> Result<Self> {
let chain_config = controller.chain_config().as_ref();
let chain_config = controller.chain_config();
let head_state = controller.head_state().value;

let fork_context = Arc::new(ForkContext::new::<P>(
Expand All @@ -157,8 +157,13 @@ impl<P: Preset> Network<P> {
};

// Box the future to pass `clippy::large_futures`.
let (service, network_globals) =
Box::pin(Service::new(chain_config, executor, context, &logger)).await?;
let (service, network_globals) = Box::pin(Service::new(
chain_config.clone_arc(),
executor,
context,
&logger,
))
.await?;

let mut port_mappings = None;

Expand Down Expand Up @@ -1318,6 +1323,9 @@ impl<P: Preset> Network<P> {
block_seen,
);
}
PubsubMessage::DataColumnSidecar(_) => {
// TODO
}
PubsubMessage::AggregateAndProofAttestation(aggregate_and_proof) => {
if let Some(metrics) = self.metrics.as_ref() {
metrics.register_gossip_object(&["aggregate_and_proof_attestation"]);
Expand Down
1 change: 1 addition & 0 deletions scripts/ci/clippy.bash
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ options=(
--package deposit_tree
--package directories
--package eip_2335
--package eip_7594
--package eth1
--package eth1_api
--package eth2_cache_utils
Expand Down
3 changes: 2 additions & 1 deletion ssz/src/uint256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,8 @@ impl Uint256 {
Self(self.into_raw().saturating_mul(rhs.into_raw()))
}

const fn into_raw(self) -> RawUint256 {
#[must_use]
pub const fn into_raw(self) -> RawUint256 {
self.0
}
}
5 changes: 5 additions & 0 deletions types/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,11 @@ impl Config {
epoch >= self.eip7594_fork_epoch
}

#[must_use]
pub const fn is_eip7594_fork_epoch_set(&self) -> bool {
self.eip7594_fork_epoch != FAR_FUTURE_EPOCH
}

fn fork_slots<P: Preset>(&self) -> impl Iterator<Item = (Phase, Toption<Slot>)> + '_ {
enum_iterator::all().map(|phase| (phase, self.fork_slot::<P>(phase)))
}
Expand Down

0 comments on commit 2687c17

Please sign in to comment.