/
main.go
198 lines (164 loc) · 4.04 KB
/
main.go
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
package main
import (
"encoding/binary"
"fmt"
"os"
"time"
"github.com/ChainSafe/dht-tester/client"
"github.com/ipfs/go-cid"
logging "github.com/ipfs/go-log"
"github.com/libp2p/go-libp2p/core/peer"
mh "github.com/multiformats/go-multihash"
"github.com/urfave/cli/v2"
)
var log = logging.Logger("main")
var (
flagCount = "count"
flagDuration = "duration"
flagAutoTest = "auto"
flagTestCIDsCount = "num-test-cids"
flagLog = "log"
flagEndpoint = "endpoint"
cliFlagEndpoint = &cli.StringFlag{
Name: flagEndpoint,
Usage: "endpoint of server",
Value: "http://127.0.0.1:9000",
}
app = &cli.App{
Name: "dht-tester",
Usage: "test libp2p nodes running go-libp2p-kad-dht",
Action: run,
EnableBashCompletion: true,
Suggest: true,
Flags: []cli.Flag{
&cli.UintFlag{
Name: flagDuration,
Usage: "length of time to run simulation in seconds",
Value: 600,
},
&cli.IntFlag{
Name: flagTestCIDsCount,
Usage: "number of test CIDs to generate",
Value: 20,
},
cliFlagEndpoint,
},
}
)
// test CIDs generated at startup
var cids []cid.Cid
func main() {
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}
func run(c *cli.Context) error {
_ = logging.SetLogLevel("main", "info")
cids = getTestCIDs(c.Int(flagTestCIDsCount))
client := client.NewClient(c.String(flagEndpoint))
numHosts, err := client.NumHosts()
if err != nil {
return err
}
provides := make(map[cid.Cid][]peer.ID)
// get at least one host to provide each test CID
for i, c := range cids {
idx := i % numHosts
err = client.Provide(idx, []cid.Cid{c})
if err != nil {
return err
}
id, err := client.ID(idx)
if err != nil {
return err
}
providers, has := provides[c]
if !has {
provides[c] = []peer.ID{id}
} else {
provides[c] = append(providers, id)
}
idx = (i + numHosts/2) % numHosts
err = client.Provide(idx, []cid.Cid{c})
if err != nil {
return err
}
id, err = client.ID(idx)
if err != nil {
return err
}
providers, has = provides[c]
if !has {
provides[c] = []peer.ID{id}
} else {
provides[c] = append(providers, id)
}
}
doneCh := make(chan struct{})
go func() {
err := lookup(client, provides, numHosts, doneCh)
if err != nil {
panic(err)
}
}()
duration, err := time.ParseDuration(fmt.Sprintf("%ds", c.Uint(flagDuration)))
if err != nil {
return err
}
select {
case <-time.After(duration):
case <-doneCh:
}
return nil
}
func lookup(c *client.Client, provides map[cid.Cid][]peer.ID, numHosts int, doneCh chan<- struct{}) error {
defer close(doneCh)
keyIdx := 0
for key, provs := range provides {
provsMap := make(map[peer.ID]struct{})
for _, p := range provs {
provsMap[p] = struct{}{}
}
for i := 0; i < numHosts; i++ {
// TODO: vary prefix lengths also
prefixLength := 33
found, err := c.Lookup(i, key, prefixLength)
if err != nil {
return fmt.Errorf("%d: lookup for key %s at host %d failed: %s", keyIdx, key, i, err)
}
if len(found) == 0 {
return fmt.Errorf("%d: failed to find providers for key %s at host %d", keyIdx, key, i)
}
// if len(found) != len(provs) {
// return fmt.Errorf("%d: found providers length %d didn't match expected %d", keyIdx, len(found), len(provs))
// }
// check peer IDs
for _, f := range found {
_, has := provsMap[f.ID]
if !has {
return fmt.Errorf("%d: found provider that doesn't have key %s at host %d", keyIdx, key, i)
}
}
}
keyIdx++
}
return nil
}
func getTestCIDs(count int) []cid.Cid {
const length = 32
const code = mh.SHA2_256
const base = "dhttest"
const codecType = cid.Raw // TODO: is this right?
cids := make([]cid.Cid, count)
var buf [8]byte
for i := 0; i < count; i++ {
binary.LittleEndian.PutUint64(buf[:], uint64(i))
mh, err := mh.Sum(append([]byte(base), buf[:]...), code, length)
if err != nil {
panic(err)
}
cids[i] = cid.NewCidV1(codecType, mh)
log.Infof("test CID: %s %08b", cids[i], (cids[i].Bytes())[:5])
}
return cids
}