aboutsummaryrefslogtreecommitdiffstats
path: root/dex/nodetable_test.go
blob: 06078a0d8bc01c291a506a4aff8d12065f40447c (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
package dex

import (
    "crypto/ecdsa"
    "net"
    "testing"
    "time"

    "github.com/dexon-foundation/dexon/common"
    "github.com/dexon-foundation/dexon/crypto"
    "github.com/dexon-foundation/dexon/p2p/enode"
    "github.com/dexon-foundation/dexon/p2p/enr"
)

func TestNodeTable(t *testing.T) {
    table := newNodeTable()
    ch := make(chan newRecordsEvent)
    table.SubscribeNewRecordsEvent(ch)

    records1 := []*enr.Record{
        randomNode().Record(),
        randomNode().Record(),
    }

    records2 := []*enr.Record{
        randomNode().Record(),
        randomNode().Record(),
    }

    go table.AddRecords(records1)

    select {
    case newRecords := <-ch:
        m := map[common.Hash]struct{}{}
        for _, record := range newRecords.Records {
            m[rlpHash(record)] = struct{}{}
        }

        if len(m) != len(records1) {
            t.Errorf("len mismatch: got %d, want: %d",
                len(m), len(records1))
        }

        for _, record := range records1 {
            if _, ok := m[rlpHash(record)]; !ok {
                t.Errorf("expected record (%s) not exists", rlpHash(record))
            }
        }
    case <-time.After(1 * time.Second):
        t.Error("did not receive new records event within one second")
    }

    go table.AddRecords(records2)
    select {
    case newRecords := <-ch:
        m := map[common.Hash]struct{}{}
        for _, record := range newRecords.Records {
            m[rlpHash(record)] = struct{}{}
        }

        if len(m) != len(records2) {
            t.Errorf("len mismatch: got %d, want: %d",
                len(m), len(records2))
        }

        for _, record := range records2 {
            if _, ok := m[rlpHash(record)]; !ok {
                t.Errorf("expected record (%s) not exists", rlpHash(record))
            }
        }
    case <-time.After(1 * time.Second):
        t.Error("did not receive new records event within one second")
    }

    var records []*enr.Record
    records = append(records, records1...)
    records = append(records, records2...)
    allRecords := table.Records()
    if len(allRecords) != len(records) {
        t.Errorf("all metas num mismatch: got %d, want %d",
            len(records), len(allRecords))
    }

    for _, r := range records {
        n, err := enode.New(enode.V4ID{}, r)
        if err != nil {
            t.Errorf(err.Error())
        }
        if rlpHash(r) != rlpHash(table.GetNode(n.ID()).Record()) {
            t.Errorf("record (%s) mismatch", n.ID().String())
        }
    }
}

func randomNode() *enode.Node {
    var err error
    var privkey *ecdsa.PrivateKey
    for {
        privkey, err = crypto.GenerateKey()
        if err == nil {
            break
        }
    }
    var r enr.Record
    r.Set(enr.IP(net.IP{}))
    r.Set(enr.UDP(0))
    r.Set(enr.TCP(0))
    if err := enode.SignV4(&r, privkey); err != nil {
        panic(err)
    }
    node, err := enode.New(enode.V4ID{}, &r)
    if err != nil {
        panic(err)

    }
    return node
}

func randomID() enode.ID {
    return randomNode().ID()
}