aboutsummaryrefslogtreecommitdiffstats
path: root/p2p/discover/table_test.go
blob: 895c284b270b38f9779e213c6b6191fdaa575aee (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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
// Copyright 2015 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 discover

import (
    "crypto/ecdsa"
    "fmt"
    "math/rand"

    "net"
    "reflect"
    "testing"
    "testing/quick"
    "time"

    "github.com/ethereum/go-ethereum/crypto"
    "github.com/ethereum/go-ethereum/p2p/enode"
    "github.com/ethereum/go-ethereum/p2p/enr"
    "github.com/ethereum/go-ethereum/p2p/netutil"
)

func TestTable_pingReplace(t *testing.T) {
    run := func(newNodeResponding, lastInBucketResponding bool) {
        name := fmt.Sprintf("newNodeResponding=%t/lastInBucketResponding=%t", newNodeResponding, lastInBucketResponding)
        t.Run(name, func(t *testing.T) {
            t.Parallel()
            testPingReplace(t, newNodeResponding, lastInBucketResponding)
        })
    }

    run(true, true)
    run(false, true)
    run(true, false)
    run(false, false)
}

func testPingReplace(t *testing.T, newNodeIsResponding, lastInBucketIsResponding bool) {
    transport := newPingRecorder()
    tab, db := newTestTable(transport)
    defer db.Close()
    defer tab.close()

    <-tab.initDone

    // Fill up the sender's bucket.
    pingKey, _ := crypto.HexToECDSA("45a915e4d060149eb4365960e6a7a45f334393093061116b197e3240065ff2d8")
    pingSender := wrapNode(enode.NewV4(&pingKey.PublicKey, net.IP{}, 99, 99))
    last := fillBucket(tab, pingSender)

    // Add the sender as if it just pinged us. Revalidate should replace the last node in
    // its bucket if it is unresponsive. Revalidate again to ensure that
    transport.dead[last.ID()] = !lastInBucketIsResponding
    transport.dead[pingSender.ID()] = !newNodeIsResponding
    tab.addSeenNode(pingSender)
    tab.doRevalidate(make(chan struct{}, 1))
    tab.doRevalidate(make(chan struct{}, 1))

    if !transport.pinged[last.ID()] {
        // Oldest node in bucket is pinged to see whether it is still alive.
        t.Error("table did not ping last node in bucket")
    }

    tab.mutex.Lock()
    defer tab.mutex.Unlock()
    wantSize := bucketSize
    if !lastInBucketIsResponding && !newNodeIsResponding {
        wantSize--
    }
    if l := len(tab.bucket(pingSender.ID()).entries); l != wantSize {
        t.Errorf("wrong bucket size after bond: got %d, want %d", l, wantSize)
    }
    if found := contains(tab.bucket(pingSender.ID()).entries, last.ID()); found != lastInBucketIsResponding {
        t.Errorf("last entry found: %t, want: %t", found, lastInBucketIsResponding)
    }
    wantNewEntry := newNodeIsResponding && !lastInBucketIsResponding
    if found := contains(tab.bucket(pingSender.ID()).entries, pingSender.ID()); found != wantNewEntry {
        t.Errorf("new entry found: %t, want: %t", found, wantNewEntry)
    }
}

func TestBucket_bumpNoDuplicates(t *testing.T) {
    t.Parallel()
    cfg := &quick.Config{
        MaxCount: 1000,
        Rand:     rand.New(rand.NewSource(time.Now().Unix())),
        Values: func(args []reflect.Value, rand *rand.Rand) {
            // generate a random list of nodes. this will be the content of the bucket.
            n := rand.Intn(bucketSize-1) + 1
            nodes := make([]*node, n)
            for i := range nodes {
                nodes[i] = nodeAtDistance(enode.ID{}, 200, intIP(200))
            }
            args[0] = reflect.ValueOf(nodes)
            // generate random bump positions.
            bumps := make([]int, rand.Intn(100))
            for i := range bumps {
                bumps[i] = rand.Intn(len(nodes))
            }
            args[1] = reflect.ValueOf(bumps)
        },
    }

    prop := func(nodes []*node, bumps []int) (ok bool) {
        tab, db := newTestTable(newPingRecorder())
        defer db.Close()
        defer tab.close()

        b := &bucket{entries: make([]*node, len(nodes))}
        copy(b.entries, nodes)
        for i, pos := range bumps {
            tab.bumpInBucket(b, b.entries[pos])
            if hasDuplicates(b.entries) {
                t.Logf("bucket has duplicates after %d/%d bumps:", i+1, len(bumps))
                for _, n := range b.entries {
                    t.Logf("  %p", n)
                }
                return false
            }
        }
        checkIPLimitInvariant(t, tab)
        return true
    }
    if err := quick.Check(prop, cfg); err != nil {
        t.Error(err)
    }
}

// This checks that the table-wide IP limit is applied correctly.
func TestTable_IPLimit(t *testing.T) {
    transport := newPingRecorder()
    tab, db := newTestTable(transport)
    defer db.Close()
    defer tab.close()

    for i := 0; i < tableIPLimit+1; i++ {
        n := nodeAtDistance(tab.self().ID(), i, net.IP{172, 0, 1, byte(i)})
        tab.addSeenNode(n)
    }
    if tab.len() > tableIPLimit {
        t.Errorf("too many nodes in table")
    }
    checkIPLimitInvariant(t, tab)
}

// This checks that the per-bucket IP limit is applied correctly.
func TestTable_BucketIPLimit(t *testing.T) {
    transport := newPingRecorder()
    tab, db := newTestTable(transport)
    defer db.Close()
    defer tab.close()

    d := 3
    for i := 0; i < bucketIPLimit+1; i++ {
        n := nodeAtDistance(tab.self().ID(), d, net.IP{172, 0, 1, byte(i)})
        tab.addSeenNode(n)
    }
    if tab.len() > bucketIPLimit {
        t.Errorf("too many nodes in table")
    }
    checkIPLimitInvariant(t, tab)
}

// checkIPLimitInvariant checks that ip limit sets contain an entry for every
// node in the table and no extra entries.
func checkIPLimitInvariant(t *testing.T, tab *Table) {
    t.Helper()

    tabset := netutil.DistinctNetSet{Subnet: tableSubnet, Limit: tableIPLimit}
    for _, b := range tab.buckets {
        for _, n := range b.entries {
            tabset.Add(n.IP())
        }
    }
    if tabset.String() != tab.ips.String() {
        t.Errorf("table IP set is incorrect:\nhave: %v\nwant: %v", tab.ips, tabset)
    }
}

func TestTable_closest(t *testing.T) {
    t.Parallel()

    test := func(test *closeTest) bool {
        // for any node table, Target and N
        transport := newPingRecorder()
        tab, db := newTestTable(transport)
        defer db.Close()
        defer tab.close()
        fillTable(tab, test.All)

        // check that closest(Target, N) returns nodes
        result := tab.closest(test.Target, test.N, false).entries
        if hasDuplicates(result) {
            t.Errorf("result contains duplicates")
            return false
        }
        if !sortedByDistanceTo(test.Target, result) {
            t.Errorf("result is not sorted by distance to target")
            return false
        }

        // check that the number of results is min(N, tablen)
        wantN := test.N
        if tlen := tab.len(); tlen < test.N {
            wantN = tlen
        }
        if len(result) != wantN {
            t.Errorf("wrong number of nodes: got %d, want %d", len(result), wantN)
            return false
        } else if len(result) == 0 {
            return true // no need to check distance
        }

        // check that the result nodes have minimum distance to target.
        for _, b := range tab.buckets {
            for _, n := range b.entries {
                if contains(result, n.ID()) {
                    continue // don't run the check below for nodes in result
                }
                farthestResult := result[len(result)-1].ID()
                if enode.DistCmp(test.Target, n.ID(), farthestResult) < 0 {
                    t.Errorf("table contains node that is closer to target but it's not in result")
                    t.Logf("  Target:          %v", test.Target)
                    t.Logf("  Farthest Result: %v", farthestResult)
                    t.Logf("  ID:              %v", n.ID())
                    return false
                }
            }
        }
        return true
    }
    if err := quick.Check(test, quickcfg()); err != nil {
        t.Error(err)
    }
}

func TestTable_ReadRandomNodesGetAll(t *testing.T) {
    cfg := &quick.Config{
        MaxCount: 200,
        Rand:     rand.New(rand.NewSource(time.Now().Unix())),
        Values: func(args []reflect.Value, rand *rand.Rand) {
            args[0] = reflect.ValueOf(make([]*enode.Node, rand.Intn(1000)))
        },
    }
    test := func(buf []*enode.Node) bool {
        transport := newPingRecorder()
        tab, db := newTestTable(transport)
        defer db.Close()
        defer tab.close()
        <-tab.initDone

        for i := 0; i < len(buf); i++ {
            ld := cfg.Rand.Intn(len(tab.buckets))
            fillTable(tab, []*node{nodeAtDistance(tab.self().ID(), ld, intIP(ld))})
        }
        gotN := tab.ReadRandomNodes(buf)
        if gotN != tab.len() {
            t.Errorf("wrong number of nodes, got %d, want %d", gotN, tab.len())
            return false
        }
        if hasDuplicates(wrapNodes(buf[:gotN])) {
            t.Errorf("result contains duplicates")
            return false
        }
        return true
    }
    if err := quick.Check(test, cfg); err != nil {
        t.Error(err)
    }
}

type closeTest struct {
    Self   enode.ID
    Target enode.ID
    All    []*node
    N      int
}

func (*closeTest) Generate(rand *rand.Rand, size int) reflect.Value {
    t := &closeTest{
        Self:   gen(enode.ID{}, rand).(enode.ID),
        Target: gen(enode.ID{}, rand).(enode.ID),
        N:      rand.Intn(bucketSize),
    }
    for _, id := range gen([]enode.ID{}, rand).([]enode.ID) {
        r := new(enr.Record)
        r.Set(enr.IP(genIP(rand)))
        n := wrapNode(enode.SignNull(r, id))
        n.livenessChecks = 1
        t.All = append(t.All, n)
    }
    return reflect.ValueOf(t)
}

func TestTable_addVerifiedNode(t *testing.T) {
    tab, db := newTestTable(newPingRecorder())
    <-tab.initDone
    defer db.Close()
    defer tab.close()

    // Insert two nodes.
    n1 := nodeAtDistance(tab.self().ID(), 256, net.IP{88, 77, 66, 1})
    n2 := nodeAtDistance(tab.self().ID(), 256, net.IP{88, 77, 66, 2})
    tab.addSeenNode(n1)
    tab.addSeenNode(n2)

    // Verify bucket content:
    bcontent := []*node{n1, n2}
    if !reflect.DeepEqual(tab.bucket(n1.ID()).entries, bcontent) {
        t.Fatalf("wrong bucket content: %v", tab.bucket(n1.ID()).entries)
    }

    // Add a changed version of n2.
    newrec := n2.Record()
    newrec.Set(enr.IP{99, 99, 99, 99})
    newn2 := wrapNode(enode.SignNull(newrec, n2.ID()))
    tab.addVerifiedNode(newn2)

    // Check that bucket is updated correctly.
    newBcontent := []*node{newn2, n1}
    if !reflect.DeepEqual(tab.bucket(n1.ID()).entries, newBcontent) {
        t.Fatalf("wrong bucket content after update: %v", tab.bucket(n1.ID()).entries)
    }
    checkIPLimitInvariant(t, tab)
}

func TestTable_addSeenNode(t *testing.T) {
    tab, db := newTestTable(newPingRecorder())
    <-tab.initDone
    defer db.Close()
    defer tab.close()

    // Insert two nodes.
    n1 := nodeAtDistance(tab.self().ID(), 256, net.IP{88, 77, 66, 1})
    n2 := nodeAtDistance(tab.self().ID(), 256, net.IP{88, 77, 66, 2})
    tab.addSeenNode(n1)
    tab.addSeenNode(n2)

    // Verify bucket content:
    bcontent := []*node{n1, n2}
    if !reflect.DeepEqual(tab.bucket(n1.ID()).entries, bcontent) {
        t.Fatalf("wrong bucket content: %v", tab.bucket(n1.ID()).entries)
    }

    // Add a changed version of n2.
    newrec := n2.Record()
    newrec.Set(enr.IP{99, 99, 99, 99})
    newn2 := wrapNode(enode.SignNull(newrec, n2.ID()))
    tab.addSeenNode(newn2)

    // Check that bucket content is unchanged.
    if !reflect.DeepEqual(tab.bucket(n1.ID()).entries, bcontent) {
        t.Fatalf("wrong bucket content after update: %v", tab.bucket(n1.ID()).entries)
    }
    checkIPLimitInvariant(t, tab)
}

// This test checks that ENR updates happen during revalidation. If a node in the table
// announces a new sequence number, the new record should be pulled.
func TestTable_revalidateSyncRecord(t *testing.T) {
    transport := newPingRecorder()
    tab, db := newTestTable(transport)
    <-tab.initDone
    defer db.Close()
    defer tab.close()

    // Insert a node.
    var r enr.Record
    r.Set(enr.IP(net.IP{127, 0, 0, 1}))
    id := enode.ID{1}
    n1 := wrapNode(enode.SignNull(&r, id))
    tab.addSeenNode(n1)

    // Update the node record.
    r.Set(enr.WithEntry("foo", "bar"))
    n2 := enode.SignNull(&r, id)
    transport.updateRecord(n2)

    tab.doRevalidate(make(chan struct{}, 1))
    intable := tab.getNode(id)
    if !reflect.DeepEqual(intable, n2) {
        t.Fatalf("table contains old record with seq %d, want seq %d", intable.Seq(), n2.Seq())
    }
}

// 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()
}

func genIP(rand *rand.Rand) net.IP {
    ip := make(net.IP, 4)
    rand.Read(ip)
    return ip
}

func quickcfg() *quick.Config {
    return &quick.Config{
        MaxCount: 5000,
        Rand:     rand.New(rand.NewSource(time.Now().Unix())),
    }
}

func newkey() *ecdsa.PrivateKey {
    key, err := crypto.GenerateKey()
    if err != nil {
        panic("couldn't generate key: " + err.Error())
    }
    return key
}