Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions bin/ethlambda/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ struct CliOptions {
/// Whether this node acts as a committee aggregator
#[arg(long, default_value = "false")]
is_aggregator: bool,
/// Number of attestation committees (subnets) per slot
#[arg(long, default_value = "1", value_parser = clap::value_parser!(u64).range(1..))]
attestation_committee_count: u64,
}

#[tokio::main]
Expand Down Expand Up @@ -130,6 +133,7 @@ async fn main() -> eyre::Result<()> {
p2p_rx,
store.clone(),
first_validator_id,
options.attestation_committee_count,
));

ethlambda_rpc::start_rpc_server(metrics_socket, store)
Expand Down
3 changes: 0 additions & 3 deletions crates/blockchain/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,6 @@ pub const MILLISECONDS_PER_SLOT: u64 = 4_000;
pub const MILLISECONDS_PER_INTERVAL: u64 = 800;
/// Number of intervals per slot (5 intervals of 800ms = 4 seconds).
pub const INTERVALS_PER_SLOT: u64 = 5;
/// Number of attestation committees per slot.
pub const ATTESTATION_COMMITTEE_COUNT: u64 = 1;

impl BlockChain {
pub fn spawn(
store: Store,
Expand Down
9 changes: 5 additions & 4 deletions crates/net/p2p/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{
time::Duration,
};

use ethlambda_blockchain::{ATTESTATION_COMMITTEE_COUNT, BlockChain, P2PMessage};
use ethlambda_blockchain::{BlockChain, P2PMessage};
use ethlambda_storage::Store;
use ethlambda_types::primitives::H256;
use ethrex_common::H264;
Expand Down Expand Up @@ -56,6 +56,7 @@ pub(crate) struct PendingRequest {
pub(crate) last_peer: Option<PeerId>,
}

#[allow(clippy::too_many_arguments)]
pub async fn start_p2p(
node_key: Vec<u8>,
bootnodes: Vec<Bootnode>,
Expand All @@ -64,6 +65,7 @@ pub async fn start_p2p(
p2p_rx: mpsc::UnboundedReceiver<P2PMessage>,
store: Store,
validator_id: Option<u64>,
attestation_committee_count: u64,
) {
let config = libp2p::gossipsub::ConfigBuilder::default()
// d
Expand Down Expand Up @@ -175,9 +177,8 @@ pub async fn start_p2p(
.unwrap();

// Subscribe to attestation subnet topic (validators subscribe to their committee's subnet)
// ATTESTATION_COMMITTEE_COUNT is 1 for devnet-3 but will increase in future devnets.
#[allow(clippy::modulo_one)]
let subnet_id = validator_id.map(|vid| vid % ATTESTATION_COMMITTEE_COUNT);
// attestation_committee_count is validated to be >= 1 by clap at CLI parse time.
let subnet_id = validator_id.map(|vid| vid % attestation_committee_count);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

modulo by zero will panic if attestation_committee_count is 0

Suggested change
let subnet_id = validator_id.map(|vid| vid % attestation_committee_count);
let subnet_id = validator_id.and_then(|vid| {
if attestation_committee_count == 0 {
None
} else {
Some(vid % attestation_committee_count)
}
});
Prompt To Fix With AI
This is a comment left during a code review.
Path: crates/net/p2p/src/lib.rs
Line: 180

Comment:
modulo by zero will panic if `attestation_committee_count` is 0

```suggestion
    let subnet_id = validator_id.and_then(|vid| {
        if attestation_committee_count == 0 {
            None
        } else {
            Some(vid % attestation_committee_count)
        }
    });
```

How can I resolve this? If you propose a fix, please make it concise.

let attestation_topic_kind = match subnet_id {
Some(id) => format!("{ATTESTATION_SUBNET_TOPIC_PREFIX}_{id}"),
// Non-validators subscribe to subnet 0 to receive attestations
Expand Down