-
Notifications
You must be signed in to change notification settings - Fork 44
/
statebuffer.go
228 lines (205 loc) · 4.57 KB
/
statebuffer.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package state
import (
"sort"
"github.com/aergoio/aergo/v2/internal/common"
"github.com/aergoio/aergo/v2/pkg/trie"
"github.com/aergoio/aergo/v2/types"
"github.com/golang/protobuf/proto"
)
type entry interface {
KeyID() types.HashID
Hash() []byte
Value() interface{}
}
type cached interface {
cache() *stateBuffer
}
type valueEntry struct {
key types.HashID
value interface{}
}
func newValueEntry(key types.HashID, value interface{}) entry {
return &valueEntry{
key: key,
value: value,
}
}
func newValueEntryDelete(key types.HashID) entry {
return &valueEntry{
key: key,
value: nil,
}
}
func (et *valueEntry) KeyID() types.HashID {
return et.key
}
func (et *valueEntry) Hash() []byte {
if hash := getHashBytes(et.value); hash != nil {
return hash
}
return []byte{0}
}
func (et *valueEntry) Value() interface{} {
return et.value
}
type metaEntry struct {
*valueEntry
}
func newMetaEntry(key types.HashID, value interface{}) entry {
return &metaEntry{
valueEntry: &valueEntry{
key: key,
value: value,
},
}
}
type bufferIndex map[types.HashID]*stack
func (idxs *bufferIndex) peek(key types.HashID) int {
return (*idxs)[key].peek()
}
func (idxs *bufferIndex) pop(key types.HashID) int {
return (*idxs)[key].pop()
}
func (idxs *bufferIndex) push(key types.HashID, argv ...int) {
(*idxs)[key] = (*idxs)[key].push(argv...)
}
func (idxs *bufferIndex) rollback(snapshot int) {
for k, v := range *idxs {
for v.peek() >= snapshot {
v.pop()
}
if v.peek() < 0 {
delete(*idxs, k)
}
}
}
type stateBuffer struct {
entries []entry
indexes bufferIndex
nextIdx int
}
func newStateBuffer() *stateBuffer {
buffer := stateBuffer{
entries: []entry{},
indexes: bufferIndex{},
nextIdx: 0,
}
return &buffer
}
func (buffer *stateBuffer) reset() error {
return buffer.rollback(0)
}
func (buffer *stateBuffer) get(key types.HashID) entry {
if index, ok := buffer.indexes[key]; ok {
return buffer.entries[index.peek()]
}
return nil
}
func (buffer *stateBuffer) has(key types.HashID) bool {
_, ok := buffer.indexes[key]
return ok
}
func (buffer *stateBuffer) put(et entry) {
snapshot := buffer.snapshot()
buffer.entries = append(buffer.entries, et)
buffer.indexes[et.KeyID()] = buffer.indexes[et.KeyID()].push(snapshot)
buffer.nextIdx++
}
func (buffer *stateBuffer) snapshot() int {
return buffer.nextIdx
}
func (buffer *stateBuffer) rollback(snapshot int) error {
for i := buffer.nextIdx - 1; i >= snapshot; i-- {
et := buffer.entries[i]
buffer.indexes.pop(et.KeyID())
idx := buffer.indexes.peek(et.KeyID())
if idx < 0 {
delete(buffer.indexes, et.KeyID())
continue
}
}
buffer.entries = buffer.entries[:snapshot]
//buffer.indexes.rollback(snapshot)
buffer.nextIdx = snapshot
return nil
}
func (buffer *stateBuffer) isEmpty() bool {
return len(buffer.entries) == 0
}
func (buffer *stateBuffer) export() ([][]byte, [][]byte) {
bufs := make([]entry, 0, len(buffer.indexes))
for _, v := range buffer.indexes {
idx := v.peek()
if idx < 0 {
continue
}
et := buffer.entries[idx]
if _, ok := et.(metaEntry); ok {
// skip meta entry
continue
}
bufs = append(bufs, et)
}
sort.Slice(bufs, func(i, j int) bool {
return -1 == (bufs[i].KeyID().Compare(bufs[j].KeyID()))
})
size := len(bufs)
keys := make([][]byte, size)
vals := make([][]byte, size)
for i, et := range bufs {
keys[i] = append(keys[i], et.KeyID().Bytes()...)
vals[i] = append(vals[i], et.Hash()...)
}
return keys, vals
}
func (buffer *stateBuffer) updateTrie(tr *trie.Trie) error {
keys, vals := buffer.export()
if len(keys) == 0 || len(vals) == 0 {
// nothing to update
return nil
}
if _, err := tr.Update(keys, vals); err != nil {
return err
}
return nil
}
func (buffer *stateBuffer) stage(txn trie.DbTx) error {
for _, v := range buffer.indexes {
et := buffer.entries[v.peek()]
buf, err := marshal(et.Value())
if err != nil {
return err
}
txn.Set(et.Hash(), buf)
}
return nil
}
func marshal(data interface{}) ([]byte, error) {
switch data.(type) {
case ([]byte):
return data.([]byte), nil
case (*[]byte):
return *(data.(*[]byte)), nil
case (types.ImplMarshal):
return data.(types.ImplMarshal).Marshal()
case (proto.Message):
return proto.Marshal(data.(proto.Message))
}
return nil, nil
}
func getHashBytes(data interface{}) []byte {
if data == nil {
return nil
}
switch data.(type) {
case (types.ImplHashBytes):
return data.(types.ImplHashBytes).Hash()
default:
}
buf, err := marshal(data)
if err != nil {
logger.Error().Err(err).Msg("failed to get hash bytes: marshal")
return nil
}
return common.Hasher(buf)
}