Skip to content

Commit

Permalink
Remove version Encode method
Browse files Browse the repository at this point in the history
- Update comment for `scale.Marshal` on `Version`
- Remove `Test_Version_Encode`
  • Loading branch information
qdm12 committed Aug 10, 2022
1 parent a2b9f95 commit b5dd0cd
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 64 deletions.
12 changes: 0 additions & 12 deletions lib/runtime/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,6 @@ type Version struct {
TransactionVersion uint32
}

// Encode returns the scale encoding of the version.
// Note the encoding contains all the latest Core_version fields as defined in
// https://spec.polkadot.network/#defn-rt-core-version
// In other words, decoding older version data with missing fields
// and then encoding it will result in a longer encoding due to the
// extra version fields. This however remains compatible since the
// version fields are still encoded in the same order and an older
// decoder would succeed with the longer encoding.
func (v *Version) Encode() (encoded []byte, err error) {
return scale.Marshal(*v)
}

var (
ErrDecodingVersionField = errors.New("decoding version field")
)
Expand Down
47 changes: 1 addition & 46 deletions lib/runtime/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,51 +24,6 @@ func concatBytes(slices [][]byte) (concatenated []byte) {
return concatenated
}

func Test_Version_Encode(t *testing.T) {
t.Parallel()

testCases := map[string]struct {
version Version
encoding []byte
errWrapped error
errMessage string
}{
"all optional fields set": {
version: Version{
SpecName: []byte{1},
ImplName: []byte{2},
AuthoringVersion: 3,
SpecVersion: 4,
ImplVersion: 5,
APIItems: []APIItem{{
Name: [8]byte{1, 2, 3, 4, 5, 6, 7, 8},
Ver: 6,
}},
TransactionVersion: 7,
},
encoding: []byte{
0x4, 0x1, 0x4, 0x2, 0x3, 0x0, 0x0, 0x0, 0x4, 0x0, 0x0, 0x0,
0x5, 0x0, 0x0, 0x0, 0x4, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
0x8, 0x6, 0x0, 0x0, 0x0, 0x7, 0x0, 0x0, 0x0},
},
}

for name, testCase := range testCases {
testCase := testCase
t.Run(name, func(t *testing.T) {
t.Parallel()

encoded, err := testCase.version.Encode()

assert.ErrorIs(t, err, testCase.errWrapped)
if testCase.errWrapped != nil {
require.EqualError(t, err, testCase.errMessage)
}
assert.Equal(t, testCase.encoding, encoded)
})
}
}

func Test_DecodeVersion(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -200,7 +155,7 @@ func Test_Version_Scale(t *testing.T) {
t.Run(name, func(t *testing.T) {
t.Parallel()

encoded, err := testCase.version.Encode()
encoded, err := scale.Marshal(testCase.version)

require.NoError(t, err)
require.Equal(t, testCase.encoding, encoded)
Expand Down
14 changes: 8 additions & 6 deletions lib/runtime/wasmer/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -964,12 +964,14 @@ func ext_misc_runtime_version_version_1(context unsafe.Pointer, dataSpan C.int64
return C.int64_t(out)
}

// Note: we must call the `Encode` method and NOT
// scale.Marshal or this one would encode the Version
// interface pointer instead of the actual struct implementation.
// Encode also respects the legacy boolean field of the version
// and encodes the version differently if it is set to true.
encodedData, err := version.Encode()
// Note the encoding contains all the latest Core_version fields as defined in
// https://spec.polkadot.network/#defn-rt-core-version
// In other words, decoding older version data with missing fields
// and then encoding it will result in a longer encoding due to the
// extra version fields. This however remains compatible since the
// version fields are still encoded in the same order and an older
// decoder would succeed with the longer encoding.
encodedData, err := scale.Marshal(version)
if err != nil {
logger.Errorf("failed to encode result: %s", err)
return 0
Expand Down

0 comments on commit b5dd0cd

Please sign in to comment.