-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver_test.go
More file actions
368 lines (311 loc) · 9.36 KB
/
server_test.go
File metadata and controls
368 lines (311 loc) · 9.36 KB
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
package jsonrpc
import (
"bytes"
"context"
"encoding/json"
"errors"
"io"
"net/http"
"net/http/httptest"
"testing"
"github.com/rs/zerolog"
"github.com/evstack/apex/pkg/api"
"github.com/evstack/apex/pkg/store"
"github.com/evstack/apex/pkg/types"
)
// mockStore for JSON-RPC handler tests.
type mockStore struct {
headers map[uint64]*types.Header
blobs map[uint64][]types.Blob
syncState *types.SyncStatus
}
func newMockStore() *mockStore {
return &mockStore{
headers: make(map[uint64]*types.Header),
blobs: make(map[uint64][]types.Blob),
}
}
func (m *mockStore) PutBlobs(_ context.Context, blobs []types.Blob) error {
for _, b := range blobs {
m.blobs[b.Height] = append(m.blobs[b.Height], b)
}
return nil
}
func (m *mockStore) GetBlob(_ context.Context, ns types.Namespace, height uint64, index int) (*types.Blob, error) {
for _, b := range m.blobs[height] {
if b.Namespace == ns && b.Index == index {
return &b, nil
}
}
return nil, store.ErrNotFound
}
func (m *mockStore) GetBlobs(_ context.Context, ns types.Namespace, startHeight, endHeight uint64, _, _ int) ([]types.Blob, error) {
var result []types.Blob
for h := startHeight; h <= endHeight; h++ {
for _, b := range m.blobs[h] {
if b.Namespace == ns {
result = append(result, b)
}
}
}
return result, nil
}
func (m *mockStore) GetBlobByCommitment(_ context.Context, commitment []byte) (*types.Blob, error) {
for _, blobs := range m.blobs {
for i := range blobs {
if bytes.Equal(blobs[i].Commitment, commitment) {
return &blobs[i], nil
}
}
}
return nil, store.ErrNotFound
}
func (m *mockStore) PutHeader(_ context.Context, h *types.Header) error {
m.headers[h.Height] = h
return nil
}
func (m *mockStore) GetHeader(_ context.Context, height uint64) (*types.Header, error) {
h, ok := m.headers[height]
if !ok {
return nil, store.ErrNotFound
}
return h, nil
}
func (m *mockStore) PutNamespace(_ context.Context, _ types.Namespace) error { return nil }
func (m *mockStore) GetNamespaces(_ context.Context) ([]types.Namespace, error) { return nil, nil }
func (m *mockStore) GetSyncState(_ context.Context) (*types.SyncStatus, error) {
if m.syncState == nil {
return nil, store.ErrNotFound
}
return m.syncState, nil
}
func (m *mockStore) SetSyncState(_ context.Context, s types.SyncStatus) error {
m.syncState = &s
return nil
}
func (m *mockStore) Close() error { return nil }
type mockFetcher struct {
networkHead *types.Header
}
func (f *mockFetcher) GetHeader(_ context.Context, _ uint64) (*types.Header, error) {
return nil, errors.New("not implemented")
}
func (f *mockFetcher) GetBlobs(_ context.Context, _ uint64, _ []types.Namespace) ([]types.Blob, error) {
return nil, nil
}
func (f *mockFetcher) GetNetworkHead(_ context.Context) (*types.Header, error) {
if f.networkHead == nil {
return nil, errors.New("no network head")
}
return f.networkHead, nil
}
func (f *mockFetcher) SubscribeHeaders(_ context.Context) (<-chan *types.Header, error) {
return make(chan *types.Header), nil
}
func (f *mockFetcher) Close() error { return nil }
func testNamespace(b byte) types.Namespace {
var ns types.Namespace
ns[types.NamespaceSize-1] = b
return ns
}
type jsonRPCRequest struct {
Jsonrpc string `json:"jsonrpc"`
Method string `json:"method"`
Params []any `json:"params"`
ID int `json:"id"`
}
type jsonRPCResponse struct {
Jsonrpc string `json:"jsonrpc"`
Result json.RawMessage `json:"result"`
Error *jsonRPCError `json:"error,omitempty"`
ID int `json:"id"`
}
type jsonRPCError struct {
Code int `json:"code"`
Message string `json:"message"`
}
func doRPC(t *testing.T, srv http.Handler, method string, params ...any) jsonRPCResponse {
t.Helper()
if params == nil {
params = []any{}
}
req := jsonRPCRequest{
Jsonrpc: "2.0",
Method: method,
Params: params,
ID: 1,
}
body, err := json.Marshal(req)
if err != nil {
t.Fatalf("marshal request: %v", err)
}
httpReq := httptest.NewRequest(http.MethodPost, "/", bytes.NewReader(body)) //nolint:noctx
httpReq.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
srv.ServeHTTP(w, httpReq)
resp := w.Result()
defer resp.Body.Close() //nolint:errcheck
respBody, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatalf("read response: %v", err)
}
var rpcResp jsonRPCResponse
if err := json.Unmarshal(respBody, &rpcResp); err != nil {
t.Fatalf("unmarshal response: %v (body: %s)", err, respBody)
}
return rpcResp
}
func TestJSONRPCHeaderGetByHeight(t *testing.T) {
st := newMockStore()
st.headers[42] = &types.Header{
Height: 42,
RawHeader: []byte(`{"height":"42"}`),
}
notifier := api.NewNotifier(16, 1024, zerolog.Nop())
svc := api.NewService(st, &mockFetcher{}, nil, notifier, zerolog.Nop())
srv := NewServer(svc, zerolog.Nop())
resp := doRPC(t, srv, "header.GetByHeight", uint64(42))
if resp.Error != nil {
t.Fatalf("RPC error: %s", resp.Error.Message)
}
// Result should be the raw header JSON.
if string(resp.Result) != `{"height":"42"}` {
t.Errorf("result = %s, want raw header", resp.Result)
}
}
func TestJSONRPCHeaderLocalHead(t *testing.T) {
st := newMockStore()
st.syncState = &types.SyncStatus{LatestHeight: 100}
st.headers[100] = &types.Header{
Height: 100,
RawHeader: []byte(`{"height":"100"}`),
}
notifier := api.NewNotifier(16, 1024, zerolog.Nop())
svc := api.NewService(st, &mockFetcher{}, nil, notifier, zerolog.Nop())
srv := NewServer(svc, zerolog.Nop())
resp := doRPC(t, srv, "header.LocalHead")
if resp.Error != nil {
t.Fatalf("RPC error: %s", resp.Error.Message)
}
if string(resp.Result) != `{"height":"100"}` {
t.Errorf("result = %s", resp.Result)
}
}
func TestJSONRPCHeaderNetworkHead(t *testing.T) {
ft := &mockFetcher{
networkHead: &types.Header{
Height: 200,
RawHeader: []byte(`{"height":"200"}`),
},
}
notifier := api.NewNotifier(16, 1024, zerolog.Nop())
svc := api.NewService(newMockStore(), ft, nil, notifier, zerolog.Nop())
srv := NewServer(svc, zerolog.Nop())
resp := doRPC(t, srv, "header.NetworkHead")
if resp.Error != nil {
t.Fatalf("RPC error: %s", resp.Error.Message)
}
if string(resp.Result) != `{"height":"200"}` {
t.Errorf("result = %s", resp.Result)
}
}
func TestJSONRPCBlobGetAll(t *testing.T) {
st := newMockStore()
ns := testNamespace(1)
st.blobs[10] = []types.Blob{
{Height: 10, Namespace: ns, Data: []byte("d1"), Commitment: []byte("c1"), Index: 0},
}
notifier := api.NewNotifier(16, 1024, zerolog.Nop())
svc := api.NewService(st, &mockFetcher{}, nil, notifier, zerolog.Nop())
srv := NewServer(svc, zerolog.Nop())
resp := doRPC(t, srv, "blob.GetAll", uint64(10), [][]byte{ns[:]})
if resp.Error != nil {
t.Fatalf("RPC error: %s", resp.Error.Message)
}
var blobs []json.RawMessage
if err := json.Unmarshal(resp.Result, &blobs); err != nil {
t.Fatalf("unmarshal blobs: %v", err)
}
if len(blobs) != 1 {
t.Errorf("got %d blobs, want 1", len(blobs))
}
}
func TestJSONRPCBlobGetAllAllowsCompatibilitySizedNamespaceList(t *testing.T) {
st := newMockStore()
namespaces := make([][]byte, 0, 17)
for i := range 17 {
ns := testNamespace(byte(i + 1))
namespaces = append(namespaces, ns[:])
st.blobs[10] = append(st.blobs[10], types.Blob{
Height: 10,
Namespace: ns,
Data: []byte{byte(i)},
Commitment: []byte{byte(i)},
Index: 0,
})
}
notifier := api.NewNotifier(16, 1024, zerolog.Nop())
svc := api.NewService(st, &mockFetcher{}, nil, notifier, zerolog.Nop())
srv := NewServer(svc, zerolog.Nop())
resp := doRPC(t, srv, "blob.GetAll", uint64(10), namespaces)
if resp.Error != nil {
t.Fatalf("RPC error: %s", resp.Error.Message)
}
var blobs []json.RawMessage
if err := json.Unmarshal(resp.Result, &blobs); err != nil {
t.Fatalf("unmarshal blobs: %v", err)
}
if len(blobs) != 17 {
t.Fatalf("got %d blobs, want 17", len(blobs))
}
}
func TestJSONRPCBlobGetByCommitment(t *testing.T) {
st := newMockStore()
ns := testNamespace(1)
st.blobs[10] = []types.Blob{
{Height: 10, Namespace: ns, Data: []byte("d1"), Commitment: []byte("c1"), Index: 0},
}
notifier := api.NewNotifier(16, 1024, zerolog.Nop())
svc := api.NewService(st, &mockFetcher{}, nil, notifier, zerolog.Nop())
srv := NewServer(svc, zerolog.Nop())
t.Run("found", func(t *testing.T) {
resp := doRPC(t, srv, "blob.GetByCommitment", []byte("c1"))
if resp.Error != nil {
t.Fatalf("RPC error: %s", resp.Error.Message)
}
var blob map[string]json.RawMessage
if err := json.Unmarshal(resp.Result, &blob); err != nil {
t.Fatalf("unmarshal blob: %v", err)
}
if _, ok := blob["commitment"]; !ok {
t.Error("blob JSON missing 'commitment' field")
}
})
t.Run("not found", func(t *testing.T) {
resp := doRPC(t, srv, "blob.GetByCommitment", []byte("missing"))
if resp.Error == nil {
t.Fatal("expected error for missing commitment")
}
})
}
func TestJSONRPCStubMethods(t *testing.T) {
notifier := api.NewNotifier(16, 1024, zerolog.Nop())
svc := api.NewService(newMockStore(), &mockFetcher{}, nil, notifier, zerolog.Nop())
srv := NewServer(svc, zerolog.Nop())
tests := []struct {
method string
params []any
}{
{"share.GetShare", []any{uint64(1), 0, 0}},
{"share.GetEDS", []any{uint64(1)}},
{"fraud.Get", []any{"befp"}},
}
for _, tt := range tests {
t.Run(tt.method, func(t *testing.T) {
resp := doRPC(t, srv, tt.method, tt.params...)
if resp.Error == nil {
t.Error("expected error from stub method")
}
})
}
}