diff --git a/beacon_node/http_api/src/test_utils.rs b/beacon_node/http_api/src/test_utils.rs index 74418bc415..930ab5e4af 100644 --- a/beacon_node/http_api/src/test_utils.rs +++ b/beacon_node/http_api/src/test_utils.rs @@ -14,7 +14,7 @@ use lighthouse_network::{ behaviour::{ConnectionEstablished, FromSwarm}, ConnectionId, NetworkBehaviour, }, - types::SyncState, + types::{CGCUpdates, SyncState}, ConnectedPoint, Enr, NetworkConfig, NetworkGlobals, PeerId, PeerManager, }; use network::{NetworkReceivers, NetworkSenders}; @@ -140,8 +140,10 @@ pub async fn create_api_server_with_config( let enr_key = CombinedKey::generate_secp256k1(); let enr = Enr::builder().build(&enr_key).unwrap(); let network_config = Arc::new(NetworkConfig::default()); + let cgc_updates = CGCUpdates::new(chain.spec.custody_requirement); let network_globals = Arc::new(NetworkGlobals::new( enr.clone(), + cgc_updates, vec![], false, network_config, diff --git a/beacon_node/lighthouse_network/src/discovery/mod.rs b/beacon_node/lighthouse_network/src/discovery/mod.rs index bd54f48f16..3ed538a9ba 100644 --- a/beacon_node/lighthouse_network/src/discovery/mod.rs +++ b/beacon_node/lighthouse_network/src/discovery/mod.rs @@ -1194,6 +1194,7 @@ impl Discovery { #[cfg(test)] mod tests { use super::*; + use crate::types::CGCUpdates; use libp2p::identity::secp256k1; use types::{BitVector, MinimalEthSpec, SubnetId}; @@ -1207,7 +1208,15 @@ mod tests { let config = Arc::new(config); let enr_key: CombinedKey = CombinedKey::from_secp256k1(&keypair); let enr: Enr = build_enr::(&enr_key, &config, &EnrForkId::default(), &spec).unwrap(); - let globals = NetworkGlobals::new(enr, vec![], false, config.clone(), spec.clone()); + let cgc_updates = CGCUpdates::new(spec.custody_requirement); + let globals = NetworkGlobals::new( + enr, + cgc_updates, + vec![], + false, + config.clone(), + spec.clone(), + ); let keypair = keypair.into(); Discovery::new(keypair, &config, Arc::new(globals), &spec) .await diff --git a/beacon_node/lighthouse_network/src/service/mod.rs b/beacon_node/lighthouse_network/src/service/mod.rs index a9050a4678..ac5e7a4ff0 100644 --- a/beacon_node/lighthouse_network/src/service/mod.rs +++ b/beacon_node/lighthouse_network/src/service/mod.rs @@ -15,7 +15,7 @@ use crate::rpc::{ }; use crate::types::{ all_topics_at_fork, core_topics_to_subscribe, is_fork_non_core_topic, subnet_from_topic_hash, - GossipEncoding, GossipKind, GossipTopic, SnappyTransform, Subnet, SubnetDiscovery, + CGCUpdates, GossipEncoding, GossipKind, GossipTopic, SnappyTransform, Subnet, SubnetDiscovery, }; use crate::EnrExt; use crate::Eth2Enr; @@ -196,9 +196,16 @@ impl Network { &ctx.chain_spec, )?; + // TODO: Load from disk, and check consistency with DB somewhere + let initial_cgc = ctx + .chain_spec + .custody_group_count(config.subscribe_all_data_column_subnets); + let cgc_updates = CGCUpdates::new(initial_cgc); + // Construct the metadata let globals = NetworkGlobals::new( enr, + cgc_updates, trusted_peers, config.disable_peer_scoring, config.clone(), diff --git a/beacon_node/lighthouse_network/src/types/globals.rs b/beacon_node/lighthouse_network/src/types/globals.rs index 9ac22711c6..4f0695384f 100644 --- a/beacon_node/lighthouse_network/src/types/globals.rs +++ b/beacon_node/lighthouse_network/src/types/globals.rs @@ -32,20 +32,23 @@ pub struct NetworkGlobals { all_sampling_subnets: Vec, all_sampling_columns: Vec, /// Dynamic custody group count (CGC) - custody_group_count: RwLock, + cgc_updates: RwLock, /// Network-related configuration. Immutable after initialization. pub config: Arc, /// Ethereum chain configuration. Immutable after initialization. pub spec: Arc, } -struct CustodyGroupCount { - value: u64, +pub struct CGCUpdates { + initial_value: u64, + updates: Vec<(Slot, u64)>, + // TODO(das): Track backfilled CGC } impl NetworkGlobals { pub fn new( enr: Enr, + cgc_updates: CGCUpdates, trusted_peers: Vec, disable_peer_scoring: bool, config: Arc, @@ -82,7 +85,7 @@ impl NetworkGlobals { backfill_state: RwLock::new(BackFillState::Paused), all_sampling_subnets, all_sampling_columns, - custody_group_count: RwLock::new(CustodyGroupCount { value: 0 }), + cgc_updates: RwLock::new(cgc_updates), config, spec, } @@ -157,8 +160,7 @@ impl NetworkGlobals { /// Returns the custody group count (CGC) fn custody_group_count(&self, slot: Slot) -> u64 { - let cgc = self.custody_group_count.read().value; - todo!("CGC at slot {slot} {cgc}"); + self.cgc_updates.read().at_slot(slot) } /// Returns the count of custody columns this node must sample for block import @@ -169,6 +171,11 @@ impl NetworkGlobals { .expect("should compute node sampling size from valid chain spec") } + /// Adds a new CGC value update + pub fn add_cgc_update(&self, update: (Slot, u64)) { + self.cgc_updates.write().add_latest_update(update); + } + /// Returns the number of libp2p connected peers. pub fn connected_peers(&self) -> usize { self.peers.read().connected_peer_ids().count() @@ -266,7 +273,32 @@ impl NetworkGlobals { let keypair = libp2p::identity::secp256k1::Keypair::generate(); let enr_key: discv5::enr::CombinedKey = discv5::enr::CombinedKey::from_secp256k1(&keypair); let enr = discv5::enr::Enr::builder().build(&enr_key).unwrap(); - NetworkGlobals::new(enr, trusted_peers, false, config, spec) + let cgc_updates = CGCUpdates::new(spec.custody_requirement); + NetworkGlobals::new(enr, cgc_updates, trusted_peers, false, config, spec) + } +} + +impl CGCUpdates { + pub fn new(initial_value: u64) -> Self { + Self { + initial_value, + updates: vec![], + } + } + + fn at_slot(&self, slot: Slot) -> u64 { + // TODO: Test and fix logic + for (update_slot, cgc) in &self.updates { + if slot > *update_slot { + return *cgc; + } + } + + self.initial_value + } + + fn add_latest_update(&mut self, update: (Slot, u64)) { + self.updates.push(update); } } diff --git a/beacon_node/lighthouse_network/src/types/mod.rs b/beacon_node/lighthouse_network/src/types/mod.rs index 868cdb6eb9..56fe030046 100644 --- a/beacon_node/lighthouse_network/src/types/mod.rs +++ b/beacon_node/lighthouse_network/src/types/mod.rs @@ -11,7 +11,7 @@ pub type EnrSyncCommitteeBitfield = BitVector<::SyncCommitteeSu pub type Enr = discv5::enr::Enr; pub use eth2::lighthouse::sync_state::{BackFillState, SyncState}; -pub use globals::NetworkGlobals; +pub use globals::{CGCUpdates, NetworkGlobals}; pub use pubsub::{PubsubMessage, SnappyTransform}; pub use subnet::{Subnet, SubnetDiscovery}; pub use topics::{