mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-20 05:14:35 +00:00
@@ -45,22 +45,24 @@ pub trait Eth2Enr {
|
||||
|
||||
impl Eth2Enr for Enr {
|
||||
fn attestation_bitfield<E: EthSpec>(&self) -> Result<EnrAttestationBitfield<E>, &'static str> {
|
||||
let bitfield_bytes = self
|
||||
.get(ATTESTATION_BITFIELD_ENR_KEY)
|
||||
.ok_or("ENR attestation bitfield non-existent")?;
|
||||
let bitfield_bytes: Vec<u8> = self
|
||||
.get_decodable(ATTESTATION_BITFIELD_ENR_KEY)
|
||||
.ok_or("ENR attestation bitfield non-existent")?
|
||||
.map_err(|_| "Invalid RLP Encoding")?;
|
||||
|
||||
BitVector::<E::SubnetBitfieldLength>::from_ssz_bytes(bitfield_bytes)
|
||||
BitVector::<E::SubnetBitfieldLength>::from_ssz_bytes(&bitfield_bytes)
|
||||
.map_err(|_| "Could not decode the ENR attnets bitfield")
|
||||
}
|
||||
|
||||
fn sync_committee_bitfield<E: EthSpec>(
|
||||
&self,
|
||||
) -> Result<EnrSyncCommitteeBitfield<E>, &'static str> {
|
||||
let bitfield_bytes = self
|
||||
.get(SYNC_COMMITTEE_BITFIELD_ENR_KEY)
|
||||
.ok_or("ENR sync committee bitfield non-existent")?;
|
||||
let bitfield_bytes: Vec<u8> = self
|
||||
.get_decodable(SYNC_COMMITTEE_BITFIELD_ENR_KEY)
|
||||
.ok_or("ENR sync committee bitfield non-existent")?
|
||||
.map_err(|_| "Invalid RLP Encoding")?;
|
||||
|
||||
BitVector::<E::SyncCommitteeSubnetCount>::from_ssz_bytes(bitfield_bytes)
|
||||
BitVector::<E::SyncCommitteeSubnetCount>::from_ssz_bytes(&bitfield_bytes)
|
||||
.map_err(|_| "Could not decode the ENR syncnets bitfield")
|
||||
}
|
||||
|
||||
@@ -78,9 +80,12 @@ impl Eth2Enr for Enr {
|
||||
}
|
||||
|
||||
fn eth2(&self) -> Result<EnrForkId, &'static str> {
|
||||
let eth2_bytes = self.get(ETH2_ENR_KEY).ok_or("ENR has no eth2 field")?;
|
||||
let eth2_bytes: Vec<u8> = self
|
||||
.get_decodable(ETH2_ENR_KEY)
|
||||
.ok_or("ENR has no eth2 field")?
|
||||
.map_err(|_| "Invalid RLP Encoding")?;
|
||||
|
||||
EnrForkId::from_ssz_bytes(eth2_bytes).map_err(|_| "Could not decode EnrForkId")
|
||||
EnrForkId::from_ssz_bytes(ð2_bytes).map_err(|_| "Could not decode EnrForkId")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -270,16 +275,16 @@ fn compare_enr(local_enr: &Enr, disk_enr: &Enr) -> bool {
|
||||
&& local_enr.quic4() == disk_enr.quic4()
|
||||
&& local_enr.quic6() == disk_enr.quic6()
|
||||
// must match on the same fork
|
||||
&& local_enr.get(ETH2_ENR_KEY) == disk_enr.get(ETH2_ENR_KEY)
|
||||
&& local_enr.get_decodable::<Vec<u8>>(ETH2_ENR_KEY) == disk_enr.get_decodable(ETH2_ENR_KEY)
|
||||
// take preference over disk udp port if one is not specified
|
||||
&& (local_enr.udp4().is_none() || local_enr.udp4() == disk_enr.udp4())
|
||||
&& (local_enr.udp6().is_none() || local_enr.udp6() == disk_enr.udp6())
|
||||
// we need the ATTESTATION_BITFIELD_ENR_KEY and SYNC_COMMITTEE_BITFIELD_ENR_KEY and
|
||||
// PEERDAS_CUSTODY_SUBNET_COUNT_ENR_KEY key to match, otherwise we use a new ENR. This will
|
||||
// likely only be true for non-validating nodes.
|
||||
&& local_enr.get(ATTESTATION_BITFIELD_ENR_KEY) == disk_enr.get(ATTESTATION_BITFIELD_ENR_KEY)
|
||||
&& local_enr.get(SYNC_COMMITTEE_BITFIELD_ENR_KEY) == disk_enr.get(SYNC_COMMITTEE_BITFIELD_ENR_KEY)
|
||||
&& local_enr.get(PEERDAS_CUSTODY_SUBNET_COUNT_ENR_KEY) == disk_enr.get(PEERDAS_CUSTODY_SUBNET_COUNT_ENR_KEY)
|
||||
&& local_enr.get_decodable::<Vec<u8>>(ATTESTATION_BITFIELD_ENR_KEY) == disk_enr.get_decodable(ATTESTATION_BITFIELD_ENR_KEY)
|
||||
&& local_enr.get_decodable::<Vec<u8>>(SYNC_COMMITTEE_BITFIELD_ENR_KEY) == disk_enr.get_decodable(SYNC_COMMITTEE_BITFIELD_ENR_KEY)
|
||||
&& local_enr.get_decodable::<Vec<u8>>(PEERDAS_CUSTODY_SUBNET_COUNT_ENR_KEY) == disk_enr.get_decodable(PEERDAS_CUSTODY_SUBNET_COUNT_ENR_KEY)
|
||||
}
|
||||
|
||||
/// Loads enr from the given directory
|
||||
|
||||
@@ -1072,10 +1072,7 @@ impl<E: EthSpec> NetworkBehaviour for Discovery<E> {
|
||||
// NOTE: We assume libp2p itself can keep track of IP changes and we do
|
||||
// not inform it about IP changes found via discovery.
|
||||
}
|
||||
discv5::Event::EnrAdded { .. }
|
||||
| discv5::Event::TalkRequest(_)
|
||||
| discv5::Event::NodeInserted { .. }
|
||||
| discv5::Event::SessionEstablished { .. } => {} // Ignore all other discv5 server events
|
||||
_ => {} // Ignore all other discv5 server events
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ tokio-stream = { workspace = true }
|
||||
smallvec = { workspace = true }
|
||||
rand = { workspace = true }
|
||||
fnv = { workspace = true }
|
||||
rlp = "0.5.0"
|
||||
alloy-rlp = { workspace = true }
|
||||
lighthouse_metrics = { workspace = true }
|
||||
logging = { workspace = true }
|
||||
task_executor = { workspace = true }
|
||||
|
||||
@@ -45,14 +45,23 @@ impl StoreItem for PersistedDht {
|
||||
}
|
||||
|
||||
fn as_store_bytes(&self) -> Vec<u8> {
|
||||
rlp::encode_list(&self.enrs).to_vec()
|
||||
let mut buffer = Vec::<u8>::new();
|
||||
alloy_rlp::encode_list(&self.enrs, &mut buffer);
|
||||
buffer
|
||||
}
|
||||
|
||||
fn from_store_bytes(bytes: &[u8]) -> Result<Self, StoreError> {
|
||||
let rlp = rlp::Rlp::new(bytes);
|
||||
let enrs: Vec<Enr> = rlp
|
||||
.as_list()
|
||||
.map_err(|e| StoreError::RlpError(format!("{}", e)))?;
|
||||
let mut enrs: Vec<Enr> = Vec::new();
|
||||
let mut rlp = alloy_rlp::Rlp::new(bytes)
|
||||
.map_err(|e| StoreError::RlpError(format!("Failed to decode RLP: {}", e)))?;
|
||||
loop {
|
||||
match rlp.get_next() {
|
||||
Ok(Some(enr)) => enrs.push(enr),
|
||||
Ok(None) => break, // No more list elements
|
||||
Err(e) => return Err(StoreError::RlpError(format!("{}", e))),
|
||||
}
|
||||
}
|
||||
|
||||
Ok(PersistedDht { enrs })
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user