/
store.go
313 lines (250 loc) · 7.29 KB
/
store.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
package fake
import (
"bytes"
"io"
"sort"
"sync"
"github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/axelarnetwork/axelar-core/testutils/fake/interfaces"
"github.com/axelarnetwork/axelar-core/testutils/fake/interfaces/mock"
)
// MultiStore is a simple multistore used for testing
type MultiStore struct {
kvstore map[string]interfaces.KVStore
*mock.MultiStoreMock
}
// CachedMultiStore is a simple chached multistore for testing
type CachedMultiStore struct {
kvstore map[string]interfaces.KVStore
*mock.CacheMultiStoreMock
}
// NewMultiStore returns a new Multistore instance used for testing
func NewMultiStore() sdk.MultiStore {
ms := MultiStore{
kvstore: map[string]interfaces.KVStore{},
MultiStoreMock: &mock.MultiStoreMock{},
}
ms.GetKVStoreFunc = func(storeKey types.StoreKey) types.KVStore {
if store, ok := ms.kvstore[storeKey.String()]; ok {
return store
}
store := newTestKVStore()
ms.kvstore[storeKey.String()] = store
return store
}
ms.CacheMultiStoreFunc = func() types.CacheMultiStore {
return NewCachedMultiStore(ms)
}
return ms
}
// NewCachedMultiStore returns a new CacheMultiStore instance for testing
func NewCachedMultiStore(ms MultiStore) sdk.CacheMultiStore {
kvstore := map[string]interfaces.KVStore{}
for key, store := range ms.kvstore {
kvstore[key] = store.CacheWrap().(interfaces.KVStore)
}
cached := CachedMultiStore{
kvstore: kvstore,
CacheMultiStoreMock: &mock.CacheMultiStoreMock{},
}
cached.GetKVStoreFunc = func(storeKey types.StoreKey) types.KVStore {
if store, ok := cached.kvstore[storeKey.String()]; ok {
return store
}
store := newTestKVStore()
store.write = func() {
ms.kvstore[storeKey.String()] = store
}
cached.kvstore[storeKey.String()] = store
return store
}
cached.WriteFunc = func() {
for _, store := range cached.kvstore {
store.(*TestKVStore).Write()
}
}
return cached
}
// TestKVStore is a kv store for testing
type TestKVStore struct {
mutex *sync.RWMutex
store map[string][]byte
write func()
}
func (t TestKVStore) Write() {
t.write()
}
// newTestKVStore returns a new kv store instance for testing
func newTestKVStore() *TestKVStore {
return &TestKVStore{
mutex: &sync.RWMutex{},
store: map[string][]byte{},
write: func() {},
}
}
// GetStoreType is not implemented
func (t TestKVStore) GetStoreType() sdk.StoreType {
panic("implement me")
}
// CacheWrap is not implemented
func (t *TestKVStore) CacheWrap() sdk.CacheWrap {
t.mutex.RLock()
defer t.mutex.RUnlock()
cache := &TestKVStore{
mutex: &sync.RWMutex{},
store: map[string][]byte{},
}
cache.write = func() { t.store = cache.store }
for key, val := range t.store {
cache.store[key] = val
}
return cache
}
// CacheWrapWithTrace is not implemented
func (t TestKVStore) CacheWrapWithTrace(_ io.Writer, _ sdk.TraceContext) sdk.CacheWrap {
panic("implement me")
}
// CacheWrapWithListeners is not implemented
func (t TestKVStore) CacheWrapWithListeners(storeKey types.StoreKey, listeners []types.WriteListener) types.CacheWrap {
panic("implement me")
}
// Get returns the value of the given key, nil if it does not exist
func (t TestKVStore) Get(key []byte) []byte {
t.mutex.RLock()
defer t.mutex.RUnlock()
val, ok := t.store[string(key)]
if !ok {
return nil
}
return val
}
// Has checks if an entry for the given key exists
func (t TestKVStore) Has(key []byte) bool {
t.mutex.RLock()
defer t.mutex.RUnlock()
_, ok := t.store[string(key)]
return ok
}
// Set stores the given key value pair
func (t TestKVStore) Set(key, value []byte) {
t.mutex.Lock()
defer t.mutex.Unlock()
t.store[string(key)] = value
}
// Delete deletes a key if it exists
func (t TestKVStore) Delete(key []byte) {
t.mutex.Lock()
defer t.mutex.Unlock()
delete(t.store, string(key))
}
// Iterator returns an interator over the given key domain
func (t TestKVStore) Iterator(start, end []byte) sdk.Iterator {
t.mutex.Lock()
defer t.mutex.Unlock()
return newMockIterator(start, end, t.store)
}
// ReverseIterator returns an iterator that iterates over all keys in the given domain in reverse order
func (t TestKVStore) ReverseIterator(start, end []byte) sdk.Iterator {
t.mutex.Lock()
defer t.mutex.Unlock()
iter := newMockIterator(start, end, t.store)
// reverse the order of the iterator, which is returned already
// sorted in ascending order
for i, j := 0, len(iter.keys)-1; i < j; i, j = i+1, j-1 {
iter.keys[i], iter.keys[j] = iter.keys[j], iter.keys[i]
iter.values[i], iter.values[j] = iter.values[j], iter.values[i]
}
iter.start = end
iter.end = start
return iter
}
// fake iterator
type mockIterator struct {
keys [][]byte
values [][]byte
index int
start, end []byte
}
func newMockIterator(start, end []byte, content map[string][]byte) *mockIterator {
keys := make([][]byte, 0)
// select the keys according to the specified domain
for k := range content {
b := []byte(k)
if (start == nil && end == nil) || (bytes.Compare(b, start) >= 0 && bytes.Compare(b, end) < 0) {
// make sure data is a copy so that there is no concurrent writing
temp := make([]byte, len(k))
copy(temp, k)
keys = append(keys, temp)
}
}
// Sort the keys in ascending order
sort.Slice(keys, func(i, j int) bool {
return bytes.Compare(keys[i], keys[j]) < 0
})
// With the keys chosen and sorted, we can now populate the slice of values
values := make([][]byte, len(keys))
for i := 0; i < len(keys); i++ {
// make sure data is a copy so that there is no concurrent writing
value := content[string(keys[i])]
temp := make([]byte, len(value))
copy(temp, value)
values[i] = temp
}
return &mockIterator{
keys: keys,
values: values,
index: 0,
start: start,
end: end,
}
}
// Domain returns the key domain of the iterator.
// The start & end (exclusive) limits to iterate over.
// If end < start, then the Iterator goes in reverse order.
//
// A domain of ([]byte{12, 13}, []byte{12, 14}) will iterate
// over anything with the prefix []byte{12, 13}.
//
// The smallest key is the empty byte array []byte{} - see BeginningKey().
// The largest key is the nil byte array []byte(nil) - see EndingKey().
// CONTRACT: start, end readonly []byte
func (mi mockIterator) Domain() (start []byte, end []byte) {
return mi.start, mi.end
}
// Valid returns whether the current position is valid.
// Once invalid, an Iterator is forever invalid.
func (mi mockIterator) Valid() bool {
return mi.index < len(mi.keys)
}
// Next moves the iterator to the next sequential key in the database, as
// defined by order of iteration.
// If Valid returns false, this method will panic.
func (mi *mockIterator) Next() {
mi.index++
}
// Key returns the key of the cursor.
// If Valid returns false, this method will panic.
// CONTRACT: key readonly []byte
func (mi mockIterator) Key() (key []byte) {
if !mi.Valid() {
panic("Iterator position out of bounds")
}
return mi.keys[mi.index]
}
// Value returns the value of the cursor.
// If Valid returns false, this method will panic.
// CONTRACT: value readonly []byte
func (mi mockIterator) Value() (value []byte) {
if !mi.Valid() {
panic("Iterator position out of bounds")
}
return mi.values[mi.index]
}
func (mi mockIterator) Error() error {
return nil
}
// Close releases the Iterator.
func (mi mockIterator) Close() error {
return nil
}