-
Notifications
You must be signed in to change notification settings - Fork 129
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
lib/trie: add Version
type
#2736
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright 2022 ChainSafe Systems (ON) | ||
// SPDX-License-Identifier: LGPL-3.0-only | ||
|
||
package trie | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
// Version is the state trie version which dictates how a | ||
// Merkle root should be constructed. It is defined in | ||
// https://spec.polkadot.network/#defn-state-version | ||
type Version uint8 | ||
|
||
const ( | ||
// V0 is the state trie version 0 where the values of the keys are | ||
// inserted into the trie directly. | ||
// TODO set to iota once CI passes | ||
V0 Version = 1 | ||
) | ||
|
||
func (v Version) String() string { | ||
switch v { | ||
case V0: | ||
return "v0" | ||
default: | ||
panic(fmt.Sprintf("unknown version %d", v)) | ||
} | ||
} | ||
|
||
var ErrParseVersion = errors.New("parsing version failed") | ||
|
||
// ParseVersion parses a state trie version string. | ||
func ParseVersion(s string) (version Version, err error) { | ||
switch { | ||
case strings.EqualFold(s, V0.String()): | ||
return V0, nil | ||
default: | ||
return version, fmt.Errorf("%w: %q must be %s", | ||
ErrParseVersion, s, V0) | ||
} | ||
} |
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,86 @@ | ||
// Copyright 2022 ChainSafe Systems (ON) | ||
// SPDX-License-Identifier: LGPL-3.0-only | ||
|
||
package trie | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_Version_String(t *testing.T) { | ||
t.Parallel() | ||
|
||
testCases := map[string]struct { | ||
version Version | ||
versionString string | ||
panicMessage string | ||
}{ | ||
"v0": { | ||
version: V0, | ||
versionString: "v0", | ||
}, | ||
"invalid": { | ||
version: Version(99), | ||
panicMessage: "unknown version 99", | ||
}, | ||
} | ||
|
||
for name, testCase := range testCases { | ||
testCase := testCase | ||
t.Run(name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
if testCase.panicMessage != "" { | ||
assert.PanicsWithValue(t, testCase.panicMessage, func() { | ||
_ = testCase.version.String() | ||
}) | ||
return | ||
} | ||
|
||
versionString := testCase.version.String() | ||
assert.Equal(t, testCase.versionString, versionString) | ||
}) | ||
} | ||
} | ||
|
||
func Test_ParseVersion(t *testing.T) { | ||
t.Parallel() | ||
|
||
testCases := map[string]struct { | ||
s string | ||
version Version | ||
errWrapped error | ||
errMessage string | ||
}{ | ||
"v0": { | ||
s: "v0", | ||
version: V0, | ||
}, | ||
"V0": { | ||
s: "V0", | ||
version: V0, | ||
}, | ||
"invalid": { | ||
s: "xyz", | ||
errWrapped: ErrParseVersion, | ||
errMessage: "parsing version failed: \"xyz\" must be v0", | ||
}, | ||
} | ||
|
||
for name, testCase := range testCases { | ||
testCase := testCase | ||
t.Run(name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
version, err := ParseVersion(testCase.s) | ||
|
||
assert.Equal(t, testCase.version, version) | ||
assert.ErrorIs(t, err, testCase.errWrapped) | ||
if testCase.errWrapped != nil { | ||
assert.EqualError(t, err, testCase.errMessage) | ||
} | ||
}) | ||
} | ||
} |
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.
This will be done in a subsequent PR.
It is used to ensure we set versions everywhere and do not leave it to its default value
0
.