aboutsummaryrefslogtreecommitdiffstats
path: root/swarm/network/kademlia/kademlia_test.go
blob: 417ccecae6f0974ed04ba9766a4edb2384e3308e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
// Copyright 2016 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

package kademlia

import (
    "fmt"
    "math"
    "math/rand"
    "os"
    "path/filepath"
    "reflect"
    "testing"
    "testing/quick"
    "time"
)

var (
    quickrand           = rand.New(rand.NewSource(time.Now().Unix()))
    quickcfgFindClosest = &quick.Config{MaxCount: 50, Rand: quickrand}
    quickcfgBootStrap   = &quick.Config{MaxCount: 100, Rand: quickrand}
)

type testNode struct {
    addr Address
}

func (n *testNode) String() string {
    return fmt.Sprintf("%x", n.addr[:])
}

func (n *testNode) Addr() Address {
    return n.addr
}

func (n *testNode) Drop() {
}

func (n *testNode) Url() string {
    return ""
}

func (n *testNode) LastActive() time.Time {
    return time.Now()
}

func TestOn(t *testing.T) {
    addr, ok1 := gen(Address{}, quickrand).(Address)
    other, ok2 := gen(Address{}, quickrand).(Address)
    if !ok1 || !ok2 {
        t.Errorf("oops")
    }
    kad := New(addr, NewKadParams())
    err := kad.On(&testNode{addr: other}, nil)
    _ = err
}

func TestBootstrap(t *testing.T) {

    test := func(test *bootstrapTest) bool {
        // for any node kad.le, Target and N
        params := NewKadParams()
        params.MaxProx = test.MaxProx
        params.BucketSize = test.BucketSize
        params.ProxBinSize = test.BucketSize
        kad := New(test.Self, params)
        var err error

        for p := 0; p < 9; p++ {
            var nrs []*NodeRecord
            n := math.Pow(float64(2), float64(7-p))
            for i := 0; i < int(n); i++ {
                addr := RandomAddressAt(test.Self, p)
                nrs = append(nrs, &NodeRecord{
                    Addr: addr,
                })
            }
            kad.Add(nrs)
        }

        node := &testNode{test.Self}

        n := 0
        for n < 100 {
            err = kad.On(node, nil)
            if err != nil {
                t.Fatalf("backend not accepting node: %v", err)
            }

            record, need, _ := kad.Suggest()
            if !need {
                break
            }
            n++
            if record == nil {
                continue
            }
            node = &testNode{record.Addr}
        }
        exp := test.BucketSize * (test.MaxProx + 1)
        if kad.Count() != exp {
            t.Errorf("incorrect number of peers, expected %d, got %d\n%v", exp, kad.Count(), kad)
            return false
        }
        return true
    }
    if err := quick.Check(test, quickcfgBootStrap); err != nil {
        t.Error(err)
    }

}

func TestFindClosest(t *testing.T) {

    test := func(test *FindClosestTest) bool {
        // for any node kad.le, Target and N
        params := NewKadParams()
        params.MaxProx = 7
        kad := New(test.Self, params)
        var err error
        for _, node := range test.All {
            err = kad.On(node, nil)
            if err != nil && err.Error() != "bucket full" {
                t.Fatalf("backend not accepting node: %v", err)
            }
        }

        if len(test.All) == 0 || test.N == 0 {
            return true
        }
        nodes := kad.FindClosest(test.Target, test.N)

        // check that the number of results is min(N, kad.len)
        wantN := test.N
        if tlen := kad.Count(); tlen < test.N {
            wantN = tlen
        }

        if len(nodes) != wantN {
            t.Errorf("wrong number of nodes: got %d, want %d", len(nodes), wantN)
            return false
        }

        if hasDuplicates(nodes) {
            t.Errorf("result contains duplicates")
            return false
        }

        if !sortedByDistanceTo(test.Target, nodes) {
            t.Errorf("result is not sorted by distance to target")
            return false
        }

        // check that the result nodes have minimum distance to target.
        farthestResult := nodes[len(nodes)-1].Addr()
        for i, b := range kad.buckets {
            for j, n := range b {
                if contains(nodes, n.Addr()) {
                    continue // don't run the check below for nodes in result
                }
                if test.Target.ProxCmp(n.Addr(), farthestResult) < 0 {
                    _ = i * j
                    t.Errorf("kad.le contains node that is closer to target but it's not in result")
                    return false
                }
            }
        }
        return true
    }
    if err := quick.Check(test, quickcfgFindClosest); err != nil {
        t.Error(err)
    }
}

type proxTest struct {
    add   bool
    index int
    addr  Address
}

var (
    addresses []Address
)

func TestProxAdjust(t *testing.T) {
    r := rand.New(rand.NewSource(time.Now().UnixNano()))
    self := gen(Address{}, r).(Address)
    params := NewKadParams()
    params.MaxProx = 7
    kad := New(self, params)

    var err error
    for i := 0; i < 100; i++ {
        a := gen(Address{}, r).(Address)
        addresses = append(addresses, a)
        err = kad.On(&testNode{addr: a}, nil)
        if err != nil && err.Error() != "bucket full" {
            t.Fatalf("backend not accepting node: %v", err)
        }
        if !kad.proxCheck(t) {
            return
        }
    }
    test := func(test *proxTest) bool {
        node := &testNode{test.addr}
        if test.add {
            kad.On(node, nil)
        } else {
            kad.Off(node, nil)
        }
        return kad.proxCheck(t)
    }
    if err := quick.Check(test, quickcfgFindClosest); err != nil {
        t.Error(err)
    }
}

