aboutsummaryrefslogtreecommitdiffstats
path: root/swarm/network/networkid_test.go
blob: 6ea400e99f581141f8cfb702290384244810b404 (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
// Copyright 2018 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 network

import (
    "bytes"
    "context"
    "flag"
    "fmt"
    "math/rand"
    "strings"
    "testing"
    "time"

    "github.com/dexon-foundation/dexon/log"
    "github.com/dexon-foundation/dexon/node"
    "github.com/dexon-foundation/dexon/p2p"
    "github.com/dexon-foundation/dexon/p2p/enode"
    "github.com/dexon-foundation/dexon/p2p/simulations"
    "github.com/dexon-foundation/dexon/p2p/simulations/adapters"
    "github.com/dexon-foundation/dexon/rpc"
)

var (
    currentNetworkID int
    cnt              int
    nodeMap          map[int][]enode.ID
    kademlias        map[enode.ID]*Kademlia
)

const (
    NumberOfNets = 4
    MaxTimeout   = 15 * time.Second
)

func init() {
    flag.Parse()
    rand.Seed(time.Now().Unix())
}

/*
Run the network ID test.
The test creates one simulations.Network instance,
a number of nodes, then connects nodes with each other in this network.

Each node gets a network ID assigned according to the number of networks.
Having more network IDs is just arbitrary in order to exclude
false positives.

Nodes should only connect with other nodes with the same network ID.
After the setup phase, the test checks on each node if it has the
expected node connections (excluding those not sharing the network ID).
*/
func TestNetworkID(t *testing.T) {
    log.Debug("Start test")
    //arbitrarily set the number of nodes. It could be any number
    numNodes := 24
    //the nodeMap maps all nodes (slice value) with the same network ID (key)
    nodeMap = make(map[int][]enode.ID)
    //set up the network and connect nodes
    net, err := setupNetwork(numNodes)
    if err != nil {
        t.Fatalf("Error setting up network: %v", err)
    }
    //let's sleep to ensure all nodes are connected
    time.Sleep(1 * time.Second)
    // shutdown the the network to avoid race conditions
    // on accessing kademlias global map while network nodes
    // are accepting messages
    net.Shutdown()
    //for each group sharing the same network ID...
    for _, netIDGroup := range nodeMap {
        log.Trace("netIDGroup size", "size", len(netIDGroup))
        //...check that their size of the kademlia is of the expected size
        //the assumption is that it should be the size of the group minus 1 (the node itself)
        for _, node := range netIDGroup {
            if kademlias[node].addrs.Size() != len(netIDGroup)-1 {
                t.Fatalf("Kademlia size has not expected peer size. Kademlia size: %d, expected size: %d", kademlias[node].addrs.Size(), len(netIDGroup)-1)
            }
            kademlias[node].EachAddr(nil, 0, func(addr *BzzAddr, _ int) bool {
                found := false
                for _, nd := range netIDGroup {
                    if bytes.Equal(kademlias[nd].BaseAddr(), addr.Address()) {
                        found = true
                    }
                }
                if !found {
                    t.Fatalf("Expected node not found for node %s", node.String())
                }
                return true
            })
        }
    }
    log.Info("Test terminated successfully")
}

// setup simulated network with bzz/discovery and pss services.
// connects nodes in a circle
// if allowRaw is set, omission of builtin pss encryption is enabled (see PssParams)
func setupNetwork(numnodes int) (net *simulations.Network, err error) {
    log.Debug("Setting up network")
    quitC := make(chan struct{})
    errc := make(chan error)
    nodes := make([]*simulations.Node, numnodes)
    if numnodes < 16 {
        return nil, fmt.Errorf("Minimum sixteen nodes in network")
    }
    adapter := adapters.NewSimAdapter(newServices())
    //create the network
    net = simulations.NewNetwork(adapter, &simulations.NetworkConfig{
        ID:             "NetworkIdTestNet",
        DefaultService: "bzz",
    })
    log.Debug("Creating networks and nodes")

    var connCount int

    //create nodes and connect them to each other
    for i := 0; i < numnodes; i++ {
        log.Trace("iteration: ", "i", i)
        nodeconf := adapters.RandomNodeConfig()
        nodes[i], err = net.NewNodeWithConfig(nodeconf)
        if err != nil {
            return nil, fmt.Errorf("error creating node %d: %v", i, err)
        }
        err = net.Start(nodes[i].ID())
        if err != nil {
            return nil, fmt.Errorf("error starting node %d: %v", i, err)
        }
        client, err := nodes[i].Client()
        if err != nil {
            return nil, fmt.Errorf("create node %d rpc client fail: %v", i, err)
        }
        //now setup and start event watching in order to know when we can upload
        ctx, watchCancel := context.WithTimeout(context.Background(), MaxTimeout)
        defer watchCancel()
        watchSubscriptionEvents(ctx, nodes[i].ID(), client, errc, quitC)
        //on every iteration we connect to all previous ones
        for k := i - 1; k >= 0; k-- {
            connCount++
            log.Debug(fmt.Sprintf("Connecting node %d with node %d; connection count is %d", i, k, connCount))
            err = net.Connect(nodes[i].ID(), nodes[k].ID())
            if err != nil {
                if !strings.Contains(err.Error(), "already connected") {
                    return nil, fmt.Errorf("error connecting nodes: %v", err)
                }
            }
        }
    }
    //now wait until the number of expected subscriptions has been finished
    //`watchSubscriptionEvents` will write with a `nil` value to errc
    for err := range errc {
        if err != nil {
            return nil, err
        }
        //`nil` received, decrement count
        connCount--
        log.Trace("count down", "cnt", connCount)
        //all subscriptions received
        if connCount == 0 {
            close(quitC)
            break
        }
    }
    log.Debug("Network setup phase terminated")
    return net, nil
}

func newServices() adapters.Services {
    kademlias = make(map[enode.ID]*Kademlia)
    kademlia := func(id enode.ID) *Kademlia {
        if k, ok := kademlias[id]; ok {
            return k
        }
        params := NewKadParams()
        params.NeighbourhoodSize = 2
        params.MaxBinSize = 3
        params.MinBinSize = 1
        params.MaxRetries = 1000
        params.RetryExponent = 2
        params.RetryInterval = 1000000
        kademlias[id] = NewKademlia(id[:], params)
        return kademlias[id]
    }
    return adapters.Services{
        "bzz": func(ctx *adapters.ServiceContext) (node.Service, error) {
            addr := NewAddr(ctx.Config.Node())
            hp := NewHiveParams()
            hp.Discovery = false
            cnt++
            //assign the network ID
            currentNetworkID = cnt % NumberOfNets
            if ok := nodeMap[currentNetworkID]; ok == nil {
                nodeMap[currentNetworkID] = make([]enode.ID, 0)
            }
            //add this node to the group sharing the same network ID
            nodeMap[currentNetworkID] = append(nodeMap[currentNetworkID], ctx.Config.ID)
            log.Debug("current network ID:", "id", currentNetworkID)
            config := &BzzConfig{
                OverlayAddr:  addr.Over(),
                UnderlayAddr: addr.Under(),
                HiveParams:   hp,
                NetworkID:    uint64(currentNetworkID),
            }
            return NewBzz(config, kademlia(ctx.Config.ID), nil, nil, nil), nil
        },
    }
}

func watchSubscriptionEvents(ctx context.Context, id enode.ID, client *rpc.Client, errc chan error, quitC chan struct{}) {
    events := make(chan *p2p.PeerEvent)
    sub, err := client.Subscribe(context.Background(), "admin", events, "peerEvents")
    if err != nil {
        log.Error(err.Error())
        errc <- fmt.Errorf("error getting peer events for node %v: %s", id, err)
        return
    }
    go func() {
        defer func() {
            sub.Unsubscribe()
            log.Trace("watch subscription events: unsubscribe", "id", id)
        }()

        for {
            select {
            case <-quitC:
                return
            case <-ctx.Done():
                select {
                case errc <- ctx.Err():
                case <-quitC:
                }
                return
            case e := <-events:
                if e.Type == p2p.PeerEventTypeAdd {
                    errc <- nil
                }
            case err := <-sub.Err():
                if err != nil {
                    select {
                    case errc <- fmt.Errorf("error getting peer events for node %v: %v", id, err):
                    case <-quitC:
                    }
                    return
                }
            }
        }
    }()
}