Skip to content

Commit

Permalink
Decode method -> DecodeVersion function
Browse files Browse the repository at this point in the history
  • Loading branch information
qdm12 committed Jul 20, 2022
1 parent 2c67b56 commit 54ed762
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 16 deletions.
20 changes: 8 additions & 12 deletions lib/runtime/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,39 +54,35 @@ func (v *Version) Encode() (encoded []byte, err error) {
return scale.Marshal(toEncode)
}

// Decode scale decodes the encoded version data and returns an error.
// DecodeVersion scale decodes the encoded version data and returns an error.
// It first tries to decode the data using the current version format.
// If that fails with an EOF error, it then tries to decode the data
// using the legacy version format (for Kusama).
func (v *Version) Decode(encoded []byte) (err error) {
var newVersionData Version
err = scale.Unmarshal(encoded, &newVersionData)
func DecodeVersion(encoded []byte) (version Version, err error) {
err = scale.Unmarshal(encoded, &version)
if err == nil {
*v = newVersionData
return nil
return version, nil
}

if !strings.Contains(err.Error(), "EOF") {
// TODO io.EOF should be wrapped in scale
return err
return version, err
}

// TODO: kusama seems to use the legacy version format
var legacy legacyData
err = scale.Unmarshal(encoded, &legacy)
if err != nil {
return fmt.Errorf("decoding legacy version: %w", err)
return version, fmt.Errorf("decoding legacy version: %w", err)
}

*v = Version{
return Version{
SpecName: legacy.SpecName,
ImplName: legacy.ImplName,
AuthoringVersion: legacy.AuthoringVersion,
SpecVersion: legacy.SpecVersion,
ImplVersion: legacy.ImplVersion,
APIItems: legacy.APIItems,
legacy: true,
}

return nil
}, nil
}
3 changes: 1 addition & 2 deletions lib/runtime/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,7 @@ func Test_VersionData_Scale(t *testing.T) {
require.NoError(t, err)
require.Equal(t, testCase.encoding, encoded)

var decoded Version
err = decoded.Decode(encoded)
decoded, err := DecodeVersion(encoded)
require.NoError(t, err)

assert.Equal(t, testCase.decoded, decoded)
Expand Down
4 changes: 2 additions & 2 deletions lib/runtime/wasmer/exports.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ func (in *Instance) Version() (version runtime.Version, err error) {
return version, err
}

err = version.Decode(res)
version, err = runtime.DecodeVersion(res)
if err != nil {
return version, fmt.Errorf("decoding: %w", err)
return version, fmt.Errorf("decoding version: %w", err)
}

return version, nil
Expand Down

0 comments on commit 54ed762

Please sign in to comment.