Skip to content

Commit 2b6ec96

Browse files
authored
Add MetaData V3 support to node/identity API (#6827)
* Add metadata v3 support to `node/identity` api.
1 parent c33307d commit 2b6ec96

File tree

3 files changed

+52
-21
lines changed

3 files changed

+52
-21
lines changed

beacon_node/http_api/src/lib.rs

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ use eth2::types::{
5252
};
5353
use eth2::{CONSENSUS_VERSION_HEADER, CONTENT_TYPE_HEADER, SSZ_CONTENT_TYPE_HEADER};
5454
use health_metrics::observe::Observe;
55+
use lighthouse_network::rpc::methods::MetaData;
5556
use lighthouse_network::{types::SyncState, EnrExt, NetworkGlobals, PeerId, PubsubMessage};
5657
use lighthouse_version::version_with_platform;
5758
use logging::SSELoggingComponents;
@@ -82,6 +83,7 @@ use tokio_stream::{
8283
wrappers::{errors::BroadcastStreamRecvError, BroadcastStream},
8384
StreamExt,
8485
};
86+
use types::ChainSpec;
8587
use types::{
8688
fork_versioned_response::EmptyMetadata, Attestation, AttestationData, AttestationShufflingId,
8789
AttesterSlashing, BeaconStateError, CommitteeCache, ConfigAndPreset, Epoch, EthSpec, ForkName,
@@ -2898,36 +2900,24 @@ pub fn serve<T: BeaconChainTypes>(
28982900
.and(warp::path::end())
28992901
.and(task_spawner_filter.clone())
29002902
.and(network_globals.clone())
2903+
.and(chain_filter.clone())
29012904
.then(
29022905
|task_spawner: TaskSpawner<T::EthSpec>,
2903-
network_globals: Arc<NetworkGlobals<T::EthSpec>>| {
2906+
network_globals: Arc<NetworkGlobals<T::EthSpec>>,
2907+
chain: Arc<BeaconChain<T>>| {
29042908
task_spawner.blocking_json_task(Priority::P1, move || {
29052909
let enr = network_globals.local_enr();
29062910
let p2p_addresses = enr.multiaddr_p2p_tcp();
29072911
let discovery_addresses = enr.multiaddr_p2p_udp();
2908-
let meta_data = network_globals.local_metadata.read();
29092912
Ok(api_types::GenericResponse::from(api_types::IdentityData {
29102913
peer_id: network_globals.local_peer_id().to_base58(),
29112914
enr,
29122915
p2p_addresses,
29132916
discovery_addresses,
2914-
metadata: api_types::MetaData {
2915-
seq_number: *meta_data.seq_number(),
2916-
attnets: format!(
2917-
"0x{}",
2918-
hex::encode(meta_data.attnets().clone().into_bytes()),
2919-
),
2920-
syncnets: format!(
2921-
"0x{}",
2922-
hex::encode(
2923-
meta_data
2924-
.syncnets()
2925-
.cloned()
2926-
.unwrap_or_default()
2927-
.into_bytes()
2928-
)
2929-
),
2930-
},
2917+
metadata: from_meta_data::<T::EthSpec>(
2918+
&network_globals.local_metadata,
2919+
&chain.spec,
2920+
),
29312921
}))
29322922
})
29332923
},
@@ -4844,6 +4834,39 @@ pub fn serve<T: BeaconChainTypes>(
48444834
Ok(http_server)
48454835
}
48464836

4837+
fn from_meta_data<E: EthSpec>(
4838+
meta_data: &RwLock<MetaData<E>>,
4839+
spec: &ChainSpec,
4840+
) -> api_types::MetaData {
4841+
let meta_data = meta_data.read();
4842+
let format_hex = |bytes: &[u8]| format!("0x{}", hex::encode(bytes));
4843+
4844+
let seq_number = *meta_data.seq_number();
4845+
let attnets = format_hex(&meta_data.attnets().clone().into_bytes());
4846+
let syncnets = format_hex(
4847+
&meta_data
4848+
.syncnets()
4849+
.cloned()
4850+
.unwrap_or_default()
4851+
.into_bytes(),
4852+
);
4853+
4854+
if spec.is_peer_das_scheduled() {
4855+
api_types::MetaData::V3(api_types::MetaDataV3 {
4856+
seq_number,
4857+
attnets,
4858+
syncnets,
4859+
custody_group_count: meta_data.custody_group_count().cloned().unwrap_or_default(),
4860+
})
4861+
} else {
4862+
api_types::MetaData::V2(api_types::MetaDataV2 {
4863+
seq_number,
4864+
attnets,
4865+
syncnets,
4866+
})
4867+
}
4868+
}
4869+
48474870
/// Publish a message to the libp2p pubsub network.
48484871
fn publish_pubsub_message<E: EthSpec>(
48494872
network_tx: &UnboundedSender<NetworkMessage<E>>,

beacon_node/http_api/tests/tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2362,11 +2362,11 @@ impl ApiTester {
23622362
enr: self.local_enr.clone(),
23632363
p2p_addresses: self.local_enr.multiaddr_p2p_tcp(),
23642364
discovery_addresses: self.local_enr.multiaddr_p2p_udp(),
2365-
metadata: eth2::types::MetaData {
2365+
metadata: MetaData::V2(MetaDataV2 {
23662366
seq_number: 0,
23672367
attnets: "0x0000000000000000".to_string(),
23682368
syncnets: "0x00".to_string(),
2369-
},
2369+
}),
23702370
};
23712371

23722372
assert_eq!(result, expected);

common/eth2/src/types.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,12 +584,20 @@ pub struct IdentityData {
584584
pub metadata: MetaData,
585585
}
586586

587+
#[superstruct(
588+
variants(V2, V3),
589+
variant_attributes(derive(Clone, Debug, PartialEq, Serialize, Deserialize))
590+
)]
587591
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
592+
#[serde(untagged)]
588593
pub struct MetaData {
589594
#[serde(with = "serde_utils::quoted_u64")]
590595
pub seq_number: u64,
591596
pub attnets: String,
592597
pub syncnets: String,
598+
#[superstruct(only(V3))]
599+
#[serde(with = "serde_utils::quoted_u64")]
600+
pub custody_group_count: u64,
593601
}
594602

595603
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]

0 commit comments

Comments
 (0)