-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
318 lines (280 loc) · 8.03 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
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
package main
import (
"bytes"
"crypto/rand"
"encoding/json"
"flag"
"fmt"
"io"
"log"
"math"
"math/big"
"net"
"net/http"
"net/url"
"os"
"strings"
"sync"
"time"
)
type rpcSuccessorResponse struct {
Result []string `json:"result"`
}
type rpcChordRingInfoResponse struct {
Result struct {
LocalNode struct {
ID string `json:"id"`
RelayMessageCount uint64 `json:"relayMessageCount"`
Uptime int `json:"uptime"`
} `json:"localNode"`
Successors []struct {
Addr string `json:"addr"`
ID string `json:"id"`
} `json:"successors"`
Predecessors []struct {
Addr string `json:"addr"`
ID string `json:"id"`
} `json:"predecessors"`
} `json:"result"`
}
var (
// Version string
Version string
)
var (
totalSpace *big.Int = new(big.Int).Exp(big.NewInt(2), big.NewInt(256), nil)
)
func makeRPCRequest(url string, method string, params interface{}) ([]byte, error) {
requestBody := struct {
Method string `json:"method"`
Params interface{} `json:"params"`
}{
Method: method,
Params: params,
}
jsonData, err := json.Marshal(requestBody)
if err != nil {
return nil, err
}
client := &http.Client{
Timeout: time.Second * 10,
Transport: &http.Transport{
DialContext: (&net.Dialer{
Timeout: time.Second * 5,
KeepAlive: time.Second * 30,
}).DialContext,
TLSHandshakeTimeout: time.Second * 5,
ResponseHeaderTimeout: time.Second * 5,
ExpectContinueTimeout: time.Second * 1,
},
}
resp, err := client.Post(url, "application/json", bytes.NewBuffer(jsonData))
if err != nil {
return nil, err
}
defer resp.Body.Close()
return io.ReadAll(resp.Body)
}
func tcpAddrToRPCAddr(tcpAddr string) (string, error) {
parsedURL, err := url.Parse(tcpAddr)
if err != nil {
return "", err
}
host := strings.Split(parsedURL.Host, ":")[0]
return fmt.Sprintf("http://%s:30003", host), nil
}
func sampleNodes(startKey *big.Int, initialRPCAddress string, n int) (int, uint64, int, *big.Int, error) {
findSuccessorReq := struct {
Key string `json:"key"`
}{
Key: startKey.Text(16),
}
respBody, err := makeRPCRequest(initialRPCAddress, "findsuccessoraddrs", findSuccessorReq)
if err != nil {
return 0, 0, 0, nil, fmt.Errorf("Error making RPC request: %v", err)
}
var successorResp rpcSuccessorResponse
if err := json.Unmarshal(respBody, &successorResp); err != nil || len(successorResp.Result) == 0 {
return 0, 0, 0, nil, fmt.Errorf("Error parsing findsuccessoraddrs response: %v", err)
}
rpcAddresses := make([]string, 1)
if len(successorResp.Result) == 0 {
return 0, 0, 0, nil, fmt.Errorf("Found no successors")
}
firstAddr := successorResp.Result[0]
rpcAddresses[0], err = tcpAddrToRPCAddr(firstAddr)
if err != nil {
return 0, 0, 0, nil, fmt.Errorf("Error parsing URL from successor address: %v", err)
}
nodesVisited := 1
lastID := startKey.Text(16)
var totalRelay uint64
totalUptime := 0
for i := 0; i <= n; i++ {
var responseBody []byte
for _, rpcAddress := range rpcAddresses {
responseBody, err = makeRPCRequest(rpcAddress, "getchordringinfo", struct{}{})
if err == nil {
break
}
}
if err != nil {
log.Println("Error getting chord ring info:", err)
break
}
var chordRingResp rpcChordRingInfoResponse
if err := json.Unmarshal(responseBody, &chordRingResp); err != nil {
log.Println("Error unmarshalling chord ring info response:", err)
break
}
successors := chordRingResp.Result.Successors
predecessors := chordRingResp.Result.Predecessors
if i > 0 {
found := false
for j, pred := range predecessors {
if pred.ID == lastID {
nodesVisited += j + 1
found = true
break
}
}
if !found {
log.Println("Prev ID not found in predecessors")
break
}
}
lastID = chordRingResp.Result.LocalNode.ID
totalRelay += chordRingResp.Result.LocalNode.RelayMessageCount
totalUptime += chordRingResp.Result.LocalNode.Uptime
if len(successors) < 1 {
log.Println("Not enough successors")
break
}
rpcAddresses = make([]string, 0)
mid := len(successors) - int(2*math.Sqrt(float64(len(successors))))
for index := mid - 1; index <= mid+1; index++ {
if index > 0 && index < len(successors) {
rpcAddress, err := tcpAddrToRPCAddr(successors[index].Addr)
if err != nil {
log.Println("Error parsing URL from successor address:", err)
continue
}
rpcAddresses = append(rpcAddresses, rpcAddress)
}
}
if len(rpcAddresses) == 0 {
break
}
}
var totalArea *big.Int
lastKey := new(big.Int)
lastKey.SetString(lastID, 16)
if startKey.Cmp(lastKey) > 0 {
totalArea = new(big.Int).Sub(totalSpace, startKey)
totalArea.Add(totalArea, lastKey)
} else {
totalArea = new(big.Int).Sub(lastKey, startKey)
}
return nodesVisited, totalRelay, totalUptime, totalArea, nil
}
func main() {
timeStart := time.Now()
var (
rpcAddr string
m, n int
jsonOut string
version bool
)
flag.StringVar(&rpcAddr, "rpc", "http://seed.nkn.org:30003", "Initial RPC address in the form ip:port")
flag.IntVar(&m, "m", 8, "Number of concurrent goroutines")
flag.IntVar(&n, "n", 8, "Number of steps to repeat")
flag.StringVar(&jsonOut, "json", "", "Write output to json file")
flag.BoolVar(&version, "version", false, "Print version")
flag.Parse()
if version {
fmt.Println(Version)
os.Exit(0)
}
if len(rpcAddr) == 0 {
log.Fatal("RPC address is required")
}
wg := &sync.WaitGroup{}
nodeCountChan := make(chan int, m)
areaChan := make(chan *big.Int, m)
relayCountChan := make(chan uint64, m)
uptimeChan := make(chan int, m)
S, err := rand.Int(rand.Reader, totalSpace)
if err != nil {
log.Fatal(err)
}
for i := 0; i < m; i++ {
wg.Add(1)
offset := new(big.Int).Mul(totalSpace, big.NewInt(int64(i)))
offset.Div(offset, big.NewInt(int64(m)))
startKey := new(big.Int).Add(S, offset)
startKey.Mod(startKey, totalSpace)
go func() {
defer wg.Done()
nodeCount, relayCount, uptime, area, err := sampleNodes(startKey, rpcAddr, n)
if err != nil {
log.Println(err)
}
nodeCountChan <- nodeCount
relayCountChan <- relayCount
uptimeChan <- uptime
areaChan <- area
}()
}
wg.Wait()
close(nodeCountChan)
close(areaChan)
close(relayCountChan)
close(uptimeChan)
totalNodesVisited := 0
var totalRelay uint64
var totalUptime int
totalArea := big.NewInt(0)
for nodesVisited := range nodeCountChan {
totalNodesVisited += nodesVisited
}
for relayCount := range relayCountChan {
totalRelay += relayCount
}
for uptime := range uptimeChan {
totalUptime += uptime
}
for area := range areaChan {
if area != nil {
totalArea.Add(totalArea, area)
}
}
if totalArea.Sign() == 0 {
log.Fatal("Error: Total area covered is zero, cannot estimate total number of nodes.")
}
estimatedTotalNodes := new(big.Int).Div(new(big.Int).Mul(big.NewInt(int64(totalNodesVisited)), totalSpace), totalArea).Int64()
uncertainty := new(big.Int).Div(new(big.Int).Mul(big.NewInt(int64(math.Sqrt(float64(totalNodesVisited)))), totalSpace), totalArea).Int64()
estimatedRelayPerSecond := float64(totalRelay) / float64(totalUptime) * float64(estimatedTotalNodes) / (math.Log2(float64(estimatedTotalNodes)) / 2)
if len(jsonOut) > 0 {
jsonStr, err := json.Marshal(map[string]interface{}{
"nodesVisited": totalNodesVisited,
"areaCovered": float64(totalNodesVisited) / float64(estimatedTotalNodes),
"nodesEstimated": estimatedTotalNodes,
"nodesUncertainty": uncertainty,
"relayPerSecond": estimatedRelayPerSecond,
"updateTime": time.Now().Unix(),
})
if err != nil {
log.Fatalln("Json formatter error:", err)
}
err = os.WriteFile(jsonOut, jsonStr, 0644)
if err != nil {
log.Fatalln("Write output to file error:", err)
}
} else {
log.Printf("Total nodes visited: %d\n", totalNodesVisited)
log.Printf("Total area covered: %.2f%%\n", 100*float64(totalNodesVisited)/float64(estimatedTotalNodes))
log.Printf("Estimated total number of nodes in the network: %d +- %d\n", estimatedTotalNodes, uncertainty)
log.Printf("Estimated network relay per second: %.0f\n", estimatedRelayPerSecond)
}
log.Printf("Time used: %v\n", time.Since(timeStart))
}