Implement basic validator custody framework (no backfill) (#7578)

Resolves #6767


  This PR implements a basic version of validator custody.
- It introduces a new `CustodyContext` object which contains info regarding number of validators attached to a node and  the custody count they contribute to the cgc.
- The `CustodyContext` is added in the da_checker and has methods for returning the current cgc and the number of columns to sample at head. Note that the logic for returning the cgc existed previously in the network globals.
- To estimate the number of validators attached, we use the `beacon_committee_subscriptions` endpoint. This might overestimate the number of validators actually publishing attestations from the node in the case of multi BN setups. We could also potentially use the `publish_attestations` endpoint to get a more conservative estimate at a later point.
- Anytime there's a change in the `custody_group_count` due to addition/removal of validators, the custody context should send an event on a broadcast channnel. The only subscriber for the channel exists in the network service which simply subscribes to more subnets. There can be additional subscribers in sync that will start a backfill once the cgc changes.

TODO

- [ ] **NOT REQUIRED:** Currently, the logic only handles an increase in validator count and does not handle a decrease. We should ideally unsubscribe from subnets when the cgc has decreased.
- [ ] **NOT REQUIRED:** Add a service in the `CustodyContext` that emits an event once `MIN_EPOCHS_FOR_BLOB_SIDECARS_REQUESTS ` passes after updating the current cgc. This event should be picked up by a subscriber which updates the enr and metadata.
- [x] Add more tests
This commit is contained in:
Pawan Dhananjay
2025-06-11 11:10:06 -07:00
committed by GitHub
parent 076a1c3fae
commit 5f208bb858
38 changed files with 928 additions and 350 deletions

View File

@@ -177,6 +177,7 @@ impl<E: EthSpec> Network<E> {
pub async fn new(
executor: task_executor::TaskExecutor,
mut ctx: ServiceContext<'_>,
custody_group_count: u64,
) -> Result<(Self, Arc<NetworkGlobals<E>>), String> {
let config = ctx.config.clone();
trace!("Libp2p Service starting");
@@ -201,11 +202,12 @@ impl<E: EthSpec> Network<E> {
)?;
// Construct the metadata
let custody_group_count = ctx.chain_spec.is_peer_das_scheduled().then(|| {
ctx.chain_spec
.custody_group_count(config.subscribe_all_data_column_subnets)
});
let meta_data = utils::load_or_build_metadata(&config.network_dir, custody_group_count);
let custody_group_count_metadata = ctx
.chain_spec
.is_peer_das_scheduled()
.then_some(custody_group_count);
let meta_data =
utils::load_or_build_metadata(&config.network_dir, custody_group_count_metadata);
let seq_number = *meta_data.seq_number();
let globals = NetworkGlobals::new(
enr,
@@ -885,6 +887,23 @@ impl<E: EthSpec> Network<E> {
}
}
/// Subscribe to all data columns determined by the cgc.
#[instrument(parent = None,
level = "trace",
fields(service = "libp2p"),
name = "libp2p",
skip_all
)]
pub fn subscribe_new_data_column_subnets(&mut self, custody_column_count: u64) {
self.network_globals
.update_data_column_subnets(custody_column_count);
for column in self.network_globals.sampling_subnets() {
let kind = GossipKind::DataColumnSidecar(column);
self.subscribe_kind(kind);
}
}
/// Returns the scoring parameters for a topic if set.
#[instrument(parent = None,
level = "trace",
@@ -1254,6 +1273,21 @@ impl<E: EthSpec> Network<E> {
self.update_metadata_bitfields();
}
/// Updates the cgc value in the ENR.
#[instrument(parent = None,
level = "trace",
fields(service = "libp2p"),
name = "libp2p",
skip_all
)]
pub fn update_enr_cgc(&mut self, new_custody_group_count: u64) {
if let Err(e) = self.discovery_mut().update_enr_cgc(new_custody_group_count) {
crit!(error = e, "Could not update cgc in ENR");
}
// update the local meta data which informs our peers of the update during PINGS
self.update_metadata_cgc(new_custody_group_count);
}
/// Attempts to discover new peers for a given subnet. The `min_ttl` gives the time at which we
/// would like to retain the peers for.
#[instrument(parent = None,
@@ -1368,6 +1402,28 @@ impl<E: EthSpec> Network<E> {
utils::save_metadata_to_disk(&self.network_dir, meta_data);
}
#[instrument(parent = None,
level = "trace",
fields(service = "libp2p"),
name = "libp2p",
skip_all
)]
fn update_metadata_cgc(&mut self, custody_group_count: u64) {
let mut meta_data_w = self.network_globals.local_metadata.write();
*meta_data_w.seq_number_mut() += 1;
if let Ok(cgc) = meta_data_w.custody_group_count_mut() {
*cgc = custody_group_count;
}
let seq_number = *meta_data_w.seq_number();
let meta_data = meta_data_w.clone();
drop(meta_data_w);
self.eth2_rpc_mut().update_seq_number(seq_number);
// Save the updated metadata to disk
utils::save_metadata_to_disk(&self.network_dir, meta_data);
}
/// Sends a Ping request to the peer.
#[instrument(parent = None,
level = "trace",