-
Notifications
You must be signed in to change notification settings - Fork 179
/
rate_limit_test.go
301 lines (256 loc) · 9.42 KB
/
rate_limit_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package rpc
import (
"context"
"fmt"
"io"
"os"
"testing"
"time"
accessproto "github.com/onflow/flow/protobuf/go/flow/access"
"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/status"
accessmock "github.com/onflow/flow-go/engine/access/mock"
"github.com/onflow/flow-go/engine/access/rest/websockets"
"github.com/onflow/flow-go/engine/access/rpc/backend"
statestreambackend "github.com/onflow/flow-go/engine/access/state_stream/backend"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/module/grpcserver"
"github.com/onflow/flow-go/module/irrecoverable"
"github.com/onflow/flow-go/module/metrics"
module "github.com/onflow/flow-go/module/mock"
"github.com/onflow/flow-go/network"
protocol "github.com/onflow/flow-go/state/protocol/mock"
storagemock "github.com/onflow/flow-go/storage/mock"
"github.com/onflow/flow-go/utils/grpcutils"
"github.com/onflow/flow-go/utils/unittest"
)
type RateLimitTestSuite struct {
suite.Suite
state *protocol.State
snapshot *protocol.Snapshot
epochQuery *protocol.EpochQuery
log zerolog.Logger
net *network.EngineRegistry
request *module.Requester
collClient *accessmock.AccessAPIClient
execClient *accessmock.ExecutionAPIClient
me *module.Local
chainID flow.ChainID
metrics *metrics.NoopCollector
rpcEng *Engine
client accessproto.AccessAPIClient
closer io.Closer
// storage
blocks *storagemock.Blocks
headers *storagemock.Headers
collections *storagemock.Collections
transactions *storagemock.Transactions
receipts *storagemock.ExecutionReceipts
// test rate limit
rateLimit int
burstLimit int
ctx irrecoverable.SignalerContext
cancel context.CancelFunc
// grpc servers
secureGrpcServer *grpcserver.GrpcServer
unsecureGrpcServer *grpcserver.GrpcServer
}
func (suite *RateLimitTestSuite) SetupTest() {
suite.log = zerolog.New(os.Stdout)
suite.net = new(network.EngineRegistry)
suite.state = new(protocol.State)
suite.snapshot = new(protocol.Snapshot)
rootHeader := unittest.BlockHeaderFixture()
params := new(protocol.Params)
params.On("SporkID").Return(unittest.IdentifierFixture(), nil)
params.On("ProtocolVersion").Return(uint(unittest.Uint64InRange(10, 30)), nil)
params.On("SporkRootBlockHeight").Return(rootHeader.Height, nil)
params.On("SealedRoot").Return(rootHeader, nil)
suite.epochQuery = new(protocol.EpochQuery)
suite.state.On("Sealed").Return(suite.snapshot, nil).Maybe()
suite.state.On("Final").Return(suite.snapshot, nil).Maybe()
suite.state.On("Params").Return(params, nil).Maybe()
suite.snapshot.On("Epochs").Return(suite.epochQuery).Maybe()
suite.blocks = new(storagemock.Blocks)
suite.headers = new(storagemock.Headers)
suite.transactions = new(storagemock.Transactions)
suite.collections = new(storagemock.Collections)
suite.receipts = new(storagemock.ExecutionReceipts)
suite.collClient = new(accessmock.AccessAPIClient)
suite.execClient = new(accessmock.ExecutionAPIClient)
suite.request = new(module.Requester)
suite.request.On("EntityByID", mock.Anything, mock.Anything)
suite.me = new(module.Local)
accessIdentity := unittest.IdentityFixture(unittest.WithRole(flow.RoleAccess))
suite.me.
On("NodeID").
Return(accessIdentity.NodeID)
suite.chainID = flow.Testnet
suite.metrics = metrics.NewNoopCollector()
config := Config{
UnsecureGRPCListenAddr: unittest.DefaultAddress,
SecureGRPCListenAddr: unittest.DefaultAddress,
HTTPListenAddr: unittest.DefaultAddress,
WebSocketConfig: websockets.NewDefaultWebsocketConfig(),
}
// generate a server certificate that will be served by the GRPC server
networkingKey := unittest.NetworkingPrivKeyFixture()
x509Certificate, err := grpcutils.X509Certificate(networkingKey)
assert.NoError(suite.T(), err)
tlsConfig := grpcutils.DefaultServerTLSConfig(x509Certificate)
// set the transport credentials for the server to use
config.TransportCredentials = credentials.NewTLS(tlsConfig)
// set the rate limit to test with
suite.rateLimit = 2
// set the burst limit to test with
suite.burstLimit = 2
apiRateLimt := map[string]int{
"Ping": suite.rateLimit,
}
apiBurstLimt := map[string]int{
"Ping": suite.rateLimit,
}
suite.secureGrpcServer = grpcserver.NewGrpcServerBuilder(suite.log,
config.SecureGRPCListenAddr,
grpcutils.DefaultMaxMsgSize,
false,
apiRateLimt,
apiBurstLimt,
grpcserver.WithTransportCredentials(config.TransportCredentials)).Build()
suite.unsecureGrpcServer = grpcserver.NewGrpcServerBuilder(suite.log,
config.UnsecureGRPCListenAddr,
grpcutils.DefaultMaxMsgSize,
false,
apiRateLimt,
apiBurstLimt).Build()
block := unittest.BlockHeaderFixture()
suite.snapshot.On("Head").Return(block, nil)
bnd, err := backend.New(backend.Params{
State: suite.state,
CollectionRPC: suite.collClient,
Blocks: suite.blocks,
Headers: suite.headers,
Collections: suite.collections,
Transactions: suite.transactions,
ChainID: suite.chainID,
AccessMetrics: suite.metrics,
MaxHeightRange: 0,
Log: suite.log,
SnapshotHistoryLimit: 0,
Communicator: backend.NewNodeCommunicator(false),
})
suite.Require().NoError(err)
stateStreamConfig := statestreambackend.Config{}
rpcEngBuilder, err := NewBuilder(
suite.log,
suite.state,
config,
suite.chainID,
suite.metrics,
false,
suite.me,
bnd,
bnd,
suite.secureGrpcServer,
suite.unsecureGrpcServer,
nil,
stateStreamConfig,
nil,
)
require.NoError(suite.T(), err)
suite.rpcEng, err = rpcEngBuilder.WithLegacy().Build()
require.NoError(suite.T(), err)
suite.ctx, suite.cancel = irrecoverable.NewMockSignalerContextWithCancel(suite.T(), context.Background())
suite.rpcEng.Start(suite.ctx)
suite.secureGrpcServer.Start(suite.ctx)
suite.unsecureGrpcServer.Start(suite.ctx)
// wait for the servers to startup
unittest.AssertClosesBefore(suite.T(), suite.secureGrpcServer.Ready(), 2*time.Second)
unittest.AssertClosesBefore(suite.T(), suite.unsecureGrpcServer.Ready(), 2*time.Second)
// wait for the engine to startup
unittest.RequireCloseBefore(suite.T(), suite.rpcEng.Ready(), 2*time.Second, "engine not ready at startup")
// create the access api client
suite.client, suite.closer, err = accessAPIClient(suite.unsecureGrpcServer.GRPCAddress().String())
require.NoError(suite.T(), err)
}
func (suite *RateLimitTestSuite) TearDownTest() {
if suite.cancel != nil {
suite.cancel()
}
// close the client
if suite.closer != nil {
suite.closer.Close()
}
// close servers
unittest.AssertClosesBefore(suite.T(), suite.secureGrpcServer.Done(), 2*time.Second)
unittest.AssertClosesBefore(suite.T(), suite.unsecureGrpcServer.Done(), 2*time.Second)
}
func TestRateLimit(t *testing.T) {
suite.Run(t, new(RateLimitTestSuite))
}
// TestRatelimitingWithoutBurst tests that rate limit is correctly applied to an Access API call
func (suite *RateLimitTestSuite) TestRatelimitingWithoutBurst() {
req := &accessproto.PingRequest{}
ctx := context.Background()
// expect 2 upstream calls
suite.execClient.On("Ping", mock.Anything, mock.Anything).Return(nil, nil).Times(suite.rateLimit)
suite.collClient.On("Ping", mock.Anything, mock.Anything).Return(nil, nil).Times(suite.rateLimit)
requestCnt := 0
// requests within the burst should succeed
for requestCnt < suite.rateLimit {
resp, err := suite.client.Ping(ctx, req)
assert.NoError(suite.T(), err)
assert.NotNil(suite.T(), resp)
// sleep to prevent burst
time.Sleep(100 * time.Millisecond)
requestCnt++
}
// request more than the limit should fail
_, err := suite.client.Ping(ctx, req)
suite.assertRateLimitError(err)
}
// TestRatelimitingWithBurst tests that burst limit is correctly applied to an Access API call
func (suite *RateLimitTestSuite) TestRatelimitingWithBurst() {
req := &accessproto.PingRequest{}
ctx := context.Background()
// expect rpc.defaultBurst number of upstream calls
suite.execClient.On("Ping", mock.Anything, mock.Anything).Return(nil, nil).Times(suite.burstLimit)
suite.collClient.On("Ping", mock.Anything, mock.Anything).Return(nil, nil).Times(suite.burstLimit)
requestCnt := 0
// generate a permissible burst of request and assert that they succeed
for requestCnt < suite.burstLimit {
resp, err := suite.client.Ping(ctx, req)
assert.NoError(suite.T(), err)
assert.NotNil(suite.T(), resp)
requestCnt++
}
// request more than the permissible burst and assert that it fails
_, err := suite.client.Ping(ctx, req)
suite.assertRateLimitError(err)
}
func (suite *RateLimitTestSuite) assertRateLimitError(err error) {
assert.Error(suite.T(), err)
status, ok := status.FromError(err)
assert.True(suite.T(), ok)
assert.Equal(suite.T(), codes.ResourceExhausted, status.Code())
}
func accessAPIClient(address string) (accessproto.AccessAPIClient, io.Closer, error) {
conn, err := grpc.Dial(
address,
grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(grpcutils.DefaultMaxMsgSize)),
grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
return nil, nil, fmt.Errorf("failed to connect to address %s: %w", address, err)
}
client := accessproto.NewAccessAPIClient(conn)
closer := io.Closer(conn)
return client, closer, nil
}