Skip to content

Commit

Permalink
get rid of next domain type
Browse files Browse the repository at this point in the history
  • Loading branch information
nkryuchkov committed Nov 2, 2024
1 parent dcb682f commit 00f96ff
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 100 deletions.
20 changes: 4 additions & 16 deletions network/discovery/dv5_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ import (
"github.com/ethereum/go-ethereum/p2p/enode"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/pkg/errors"
"go.uber.org/zap"

"github.com/ssvlabs/ssv/logging"
"github.com/ssvlabs/ssv/logging/fields"
"github.com/ssvlabs/ssv/network/commons"
"github.com/ssvlabs/ssv/network/peers"
"github.com/ssvlabs/ssv/network/records"
"github.com/ssvlabs/ssv/networkconfig"
"go.uber.org/zap"
)

var (
Expand Down Expand Up @@ -170,14 +171,8 @@ func (dvs *DiscV5Service) checkPeer(logger *zap.Logger, e PeerEvent) error {
if err != nil {
return errors.Wrap(err, "could not read domain type")
}
nodeNextDomainType, err := records.GetDomainTypeEntry(e.Node.Record(), records.KeyNextDomainType)
if err != nil && !errors.Is(err, records.ErrEntryNotFound) {
return errors.Wrap(err, "could not read domain type")
}
if dvs.networkConfig.DomainType != nodeDomainType &&
dvs.networkConfig.DomainType != nodeNextDomainType {
return fmt.Errorf("mismatched domain type: neither %x nor %x match %x",
nodeDomainType, nodeNextDomainType, dvs.networkConfig.DomainType)
if dvs.networkConfig.DomainType != nodeDomainType {
return fmt.Errorf("domain type %x doesn't match %x", nodeDomainType, dvs.networkConfig.DomainType)
}

// Get the peer's subnets, skipping if it has none.
Expand Down Expand Up @@ -345,12 +340,6 @@ func (dvs *DiscV5Service) PublishENR(logger *zap.Logger) {
logger.Error("could not set domain type", zap.Error(err))
return
}
// TODO: do we need to set it?
err = records.SetDomainTypeEntry(dvs.dv5Listener.LocalNode(), records.KeyNextDomainType, dvs.networkConfig.DomainType)
if err != nil {
logger.Error("could not set next domain type", zap.Error(err))
return
}

// Acquire publish lock to prevent parallel publishing.
// If there's an ongoing goroutine, it would now start publishing the record updated above,
Expand Down Expand Up @@ -414,7 +403,6 @@ func (dvs *DiscV5Service) createLocalNode(logger *zap.Logger, discOpts *Options,

// Satisfy decorations of forks supported by this node.
DecorateWithDomainType(records.KeyDomainType, dvs.networkConfig.DomainType),
DecorateWithDomainType(records.KeyNextDomainType, dvs.networkConfig.DomainType),
DecorateWithSubnets(opts.Subnets),
)
if err != nil {
Expand Down
49 changes: 8 additions & 41 deletions network/discovery/dv5_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (

var TestNetwork = networkconfig.NetworkConfig{
Beacon: beacon.NewNetwork(spectypes.BeaconTestNetwork),
DomainType: spectypes.DomainType{0x1, 0x2, 0x3, 0x5},
DomainType: spectypes.DomainType{0x1, 0x2, 0x3, 0x4},
}

func TestCheckPeer(t *testing.T) {
Expand All @@ -46,38 +46,10 @@ func TestCheckPeer(t *testing.T) {
expectedError: errors.New("could not read domain type: not found"),
},
{
name: "missing domain type but has next domain type",
domainType: nil,
nextDomainType: &spectypes.DomainType{0x1, 0x2, 0x3, 0x5},
subnets: mySubnets,
expectedError: errors.New("could not read domain type: not found"),
},
{
name: "domain type mismatch",
domainType: &spectypes.DomainType{0x1, 0x2, 0x3, 0x5},
nextDomainType: &spectypes.DomainType{0x1, 0x2, 0x3, 0x6},
subnets: mySubnets,
expectedError: errors.New("mismatched domain type: neither 01020305 nor 01020306 match 01020304"),
},
{
name: "domain type mismatch (missing next domain type)",
name: "domain type mismatch",
domainType: &spectypes.DomainType{0x1, 0x2, 0x3, 0x5},
subnets: mySubnets,
expectedError: errors.New("mismatched domain type: neither 01020305 nor 00000000 match 01020304"),
},
{
name: "only next domain type matches",
domainType: &spectypes.DomainType{0x1, 0x2, 0x3, 0x3},
nextDomainType: &spectypes.DomainType{0x1, 0x2, 0x3, 0x4},
subnets: mySubnets,
expectedError: nil,
},
{
name: "both domain types match",
domainType: &spectypes.DomainType{0x1, 0x2, 0x3, 0x4},
nextDomainType: &spectypes.DomainType{0x1, 0x2, 0x3, 0x4},
subnets: mySubnets,
expectedError: nil,
expectedError: errors.New("domain type 01020305 doesn't match 01020304"),
},
{
name: "missing subnets",
Expand Down Expand Up @@ -131,10 +103,6 @@ func TestCheckPeer(t *testing.T) {
err := records.SetDomainTypeEntry(localNode, records.KeyDomainType, *test.domainType)
require.NoError(t, err)
}
if test.nextDomainType != nil {
err := records.SetDomainTypeEntry(localNode, records.KeyNextDomainType, *test.nextDomainType)
require.NoError(t, err)
}
if test.subnets != nil {
err := records.SetSubnetsEntry(localNode, test.subnets)
require.NoError(t, err)
Expand Down Expand Up @@ -170,12 +138,11 @@ func TestCheckPeer(t *testing.T) {
}

type checkPeerTest struct {
name string
domainType *spectypes.DomainType
nextDomainType *spectypes.DomainType
subnets []byte
localNode *enode.LocalNode
expectedError error
name string
domainType *spectypes.DomainType
subnets []byte
localNode *enode.LocalNode
expectedError error
}

func mockSubnets(active ...int) []byte {
Expand Down
23 changes: 3 additions & 20 deletions network/discovery/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,6 @@ func checkLocalNodeDomainTypeAlignment(t *testing.T, localNode *enode.LocalNode,
err := localNode.Node().Record().Load(&domainEntry)
require.NoError(t, err)
require.Equal(t, netConfig.DomainType, domainEntry.DomainType)

// Check next domain entry
nextDomainEntry := records.DomainTypeEntry{
Key: records.KeyNextDomainType,
DomainType: spectypes.DomainType{},
}
err = localNode.Node().Record().Load(&nextDomainEntry)
require.NoError(t, err)
require.Equal(t, netConfig.DomainType, nextDomainEntry.DomainType)
}

func TestDiscV5Service_PublishENR(t *testing.T) {
Expand Down Expand Up @@ -250,21 +241,13 @@ func TestDiscV5Service_checkPeer(t *testing.T) {
err = dvs.checkPeer(testLogger, ToPeerEvent(NodeWithoutDomain(t)))
require.ErrorContains(t, err, "could not read domain type: not found")

// No next domain. No error since it's not enforced
err = dvs.checkPeer(testLogger, ToPeerEvent(NodeWithoutNextDomain(t)))
require.NoError(t, err)

// Matching main domain
err = dvs.checkPeer(testLogger, ToPeerEvent(NodeWithCustomDomains(t, testNetConfig.DomainType, spectypes.DomainType{})))
require.NoError(t, err)

// Matching next domain
err = dvs.checkPeer(testLogger, ToPeerEvent(NodeWithCustomDomains(t, spectypes.DomainType{}, testNetConfig.DomainType)))
err = dvs.checkPeer(testLogger, ToPeerEvent(NodeWithCustomDomain(t, testNetConfig.DomainType)))
require.NoError(t, err)

// Mismatching domains
err = dvs.checkPeer(testLogger, ToPeerEvent(NodeWithCustomDomains(t, spectypes.DomainType{}, spectypes.DomainType{})))
require.ErrorContains(t, err, "mismatched domain type: neither 00000000 nor 00000000 match 00000302")
err = dvs.checkPeer(testLogger, ToPeerEvent(NodeWithCustomDomain(t, spectypes.DomainType{})))
require.ErrorContains(t, err, "domain type 00000000 doesn't match 00000302")

// No subnets
err = dvs.checkPeer(testLogger, ToPeerEvent(NodeWithoutSubnets(t)))
Expand Down
35 changes: 14 additions & 21 deletions network/discovery/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func testingDiscovery(t *testing.T) *DiscV5Service {

// Testing node
func NewTestingNode(t *testing.T) *enode.Node {
return CustomNode(t, true, testNetConfig.DomainType, true, testNetConfig.DomainType, true, mockSubnets(1))
return CustomNode(t, true, testNetConfig.DomainType, true, mockSubnets(1))
}

func NewTestingNodes(t *testing.T, count int) []*enode.Node {
Expand All @@ -102,33 +102,32 @@ func NewTestingNodes(t *testing.T, count int) []*enode.Node {
}

func NodeWithoutDomain(t *testing.T) *enode.Node {
return CustomNode(t, false, spectypes.DomainType{}, true, testNetConfig.DomainType, true, mockSubnets(1))
}

func NodeWithoutNextDomain(t *testing.T) *enode.Node {
return CustomNode(t, true, testNetConfig.DomainType, false, spectypes.DomainType{}, true, mockSubnets(1))
return CustomNode(t, false, spectypes.DomainType{}, true, mockSubnets(1))
}

func NodeWithoutSubnets(t *testing.T) *enode.Node {
return CustomNode(t, true, testNetConfig.DomainType, true, testNetConfig.DomainType, false, nil)
return CustomNode(t, true, testNetConfig.DomainType, false, nil)
}

func NodeWithCustomDomains(t *testing.T, domainType spectypes.DomainType, nextDomainType spectypes.DomainType) *enode.Node {
return CustomNode(t, true, domainType, true, nextDomainType, true, mockSubnets(1))
func NodeWithCustomDomain(t *testing.T, domainType spectypes.DomainType) *enode.Node {
return CustomNode(t, true, domainType, true, mockSubnets(1))
}

func NodeWithZeroSubnets(t *testing.T) *enode.Node {
return CustomNode(t, true, testNetConfig.DomainType, true, testNetConfig.DomainType, true, zeroSubnets)
return CustomNode(t, true, testNetConfig.DomainType, true, zeroSubnets)
}

func NodeWithCustomSubnets(t *testing.T, subnets []byte) *enode.Node {
return CustomNode(t, true, testNetConfig.DomainType, true, testNetConfig.DomainType, true, subnets)
return CustomNode(t, true, testNetConfig.DomainType, true, subnets)
}

func CustomNode(t *testing.T,
setDomainType bool, domainType spectypes.DomainType,
setNextDomainType bool, nextDomainType spectypes.DomainType,
setSubnets bool, subnets []byte) *enode.Node {
func CustomNode(
t *testing.T,
setDomainType bool,
domainType spectypes.DomainType,
setSubnets bool,
subnets []byte,
) *enode.Node {

// Generate key
nodeKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
Expand All @@ -152,12 +151,6 @@ func CustomNode(t *testing.T,
DomainType: domainType,
})
}
if setNextDomainType {
record.Set(records.DomainTypeEntry{
Key: records.KeyNextDomainType,
DomainType: nextDomainType,
})
}
if setSubnets {
subnetsVec := bitfield.NewBitvector128()
for i, subnet := range subnets {
Expand Down
3 changes: 1 addition & 2 deletions network/records/entries.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ import (
type ENRKey string

const (
KeyDomainType = "domaintype"
KeyNextDomainType = "next_domaintype"
KeyDomainType = "domaintype"
)

var ErrEntryNotFound = errors.New("not found")
Expand Down

0 comments on commit 00f96ff

Please sign in to comment.