-
Notifications
You must be signed in to change notification settings - Fork 147
Expand file tree
/
Copy pathrpc_05-state_test.go
More file actions
392 lines (330 loc) · 12.3 KB
/
Copy pathrpc_05-state_test.go
File metadata and controls
392 lines (330 loc) · 12.3 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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
// Copyright 2021 ChainSafe Systems (ON)
// SPDX-License-Identifier: LGPL-3.0-only
package rpc
import (
"context"
"fmt"
"testing"
"time"
"github.com/ChainSafe/gossamer/dot/rpc/modules"
"github.com/ChainSafe/gossamer/lib/common"
libutils "github.com/ChainSafe/gossamer/lib/utils"
"github.com/ChainSafe/gossamer/tests/utils/config"
"github.com/ChainSafe/gossamer/tests/utils/node"
"github.com/ChainSafe/gossamer/tests/utils/rpc"
"github.com/stretchr/testify/require"
)
func TestStateRPCResponseValidation(t *testing.T) { //nolint:tparallel
genesisPath := libutils.GetWestendDevRawGenesisPath(t)
tomlConfig := config.Default()
tomlConfig.Init.Genesis = genesisPath
tomlConfig.Core.BABELead = true
node := node.New(t, tomlConfig)
ctx, cancel := context.WithCancel(context.Background())
node.InitAndStartTest(ctx, t, cancel)
getBlockHashCtx, getBlockHashCancel := context.WithTimeout(ctx, time.Second)
blockHash, err := rpc.GetBlockHash(getBlockHashCtx, node.RPCPort(), "")
getBlockHashCancel()
require.NoError(t, err)
t.Run("state_call", func(t *testing.T) {
t.Parallel()
const params = `["", "","0x580d77a9136035a0bc3c3cd86286172f7f81291164c5914266073a30466fba21"]`
var response modules.StateCallResponse
fetchWithTimeout(ctx, t, "state_call", params, &response)
// TODO assert stateCallResponse
})
t.Run("state_getKeysPaged", func(t *testing.T) {
t.Parallel()
t.SkipNow()
var response struct{} // TODO
fetchWithTimeout(ctx, t, "state_getKeysPaged", "", &response)
// TODO assert response
})
t.Run("state_queryStorage", func(t *testing.T) {
t.Parallel()
t.SkipNow() // TODO disable skip
params := fmt.Sprintf(
`[["0xf2794c22e353e9a839f12faab03a911bf68967d635641a7087e53f2bff1ecad3c6756fee45ec79ead60347fffb770bcdf0ec74da701ab3d6495986fe1ecc3027"], "%s", null]`, //nolint:lll
blockHash)
var response modules.StorageChangeSetResponse
fetchWithTimeout(ctx, t, "state_queryStorage", params, &response)
// TODO assert response
})
t.Run("state_getRuntimeVersion", func(t *testing.T) {
t.Parallel()
params := fmt.Sprintf(`[%q]`, blockHash)
var response modules.StateRuntimeVersionResponse
fetchWithTimeout(ctx, t, "state_getRuntimeVersion", params, &response)
// TODO assert response
})
t.Run("valid block hash state_getPairs", func(t *testing.T) {
t.Parallel()
params := fmt.Sprintf(`["0x", "%s"]`, blockHash)
var response modules.StatePairResponse
fetchWithTimeout(ctx, t, "state_getPairs", params, &response)
// TODO assert response
})
t.Run("valid block hash state_getMetadata", func(t *testing.T) {
t.Parallel()
params := fmt.Sprintf(`["%s"]`, blockHash)
var response modules.StateMetadataResponse
fetchWithTimeout(ctx, t, "state_getMetadata", params, &response)
// TODO assert response
})
t.Run("valid block hash state_getRuntimeVersion", func(t *testing.T) {
t.Parallel()
var response modules.StateRuntimeVersionResponse
fetchWithTimeout(ctx, t, "state_getRuntimeVersion", "[]", &response)
// TODO assert response
})
t.Run("optional params hash state_getPairs", func(t *testing.T) {
t.Parallel()
var response modules.StatePairResponse
fetchWithTimeout(ctx, t, "state_getPairs", `["0x"]`, &response)
// TODO assert response
})
t.Run("optional param hash state_getMetadata", func(t *testing.T) {
t.Parallel()
var response modules.StateMetadataResponse
fetchWithTimeout(ctx, t, "state_getMetadata", "[]", &response)
// TODO assert response
})
t.Run("optional param value as null state_getRuntimeVersion", func(t *testing.T) {
t.Parallel()
var response modules.StateRuntimeVersionResponse
fetchWithTimeout(ctx, t, "state_getRuntimeVersion", "[null]", &response)
// TODO assert response
})
t.Run("optional param value as null state_getMetadata", func(t *testing.T) {
t.Parallel()
var response modules.StateMetadataResponse
fetchWithTimeout(ctx, t, "state_getMetadata", "[null]", &response)
// TODO assert response
})
t.Run("optional param value as null state_getPairs", func(t *testing.T) {
t.Parallel()
var response modules.StatePairResponse
fetchWithTimeout(ctx, t, "state_getPairs", `["0x", null]`, &response)
// TODO assert response
})
}
func TestStateRPCAPI(t *testing.T) {
genesisPath := libutils.GetWestendLocalRawGenesisPath(t)
tomlConfig := config.Default()
tomlConfig.Init.Genesis = genesisPath
tomlConfig.Core.BABELead = true
node := node.New(t, tomlConfig)
ctx, cancel := context.WithCancel(context.Background())
node.InitAndStartTest(ctx, t, cancel)
time.Sleep(5 * time.Second) // Wait for block production
getBlockHashCtx, getBlockHashCancel := context.WithTimeout(ctx, time.Second)
blockHash, err := rpc.GetBlockHash(getBlockHashCtx, node.RPCPort(), "")
getBlockHashCancel()
require.NoError(t, err)
const (
randomHash = "0x580d77a9136035a0bc3c3cd86286172f7f81291164c5914266073a30466fba21"
ErrKeyNotFound = "Key not found"
InvalidHashFormat = "invalid hash format"
// `:grandpa_authorities` key
GrandpaAuthorityKey = "0x3a6772616e6470615f617574686f726974696573"
GrandpaAuthorityValue = "0x010c439660b36c6c03afafca027b910b4fecf99801834c62a5e6006f27d978de2" +
"34f010000000000000088dc3417d5058ec4b4503e0c12ea1a0a89be200fe98922423d4334014fa6b0ee0100000" +
"000000000d17c2d7823ebf260fd138f2d7e27d114c0145d968b5ff5006125f2414fadae690100000000000000"
StorageSizeGrandpaAuthorityKey = "122"
)
hash := common.MustBlake2bHash(common.MustHexToBytes(GrandpaAuthorityValue))
storageHashGrandpaAuthorityKey := common.BytesToHex(hash[:])
testCases := []*testCase{
{
description: "Test valid block hash state_getStorage",
method: "state_getStorage",
params: fmt.Sprintf(`["%s", "%s"]`, GrandpaAuthorityKey, blockHash.String()),
expected: GrandpaAuthorityValue,
},
{
description: "Test valid block hash state_getStorageHash",
method: "state_getStorageHash",
params: fmt.Sprintf(`["%s","%s"]`, GrandpaAuthorityKey, blockHash.String()),
expected: storageHashGrandpaAuthorityKey,
},
{
description: "Test valid block hash state_getStorageSize",
method: "state_getStorageSize",
params: fmt.Sprintf(`["%s", "%s"]`, GrandpaAuthorityKey, blockHash.String()),
expected: StorageSizeGrandpaAuthorityKey,
},
{
description: "Test empty value state_getRuntimeVersion",
method: "state_getRuntimeVersion",
params: `[""]`,
expected: InvalidHashFormat,
},
{
description: "Test empty value hash state_getPairs",
method: "state_getPairs",
params: `["0x", ""]`,
expected: InvalidHashFormat,
},
{
description: "Test empty value hash state_getMetadata",
method: "state_getMetadata",
params: `[""]`,
expected: InvalidHashFormat,
},
{
description: "Test empty value hash state_getStorage",
method: "state_getStorage",
params: fmt.Sprintf(`["%s", ""]`, GrandpaAuthorityKey),
expected: InvalidHashFormat,
},
{
description: "Test empty value hash state_getStorageHash",
method: "state_getStorageHash",
params: fmt.Sprintf(`["%s",""]`, GrandpaAuthorityKey),
expected: InvalidHashFormat,
},
{
description: "Test empty value hash state_getStorageSize",
method: "state_getStorageSize",
params: fmt.Sprintf(`["%s", ""]`, GrandpaAuthorityKey),
expected: InvalidHashFormat,
},
{
description: "Test optional params hash state_getStorage",
method: "state_getStorage",
params: fmt.Sprintf(`["%s"]`, GrandpaAuthorityKey),
expected: GrandpaAuthorityValue,
},
{
description: "Test optional params hash state_getStorageHash",
method: "state_getStorageHash",
params: fmt.Sprintf(`["%s"]`, GrandpaAuthorityKey),
expected: storageHashGrandpaAuthorityKey,
},
{
description: "Test optional params hash state_getStorageSize",
method: "state_getStorageSize",
params: fmt.Sprintf(`["%s"]`, GrandpaAuthorityKey),
expected: StorageSizeGrandpaAuthorityKey,
},
{
description: "Test invalid block hash state_getRuntimeVersion",
method: "state_getRuntimeVersion",
params: fmt.Sprintf(`["%s"]`, randomHash),
expected: ErrKeyNotFound,
},
{
description: "Test invalid block hash state_getPairs",
method: "state_getPairs",
params: fmt.Sprintf(`["0x", "%s"]`, randomHash),
expected: ErrKeyNotFound,
},
{
description: "Test invalid block hash state_getMetadata",
method: "state_getMetadata",
params: fmt.Sprintf(`["%s"]`, randomHash),
expected: ErrKeyNotFound,
},
{
description: "Test invalid block hash state_getStorage",
method: "state_getStorage",
params: fmt.Sprintf(`["%s", "%s"]`, GrandpaAuthorityKey, randomHash),
expected: ErrKeyNotFound,
},
{
description: "Test invalid block hash state_getStorageHash",
method: "state_getStorageHash",
params: fmt.Sprintf(`["%s","%s"]`, GrandpaAuthorityKey, randomHash),
expected: ErrKeyNotFound,
},
{
description: "Test invalid block hash state_getStorageSize",
method: "state_getStorageSize",
params: fmt.Sprintf(`["%s","%s"]`, GrandpaAuthorityKey, randomHash),
expected: ErrKeyNotFound,
},
{
description: "Test required param missing key state_getPairs",
method: "state_getPairs",
params: `[]`,
expected: "Field validation for 'Prefix' failed on the 'required' tag",
},
{
description: "Test required param missing key state_getStorage",
method: "state_getStorage",
params: `[]`,
expected: "Field validation for 'Key' failed on the 'required' tag",
},
{
description: "Test required param missing key state_getStorageSize",
method: "state_getStorageSize",
params: `[]`,
expected: "Field validation for 'Key' failed on the 'required' tag",
},
{
description: "Test required param missing key state_getStorageHash",
method: "state_getStorageHash",
params: `[]`,
expected: "Field validation for 'Key' failed on the 'required' tag",
},
{
description: "Test required param null state_getPairs",
method: "state_getPairs",
params: `[null]`,
expected: "Field validation for 'Prefix' failed on the 'required' tag",
},
{
description: "Test required param as null state_getStorage",
method: "state_getStorage",
params: `[null]`,
expected: "Field validation for 'Key' failed on the 'required' tag",
},
{
description: "Test required param as null state_getStorageSize",
method: "state_getStorageSize",
params: `[null]`,
expected: "Field validation for 'Key' failed on the 'required' tag",
},
{
description: "Test required param as null state_getStorageHash",
method: "state_getStorageHash",
params: `[null]`,
expected: "Field validation for 'Key' failed on the 'required' tag",
},
}
// Cases for valid block hash in RPC params
for _, test := range testCases {
t.Run(test.description, func(t *testing.T) {
postRPCCtx, cancel := context.WithTimeout(ctx, time.Second)
endpoint := rpc.NewEndpoint(node.RPCPort())
respBody, err := rpc.Post(postRPCCtx, endpoint, test.method, test.params)
cancel()
require.NoError(t, err)
require.Contains(t, string(respBody), test.expected)
})
}
}
func TestRPCStructParamUnmarshal(t *testing.T) {
genesisPath := libutils.GetWestendDevRawGenesisPath(t)
tomlConfig := config.Default()
tomlConfig.Core.BABELead = true
tomlConfig.Init.Genesis = genesisPath
node := node.New(t, tomlConfig)
ctx, cancel := context.WithCancel(context.Background())
node.InitAndStartTest(ctx, t, cancel)
time.Sleep(2 * time.Second) // Wait for block production
test := testCase{
description: "Test valid read request in local json2",
method: "state_queryStorage",
params: `[["0xf2794c22e353e9a839f12faab03a911bf68967d635641a7087e53f2bff1ecad3c6756fee45ec79ead60347fffb770bcdf0ec74da701ab3d6495986fe1ecc3027"],"0xa32c60dee8647b07435ae7583eb35cee606209a595718562dd4a486a07b6de15", null]`, //nolint:lll
}
t.Run(test.description, func(t *testing.T) {
postRPCCtx, postRPCCancel := context.WithTimeout(ctx, time.Second)
endpoint := rpc.NewEndpoint(node.RPCPort())
respBody, err := rpc.Post(postRPCCtx, endpoint, test.method, test.params)
postRPCCancel()
require.NoError(t, err)
require.NotContains(t, string(respBody), "json: cannot unmarshal")
fmt.Println(string(respBody))
})
}