Skip to content

Commit

Permalink
chore(dot/rpc): NewStateRuntimeVersionResponse (#2740)
Browse files Browse the repository at this point in the history
- 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`
  • Loading branch information
qdm12 authored Aug 16, 2022
1 parent 83eac8a commit 31383eb
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 41 deletions.
45 changes: 26 additions & 19 deletions dot/rpc/modules/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,30 @@ type StateRuntimeVersionResponse struct {
Apis []interface{} `json:"apis"`
}

// NewStateRuntimeVersionResponse converts a runtime.Version to a
// StateRuntimeVersionResponse struct.
func NewStateRuntimeVersionResponse(runtimeVersion runtime.Version) (
response StateRuntimeVersionResponse) {
apisResponse := make([]interface{}, len(runtimeVersion.APIItems))
for i, apiItem := range runtimeVersion.APIItems {
hexItemName := hex.EncodeToString(apiItem.Name[:])
apisResponse[i] = []interface{}{
"0x" + hexItemName,
apiItem.Ver,
}
}

return StateRuntimeVersionResponse{
SpecName: string(runtimeVersion.SpecName),
ImplName: string(runtimeVersion.ImplName),
AuthoringVersion: runtimeVersion.AuthoringVersion,
SpecVersion: runtimeVersion.SpecVersion,
ImplVersion: runtimeVersion.ImplVersion,
TransactionVersion: runtimeVersion.TransactionVersion,
Apis: apisResponse,
}
}

// StateModule is an RPC module providing access to storage API points.
type StateModule struct {
networkAPI NetworkAPI
Expand Down Expand Up @@ -296,22 +320,15 @@ func (sm *StateModule) GetReadProof(
}

// GetRuntimeVersion Get the runtime version at a given block.
// If no block hash is provided, the latest version gets returned.
// If no block hash is provided, the latest version gets returned.
func (sm *StateModule) GetRuntimeVersion(
_ *http.Request, req *StateRuntimeVersionRequest, res *StateRuntimeVersionResponse) error {
rtVersion, err := sm.coreAPI.GetRuntimeVersion(req.Bhash)
if err != nil {
return err
}

res.SpecName = string(rtVersion.SpecName)
res.ImplName = string(rtVersion.ImplName)
res.AuthoringVersion = rtVersion.AuthoringVersion
res.SpecVersion = rtVersion.SpecVersion
res.ImplVersion = rtVersion.ImplVersion
res.TransactionVersion = rtVersion.TransactionVersion
res.Apis = ConvertAPIs(rtVersion.APIItems)

*res = NewStateRuntimeVersionResponse(rtVersion)
return nil
}

Expand Down Expand Up @@ -489,13 +506,3 @@ func (*StateModule) SubscribeStorage(
_ *http.Request, _ *StateStorageQueryRangeRequest, _ *StorageChangeSetResponse) error {
return nil
}

// ConvertAPIs runtime.APIItems to []interface
func ConvertAPIs(in []runtime.APIItem) []interface{} {
ret := make([]interface{}, 0)
for _, item := range in {
encStr := hex.EncodeToString(item.Name[:])
ret = append(ret, []interface{}{"0x" + encStr, item.Ver})
}
return ret
}
28 changes: 8 additions & 20 deletions dot/rpc/subscription/listeners.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,16 +409,11 @@ func (l *RuntimeVersionListener) Listen() {
if err != nil {
return
}
ver := modules.StateRuntimeVersionResponse{}
ver.SpecName = string(rtVersion.SpecName)
ver.ImplName = string(rtVersion.ImplName)
ver.AuthoringVersion = rtVersion.AuthoringVersion
ver.SpecVersion = rtVersion.SpecVersion
ver.ImplVersion = rtVersion.ImplVersion
ver.TransactionVersion = rtVersion.TransactionVersion
ver.Apis = modules.ConvertAPIs(rtVersion.APIItems)

go l.wsconn.safeSend(newSubscriptionResponse(stateRuntimeVersionMethod, l.subID, ver))
versionResponse := modules.NewStateRuntimeVersionResponse(rtVersion)
subscriptionResponse := newSubscriptionResponse(
stateRuntimeVersionMethod, l.subID, versionResponse)
go l.wsconn.safeSend(subscriptionResponse)

// listen for runtime updates
go func() {
Expand All @@ -428,17 +423,10 @@ func (l *RuntimeVersionListener) Listen() {
return
}

ver := modules.StateRuntimeVersionResponse{}

ver.SpecName = string(info.SpecName)
ver.ImplName = string(info.ImplName)
ver.AuthoringVersion = info.AuthoringVersion
ver.SpecVersion = info.SpecVersion
ver.ImplVersion = info.ImplVersion
ver.TransactionVersion = info.TransactionVersion
ver.Apis = modules.ConvertAPIs(info.APIItems)

l.wsconn.safeSend(newSubscriptionResponse(stateRuntimeVersionMethod, l.subID, ver))
versionResponse := modules.NewStateRuntimeVersionResponse(info)
subscriptionResponse := newSubscriptionResponse(
stateRuntimeVersionMethod, l.subID, versionResponse)
l.wsconn.safeSend(subscriptionResponse)
}
}()
}
Expand Down
17 changes: 15 additions & 2 deletions dot/rpc/subscription/listeners_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ func TestRuntimeChannelListener_Listen(t *testing.T) {

expectedInitialVersion := modules.StateRuntimeVersionResponse{
SpecName: "mock-spec",
Apis: modules.ConvertAPIs(nil),
Apis: []interface{}{},
}

expectedInitialResponse := newSubcriptionBaseResponseJSON()
Expand All @@ -365,7 +365,20 @@ func TestRuntimeChannelListener_Listen(t *testing.T) {
SpecVersion: 25,
ImplVersion: 0,
TransactionVersion: 5,
Apis: modules.ConvertAPIs(version.APIItems),
Apis: []interface{}{
[]interface{}{"0xdf6acb689907609b", uint32(0x3)},
[]interface{}{"0x37e397fc7c91f5e4", uint32(0x1)},
[]interface{}{"0x40fe3ad401f8959a", uint32(0x4)},
[]interface{}{"0xd2bc9897eed08f15", uint32(0x2)},
[]interface{}{"0xf78b278be53f454c", uint32(0x2)},
[]interface{}{"0xaf2c0297a23e6d3d", uint32(0x1)},
[]interface{}{"0xed99c5acb25eedf5", uint32(0x2)},
[]interface{}{"0xcbca25e39f142387", uint32(0x2)},
[]interface{}{"0x687ad44ad37f03c2", uint32(0x1)},
[]interface{}{"0xab3c0572291feb8b", uint32(0x1)},
[]interface{}{"0xbc9d89904f5b923f", uint32(0x1)},
[]interface{}{"0x37c8bb1350a9a2a8", uint32(0x1)},
},
}

expectedUpdateResponse := newSubcriptionBaseResponseJSON()
Expand Down

0 comments on commit 31383eb

Please sign in to comment.