swarm/network/kademlia: fix index out of range when deleting last idle peer from kaddb

This commit is contained in:
zelig 2016-06-11 17:10:08 +01:00
parent 74cc29789a
commit 329c9b1c5d
2 changed files with 4 additions and 40 deletions

View file

@ -247,14 +247,14 @@ func (self *KadDb) delete(row int, indexes ...int) {
dbrow := self.Nodes[row]
for _, next := range indexes {
// need to adjust dbcursor
if next > 0 {
if next <= self.cursors[row] {
self.cursors[row]--
}
if next > 0 {
nodes = append(nodes, dbrow[prev:next]...)
}
prev = next + 1
delete(self.index, dbrow[next].Addr)
prev = next + 1
}
self.Nodes[row] = append(nodes, dbrow[prev:]...)
}

View file

@ -3,7 +3,6 @@
package storage
import (
"bytes"
"sync"
)
@ -44,41 +43,6 @@ func NewMemStore(d *DbStore, capacity uint) (m *MemStore) {
return
}
func (x Key) Size() uint {
return uint(len(x))
}
func (x Key) isEqual(y Key) bool {
return bytes.Compare(x, y) == 0
}
func (h Key) bits(i, j uint) uint {
ii := i >> 3
jj := i & 7
if ii >= h.Size() {
return 0
}
if jj+j <= 8 {
return uint((h[ii] >> jj) & ((1 << j) - 1))
}
res := uint(h[ii] >> jj)
jj = 8 - jj
j -= jj
for j != 0 {
ii++
if j < 8 {
res += uint(h[ii]&((1<<j)-1)) << jj
return res
}
res += uint(h[ii]) << jj
jj += 8
j -= 8
}
return res
}
type memTree struct {
subtree []*memTree
parent *memTree