diff --git a/core/processor/abci.go b/core/processor/abci.go index 08c9e5e33e..82e6b7142f 100644 --- a/core/processor/abci.go +++ b/core/processor/abci.go @@ -40,7 +40,6 @@ import ( "code.vegaprotocol.io/vega/core/idgeneration" "code.vegaprotocol.io/vega/core/netparams" "code.vegaprotocol.io/vega/core/pow" - "code.vegaprotocol.io/vega/core/processor/ratelimit" "code.vegaprotocol.io/vega/core/snapshot" "code.vegaprotocol.io/vega/core/teams" "code.vegaprotocol.io/vega/core/txn" @@ -211,7 +210,6 @@ type App struct { log *logging.Logger cancelFn func() stopBlockchain func() error - rates *ratelimit.Rates // service injection assets Assets @@ -306,15 +304,11 @@ func NewApp(log *logging.Logger, app := &App{ abci: abci.New(codec), - log: log, - vegaPaths: vegaPaths, - cfg: config, - cancelFn: cancelFn, - stopBlockchain: stopBlockchain, - rates: ratelimit.New( - config.Ratelimit.Requests, - config.Ratelimit.PerNBlocks, - ), + log: log, + vegaPaths: vegaPaths, + cfg: config, + cancelFn: cancelFn, + stopBlockchain: stopBlockchain, assets: assets, banking: banking, broker: broker, @@ -1048,7 +1042,6 @@ func (app *App) OnBeginBlock(blockHeight uint64, blockHash string, blockTime tim app.blockCtx = ctx now := blockTime app.time.SetTimeNow(ctx, now) - app.rates.NextBlock() app.currentTimestamp = app.time.GetTimeNow() app.previousTimestamp = app.time.GetTimeLastBatch() app.log.Debug("ABCI service BEGIN completed", @@ -1330,10 +1323,6 @@ func (app *App) OnCheckTx(ctx context.Context, _ *tmtypes.RequestCheckTx, tx abc return ctx, &resp } - // Check ratelimits - // FIXME(): temporary disable all rate limiting - _, isval := app.limitPubkey(tx.PubKeyHex()) - gasWanted, err := app.gastimator.CalcGasWantedForTx(tx) if err != nil { // this error means the transaction couldn't be parsed app.log.Error("error getting gas estimate", logging.Error(err)) @@ -1347,30 +1336,9 @@ func (app *App) OnCheckTx(ctx context.Context, _ *tmtypes.RequestCheckTx, tx abc app.log.Debug("transaction passed checkTx", logging.String("tid", tx.GetPoWTID()), logging.String("command", tx.Command().String())) } - if isval { - return ctx, &resp - } - return ctx, &resp } -// limitPubkey returns whether a request should be rate limited or not. -func (app *App) limitPubkey(pk string) (limit bool, isValidator bool) { - // Do not rate limit validators nodes. - if app.top.IsValidatorVegaPubKey(pk) { - return false, true - } - - key := ratelimit.Key(pk).String() - if !app.rates.Allow(key) { - app.log.Debug("Rate limit exceeded", logging.String("key", key)) - return true, false - } - - app.log.Debug("RateLimit allowance", logging.String("key", key), logging.Int("count", app.rates.Count(key))) - return false, false -} - func (app *App) canSubmitTx(tx abci.Tx) (err error) { defer func() { if err != nil { diff --git a/core/processor/config.go b/core/processor/config.go index 093025c86e..57a2fd07f2 100644 --- a/core/processor/config.go +++ b/core/processor/config.go @@ -16,7 +16,6 @@ package processor import ( - "code.vegaprotocol.io/vega/core/processor/ratelimit" "code.vegaprotocol.io/vega/libs/config/encoding" "code.vegaprotocol.io/vega/logging" ) @@ -37,7 +36,6 @@ type Config struct { LogOrderSubmitDebug encoding.Bool `long:"log-order-submit-debug"` LogOrderAmendDebug encoding.Bool `long:"log-order-amend-debug"` LogOrderCancelDebug encoding.Bool `long:"log-order-cancel-debug"` - Ratelimit ratelimit.Config `group:"Ratelimit" namespace:"ratelimit"` KeepCheckpointsMax uint `long:"keep-checkpoints-max"` SnapshotDebug Snapshot `group:"SnapshotDebug" namespace:"snapshotdebug"` } @@ -48,7 +46,6 @@ func NewDefaultConfig() Config { return Config{ Level: encoding.LogLevel{Level: logging.InfoLevel}, LogOrderSubmitDebug: true, - Ratelimit: ratelimit.NewDefaultConfig(), KeepCheckpointsMax: 20, SnapshotDebug: Snapshot{ DevEnabled: false, diff --git a/core/processor/ratelimit/config.go b/core/processor/ratelimit/config.go deleted file mode 100644 index 342d4a679f..0000000000 --- a/core/processor/ratelimit/config.go +++ /dev/null @@ -1,32 +0,0 @@ -// Copyright (C) 2023 Gobalsky Labs Limited -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as -// published by the Free Software Foundation, either version 3 of the -// License, or (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - -package ratelimit - -type Config struct { - // How many requests - Requests int `description:" " long:"requests"` - - // In the last `PerNBlocks` blocks - PerNBlocks int `description:" " long:"per-n-blocks"` -} - -// NewDefaultConfig allows 500 requests in the last 10 blocks. -func NewDefaultConfig() Config { - return Config{ - Requests: 500, - PerNBlocks: 10, - } -} diff --git a/core/processor/ratelimit/ratelimit.go b/core/processor/ratelimit/ratelimit.go deleted file mode 100644 index f69ac27870..0000000000 --- a/core/processor/ratelimit/ratelimit.go +++ /dev/null @@ -1,89 +0,0 @@ -// Copyright (C) 2023 Gobalsky Labs Limited -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as -// published by the Free Software Foundation, either version 3 of the -// License, or (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - -package ratelimit - -import ( - "encoding/base64" -) - -type Key []byte - -func (k Key) String() string { - return base64.StdEncoding.EncodeToString(k) -} - -type Rates struct { - block int - requests int - perNBlocks int - entries map[string][]int -} - -func New(requests, perNBlocks int) *Rates { - return &Rates{ - block: 0, - requests: requests, - perNBlocks: perNBlocks, - entries: map[string][]int{}, - } -} - -// Count returns the number of requests recorded for a given key -// It returns -1 if the key has been not recorded or evicted. -func (r *Rates) Count(key string) int { - entry, ok := r.entries[key] - if !ok { - return -1 - } - - var count int - for _, n := range entry { - count += n - } - return count -} - -func (r *Rates) NextBlock() { - // compute the next block index - r.block = (r.block + 1) % (r.perNBlocks) - - // reset the counters for that particular block index - for _, c := range r.entries { - c[r.block] = 0 - } - - // We clean up the entries after finishing the block round - if r.block != 0 { - return - } - - for key := range r.entries { - if r.Count(key) == 0 { - delete(r.entries, key) - } - } -} - -func (r *Rates) Allow(key string) bool { - entry, ok := r.entries[key] - if !ok { - entry = make([]int, r.perNBlocks) - r.entries[key] = entry - } - entry[r.block]++ - - return r.Count(key) < r.requests -} diff --git a/core/processor/ratelimit/ratelimit_test.go b/core/processor/ratelimit/ratelimit_test.go deleted file mode 100644 index 072c333427..0000000000 --- a/core/processor/ratelimit/ratelimit_test.go +++ /dev/null @@ -1,98 +0,0 @@ -// Copyright (C) 2023 Gobalsky Labs Limited -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as -// published by the Free Software Foundation, either version 3 of the -// License, or (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - -package ratelimit_test - -import ( - "testing" - - "code.vegaprotocol.io/vega/core/processor/ratelimit" - - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" -) - -// runN executes the given `fn` func, `n` times. -func runN(n int, fn func()) { - for { - if n == 0 { - return - } - n-- - fn() - } -} - -func TestRateLimits(t *testing.T) { - t.Run("Single Block", func(t *testing.T) { - r := ratelimit.New(10, 10) // 10 requests in the last 10 blocks - - // make 9 requests, all should be allowed - runN(9, func() { - ok := r.Allow("test") - assert.True(t, ok) - }) - - // request number 10 should not be allowed - ok := r.Allow("test") - assert.False(t, ok) - }) - - t.Run("Even Blocks", func(t *testing.T) { - r := ratelimit.New(10, 10) // 10 requests in the last 10 blocks - - // this will make 1 request and move to the next block. - runN(9, func() { - ok := r.Allow("test") - assert.True(t, ok) - r.NextBlock() - }) - - ok := r.Allow("test") - assert.False(t, ok) - }) - - t.Run("Uneven Blocks", func(t *testing.T) { - r := ratelimit.New(10, 3) // 10 request in the last 3 blocks - - // let's fill the rate limiter - runN(100, func() { - _ = r.Allow("test") - }) - require.False(t, r.Allow("test")) - - r.NextBlock() - assert.False(t, r.Allow("test")) - - r.NextBlock() - assert.False(t, r.Allow("test")) - - r.NextBlock() - assert.True(t, r.Allow("test")) - }) - - t.Run("Clean up", func(t *testing.T) { - r := ratelimit.New(10, 10) - runN(10, func() { - r.Allow("test") - }) - require.Equal(t, 10, r.Count("test")) - - runN(10, func() { - r.NextBlock() - }) - require.Equal(t, -1, r.Count("test")) - }) -}