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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
|
// Copyright 2018 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum 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 go-ethereum 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 go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
// Package db implements a mock store that keeps all chunk data in LevelDB database.
package db
import (
"archive/tar"
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"sync"
"time"
"github.com/syndtr/goleveldb/leveldb"
"github.com/syndtr/goleveldb/leveldb/util"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/swarm/storage/mock"
)
// GlobalStore contains the LevelDB database that is storing
// chunk data for all swarm nodes.
// Closing the GlobalStore with Close method is required to
// release resources used by the database.
type GlobalStore struct {
db *leveldb.DB
// protects nodes and keys indexes
// in Put and Delete methods
nodesLocks sync.Map
keysLocks sync.Map
}
// NewGlobalStore creates a new instance of GlobalStore.
func NewGlobalStore(path string) (s *GlobalStore, err error) {
db, err := leveldb.OpenFile(path, nil)
if err != nil {
return nil, err
}
return &GlobalStore{
db: db,
}, nil
}
// Close releases the resources used by the underlying LevelDB.
func (s *GlobalStore) Close() error {
return s.db.Close()
}
// NewNodeStore returns a new instance of NodeStore that retrieves and stores
// chunk data only for a node with address addr.
func (s *GlobalStore) NewNodeStore(addr common.Address) *mock.NodeStore {
return mock.NewNodeStore(addr, s)
}
// Get returns chunk data if the chunk with key exists for node
// on address addr.
func (s *GlobalStore) Get(addr common.Address, key []byte) (data []byte, err error) {
has, err := s.db.Has(indexForHashesPerNode(addr, key), nil)
if err != nil {
return nil, mock.ErrNotFound
}
if !has {
return nil, mock.ErrNotFound
}
data, err = s.db.Get(indexDataKey(key), nil)
if err == leveldb.ErrNotFound {
err = mock.ErrNotFound
}
return
}
// Put saves the chunk data for node with address addr.
func (s *GlobalStore) Put(addr common.Address, key []byte, data []byte) error {
unlock, err := s.lock(addr, key)
if err != nil {
return err
}
defer unlock()
batch := new(leveldb.Batch)
batch.Put(indexForHashesPerNode(addr, key), nil)
batch.Put(indexForNodesWithHash(key, addr), nil)
batch.Put(indexForNodes(addr), nil)
batch.Put(indexForHashes(key), nil)
batch.Put(indexDataKey(key), data)
return s.db.Write(batch, nil)
}
// Delete removes the chunk reference to node with address addr.
func (s *GlobalStore) Delete(addr common.Address, key []byte) error {
unlock, err := s.lock(addr, key)
if err != nil {
return err
}
defer unlock()
batch := new(leveldb.Batch)
batch.Delete(indexForHashesPerNode(addr, key))
batch.Delete(indexForNodesWithHash(key, addr))
// check if this node contains any keys, and if not
// remove it from the
x := indexForHashesPerNodePrefix(addr)
if k, _ := s.db.Get(x, nil); !bytes.HasPrefix(k, x) {
batch.Delete(indexForNodes(addr))
}
x = indexForNodesWithHashPrefix(key)
if k, _ := s.db.Get(x, nil); !bytes.HasPrefix(k, x) {
batch.Delete(indexForHashes(key))
}
return s.db.Write(batch, nil)
}
// HasKey returns whether a node with addr contains the key.
func (s *GlobalStore) HasKey(addr common.Address, key []byte) bool {
has, err := s.db.Has(indexForHashesPerNode(addr, key), nil)
if err != nil {
has = false
}
return has
}
// Keys returns a paginated list of keys on all nodes.
func (s *GlobalStore) Keys(startKey []byte, limit int) (keys mock.Keys, err error) {
return s.keys(nil, startKey, limit)
}
// Nodes returns a paginated list of all known nodes.
func (s *GlobalStore) Nodes(startAddr *common.Address, limit int) (nodes mock.Nodes, err error) {
return s.nodes(nil, startAddr, limit)
}
// NodeKeys returns a paginated list of keys on a node with provided address.
func (s *GlobalStore) NodeKeys(addr common.Address, startKey []byte, limit int) (keys mock.Keys, err error) {
return s.keys(&addr, startKey, limit)
}
// KeyNodes returns a paginated list of nodes that contain a particular key.
func (s *GlobalStore) KeyNodes(key []byte, startAddr *common.Address, limit int) (nodes mock.Nodes, err error) {
return s.nodes(key, startAddr, limit)
}
// keys returns a paginated list of keys. If addr is not nil, only keys on that
// node will be returned.
func (s *GlobalStore) keys(addr *common.Address, startKey []byte, limit int) (keys mock.Keys, err error) {
iter := s.db.NewIterator(nil, nil)
defer iter.Release()
if limit <= 0 {
limit = mock.DefaultLimit
}
prefix := []byte{indexForHashesPrefix}
if addr != nil {
prefix = indexForHashesPerNodePrefix(*addr)
}
if startKey != nil {
if addr != nil {
startKey = indexForHashesPerNode(*addr, startKey)
} else {
startKey = indexForHashes(startKey)
}
} else {
startKey = prefix
}
ok := iter.Seek(startKey)
if !ok {
return keys, iter.Error()
}
for ; ok; ok = iter.Next() {
k := iter.Key()
if !bytes.HasPrefix(k, prefix) {
break
}
key := append([]byte(nil), bytes.TrimPrefix(k, prefix)...)
if len(keys.Keys) >= limit {
keys.Next = key
break
}
keys.Keys = append(keys.Keys, key)
}
return keys, iter.Error()
}
// nodes returns a paginated list of node addresses. If key is not nil,
// only nodes that contain that key will be returned.
func (s *GlobalStore) nodes(key []byte, startAddr *common.Address, limit int) (nodes mock.Nodes, err error) {
iter := s.db.NewIterator(nil, nil)
defer iter.Release()
if limit <= 0 {
limit = mock.DefaultLimit
}
prefix := []byte{indexForNodesPrefix}
if key != nil {
prefix = indexForNodesWithHashPrefix(key)
}
startKey := prefix
if startAddr != nil {
if key != nil {
startKey = indexForNodesWithHash(key, *startAddr)
} else {
startKey = indexForNodes(*startAddr)
}
}
ok := iter.Seek(startKey)
if !ok {
return nodes, iter.Error()
}
for ; ok; ok = iter.Next() {
k := iter.Key()
if !bytes.HasPrefix(k, prefix) {
break
}
addr := common.BytesToAddress(append([]byte(nil), bytes.TrimPrefix(k, prefix)...))
if len(nodes.Addrs) >= limit {
nodes.Next = &addr
break
}
nodes.Addrs = append(nodes.Addrs, addr)
}
return nodes, iter.Error()
}
// Import reads tar archive from a reader that contains exported chunk data.
// It returns the number of chunks imported and an error.
func (s *GlobalStore) Import(r io.Reader) (n int, err error) {
tr := tar.NewReader(r)
for {
hdr, err := tr.Next()
if err != nil {
if err == io.EOF {
break
}
return n, err
}
data, err := ioutil.ReadAll(tr)
if err != nil {
return n, err
}
var c mock.ExportedChunk
if err = json.Unmarshal(data, &c); err != nil {
return n, err
}
key := common.Hex2Bytes(hdr.Name)
batch := new(leveldb.Batch)
for _, addr := range c.Addrs {
batch.Put(indexForHashesPerNode(addr, key), nil)
batch.Put(indexForNodesWithHash(key, addr), nil)
batch.Put(indexForNodes(addr), nil)
}
batch.Put(indexForHashes(key), nil)
batch.Put(indexDataKey(key), c.Data)
if err = s.db.Write(batch, nil); err != nil {
return n, err
}
n++
}
return n, err
}
// Export writes to a writer a tar archive with all chunk data from
// the store. It returns the number fo chunks exported and an error.
func (s *GlobalStore) Export(w io.Writer) (n int, err error) {
tw := tar.NewWriter(w)
defer tw.Close()
buf := bytes.NewBuffer(make([]byte, 0, 1024))
encoder := json.NewEncoder(buf)
snap, err := s.db.GetSnapshot()
if err != nil {
return 0, err
}
iter := snap.NewIterator(util.BytesPrefix([]byte{indexForHashesByNodePrefix}), nil)
defer iter.Release()
var currentKey string
var addrs []common.Address
saveChunk := func() error {
hexKey := currentKey
data, err := snap.Get(indexDataKey(common.Hex2Bytes(hexKey)), nil)
if err != nil {
return fmt.Errorf("get data %s: %v", hexKey, err)
}
buf.Reset()
if err = encoder.Encode(mock.ExportedChunk{
Addrs: addrs,
Data: data,
}); err != nil {
return err
}
d := buf.Bytes()
hdr := &tar.Header{
Name: hexKey,
Mode: 0644,
Size: int64(len(d)),
}
if err := tw.WriteHeader(hdr); err != nil {
return err
}
if _, err := tw.Write(d); err != nil {
return err
}
n++
return nil
}
for iter.Next() {
k := bytes.TrimPrefix(iter.Key(), []byte{indexForHashesByNodePrefix})
i := bytes.Index(k, []byte{keyTermByte})
if i < 0 {
continue
}
hexKey := string(k[:i])
if currentKey == "" {
currentKey = hexKey
}
if hexKey != currentKey {
if err = saveChunk(); err != nil {
return n, err
}
addrs = addrs[:0]
}
currentKey = hexKey
addrs = append(addrs, common.BytesToAddress(k[i+1:]))
}
if len(addrs) > 0 {
if err = saveChunk(); err != nil {
return n, err
}
}
return n, iter.Error()
}
var (
// maximal time for lock to wait until it returns error
lockTimeout = 3 * time.Second
// duration between two lock checks.
lockCheckDelay = 30 * time.Microsecond
// error returned by lock method when lock timeout is reached
errLockTimeout = errors.New("lock timeout")
)
// lock protects parallel writes in Put and Delete methods for both
// node with provided address and for data with provided key.
func (s *GlobalStore) lock(addr common.Address, key []byte) (unlock func(), err error) {
start := time.Now()
nodeLockKey := addr.Hex()
for {
_, loaded := s.nodesLocks.LoadOrStore(nodeLockKey, struct{}{})
if !loaded {
break
}
time.Sleep(lockCheckDelay)
if time.Since(start) > lockTimeout {
return nil, errLockTimeout
}
}
start = time.Now()
keyLockKey := common.Bytes2Hex(key)
for {
_, loaded := s.keysLocks.LoadOrStore(keyLockKey, struct{}{})
if !loaded {
break
}
time.Sleep(lockCheckDelay)
if time.Since(start) > lockTimeout {
return nil, errLockTimeout
}
}
return func() {
s.nodesLocks.Delete(nodeLockKey)
s.keysLocks.Delete(keyLockKey)
}, nil
}
const (
// prefixes for different indexes
indexDataPrefix = 0
indexForNodesWithHashesPrefix = 1
indexForHashesByNodePrefix = 2
indexForNodesPrefix = 3
indexForHashesPrefix = 4
// keyTermByte splits keys and node addresses
// in database keys
keyTermByte = 0xff
)
// indexForHashesPerNode constructs a database key to store keys used in
// NodeKeys method.
func indexForHashesPerNode(addr common.Address, key []byte) []byte {
return append(indexForHashesPerNodePrefix(addr), key...)
}
// indexForHashesPerNodePrefix returns a prefix containing a node address used in
// NodeKeys method. Node address is hex encoded to be able to use keyTermByte
// for splitting node address and key.
func indexForHashesPerNodePrefix(addr common.Address) []byte {
return append([]byte{indexForNodesWithHashesPrefix}, append([]byte(addr.Hex()), keyTermByte)...)
}
// indexForNodesWithHash constructs a database key to store keys used in
// KeyNodes method.
func indexForNodesWithHash(key []byte, addr common.Address) []byte {
return append(indexForNodesWithHashPrefix(key), addr[:]...)
}
// indexForNodesWithHashPrefix returns a prefix containing a key used in
// KeyNodes method. Key is hex encoded to be able to use keyTermByte
// for splitting key and node address.
func indexForNodesWithHashPrefix(key []byte) []byte {
return append([]byte{indexForHashesByNodePrefix}, append([]byte(common.Bytes2Hex(key)), keyTermByte)...)
}
// indexForNodes constructs a database key to store keys used in
// Nodes method.
func indexForNodes(addr common.Address) []byte {
return append([]byte{indexForNodesPrefix}, addr[:]...)
}
// indexForHashes constructs a database key to store keys used in
// Keys method.
func indexForHashes(key []byte) []byte {
return append([]byte{indexForHashesPrefix}, key...)
}
// indexDataKey constructs a database key for key/data storage.
func indexDataKey(key []byte) []byte {
return append([]byte{indexDataPrefix}, key...)
}
|