-
Notifications
You must be signed in to change notification settings - Fork 0
/
trie.go
81 lines (64 loc) · 2.12 KB
/
trie.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package core
import (
"strconv"
"github.com/ThotaGopichandThota/gn-core3/core/check"
)
// TrieNodeVersion defines the version of the trie node
type TrieNodeVersion uint8
const (
// NotSpecified means that the value is not populated or is not important
NotSpecified TrieNodeVersion = iota
// AutoBalanceEnabled is used for data tries, and only after the activation of AutoBalanceDataTriesEnableEpoch flag
AutoBalanceEnabled
)
const (
// NotSpecifiedString is the string representation of NotSpecified trie node version
NotSpecifiedString = "not specified"
// AutoBalanceEnabledString is the string representation of AutoBalanceEnabled trie node version
AutoBalanceEnabledString = "auto balanced"
)
func (version TrieNodeVersion) String() string {
switch version {
case NotSpecified:
return NotSpecifiedString
case AutoBalanceEnabled:
return AutoBalanceEnabledString
default:
return "unknown: " + strconv.Itoa(int(version))
}
}
type trieNodeVersionVerifier struct {
enableEpochsHandler EnableEpochsHandler
}
func NewTrieNodeVersionVerifier(enableEpochsHandler EnableEpochsHandler) (*trieNodeVersionVerifier, error) {
if check.IfNil(enableEpochsHandler) {
return nil, ErrNilEnableEpochsHandler
}
return &trieNodeVersionVerifier{
enableEpochsHandler: enableEpochsHandler,
}, nil
}
// IsValidVersion returns true if the given trie node version is valid
func (vv *trieNodeVersionVerifier) IsValidVersion(version TrieNodeVersion) bool {
if vv.enableEpochsHandler.IsAutoBalanceDataTriesEnabled() {
return version <= AutoBalanceEnabled
}
return version == NotSpecified
}
// IsInterfaceNil returns true if there is no value under the interface
func (vv *trieNodeVersionVerifier) IsInterfaceNil() bool {
return vv == nil
}
// GetVersionForNewData returns the trie node version that should be used for new data
func GetVersionForNewData(handler EnableEpochsHandler) TrieNodeVersion {
if handler.IsAutoBalanceDataTriesEnabled() {
return AutoBalanceEnabled
}
return NotSpecified
}
// TrieData holds the data that will be inserted into the trie
type TrieData struct {
Key []byte
Value []byte
Version TrieNodeVersion
}