Skip to content

Commit

Permalink
Add Block Batch Limit Flag (#5646)
Browse files Browse the repository at this point in the history
* add flag

* add flag

* gaz

* fix lint

* fix build issues

* revert initial sync changes

* fix tests

* Update beacon-chain/sync/service.go

Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
  • Loading branch information
nisdas and prylabs-bulldozer[bot] authored Apr 27, 2020
1 parent 4da7701 commit b6353da
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 6 deletions.
6 changes: 6 additions & 0 deletions beacon-chain/flags/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,10 @@ var (
Name: "disable-discv5",
Usage: "Does not run the discoveryV5 dht.",
}
// BlockBatchLimit specifies the requested block batch size.
BlockBatchLimit = &cli.IntFlag{
Name: "block-batch-limit",
Usage: "The amount of blocks the local peer is bounded to request and respond to in a batch.",
Value: 64,
}
)
2 changes: 2 additions & 0 deletions beacon-chain/flags/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type GlobalFlags struct {
MinimumSyncPeers int
MaxPageSize int
DeploymentBlock int
BlockBatchLimit int
}

var globalConfig *GlobalFlags
Expand Down Expand Up @@ -57,6 +58,7 @@ func ConfigureGlobalFlags(ctx *cli.Context) {
if ctx.Bool(DisableDiscv5.Name) {
cfg.DisableDiscv5 = true
}
cfg.BlockBatchLimit = ctx.Int(BlockBatchLimit.Name)
cfg.MaxPageSize = ctx.Int(RPCMaxPageSize.Name)
cfg.DeploymentBlock = ctx.Int(ContractDeploymentBlock.Name)
configureMinimumPeers(ctx, cfg)
Expand Down
1 change: 1 addition & 0 deletions beacon-chain/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ var appFlags = []cli.Flag{
flags.SetGCPercent,
flags.UnsafeSync,
flags.DisableDiscv5,
flags.BlockBatchLimit,
flags.InteropMockEth1DataVotesFlag,
flags.InteropGenesisStateFlag,
flags.InteropNumValidatorsFlag,
Expand Down
1 change: 1 addition & 0 deletions beacon-chain/sync/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ go_library(
"//beacon-chain/core/state/interop:go_default_library",
"//beacon-chain/db:go_default_library",
"//beacon-chain/db/filters:go_default_library",
"//beacon-chain/flags:go_default_library",
"//beacon-chain/operations/attestations:go_default_library",
"//beacon-chain/operations/slashings:go_default_library",
"//beacon-chain/operations/voluntaryexits:go_default_library",
Expand Down
8 changes: 4 additions & 4 deletions beacon-chain/sync/rpc_beacon_blocks_by_range.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ func (r *Service) beaconBlocksByRangeRPCHandler(ctx context.Context, msg interfa

// The initial count for the first batch to be returned back.
count := m.Count
if count > allowedBlocksPerSecond {
count = allowedBlocksPerSecond
if count > uint64(allowedBlocksPerSecond) {
count = uint64(allowedBlocksPerSecond)
}
// initial batch start and end slots to be returned to remote peer.
startSlot := m.StartSlot
Expand All @@ -60,7 +60,7 @@ func (r *Service) beaconBlocksByRangeRPCHandler(ctx context.Context, msg interfa
for startSlot <= endReqSlot {
remainingBucketCapacity = r.blocksRateLimiter.Remaining(stream.Conn().RemotePeer().String())

if allowedBlocksPerSecond > uint64(remainingBucketCapacity) {
if int64(allowedBlocksPerSecond) > remainingBucketCapacity {
r.p2p.Peers().IncrementBadResponses(stream.Conn().RemotePeer())
if r.p2p.Peers().IsBad(stream.Conn().RemotePeer()) {
log.Debug("Disconnecting bad peer")
Expand Down Expand Up @@ -89,7 +89,7 @@ func (r *Service) beaconBlocksByRangeRPCHandler(ctx context.Context, msg interfa

// Recalculate start and end slots for the next batch to be returned to the remote peer.
startSlot = endSlot + m.Step
endSlot = startSlot + (m.Step * (allowedBlocksPerSecond - 1))
endSlot = startSlot + (m.Step * (uint64(allowedBlocksPerSecond) - 1))
if endSlot > endReqSlot {
endSlot = endReqSlot
}
Expand Down
10 changes: 8 additions & 2 deletions beacon-chain/sync/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"sync"
"time"

"github.com/prysmaticlabs/prysm/beacon-chain/flags"
lru "github.com/hashicorp/golang-lru"
"github.com/kevinms/leakybucket-go"
"github.com/pkg/errors"
Expand All @@ -27,8 +28,9 @@ import (

var _ = shared.Service(&Service{})

const allowedBlocksPerSecond = 32.0
const allowedBlocksBurst = 10 * allowedBlocksPerSecond
var allowedBlocksPerSecond float64
var allowedBlocksBurst int64

const rangeLimit = 1000
const seenBlockSize = 1000
const seenAttSize = 10000
Expand Down Expand Up @@ -102,6 +104,10 @@ type Service struct {

// NewRegularSync service.
func NewRegularSync(cfg *Config) *Service {
// Intialize block limits.
allowedBlocksPerSecond = float64(flags.Get().BlockBatchLimit)
allowedBlocksBurst = int64(10 * allowedBlocksPerSecond)

ctx, cancel := context.WithCancel(context.Background())
r := &Service{
ctx: ctx,
Expand Down
5 changes: 5 additions & 0 deletions beacon-chain/sync/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ import (
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
)

func init() {
allowedBlocksPerSecond = 64
allowedBlocksBurst = int64(10 * allowedBlocksPerSecond)
}

func TestService_StatusZeroEpoch(t *testing.T) {
bState, err := stateTrie.InitializeFromProto(&pb.BeaconState{Slot: 0})
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions beacon-chain/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ var appHelpFlagGroups = []flagGroup{
flags.UnsafeSync,
flags.SlotsPerArchivedPoint,
flags.DisableDiscv5,
flags.BlockBatchLimit,
},
},
{
Expand Down

0 comments on commit b6353da

Please sign in to comment.