func TestSaveLoad(t *testing.T) {
    r := rand.New(rand.NewSource(time.Now().UnixNano()))
    addresses := gen([]Address{}, r).([]Address)
    self := RandomAddress()
    params := NewKadParams()
    params.MaxProx = 7
    kad := New(self, params)

    var err error

    for _, a := range addresses {
        err = kad.On(&testNode{addr: a}, nil)
        if err != nil && err.Error() != "bucket full" {
            t.Fatalf("backend not accepting node: %v", err)
        }
    }
    nodes := kad.FindClosest(self, 100)

    path := filepath.Join(os.TempDir(), "bzz-kad-test-save-load.peers")
    err = kad.Save(path, nil)
    if err != nil && err.Error() != "bucket full" {
        t.Fatalf("unepected error saving kaddb: %v", err)
    }
    kad = New(self, params)
    err = kad.Load(path, nil)
    if err != nil && err.Error() != "bucket full" {
        t.Fatalf("unepected error loading kaddb: %v", err)
    }
    for _, b := range kad.db.Nodes {
        for _, node := range b {
            err = kad.On(&testNode{node.Addr}, nil)
            if err != nil && err.Error() != "bucket full" {
                t.Fatalf("backend not accepting node: %v", err)
            }
        }
    }
    loadednodes := kad.FindClosest(self, 100)
    for i, node := range loadednodes {
        if nodes[i].Addr() != node.Addr() {
            t.Errorf("node mismatch at %d/%d: %v != %v", i, len(nodes), nodes[i].Addr(), node.Addr())
        }
    }
}

func (self *Kademlia) proxCheck(t *testing.T) bool {
    var sum int
    for i, b := range self.buckets {
        l := len(b)
        // if we are in the high prox multibucket
        if i >= self.proxLimit {
            sum += l
        } else if l == 0 {
            t.Errorf("bucket %d empty, yet proxLimit is %d\n%v", len(b), self.proxLimit, self)
            return false
        }
    }
    // check if merged high prox bucket does not exceed size
    if sum > 0 {
        if sum != self.proxSize {
            t.Errorf("proxSize incorrect, expected %v, got %v", sum, self.proxSize)
            return false
        }
        last := len(self.buckets[self.proxLimit])
        if last > 0 && sum >= self.ProxBinSize+last {
            t.Errorf("proxLimit %v incorrect, redundant non-empty bucket %d added to proxBin with %v (target %v)\n%v", self.proxLimit, last, sum-last, self.ProxBinSize, self)
            return false
        }
        if self.proxLimit > 0 && sum < self.ProxBinSize {
            t.Errorf("proxLimit %v incorrect. proxSize %v is less than target %v, yet there is more peers", self.proxLimit, sum, self.ProxBinSize)
            return false
        }
    }
    return true
}

type bootstrapTest struct {
    MaxProx    int
    BucketSize int
    Self       Address
}

func (*bootstrapTest) Generate(rand *rand.Rand, size int) reflect.Value {
    t := &bootstrapTest{
        Self:       gen(Address{}, rand).(Address),
        MaxProx:    5 + rand.Intn(2),
        BucketSize: rand.Intn(3) + 1,
    }
    return reflect.ValueOf(t)
}

type FindClosestTest struct {
    Self   Address
    Target Address
    All    []Node
    N      int
}

func (c FindClosestTest) String() string {
    return fmt.Sprintf("A: %064x\nT: %064x\n(%d)\n", c.Self[:], c.Target[:], c.N)
}

func (*FindClosestTest) Generate(rand *rand.Rand, size int) reflect.Value {
    t := &FindClosestTest{
        Self:   gen(Address{}, rand).(Address),
        Target: gen(Address{}, rand).(Address),
        N:      rand.Intn(bucketSize),
    }
    for _, a := range gen([]Address{}, rand).([]Address) {
        t.All = append(t.All, &testNode{addr: a})
    }
    return reflect.ValueOf(t)
}

func (*proxTest) Generate(rand *rand.Rand, size int) reflect.Value {
    var add bool
    if rand.Intn(1) == 0 {
        add = true
    }
    var t *proxTest
    if add {
        t = &proxTest{
            addr: gen(Address{}, rand).(Address),
            add:  add,
        }
    } else {
        t = &proxTest{
            index: rand.Intn(len(addresses)),
            add:   add,
        }
    }
    return reflect.ValueOf(t)
}

func hasDuplicates(slice []Node) bool {
    seen := make(map[Address]bool)
    for _, node := range slice {
        if seen[node.Addr()] {
            return true
        }
        seen[node.Addr()] = true
    }
    return false
}

func contains(nodes []Node, addr Address) bool {
    for _, n := range nodes {
        if n.Addr() == addr {
            return true
        }
    }
    return false
}

// gen wraps quick.Value so it's easier to use.
// it generates a random value of the given value's type.
func gen(typ interface{}, rand *rand.Rand) interface{} {
    v, ok := quick.Value(reflect.TypeOf(typ), rand)
    if !ok {
        panic(fmt.Sprintf("couldn't generate random value of type %T", typ))
    }
    return v.Interface()
}