Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
nalepae committed Jun 20, 2024
1 parent b2d0537 commit 7a5f2bf
Show file tree
Hide file tree
Showing 13 changed files with 506 additions and 150 deletions.
35 changes: 31 additions & 4 deletions beacon-chain/core/peerdas/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

cKzg4844 "github.com/ethereum/c-kzg-4844/bindings/go"
"github.com/ethereum/go-ethereum/p2p/enode"
"github.com/ethereum/go-ethereum/p2p/enr"
"github.com/holiman/uint256"
errors "github.com/pkg/errors"
"github.com/prysmaticlabs/prysm/v5/config/params"
Expand All @@ -18,13 +19,24 @@ import (
)

// Bytes per cell
const bytesPerCell = cKzg4844.FieldElementsPerCell * cKzg4844.BytesPerFieldElement
const (
CustodySubnetCountEnrKey = "csc"

bytesPerCell = cKzg4844.FieldElementsPerCell * cKzg4844.BytesPerFieldElement
)

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

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

var (
// Custom errors
errCustodySubnetCountTooLarge = errors.New("custody subnet count larger than data column sidecar subnet count")
errIndexTooLarge = errors.New("column index is larger than the specified columns count")
errMismatchLength = errors.New("mismatch in the length of the commitments and proofs")
errCustodySubnetCountTooLarge = errors.New("custody subnet count larger than data column sidecar subnet count")
errIndexTooLarge = errors.New("column index is larger than the specified columns count")
errMismatchLength = errors.New("mismatch in the length of the commitments and proofs")
errRecordNil = errors.New("record is nil")
errCannotLoadCustodySubnetCount = errors.New("cannot load the custody subnet count from peer")

// maxUint256 is the maximum value of a uint256.
maxUint256 = &uint256.Int{math.MaxUint64, math.MaxUint64, math.MaxUint64, math.MaxUint64}
Expand Down Expand Up @@ -350,3 +362,18 @@ func ExtendedSampleCount(samplesPerSlot, allowedFailures uint64) uint64 {

return sampleCount
}

func CustodyCountFromRecord(record *enr.Record) (uint64, error) {
// By default, we assume the peer custodies the minimum number of subnets.
if record == nil {
return 0, errRecordNil
}

// Load the `custody_subnet_count`
var csc CustodySubnetCount
if err := record.Load(&csc); err != nil {
return 0, errCannotLoadCustodySubnetCount
}

return uint64(csc), nil
}
5 changes: 5 additions & 0 deletions beacon-chain/core/peerdas/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package peerdas

import "github.com/sirupsen/logrus"

var log = logrus.WithField("prefix", "peerdas")
35 changes: 11 additions & 24 deletions beacon-chain/p2p/custody.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,42 +82,29 @@ loop:
// CustodyCountFromRemotePeer retrieves the custody count from a remote peer.
func (s *Service) CustodyCountFromRemotePeer(pid peer.ID) uint64 {
// By default, we assume the peer custodies the minimum number of subnets.
custodyCount := params.BeaconConfig().CustodyRequirement
custodyRequirement := params.BeaconConfig().CustodyRequirement

// Retrieve the ENR of the peer.
record, err := s.peers.ENR(pid)
if err != nil {
log.WithError(err).WithFields(logrus.Fields{
"peerID": pid,
"defaultValue": custodyCount,
"defaultValue": custodyRequirement,
}).Error("Failed to retrieve ENR for peer, defaulting to the default value")
return custodyCount
}

if record == nil {
// This is the case for inbound peers. So we don't log an error for this.
log.WithFields(logrus.Fields{
"peerID": pid,
"defaultValue": custodyCount,
}).Debug("No ENR found for peer, defaulting to the default value")
return custodyCount
return custodyRequirement
}

// Load the `custody_subnet_count`
var csc CustodySubnetCount
if err := record.Load(&csc); err != nil {
log.WithFields(logrus.Fields{
// Retrieve the custody subnets count from the ENR.
custodyCount, err := peerdas.CustodyCountFromRecord(record)
if err != nil {
log.WithError(err).WithFields(logrus.Fields{
"peerID": pid,
"defaultValue": custodyCount,
}).Warning("Cannot load the custody subnet count from peer, defaulting to the default value")
"defaultValue": custodyRequirement,
}).Error("Failed to retrieve custody count from ENR for peer, defaulting to the default value")

return custodyCount
return custodyRequirement
}

log.WithFields(logrus.Fields{
"peerID": pid,
"custodyCount": csc,
}).Debug("Custody count read from peer's ENR")

return uint64(csc)
return custodyCount
}
5 changes: 3 additions & 2 deletions beacon-chain/p2p/custody_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/prysmaticlabs/prysm/v5/beacon-chain/core/peerdas"
"github.com/prysmaticlabs/prysm/v5/beacon-chain/p2p/peers"
"github.com/prysmaticlabs/prysm/v5/beacon-chain/p2p/peers/scorers"
"github.com/prysmaticlabs/prysm/v5/config/params"
Expand All @@ -36,7 +37,7 @@ func createPeer(t *testing.T, privateKeyOffset int, custodyCount uint64) (*enr.R
require.NoError(t, err)

record := &enr.Record{}
record.Set(CustodySubnetCount(custodyCount))
record.Set(peerdas.CustodySubnetCount(custodyCount))
record.Set(enode.Secp256k1(privateKey.PublicKey))

return record, peerID, privateKey
Expand Down Expand Up @@ -102,7 +103,7 @@ func TestCustodyCountFromRemotePeer(t *testing.T) {
pid = "test-id"
)

csc := CustodySubnetCount(expected)
csc := peerdas.CustodySubnetCount(expected)

// Define a nil record
var nilRecord *enr.Record = nil
Expand Down
18 changes: 5 additions & 13 deletions beacon-chain/p2p/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/prysmaticlabs/go-bitfield"

"github.com/prysmaticlabs/prysm/v5/beacon-chain/cache"
"github.com/prysmaticlabs/prysm/v5/beacon-chain/core/peerdas"
"github.com/prysmaticlabs/prysm/v5/cmd/beacon-chain/flags"
"github.com/prysmaticlabs/prysm/v5/config/features"
"github.com/prysmaticlabs/prysm/v5/config/params"
Expand All @@ -43,22 +44,13 @@ const (
udp6
)

const (
quickProtocolEnrKey = "quic"
custodySubnetCountEnrKey = "csc"
)
const quickProtocolEnrKey = "quic"

type (
quicProtocol uint16
CustodySubnetCount uint64
)
type quicProtocol uint16

// quicProtocol is the "quic" key, which holds the QUIC port of the node.
func (quicProtocol) ENRKey() string { return quickProtocolEnrKey }

// https://github.com/ethereum/consensus-specs/blob/dev/specs/_features/eip7594/p2p-interface.md#the-discovery-domain-discv5
func (CustodySubnetCount) ENRKey() string { return custodySubnetCountEnrKey }

// RefreshPersistentSubnets checks that we are tracking our local persistent subnets for a variety of gossip topics.
// This routine checks for our attestation, sync committee and data column subnets and updates them if they have
// been rotated.
Expand Down Expand Up @@ -275,9 +267,9 @@ func (s *Service) createLocalNode(
}

if features.Get().EnablePeerDAS {
custodySubnetEntry := CustodySubnetCount(params.BeaconConfig().CustodyRequirement)
custodySubnetEntry := peerdas.CustodySubnetCount(params.BeaconConfig().CustodyRequirement)
if flags.Get().SubscribeToAllSubnets {
custodySubnetEntry = CustodySubnetCount(params.BeaconConfig().DataColumnSidecarSubnetCount)
custodySubnetEntry = peerdas.CustodySubnetCount(params.BeaconConfig().DataColumnSidecarSubnetCount)
}
localNode.Set(custodySubnetEntry)
}
Expand Down
3 changes: 2 additions & 1 deletion beacon-chain/p2p/discovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

mock "github.com/prysmaticlabs/prysm/v5/beacon-chain/blockchain/testing"
"github.com/prysmaticlabs/prysm/v5/beacon-chain/cache"
"github.com/prysmaticlabs/prysm/v5/beacon-chain/core/peerdas"
"github.com/prysmaticlabs/prysm/v5/beacon-chain/p2p/peers"
"github.com/prysmaticlabs/prysm/v5/beacon-chain/p2p/peers/peerdata"
"github.com/prysmaticlabs/prysm/v5/beacon-chain/p2p/peers/scorers"
Expand Down Expand Up @@ -237,7 +238,7 @@ func TestCreateLocalNode(t *testing.T) {

// Check custody_subnet_count config.
custodySubnetCount := new(uint64)
require.NoError(t, localNode.Node().Record().Load(enr.WithEntry(custodySubnetCountEnrKey, custodySubnetCount)))
require.NoError(t, localNode.Node().Record().Load(enr.WithEntry(peerdas.CustodySubnetCountEnrKey, custodySubnetCount)))
require.Equal(t, uint64(1), *custodySubnetCount)
})
}
Expand Down
Loading

0 comments on commit 7a5f2bf

Please sign in to comment.