More progress

This commit is contained in:
dapplion
2025-04-04 14:16:28 -03:00
parent 614c01698d
commit 661bfebdf0
10 changed files with 148 additions and 101 deletions

View File

@@ -40,7 +40,7 @@ pub trait Eth2Enr {
) -> Result<EnrSyncCommitteeBitfield<E>, &'static str>;
/// The peerdas custody group count associated with the ENR.
fn custody_group_count<E: EthSpec>(&self, spec: &ChainSpec) -> Result<u64, &'static str>;
fn custody_group_count(&self, spec: &ChainSpec) -> Result<u64, &'static str>;
fn eth2(&self) -> Result<EnrForkId, &'static str>;
}
@@ -68,17 +68,19 @@ impl Eth2Enr for Enr {
.map_err(|_| "Could not decode the ENR syncnets bitfield")
}
fn custody_group_count<E: EthSpec>(&self, spec: &ChainSpec) -> Result<u64, &'static str> {
fn custody_group_count(&self, spec: &ChainSpec) -> Result<u64, &'static str> {
let cgc = self
.get_decodable::<u64>(PEERDAS_CUSTODY_GROUP_COUNT_ENR_KEY)
.ok_or("ENR custody group count non-existent")?
.map_err(|_| "Could not decode the ENR custody group count")?;
if (spec.custody_requirement..=spec.number_of_custody_groups).contains(&cgc) {
Ok(cgc)
} else {
Err("Invalid custody group count in ENR")
if cgc < spec.custody_requirement {
return Err("ENR CGC < custody_requirement");
}
if cgc > spec.number_of_custody_groups {
return Err("ENR CGC > number_of_custody_groups");
}
Ok(cgc)
}
fn eth2(&self) -> Result<EnrForkId, &'static str> {
@@ -363,7 +365,7 @@ mod test {
let enr = build_enr_with_config(config, &spec).0;
assert_eq!(
enr.custody_group_count::<E>(&spec).unwrap(),
enr.custody_group_count(&spec).unwrap(),
spec.custody_requirement,
);
}
@@ -378,7 +380,7 @@ mod test {
let enr = build_enr_with_config(config, &spec).0;
assert_eq!(
enr.custody_group_count::<E>(&spec).unwrap(),
enr.custody_group_count(&spec).unwrap(),
spec.number_of_custody_groups,
);
}

View File

@@ -594,6 +594,26 @@ impl<E: EthSpec> Discovery<E> {
enr::save_enr_to_disk(Path::new(&self.enr_dir), &self.local_enr());
}
/// Updates the `cgc` field of our local ENR.
pub fn update_cgc_enr(&mut self, cgc: u64) -> Result<bool, String> {
if let Ok(current_cgc) = self.local_enr().custody_group_count(&self.spec) {
if current_cgc == cgc {
return Ok(false);
}
}
self.discv5
.enr_insert(ETH2_ENR_KEY, &cgc)
.map_err(|e| format!("{:?}", e))?;
// replace the global version with discovery version
self.network_globals.set_enr(self.discv5.local_enr());
// persist modified enr to disk
enr::save_enr_to_disk(Path::new(&self.enr_dir), &self.local_enr());
Ok(true)
}
// Bans a peer and it's associated seen IP addresses.
pub fn ban_peer(&mut self, peer_id: &PeerId, ip_addresses: Vec<IpAddr>) {
// first try and convert the peer_id to a node_id.

View File

@@ -34,7 +34,7 @@ where
.as_ref()
.is_ok_and(|b| b.get(*s.deref() as usize).unwrap_or(false)),
Subnet::DataColumn(s) => {
if let Ok(custody_group_count) = enr.custody_group_count::<E>(&spec) {
if let Ok(custody_group_count) = enr.custody_group_count(&spec) {
compute_subnets_for_node(enr.node_id().raw(), custody_group_count, &spec)
.is_ok_and(|subnets| subnets.contains(s))
} else {

View File

@@ -173,7 +173,7 @@ impl<E: EthSpec> Network<E> {
pub async fn new(
executor: task_executor::TaskExecutor,
mut ctx: ServiceContext<'_>,
cgc_updates: Option<CGCUpdates>,
initial_cgc_updates: Option<CGCUpdates>,
) -> Result<(Self, Arc<NetworkGlobals<E>>), String> {
let config = ctx.config.clone();
trace!("Libp2p Service starting");
@@ -197,8 +197,8 @@ impl<E: EthSpec> Network<E> {
&ctx.chain_spec,
)?;
// TODO: Load from disk, and check consistency with DB somewhere
let cgc_updates = cgc_updates.unwrap_or_else(|| {
// Load initial CGC updates from persisted source (DB) or default to minimum CGC
let cgc_updates = initial_cgc_updates.unwrap_or_else(|| {
CGCUpdates::new(
ctx.chain_spec
.custody_group_count(config.subscribe_all_data_column_subnets),
@@ -1259,12 +1259,23 @@ impl<E: EthSpec> Network<E> {
crit!(error = e, "Could not update ENR bitfield");
}
// TODO: Can we deprecate this for a single source of truth?
// TODO(das): Can we deprecate this for a single source of truth?
let metadata = self.network_globals.local_metadata();
self.eth2_rpc_mut()
.update_seq_number(*metadata.seq_number());
}
/// Updates the CGC value in our local ENR
#[instrument(parent = None,
level = "trace",
fields(service = "libp2p"),
name = "libp2p",
skip_all
)]
pub fn update_enr_cgc(&mut self, cgc: u64) -> Result<bool, String> {
self.discovery_mut().update_cgc_enr(cgc)
}
/// 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,