aboutsummaryrefslogtreecommitdiff
path: root/src/gnunet/service/dht/routingtable_test.go
blob: 25793566c1035b6d8d7f82f0e012fcb99867414f (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
// This file is part of gnunet-go, a GNUnet-implementation in Golang.
// Copyright (C) 2019-2022 Bernd Fix  >Y<
//
// gnunet-go is free software: you can redistribute it and/or modify it
// under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License,
// or (at your option) any later version.
//
// gnunet-go 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
// Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//
// SPDX-License-Identifier: AGPL3.0-or-later

package dht

import (
	"gnunet/config"
	"gnunet/core"
	"gnunet/util"
	"math/rand"
	"testing"
)

const (
	NUMP   = 1000  // Total number of peers
	EPOCHS = 10000 // number of epochs to run
)

type Entry struct {
	addr   *PeerAddress // address of peer
	ttl    int64        // time to live (in epochs)
	born   int64        // epoch of birth
	last   int64        // last action
	drop   int64        // drop (in epochs)
	revive int64        // revive dropped (in epochs)
	online bool         // peer connected?
}

// test data
var (
	cfg = &config.NodeConfig{
		PrivateSeed: "YGoe6XFH3XdvFRl+agx9gIzPTvxA229WFdkazEMdcOs=",
		Endpoints: []string{
			"r5n+ip+udp://127.0.0.1:6666",
		},
	}
)

// TestRT connects and disconnects random peers to test the base
// functionality of the routing table algorithms.
func TestRT(t *testing.T) {
	// start deterministic randomizer
	rand.Seed(19031962)

	// helper functions
	genRemotePeer := func() *PeerAddress {
		d := make([]byte, 32)
		if _, err := rand.Read(d); err != nil {
			panic(err)
		}
		return NewPeerAddress(util.NewPeerID(d))
	}

	// create routing table and start command handler
	local, err := core.NewLocalPeer(cfg)
	if err != nil {
		t.Fatal(err)
	}
	rt := NewRoutingTable(NewPeerAddress(local.GetID()))

	// create a task list
	tasks := make([]*Entry, NUMP)
	for i := range tasks {
		tasks[i] = new(Entry)
		tasks[i].addr = genRemotePeer()
		tasks[i].born = rand.Int63n(EPOCHS)
		tasks[i].ttl = 1000 + rand.Int63n(7000)
		tasks[i].drop = 2000 + rand.Int63n(3000)
		tasks[i].revive = rand.Int63n(2000)
		tasks[i].online = false
	}

	// actions:
	connected := func(task *Entry, e int64, msg string) {
		rt.Add(task.addr, true)
		task.online = true
		task.last = e
		t.Logf("[%6d] %s %s\n", e, task.addr, msg)
	}
	disconnected := func(task *Entry, e int64, msg string) {
		rt.Remove(task.addr)
		task.online = false
		task.last = e
		t.Logf("[%6d] %s %s\n", e, task.addr, msg)
	}

	// run epochs
	var e int64
	for e = 0; e < EPOCHS; e++ {
		for _, task := range tasks {
			// birth
			if task.born == e {
				connected(task, e, "connected")
				continue
			}
			// death
			if task.born+task.ttl == e {
				disconnected(task, e, "disconnected")
				continue
			}
			if task.online {
				// drop out
				if task.last+task.drop == e {
					disconnected(task, e, "dropped out")
					continue
				}
			} else {
				// drop in
				if task.last+task.drop == e {
					connected(task, e, "dropped in")
					continue
				}
			}
		}
	}

	// execute some routing functions on remaining table
	k := genRemotePeer()
	bf := NewPeerBloomFilter()
	n := rt.SelectClosestPeer(k, bf)
	t.Logf("Closest: %s -> %s\n", k, n)

	n = rt.SelectRandomPeer(bf)
	t.Logf("Random: %s\n", n)
}