From 043fea20a29099b3f09b0edbb18aaff381c218e7 Mon Sep 17 00:00:00 2001 From: terence tsao Date: Sat, 18 Apr 2020 15:14:25 -0700 Subject: [PATCH] Revert "Revert "Add Separate Network Config (#5454)"" This reverts commit d43c2b7dbdac58e3d7b38c0c6b7b2f1fdbd99828. --- beacon-chain/p2p/encoder/BUILD.bazel | 2 ++ beacon-chain/p2p/encoder/ssz.go | 13 ++++++- beacon-chain/p2p/fork.go | 2 +- beacon-chain/p2p/sender.go | 3 +- beacon-chain/p2p/subnets.go | 6 ++-- beacon-chain/sync/deadlines.go | 5 +-- beacon-chain/sync/rpc.go | 6 ++-- beacon-chain/sync/subscriber.go | 5 +-- beacon-chain/sync/validate_aggregate_proof.go | 4 +-- shared/params/BUILD.bazel | 5 ++- shared/params/config.go | 2 -- shared/params/network_config.go | 36 +++++++++++++++++++ 12 files changed, 72 insertions(+), 17 deletions(-) create mode 100644 shared/params/network_config.go diff --git a/beacon-chain/p2p/encoder/BUILD.bazel b/beacon-chain/p2p/encoder/BUILD.bazel index 059d9148e18c..bb1d9a90d1bd 100644 --- a/beacon-chain/p2p/encoder/BUILD.bazel +++ b/beacon-chain/p2p/encoder/BUILD.bazel @@ -13,8 +13,10 @@ go_library( "//beacon-chain:__subpackages__", ], deps = [ + "//shared/params:go_default_library", "@com_github_gogo_protobuf//proto:go_default_library", "@com_github_golang_snappy//:go_default_library", + "@com_github_pkg_errors//:go_default_library", "@com_github_prysmaticlabs_go_ssz//:go_default_library", "@com_github_sirupsen_logrus//:go_default_library", ], diff --git a/beacon-chain/p2p/encoder/ssz.go b/beacon-chain/p2p/encoder/ssz.go index 69e3b37559a5..0ece15056a6f 100644 --- a/beacon-chain/p2p/encoder/ssz.go +++ b/beacon-chain/p2p/encoder/ssz.go @@ -7,14 +7,19 @@ import ( "github.com/gogo/protobuf/proto" "github.com/golang/snappy" + errors "github.com/pkg/errors" "github.com/prysmaticlabs/go-ssz" + "github.com/prysmaticlabs/prysm/shared/params" "github.com/sirupsen/logrus" ) var _ = NetworkEncoding(&SszNetworkEncoder{}) // MaxChunkSize allowed for decoding messages. -const MaxChunkSize = uint64(1 << 20) // 1Mb +var MaxChunkSize = params.BeaconNetworkConfig().MaxChunkSize // 1Mib + +// MaxGossipSize allowed for gossip messages. +var MaxGossipSize = params.BeaconNetworkConfig().GossipMaxSize // 1 Mib // SszNetworkEncoder supports p2p networking encoding using SimpleSerialize // with snappy compression (if enabled). @@ -50,6 +55,9 @@ func (e SszNetworkEncoder) EncodeGossip(w io.Writer, msg interface{}) (int, erro if err != nil { return 0, err } + if len(b) > int(MaxGossipSize) { + return 0, errors.Errorf("gossip message exceeds max gossip size: %d bytes > %d bytes", len(b), MaxGossipSize) + } if e.UseSnappyCompression { b = snappy.Encode(nil /*dst*/, b) } @@ -129,6 +137,9 @@ func (e SszNetworkEncoder) DecodeGossip(b []byte, to interface{}) error { return err } } + if len(b) > int(MaxGossipSize) { + return errors.Errorf("gossip message exceeds max gossip size: %d bytes > %d bytes", len(b), MaxGossipSize) + } return e.doDecode(b, to) } diff --git a/beacon-chain/p2p/fork.go b/beacon-chain/p2p/fork.go index 07c29ad9e71f..e0b6d3cce159 100644 --- a/beacon-chain/p2p/fork.go +++ b/beacon-chain/p2p/fork.go @@ -17,7 +17,7 @@ import ( ) // ENR key used for eth2-related fork data. -const eth2ENRKey = "eth2" +var eth2ENRKey = params.BeaconNetworkConfig().ETH2Key // ForkDigest returns the current fork digest of // the node. diff --git a/beacon-chain/p2p/sender.go b/beacon-chain/p2p/sender.go index d24302056e53..8df56273b098 100644 --- a/beacon-chain/p2p/sender.go +++ b/beacon-chain/p2p/sender.go @@ -7,6 +7,7 @@ import ( "github.com/libp2p/go-libp2p-core/network" "github.com/libp2p/go-libp2p-core/peer" "github.com/libp2p/go-libp2p-core/protocol" + "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/traceutil" "go.opencensus.io/trace" ) @@ -20,7 +21,7 @@ func (s *Service) Send(ctx context.Context, message interface{}, baseTopic strin span.AddAttributes(trace.StringAttribute("topic", topic)) // TTFB_TIME (5s) + RESP_TIMEOUT (10s). - const deadline = 15 * time.Second + var deadline = params.BeaconNetworkConfig().TtfbTimeout + params.BeaconNetworkConfig().RespTimeout ctx, cancel := context.WithTimeout(ctx, deadline) defer cancel() diff --git a/beacon-chain/p2p/subnets.go b/beacon-chain/p2p/subnets.go index b73eb3bcdb13..fa5a4e69dd51 100644 --- a/beacon-chain/p2p/subnets.go +++ b/beacon-chain/p2p/subnets.go @@ -4,10 +4,12 @@ import ( "github.com/ethereum/go-ethereum/p2p/enode" "github.com/ethereum/go-ethereum/p2p/enr" "github.com/prysmaticlabs/go-bitfield" + "github.com/prysmaticlabs/prysm/shared/params" ) -const attestationSubnetCount = 64 -const attSubnetEnrKey = "attnets" +var attestationSubnetCount = params.BeaconNetworkConfig().AttestationSubnetCount + +var attSubnetEnrKey = params.BeaconNetworkConfig().AttSubnetKey func intializeAttSubnets(node *enode.LocalNode) *enode.LocalNode { bitV := bitfield.NewBitvector64() diff --git a/beacon-chain/sync/deadlines.go b/beacon-chain/sync/deadlines.go index 11bbab0be726..178ce31e6517 100644 --- a/beacon-chain/sync/deadlines.go +++ b/beacon-chain/sync/deadlines.go @@ -4,10 +4,11 @@ import ( "time" "github.com/libp2p/go-libp2p-core/network" + "github.com/prysmaticlabs/prysm/shared/params" ) -const defaultReadDuration = ttfbTimeout -const defaultWriteDuration = 10 * time.Second // RESP_TIMEOUT +var defaultReadDuration = ttfbTimeout +var defaultWriteDuration = params.BeaconNetworkConfig().RespTimeout // RESP_TIMEOUT func setRPCStreamDeadlines(stream network.Stream) { setStreamReadDeadline(stream, defaultReadDuration) diff --git a/beacon-chain/sync/rpc.go b/beacon-chain/sync/rpc.go index 7fa3b5fb9e96..22d365e27634 100644 --- a/beacon-chain/sync/rpc.go +++ b/beacon-chain/sync/rpc.go @@ -4,12 +4,12 @@ import ( "context" "reflect" "strings" - "time" libp2pcore "github.com/libp2p/go-libp2p-core" "github.com/libp2p/go-libp2p-core/network" "github.com/prysmaticlabs/prysm/beacon-chain/p2p" pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" + "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/roughtime" "github.com/prysmaticlabs/prysm/shared/traceutil" "go.opencensus.io/trace" @@ -18,12 +18,12 @@ import ( // Time to first byte timeout. The maximum time to wait for first byte of // request response (time-to-first-byte). The client is expected to give up if // they don't receive the first byte within 5 seconds. -const ttfbTimeout = 5 * time.Second +var ttfbTimeout = params.BeaconNetworkConfig().TtfbTimeout // maxChunkSize would be the maximum allowed size that a request/response chunk can be. // any size beyond that would be rejected and the corresponding stream reset. This would // be 1048576 bytes or 1 MiB. -const maxChunkSize = 1 << 20 +var maxChunkSize = params.BeaconNetworkConfig().MaxChunkSize // rpcHandler is responsible for handling and responding to any incoming message. // This method may return an error to internal monitoring, but the error will diff --git a/beacon-chain/sync/subscriber.go b/beacon-chain/sync/subscriber.go index d4be1dde036c..6eecf6e45d33 100644 --- a/beacon-chain/sync/subscriber.go +++ b/beacon-chain/sync/subscriber.go @@ -12,7 +12,6 @@ import ( "github.com/libp2p/go-libp2p-core/peer" pubsub "github.com/libp2p/go-libp2p-pubsub" pb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" - "go.opencensus.io/trace" "github.com/prysmaticlabs/prysm/beacon-chain/core/feed" statefeed "github.com/prysmaticlabs/prysm/beacon-chain/core/feed/state" "github.com/prysmaticlabs/prysm/beacon-chain/p2p" @@ -22,10 +21,12 @@ import ( "github.com/prysmaticlabs/prysm/shared/roughtime" "github.com/prysmaticlabs/prysm/shared/slotutil" "github.com/prysmaticlabs/prysm/shared/traceutil" + "go.opencensus.io/trace" ) const pubsubMessageTimeout = 30 * time.Second -const maximumGossipClockDisparity = 500 * time.Millisecond + +var maximumGossipClockDisparity = params.BeaconNetworkConfig().MaximumGossipClockDisparity // subHandler represents handler for a given subscription. type subHandler func(context.Context, proto.Message) error diff --git a/beacon-chain/sync/validate_aggregate_proof.go b/beacon-chain/sync/validate_aggregate_proof.go index 501efd016819..7be744cc1803 100644 --- a/beacon-chain/sync/validate_aggregate_proof.go +++ b/beacon-chain/sync/validate_aggregate_proof.go @@ -197,7 +197,7 @@ func validateIndexInCommittee(ctx context.Context, s *stateTrie.BeaconState, a * func validateAggregateAttTime(attSlot uint64, genesisTime uint64) error { // in milliseconds attTime := 1000 * (genesisTime + (attSlot * params.BeaconConfig().SecondsPerSlot)) - attSlotRange := attSlot + params.BeaconConfig().AttestationPropagationSlotRange + attSlotRange := attSlot + params.BeaconNetworkConfig().AttestationPropagationSlotRange attTimeRange := 1000 * (genesisTime + (attSlotRange * params.BeaconConfig().SecondsPerSlot)) currentTimeInSec := roughtime.Now().Unix() currentTime := 1000 * currentTimeInSec @@ -206,7 +206,7 @@ func validateAggregateAttTime(attSlot uint64, genesisTime uint64) error { currentSlot := (uint64(currentTimeInSec) - genesisTime) / params.BeaconConfig().SecondsPerSlot if attTime-uint64(maximumGossipClockDisparity.Milliseconds()) > uint64(currentTime) || uint64(currentTime-maximumGossipClockDisparity.Milliseconds()) > attTimeRange { - return fmt.Errorf("attestation slot out of range %d <= %d <= %d", attSlot, currentSlot, attSlot+params.BeaconConfig().AttestationPropagationSlotRange) + return fmt.Errorf("attestation slot out of range %d <= %d <= %d", attSlot, currentSlot, attSlot+params.BeaconNetworkConfig().AttestationPropagationSlotRange) } return nil } diff --git a/shared/params/BUILD.bazel b/shared/params/BUILD.bazel index d27f10843f8f..e796ac865b2b 100644 --- a/shared/params/BUILD.bazel +++ b/shared/params/BUILD.bazel @@ -2,7 +2,10 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") go_library( name = "go_default_library", - srcs = ["config.go"], + srcs = [ + "config.go", + "network_config.go", + ], importpath = "github.com/prysmaticlabs/prysm/shared/params", visibility = ["//visibility:public"], deps = ["//shared/bytesutil:go_default_library"], diff --git a/shared/params/config.go b/shared/params/config.go index e507666ce06e..9ed2fe16ecc9 100644 --- a/shared/params/config.go +++ b/shared/params/config.go @@ -53,7 +53,6 @@ type BeaconChainConfig struct { MinEpochsToInactivityPenalty uint64 `yaml:"MIN_EPOCHS_TO_INACTIVITY_PENALTY"` // MinEpochsToInactivityPenalty defines the minimum amount of epochs since finality to begin penalizing inactivity. Eth1FollowDistance uint64 // Eth1FollowDistance is the number of eth1.0 blocks to wait before considering a new deposit for voting. This only applies after the chain as been started. SafeSlotsToUpdateJustified uint64 // SafeSlotsToUpdateJustified is the minimal slots needed to update justified check point. - AttestationPropagationSlotRange uint64 // AttestationPropagationSlotRange is the maximum number of slots during which an attestation can be propagated. SecondsPerETH1Block uint64 `yaml:"SECONDS_PER_ETH1_BLOCK"` // SecondsPerETH1Block is the approximate time for a single eth1 block to be produced. // State list lengths EpochsPerHistoricalVector uint64 `yaml:"EPOCHS_PER_HISTORICAL_VECTOR"` // EpochsPerHistoricalVector defines max length in epoch to store old historical stats in beacon state. @@ -155,7 +154,6 @@ var defaultBeaconConfig = &BeaconChainConfig{ MinEpochsToInactivityPenalty: 4, Eth1FollowDistance: 1024, SafeSlotsToUpdateJustified: 8, - AttestationPropagationSlotRange: 32, SecondsPerETH1Block: 14, // State list length constants. diff --git a/shared/params/network_config.go b/shared/params/network_config.go new file mode 100644 index 000000000000..f471901531ff --- /dev/null +++ b/shared/params/network_config.go @@ -0,0 +1,36 @@ +package params + +import "time" + +// NetworkConfig defines the spec based network parameters. +type NetworkConfig struct { + GossipMaxSize uint64 `yaml:"GOSSIP_MAX_SIZE"` // GossipMaxSize is the maximum allowed size of uncompressed gossip messages. + MaxChunkSize uint64 `yaml:"MAX_CHUNK_SIZE"` // MaxChunkSize is the the maximum allowed size of uncompressed req/resp chunked responses. + AttestationSubnetCount uint64 `yaml:"ATTESTATION_SUBNET_COUNT"` // AttestationSubnetCount is the number of attestation subnets used in the gossipsub protocol. + AttestationPropagationSlotRange uint64 `yaml:"ATTESTATION_PROPAGATION_SLOT_RANGE"` // AttestationPropagationSlotRange is the maximum number of slots during which an attestation can be propagated. + TtfbTimeout time.Duration `yaml:"TTFB_TIMEOUT"` // TtfbTimeout is the maximum time to wait for first byte of request response (time-to-first-byte). + RespTimeout time.Duration `yaml:"RESP_TIMEOUT"` // RespTimeout is the maximum time for complete response transfer. + MaximumGossipClockDisparity time.Duration `yaml:"MAXIMUM_GOSSIP_CLOCK_DISPARITY"` // MaximumGossipClockDisparity is the maximum milliseconds of clock disparity assumed between honest nodes. + + // DiscoveryV5 Config + ETH2Key string // ETH2Key is the ENR key of the eth2 object in an enr. + AttSubnetKey string // AttSubnetKey is the ENR key of the subnet bitfield in the enr. +} + +var defaultNetworkConfig = &NetworkConfig{ + GossipMaxSize: 1 << 20, // 1 MiB + MaxChunkSize: 1 << 20, // 1 MiB + AttestationSubnetCount: 64, + AttestationPropagationSlotRange: 32, + TtfbTimeout: 5 * time.Second, + RespTimeout: 10 * time.Second, + MaximumGossipClockDisparity: 500 * time.Millisecond, + ETH2Key: "eth2", + AttSubnetKey: "attnets", +} + +// BeaconNetworkConfig returns the current network config for +// the beacon chain. +func BeaconNetworkConfig() *NetworkConfig { + return defaultNetworkConfig +}