-
Notifications
You must be signed in to change notification settings - Fork 47
/
changeset.go
271 lines (247 loc) · 6.43 KB
/
changeset.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
// Copyright 2023 The AmazeChain Authors
// This file is part of the AmazeChain library.
//
// The AmazeChain library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The AmazeChain library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the AmazeChain library. If not, see <http://www.gnu.org/licenses/>.
package changeset
import (
"bytes"
"encoding/binary"
"fmt"
"github.com/amazechain/amc/common/types"
"github.com/amazechain/amc/modules"
"github.com/amazechain/amc/modules/ethdb"
"math"
"reflect"
"github.com/ledgerwatch/erigon-lib/kv"
)
func NewChangeSet() *ChangeSet {
return &ChangeSet{
Changes: make([]Change, 0),
}
}
type Change struct {
Key []byte
Value []byte
}
// ChangeSet is a map with keys of the same size.
// Both keys and values are byte strings.
type ChangeSet struct {
// Invariant: all keys are of the same size.
Changes []Change
keyLen int
}
// BEGIN sort.Interface
func (s *ChangeSet) Len() int {
return len(s.Changes)
}
func (s *ChangeSet) Swap(i, j int) {
s.Changes[i], s.Changes[j] = s.Changes[j], s.Changes[i]
}
func (s *ChangeSet) Less(i, j int) bool {
cmp := bytes.Compare(s.Changes[i].Key, s.Changes[j].Key)
if cmp == 0 {
cmp = bytes.Compare(s.Changes[i].Value, s.Changes[j].Value)
}
return cmp < 0
}
// END sort.Interface
func (s *ChangeSet) KeySize() int {
if s.keyLen != 0 {
return s.keyLen
}
for _, c := range s.Changes {
return len(c.Key)
}
return 0
}
func (s *ChangeSet) checkKeySize(key []byte) error {
if (s.Len() == 0 && s.KeySize() == 0) || (len(key) == s.KeySize() && len(key) > 0) {
return nil
}
return fmt.Errorf("wrong key size in AccountChangeSet: expected %d, actual %d", s.KeySize(), len(key))
}
// Add adds a new entry to the AccountChangeSet.
// One must not add an existing key
// and may add keys only of the same size.
func (s *ChangeSet) Add(key []byte, value []byte) error {
if err := s.checkKeySize(key); err != nil {
return err
}
s.Changes = append(s.Changes, Change{
Key: key,
Value: value,
})
return nil
}
func (s *ChangeSet) ChangedKeys() map[string]struct{} {
m := make(map[string]struct{}, len(s.Changes))
for i := range s.Changes {
m[string(s.Changes[i].Key)] = struct{}{}
}
return m
}
func (s *ChangeSet) Equals(s2 *ChangeSet) bool {
return reflect.DeepEqual(s.Changes, s2.Changes)
}
func (s *ChangeSet) String() string {
str := ""
for _, v := range s.Changes {
str += fmt.Sprintf("%v %s : %s\n", len(v.Key), types.Bytes2Hex(v.Key), string(v.Value))
}
return str
}
// Encoded Method
func FromDBFormat(dbKey, dbValue []byte) (uint64, []byte, []byte, error) {
if len(dbKey) == 8 {
return DecodeAccounts(dbKey, dbValue)
} else {
return DecodeStorage(dbKey, dbValue)
}
}
func AvailableFrom(tx kv.Tx) (uint64, error) {
c, err := tx.Cursor(modules.AccountChangeSet)
if err != nil {
return math.MaxUint64, err
}
defer c.Close()
k, _, err := c.First()
if err != nil {
return math.MaxUint64, err
}
if len(k) == 0 {
return math.MaxUint64, nil
}
return binary.BigEndian.Uint64(k), nil
}
func AvailableStorageFrom(tx kv.Tx) (uint64, error) {
c, err := tx.Cursor(modules.StorageChangeSet)
if err != nil {
return math.MaxUint64, err
}
defer c.Close()
k, _, err := c.First()
if err != nil {
return math.MaxUint64, err
}
if len(k) == 0 {
return math.MaxUint64, nil
}
return binary.BigEndian.Uint64(k), nil
}
// [from:to)
func ForRange(db kv.Tx, bucket string, from, to uint64, walker func(blockN uint64, k, v []byte) error) error {
var blockN uint64
c, err := db.Cursor(bucket)
if err != nil {
return err
}
defer c.Close()
return ethdb.Walk(c, modules.EncodeBlockNumber(from), 0, func(k, v []byte) (bool, error) {
var err error
blockN, k, v, err = FromDBFormat(k, v)
if err != nil {
return false, err
}
if blockN >= to {
return false, nil
}
if err = walker(blockN, k, v); err != nil {
return false, err
}
return true, nil
})
}
func ForEach(db kv.Tx, bucket string, startkey []byte, walker func(blockN uint64, k, v []byte) error) error {
var blockN uint64
return db.ForEach(bucket, startkey, func(k, v []byte) error {
var err error
blockN, k, v, err = FromDBFormat(k, v)
if err != nil {
return err
}
return walker(blockN, k, v)
})
}
func ForPrefix(db kv.Tx, bucket string, startkey []byte, walker func(blockN uint64, k, v []byte) error) error {
var blockN uint64
return db.ForPrefix(bucket, startkey, func(k, v []byte) error {
var err error
blockN, k, v, err = FromDBFormat(k, v)
if err != nil {
return err
}
return walker(blockN, k, v)
})
}
func Truncate(tx kv.RwTx, from uint64) error {
keyStart := modules.EncodeBlockNumber(from)
{
c, err := tx.RwCursorDupSort(modules.AccountChangeSet)
if err != nil {
return err
}
defer c.Close()
for k, _, err := c.Seek(keyStart); k != nil; k, _, err = c.NextNoDup() {
if err != nil {
return err
}
err = c.DeleteCurrentDuplicates()
if err != nil {
return err
}
}
}
{
c, err := tx.RwCursorDupSort(modules.StorageChangeSet)
if err != nil {
return err
}
defer c.Close()
for k, _, err := c.Seek(keyStart); k != nil; k, _, err = c.NextNoDup() {
if err != nil {
return err
}
err = c.DeleteCurrentDuplicates()
if err != nil {
return err
}
}
}
return nil
}
var Mapper = map[string]struct {
IndexBucket string
IndexChunkKey func([]byte, uint64) []byte
Find func(cursor kv.CursorDupSort, blockNumber uint64, key []byte) ([]byte, error)
New func() *ChangeSet
Encode Encoder
Decode Decoder
}{
modules.AccountChangeSet: {
IndexBucket: modules.AccountsHistory,
IndexChunkKey: modules.AccountIndexChunkKey,
New: NewAccountChangeSet,
Find: FindAccount,
Encode: EncodeAccounts,
Decode: DecodeAccounts,
},
modules.StorageChangeSet: {
IndexBucket: modules.StorageHistory,
IndexChunkKey: modules.StorageIndexChunkKey,
Find: FindStorage,
New: NewStorageChangeSet,
Encode: EncodeStorage,
Decode: DecodeStorage,
},
}