forked from ethersphere/swarm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pin.go
491 lines (439 loc) · 14.9 KB
/
pin.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
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
// Copyright 2019 The Swarm Authors
// This file is part of the Swarm library.
//
// The Swarm 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 Swarm 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 Swarm library. If not, see <http://www.gnu.org/licenses/>.
package pin
import (
"context"
"encoding/binary"
"encoding/hex"
"errors"
"sync"
"github.com/holisticode/swarm/api"
"github.com/holisticode/swarm/chunk"
"github.com/holisticode/swarm/log"
"github.com/holisticode/swarm/state"
"github.com/holisticode/swarm/storage"
"github.com/holisticode/swarm/storage/localstore"
)
const (
Version = "1.0"
WorkerChanSize = 8 // Max no of goroutines when walking the file tree
)
var (
errInvalidChunkData = errors.New("invalid chunk data")
errInvalidUnmarshallData = errors.New("invalid data length")
)
// PinInfo is the struct that stores the information about pinned files
// This is stored in the state DB with Address as key
type PinInfo struct {
Address storage.Address
IsRaw bool
FileSize uint64
PinCounter uint64
}
// MarshalBinary encodes the PinInfo object in to a binary form for storage
func (f *PinInfo) MarshalBinary() (data []byte, err error) {
data = make([]byte, 17)
if f.IsRaw {
data[0] = 1
} else {
data[0] = 0
}
binary.BigEndian.PutUint64(data[1:], f.FileSize)
binary.BigEndian.PutUint64(data[9:], f.PinCounter)
return data, nil
}
// UnmarshalBinary decodes the binary form from the state store to the PinInfo object
func (f *PinInfo) UnmarshalBinary(data []byte) error {
if len(data) != 17 {
return errInvalidUnmarshallData
}
if data[0] == 1 {
f.IsRaw = true
} else {
f.IsRaw = false
}
f.FileSize = binary.BigEndian.Uint64(data[1:])
f.PinCounter = binary.BigEndian.Uint64(data[9:])
return nil
}
// API is the main object which implements all things pinning.
type API struct {
db *localstore.DB
api *api.API
fileParams *storage.FileStoreParams
tag *chunk.Tags
hashSize int
state state.Store // the state store used to store info about pinned files
}
// NewAPI creates a API object that is required for pinning and unpinning
func NewAPI(lstore *localstore.DB, stateStore state.Store, params *storage.FileStoreParams, tags *chunk.Tags, api *api.API) *API {
hashFunc := storage.MakeHashFunc(storage.DefaultHash)
return &API{
db: lstore,
api: api,
fileParams: params,
tag: tags,
hashSize: hashFunc().Size(),
state: stateStore,
}
}
// PinFiles is used to pin a RAW file or a collection (which hash manifest's)
// to the local Swarm node. It takes the root hash as the argument and walks
// down the merkle tree and pin all the chunks that are encountered on the
// way. It pins both data chunk and tree chunks. The pre-requisite is that
// the file should be present in the local database. This function is called
// from two places 1) Just after the file is uploaded 2) anytime after
// uploading the file using the pin command. This function can pin both
// encrypted and non-encrypted files.
func (p *API) PinFiles(addr []byte, isRaw bool, credentials string) error {
hasChunk, err := p.db.Has(context.Background(), chunk.Address(p.removeDecryptionKeyFromChunkHash(addr)))
if !hasChunk {
log.Error("Could not pin hash. File not uploaded", "rootHash", hex.EncodeToString(addr))
return err
}
// Walk the root hash and pin all the chunks
walkerFunction := func(ref storage.Reference) error {
chunkAddr := p.removeDecryptionKeyFromChunkHash(ref)
err := p.db.Set(context.Background(), chunk.ModeSetPin, chunkAddr)
if err != nil {
log.Error("Could not pin chunk. Address "+"Address", hex.EncodeToString(chunkAddr))
return err
} else {
log.Trace("Pinning chunk", "Address", hex.EncodeToString(chunkAddr))
}
return nil
}
err = p.walkChunksFromRootHash(addr, isRaw, credentials, walkerFunction)
if err != nil {
log.Error("Error walking root hash.", "Hash", hex.EncodeToString(addr), "err", err)
return nil
}
// Check if the root hash is already pinned and add it to the pinInfo struct
pinInfo, err := p.getPinnedFile(addr)
if err != nil {
// Get the file size from the root chunk first 8 bytes
hashFunc := storage.MakeHashFunc(storage.DefaultHash)
isEncrypted := len(addr) > hashFunc().Size()
getter := storage.NewHasherStore(p.db, hashFunc, isEncrypted, chunk.NewTag(0, "show-chunks-tag", 0, false))
chunkData, err := getter.Get(context.Background(), addr)
if err != nil {
log.Error("Error getting chunk data from localstore.", "Address", hex.EncodeToString(addr))
return nil
}
fileSize := chunkData.Size()
// Get the pin counter from the pinIndex
pinCounter, err := p.getPinCounterOfChunk(chunk.Address(p.removeDecryptionKeyFromChunkHash(addr)))
if err != nil {
log.Error("Error getting pin counter of root hash.", "rootHash", hex.EncodeToString(addr), "err", err)
return nil
}
pinInfo = PinInfo{
Address: addr,
IsRaw: isRaw,
FileSize: fileSize,
PinCounter: pinCounter,
}
} else {
// Get the pin counter from the pinIndex
pinCounter, err := p.getPinCounterOfChunk(chunk.Address(p.removeDecryptionKeyFromChunkHash(addr)))
if err != nil {
log.Error("Error getting pin counter of root hash.", "rootHash", hex.EncodeToString(addr), "err", err)
return nil
}
pinInfo.PinCounter = pinCounter
}
// Store the pinned files in state DB
err = p.savePinnedFile(pinInfo)
if err != nil {
log.Error("Error saving pinned file info to state store.", "rootHash", hex.EncodeToString(addr), "err", err)
return nil
}
log.Debug("File pinned", "Address", hex.EncodeToString(addr))
return nil
}
// UnPinFiles is used to unpin an already pinned file. It takes the root
// hash of the file and walks down the merkle tree unpinning all the chunks
// that are encountered on the way. The pre-requisite is that the file should
// have been already pinned using the PinFiles function. This function can
// be called only from an external command.
func (p *API) UnpinFiles(addr []byte, credentials string) error {
pinInfo, err := p.getPinnedFile(addr)
if err != nil {
log.Error("Root hash is not pinned", "rootHash", hex.EncodeToString(addr), "err", err)
return err
}
// Walk the root hash and unpin all the chunks
walkerFunction := func(ref storage.Reference) error {
chunkAddr := p.removeDecryptionKeyFromChunkHash(ref)
err := p.db.Set(context.Background(), chunk.ModeSetUnpin, chunkAddr)
if err != nil {
log.Error("Could not unpin chunk", "Address", hex.EncodeToString(chunkAddr))
return err
} else {
log.Trace("Unpinning chunk", "Address", hex.EncodeToString(chunkAddr))
}
return nil
}
err = p.walkChunksFromRootHash(addr, pinInfo.IsRaw, credentials, walkerFunction)
if err != nil {
log.Error("Error walking root hash.", "Hash", hex.EncodeToString(addr), "err", err)
return nil
}
// Delete or Update the state DB
pinCounter, err := p.getPinCounterOfChunk(chunk.Address(p.removeDecryptionKeyFromChunkHash(addr)))
if err != nil {
err := p.removePinnedFile(addr)
if err != nil {
log.Error("Error unpinning file.", "rootHash", hex.EncodeToString(addr), "err", err)
return nil
}
} else {
pinInfo.PinCounter = pinCounter
err = p.savePinnedFile(pinInfo)
if err != nil {
log.Error("Error updating file info to state store.", "rootHash", hex.EncodeToString(addr), "err", err)
return nil
}
}
log.Debug("File unpinned", "Address", hex.EncodeToString(addr))
return nil
}
// ListPins functions logs information of all the files that are pinned
// in the current local node. It displays the root hash of the pinned file
// or collection. It also display three vital information's
// 1) Whether the file is a RAW file or not
// 2) Size of the pinned file or collection
// 3) the number of times that particular file or collection is pinned.
func (p *API) ListPins() ([]PinInfo, error) {
pinnedFiles := make([]PinInfo, 0)
iterFunc := func(key []byte, value []byte) (stop bool, err error) {
hash := string(key[4:])
pinInfo := PinInfo{}
err = pinInfo.UnmarshalBinary(value)
if err != nil {
log.Debug("Error unmarshaling pininfo from state store", "Address", hash)
return true, err
}
pinInfo.Address, err = hex.DecodeString(hash)
if err != nil {
log.Debug("Error unmarshaling pininfo from state store", "Address", hash)
return
}
log.Trace("Pinned file", "Address", hash, "IsRAW", pinInfo.IsRaw,
"FileSize", pinInfo.FileSize, "PinCounter", pinInfo.PinCounter)
pinnedFiles = append(pinnedFiles, pinInfo)
return stop, err
}
err := p.state.Iterate("pin_", iterFunc)
if err != nil {
log.Error("Error iterating pinned files", "err", err)
return nil, err
}
return pinnedFiles, nil
}
func (p *API) walkChunksFromRootHash(addr []byte, isRaw bool, credentials string,
executeFunc func(storage.Reference) error) error {
fileHashesC := make(chan storage.Reference, WorkerChanSize)
fileErrC := make(chan error)
var fwg sync.WaitGroup // wait group for file walker reoutine to complete
fwg.Add(1)
go func() {
defer fwg.Done()
if !isRaw {
// If it is not a raw file... load the manifest and add the files inside one by one
walker, err := p.api.NewManifestWalker(context.Background(), storage.Address(addr),
p.api.Decryptor(context.Background(), credentials), nil)
if err != nil {
log.Error("Could not decode manifest.", "err", err)
fileErrC <- err
return
}
err = walker.Walk(func(entry *api.ManifestEntry) error {
fileAddr, err := hex.DecodeString(entry.Hash)
if err != nil {
log.Error("Error decoding hash present in manifest", "err", err)
return err
}
// send the file to file workers
fileHashesC <- storage.Reference(fileAddr)
return nil
})
if err != nil {
log.Error("Error walking manifest", "err", err)
fileErrC <- err
return
}
// Finally, add the root manifest hash too
fileHashesC <- storage.Reference(addr)
// Signal end of file hash stream
close(fileHashesC)
} else {
// Its a raw file.. no manifest.. so process only this hash
fileHashesC <- storage.Reference(addr)
// Signal end of file hash
close(fileHashesC)
}
}()
fwg.Add(1)
go func() {
defer fwg.Done()
for {
select {
case fileRef, ok := <-fileHashesC:
if !ok {
return
}
// Walk the file and its chunks
err := p.walkFile(fileRef, executeFunc, addr)
if err != nil {
fileErrC <- err
return
}
// got error from manifest walker goroutine, so quit file walker too
case <-fileErrC:
return
}
}
}()
go func() {
// Wait for all the chunks to finish execution
fwg.Wait()
// close internal error channel after the file routine is done
close(fileErrC)
}()
return <-fileErrC
}
func (p *API) walkFile(fileRef storage.Reference, executeFunc func(storage.Reference) error, addr []byte) error {
chunkHashesC := make(chan storage.Reference, WorkerChanSize)
chunkErrC := make(chan error)
var cwg sync.WaitGroup // Wait group to wait for chunk routines to complete
actualFileSize := uint64(0)
rcvdFileSize := uint64(0)
var fileSizeLock sync.Mutex // lock to protect the FileSize variables
doneChunkWorker := make(chan struct{})
hashFunc := storage.MakeHashFunc(storage.DefaultHash)
hashSize := len(addr)
isEncrypted := len(addr) > hashFunc().Size()
getter := storage.NewHasherStore(p.db, hashFunc, isEncrypted, chunk.NewTag(0, "show-chunks-tag", 0, false))
// Trigger unwrapping the merkle tree starting from root hash of the file
chunkHashesC <- fileRef
QuitChunkFor:
for {
select {
case <-doneChunkWorker:
break QuitChunkFor
case ref := <-chunkHashesC:
cwg.Add(1)
go func() {
defer cwg.Done()
chunkData, err := getter.Get(context.Background(), ref)
if err != nil {
log.Error("Error getting chunk data from localstore.",
"Address", hex.EncodeToString(ref), "err", err)
chunkErrC <- err
close(doneChunkWorker)
return
}
datalen := len(chunkData)
if datalen < 9 { // Atleast 1 data byte. first 8 bytes are address
log.Error("Invalid chunk data from localstore.",
"Address", hex.EncodeToString(ref), "err", err)
chunkErrC <- errInvalidChunkData
close(doneChunkWorker)
return
}
subTreeSize := chunkData.Size()
fileSizeLock.Lock()
if actualFileSize < subTreeSize {
actualFileSize = subTreeSize
}
fileSizeLock.Unlock()
if subTreeSize > chunk.DefaultSize {
// this is a tree chunk
// load the tree's branches
branches := (datalen - 8) / hashSize
for i := 0; i < branches; i++ {
brAddr := make([]byte, hashSize)
start := (i * hashSize) + 8
end := ((i + 1) * hashSize) + 8
copy(brAddr[:], chunkData[start:end])
chunkHashesC <- storage.Reference(brAddr)
}
} else {
// this is a data chunk
fileSizeLock.Lock()
rcvdFileSize = rcvdFileSize + chunk.DefaultSize
got := rcvdFileSize
need := actualFileSize
fileSizeLock.Unlock()
if got >= need {
close(doneChunkWorker)
}
}
// process the chunk (pin / unpin / display)
err = executeFunc(ref)
if err != nil {
// TODO: if this happens, we should go back and revert the entire file's chunks
log.Error("Error executing walker function",
"Address", hex.EncodeToString(ref), "err", err)
chunkErrC <- err
close(doneChunkWorker)
}
}()
}
}
func() {
// Wait for all the chunks to finish execution
cwg.Wait()
// close internal error channel after all routines are done
close(chunkErrC)
}()
return <-chunkErrC
}
func (p *API) removeDecryptionKeyFromChunkHash(ref []byte) []byte {
// remove the decryption key from the encrypted file hash
isEncrypted := len(ref) > p.hashSize
if isEncrypted {
chunkAddr := make([]byte, p.hashSize)
copy(chunkAddr, ref[0:p.hashSize])
return chunkAddr
}
return ref
}
func (p *API) getPinCounterOfChunk(addr chunk.Address) (uint64, error) {
pinnedChunk, err := p.db.Get(context.Background(), chunk.ModeGetPin, p.removeDecryptionKeyFromChunkHash(addr))
if err != nil {
return 0, err
}
return pinnedChunk.PinCounter(), nil
}
func (p *API) savePinnedFile(pinInfo PinInfo) error {
key := "pin_" + hex.EncodeToString(pinInfo.Address)
err := p.state.Put(key, &pinInfo)
return err
}
func (p *API) removePinnedFile(addr []byte) error {
key := "pin_" + hex.EncodeToString(addr)
err := p.state.Delete(key)
return err
}
func (p *API) getPinnedFile(addr []byte) (PinInfo, error) {
key := "pin_" + hex.EncodeToString(addr)
pinInfo := PinInfo{}
err := p.state.Get(key, &pinInfo)
pinInfo.Address = addr
return pinInfo, err
}