lightnodes should be omitted from the addressbook

This commit is contained in:
Mark Vujevits 2018-10-19 11:20:26 +02:00
parent 3fae03a6b7
commit 608ac82f53
2 changed files with 40 additions and 12 deletions

View file

@ -261,7 +261,7 @@ func (k *Kademlia) On(p *Peer) (uint8, bool) {
// found among live peers, do nothing // found among live peers, do nothing
return v return v
}) })
if ins { if ins && !p.BzzPeer.LightNode {
a := newEntry(p.BzzAddr) a := newEntry(p.BzzAddr)
a.conn = p a.conn = p
// insert new online peer into addrs // insert new online peer into addrs
@ -329,6 +329,7 @@ func (k *Kademlia) Off(p *Peer) {
k.lock.Lock() k.lock.Lock()
defer k.lock.Unlock() defer k.lock.Unlock()
var del bool var del bool
if !p.BzzPeer.LightNode {
k.addrs, _, _, _ = pot.Swap(k.addrs, p, pof, func(v pot.Val) pot.Val { k.addrs, _, _, _ = pot.Swap(k.addrs, p, pof, func(v pot.Val) pot.Val {
// v cannot be nil, must check otherwise we overwrite entry // v cannot be nil, must check otherwise we overwrite entry
if v == nil { if v == nil {
@ -337,6 +338,9 @@ func (k *Kademlia) Off(p *Peer) {
del = true del = true
return newEntry(p.BzzAddr) return newEntry(p.BzzAddr)
}) })
} else {
del = true
}
if del { if del {
k.conns, _, _, _ = pot.Swap(k.conns, p, pof, func(_ pot.Val) pot.Val { k.conns, _, _, _ = pot.Swap(k.conns, p, pof, func(_ pot.Val) pot.Val {

View file

@ -46,19 +46,19 @@ func newTestKademlia(b string) *Kademlia {
return NewKademlia(base, params) return NewKademlia(base, params)
} }
func newTestKadPeer(k *Kademlia, s string) *Peer { func newTestKadPeer(k *Kademlia, s string, lightNode bool) *Peer {
return NewPeer(&BzzPeer{BzzAddr: testKadPeerAddr(s)}, k) return NewPeer(&BzzPeer{BzzAddr: testKadPeerAddr(s), LightNode: lightNode}, k)
} }
func On(k *Kademlia, ons ...string) { func On(k *Kademlia, ons ...string) {
for _, s := range ons { for _, s := range ons {
k.On(newTestKadPeer(k, s)) k.On(newTestKadPeer(k, s, false))
} }
} }
func Off(k *Kademlia, offs ...string) { func Off(k *Kademlia, offs ...string) {
for _, s := range offs { for _, s := range offs {
k.Off(newTestKadPeer(k, s)) k.Off(newTestKadPeer(k, s, false))
} }
} }
@ -254,6 +254,30 @@ func TestSuggestPeerFindPeers(t *testing.T) {
} }
func TestOffEffectingAddressBookNormalNode(t *testing.T) {
k := newTestKademlia("00000000")
k.On(newTestKadPeer(k, "01000000", false))
if k.addrs.Size() != 1 {
t.Fatal("known peer addresses should contain 1 entry")
}
k.Off(newTestKadPeer(k, "01000000", false))
if k.addrs.Size() != 1 {
t.Fatal("known peer addresses should contain 1 entry")
}
}
func TestOffEffectingAddressBookLightNode(t *testing.T) {
k := newTestKademlia("00000000")
k.On(newTestKadPeer(k, "01000000", true))
if k.addrs.Size() != 0 {
t.Fatal("known peer addresses should contain 0 entry")
}
k.Off(newTestKadPeer(k, "01000000", true))
if k.addrs.Size() != 0 {
t.Fatal("known peer addresses should contain 0 entry")
}
}
func TestSuggestPeerRetries(t *testing.T) { func TestSuggestPeerRetries(t *testing.T) {
k := newTestKademlia("00000000") k := newTestKademlia("00000000")
k.RetryInterval = int64(300 * time.Millisecond) // cycle k.RetryInterval = int64(300 * time.Millisecond) // cycle