@@ -25,7 +25,7 @@ pub const ATTESTATION_BITFIELD_ENR_KEY: &str = "attnets";
2525/// The ENR field specifying the sync committee subnet bitfield.
2626pub const SYNC_COMMITTEE_BITFIELD_ENR_KEY : & str = "syncnets" ;
2727/// The ENR field specifying the peerdas custody subnet count.
28- pub const PEERDAS_CUSTODY_SUBNET_COUNT_ENR_KEY : & str = "custody_subnet_count " ;
28+ pub const PEERDAS_CUSTODY_SUBNET_COUNT_ENR_KEY : & str = "csc " ;
2929
3030/// Extension trait for ENR's within Eth2.
3131pub trait Eth2Enr {
@@ -68,7 +68,9 @@ impl Eth2Enr for Enr {
6868 /// defined in the spec.
6969 fn custody_subnet_count < E : EthSpec > ( & self , spec : & ChainSpec ) -> u64 {
7070 self . get ( PEERDAS_CUSTODY_SUBNET_COUNT_ENR_KEY )
71- . and_then ( |custody_bytes| u64:: from_ssz_bytes ( custody_bytes) . ok ( ) )
71+ . and_then ( |custody_bytes| custody_bytes. try_into ( ) . map ( u64:: from_be_bytes) . ok ( ) )
72+ // If value supplied in ENR is invalid, fallback to `custody_requirement`
73+ . filter ( |csc| csc <= & spec. data_column_sidecar_subnet_count )
7274 . unwrap_or ( spec. custody_requirement )
7375 }
7476
@@ -243,10 +245,8 @@ pub fn build_enr<E: EthSpec>(
243245 spec. custody_requirement
244246 } ;
245247
246- builder. add_value (
247- PEERDAS_CUSTODY_SUBNET_COUNT_ENR_KEY ,
248- & custody_subnet_count. as_ssz_bytes ( ) ,
249- ) ;
248+ let csc_bytes = custody_subnet_count. to_be_bytes ( ) ;
249+ builder. add_value ( PEERDAS_CUSTODY_SUBNET_COUNT_ENR_KEY , & csc_bytes. as_slice ( ) ) ;
250250
251251 builder
252252 . build ( enr_key)
@@ -309,3 +309,70 @@ pub fn save_enr_to_disk(dir: &Path, enr: &Enr, log: &slog::Logger) {
309309 }
310310 }
311311}
312+
313+ #[ cfg( test) ]
314+ mod test {
315+ use super :: * ;
316+ use crate :: config:: Config as NetworkConfig ;
317+ use types:: MainnetEthSpec ;
318+
319+ type E = MainnetEthSpec ;
320+
321+ #[ test]
322+ fn custody_subnet_count_default ( ) {
323+ let config = NetworkConfig {
324+ subscribe_all_data_column_subnets : false ,
325+ ..NetworkConfig :: default ( )
326+ } ;
327+ let spec = E :: default_spec ( ) ;
328+ let enr = build_enr_with_config ( config, & spec) . 0 ;
329+
330+ assert_eq ! (
331+ enr. custody_subnet_count:: <E >( & spec) ,
332+ spec. custody_requirement,
333+ ) ;
334+ }
335+
336+ #[ test]
337+ fn custody_subnet_count_all ( ) {
338+ let config = NetworkConfig {
339+ subscribe_all_data_column_subnets : true ,
340+ ..NetworkConfig :: default ( )
341+ } ;
342+ let spec = E :: default_spec ( ) ;
343+ let enr = build_enr_with_config ( config, & spec) . 0 ;
344+
345+ assert_eq ! (
346+ enr. custody_subnet_count:: <E >( & spec) ,
347+ spec. data_column_sidecar_subnet_count,
348+ ) ;
349+ }
350+
351+ #[ test]
352+ fn custody_subnet_count_fallback_default ( ) {
353+ let config = NetworkConfig :: default ( ) ;
354+ let spec = E :: default_spec ( ) ;
355+ let ( mut enr, enr_key) = build_enr_with_config ( config, & spec) ;
356+ let invalid_subnet_count = 999u64 ;
357+
358+ enr. insert (
359+ PEERDAS_CUSTODY_SUBNET_COUNT_ENR_KEY ,
360+ & invalid_subnet_count. to_be_bytes ( ) . as_slice ( ) ,
361+ & enr_key,
362+ )
363+ . unwrap ( ) ;
364+
365+ assert_eq ! (
366+ enr. custody_subnet_count:: <E >( & spec) ,
367+ spec. custody_requirement,
368+ ) ;
369+ }
370+
371+ fn build_enr_with_config ( config : NetworkConfig , spec : & ChainSpec ) -> ( Enr , CombinedKey ) {
372+ let keypair = libp2p:: identity:: secp256k1:: Keypair :: generate ( ) ;
373+ let enr_key = CombinedKey :: from_secp256k1 ( & keypair) ;
374+ let enr_fork_id = EnrForkId :: default ( ) ;
375+ let enr = build_enr :: < E > ( & enr_key, & config, & enr_fork_id, spec) . unwrap ( ) ;
376+ ( enr, enr_key)
377+ }
378+ }
0 commit comments