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 151 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: 14 additions & 2 deletions cmd/gossamer/commands/import_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ 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().Uint32("state-version", uint32(trie.DefaultStateVersion), "State version to use when importing state") //nolint:lll
dimartiro marked this conversation as resolved.
Show resolved Hide resolved
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 +28,8 @@ 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 1 --header-file header.json
--first-slot <first slot of network>`,
dimartiro marked this conversation as resolved.
Show resolved Hide resolved
RunE: func(cmd *cobra.Command, args []string) error {
return execImportState(cmd)
},
Expand Down Expand Up @@ -54,6 +57,15 @@ func execImportState(cmd *cobra.Command) error {
return fmt.Errorf("state-file must be specified")
}

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

headerFile, err := cmd.Flags().GetString("header-file")
if err != nil {
return fmt.Errorf("failed to get header-file: %s", err)
Expand All @@ -64,5 +76,5 @@ func execImportState(cmd *cobra.Command) error {

basePath = utils.ExpandDir(basePath)

return dot.ImportState(basePath, stateFile, headerFile, firstSlot)
return dot.ImportState(basePath, stateFile, headerFile, stateTrieVersion, firstSlot)
}
57 changes: 57 additions & 0 deletions cmd/gossamer/commands/import_state_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// 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 TestImportStateEmptyHeaderFile(t *testing.T) {
rootCmd, err := NewRootCommand()
require.NoError(t, err)
rootCmd.AddCommand(ImportStateCmd)

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

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

rootCmd.SetArgs([]string{ImportStateCmd.Name(),
"--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 @@ -57,7 +57,7 @@ func createTestService(t *testing.T, genesisFilePath string,
require.NoError(t, err)

genesisHeader := &types.Header{
StateRoot: genesisTrie.MustHash(),
StateRoot: trie.V0.MustHash(genesisTrie),
Number: 0,
}

Expand Down Expand Up @@ -271,7 +271,7 @@ func newWestendLocalWithTrieAndHeader(t *testing.T) (
require.NoError(t, err)

parentHash := common.NewHash([]byte{0})
stateRoot := genesisTrie.MustHash()
stateRoot := trie.V0.MustHash(genesisTrie)
extrinsicRoot := trie.EmptyHash
const number = 0
digest := types.NewDigest()
Expand Down
25 changes: 23 additions & 2 deletions dot/core/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
rtstorage "github.com/ChainSafe/gossamer/lib/runtime/storage"
wazero_runtime "github.com/ChainSafe/gossamer/lib/runtime/wazero"
"github.com/ChainSafe/gossamer/lib/transaction"
"github.com/ChainSafe/gossamer/lib/trie"

cscale "github.com/centrifuge/go-substrate-rpc-client/v4/scale"
ctypes "github.com/centrifuge/go-substrate-rpc-client/v4/types"
Expand Down Expand Up @@ -116,14 +117,34 @@ func (s *Service) Stop() error {
return nil
}

func (s *Service) GetCurrentStateTrieVersion() (trie.Version, error) {
dimartiro marked this conversation as resolved.
Show resolved Hide resolved
bestBlockHash := s.blockState.BestBlockHash()
rt, err := s.blockState.GetRuntime(bestBlockHash)
if err != nil {
return trie.NoVersion, err
}

runtimeVersion, err := rt.Version()
if err != nil {
return trie.NoVersion, err
}

return trie.ParseVersion(runtimeVersion.StateVersion)
}

// StorageRoot returns the hash of the storage root
func (s *Service) StorageRoot() (common.Hash, error) {
ts, err := s.storageState.TrieState(nil)
if err != nil {
return common.Hash{}, err
}

return ts.Root()
stateTrieVersion, err := s.GetCurrentStateTrieVersion()
if err != nil {
return common.Hash{}, err
}

return stateTrieVersion.Hash(ts.Trie())
}

// HandleBlockImport handles a block that was imported via the network
Expand Down Expand Up @@ -226,7 +247,7 @@ func (s *Service) handleBlock(block *types.Block, state *rtstorage.TrieState) er
}

logger.Debugf("imported block %s and stored state trie with root %s",
block.Header.Hash(), state.MustRoot())
block.Header.Hash(), state.MustRoot(trie.NoMaxInlineValueSize))

parentRuntimeInstance, err := s.blockState.GetRuntime(block.Header.ParentHash)
if err != nil {
Expand Down
29 changes: 28 additions & 1 deletion dot/core/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ func Test_Service_StorageRoot(t *testing.T) {
retErr error
expErr error
expErrMsg string
stateVersion uint32
}{
{
name: "storage trie state error",
Expand All @@ -147,12 +148,22 @@ func Test_Service_StorageRoot(t *testing.T) {
trieStateCall: true,
},
{
name: "storage trie state ok",
name: "storage trie state ok v0",
service: &Service{},
exp: common.Hash{0x3, 0x17, 0xa, 0x2e, 0x75, 0x97, 0xb7, 0xb7, 0xe3, 0xd8, 0x4c, 0x5, 0x39, 0x1d, 0x13, 0x9a,
0x62, 0xb1, 0x57, 0xe7, 0x87, 0x86, 0xd8, 0xc0, 0x82, 0xf2, 0x9d, 0xcf, 0x4c, 0x11, 0x13, 0x14},
retTrieState: ts,
trieStateCall: true,
stateVersion: 0,
},
{
name: "storage trie state ok v1",
service: &Service{},
exp: common.Hash{0x3, 0x17, 0xa, 0x2e, 0x75, 0x97, 0xb7, 0xb7, 0xe3, 0xd8, 0x4c, 0x5, 0x39, 0x1d, 0x13, 0x9a,
0x62, 0xb1, 0x57, 0xe7, 0x87, 0x86, 0xd8, 0xc0, 0x82, 0xf2, 0x9d, 0xcf, 0x4c, 0x11, 0x13, 0x14},
retTrieState: ts,
trieStateCall: true,
stateVersion: 1,
},
}
for _, tt := range tests {
Expand All @@ -164,7 +175,23 @@ func Test_Service_StorageRoot(t *testing.T) {
ctrl := gomock.NewController(t)
mockStorageState := NewMockStorageState(ctrl)
mockStorageState.EXPECT().TrieState(nil).Return(tt.retTrieState, tt.retErr)

service.storageState = mockStorageState

if tt.retErr == nil {
mockRuntimeVersion := runtime.Version{
StateVersion: tt.stateVersion,
}

mockRuntime := NewMockInstance(ctrl)
mockRuntime.EXPECT().Version().Return(mockRuntimeVersion, nil)

mockBlockState := NewMockBlockState(ctrl)
mockBlockState.EXPECT().BestBlockHash().Return(common.Hash{})
mockBlockState.EXPECT().GetRuntime(gomock.Any()).Return(mockRuntime, nil)

service.blockState = mockBlockState
}
}

res, err := service.StorageRoot()
Expand Down
5 changes: 4 additions & 1 deletion dot/digest/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ func newWestendDevGenesisWithTrieAndHeader(t *testing.T) (
require.NoError(t, err)

parentHash := common.NewHash([]byte{0})
stateRoot := genesisTrie.MustHash()

// We are using state trie V0 since we are using the genesis trie where v0 is used
stateRoot := trie.V0.MustHash(genesisTrie)

extrinsicRoot := trie.EmptyHash
const number = 0
digest := types.NewDigest()
Expand Down
5 changes: 4 additions & 1 deletion dot/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ func newWestendDevGenesisWithTrieAndHeader(t *testing.T) (
require.NoError(t, err)

parentHash := common.NewHash([]byte{0})
stateRoot := genesisTrie.MustHash()

// We are using state trie V0 since we are using the genesis trie where v0 is used
stateRoot := trie.V0.MustHash(genesisTrie)

extrinsicRoot := trie.EmptyHash
const number = 0
digest := types.NewDigest()
Expand Down
4 changes: 2 additions & 2 deletions dot/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ 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 {
func ImportState(basepath, stateFP, headerFP string, stateTrieVersion trie.Version, firstSlot uint64) error {
tr, err := newTrieFromPairs(stateFP)
if err != nil {
return err
Expand All @@ -38,7 +38,7 @@ func ImportState(basepath, stateFP, headerFP string, firstSlot uint64) error {
LogLevel: log.Info,
}
srv := state.NewService(config)
return srv.Import(header, tr, firstSlot)
return srv.Import(header, tr, stateTrieVersion, firstSlot)
}

func newTrieFromPairs(filename string) (*trie.Trie, error) {
Expand Down
47 changes: 29 additions & 18 deletions dot/import_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ 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"
Expand All @@ -22,20 +23,28 @@ func Test_newTrieFromPairs(t *testing.T) {
t.Parallel()

tests := []struct {
name string
filename string
want common.Hash
err error
name string
filename string
want common.Hash
stateVersion trie.Version
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"),
name: "working example",
filename: setupStateFile(t),
want: common.MustHexToHash("0x09f9ca28df0560c2291aa16b56e15e07d1e1927088f51356d522722aa90ca7cb"),
stateVersion: trie.V0,
},
{
name: "working example",
filename: setupStateFile(t),
want: common.MustHexToHash("0xcc25fe024a58297658e576e2e4c33691fe3a9fe5a7cdd2e55534164a0fcc0782"),
stateVersion: trie.V1,
},
}
for _, tt := range tests {
Expand All @@ -52,7 +61,7 @@ func Test_newTrieFromPairs(t *testing.T) {
if tt.want.IsEmpty() {
assert.Nil(t, got)
} else {
assert.Equal(t, tt.want, got.MustHash())
assert.Equal(t, tt.want, tt.stateVersion.MustHash(*got))
}
})
}
Expand Down Expand Up @@ -93,7 +102,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, trie.V0, firstSlot)
require.NoError(t, err)
// confirm data is imported into db
stateConfig := state.Config{
Expand Down Expand Up @@ -124,10 +133,11 @@ func TestImportState(t *testing.T) {
headerFP := setupHeaderFile(t)

type args struct {
basepath string
stateFP string
headerFP string
firstSlot uint64
basepath string
stateFP string
headerFP string
stateVersion trie.Version
firstSlot uint64
}
tests := []struct {
name string
Expand All @@ -141,10 +151,11 @@ func TestImportState(t *testing.T) {
{
name: "working_example",
args: args{
basepath: config.BasePath,
stateFP: stateFP,
headerFP: headerFP,
firstSlot: 262493679,
basepath: config.BasePath,
stateFP: stateFP,
headerFP: headerFP,
stateVersion: trie.V0,
firstSlot: 262493679,
},
},
}
Expand All @@ -153,7 +164,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.stateVersion, tt.args.firstSlot)
if tt.err != nil {
assert.EqualError(t, err, tt.err.Error())
} else {
Expand Down
2 changes: 1 addition & 1 deletion dot/node_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ func TestInitNode_LoadStorageRoot(t *testing.T) {
expected, err := trie.LoadFromMap(gen.GenesisFields().Raw["top"])
require.NoError(t, err)

expectedRoot, err := expected.Hash()
expectedRoot, err := trie.V0.Hash(&expected) // Since we are using a runtime with state trie V0
require.NoError(t, err)

coreServiceInterface := node.ServiceRegistry.Get(&core.Service{})
Expand Down
2 changes: 1 addition & 1 deletion dot/rpc/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func newWestendDevGenesisWithTrieAndHeader(t *testing.T) (
require.NoError(t, err)

parentHash := common.NewHash([]byte{0})
stateRoot := genesisTrie.MustHash()
stateRoot := trie.V0.MustHash(genesisTrie)
extrinsicRoot := trie.EmptyHash
const number = 0
digest := types.NewDigest()
Expand Down
Loading
Loading