Skip to content

Commit

Permalink
Prioritize eth/68 by default (#7463)
Browse files Browse the repository at this point in the history
Set `DefaultConfig.ProtocolVersion` to [68, 67, 66] instead of [67, 68].
See ethereum/hive#776
  • Loading branch information
yperbasis authored May 8, 2023
1 parent d9abfd8 commit cc11462
Show file tree
Hide file tree
Showing 12 changed files with 146 additions and 147 deletions.
10 changes: 5 additions & 5 deletions cmd/observer/observer/handshake.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"time"

libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/direct"

"github.com/ledgerwatch/erigon/common"
"github.com/ledgerwatch/erigon/core/forkid"
Expand Down Expand Up @@ -239,11 +240,10 @@ func makeOurHelloMessage(myPrivateKey *ecdsa.PrivateKey) HelloMessage {
clientID := common.MakeName("observer", version)

caps := []p2p.Cap{
{Name: eth.ProtocolName, Version: 63},
{Name: eth.ProtocolName, Version: 64},
{Name: eth.ProtocolName, Version: 65},
{Name: eth.ProtocolName, Version: eth.ETH66},
{Name: eth.ProtocolName, Version: eth.ETH67},
{Name: eth.ProtocolName, Version: direct.ETH65},
{Name: eth.ProtocolName, Version: direct.ETH66},
{Name: eth.ProtocolName, Version: direct.ETH67},
{Name: eth.ProtocolName, Version: direct.ETH68},
}

return HelloMessage{
Expand Down
10 changes: 6 additions & 4 deletions cmd/observer/observer/handshake_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import (
"context"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/ledgerwatch/erigon-lib/direct"

"github.com/ledgerwatch/erigon/crypto"
"github.com/ledgerwatch/erigon/eth/protocols/eth"
"github.com/ledgerwatch/erigon/p2p/enode"
"github.com/ledgerwatch/erigon/params"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestHandshake(t *testing.T) {
Expand All @@ -31,6 +33,6 @@ func TestHandshake(t *testing.T) {
assert.Contains(t, hello.ClientID, "erigon")

require.NotNil(t, status)
assert.Equal(t, uint32(eth.ETH66), status.ProtocolVersion)
assert.Equal(t, uint32(direct.ETH66), status.ProtocolVersion)
assert.Equal(t, uint64(1), status.NetworkID)
}
10 changes: 6 additions & 4 deletions cmd/sentry/sentry/eth_handshake_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,20 @@ import (
"testing"

"github.com/holiman/uint256"
"github.com/stretchr/testify/assert"

libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/direct"
"github.com/ledgerwatch/erigon-lib/gointerfaces"
proto_sentry "github.com/ledgerwatch/erigon-lib/gointerfaces/sentry"
"github.com/stretchr/testify/assert"

"github.com/ledgerwatch/erigon/core/forkid"
"github.com/ledgerwatch/erigon/eth/protocols/eth"
"github.com/ledgerwatch/erigon/params"
)

func TestCheckPeerStatusCompatibility(t *testing.T) {
var version uint = eth.ETH66
var version uint = direct.ETH66
networkID := params.MainnetChainConfig.ChainID.Uint64()
goodReply := eth.StatusPacket{
ProtocolVersion: uint32(version),
Expand Down Expand Up @@ -52,14 +54,14 @@ func TestCheckPeerStatusCompatibility(t *testing.T) {
})
t.Run("version mismatch min", func(t *testing.T) {
reply := goodReply
reply.ProtocolVersion = eth.ETH66 - 1
reply.ProtocolVersion = direct.ETH66 - 1
err := checkPeerStatusCompatibility(&reply, &status, version, version)
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "version is less")
})
t.Run("version mismatch max", func(t *testing.T) {
reply := goodReply
reply.ProtocolVersion = eth.ETH66 + 1
reply.ProtocolVersion = direct.ETH66 + 1
err := checkPeerStatusCompatibility(&reply, &status, version, version)
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "version is more")
Expand Down
146 changes: 70 additions & 76 deletions cmd/sentry/sentry/sentry_grpc_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,20 @@ import (
"syscall"
"time"

"github.com/ledgerwatch/log/v3"
"google.golang.org/grpc"
"google.golang.org/grpc/health"
"google.golang.org/grpc/health/grpc_health_v1"
"google.golang.org/protobuf/types/known/emptypb"

libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/common/datadir"
"github.com/ledgerwatch/erigon-lib/common/dir"
"github.com/ledgerwatch/erigon-lib/direct"
"github.com/ledgerwatch/erigon-lib/gointerfaces"
"github.com/ledgerwatch/erigon-lib/gointerfaces/grpcutil"
proto_sentry "github.com/ledgerwatch/erigon-lib/gointerfaces/sentry"
proto_types "github.com/ledgerwatch/erigon-lib/gointerfaces/types"
"github.com/ledgerwatch/log/v3"
"google.golang.org/grpc"
"google.golang.org/grpc/health"
"google.golang.org/grpc/health/grpc_health_v1"
"google.golang.org/protobuf/types/known/emptypb"

"github.com/ledgerwatch/erigon/cmd/utils"
"github.com/ledgerwatch/erigon/common/debug"
Expand Down Expand Up @@ -414,7 +416,7 @@ func runPeer(
}
send(eth.ToProto[protocol][msg.Code], peerID, b)
case eth.GetNodeDataMsg:
if protocol >= eth.ETH67 {
if protocol >= direct.ETH67 {
msg.Discard()
return fmt.Errorf("unexpected GetNodeDataMsg from %s in eth/%d", peerID, protocol)
}
Expand Down Expand Up @@ -558,75 +560,67 @@ func NewGrpcServer(ctx context.Context, dialCandidates func() enode.Iterator, re
logger: logger,
}

protocols := []uint{protocol}
if protocol == eth.ETH67 {
protocols = append(protocols, eth.ETH66)
var disc enode.Iterator
if dialCandidates != nil {
disc = dialCandidates()
}

for _, p := range protocols {
protocol := p
var disc enode.Iterator
if dialCandidates != nil {
disc = dialCandidates()
}
ss.Protocols = append(ss.Protocols, p2p.Protocol{
Name: eth.ProtocolName,
Version: protocol,
Length: 17,
DialCandidates: disc,
Run: func(peer *p2p.Peer, rw p2p.MsgReadWriter) error {
peerID := peer.Pubkey()
printablePeerID := hex.EncodeToString(peerID[:])[:20]
if ss.getPeer(peerID) != nil {
logger.Trace("[p2p] peer already has connection", "peerId", printablePeerID)
return nil
}
logger.Trace("[p2p] start with peer", "peerId", printablePeerID)

peerInfo := NewPeerInfo(peer, rw)
peerInfo.protocol = protocol
defer peerInfo.Close()

defer ss.GoodPeers.Delete(peerID)
err := handShake(ctx, ss.GetStatus(), peerID, rw, protocol, protocol, func(bestHash libcommon.Hash) error {
ss.GoodPeers.Store(peerID, peerInfo)
ss.sendNewPeerToClients(gointerfaces.ConvertHashToH512(peerID))
return ss.startSync(ctx, bestHash, peerID)
})
if err != nil {
if errors.Is(err, NetworkIdMissmatchErr) || errors.Is(err, io.EOF) || errors.Is(err, p2p.ErrShuttingDown) {
logger.Trace("[p2p] Handshake failure", "peer", printablePeerID, "err", err)
} else {
logger.Debug("[p2p] Handshake failure", "peer", printablePeerID, "err", err)
}
return fmt.Errorf("[p2p]handshake to peer %s: %w", printablePeerID, err)
}
logger.Trace("[p2p] Received status message OK", "peerId", printablePeerID, "name", peer.Name())

err = runPeer(
ctx,
peerID,
protocol,
rw,
peerInfo,
ss.send,
ss.hasSubscribers,
logger,
) // runPeer never returns a nil error
logger.Trace("[p2p] error while running peer", "peerId", printablePeerID, "err", err)
ss.sendGonePeerToClients(gointerfaces.ConvertHashToH512(peerID))
ss.Protocols = append(ss.Protocols, p2p.Protocol{
Name: eth.ProtocolName,
Version: protocol,
Length: 17,
DialCandidates: disc,
Run: func(peer *p2p.Peer, rw p2p.MsgReadWriter) error {
peerID := peer.Pubkey()
printablePeerID := hex.EncodeToString(peerID[:])[:20]
if ss.getPeer(peerID) != nil {
logger.Trace("[p2p] peer already has connection", "peerId", printablePeerID)
return nil
},
NodeInfo: func() interface{} {
return readNodeInfo()
},
PeerInfo: func(peerID [64]byte) interface{} {
// TODO: remember handshake reply per peer ID and return eth-related Status info (see ethPeerInfo in geth)
return nil
},
//Attributes: []enr.Entry{eth.CurrentENREntry(chainConfig, genesisHash, headHeight)},
})
}
}
logger.Trace("[p2p] start with peer", "peerId", printablePeerID)

peerInfo := NewPeerInfo(peer, rw)
peerInfo.protocol = protocol
defer peerInfo.Close()

defer ss.GoodPeers.Delete(peerID)
err := handShake(ctx, ss.GetStatus(), peerID, rw, protocol, protocol, func(bestHash libcommon.Hash) error {
ss.GoodPeers.Store(peerID, peerInfo)
ss.sendNewPeerToClients(gointerfaces.ConvertHashToH512(peerID))
return ss.startSync(ctx, bestHash, peerID)
})
if err != nil {
if errors.Is(err, NetworkIdMissmatchErr) || errors.Is(err, io.EOF) || errors.Is(err, p2p.ErrShuttingDown) {
logger.Trace("[p2p] Handshake failure", "peer", printablePeerID, "err", err)
} else {
logger.Debug("[p2p] Handshake failure", "peer", printablePeerID, "err", err)
}
return fmt.Errorf("[p2p]handshake to peer %s: %w", printablePeerID, err)
}
logger.Trace("[p2p] Received status message OK", "peerId", printablePeerID, "name", peer.Name())

err = runPeer(
ctx,
peerID,
protocol,
rw,
peerInfo,
ss.send,
ss.hasSubscribers,
logger,
) // runPeer never returns a nil error
logger.Trace("[p2p] error while running peer", "peerId", printablePeerID, "err", err)
ss.sendGonePeerToClients(gointerfaces.ConvertHashToH512(peerID))
return nil
},
NodeInfo: func() interface{} {
return readNodeInfo()
},
PeerInfo: func(peerID [64]byte) interface{} {
// TODO: remember handshake reply per peer ID and return eth-related Status info (see ethPeerInfo in geth)
return nil
},
//Attributes: []enr.Entry{eth.CurrentENREntry(chainConfig, genesisHash, headHeight)},
})

return ss
}
Expand Down Expand Up @@ -934,11 +928,11 @@ func (ss *GrpcServer) SendMessageToAll(ctx context.Context, req *proto_sentry.Ou
func (ss *GrpcServer) HandShake(context.Context, *emptypb.Empty) (*proto_sentry.HandShakeReply, error) {
reply := &proto_sentry.HandShakeReply{}
switch ss.Protocols[0].Version {
case eth.ETH66:
case direct.ETH66:
reply.Protocol = proto_sentry.Protocol_ETH66
case eth.ETH67:
case direct.ETH67:
reply.Protocol = proto_sentry.Protocol_ETH67
case eth.ETH68:
case direct.ETH68:
reply.Protocol = proto_sentry.Protocol_ETH68
}
return reply, nil
Expand Down
11 changes: 6 additions & 5 deletions cmd/sentry/sentry/sentry_grpc_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,20 @@ import (
"time"

"github.com/holiman/uint256"
"github.com/stretchr/testify/require"

"github.com/ledgerwatch/erigon-lib/chain"
libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/direct"
"github.com/ledgerwatch/erigon-lib/gointerfaces"
proto_sentry "github.com/ledgerwatch/erigon-lib/gointerfaces/sentry"
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon-lib/kv/memdb"
"github.com/ledgerwatch/erigon/core"
"github.com/ledgerwatch/erigon/core/types"
"github.com/stretchr/testify/require"

"github.com/ledgerwatch/erigon/core"
"github.com/ledgerwatch/erigon/core/forkid"
"github.com/ledgerwatch/erigon/core/rawdb"
"github.com/ledgerwatch/erigon/eth/protocols/eth"
"github.com/ledgerwatch/erigon/core/types"
"github.com/ledgerwatch/erigon/p2p"
)

Expand Down Expand Up @@ -55,7 +56,7 @@ func testSentryServer(db kv.Getter, genesis *types.Genesis, genesisHash libcommo

// Tests that peers are correctly accepted (or rejected) based on the advertised
// fork IDs in the protocol handshake.
func TestForkIDSplit66(t *testing.T) { testForkIDSplit(t, eth.ETH66) }
func TestForkIDSplit66(t *testing.T) { testForkIDSplit(t, direct.ETH66) }

func testForkIDSplit(t *testing.T, protocol uint) {
var (
Expand Down
27 changes: 14 additions & 13 deletions cmd/sentry/sentry/sentry_multi_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ import (

"github.com/c2h5oh/datasize"
"github.com/holiman/uint256"
"github.com/ledgerwatch/log/v3"
"google.golang.org/grpc"
"google.golang.org/grpc/backoff"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/keepalive"
"google.golang.org/protobuf/types/known/emptypb"

"github.com/ledgerwatch/erigon-lib/chain"
libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/common/dbg"
Expand All @@ -23,12 +30,6 @@ import (
proto_types "github.com/ledgerwatch/erigon-lib/gointerfaces/types"
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon-lib/kv/kvcfg"
"github.com/ledgerwatch/log/v3"
"google.golang.org/grpc"
"google.golang.org/grpc/backoff"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/keepalive"
"google.golang.org/protobuf/types/known/emptypb"

"github.com/ledgerwatch/erigon/consensus"
"github.com/ledgerwatch/erigon/core/forkid"
Expand Down Expand Up @@ -68,8 +69,8 @@ func (cs *MultiClient) RecvUploadMessageLoop(
wg *sync.WaitGroup,
) {
ids := []proto_sentry.MessageId{
eth.ToProto[eth.ETH66][eth.GetBlockBodiesMsg],
eth.ToProto[eth.ETH66][eth.GetReceiptsMsg],
eth.ToProto[direct.ETH66][eth.GetBlockBodiesMsg],
eth.ToProto[direct.ETH66][eth.GetReceiptsMsg],
}
streamFactory := func(streamCtx context.Context, sentry direct.SentryClient) (sentryMessageStream, error) {
return sentry.Messages(streamCtx, &proto_sentry.MessagesRequest{Ids: ids}, grpc.WaitForReady(true))
Expand All @@ -84,7 +85,7 @@ func (cs *MultiClient) RecvUploadHeadersMessageLoop(
wg *sync.WaitGroup,
) {
ids := []proto_sentry.MessageId{
eth.ToProto[eth.ETH66][eth.GetBlockHeadersMsg],
eth.ToProto[direct.ETH66][eth.GetBlockHeadersMsg],
}
streamFactory := func(streamCtx context.Context, sentry direct.SentryClient) (sentryMessageStream, error) {
return sentry.Messages(streamCtx, &proto_sentry.MessagesRequest{Ids: ids}, grpc.WaitForReady(true))
Expand All @@ -99,10 +100,10 @@ func (cs *MultiClient) RecvMessageLoop(
wg *sync.WaitGroup,
) {
ids := []proto_sentry.MessageId{
eth.ToProto[eth.ETH66][eth.BlockHeadersMsg],
eth.ToProto[eth.ETH66][eth.BlockBodiesMsg],
eth.ToProto[eth.ETH66][eth.NewBlockHashesMsg],
eth.ToProto[eth.ETH66][eth.NewBlockMsg],
eth.ToProto[direct.ETH66][eth.BlockHeadersMsg],
eth.ToProto[direct.ETH66][eth.BlockBodiesMsg],
eth.ToProto[direct.ETH66][eth.NewBlockHashesMsg],
eth.ToProto[direct.ETH66][eth.NewBlockMsg],
}
streamFactory := func(streamCtx context.Context, sentry direct.SentryClient) (sentryMessageStream, error) {
return sentry.Messages(streamCtx, &proto_sentry.MessagesRequest{Ids: ids}, grpc.WaitForReady(true))
Expand Down
Loading

0 comments on commit cc11462

Please sign in to comment.