Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(trie): Add support for trie V1 #3433

Merged
merged 164 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from 35 commits
Commits
Show all changes
164 commits
Select commit Hold shift + click to select a range
76767e9
feat(trie): Add v2 runtime functions
dimartiro Aug 8, 2023
155cc4b
Merge branch 'development' into diego/trieV1/runtime-functions
dimartiro Aug 9, 2023
f4ad738
chore(trie): version trie functions
dimartiro Aug 10, 2023
b0a36cc
Update tests
dimartiro Aug 10, 2023
3293693
Fix lint errors
dimartiro Aug 10, 2023
634f315
Fix deepsource checks
dimartiro Aug 10, 2023
1015ece
Revisit TODOs
dimartiro Aug 15, 2023
9d828c7
Add more tests
dimartiro Aug 15, 2023
b09f7a5
Fix linter
dimartiro Aug 15, 2023
8c8ed40
Merge remote-tracking branch 'origin/development' into diego/trieV1/v…
dimartiro Aug 15, 2023
5d25644
Fix linter
dimartiro Aug 15, 2023
a625bc4
Get hashed values
dimartiro Aug 16, 2023
40fccff
Check memory db put value error
dimartiro Aug 16, 2023
59107ce
Add memory db lock
dimartiro Aug 16, 2023
ce73de2
Fix linter
dimartiro Aug 16, 2023
e0dd691
Deepsource
dimartiro Aug 16, 2023
7e16064
Add default version
dimartiro Aug 16, 2023
61f0d8d
Fix doc
dimartiro Aug 16, 2023
7974017
Add test to cover case for #2329
dimartiro Aug 16, 2023
494a85b
Fix linter
dimartiro Aug 16, 2023
2703b93
chore(trie): version trie functions
dimartiro Aug 10, 2023
00d4231
Update tests
dimartiro Aug 10, 2023
447ccf5
Fix lint errors
dimartiro Aug 10, 2023
752ce6d
Fix deepsource checks
dimartiro Aug 10, 2023
0514501
Revisit TODOs
dimartiro Aug 15, 2023
a0b8dc1
Add more tests
dimartiro Aug 15, 2023
db45755
Fix linter
dimartiro Aug 15, 2023
5719b28
fix(dot/sync): rework on bootstrap/tip sync (#3227)
EclesioMeloJunior Aug 11, 2023
ae86ad0
chore(deps): bump github.com/multiformats/go-multiaddr from 0.10.1 to…
dependabot[bot] Aug 14, 2023
7046c94
chore(deps): bump github.com/go-playground/validator/v10 from 10.14.1…
dependabot[bot] Aug 15, 2023
9dc3af5
chore(deps): bump github.com/ethereum/go-ethereum from 1.12.0 to 1.12…
dependabot[bot] Aug 15, 2023
970e6aa
chore(deepsource): address Deepsource suggestions (#3436)
EclesioMeloJunior Aug 15, 2023
2ebf184
Fix linter
dimartiro Aug 15, 2023
1761922
Get hashed values
dimartiro Aug 16, 2023
7b4b6d1
Check memory db put value error
dimartiro Aug 16, 2023
5ec4460
Add memory db lock
dimartiro Aug 16, 2023
8d70d77
Fix linter
dimartiro Aug 16, 2023
461596e
Deepsource
dimartiro Aug 16, 2023
c9b42e9
Add default version
dimartiro Aug 16, 2023
503fc27
Fix doc
dimartiro Aug 16, 2023
3f6cdcc
Add test to cover case for #2329
dimartiro Aug 16, 2023
0c5f542
Fix linter
dimartiro Aug 16, 2023
3e3878a
Use state version param in runtime functions
dimartiro Aug 16, 2023
45394d7
Merge branch 'diego/trieV1/versioning' into diego/trieV1/runtime-func…
dimartiro Aug 16, 2023
4f8d81c
PR comments
dimartiro Aug 16, 2023
1b5c0d3
Merge branch 'development' of https://github.com/ChainSafe/gossamer i…
dimartiro Aug 16, 2023
2da48d7
Fix merge
dimartiro Aug 16, 2023
c0bda83
Merge branch 'diego/trieV1/versioning' into diego/trieV1/runtime-func…
dimartiro Aug 16, 2023
264ca68
Merge branch 'development' of https://github.com/ChainSafe/gossamer i…
dimartiro Aug 17, 2023
7e1e42f
Fix merge
dimartiro Aug 17, 2023
644dc20
Version child tries functions
dimartiro Aug 17, 2023
99626a3
Improve version test cover
dimartiro Aug 22, 2023
376a97e
Remove unused function
dimartiro Aug 22, 2023
130e9bb
Refactor tests
dimartiro Aug 22, 2023
15770b4
Add child storage tests
dimartiro Aug 22, 2023
f00678c
Add more tests to increase coverage
dimartiro Aug 23, 2023
8834add
Fix linters
dimartiro Aug 23, 2023
265c747
Merge branch 'diego/trieV1/versioning' into diego/trieV1/runtime-func…
dimartiro Aug 23, 2023
d4e2ac6
PR comments
dimartiro Aug 23, 2023
024965a
Increase coverage in child storage tests
dimartiro Aug 23, 2023
4f87801
Remove unused functions
dimartiro Aug 23, 2023
bc167e2
Merge branch 'diego/trieV1/versioning' of https://github.com/ChainSaf…
dimartiro Aug 23, 2023
84541d8
Removes todos and skip validations for unused params
dimartiro Aug 24, 2023
884a2ae
Merge branch 'development' of https://github.com/ChainSafe/gossamer i…
dimartiro Aug 28, 2023
b765f73
Merge branch 'diego/trieV1/versioning' into diego/trieV1/runtime-func…
dimartiro Aug 28, 2023
793d80c
Add new runtime functions tests
dimartiro Aug 28, 2023
4139160
Improve largest lines to not ignore linter
dimartiro Aug 29, 2023
d02b3d3
Merge branch 'development' of https://github.com/ChainSafe/gossamer i…
dimartiro Aug 30, 2023
19ffee8
Merge branch 'development' of https://github.com/ChainSafe/gossamer i…
dimartiro Aug 31, 2023
cfcb324
Fix trie snapshot creation copying internal db
dimartiro Sep 1, 2023
3da0d32
Fix DBGetter mock
dimartiro Sep 1, 2023
919468c
Remove comments
dimartiro Sep 4, 2023
b94c622
Fix get version in runtime functions
dimartiro Sep 5, 2023
c088287
Merge branch 'diego/trieV1/versioning' of https://github.com/ChainSaf…
dimartiro Sep 7, 2023
55c51b2
Merge branch 'development' of https://github.com/ChainSafe/gossamer i…
dimartiro Sep 7, 2023
19fd637
Merge branch 'development' of https://github.com/ChainSafe/gossamer i…
dimartiro Sep 7, 2023
48ccf91
Fix merge
dimartiro Sep 7, 2023
f695530
Improve version parsing and simplify checks
dimartiro Sep 8, 2023
31413a4
lint
dimartiro Sep 8, 2023
fd95c86
Merge branch 'diego/trieV1/versioning' into diego/trieV1/runtime-func…
dimartiro Sep 8, 2023
215d4b3
PR comments
dimartiro Sep 8, 2023
a5a10c4
Add comment
dimartiro Sep 8, 2023
bb1a1c4
Fixes
dimartiro Sep 8, 2023
0043e31
Rollback runtime constants changes
dimartiro Sep 8, 2023
28ee3c9
Remove unused version parse switch case
dimartiro Sep 8, 2023
a8f1d23
Change panic to test fail
dimartiro Sep 8, 2023
da95dd7
Merge branch 'development' of https://github.com/ChainSafe/gossamer i…
dimartiro Sep 12, 2023
39c3923
Use sharing db in trie
dimartiro Sep 12, 2023
c481b6a
Fix mocks
dimartiro Sep 12, 2023
8ac581c
Remove old in memory db implementation and replace it to pebble
dimartiro Sep 12, 2023
50f52de
Fix lint
dimartiro Sep 12, 2023
191ff3e
Fix fuzz test
dimartiro Sep 12, 2023
22b4216
Remove value nodes from db when hashed node is deleted
dimartiro Sep 13, 2023
ab2ee18
Merge branch 'diego/trieV1/versioning' of https://github.com/ChainSaf…
dimartiro Sep 13, 2023
8d7c6b6
Add comments
dimartiro Sep 13, 2023
233c2f2
Merge branch 'diego/trieV1/versioning' of https://github.com/ChainSaf…
dimartiro Sep 13, 2023
f32c939
Return empty array instead 0
dimartiro Sep 13, 2023
8e076bb
Fix return zero value
dimartiro Sep 13, 2023
c187943
Revert using pebble as in memory db for tries
dimartiro Sep 13, 2023
ce04c1f
Merge branch 'diego/trieV1/versioning' of https://github.com/ChainSaf…
dimartiro Sep 13, 2023
97ce272
Add more test cases
dimartiro Sep 14, 2023
16ea637
Merge branch 'diego/trieV1/runtime-functions' of https://github.com/C…
dimartiro Sep 14, 2023
26f3eb5
Fix documentations
dimartiro Sep 14, 2023
bf28117
Merge branch 'development' of https://github.com/ChainSafe/gossamer i…
dimartiro Sep 25, 2023
dfefbbf
Fix ClearChildStorage with versioning
dimartiro Sep 29, 2023
5f2ad5d
Merge branch 'development' of https://github.com/ChainSafe/gossamer i…
dimartiro Sep 29, 2023
1dbf91b
Fix tests
dimartiro Sep 29, 2023
436100c
Add todo reminder
dimartiro Oct 2, 2023
c2bdf2f
Fix merge trie nodes preserve isHashed property
dimartiro Oct 2, 2023
242bba6
Merge branch 'development' of https://github.com/ChainSafe/gossamer i…
dimartiro Oct 2, 2023
18c6a22
Merge branch 'development' of https://github.com/ChainSafe/gossamer i…
dimartiro Oct 3, 2023
6e1e612
Simplify runtime functions reusing v2 in v1
dimartiro Oct 3, 2023
1096a93
Reuse new root from entries function in ext_ordered_root
dimartiro Oct 3, 2023
4d6434a
Add tests for new root function
dimartiro Oct 3, 2023
d8df35e
Fix lint lll
dimartiro Oct 3, 2023
8c92fbb
Improve trie versions functions
dimartiro Oct 3, 2023
02928a0
Merge branch 'development' of https://github.com/ChainSafe/gossamer i…
dimartiro Oct 4, 2023
784f96e
Fix test
dimartiro Oct 4, 2023
f5a67b9
Merge branch 'development' of https://github.com/ChainSafe/gossamer i…
dimartiro Oct 8, 2023
a276576
PR comments
dimartiro Oct 8, 2023
0915077
Merge branch 'development' of https://github.com/ChainSafe/gossamer i…
dimartiro Oct 31, 2023
8cf3b4a
PR review comments
dimartiro Nov 6, 2023
a9a25e9
Add godoc for exported method and consts
dimartiro Nov 6, 2023
487e1ea
Do not store hashed values
dimartiro Nov 7, 2023
f0478c2
Remove DB
dimartiro Nov 7, 2023
b04de53
Use trie version to calculate hash only
dimartiro Nov 7, 2023
a16d3de
Fix import state command
dimartiro Nov 7, 2023
eda5f6c
Use default state version in helpers test
dimartiro Nov 7, 2023
3ec5be3
Merge branch 'development' of https://github.com/ChainSafe/gossamer i…
dimartiro Nov 7, 2023
a3db05c
Use default state version in helpers test
dimartiro Nov 7, 2023
50f9616
Revert import integration tests change
dimartiro Nov 7, 2023
ffa7157
Lint
dimartiro Nov 7, 2023
9ba1bee
Merge branch 'development' of https://github.com/ChainSafe/gossamer i…
dimartiro Nov 13, 2023
c72deb8
Change default version to 0
dimartiro Nov 13, 2023
93c29f7
Fix Test_newTrieFromPairs
dimartiro Nov 13, 2023
9f66a11
Add one more test case
dimartiro Nov 13, 2023
a47970a
Fix version test
dimartiro Nov 13, 2023
2970674
Fix deepsource checks
dimartiro Nov 13, 2023
34f4e6b
Fix deepsource checks
dimartiro Nov 13, 2023
39bcb46
Replace max int to constant
dimartiro Nov 13, 2023
0e4ab54
Use default state version in e2e tests
dimartiro Nov 13, 2023
8a7cc08
Change constant
dimartiro Nov 13, 2023
8c49535
Simplify database interface
dimartiro Nov 13, 2023
bd21886
Removes harcoded state trie version
dimartiro Nov 13, 2023
5946baf
Merge branch 'development' of https://github.com/ChainSafe/gossamer i…
dimartiro Nov 13, 2023
bc85310
Get state trie version from runtime
dimartiro Nov 13, 2023
1d1bd25
Move comment
dimartiro Nov 13, 2023
2648240
Add import state version
dimartiro Nov 13, 2023
2de3013
Change error message
dimartiro Nov 14, 2023
4181e0b
Merge branch 'development' into diego/trieV1/versioning
dimartiro Nov 14, 2023
6fa58b7
Merge branch 'development' into diego/trieV1/versioning
dimartiro Nov 15, 2023
337ab9c
Merge branch 'development' into diego/trieV1/versioning
dimartiro Nov 16, 2023
c56bd71
Removes Database interface
dimartiro Nov 17, 2023
2ff1a4e
Make GetCurrentStateTrieVersion private
dimartiro Nov 17, 2023
38b4b37
Rename trie version to layout
dimartiro Nov 17, 2023
14ad372
use NoMaxInlineValueSize
dimartiro Nov 17, 2023
2185edb
Fix mocks
dimartiro Nov 17, 2023
f4661c9
Fix tests using new runtime version parameter
dimartiro Nov 20, 2023
2cfbdca
Fix Test_ext_default_child_storage_root_version_2
dimartiro Nov 20, 2023
8f56a8d
Use right test runtime URL
dimartiro Nov 20, 2023
5b08816
Merge branch 'development' of https://github.com/ChainSafe/gossamer i…
dimartiro Nov 20, 2023
a747c37
Add missing err check
dimartiro Nov 20, 2023
8dcca66
Fix commands checks and doc
dimartiro Nov 21, 2023
2ce26f7
Merge branch 'development' into diego/trieV1/versioning
dimartiro Nov 21, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions cmd/gossamer/commands/import_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import (
"fmt"

"github.com/ChainSafe/gossamer/dot"
"github.com/ChainSafe/gossamer/lib/trie"
"github.com/ChainSafe/gossamer/lib/utils"
"github.com/spf13/cobra"
)

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().String("state-version", trie.DefaultStateVersion.String(), "State version v0 or v1")
ImportStateCmd.Flags().String("header-file", "", "Path to JSON file of block header corresponding to the given state")
ImportStateCmd.Flags().Uint64("first-slot", 0, "The first BABE slot of the network")
}
Expand All @@ -26,7 +27,7 @@ var ImportStateCmd = &cobra.Command{
in the form of key-value pairs to be imported.
Input can be generated by using the RPC function state_getPairs.
Example:
gossamer import-state --state-file state.json --header-file header.json --first-slot <first slot of network>`,
gossamer import-state --state-file state.json --state-version v1 --header-file header.json --first-slot <first slot of network>`, //nolint:lll
RunE: func(cmd *cobra.Command, args []string) error {
return execImportState(cmd)
},
Expand Down Expand Up @@ -62,7 +63,16 @@ func execImportState(cmd *cobra.Command) error {
return fmt.Errorf("header-file must be specified")
}

stateVersionFlag, err := cmd.Flags().GetString("state-version")
if err != nil {
return fmt.Errorf("failed to get state-version: %s", err)
}
stateVersion, err := trie.ParseVersion(stateVersionFlag)
if err != nil {
return fmt.Errorf("failed to parse state-version: %s", err)
dimartiro marked this conversation as resolved.
Show resolved Hide resolved
}

basePath = utils.ExpandDir(basePath)

return dot.ImportState(basePath, stateFile, headerFile, firstSlot)
return dot.ImportState(basePath, stateFile, headerFile, firstSlot, stateVersion)
}
83 changes: 83 additions & 0 deletions cmd/gossamer/commands/import_state_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright 2023 ChainSafe Systems (ON)
// SPDX-License-Identifier: LGPL-3.0-only

package commands

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestImportStateMissingStateFile(t *testing.T) {
rootCmd, err := NewRootCommand()
require.NoError(t, err)
rootCmd.AddCommand(ImportStateCmd)

rootCmd.SetArgs([]string{ImportStateCmd.Name()})
err = rootCmd.Execute()
assert.ErrorContains(t, err, "state-file must be specified")
}

func TestImportStateInvalidFirstSlot(t *testing.T) {
rootCmd, err := NewRootCommand()
require.NoError(t, err)
rootCmd.AddCommand(ImportStateCmd)

rootCmd.SetArgs([]string{ImportStateCmd.Name(), "--first-slot", "wrong"})
err = rootCmd.Execute()
assert.ErrorContains(t, err, "invalid argument \"wrong\"")
}

func TestImportStateEmptyStateFile(t *testing.T) {
rootCmd, err := NewRootCommand()
require.NoError(t, err)
rootCmd.AddCommand(ImportStateCmd)

rootCmd.SetArgs([]string{ImportStateCmd.Name(), "--state-version", "v0", "--state-file", ""})
err = rootCmd.Execute()
assert.ErrorContains(t, err, "state-file must be specified")
}

func TestImportStateEmptyHeaderFile(t *testing.T) {
rootCmd, err := NewRootCommand()
require.NoError(t, err)
rootCmd.AddCommand(ImportStateCmd)

rootCmd.SetArgs([]string{ImportStateCmd.Name(),
"--state-version", "v0",
"--state-file", "test",
"--header-file", "",
})
err = rootCmd.Execute()
assert.ErrorContains(t, err, "header-file must be specified")
}

func TestImportStateInvalidStateVersion(t *testing.T) {
rootCmd, err := NewRootCommand()
require.NoError(t, err)
rootCmd.AddCommand(ImportStateCmd)

rootCmd.SetArgs([]string{ImportStateCmd.Name(),
"--state-version", "v999",
"--state-file", "test",
"--header-file", "test",
})
err = rootCmd.Execute()
assert.ErrorContains(t, err, "failed to parse state-version: parsing version failed: \"v999\" must be one of [v0, v1]")
}

func TestImportStateErrorImportingState(t *testing.T) {
rootCmd, err := NewRootCommand()
require.NoError(t, err)
rootCmd.AddCommand(ImportStateCmd)

rootCmd.SetArgs([]string{ImportStateCmd.Name(),
"--state-version", "v0",
"--state-file", "test",
"--header-file", "test",
})
err = rootCmd.Execute()
assert.ErrorContains(t, err, "no such file or directory")
}
dimartiro marked this conversation as resolved.
Show resolved Hide resolved
4 changes: 2 additions & 2 deletions dot/core/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,9 @@ func createTestService(t *testing.T, genesisFilePath string,
cfgRuntime, err := wazero_runtime.NewRuntimeFromGenesis(rtCfg)
require.NoError(t, err)

cfgRuntime.Context.Storage.Put(aliceBalanceKey, encodedAccountInfo)
cfgRuntime.Context.Storage.Put(aliceBalanceKey, encodedAccountInfo, trie.V0)
// this key is System.UpgradedToDualRefCount -> set to true since all accounts have been upgraded to v0.9 format
cfgRuntime.Context.Storage.Put(common.UpgradedToDualRefKey, []byte{1})
cfgRuntime.Context.Storage.Put(common.UpgradedToDualRefKey, []byte{1}, trie.V0)

cfgBlockState.StoreRuntime(cfgBlockState.BestBlockHash(), cfgRuntime)

Expand Down
4 changes: 2 additions & 2 deletions dot/core/service_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ func createBlockUsingNewRuntime(t *testing.T, bestBlockHash common.Hash, newRunt
testRuntime, err := os.ReadFile(newRuntimePath)
require.NoError(t, err)

trieState.Put(common.CodeKey, testRuntime)
trieState.Put(common.CodeKey, testRuntime, trie.V0)

primaryDigestData := types.NewBabePrimaryPreDigest(0, uint64(1), [32]byte{}, [64]byte{})
digest := types.NewDigest()
Expand Down Expand Up @@ -673,7 +673,7 @@ func TestService_HandleRuntimeChangesAfterCodeSubstitutes(t *testing.T) {
ts, err = s.storageState.TrieState(nil)
require.NoError(t, err)

ts.Put(common.CodeKey, testRuntime)
ts.Put(common.CodeKey, testRuntime, trie.V0)
rtUpdateBhash := newBlock.Header.Hash()

// update runtime for new block
Expand Down
8 changes: 4 additions & 4 deletions dot/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (
)

// ImportState imports the state in the given files to the database with the given path.
func ImportState(basepath, stateFP, headerFP string, firstSlot uint64) error {
tr, err := newTrieFromPairs(stateFP)
func ImportState(basepath, stateFP, headerFP string, firstSlot uint64, stateVersion trie.Version) error {
tr, err := newTrieFromPairs(stateFP, stateVersion)
if err != nil {
return err
}
Expand All @@ -41,7 +41,7 @@ func ImportState(basepath, stateFP, headerFP string, firstSlot uint64) error {
return srv.Import(header, tr, firstSlot)
}

func newTrieFromPairs(filename string) (*trie.Trie, error) {
func newTrieFromPairs(filename string, stateVersion trie.Version) (*trie.Trie, error) {
data, err := os.ReadFile(filepath.Clean(filename))
if err != nil {
return nil, err
Expand All @@ -62,7 +62,7 @@ func newTrieFromPairs(filename string) (*trie.Trie, error) {
entries[pairArr[0].(string)] = pairArr[1].(string)
}

tr, err := trie.LoadFromMap(entries)
tr, err := trie.LoadFromMap(entries, stateVersion)
if err != nil {
return nil, err
}
Expand Down
46 changes: 4 additions & 42 deletions dot/import_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,51 +13,12 @@ import (
"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/internal/log"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/trie"
"github.com/ChainSafe/gossamer/pkg/scale"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

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

tests := []struct {
name string
filename string
want common.Hash
err error
}{
{
name: "no_arguments",
err: errors.New("read .: is a directory"),
want: common.Hash{},
},
{
name: "working example",
filename: setupStateFile(t),
want: common.MustHexToHash("0x09f9ca28df0560c2291aa16b56e15e07d1e1927088f51356d522722aa90ca7cb"),
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

got, err := newTrieFromPairs(tt.filename)
if tt.err != nil {
assert.EqualError(t, err, tt.err.Error())
} else {
assert.NoError(t, err)
}
if tt.want.IsEmpty() {
assert.Nil(t, got)
} else {
assert.Equal(t, tt.want, got.MustHash())
}
})
}
}

func TestNewHeaderFromFile(t *testing.T) {
fp := setupHeaderFile(t)
header, err := newHeaderFromFile(fp)
Expand Down Expand Up @@ -93,7 +54,7 @@ func TestImportState_Integration(t *testing.T) {
headerFP := setupHeaderFile(t)

const firstSlot = uint64(262493679)
err = ImportState(config.BasePath, stateFP, headerFP, firstSlot)
err = ImportState(config.BasePath, stateFP, headerFP, firstSlot, trie.V0)
require.NoError(t, err)
// confirm data is imported into db
stateConfig := state.Config{
Expand Down Expand Up @@ -126,6 +87,7 @@ func TestImportState(t *testing.T) {
type args struct {
basepath string
stateFP string
version trie.Version
headerFP string
firstSlot uint64
}
Expand Down Expand Up @@ -153,7 +115,7 @@ func TestImportState(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

err := ImportState(tt.args.basepath, tt.args.stateFP, tt.args.headerFP, tt.args.firstSlot)
err := ImportState(tt.args.basepath, tt.args.stateFP, tt.args.headerFP, tt.args.firstSlot, tt.args.version)
if tt.err != nil {
assert.EqualError(t, err, tt.err.Error())
} else {
Expand Down
43 changes: 43 additions & 0 deletions dot/import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/trie"
"github.com/ChainSafe/gossamer/lib/utils"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -139,3 +140,45 @@ func Test_newHeaderFromFile(t *testing.T) {
})
}
}

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

tests := []struct {
name string
filename string
version trie.Version
want common.Hash
err error
}{
{
name: "no_arguments",
err: errors.New("read .: is a directory"),
want: common.Hash{},
},
{
name: "working example",
filename: setupStateFile(t),
version: trie.V0,
want: common.MustHexToHash("0x09f9ca28df0560c2291aa16b56e15e07d1e1927088f51356d522722aa90ca7cb"),
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

got, err := newTrieFromPairs(tt.filename, tt.version)
if tt.err != nil {
assert.EqualError(t, err, tt.err.Error())
} else {
assert.NoError(t, err)
}
if tt.want.IsEmpty() {
assert.Nil(t, got)
} else {
assert.Equal(t, tt.want, got.MustHash())
}
})
}
}
2 changes: 1 addition & 1 deletion dot/node_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ func TestInitNode_LoadStorageRoot(t *testing.T) {
node, err := NewNode(config, ks)
require.NoError(t, err)

expected, err := trie.LoadFromMap(gen.GenesisFields().Raw["top"])
expected, err := trie.LoadFromMap(gen.GenesisFields().Raw["top"], trie.V0)
require.NoError(t, err)

expectedRoot, err := expected.Hash()
Expand Down
2 changes: 1 addition & 1 deletion dot/rpc/modules/author_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func useInstanceFromRuntimeV0929(t *testing.T, rtStorage *storage.TrieState) (in
bytes, err := os.ReadFile(testRuntimeFilePath)
require.NoError(t, err)

rtStorage.Put(common.CodeKey, bytes)
rtStorage.Put(common.CodeKey, bytes, trie.V0)

cfg := wazero_runtime.Config{
Role: 0,
Expand Down
12 changes: 6 additions & 6 deletions dot/rpc/modules/childstate_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,15 +244,15 @@ func setupChildStateStorage(t *testing.T) (*ChildStateModule, common.Hash) {
tr, err := st.Storage.TrieState(nil)
require.NoError(t, err)

tr.Put([]byte(":first_key"), []byte(":value1"))
tr.Put([]byte(":second_key"), []byte(":second_value"))
tr.Put([]byte(":first_key"), []byte(":value1"), trie.V0)
tr.Put([]byte(":second_key"), []byte(":second_value"), trie.V0)

childTr := trie.NewEmptyTrie()
childTr.Put([]byte(":child_first"), []byte(":child_first_value"))
childTr.Put([]byte(":child_second"), []byte(":child_second_value"))
childTr.Put([]byte(":another_child"), []byte("value"))
childTr.Put([]byte(":child_first"), []byte(":child_first_value"), trie.V0)
childTr.Put([]byte(":child_second"), []byte(":child_second_value"), trie.V0)
childTr.Put([]byte(":another_child"), []byte("value"), trie.V0)

err = tr.SetChild([]byte(":child_storage_key"), childTr)
err = tr.SetChild([]byte(":child_storage_key"), childTr, trie.V0)
require.NoError(t, err)

stateRoot, err := tr.Root()
Expand Down
12 changes: 6 additions & 6 deletions dot/rpc/modules/childstate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ func createTestTrieState(t *testing.T) (*trie.Trie, common.Hash) {
_, genesisTrie, _ := newWestendLocalGenesisWithTrieAndHeader(t)
tr := rtstorage.NewTrieState(&genesisTrie)

tr.Put([]byte(":first_key"), []byte(":value1"))
tr.Put([]byte(":second_key"), []byte(":second_value"))
tr.Put([]byte(":first_key"), []byte(":value1"), trie.V0)
tr.Put([]byte(":second_key"), []byte(":second_value"), trie.V0)

childTr := trie.NewEmptyTrie()
childTr.Put([]byte(":child_first"), []byte(":child_first_value"))
childTr.Put([]byte(":child_second"), []byte(":child_second_value"))
childTr.Put([]byte(":another_child"), []byte("value"))
childTr.Put([]byte(":child_first"), []byte(":child_first_value"), trie.V0)
childTr.Put([]byte(":child_second"), []byte(":child_second_value"), trie.V0)
childTr.Put([]byte(":another_child"), []byte("value"), trie.V0)

err := tr.SetChild([]byte(":child_storage_key"), childTr)
err := tr.SetChild([]byte(":child_storage_key"), childTr, trie.V0)
require.NoError(t, err)

stateRoot, err := tr.Root()
Expand Down
3 changes: 2 additions & 1 deletion dot/rpc/modules/dev_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ func newBABEService(t *testing.T) *babe.Service {
"0d4a9e054df4e01000000000000001cbd2d43530a44705ad088af313e18f80b53ef16b36177cd4b77b846f2a5f07c010000"+
"00000000004603307f855321776922daeea21ee31720388d097cdaac66f05a6f8462b317570100000000000000be1d9d59d"+
"e1283380100550a7b024501cb62d6cc40e3db35fcc5cf341814986e01000000000000001206960f920a23f7f4c43cc9081"+
"ec2ed0721f31a9bef2c10fd7602e16e08a32c0100000000000000"))
"ec2ed0721f31a9bef2c10fd7602e16e08a32c0100000000000000"),
trie.V0)

cfg := &babe.ServiceConfig{
BlockState: bs,
Expand Down
Loading