From c8fdb144cbdc31690c1d69900cf3d6f13fcddad5 Mon Sep 17 00:00:00 2001 From: Radosvet M Date: Fri, 1 Mar 2024 16:29:53 +0200 Subject: [PATCH] fix(state-version): should be uint8 instead of uint32 (#3779) --- cmd/gossamer/commands/import_state.go | 6 +++--- dot/core/service_test.go | 2 +- lib/runtime/version.go | 2 +- lib/runtime/version_test.go | 2 +- lib/runtime/wazero/imports.go | 8 ++++---- pkg/trie/layout.go | 4 ++-- pkg/trie/layout_test.go | 12 ++++++------ 7 files changed, 18 insertions(+), 18 deletions(-) diff --git a/cmd/gossamer/commands/import_state.go b/cmd/gossamer/commands/import_state.go index d262a72488..c83f267cbf 100644 --- a/cmd/gossamer/commands/import_state.go +++ b/cmd/gossamer/commands/import_state.go @@ -15,8 +15,8 @@ import ( func init() { ImportStateCmd.Flags().String("chain", "", "Chain id used to load default configuration for specified chain") ImportStateCmd.Flags().String("state-file", "", "Path to JSON file consisting of key-value pairs") - ImportStateCmd.Flags().Uint32("state-version", - uint32(trie.DefaultStateVersion), + ImportStateCmd.Flags().Uint8("state-version", + uint8(trie.DefaultStateVersion), "State version to use when importing state", ) ImportStateCmd.Flags().String("header-file", "", "Path to JSON file of block header corresponding to the given state") @@ -60,7 +60,7 @@ func execImportState(cmd *cobra.Command) error { return fmt.Errorf("state-file must be specified") } - stateVersion, err := cmd.Flags().GetUint32("state-version") + stateVersion, err := cmd.Flags().GetUint8("state-version") if err != nil { return fmt.Errorf("failed to get state-version: %s", err) } diff --git a/dot/core/service_test.go b/dot/core/service_test.go index cd7bd645b3..eb4f1be06f 100644 --- a/dot/core/service_test.go +++ b/dot/core/service_test.go @@ -135,7 +135,7 @@ func Test_Service_StorageRoot(t *testing.T) { retErr error expErr error expErrMsg string - stateVersion uint32 + stateVersion uint8 }{ { name: "storage trie state error", diff --git a/lib/runtime/version.go b/lib/runtime/version.go index 277b3b71b0..d5329991f1 100644 --- a/lib/runtime/version.go +++ b/lib/runtime/version.go @@ -28,7 +28,7 @@ type Version struct { ImplVersion uint32 APIItems []APIItem TransactionVersion uint32 - StateVersion uint32 + StateVersion uint8 } var ( diff --git a/lib/runtime/version_test.go b/lib/runtime/version_test.go index d553731795..76d21e2335 100644 --- a/lib/runtime/version_test.go +++ b/lib/runtime/version_test.go @@ -171,7 +171,7 @@ func Test_Version_Scale(t *testing.T) { 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, 0x4, 0x0, 0x0, 0x0}, + 0x8, 0x6, 0x0, 0x0, 0x0, 0x7, 0x0, 0x0, 0x0, 0x4}, decoded: Version{ SpecName: []byte{1}, ImplName: []byte{2}, diff --git a/lib/runtime/wazero/imports.go b/lib/runtime/wazero/imports.go index e05409b952..bb1f8666a8 100644 --- a/lib/runtime/wazero/imports.go +++ b/lib/runtime/wazero/imports.go @@ -795,7 +795,7 @@ func ext_trie_blake2_256_root_version_2(ctx context.Context, m api.Module, dataS panic("nil runtime context") } - stateVersion, err := trie.ParseVersion(version) + stateVersion, err := trie.ParseVersion(uint8(version)) if err != nil { logger.Errorf("failed parsing state version: %s", err) return 0 @@ -841,7 +841,7 @@ func ext_trie_blake2_256_ordered_root_version_2( data := read(m, dataSpan) - stateVersion, err := trie.ParseVersion(version) + stateVersion, err := trie.ParseVersion(uint8(version)) if err != nil { logger.Errorf("failed parsing state version: %s", err) return 0 @@ -923,7 +923,7 @@ func ext_trie_blake2_256_verify_proof_version_2( panic("nil runtime context") } - _, err := trie.ParseVersion(version) + _, err := trie.ParseVersion(uint8(version)) if err != nil { logger.Errorf("failed parsing state version: %s", err) return 0 @@ -1275,7 +1275,7 @@ func ext_default_child_storage_root_version_2(ctx context.Context, m api.Module, return mustWrite(m, rtCtx.Allocator, emptyByteVectorEncoded) } - stateVersion, err := trie.ParseVersion(version) + stateVersion, err := trie.ParseVersion(uint8(version)) if err != nil { logger.Errorf("failed parsing state version: %s", err) return 0 diff --git a/pkg/trie/layout.go b/pkg/trie/layout.go index f83f127a47..53d5e17f9e 100644 --- a/pkg/trie/layout.go +++ b/pkg/trie/layout.go @@ -99,12 +99,12 @@ func (v TrieLayout) MustHash(t Trie) common.Hash { } // ParseVersion parses a state trie version string. -func ParseVersion[T string | uint32](v T) (version TrieLayout, err error) { +func ParseVersion[T string | uint8](v T) (version TrieLayout, err error) { var s string switch value := any(v).(type) { case string: s = value - case uint32: + case uint8: s = fmt.Sprintf("V%d", value) } diff --git a/pkg/trie/layout_test.go b/pkg/trie/layout_test.go index b2562ad7ab..5d617dc180 100644 --- a/pkg/trie/layout_test.go +++ b/pkg/trie/layout_test.go @@ -64,7 +64,7 @@ func Test_ParseVersion(t *testing.T) { version: V0, }, "0": { - v: uint32(0), + v: uint8(0), version: V0, }, "v1": { @@ -76,7 +76,7 @@ func Test_ParseVersion(t *testing.T) { version: V1, }, "1": { - v: uint32(1), + v: uint8(1), version: V1, }, "invalid": { @@ -84,10 +84,10 @@ func Test_ParseVersion(t *testing.T) { errWrapped: ErrParseVersion, errMessage: "parsing version failed: \"xyz\" must be one of [v0, v1]", }, - "invalid_uint32": { - v: uint32(999), + "invalid_uint8": { + v: uint8(99), errWrapped: ErrParseVersion, - errMessage: "parsing version failed: \"V999\" must be one of [v0, v1]", + errMessage: "parsing version failed: \"V99\" must be one of [v0, v1]", }, } @@ -102,7 +102,7 @@ func Test_ParseVersion(t *testing.T) { switch typed := testCase.v.(type) { case string: version, err = ParseVersion(typed) - case uint32: + case uint8: version, err = ParseVersion(typed) default: t.Fail()