@@ -150,38 +150,38 @@ func NewTreePool(hasher BaseHasher, segmentCount, capacity int) *TreePool {
150
150
}
151
151
152
152
// Drain drains the pool until it has no more than n resources
153
- func (self * TreePool ) Drain (n int ) {
154
- self .lock .Lock ()
155
- defer self .lock .Unlock ()
156
- for len (self .c ) > n {
157
- <- self .c
158
- self .count --
153
+ func (p * TreePool ) Drain (n int ) {
154
+ p .lock .Lock ()
155
+ defer p .lock .Unlock ()
156
+ for len (p .c ) > n {
157
+ <- p .c
158
+ p .count --
159
159
}
160
160
}
161
161
162
162
// Reserve is blocking until it returns an available Tree
163
163
// it reuses free Trees or creates a new one if size is not reached
164
- func (self * TreePool ) Reserve () * Tree {
165
- self .lock .Lock ()
166
- defer self .lock .Unlock ()
164
+ func (p * TreePool ) Reserve () * Tree {
165
+ p .lock .Lock ()
166
+ defer p .lock .Unlock ()
167
167
var t * Tree
168
- if self .count == self .Capacity {
169
- return <- self .c
168
+ if p .count == p .Capacity {
169
+ return <- p .c
170
170
}
171
171
select {
172
- case t = <- self .c :
172
+ case t = <- p .c :
173
173
default :
174
- t = NewTree (self .hasher , self .SegmentSize , self .SegmentCount )
175
- self .count ++
174
+ t = NewTree (p .hasher , p .SegmentSize , p .SegmentCount )
175
+ p .count ++
176
176
}
177
177
return t
178
178
}
179
179
180
180
// Release gives back a Tree to the pool.
181
181
// This Tree is guaranteed to be in reusable state
182
182
// does not need locking
183
- func (self * TreePool ) Release (t * Tree ) {
184
- self .c <- t // can never fail but...
183
+ func (p * TreePool ) Release (t * Tree ) {
184
+ p .c <- t // can never fail but...
185
185
}
186
186
187
187
// Tree is a reusable control structure representing a BMT
@@ -193,17 +193,17 @@ type Tree struct {
193
193
}
194
194
195
195
// Draw draws the BMT (badly)
196
- func (self * Tree ) Draw (hash []byte , d int ) string {
196
+ func (t * Tree ) Draw (hash []byte , d int ) string {
197
197
var left , right []string
198
198
var anc []* Node
199
- for i , n := range self .leaves {
199
+ for i , n := range t .leaves {
200
200
left = append (left , fmt .Sprintf ("%v" , hashstr (n .left )))
201
201
if i % 2 == 0 {
202
202
anc = append (anc , n .parent )
203
203
}
204
204
right = append (right , fmt .Sprintf ("%v" , hashstr (n .right )))
205
205
}
206
- anc = self .leaves
206
+ anc = t .leaves
207
207
var hashes [][]string
208
208
for l := 0 ; len (anc ) > 0 ; l ++ {
209
209
var nodes []* Node
@@ -277,42 +277,42 @@ func NewTree(hasher BaseHasher, segmentSize, segmentCount int) *Tree {
277
277
// methods needed by hash.Hash
278
278
279
279
// Size returns the size
280
- func (self * Hasher ) Size () int {
281
- return self .size
280
+ func (h * Hasher ) Size () int {
281
+ return h .size
282
282
}
283
283
284
284
// BlockSize returns the block size
285
- func (self * Hasher ) BlockSize () int {
286
- return self .blocksize
285
+ func (h * Hasher ) BlockSize () int {
286
+ return h .blocksize
287
287
}
288
288
289
289
// Sum returns the hash of the buffer
290
290
// hash.Hash interface Sum method appends the byte slice to the underlying
291
291
// data before it calculates and returns the hash of the chunk
292
- func (self * Hasher ) Sum (b []byte ) (r []byte ) {
293
- t := self .bmt
294
- i := self .cur
292
+ func (h * Hasher ) Sum (b []byte ) (r []byte ) {
293
+ t := h .bmt
294
+ i := h .cur
295
295
n := t .leaves [i ]
296
296
j := i
297
297
// must run strictly before all nodes calculate
298
298
// datanodes are guaranteed to have a parent
299
- if len (self .segment ) > self .size && i > 0 && n .parent != nil {
299
+ if len (h .segment ) > h .size && i > 0 && n .parent != nil {
300
300
n = n .parent
301
301
} else {
302
302
i *= 2
303
303
}
304
- d := self .finalise (n , i )
305
- self .writeSegment (j , self .segment , d )
306
- c := <- self .result
307
- self .releaseTree ()
304
+ d := h .finalise (n , i )
305
+ h .writeSegment (j , h .segment , d )
306
+ c := <- h .result
307
+ h .releaseTree ()
308
308
309
309
// sha3(length + BMT(pure_chunk))
310
- if self .blockLength == nil {
310
+ if h .blockLength == nil {
311
311
return c
312
312
}
313
- res := self .pool .hasher ()
313
+ res := h .pool .hasher ()
314
314
res .Reset ()
315
- res .Write (self .blockLength )
315
+ res .Write (h .blockLength )
316
316
res .Write (c )
317
317
return res .Sum (nil )
318
318
}
@@ -321,25 +321,25 @@ func (self *Hasher) Sum(b []byte) (r []byte) {
321
321
322
322
// Hash waits for the hasher result and returns it
323
323
// caller must call this on a BMT Hasher being written to
324
- func (self * Hasher ) Hash () []byte {
325
- return <- self .result
324
+ func (h * Hasher ) Hash () []byte {
325
+ return <- h .result
326
326
}
327
327
328
328
// Hasher implements the io.Writer interface
329
329
330
330
// Write fills the buffer to hash
331
331
// with every full segment complete launches a hasher go routine
332
332
// that shoots up the BMT
333
- func (self * Hasher ) Write (b []byte ) (int , error ) {
333
+ func (h * Hasher ) Write (b []byte ) (int , error ) {
334
334
l := len (b )
335
335
if l <= 0 {
336
336
return 0 , nil
337
337
}
338
- s := self .segment
339
- i := self .cur
340
- count := (self .count + 1 ) / 2
341
- need := self .count * self .size - self .cur * 2 * self .size
342
- size := self .size
338
+ s := h .segment
339
+ i := h .cur
340
+ count := (h .count + 1 ) / 2
341
+ need := h .count * h .size - h .cur * 2 * h .size
342
+ size := h .size
343
343
if need > size {
344
344
size *= 2
345
345
}
@@ -356,7 +356,7 @@ func (self *Hasher) Write(b []byte) (int, error) {
356
356
// read full segments and the last possibly partial segment
357
357
for need > 0 && i < count - 1 {
358
358
// push all finished chunks we read
359
- self .writeSegment (i , s , self .depth )
359
+ h .writeSegment (i , s , h .depth )
360
360
need -= size
361
361
if need < 0 {
362
362
size += need
@@ -365,8 +365,8 @@ func (self *Hasher) Write(b []byte) (int, error) {
365
365
rest += size
366
366
i ++
367
367
}
368
- self .segment = s
369
- self .cur = i
368
+ h .segment = s
369
+ h .cur = i
370
370
// otherwise, we can assume len(s) == 0, so all buffer is read and chunk is not yet full
371
371
return l , nil
372
372
}
@@ -376,16 +376,16 @@ func (self *Hasher) Write(b []byte) (int, error) {
376
376
// ReadFrom reads from io.Reader and appends to the data to hash using Write
377
377
// it reads so that chunk to hash is maximum length or reader reaches EOF
378
378
// caller must Reset the hasher prior to call
379
- func (self * Hasher ) ReadFrom (r io.Reader ) (m int64 , err error ) {
380
- bufsize := self .size * self .count - self .size * self .cur - len (self .segment )
379
+ func (h * Hasher ) ReadFrom (r io.Reader ) (m int64 , err error ) {
380
+ bufsize := h .size * h .count - h .size * h .cur - len (h .segment )
381
381
buf := make ([]byte , bufsize )
382
382
var read int
383
383
for {
384
384
var n int
385
385
n , err = r .Read (buf )
386
386
read += n
387
387
if err == io .EOF || read == len (buf ) {
388
- hash := self .Sum (buf [:n ])
388
+ hash := h .Sum (buf [:n ])
389
389
if read == len (buf ) {
390
390
err = NewEOC (hash )
391
391
}
@@ -394,7 +394,7 @@ func (self *Hasher) ReadFrom(r io.Reader) (m int64, err error) {
394
394
if err != nil {
395
395
break
396
396
}
397
- n , err = self .Write (buf [:n ])
397
+ n , err = h .Write (buf [:n ])
398
398
if err != nil {
399
399
break
400
400
}
@@ -403,62 +403,62 @@ func (self *Hasher) ReadFrom(r io.Reader) (m int64, err error) {
403
403
}
404
404
405
405
// Reset needs to be called before writing to the hasher
406
- func (self * Hasher ) Reset () {
407
- self .getTree ()
408
- self .blockLength = nil
406
+ func (h * Hasher ) Reset () {
407
+ h .getTree ()
408
+ h .blockLength = nil
409
409
}
410
410
411
411
// Hasher implements the SwarmHash interface
412
412
413
413
// ResetWithLength needs to be called before writing to the hasher
414
414
// the argument is supposed to be the byte slice binary representation of
415
415
// the length of the data subsumed under the hash
416
- func (self * Hasher ) ResetWithLength (l []byte ) {
417
- self .Reset ()
418
- self .blockLength = l
416
+ func (h * Hasher ) ResetWithLength (l []byte ) {
417
+ h .Reset ()
418
+ h .blockLength = l
419
419
}
420
420
421
421
// Release gives back the Tree to the pool whereby it unlocks
422
422
// it resets tree, segment and index
423
- func (self * Hasher ) releaseTree () {
424
- if self .bmt != nil {
425
- n := self .bmt .leaves [self .cur ]
423
+ func (h * Hasher ) releaseTree () {
424
+ if h .bmt != nil {
425
+ n := h .bmt .leaves [h .cur ]
426
426
for ; n != nil ; n = n .parent {
427
427
n .unbalanced = false
428
428
if n .parent != nil {
429
429
n .root = false
430
430
}
431
431
}
432
- self .pool .Release (self .bmt )
433
- self .bmt = nil
432
+ h .pool .Release (h .bmt )
433
+ h .bmt = nil
434
434
435
435
}
436
- self .cur = 0
437
- self .segment = nil
436
+ h .cur = 0
437
+ h .segment = nil
438
438
}
439
439
440
- func (self * Hasher ) writeSegment (i int , s []byte , d int ) {
441
- h := self .pool .hasher ()
442
- n := self .bmt .leaves [i ]
440
+ func (h * Hasher ) writeSegment (i int , s []byte , d int ) {
441
+ hash := h .pool .hasher ()
442
+ n := h .bmt .leaves [i ]
443
443
444
- if len (s ) > self .size && n .parent != nil {
444
+ if len (s ) > h .size && n .parent != nil {
445
445
go func () {
446
- h .Reset ()
447
- h .Write (s )
448
- s = h .Sum (nil )
446
+ hash .Reset ()
447
+ hash .Write (s )
448
+ s = hash .Sum (nil )
449
449
450
450
if n .root {
451
- self .result <- s
451
+ h .result <- s
452
452
return
453
453
}
454
- self .run (n .parent , h , d , n .index , s )
454
+ h .run (n .parent , hash , d , n .index , s )
455
455
}()
456
456
return
457
457
}
458
- go self .run (n , h , d , i * 2 , s )
458
+ go h .run (n , hash , d , i * 2 , s )
459
459
}
460
460
461
- func (self * Hasher ) run (n * Node , h hash.Hash , d int , i int , s []byte ) {
461
+ func (h * Hasher ) run (n * Node , hash hash.Hash , d int , i int , s []byte ) {
462
462
isLeft := i % 2 == 0
463
463
for {
464
464
if isLeft {
@@ -470,18 +470,18 @@ func (self *Hasher) run(n *Node, h hash.Hash, d int, i int, s []byte) {
470
470
return
471
471
}
472
472
if ! n .unbalanced || ! isLeft || i == 0 && d == 0 {
473
- h .Reset ()
474
- h .Write (n .left )
475
- h .Write (n .right )
476
- s = h .Sum (nil )
473
+ hash .Reset ()
474
+ hash .Write (n .left )
475
+ hash .Write (n .right )
476
+ s = hash .Sum (nil )
477
477
478
478
} else {
479
479
s = append (n .left , n .right ... )
480
480
}
481
481
482
- self .hash = s
482
+ h .hash = s
483
483
if n .root {
484
- self .result <- s
484
+ h .result <- s
485
485
return
486
486
}
487
487
@@ -492,20 +492,20 @@ func (self *Hasher) run(n *Node, h hash.Hash, d int, i int, s []byte) {
492
492
}
493
493
494
494
// getTree obtains a BMT resource by reserving one from the pool
495
- func (self * Hasher ) getTree () * Tree {
496
- if self .bmt != nil {
497
- return self .bmt
495
+ func (h * Hasher ) getTree () * Tree {
496
+ if h .bmt != nil {
497
+ return h .bmt
498
498
}
499
- t := self .pool .Reserve ()
500
- self .bmt = t
499
+ t := h .pool .Reserve ()
500
+ h .bmt = t
501
501
return t
502
502
}
503
503
504
504
// atomic bool toggle implementing a concurrent reusable 2-state object
505
505
// atomic addint with %2 implements atomic bool toggle
506
506
// it returns true if the toggler just put it in the active/waiting state
507
- func (self * Node ) toggle () bool {
508
- return atomic .AddInt32 (& self .state , 1 )% 2 == 1
507
+ func (n * Node ) toggle () bool {
508
+ return atomic .AddInt32 (& n .state , 1 )% 2 == 1
509
509
}
510
510
511
511
func hashstr (b []byte ) string {
@@ -525,7 +525,7 @@ func depth(n int) (d int) {
525
525
526
526
// finalise is following the zigzags on the tree belonging
527
527
// to the final datasegment
528
- func (self * Hasher ) finalise (n * Node , i int ) (d int ) {
528
+ func (h * Hasher ) finalise (n * Node , i int ) (d int ) {
529
529
isLeft := i % 2 == 0
530
530
for {
531
531
// when the final segment's path is going via left segments
@@ -550,8 +550,8 @@ type EOC struct {
550
550
}
551
551
552
552
// Error returns the error string
553
- func (self * EOC ) Error () string {
554
- return fmt .Sprintf ("hasher limit reached, chunk hash: %x" , self .Hash )
553
+ func (e * EOC ) Error () string {
554
+ return fmt .Sprintf ("hasher limit reached, chunk hash: %x" , e .Hash )
555
555
}
556
556
557
557
// NewEOC creates new end of chunk error with the hash
0 commit comments