Skip to content

Commit 784aa83

Browse files
kielbarrykaralabe
authored andcommitted
bmt: golint updates for this or self warning (#16628)
* bmt/*: golint updates for this or self warning * Update bmt.go
1 parent fcc18f4 commit 784aa83

File tree

1 file changed

+89
-89
lines changed

1 file changed

+89
-89
lines changed

bmt/bmt.go

Lines changed: 89 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -150,38 +150,38 @@ func NewTreePool(hasher BaseHasher, segmentCount, capacity int) *TreePool {
150150
}
151151

152152
// 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--
159159
}
160160
}
161161

162162
// Reserve is blocking until it returns an available Tree
163163
// 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()
167167
var t *Tree
168-
if self.count == self.Capacity {
169-
return <-self.c
168+
if p.count == p.Capacity {
169+
return <-p.c
170170
}
171171
select {
172-
case t = <-self.c:
172+
case t = <-p.c:
173173
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++
176176
}
177177
return t
178178
}
179179

180180
// Release gives back a Tree to the pool.
181181
// This Tree is guaranteed to be in reusable state
182182
// 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...
185185
}
186186

187187
// Tree is a reusable control structure representing a BMT
@@ -193,17 +193,17 @@ type Tree struct {
193193
}
194194

195195
// 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 {
197197
var left, right []string
198198
var anc []*Node
199-
for i, n := range self.leaves {
199+
for i, n := range t.leaves {
200200
left = append(left, fmt.Sprintf("%v", hashstr(n.left)))
201201
if i%2 == 0 {
202202
anc = append(anc, n.parent)
203203
}
204204
right = append(right, fmt.Sprintf("%v", hashstr(n.right)))
205205
}
206-
anc = self.leaves
206+
anc = t.leaves
207207
var hashes [][]string
208208
for l := 0; len(anc) > 0; l++ {
209209
var nodes []*Node
@@ -277,42 +277,42 @@ func NewTree(hasher BaseHasher, segmentSize, segmentCount int) *Tree {
277277
// methods needed by hash.Hash
278278

279279
// Size returns the size
280-
func (self *Hasher) Size() int {
281-
return self.size
280+
func (h *Hasher) Size() int {
281+
return h.size
282282
}
283283

284284
// 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
287287
}
288288

289289
// Sum returns the hash of the buffer
290290
// hash.Hash interface Sum method appends the byte slice to the underlying
291291
// 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
295295
n := t.leaves[i]
296296
j := i
297297
// must run strictly before all nodes calculate
298298
// 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 {
300300
n = n.parent
301301
} else {
302302
i *= 2
303303
}
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()
308308

309309
// sha3(length + BMT(pure_chunk))
310-
if self.blockLength == nil {
310+
if h.blockLength == nil {
311311
return c
312312
}
313-
res := self.pool.hasher()
313+
res := h.pool.hasher()
314314
res.Reset()
315-
res.Write(self.blockLength)
315+
res.Write(h.blockLength)
316316
res.Write(c)
317317
return res.Sum(nil)
318318
}
@@ -321,25 +321,25 @@ func (self *Hasher) Sum(b []byte) (r []byte) {
321321

322322
// Hash waits for the hasher result and returns it
323323
// 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
326326
}
327327

328328
// Hasher implements the io.Writer interface
329329

330330
// Write fills the buffer to hash
331331
// with every full segment complete launches a hasher go routine
332332
// that shoots up the BMT
333-
func (self *Hasher) Write(b []byte) (int, error) {
333+
func (h *Hasher) Write(b []byte) (int, error) {
334334
l := len(b)
335335
if l <= 0 {
336336
return 0, nil
337337
}
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
343343
if need > size {
344344
size *= 2
345345
}
@@ -356,7 +356,7 @@ func (self *Hasher) Write(b []byte) (int, error) {
356356
// read full segments and the last possibly partial segment
357357
for need > 0 && i < count-1 {
358358
// push all finished chunks we read
359-
self.writeSegment(i, s, self.depth)
359+
h.writeSegment(i, s, h.depth)
360360
need -= size
361361
if need < 0 {
362362
size += need
@@ -365,8 +365,8 @@ func (self *Hasher) Write(b []byte) (int, error) {
365365
rest += size
366366
i++
367367
}
368-
self.segment = s
369-
self.cur = i
368+
h.segment = s
369+
h.cur = i
370370
// otherwise, we can assume len(s) == 0, so all buffer is read and chunk is not yet full
371371
return l, nil
372372
}
@@ -376,16 +376,16 @@ func (self *Hasher) Write(b []byte) (int, error) {
376376
// ReadFrom reads from io.Reader and appends to the data to hash using Write
377377
// it reads so that chunk to hash is maximum length or reader reaches EOF
378378
// 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)
381381
buf := make([]byte, bufsize)
382382
var read int
383383
for {
384384
var n int
385385
n, err = r.Read(buf)
386386
read += n
387387
if err == io.EOF || read == len(buf) {
388-
hash := self.Sum(buf[:n])
388+
hash := h.Sum(buf[:n])
389389
if read == len(buf) {
390390
err = NewEOC(hash)
391391
}
@@ -394,7 +394,7 @@ func (self *Hasher) ReadFrom(r io.Reader) (m int64, err error) {
394394
if err != nil {
395395
break
396396
}
397-
n, err = self.Write(buf[:n])
397+
n, err = h.Write(buf[:n])
398398
if err != nil {
399399
break
400400
}
@@ -403,62 +403,62 @@ func (self *Hasher) ReadFrom(r io.Reader) (m int64, err error) {
403403
}
404404

405405
// 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
409409
}
410410

411411
// Hasher implements the SwarmHash interface
412412

413413
// ResetWithLength needs to be called before writing to the hasher
414414
// the argument is supposed to be the byte slice binary representation of
415415
// 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
419419
}
420420

421421
// Release gives back the Tree to the pool whereby it unlocks
422422
// 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]
426426
for ; n != nil; n = n.parent {
427427
n.unbalanced = false
428428
if n.parent != nil {
429429
n.root = false
430430
}
431431
}
432-
self.pool.Release(self.bmt)
433-
self.bmt = nil
432+
h.pool.Release(h.bmt)
433+
h.bmt = nil
434434

435435
}
436-
self.cur = 0
437-
self.segment = nil
436+
h.cur = 0
437+
h.segment = nil
438438
}
439439

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]
443443

444-
if len(s) > self.size && n.parent != nil {
444+
if len(s) > h.size && n.parent != nil {
445445
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)
449449

450450
if n.root {
451-
self.result <- s
451+
h.result <- s
452452
return
453453
}
454-
self.run(n.parent, h, d, n.index, s)
454+
h.run(n.parent, hash, d, n.index, s)
455455
}()
456456
return
457457
}
458-
go self.run(n, h, d, i*2, s)
458+
go h.run(n, hash, d, i*2, s)
459459
}
460460

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) {
462462
isLeft := i%2 == 0
463463
for {
464464
if isLeft {
@@ -470,18 +470,18 @@ func (self *Hasher) run(n *Node, h hash.Hash, d int, i int, s []byte) {
470470
return
471471
}
472472
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)
477477

478478
} else {
479479
s = append(n.left, n.right...)
480480
}
481481

482-
self.hash = s
482+
h.hash = s
483483
if n.root {
484-
self.result <- s
484+
h.result <- s
485485
return
486486
}
487487

@@ -492,20 +492,20 @@ func (self *Hasher) run(n *Node, h hash.Hash, d int, i int, s []byte) {
492492
}
493493

494494
// 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
498498
}
499-
t := self.pool.Reserve()
500-
self.bmt = t
499+
t := h.pool.Reserve()
500+
h.bmt = t
501501
return t
502502
}
503503

504504
// atomic bool toggle implementing a concurrent reusable 2-state object
505505
// atomic addint with %2 implements atomic bool toggle
506506
// 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
509509
}
510510

511511
func hashstr(b []byte) string {
@@ -525,7 +525,7 @@ func depth(n int) (d int) {
525525

526526
// finalise is following the zigzags on the tree belonging
527527
// 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) {
529529
isLeft := i%2 == 0
530530
for {
531531
// when the final segment's path is going via left segments
@@ -550,8 +550,8 @@ type EOC struct {
550550
}
551551

552552
// 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)
555555
}
556556

557557
// NewEOC creates new end of chunk error with the hash

0 commit comments

Comments
 (0)