Skip to content

Commit

Permalink
Implement MetadataV3 (#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
zilm13 authored Aug 15, 2024
1 parent 7b467a3 commit 8d3307a
Show file tree
Hide file tree
Showing 20 changed files with 360 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public void shouldReturnNetworkIdentity() throws Exception {
final MetadataMessage metadataMessage =
spec.getGenesisSchemaDefinitions()
.getMetadataMessageSchema()
.create(seqnr, List.of(1, 11, 15), Collections.emptyList());
.create(seqnr, List.of(1, 11, 15), Collections.emptyList(), Optional.empty());

when(eth2P2PNetwork.getMetadata()).thenReturn(metadataMessage);

Expand All @@ -83,7 +83,32 @@ public void shouldReturnNetworkIdentityAltair() throws Exception {
final MetadataMessage metadataMessage =
spec.getGenesisSchemaDefinitions()
.getMetadataMessageSchema()
.create(seqnr, List.of(1, 11, 15), List.of(0, 1, 2, 3));
.create(seqnr, List.of(1, 11, 15), List.of(0, 1, 2, 3), Optional.empty());

when(eth2P2PNetwork.getMetadata()).thenReturn(metadataMessage);

final Response response = get();
assertThat(response.code()).isEqualTo(SC_OK);
final IdentityResponse identityResponse =
jsonProvider.jsonToObject(response.body().string(), IdentityResponse.class);
assertThat(identityResponse.data)
.isEqualTo(
new Identity(
node1.toBase58(),
enr,
List.of(address),
List.of(discoveryAddress),
new Metadata(metadataMessage)));
}

@Test
public void shouldReturnNetworkIdentityEip7594() throws Exception {
startRestAPIAtGenesis(SpecMilestone.EIP7594);

final MetadataMessage metadataMessage =
spec.getGenesisSchemaDefinitions()
.getMetadataMessageSchema()
.create(seqnr, List.of(1, 11, 15), List.of(0, 1, 2, 3), Optional.of(UInt64.valueOf(4)));

when(eth2P2PNetwork.getMetadata()).thenReturn(metadataMessage);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@
"pattern" : "^0x[a-fA-F0-9]{2,}$",
"description" : "Bitvector representing the node's persistent sync committee subnet subscriptions.",
"format" : "bytes"
},
"custody_subnet_count" : {
"type" : "string",
"description" : "PeerDAS custody subnet count.",
"example" : "1",
"format" : "uint64"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ private static SerializableTypeDefinition<MetadataMessage> createMetadataType(
.withDescription(
"Bitvector representing the node's persistent sync committee subnet subscriptions."),
MetadataMessage::getOptionalSyncnets)
.withOptionalField(
"custody_subnet_count",
UINT64_TYPE.withDescription("PeerDAS custody subnet count."),
MetadataMessage::getOptionalCustodySubnetCount)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.Objects;
import tech.pegasys.teku.spec.datastructures.networking.libp2p.rpc.metadata.MetadataMessage;
import tech.pegasys.teku.spec.datastructures.networking.libp2p.rpc.metadata.versions.altair.MetadataMessageAltair;
import tech.pegasys.teku.spec.datastructures.networking.libp2p.rpc.metadata.versions.eip7594.MetadataMessageEip7594;

@Schema(
description =
Expand Down Expand Up @@ -55,29 +56,50 @@ public class Metadata {
@JsonInclude(JsonInclude.Include.NON_NULL)
public final String syncCommitteeSubscriptions;

@JsonProperty("custody_subnet_count")
@Schema(
type = "string",
pattern = PATTERN_UINT64,
description = "Uint64 value representing the node's custody subnet count")
public final String custodySubnetCount;

@JsonCreator
public Metadata(
@JsonProperty("seq_number") final String sequenceNumber,
@JsonProperty("attnets") final String attestationSubnetSubscriptions,
@JsonProperty("syncnets") final String syncCommitteeSubscriptions) {
@JsonProperty("syncnets") final String syncCommitteeSubscriptions,
@JsonProperty("custody_subnet_count") final String custodySubnetCount) {
this.sequenceNumber = sequenceNumber;
this.attestationSubnetSubscriptions = attestationSubnetSubscriptions;
this.syncCommitteeSubscriptions = syncCommitteeSubscriptions;
this.custodySubnetCount = custodySubnetCount;
}

public Metadata(final MetadataMessage metadataMessage) {
this.sequenceNumber = metadataMessage.getSeqNumber().toString();
this.attestationSubnetSubscriptions =
metadataMessage.getAttnets().sszSerialize().toHexString().toLowerCase(Locale.ROOT);
if (metadataMessage instanceof MetadataMessageAltair) {

if (metadataMessage instanceof MetadataMessageEip7594) {
this.syncCommitteeSubscriptions =
((MetadataMessageEip7594) metadataMessage)
.getSyncnets()
.sszSerialize()
.toHexString()
.toLowerCase(Locale.ROOT);
this.custodySubnetCount =
((MetadataMessageEip7594) metadataMessage).getCustodySubnetCount().toString();
} else if (metadataMessage instanceof MetadataMessageAltair) {
this.syncCommitteeSubscriptions =
((MetadataMessageAltair) metadataMessage)
.getSyncnets()
.sszSerialize()
.toHexString()
.toLowerCase(Locale.ROOT);
this.custodySubnetCount = null;
} else {
this.syncCommitteeSubscriptions = null;
this.custodySubnetCount = null;
}
}

Expand All @@ -92,11 +114,16 @@ public boolean equals(final Object o) {
final Metadata metadata = (Metadata) o;
return Objects.equals(sequenceNumber, metadata.sequenceNumber)
&& Objects.equals(attestationSubnetSubscriptions, metadata.attestationSubnetSubscriptions)
&& Objects.equals(syncCommitteeSubscriptions, metadata.syncCommitteeSubscriptions);
&& Objects.equals(syncCommitteeSubscriptions, metadata.syncCommitteeSubscriptions)
&& Objects.equals(custodySubnetCount, metadata.custodySubnetCount);
}

@Override
public int hashCode() {
return Objects.hash(sequenceNumber, attestationSubnetSubscriptions, syncCommitteeSubscriptions);
return Objects.hash(
sequenceNumber,
attestationSubnetSubscriptions,
syncCommitteeSubscriptions,
custodySubnetCount);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,8 @@ default int getMaximumResponseChunks() {
default Optional<SszBitvector> getOptionalSyncnets() {
return Optional.empty();
}

default Optional<UInt64> getOptionalCustodySubnetCount() {
return Optional.empty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

package tech.pegasys.teku.spec.datastructures.networking.libp2p.rpc.metadata;

import java.util.Optional;
import tech.pegasys.teku.infrastructure.ssz.schema.SszContainerSchema;
import tech.pegasys.teku.infrastructure.ssz.tree.TreeNode;
import tech.pegasys.teku.infrastructure.unsigned.UInt64;
Expand All @@ -21,7 +22,11 @@ public interface MetadataMessageSchema<T extends MetadataMessage> extends SszCon
@Override
T createFromBackingNode(TreeNode node);

T create(UInt64 seqNumber, Iterable<Integer> attnets, final Iterable<Integer> syncnets);
T create(
UInt64 seqNumber,
Iterable<Integer> attnets,
final Iterable<Integer> syncnets,
final Optional<UInt64> custodySubnetCount);

T createDefault();
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

package tech.pegasys.teku.spec.datastructures.networking.libp2p.rpc.metadata.versions.altair;

import java.util.Optional;
import tech.pegasys.teku.infrastructure.ssz.collections.SszBitvector;
import tech.pegasys.teku.infrastructure.ssz.containers.ContainerSchema3;
import tech.pegasys.teku.infrastructure.ssz.primitive.SszUInt64;
Expand All @@ -39,7 +40,10 @@ public MetadataMessageSchemaAltair(final NetworkingSpecConfig networkingSpecConf

@Override
public MetadataMessageAltair create(
final UInt64 seqNumber, final Iterable<Integer> attnets, final Iterable<Integer> syncnets) {
final UInt64 seqNumber,
final Iterable<Integer> attnets,
final Iterable<Integer> syncnets,
final Optional<UInt64> custodySubnetCount) {
return new MetadataMessageAltair(
this, seqNumber, getAttnestSchema().ofBits(attnets), getSyncnetsSchema().ofBits(syncnets));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright Consensys Software Inc., 2022
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

package tech.pegasys.teku.spec.datastructures.networking.libp2p.rpc.metadata.versions.eip7594;

import java.util.Optional;
import tech.pegasys.teku.infrastructure.ssz.collections.SszBitvector;
import tech.pegasys.teku.infrastructure.ssz.containers.Container4;
import tech.pegasys.teku.infrastructure.ssz.primitive.SszUInt64;
import tech.pegasys.teku.infrastructure.ssz.tree.TreeNode;
import tech.pegasys.teku.infrastructure.unsigned.UInt64;
import tech.pegasys.teku.spec.datastructures.networking.libp2p.rpc.metadata.MetadataMessage;

public class MetadataMessageEip7594
extends Container4<MetadataMessageEip7594, SszUInt64, SszBitvector, SszBitvector, SszUInt64>
implements MetadataMessage {

MetadataMessageEip7594(final MetadataMessageSchemaEip7594 schema) {
super(schema);
}

MetadataMessageEip7594(final MetadataMessageSchemaEip7594 schema, final TreeNode backingNode) {
super(schema, backingNode);
}

MetadataMessageEip7594(
final MetadataMessageSchemaEip7594 schema,
final UInt64 seqNumber,
final SszBitvector attNets,
final SszBitvector syncNets,
final UInt64 custodySubnetCount) {
super(schema, SszUInt64.of(seqNumber), attNets, syncNets, SszUInt64.of(custodySubnetCount));
}

@Override
public UInt64 getSeqNumber() {
return getField0().get();
}

@Override
public SszBitvector getAttnets() {
return getField1();
}

public SszBitvector getSyncnets() {
return getField2();
}

public UInt64 getCustodySubnetCount() {
return getField3().get();
}

@Override
public Optional<SszBitvector> getOptionalSyncnets() {
return Optional.of(getSyncnets());
}

@Override
public Optional<UInt64> getOptionalCustodySubnetCount() {
return Optional.of(getCustodySubnetCount());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright Consensys Software Inc., 2022
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

package tech.pegasys.teku.spec.datastructures.networking.libp2p.rpc.metadata.versions.eip7594;

import java.util.Optional;
import tech.pegasys.teku.infrastructure.ssz.collections.SszBitvector;
import tech.pegasys.teku.infrastructure.ssz.containers.ContainerSchema4;
import tech.pegasys.teku.infrastructure.ssz.primitive.SszUInt64;
import tech.pegasys.teku.infrastructure.ssz.schema.SszPrimitiveSchemas;
import tech.pegasys.teku.infrastructure.ssz.schema.collections.SszBitvectorSchema;
import tech.pegasys.teku.infrastructure.ssz.tree.TreeNode;
import tech.pegasys.teku.infrastructure.unsigned.UInt64;
import tech.pegasys.teku.spec.config.NetworkingSpecConfig;
import tech.pegasys.teku.spec.constants.NetworkConstants;
import tech.pegasys.teku.spec.datastructures.networking.libp2p.rpc.metadata.MetadataMessageSchema;

public class MetadataMessageSchemaEip7594
extends ContainerSchema4<
MetadataMessageEip7594, SszUInt64, SszBitvector, SszBitvector, SszUInt64>
implements MetadataMessageSchema<MetadataMessageEip7594> {
public MetadataMessageSchemaEip7594(final NetworkingSpecConfig networkingSpecConfig) {
super(
"MetadataMessage",
namedSchema("seq_number", SszPrimitiveSchemas.UINT64_SCHEMA),
namedSchema(
"attnets", SszBitvectorSchema.create(networkingSpecConfig.getAttestationSubnetCount())),
namedSchema(
"syncnets", SszBitvectorSchema.create(NetworkConstants.SYNC_COMMITTEE_SUBNET_COUNT)),
namedSchema("custody_subnet_count", SszPrimitiveSchemas.UINT64_SCHEMA));
}

@Override
public MetadataMessageEip7594 create(
final UInt64 seqNumber,
final Iterable<Integer> attnets,
final Iterable<Integer> syncnets,
final Optional<UInt64> custodySubnetCount) {
return new MetadataMessageEip7594(
this,
seqNumber,
getAttnestSchema().ofBits(attnets),
getSyncnetsSchema().ofBits(syncnets),
custodySubnetCount.orElse(UInt64.ZERO));
}

@Override
public MetadataMessageEip7594 createDefault() {
return new MetadataMessageEip7594(this);
}

@Override
public MetadataMessageEip7594 createFromBackingNode(final TreeNode node) {
return new MetadataMessageEip7594(this, node);
}

private SszBitvectorSchema<SszBitvector> getAttnestSchema() {
return (SszBitvectorSchema<SszBitvector>) getFieldSchema1();
}

private SszBitvectorSchema<SszBitvector> getSyncnetsSchema() {
return (SszBitvectorSchema<SszBitvector>) getFieldSchema2();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

package tech.pegasys.teku.spec.datastructures.networking.libp2p.rpc.metadata.versions.phase0;

import java.util.Optional;
import tech.pegasys.teku.infrastructure.ssz.collections.SszBitvector;
import tech.pegasys.teku.infrastructure.ssz.containers.ContainerSchema2;
import tech.pegasys.teku.infrastructure.ssz.primitive.SszUInt64;
Expand Down Expand Up @@ -43,7 +44,10 @@ public MetadataMessagePhase0 createFromBackingNode(TreeNode node) {

@Override
public MetadataMessagePhase0 create(
final UInt64 seqNumber, final Iterable<Integer> attnets, final Iterable<Integer> syncnets) {
final UInt64 seqNumber,
final Iterable<Integer> attnets,
final Iterable<Integer> syncnets,
final Optional<UInt64> custodySubnetCount) {
return new MetadataMessagePhase0(this, seqNumber, getAttnestSchema().ofBits(attnets));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import tech.pegasys.teku.spec.datastructures.lightclient.LightClientHeaderSchema;
import tech.pegasys.teku.spec.datastructures.lightclient.LightClientUpdateResponseSchema;
import tech.pegasys.teku.spec.datastructures.lightclient.LightClientUpdateSchema;
import tech.pegasys.teku.spec.datastructures.networking.libp2p.rpc.metadata.MetadataMessageSchema;
import tech.pegasys.teku.spec.datastructures.networking.libp2p.rpc.metadata.versions.altair.MetadataMessageSchemaAltair;
import tech.pegasys.teku.spec.datastructures.operations.versions.altair.ContributionAndProofSchema;
import tech.pegasys.teku.spec.datastructures.operations.versions.altair.SignedContributionAndProofSchema;
Expand Down Expand Up @@ -147,7 +148,7 @@ public BeaconBlockBodyBuilder createBeaconBlockBodyBuilder() {
}

@Override
public MetadataMessageSchemaAltair getMetadataMessageSchema() {
public MetadataMessageSchema<?> getMetadataMessageSchema() {
return metadataMessageSchema;
}

Expand Down
Loading

0 comments on commit 8d3307a

Please sign in to comment.