-
Notifications
You must be signed in to change notification settings - Fork 202
/
trackableDataTrie.go
100 lines (83 loc) · 2.83 KB
/
trackableDataTrie.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package state
import (
"github.com/ElrondNetwork/elrond-go-core/core"
"github.com/ElrondNetwork/elrond-go-core/data"
"github.com/ElrondNetwork/elrond-go/common"
)
// TrackableDataTrie wraps a PatriciaMerkelTrie adding modifying data capabilities
type TrackableDataTrie struct {
dirtyData map[string][]byte
tr common.Trie
identifier []byte
}
// NewTrackableDataTrie returns an instance of DataTrieTracker
func NewTrackableDataTrie(identifier []byte, tr common.Trie) *TrackableDataTrie {
return &TrackableDataTrie{
tr: tr,
dirtyData: make(map[string][]byte),
identifier: identifier,
}
}
// ClearDataCaches empties the dirtyData map and original map
func (tdaw *TrackableDataTrie) ClearDataCaches() {
tdaw.dirtyData = make(map[string][]byte)
}
// DirtyData returns the map of (key, value) pairs that contain the data needed to be saved in the data trie
func (tdaw *TrackableDataTrie) DirtyData() map[string][]byte {
return tdaw.dirtyData
}
// RetrieveValue fetches the value from a particular key searching the account data store
// The search starts with dirty map, continues with original map and ends with the trie
// Data must have been retrieved from its trie
func (tdaw *TrackableDataTrie) RetrieveValue(key []byte) ([]byte, error) {
tailLength := len(key) + len(tdaw.identifier)
// search in dirty data cache
if value, found := tdaw.dirtyData[string(key)]; found {
log.Trace("retrieve value from dirty data", "key", key, "value", value)
return trimValue(value, tailLength)
}
// ok, not in cache, retrieve from trie
if tdaw.tr == nil {
return nil, ErrNilTrie
}
value, err := tdaw.tr.Get(key)
if err != nil {
return nil, err
}
log.Trace("retrieve value from trie", "key", key, "value", value)
value, _ = trimValue(value, tailLength)
return value, nil
}
func trimValue(value []byte, tailLength int) ([]byte, error) {
dataLength := len(value) - tailLength
if dataLength < 0 {
return nil, ErrNegativeValue
}
return value[:dataLength], nil
}
// SaveKeyValue stores in dirtyData the data keys "touched"
// It does not care if the data is really dirty as calling this check here will be sub-optimal
func (tdaw *TrackableDataTrie) SaveKeyValue(key []byte, value []byte) error {
var identifier []byte
lenValue := uint64(len(value))
if lenValue > core.MaxLeafSize {
return data.ErrLeafSizeTooBig
}
if lenValue != 0 {
identifier = append(key, tdaw.identifier...)
}
tdaw.dirtyData[string(key)] = append(value, identifier...)
return nil
}
// SetDataTrie sets the internal data trie
func (tdaw *TrackableDataTrie) SetDataTrie(tr common.Trie) {
tdaw.tr = tr
}
// DataTrie sets the internal data trie
func (tdaw *TrackableDataTrie) DataTrie() common.Trie {
return tdaw.tr
}
// IsInterfaceNil returns true if there is no value under the interface
func (tdaw *TrackableDataTrie) IsInterfaceNil() bool {
return tdaw == nil
}