Skip to content

Commit

Permalink
skademlia: add simple test for creating a new S/Kademlia block, and c…
Browse files Browse the repository at this point in the history
…heck prefix diff before updating table/logging a peers activity
  • Loading branch information
iwasaki-kenta committed Feb 17, 2019
1 parent 807360d commit 28a6158
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 8 deletions.
33 changes: 25 additions & 8 deletions skademlia/mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import (
)

const (
DefaultPrefixDiffLen = 128
DefaultPrefixDiffMin = 32

keyKademliaTable = "kademlia.table"
keyAuthChannel = "kademlia.auth.ch"
)
Expand All @@ -27,10 +30,12 @@ type block struct {
enforceSignatures bool

c1, c2 int

prefixDiffLen, prefixDiffMin int
}

func New() *block {
return &block{enforceSignatures: false, c1: DefaultC1, c2: DefaultC2}
return &block{enforceSignatures: false, c1: DefaultC1, c2: DefaultC2, prefixDiffLen: DefaultPrefixDiffLen, prefixDiffMin: DefaultPrefixDiffMin}
}

func (b *block) EnforceSignatures() *block {
Expand All @@ -48,6 +53,16 @@ func (b *block) WithC2(c2 int) *block {
return b
}

func (b *block) WithPrefixDiffLen(prefixDiffLen int) *block {
b.prefixDiffLen = prefixDiffLen
return b
}

func (b *block) WithPrefixDiffMin(prefixDiffMin int) *block {
b.prefixDiffMin = prefixDiffMin
return b
}

func (b *block) OnRegister(p *protocol.Protocol, node *noise.Node) {
b.opcodePing = noise.RegisterMessage(noise.NextAvailableOpcode(), (*Ping)(nil))
b.opcodeEvict = noise.RegisterMessage(noise.NextAvailableOpcode(), (*Evict)(nil))
Expand Down Expand Up @@ -91,10 +106,10 @@ func (b *block) OnBegin(p *protocol.Protocol, peer *noise.Peer) error {
enforceSignatures(peer, b.enforceSignatures)

// Log peer into S/Kademlia table, and have all messages update the S/Kademlia table.
_ = logPeerActivity(peer)
_ = b.logPeerActivity(peer)

peer.BeforeMessageReceived(func(node *noise.Node, peer *noise.Peer, msg []byte) (i []byte, e error) {
return msg, logPeerActivity(peer)
return msg, b.logPeerActivity(peer)
})

go b.handleLookups(peer)
Expand All @@ -112,12 +127,14 @@ func (b *block) OnEnd(p *protocol.Protocol, peer *noise.Peer) error {
return nil
}

func logPeerActivity(peer *noise.Peer) error {
err := UpdateTable(peer.Node(), protocol.PeerID(peer))
func (b *block) logPeerActivity(peer *noise.Peer) error {
if prefixDiff(protocol.NodeID(peer.Node()).Hash(), protocol.PeerID(peer).Hash(), b.prefixDiffLen) > b.prefixDiffMin {
err := UpdateTable(peer.Node(), protocol.PeerID(peer))

if err != nil {
return errors.Wrap(errors.Wrap(protocol.DisconnectPeer, err.Error()),
"kademlia: failed to update table with peer ID")
if err != nil {
return errors.Wrap(errors.Wrap(protocol.DisconnectPeer, err.Error()),
"kademlia: failed to update table with peer ID")
}
}

return nil
Expand Down
33 changes: 33 additions & 0 deletions skademlia/mod_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package skademlia

import (
"github.com/stretchr/testify/assert"
"testing"
"testing/quick"
)

func TestNew(t *testing.T) {
block := New()

assert.Equal(t, false, block.enforceSignatures)
assert.Equal(t, DefaultC1, block.c1)
assert.Equal(t, DefaultC2, block.c2)
assert.Equal(t, DefaultPrefixDiffLen, block.prefixDiffLen)
assert.Equal(t, DefaultPrefixDiffMin, block.prefixDiffMin)

assert.NoError(t, quick.Check(func(c1 int) bool {
return block.WithC1(c1).c1 == c1
}, nil))

assert.NoError(t, quick.Check(func(c2 int) bool {
return block.WithC2(c2).c2 == c2
}, nil))

assert.NoError(t, quick.Check(func(prefixDiffLen int) bool {
return block.WithPrefixDiffLen(prefixDiffLen).prefixDiffLen == prefixDiffLen
}, nil))

assert.NoError(t, quick.Check(func(prefixDiffMin int) bool {
return block.WithPrefixDiffMin(prefixDiffMin).prefixDiffMin == prefixDiffMin
}, nil))
}

0 comments on commit 28a6158

Please sign in to comment.