Skip to content

Commit

Permalink
Use eth2-types SSZUint64 (#8514)
Browse files Browse the repository at this point in the history
  • Loading branch information
pinglamb authored Feb 25, 2021
1 parent f0eb843 commit 5db5ca7
Show file tree
Hide file tree
Showing 23 changed files with 62 additions and 177 deletions.
1 change: 1 addition & 0 deletions beacon-chain/p2p/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ go_library(
"@com_github_pkg_errors//:go_default_library",
"@com_github_prometheus_client_golang//prometheus:go_default_library",
"@com_github_prometheus_client_golang//prometheus/promauto:go_default_library",
"@com_github_prysmaticlabs_eth2_types//:go_default_library",
"@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1:go_default_library",
"@com_github_prysmaticlabs_go_bitfield//:go_default_library",
"@com_github_sirupsen_logrus//:go_default_library",
Expand Down
5 changes: 3 additions & 2 deletions beacon-chain/p2p/rpc_topic_mappings.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import (
"reflect"

"github.com/pkg/errors"
"github.com/prysmaticlabs/prysm/beacon-chain/p2p/types"
types "github.com/prysmaticlabs/eth2-types"
p2ptypes "github.com/prysmaticlabs/prysm/beacon-chain/p2p/types"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
)

Expand All @@ -31,7 +32,7 @@ var RPCTopicMappings = map[string]interface{}{
RPCStatusTopic: new(pb.Status),
RPCGoodByeTopic: new(types.SSZUint64),
RPCBlocksByRangeTopic: new(pb.BeaconBlocksByRangeRequest),
RPCBlocksByRootTopic: new(types.BeaconBlockByRootsReq),
RPCBlocksByRootTopic: new(p2ptypes.BeaconBlockByRootsReq),
RPCPingTopic: new(types.SSZUint64),
RPCMetaDataTopic: new(interface{}),
}
Expand Down
2 changes: 1 addition & 1 deletion beacon-chain/p2p/types/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ go_library(
"//validator/client:__pkg__",
],
deps = [
"//shared/htrutils:go_default_library",
"//shared/params:go_default_library",
"@com_github_ferranbt_fastssz//:go_default_library",
"@com_github_pkg_errors//:go_default_library",
"@com_github_prysmaticlabs_eth2_types//:go_default_library",
],
)

Expand Down
6 changes: 5 additions & 1 deletion beacon-chain/p2p/types/rpc_goodbye_codes.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package types

import (
types "github.com/prysmaticlabs/eth2-types"
)

// RPCGoodbyeCode represents goodbye code, used in sync package.
type RPCGoodbyeCode = SSZUint64
type RPCGoodbyeCode = types.SSZUint64

const (
// Spec defined codes.
Expand Down
49 changes: 1 addition & 48 deletions beacon-chain/p2p/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,61 +6,14 @@ package types
import (
ssz "github.com/ferranbt/fastssz"
"github.com/pkg/errors"
"github.com/prysmaticlabs/prysm/shared/htrutils"
"github.com/prysmaticlabs/prysm/shared/params"
)

const rootLength = 32

const maxErrorLength = 256

// SSZUint64 is a uint64 type that satisfies the fast-ssz interface.
type SSZUint64 uint64

// SizeSSZ returns the size of the serialized representation.
func (s *SSZUint64) SizeSSZ() int {
return 8
}

// MarshalSSZTo marshals the uint64 with the provided byte slice.
func (s *SSZUint64) MarshalSSZTo(dst []byte) ([]byte, error) {
marshalledObj, err := s.MarshalSSZ()
if err != nil {
return nil, err
}
return append(dst, marshalledObj...), nil
}

// MarshalSSZ Marshals the uint64 type into the serialized object.
func (s *SSZUint64) MarshalSSZ() ([]byte, error) {
marshalledObj := ssz.MarshalUint64([]byte{}, uint64(*s))
return marshalledObj, nil
}

// UnmarshalSSZ unmarshals the provided bytes buffer into the
// uint64 object.
func (s *SSZUint64) UnmarshalSSZ(buf []byte) error {
if len(buf) != s.SizeSSZ() {
return errors.Errorf("expected buffer with length of %d but received length %d", s.SizeSSZ(), len(buf))
}
*s = SSZUint64(ssz.UnmarshallUint64(buf))
return nil
}

// HashTreeRoot hashes the uint64 object following the SSZ standard.
func (s *SSZUint64) HashTreeRoot() ([32]byte, error) {
return htrutils.Uint64Root(uint64(*s)), nil
}

// HashTreeRootWith hashes the uint64 object with the given hasher.
func (s *SSZUint64) HashTreeRootWith(hh *ssz.Hasher) error {
indx := hh.Index()
hh.PutUint64(uint64(*s))
hh.Merkleize(indx)
return nil
}

// SSZUint64 is a bytes slice that satisfies the fast-ssz interface.
// SSZBytes is a bytes slice that satisfies the fast-ssz interface.
type SSZBytes []byte

// HashTreeRoot hashes the uint64 object following the SSZ standard.
Expand Down
69 changes: 0 additions & 69 deletions beacon-chain/p2p/types/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@ import (
"github.com/prysmaticlabs/prysm/shared/testutil/require"
)

func TestSSZUint64_Limit(t *testing.T) {
sszType := SSZUint64(0)
serializedObj := [7]byte{}
require.ErrorContains(t, "expected buffer with length", sszType.UnmarshalSSZ(serializedObj[:]))
}

func TestBeaconBlockByRootsReq_Limit(t *testing.T) {
fixedRoots := make([][32]byte, 0)
for i := uint64(0); i < params.BeaconNetworkConfig().MaxRequestBlocks+100; i++ {
Expand Down Expand Up @@ -45,24 +39,10 @@ func TestErrorResponse_Limit(t *testing.T) {
}

func TestRoundTripSerialization(t *testing.T) {
roundTripTestSSZUint64(t)
roundTripTestBlocksByRootReq(t)
roundTripTestErrorMessage(t)
}

func roundTripTestSSZUint64(t *testing.T) {
fixedVal := uint64(8)
sszVal := SSZUint64(fixedVal)

marshalledObj, err := sszVal.MarshalSSZ()
require.NoError(t, err)
newVal := SSZUint64(0)

err = newVal.UnmarshalSSZ(marshalledObj)
require.NoError(t, err)
assert.DeepEqual(t, fixedVal, uint64(newVal))
}

func roundTripTestBlocksByRootReq(t *testing.T) {
fixedRoots := make([][32]byte, 0)
for i := 0; i < 200; i++ {
Expand Down Expand Up @@ -91,55 +71,6 @@ func roundTripTestErrorMessage(t *testing.T) {
assert.DeepEqual(t, []byte(newVal), errMsg)
}

func TestSSZUint64(t *testing.T) {
tests := []struct {
name string
serializedBytes []byte
actualValue uint64
root []byte
wantErr bool
}{
{
name: "max",
serializedBytes: hexDecodeOrDie(t, "ffffffffffffffff"),
actualValue: 18446744073709551615,
root: hexDecodeOrDie(t, "ffffffffffffffff000000000000000000000000000000000000000000000000"),
wantErr: false,
},
{
name: "random",
serializedBytes: hexDecodeOrDie(t, "357c8de9d7204577"),
actualValue: 8594311575614880821,
root: hexDecodeOrDie(t, "357c8de9d7204577000000000000000000000000000000000000000000000000"),
wantErr: false,
},
{
name: "zero",
serializedBytes: hexDecodeOrDie(t, "0000000000000000"),
actualValue: 0,
root: hexDecodeOrDie(t, "0000000000000000000000000000000000000000000000000000000000000000"),
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var s SSZUint64
if err := s.UnmarshalSSZ(tt.serializedBytes); (err != nil) != tt.wantErr {
t.Errorf("UnmarshalSSZ() error = %v, wantErr %v", err, tt.wantErr)
}
require.Equal(t, uint64(s), tt.actualValue)

serializedBytes, err := s.MarshalSSZ()
require.NoError(t, err)
require.DeepEqual(t, tt.serializedBytes, serializedBytes)

htr, err := s.HashTreeRoot()
require.NoError(t, err)
require.DeepEqual(t, tt.root, htr[:])
})
}
}

func TestSSZBytes_HashTreeRoot(t *testing.T) {
tests := []struct {
name string
Expand Down
7 changes: 3 additions & 4 deletions beacon-chain/sync/pending_attestations_queue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"github.com/prysmaticlabs/prysm/beacon-chain/operations/attestations"
"github.com/prysmaticlabs/prysm/beacon-chain/p2p/peers"
p2ptest "github.com/prysmaticlabs/prysm/beacon-chain/p2p/testing"
p2ptypes "github.com/prysmaticlabs/prysm/beacon-chain/p2p/types"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/abool"
"github.com/prysmaticlabs/prysm/shared/attestationutil"
Expand Down Expand Up @@ -95,7 +94,7 @@ func TestProcessPendingAtts_HasBlockSaveUnAggregatedAtt(t *testing.T) {

// Arbitrary aggregator index for testing purposes.
aggregatorIndex := committee[0]
sszUint := p2ptypes.SSZUint64(att.Data.Slot)
sszUint := types.SSZUint64(att.Data.Slot)
sig, err := helpers.ComputeDomainAndSign(beaconState, 0, &sszUint, params.BeaconConfig().DomainSelectionProof, privKeys[aggregatorIndex])
require.NoError(t, err)
aggregateAndProof := &ethpb.AggregateAttestationAndProof{
Expand Down Expand Up @@ -210,7 +209,7 @@ func TestProcessPendingAtts_NoBroadcastWithBadSignature(t *testing.T) {

// Arbitrary aggregator index for testing purposes.
aggregatorIndex := committee[0]
sszSlot := p2ptypes.SSZUint64(att.Data.Slot)
sszSlot := types.SSZUint64(att.Data.Slot)
sig, err := helpers.ComputeDomainAndSign(s, 0, &sszSlot, params.BeaconConfig().DomainSelectionProof, privKeys[aggregatorIndex])
require.NoError(t, err)
aggregateAndProof := &ethpb.AggregateAttestationAndProof{
Expand Down Expand Up @@ -287,7 +286,7 @@ func TestProcessPendingAtts_HasBlockSaveAggregatedAtt(t *testing.T) {

// Arbitrary aggregator index for testing purposes.
aggregatorIndex := committee[0]
sszUint := p2ptypes.SSZUint64(att.Data.Slot)
sszUint := types.SSZUint64(att.Data.Slot)
sig, err := helpers.ComputeDomainAndSign(beaconState, 0, &sszUint, params.BeaconConfig().DomainSelectionProof, privKeys[aggregatorIndex])
require.NoError(t, err)
aggregateAndProof := &ethpb.AggregateAttestationAndProof{
Expand Down
31 changes: 16 additions & 15 deletions beacon-chain/sync/rpc_goodbye.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,28 @@ import (
libp2pcore "github.com/libp2p/go-libp2p-core"
"github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer"
types "github.com/prysmaticlabs/eth2-types"
"github.com/prysmaticlabs/prysm/beacon-chain/p2p"
"github.com/prysmaticlabs/prysm/beacon-chain/p2p/types"
p2ptypes "github.com/prysmaticlabs/prysm/beacon-chain/p2p/types"
"github.com/prysmaticlabs/prysm/shared/mputil"
"github.com/sirupsen/logrus"
)

var backOffTime = map[types.SSZUint64]time.Duration{
// Do not dial peers which are from a different/unverifiable
// network.
types.GoodbyeCodeWrongNetwork: 24 * time.Hour,
types.GoodbyeCodeUnableToVerifyNetwork: 24 * time.Hour,
p2ptypes.GoodbyeCodeWrongNetwork: 24 * time.Hour,
p2ptypes.GoodbyeCodeUnableToVerifyNetwork: 24 * time.Hour,
// If local peer is banned, we back off for
// 2 hours to let the remote peer score us
// back up again.
types.GoodbyeCodeBadScore: 2 * time.Hour,
types.GoodbyeCodeBanned: 2 * time.Hour,
types.GoodbyeCodeClientShutdown: 1 * time.Hour,
p2ptypes.GoodbyeCodeBadScore: 2 * time.Hour,
p2ptypes.GoodbyeCodeBanned: 2 * time.Hour,
p2ptypes.GoodbyeCodeClientShutdown: 1 * time.Hour,
// Wait 5 minutes before dialing a peer who is
// 'full'
types.GoodbyeCodeTooManyPeers: 5 * time.Minute,
types.GoodbyeCodeGenericError: 2 * time.Minute,
p2ptypes.GoodbyeCodeTooManyPeers: 5 * time.Minute,
p2ptypes.GoodbyeCodeGenericError: 2 * time.Minute,
}

// goodbyeRPCHandler reads the incoming goodbye rpc message from the peer.
Expand Down Expand Up @@ -56,7 +57,7 @@ func (s *Service) disconnectBadPeer(ctx context.Context, id peer.ID) {
if !s.p2p.Peers().IsBad(id) {
return
}
goodbyeCode := types.ErrToGoodbyeCode(s.p2p.Peers().Scorers().ValidationError(id))
goodbyeCode := p2ptypes.ErrToGoodbyeCode(s.p2p.Peers().Scorers().ValidationError(id))
if err := s.sendGoodByeAndDisconnect(ctx, goodbyeCode, id); err != nil {
log.Debugf("Error when disconnecting with bad peer: %v", err)
}
Expand All @@ -65,10 +66,10 @@ func (s *Service) disconnectBadPeer(ctx context.Context, id peer.ID) {
// A custom goodbye method that is used by our connection handler, in the
// event we receive bad peers.
func (s *Service) sendGoodbye(ctx context.Context, id peer.ID) error {
return s.sendGoodByeAndDisconnect(ctx, types.GoodbyeCodeGenericError, id)
return s.sendGoodByeAndDisconnect(ctx, p2ptypes.GoodbyeCodeGenericError, id)
}

func (s *Service) sendGoodByeAndDisconnect(ctx context.Context, code types.RPCGoodbyeCode, id peer.ID) error {
func (s *Service) sendGoodByeAndDisconnect(ctx context.Context, code p2ptypes.RPCGoodbyeCode, id peer.ID) error {
lock := mputil.NewMultilock(id.String())
lock.Lock()
defer lock.Unlock()
Expand All @@ -86,7 +87,7 @@ func (s *Service) sendGoodByeAndDisconnect(ctx context.Context, code types.RPCGo
return s.p2p.Disconnect(id)
}

func (s *Service) sendGoodByeMessage(ctx context.Context, code types.RPCGoodbyeCode, id peer.ID) error {
func (s *Service) sendGoodByeMessage(ctx context.Context, code p2ptypes.RPCGoodbyeCode, id peer.ID) error {
ctx, cancel := context.WithTimeout(ctx, respTimeout)
defer cancel()

Expand All @@ -113,8 +114,8 @@ func (s *Service) sendGoodByeMessage(ctx context.Context, code types.RPCGoodbyeC
return nil
}

func goodbyeMessage(num types.RPCGoodbyeCode) string {
reason, ok := types.GoodbyeCodeMessages[num]
func goodbyeMessage(num p2ptypes.RPCGoodbyeCode) string {
reason, ok := p2ptypes.GoodbyeCodeMessages[num]
if ok {
return reason
}
Expand All @@ -123,7 +124,7 @@ func goodbyeMessage(num types.RPCGoodbyeCode) string {

// determines which backoff time to use depending on the
// goodbye code provided.
func goodByeBackoff(num types.RPCGoodbyeCode) time.Time {
func goodByeBackoff(num p2ptypes.RPCGoodbyeCode) time.Time {
duration, ok := backOffTime[num]
if !ok {
return time.Time{}
Expand Down
5 changes: 3 additions & 2 deletions beacon-chain/sync/rpc_goodbye_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/kevinms/leakybucket-go"
"github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/protocol"
types "github.com/prysmaticlabs/eth2-types"
db "github.com/prysmaticlabs/prysm/beacon-chain/db/testing"
p2ptest "github.com/prysmaticlabs/prysm/beacon-chain/p2p/testing"
p2ptypes "github.com/prysmaticlabs/prysm/beacon-chain/p2p/types"
Expand Down Expand Up @@ -156,7 +157,7 @@ func TestSendGoodbye_SendsMessage(t *testing.T) {
wg.Add(1)
p2.BHost.SetStreamHandler(pcl, func(stream network.Stream) {
defer wg.Done()
out := new(p2ptypes.SSZUint64)
out := new(types.SSZUint64)
assert.NoError(t, r.p2p.Encoding().DecodeWithMaxLength(stream, out))
assert.Equal(t, failureCode, *out)
assert.NoError(t, stream.Close())
Expand Down Expand Up @@ -198,7 +199,7 @@ func TestSendGoodbye_DisconnectWithPeer(t *testing.T) {
wg.Add(1)
p2.BHost.SetStreamHandler(pcl, func(stream network.Stream) {
defer wg.Done()
out := new(p2ptypes.SSZUint64)
out := new(types.SSZUint64)
assert.NoError(t, r.p2p.Encoding().DecodeWithMaxLength(stream, out))
assert.Equal(t, failureCode, *out)
assert.NoError(t, stream.Close())
Expand Down
Loading

0 comments on commit 5db5ca7

Please sign in to comment.