-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathproxy_test.go
312 lines (243 loc) · 11 KB
/
proxy_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
302
303
304
305
306
307
308
309
310
311
312
package main
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"time"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/require"
)
var (
newPayloadPath = "engine_newPayloadV1"
forkchoicePath = "engine_forkchoiceUpdatedV1"
transitionConfigPath = "engine_exchangeTransitionConfigurationV1"
// testLog is used to log information in the test methods
testLog = logrus.WithField("testing", true)
from = "10.0.0.0:1234"
)
type testBackend struct {
proxyService *ProxyService
builders []*mockServer
proxies []*mockServer
}
// newTestBackend creates a new backend, initializes mock builders and return the instance
func newTestBackend(t *testing.T, numBuilders, numProxies int, builderTimeout, proxyTimeout time.Duration) *testBackend {
backend := testBackend{
builders: createMockServers(t, numBuilders),
proxies: createMockServers(t, numProxies),
}
builderUrls := getURLs(t, backend.builders)
proxyUrls := getURLs(t, backend.proxies)
opts := ProxyServiceOpts{
Log: testLog,
ListenAddr: "localhost:12345",
Builders: builderUrls,
BuilderTimeout: builderTimeout,
Proxies: proxyUrls,
ProxyTimeout: proxyTimeout,
}
service, err := NewProxyService(opts)
require.NoError(t, err)
backend.proxyService = service
return &backend
}
func createMockServers(t *testing.T, num int) []*mockServer {
servers := make([]*mockServer, num)
for i := 0; i < num; i++ {
servers[i] = newMockServer(t)
servers[i].Response = []byte(mockNewPayloadResponseValid)
}
return servers
}
// get urls from the mock servers
func getURLs(t *testing.T, servers []*mockServer) []*url.URL {
urls := make([]*url.URL, len(servers))
for i := 0; i < len(servers); i++ {
url, err := url.Parse(servers[i].Server.URL)
require.NoError(t, err)
urls[i] = url
}
return urls
}
func (be *testBackend) request(t *testing.T, payload []byte, from string) *httptest.ResponseRecorder {
var req *http.Request
var err error
if payload == nil {
req, err = http.NewRequest(http.MethodGet, "/", nil)
} else {
req, err = http.NewRequest(http.MethodPost, "/", bytes.NewReader(payload))
}
require.NoError(t, err)
req.RemoteAddr = from
rr := httptest.NewRecorder()
be.proxyService.ServeHTTP(rr, req)
return rr
}
func TestRequests(t *testing.T) {
t.Run("test new payload request", func(t *testing.T) {
backend := newTestBackend(t, 2, 0, time.Second, time.Second)
backend.builders[0].Response = []byte(mockNewPayloadResponseValid)
backend.builders[1].Response = []byte(mockNewPayloadResponseValid)
rr := backend.request(t, []byte(mockNewPayloadRequest), from)
require.Equal(t, http.StatusOK, rr.Code, rr.Body.String())
require.Equal(t, 1, backend.builders[0].GetRequestCount(newPayloadPath))
require.Equal(t, 1, backend.builders[1].GetRequestCount(newPayloadPath))
var resp JSONRPCResponse
resp.Result = new(PayloadStatusV1)
err := json.Unmarshal(rr.Body.Bytes(), &resp)
require.NoError(t, err)
require.Equal(t, rr.Body.String(), mockNewPayloadResponseValid)
})
t.Run("test forkchoice updated request", func(t *testing.T) {
backend := newTestBackend(t, 2, 0, time.Second, time.Second)
backend.builders[0].Response = []byte(mockForkchoiceResponse)
backend.builders[1].Response = []byte(mockForkchoiceResponse)
rr := backend.request(t, []byte(mockForkchoiceRequest), from)
require.Equal(t, http.StatusOK, rr.Code, rr.Body.String())
require.Equal(t, 1, backend.builders[0].GetRequestCount(forkchoicePath))
require.Equal(t, 1, backend.builders[1].GetRequestCount(forkchoicePath))
var resp JSONRPCResponse
resp.Result = new(ForkChoiceResponse)
err := json.Unmarshal(rr.Body.Bytes(), &resp)
require.NoError(t, err)
require.Equal(t, rr.Body.String(), mockForkchoiceResponse)
})
t.Run("test engine request", func(t *testing.T) {
backend := newTestBackend(t, 2, 0, time.Second, time.Second)
backend.builders[0].Response = []byte(mockTransitionResponse)
backend.builders[1].Response = []byte(mockTransitionResponse)
rr := backend.request(t, []byte(mockTransitionRequest), from)
require.Equal(t, http.StatusOK, rr.Code, rr.Body.String())
require.Equal(t, 1, backend.builders[0].GetRequestCount(transitionConfigPath))
require.Equal(t, 1, backend.builders[1].GetRequestCount(transitionConfigPath))
var resp JSONRPCResponse
err := json.Unmarshal(rr.Body.Bytes(), &resp)
require.NoError(t, err)
require.Equal(t, rr.Body.String(), mockTransitionResponse)
})
t.Run("service should send request to builders as well as other proxies", func(t *testing.T) {
backend := newTestBackend(t, 2, 2, time.Second, time.Second)
rr := backend.request(t, []byte(mockNewPayloadRequest), from)
require.Equal(t, http.StatusOK, rr.Code, rr.Body.String())
require.Equal(t, 1, backend.builders[0].GetRequestCount(newPayloadPath))
require.Equal(t, 1, backend.builders[1].GetRequestCount(newPayloadPath))
})
t.Run("should filter requests not from engine or builder namespace", func(t *testing.T) {
backend := newTestBackend(t, 1, 0, time.Second, time.Second)
rr := backend.request(t, []byte(mockEthChainIDRequest), from)
require.Equal(t, http.StatusOK, rr.Code)
require.Equal(t, rr.Body.String(), "")
})
t.Run("should filter requests not from the best synced", func(t *testing.T) {
backend := newTestBackend(t, 2, 2, time.Second, time.Second)
rr := backend.request(t, []byte(mockForkchoiceRequest), "localhost:8080")
require.Equal(t, http.StatusOK, rr.Code, rr.Body.String())
rr = backend.request(t, []byte(mockForkchoiceRequest), from)
require.Equal(t, http.StatusOK, rr.Code, rr.Body.String())
require.Equal(t, 1, backend.builders[0].GetRequestCount(forkchoicePath))
require.Equal(t, 1, backend.builders[1].GetRequestCount(forkchoicePath))
})
t.Run("service should not filter new payload requests from any beacon node", func(t *testing.T) {
backend := newTestBackend(t, 2, 2, time.Second, time.Second)
rr := backend.request(t, []byte(mockNewPayloadRequest), "localhost:8080")
require.Equal(t, http.StatusOK, rr.Code, rr.Body.String())
rr = backend.request(t, []byte(mockNewPayloadRequest), from)
require.Equal(t, http.StatusOK, rr.Code, rr.Body.String())
require.Equal(t, 2, backend.builders[0].GetRequestCount(newPayloadPath))
require.Equal(t, 2, backend.builders[1].GetRequestCount(newPayloadPath))
})
t.Run("should return status ok for GET requests", func(t *testing.T) {
backend := newTestBackend(t, 1, 0, time.Second, time.Second)
rr := backend.request(t, nil, from)
require.Equal(t, http.StatusOK, rr.Code, rr.Body.String())
})
}
func TestBuilders(t *testing.T) {
t.Run("builders have different responses should return response of first builder", func(t *testing.T) {
backend := newTestBackend(t, 2, 0, time.Second, time.Second)
backend.builders[0].Response = []byte(mockNewPayloadResponseSyncing)
backend.builders[1].Response = []byte(mockNewPayloadResponseValid)
rr := backend.request(t, []byte(mockNewPayloadRequest), from)
require.Equal(t, http.StatusOK, rr.Code, rr.Body.String())
require.Equal(t, 1, backend.builders[0].GetRequestCount(newPayloadPath))
require.Equal(t, 1, backend.builders[1].GetRequestCount(newPayloadPath))
var resp JSONRPCResponse
resp.Result = new(PayloadStatusV1)
err := json.Unmarshal(rr.Body.Bytes(), &resp)
require.NoError(t, err)
require.Equal(t, rr.Body.String(), mockNewPayloadResponseSyncing)
})
t.Run("only first builder online should return response of first builder", func(t *testing.T) {
backend := newTestBackend(t, 2, 0, time.Second, time.Second)
backend.builders[0].Response = []byte(mockForkchoiceResponse)
backend.builders[1].Server.Close()
rr := backend.request(t, []byte(mockForkchoiceRequest), from)
require.Equal(t, http.StatusOK, rr.Code, rr.Body.String())
require.Equal(t, 1, backend.builders[0].GetRequestCount(forkchoicePath))
require.Equal(t, 0, backend.builders[1].GetRequestCount(forkchoicePath))
var resp JSONRPCResponse
resp.Result = new(ForkChoiceResponse)
err := json.Unmarshal(rr.Body.Bytes(), &resp)
require.NoError(t, err)
require.Equal(t, rr.Body.String(), mockForkchoiceResponse)
})
t.Run("if first builder is offline proxy should fallback to another builder", func(t *testing.T) {
backend := newTestBackend(t, 2, 0, time.Second, time.Second)
backend.builders[1].Response = []byte(mockNewPayloadResponseSyncing)
backend.builders[0].Server.Close()
rr := backend.request(t, []byte(mockNewPayloadRequest), from)
require.Equal(t, http.StatusOK, rr.Code, rr.Body.String())
require.Equal(t, 0, backend.builders[0].GetRequestCount(newPayloadPath))
require.Equal(t, 1, backend.builders[1].GetRequestCount(newPayloadPath))
var resp JSONRPCResponse
resp.Result = new(PayloadStatusV1)
err := json.Unmarshal(rr.Body.Bytes(), &resp)
require.NoError(t, err)
require.Equal(t, rr.Body.String(), mockNewPayloadResponseSyncing)
})
t.Run("all builders are down", func(t *testing.T) {
backend := newTestBackend(t, 1, 0, time.Second, time.Second)
backend.builders[0].Server.Close()
rr := backend.request(t, []byte(mockNewPayloadRequest), from)
require.Equal(t, http.StatusBadGateway, rr.Code, rr.Body.String())
require.Equal(t, 0, backend.builders[0].GetRequestCount(newPayloadPath))
})
}
func TestUpdateBestBeaconNode(t *testing.T) {
var data JSONRPCRequest
json.Unmarshal([]byte(mockForkchoiceRequestWithPayloadAttributesV1), &data)
data.Params[1].(*PayloadAttributes).Timestamp = 10
higherTimestampFcu, err := json.Marshal(data)
require.NoError(t, err)
json.Unmarshal([]byte(mockForkchoiceRequestWithPayloadAttributesV2), &data)
data.Params[1].(*PayloadAttributes).Timestamp = 1
lowerTimestampFcu, err := json.Marshal(data)
require.NoError(t, err)
json.Unmarshal([]byte(mockForkchoiceRequest), &data)
t.Run("should update address to sync if sync target address is not set", func(t *testing.T) {
backend := newTestBackend(t, 1, 0, time.Second, time.Second)
backend.request(t, []byte(mockNewPayloadRequest), from)
require.NotNil(t, backend.proxyService.bestBeaconEntry)
})
t.Run("should update address to sync if higher current timestamp is received", func(t *testing.T) {
backend := newTestBackend(t, 1, 0, time.Second, time.Second)
backend.request(t, lowerTimestampFcu, from)
require.NotNil(t, backend.proxyService.bestBeaconEntry)
require.Equal(t, backend.proxyService.bestBeaconEntry.Timestamp, uint64(1))
backend.request(t, higherTimestampFcu, from)
require.NotNil(t, backend.proxyService.bestBeaconEntry)
require.Equal(t, uint64(10), backend.proxyService.bestBeaconEntry.Timestamp)
})
t.Run("should not update address to sync if timestamp received is not higher than previously received", func(t *testing.T) {
backend := newTestBackend(t, 1, 0, time.Second, time.Second)
backend.request(t, higherTimestampFcu, from)
require.NotNil(t, backend.proxyService.bestBeaconEntry)
require.Equal(t, backend.proxyService.bestBeaconEntry.Timestamp, uint64(10))
backend.request(t, higherTimestampFcu, from)
require.NotNil(t, backend.proxyService.bestBeaconEntry)
require.Equal(t, backend.proxyService.bestBeaconEntry.Timestamp, uint64(10))
})
}