Skip to content

Commit

Permalink
Change Custody Count to Uint8 (#14386)
Browse files Browse the repository at this point in the history
* Add Changes for Uint8 Csc

* Fix Build

* Fix Build for Sync

* Fix Discovery Test
  • Loading branch information
nisdas authored and nalepae committed Aug 28, 2024
1 parent ef86a0e commit bd7ec3f
Show file tree
Hide file tree
Showing 15 changed files with 90 additions and 67 deletions.
2 changes: 1 addition & 1 deletion beacon-chain/core/peerdas/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const (
)

// https://github.com/ethereum/consensus-specs/blob/dev/specs/_features/eip7594/p2p-interface.md#the-discovery-domain-discv5
type Csc uint64
type Csc uint8

func (Csc) ENRKey() string { return CustodySubnetCountEnrKey }

Expand Down
2 changes: 1 addition & 1 deletion beacon-chain/p2p/custody.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (s *Service) CustodyCountFromRemotePeer(pid peer.ID) uint64 {
if metadata != nil {
custodyCount := metadata.CustodySubnetCount()
if custodyCount > 0 {
return custodyCount
return uint64(custodyCount)
}
}

Expand Down
4 changes: 2 additions & 2 deletions beacon-chain/p2p/custody_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,12 @@ func TestCustodyCountFromRemotePeer(t *testing.T) {

// Define a metadata with zero custody.
zeroMetadata := wrapper.WrappedMetadataV2(&pb.MetaDataV2{
CustodySubnetCount: 0,
CustodySubnetCount: []byte{0},
})

// Define a nominal metadata.
nominalMetadata := wrapper.WrappedMetadataV2(&pb.MetaDataV2{
CustodySubnetCount: expectedMetadata,
CustodySubnetCount: []byte{uint8(expectedMetadata)},
})

testCases := []struct {
Expand Down
2 changes: 1 addition & 1 deletion beacon-chain/p2p/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func (s *Service) RefreshPersistentSubnets() {
// Get the custody subnet count in our metadata.
inMetadataCustodySubnetCount := s.Metadata().CustodySubnetCount()

isCustodySubnetCountUpToDate := (custodySubnetCount == inRecordCustodySubnetCount && custodySubnetCount == inMetadataCustodySubnetCount)
isCustodySubnetCountUpToDate := custodySubnetCount == inRecordCustodySubnetCount && custodySubnetCount == uint64(inMetadataCustodySubnetCount)

if isBitVUpToDate && isBitSUpToDate && isCustodySubnetCountUpToDate {
// Nothing to do, return early.
Expand Down
6 changes: 3 additions & 3 deletions beacon-chain/p2p/discovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ type check struct {
metadataSequenceNumber uint64
attestationSubnets []uint64
syncSubnets []uint64
custodySubnetCount *uint64
custodySubnetCount *uint8
}

func checkPingCountCacheMetadataRecord(
Expand Down Expand Up @@ -542,7 +542,7 @@ func checkPingCountCacheMetadataRecord(

if expected.custodySubnetCount != nil {
// Check custody subnet count in ENR.
var actualCustodySubnetCount uint64
var actualCustodySubnetCount uint8
err := service.dv5Listener.LocalNode().Node().Record().Load(enr.WithEntry(peerdas.CustodySubnetCountEnrKey, &actualCustodySubnetCount))
require.NoError(t, err)
require.Equal(t, *expected.custodySubnetCount, actualCustodySubnetCount)
Expand All @@ -565,7 +565,7 @@ func TestRefreshPersistentSubnets(t *testing.T) {
eip7594ForkEpoch = 10
)

custodySubnetCount := params.BeaconConfig().CustodyRequirement
custodySubnetCount := uint8(params.BeaconConfig().CustodyRequirement)

// Set up epochs.
defaultCfg := params.BeaconConfig()
Expand Down
3 changes: 2 additions & 1 deletion beacon-chain/p2p/subnets.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,12 +239,13 @@ func (s *Service) updateSubnetRecordWithMetadataV3(
localNode.Set(custodySubnetCountEntry)

newSeqNumber := s.metaData.SequenceNumber() + 1
cscBytes := []byte{uint8(custodySubnetCount)}

s.metaData = wrapper.WrappedMetadataV2(&pb.MetaDataV2{
SeqNumber: newSeqNumber,
Attnets: bitVAtt,
Syncnets: bitVSync,
CustodySubnetCount: custodySubnetCount,
CustodySubnetCount: cscBytes,
})
}

Expand Down
4 changes: 2 additions & 2 deletions beacon-chain/sync/rpc_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,15 @@ func (s *Service) metaDataHandler(_ context.Context, _ interface{}, stream libp2
Attnets: metadata.AttnetsBitfield(),
SeqNumber: metadata.SequenceNumber(),
Syncnets: bitfield.Bitvector4{byte(0x00)},
CustodySubnetCount: 0,
CustodySubnetCount: []byte{0},
})
case version.Altair:
metadata = wrapper.WrappedMetadataV2(
&pb.MetaDataV2{
Attnets: metadata.AttnetsBitfield(),
SeqNumber: metadata.SequenceNumber(),
Syncnets: metadata.SyncnetsBitfield(),
CustodySubnetCount: 0,
CustodySubnetCount: []byte{0},
})
}
}
Expand Down
12 changes: 6 additions & 6 deletions beacon-chain/sync/rpc_metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func TestMetadataRPCHandler_SendMetadataRequest(t *testing.T) {
SeqNumber: seqNumber,
Attnets: attnets,
Syncnets: syncnets,
CustodySubnetCount: custodySubnetCount,
CustodySubnetCount: []byte{custodySubnetCount},
}),
expected: wrapper.WrappedMetadataV0(&pb.MetaDataV0{
SeqNumber: seqNumber,
Expand Down Expand Up @@ -200,7 +200,7 @@ func TestMetadataRPCHandler_SendMetadataRequest(t *testing.T) {
SeqNumber: seqNumber,
Attnets: attnets,
Syncnets: syncnets,
CustodySubnetCount: custodySubnetCount,
CustodySubnetCount: []byte{custodySubnetCount},
}),
expected: wrapper.WrappedMetadataV1(&pb.MetaDataV1{
SeqNumber: seqNumber,
Expand All @@ -221,7 +221,7 @@ func TestMetadataRPCHandler_SendMetadataRequest(t *testing.T) {
SeqNumber: seqNumber,
Attnets: attnets,
Syncnets: bitfield.Bitvector4{byte(0x00)},
CustodySubnetCount: 0,
CustodySubnetCount: []byte{0},
}),
},
{
Expand All @@ -238,7 +238,7 @@ func TestMetadataRPCHandler_SendMetadataRequest(t *testing.T) {
SeqNumber: seqNumber,
Attnets: attnets,
Syncnets: syncnets,
CustodySubnetCount: 0,
CustodySubnetCount: []byte{0},
}),
},
{
Expand All @@ -250,13 +250,13 @@ func TestMetadataRPCHandler_SendMetadataRequest(t *testing.T) {
SeqNumber: seqNumber,
Attnets: attnets,
Syncnets: syncnets,
CustodySubnetCount: custodySubnetCount,
CustodySubnetCount: []byte{custodySubnetCount},
}),
expected: wrapper.WrappedMetadataV2(&pb.MetaDataV2{
SeqNumber: seqNumber,
Attnets: attnets,
Syncnets: syncnets,
CustodySubnetCount: custodySubnetCount,
CustodySubnetCount: []byte{custodySubnetCount},
}),
},
}
Expand Down
1 change: 1 addition & 0 deletions consensus-types/wrapper/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ go_library(
importpath = "github.com/prysmaticlabs/prysm/v5/consensus-types/wrapper",
visibility = ["//visibility:public"],
deps = [
"//encoding/bytesutil:go_default_library",
"//proto/prysm/v1alpha1:go_default_library",
"//proto/prysm/v1alpha1/metadata:go_default_library",
"//runtime/version:go_default_library",
Expand Down
9 changes: 5 additions & 4 deletions consensus-types/wrapper/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package wrapper

import (
"github.com/prysmaticlabs/go-bitfield"
"github.com/prysmaticlabs/prysm/v5/encoding/bytesutil"
pb "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1"
"github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1/metadata"
"github.com/prysmaticlabs/prysm/v5/runtime/version"
Expand Down Expand Up @@ -37,7 +38,7 @@ func (m MetadataV0) SyncnetsBitfield() bitfield.Bitvector4 {
}

// CustodySubnetCount returns custody subnet count from the metadata.
func (m MetadataV0) CustodySubnetCount() uint64 {
func (m MetadataV0) CustodySubnetCount() uint8 {
return 0
}

Expand Down Expand Up @@ -131,7 +132,7 @@ func (m MetadataV1) SyncnetsBitfield() bitfield.Bitvector4 {
}

// CustodySubnetCount returns custody subnet count from the metadata.
func (m MetadataV1) CustodySubnetCount() uint64 {
func (m MetadataV1) CustodySubnetCount() uint8 {
return 0
}

Expand Down Expand Up @@ -225,8 +226,8 @@ func (m MetadataV2) SyncnetsBitfield() bitfield.Bitvector4 {
}

// CustodySubnetCount returns custody subnet count from the metadata.
func (m MetadataV2) CustodySubnetCount() uint64 {
return m.md.CustodySubnetCount
func (m MetadataV2) CustodySubnetCount() uint8 {
return bytesutil.FromBytes1(m.md.CustodySubnetCount)
}

// InnerObject returns the underlying metadata protobuf structure.
Expand Down
8 changes: 8 additions & 0 deletions encoding/bytesutil/integers.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ func Bytes32(x uint64) []byte {
return bytes
}

// FromBytes1 returns an integer from a byte slice with a size of 1.
func FromBytes1(x []byte) uint8 {
if len(x) < 1 {
return 0
}
return x[0]
}

// FromBytes2 returns an integer which is stored in the little-endian format(2, 'little')
// from a byte array.
func FromBytes2(x []byte) uint16 {
Expand Down
2 changes: 1 addition & 1 deletion proto/prysm/v1alpha1/metadata/metadata_interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type Metadata interface {
SequenceNumber() uint64
AttnetsBitfield() bitfield.Bitvector64
SyncnetsBitfield() bitfield.Bitvector4
CustodySubnetCount() uint64
CustodySubnetCount() uint8
InnerObject() interface{}
IsNil() bool
Copy() Metadata
Expand Down
23 changes: 17 additions & 6 deletions proto/prysm/v1alpha1/non-core.ssz.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

77 changes: 39 additions & 38 deletions proto/prysm/v1alpha1/p2p_messages.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit bd7ec3f

Please sign in to comment.