Skip to content

Commit 31383eb

Browse files
authored
chore(dot/rpc): NewStateRuntimeVersionResponse (#2740)
- Add new conversion function `NewStateRuntimeVersionResponse` - Inline `modules.ConvertAPIs` content in `NewStateRuntimeVersionResponse` - Replace all conversions with `NewStateRuntimeVersionResponse` - Update tests to have explicit expected API items to remove `modules.ConvertAPIs`
1 parent 83eac8a commit 31383eb

File tree

3 files changed

+49
-41
lines changed

3 files changed

+49
-41
lines changed

dot/rpc/modules/state.go

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,30 @@ type StateRuntimeVersionResponse struct {
144144
Apis []interface{} `json:"apis"`
145145
}
146146

147+
// NewStateRuntimeVersionResponse converts a runtime.Version to a
148+
// StateRuntimeVersionResponse struct.
149+
func NewStateRuntimeVersionResponse(runtimeVersion runtime.Version) (
150+
response StateRuntimeVersionResponse) {
151+
apisResponse := make([]interface{}, len(runtimeVersion.APIItems))
152+
for i, apiItem := range runtimeVersion.APIItems {
153+
hexItemName := hex.EncodeToString(apiItem.Name[:])
154+
apisResponse[i] = []interface{}{
155+
"0x" + hexItemName,
156+
apiItem.Ver,
157+
}
158+
}
159+
160+
return StateRuntimeVersionResponse{
161+
SpecName: string(runtimeVersion.SpecName),
162+
ImplName: string(runtimeVersion.ImplName),
163+
AuthoringVersion: runtimeVersion.AuthoringVersion,
164+
SpecVersion: runtimeVersion.SpecVersion,
165+
ImplVersion: runtimeVersion.ImplVersion,
166+
TransactionVersion: runtimeVersion.TransactionVersion,
167+
Apis: apisResponse,
168+
}
169+
}
170+
147171
// StateModule is an RPC module providing access to storage API points.
148172
type StateModule struct {
149173
networkAPI NetworkAPI
@@ -296,22 +320,15 @@ func (sm *StateModule) GetReadProof(
296320
}
297321

298322
// GetRuntimeVersion Get the runtime version at a given block.
299-
// If no block hash is provided, the latest version gets returned.
323+
// If no block hash is provided, the latest version gets returned.
300324
func (sm *StateModule) GetRuntimeVersion(
301325
_ *http.Request, req *StateRuntimeVersionRequest, res *StateRuntimeVersionResponse) error {
302326
rtVersion, err := sm.coreAPI.GetRuntimeVersion(req.Bhash)
303327
if err != nil {
304328
return err
305329
}
306330

307-
res.SpecName = string(rtVersion.SpecName)
308-
res.ImplName = string(rtVersion.ImplName)
309-
res.AuthoringVersion = rtVersion.AuthoringVersion
310-
res.SpecVersion = rtVersion.SpecVersion
311-
res.ImplVersion = rtVersion.ImplVersion
312-
res.TransactionVersion = rtVersion.TransactionVersion
313-
res.Apis = ConvertAPIs(rtVersion.APIItems)
314-
331+
*res = NewStateRuntimeVersionResponse(rtVersion)
315332
return nil
316333
}
317334

@@ -489,13 +506,3 @@ func (*StateModule) SubscribeStorage(
489506
_ *http.Request, _ *StateStorageQueryRangeRequest, _ *StorageChangeSetResponse) error {
490507
return nil
491508
}
492-
493-
// ConvertAPIs runtime.APIItems to []interface
494-
func ConvertAPIs(in []runtime.APIItem) []interface{} {
495-
ret := make([]interface{}, 0)
496-
for _, item := range in {
497-
encStr := hex.EncodeToString(item.Name[:])
498-
ret = append(ret, []interface{}{"0x" + encStr, item.Ver})
499-
}
500-
return ret
501-
}

dot/rpc/subscription/listeners.go

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -409,16 +409,11 @@ func (l *RuntimeVersionListener) Listen() {
409409
if err != nil {
410410
return
411411
}
412-
ver := modules.StateRuntimeVersionResponse{}
413-
ver.SpecName = string(rtVersion.SpecName)
414-
ver.ImplName = string(rtVersion.ImplName)
415-
ver.AuthoringVersion = rtVersion.AuthoringVersion
416-
ver.SpecVersion = rtVersion.SpecVersion
417-
ver.ImplVersion = rtVersion.ImplVersion
418-
ver.TransactionVersion = rtVersion.TransactionVersion
419-
ver.Apis = modules.ConvertAPIs(rtVersion.APIItems)
420412

421-
go l.wsconn.safeSend(newSubscriptionResponse(stateRuntimeVersionMethod, l.subID, ver))
413+
versionResponse := modules.NewStateRuntimeVersionResponse(rtVersion)
414+
subscriptionResponse := newSubscriptionResponse(
415+
stateRuntimeVersionMethod, l.subID, versionResponse)
416+
go l.wsconn.safeSend(subscriptionResponse)
422417

423418
// listen for runtime updates
424419
go func() {
@@ -428,17 +423,10 @@ func (l *RuntimeVersionListener) Listen() {
428423
return
429424
}
430425

431-
ver := modules.StateRuntimeVersionResponse{}
432-
433-
ver.SpecName = string(info.SpecName)
434-
ver.ImplName = string(info.ImplName)
435-
ver.AuthoringVersion = info.AuthoringVersion
436-
ver.SpecVersion = info.SpecVersion
437-
ver.ImplVersion = info.ImplVersion
438-
ver.TransactionVersion = info.TransactionVersion
439-
ver.Apis = modules.ConvertAPIs(info.APIItems)
440-
441-
l.wsconn.safeSend(newSubscriptionResponse(stateRuntimeVersionMethod, l.subID, ver))
426+
versionResponse := modules.NewStateRuntimeVersionResponse(info)
427+
subscriptionResponse := newSubscriptionResponse(
428+
stateRuntimeVersionMethod, l.subID, versionResponse)
429+
l.wsconn.safeSend(subscriptionResponse)
442430
}
443431
}()
444432
}

dot/rpc/subscription/listeners_test.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ func TestRuntimeChannelListener_Listen(t *testing.T) {
344344

345345
expectedInitialVersion := modules.StateRuntimeVersionResponse{
346346
SpecName: "mock-spec",
347-
Apis: modules.ConvertAPIs(nil),
347+
Apis: []interface{}{},
348348
}
349349

350350
expectedInitialResponse := newSubcriptionBaseResponseJSON()
@@ -365,7 +365,20 @@ func TestRuntimeChannelListener_Listen(t *testing.T) {
365365
SpecVersion: 25,
366366
ImplVersion: 0,
367367
TransactionVersion: 5,
368-
Apis: modules.ConvertAPIs(version.APIItems),
368+
Apis: []interface{}{
369+
[]interface{}{"0xdf6acb689907609b", uint32(0x3)},
370+
[]interface{}{"0x37e397fc7c91f5e4", uint32(0x1)},
371+
[]interface{}{"0x40fe3ad401f8959a", uint32(0x4)},
372+
[]interface{}{"0xd2bc9897eed08f15", uint32(0x2)},
373+
[]interface{}{"0xf78b278be53f454c", uint32(0x2)},
374+
[]interface{}{"0xaf2c0297a23e6d3d", uint32(0x1)},
375+
[]interface{}{"0xed99c5acb25eedf5", uint32(0x2)},
376+
[]interface{}{"0xcbca25e39f142387", uint32(0x2)},
377+
[]interface{}{"0x687ad44ad37f03c2", uint32(0x1)},
378+
[]interface{}{"0xab3c0572291feb8b", uint32(0x1)},
379+
[]interface{}{"0xbc9d89904f5b923f", uint32(0x1)},
380+
[]interface{}{"0x37c8bb1350a9a2a8", uint32(0x1)},
381+
},
369382
}
370383

371384
expectedUpdateResponse := newSubcriptionBaseResponseJSON()

0 commit comments

Comments
 (0)