Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes bug with excessive rate limiting #5853

Merged
merged 1 commit into from
May 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions beacon-chain/sync/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ go_test(
"subscriber_beacon_blocks_test.go",
"subscriber_committee_index_beacon_attestation_test.go",
"subscriber_test.go",
"sync_test.go",
"validate_aggregate_proof_test.go",
"validate_attester_slashing_test.go",
"validate_beacon_blocks_test.go",
Expand Down
8 changes: 6 additions & 2 deletions beacon-chain/sync/rpc_beacon_blocks_by_range.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ func (r *Service) beaconBlocksByRangeRPCHandler(ctx context.Context, msg interfa
)
for startSlot <= endReqSlot {
remainingBucketCapacity = r.blocksRateLimiter.Remaining(stream.Conn().RemotePeer().String())

if int64(allowedBlocksPerSecond) > remainingBucketCapacity {
r.p2p.Peers().IncrementBadResponses(stream.Conn().RemotePeer())
if r.p2p.Peers().IsBad(stream.Conn().RemotePeer()) {
Expand All @@ -75,7 +74,6 @@ func (r *Service) beaconBlocksByRangeRPCHandler(ctx context.Context, msg interfa
r.writeErrorResponseToStream(responseCodeInvalidRequest, rateLimitedError, stream)
return errors.New(rateLimitedError)
}
r.blocksRateLimiter.Add(stream.Conn().RemotePeer().String(), int64(allowedBlocksPerSecond))

// TODO(3147): Update this with reasonable constraints.
if endSlot-startSlot > rangeLimit || m.Step == 0 {
Expand All @@ -89,6 +87,12 @@ func (r *Service) beaconBlocksByRangeRPCHandler(ctx context.Context, msg interfa
return err
}

// Decrease allowed blocks capacity by the number of streamed blocks.
if startSlot <= endSlot {
r.blocksRateLimiter.Add(
stream.Conn().RemotePeer().String(), int64(1+(endSlot-startSlot)/m.Step))
}

// Recalculate start and end slots for the next batch to be returned to the remote peer.
startSlot = endSlot + m.Step
endSlot = startSlot + (m.Step * (uint64(allowedBlocksPerSecond) - 1))
Expand Down
12 changes: 10 additions & 2 deletions beacon-chain/sync/rpc_beacon_blocks_by_range_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,14 @@ func TestBeaconBlocksRPCHandler_ReturnsBlocks(t *testing.T) {
}

// Populate the database with blocks that would match the request.
for i := req.StartSlot; i < req.StartSlot+(req.Step*req.Count); i++ {
for i := req.StartSlot; i < req.StartSlot+(req.Step*req.Count); i += req.Step {
if err := d.SaveBlock(context.Background(), &ethpb.SignedBeaconBlock{Block: &ethpb.BeaconBlock{Slot: i}}); err != nil {
t.Fatal(err)
}
}

r := &Service{p2p: p1, db: d, blocksRateLimiter: leakybucket.NewCollector(10000, 10000, false)}
// Start service with 160 as allowed blocks capacity (and almost zero capacity recovery).
r := &Service{p2p: p1, db: d, blocksRateLimiter: leakybucket.NewCollector(0.000001, int64(req.Count*10), false)}
pcl := protocol.ID("/testing")

var wg sync.WaitGroup
Expand Down Expand Up @@ -68,6 +69,13 @@ func TestBeaconBlocksRPCHandler_ReturnsBlocks(t *testing.T) {
t.Errorf("Unexpected error: %v", err)
}

// Make sure that rate limiter doesn't limit capacity exceedingly.
remainingCapacity := r.blocksRateLimiter.Remaining(p2.PeerID().String())
expectedCapacity := int64(req.Count*10 - req.Count)
if remainingCapacity != expectedCapacity {
t.Fatalf("Unexpected rate limiting capacity, expected: %v, got: %v", expectedCapacity, remainingCapacity)
}

if testutil.WaitTimeout(&wg, 1*time.Second) {
t.Fatal("Did not receive stream within 1 sec")
}
Expand Down
19 changes: 19 additions & 0 deletions beacon-chain/sync/sync_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package sync

import (
"io/ioutil"
"os"
"testing"

"github.com/sirupsen/logrus"
)

func TestMain(m *testing.M) {
logrus.SetLevel(logrus.DebugLevel)
logrus.SetOutput(ioutil.Discard)

allowedBlocksPerSecond = 64
allowedBlocksBurst = int64(10 * allowedBlocksPerSecond)

os.Exit(m.Run())
}