-
Notifications
You must be signed in to change notification settings - Fork 111
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
feat: dot/state: implement import-state subcommand #1455
Merged
Merged
Changes from 8 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
56f84c6
import-state functional w/ state, header, firstSlot args
noot ad222ed
fix epoch verification issues
noot 1380cc5
Merge branch 'development' of github.com:ChainSafe/gossamer into noot…
noot 079a5e9
add flags, cleanup
noot 875a270
add tests
noot 25bad4f
Merge branch 'development' of github.com:ChainSafe/gossamer into noot…
noot 10f215b
fix tests
noot 651954a
address comments
noot 7062ce6
Merge branch 'development' into noot/state-import
noot f053767
fix babe verifier
noot cbcf1c4
Merge branch 'noot/state-import' of github.com:ChainSafe/gossamer int…
noot be1ac76
Merge branch 'development' into noot/state-import
noot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
// Copyright 2019 ChainSafe Systems (ON) Corp. | ||
// This file is part of gossamer. | ||
// | ||
// The gossamer library is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Lesser General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// The gossamer library is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Lesser General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Lesser General Public License | ||
// along with the gossamer library. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
package dot | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"errors" | ||
"io/ioutil" | ||
"math/big" | ||
"path/filepath" | ||
|
||
"github.com/ChainSafe/gossamer/dot/state" | ||
"github.com/ChainSafe/gossamer/dot/types" | ||
"github.com/ChainSafe/gossamer/lib/common" | ||
"github.com/ChainSafe/gossamer/lib/trie" | ||
|
||
log "github.com/ChainSafe/log15" | ||
) | ||
|
||
// 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) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
header, err := newHeaderFromFile(headerFP) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
log.Info("ImportState", "header", header) | ||
|
||
srv := state.NewService(basepath, log.LvlInfo) | ||
return srv.Import(header, tr, firstSlot) | ||
} | ||
|
||
func newTrieFromPairs(filename string) (*trie.Trie, error) { | ||
data, err := ioutil.ReadFile(filepath.Clean(filename)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
pairs := make([]interface{}, 0) | ||
err = json.Unmarshal(data, &pairs) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
entries := make(map[string]string) | ||
for _, pair := range pairs { | ||
pairArr := pair.([]interface{}) | ||
if len(pairArr) != 2 { | ||
return nil, errors.New("state file contains invalid pair") | ||
} | ||
entries[pairArr[0].(string)] = pairArr[1].(string) | ||
} | ||
|
||
tr := trie.NewEmptyTrie() | ||
err = tr.LoadFromMap(entries) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return tr, nil | ||
} | ||
|
||
func newHeaderFromFile(filename string) (*types.Header, error) { | ||
data, err := ioutil.ReadFile(filepath.Clean(filename)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
jsonHeader := make(map[string]interface{}) | ||
err = json.Unmarshal(data, &jsonHeader) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
hexNum, ok := jsonHeader["number"].(string) | ||
if !ok { | ||
return nil, errors.New("invalid number field in header JSON") | ||
} | ||
|
||
numBytes := common.MustHexToBytes(hexNum) | ||
num := big.NewInt(0).SetBytes(numBytes) | ||
|
||
parentHashStr, ok := jsonHeader["parentHash"].(string) | ||
if !ok { | ||
return nil, errors.New("invalid parentHash field in header JSON") | ||
} | ||
parentHash := common.MustHexToHash(parentHashStr) | ||
|
||
stateRootStr, ok := jsonHeader["stateRoot"].(string) | ||
if !ok { | ||
return nil, errors.New("invalid stateRoot field in header JSON") | ||
} | ||
stateRoot := common.MustHexToHash(stateRootStr) | ||
|
||
extrinsicsRootStr, ok := jsonHeader["extrinsicsRoot"].(string) | ||
if !ok { | ||
return nil, errors.New("invalid extrinsicsRoot field in header JSON") | ||
} | ||
extrinsicsRoot := common.MustHexToHash(extrinsicsRootStr) | ||
|
||
digestRaw, ok := jsonHeader["digest"].(map[string]interface{}) | ||
if !ok { | ||
return nil, errors.New("invalid digest field in header JSON") | ||
} | ||
logs := digestRaw["logs"].([]interface{}) | ||
|
||
digest := types.Digest{} | ||
|
||
for _, log := range logs { | ||
digestBytes := common.MustHexToBytes(log.(string)) | ||
r := &bytes.Buffer{} | ||
_, _ = r.Write(digestBytes) | ||
digestItem, err := types.DecodeDigestItem(r) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
digest = append(digest, digestItem) | ||
} | ||
|
||
header := &types.Header{ | ||
ParentHash: parentHash, | ||
Number: num, | ||
StateRoot: stateRoot, | ||
ExtrinsicsRoot: extrinsicsRoot, | ||
Digest: digest, | ||
} | ||
|
||
return header, nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we assert as a safety check that the length of pairArr is always equal to two?