-
Notifications
You must be signed in to change notification settings - Fork 197
/
keyBuilder.go
61 lines (50 loc) · 1.54 KB
/
keyBuilder.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
package keyBuilder
import (
"github.com/ElrondNetwork/elrond-go/common"
)
const (
// NibbleSize marks the size of a byte nibble
NibbleSize = 4
keyLength = 32
)
type keyBuilder struct {
key []byte
}
// NewKeyBuilder creates a new key builder. This is used for building trie keys when traversing the trie.
// Use this only if you traverse the trie from the root, else hexToTrieKeyBytes might fail
func NewKeyBuilder() *keyBuilder {
return &keyBuilder{
key: make([]byte, 0, keyLength),
}
}
// BuildKey appends the given byte array to the existing key
func (kb *keyBuilder) BuildKey(keyPart []byte) {
kb.key = append(kb.key, keyPart...)
}
// GetKey transforms the key from hex to trie key, and returns it.
// Is mandatory that GetKey always returns a new byte slice, not a pointer to an existing one
func (kb *keyBuilder) GetKey() ([]byte, error) {
return hexToTrieKeyBytes(kb.key)
}
// Clone returns a new KeyBuilder with the same key
func (kb *keyBuilder) Clone() common.KeyBuilder {
return &keyBuilder{
key: kb.key,
}
}
// hexToTrieKeyBytes transforms hex nibbles into key bytes. The hex terminator is removed from the end of the hex slice,
// and then the hex slice is reversed when forming the key bytes.
func hexToTrieKeyBytes(hex []byte) ([]byte, error) {
hex = hex[:len(hex)-1]
length := len(hex)
if length%2 != 0 {
return nil, ErrInvalidLength
}
key := make([]byte, length/2)
hexSliceIndex := 0
for i := len(key) - 1; i >= 0; i-- {
key[i] = hex[hexSliceIndex+1]<<NibbleSize | hex[hexSliceIndex]
hexSliceIndex += 2
}
return key, nil
}