forked from FactomProject/factomd
-
Notifications
You must be signed in to change notification settings - Fork 1
/
NetworkTest.go
272 lines (227 loc) · 7.72 KB
/
NetworkTest.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
// Copyright 2017 Factom Foundation
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
package main
import (
"flag"
"fmt"
"math/rand"
"net/http"
"os"
"strings"
"sync"
"time"
"github.com/FactomProject/factomd/common/interfaces"
"github.com/FactomProject/factomd/common/messages"
"github.com/FactomProject/factomd/common/primitives"
"github.com/FactomProject/factomd/engine"
"github.com/FactomProject/factomd/p2p"
)
var p2pProxy *engine.P2PProxy
var old []map[[32]byte]int
var oldsync sync.Mutex
var oldcnt int
var broadcastSent int
var broadcastReceived int
var p2pSent int
var p2pReceived int
var p2pRequestSent int
var p2pRequestReceived int
var name string
var isp2p bool
var numStamps int
var numReplies int
var size int
var logPort string
func InitNetwork() {
go http.ListenAndServe(fmt.Sprintf("localhost:%s", logPort), nil)
namePtr := flag.String("name", fmt.Sprintf("%d", rand.Int()), "Name for this node")
networkPortOverridePtr := flag.String("networkPort", "8108", "Address for p2p network to listen on.")
logportPtr := flag.String("logPort", "6060", "Port for the profiler")
peersPtr := flag.String("peers", "", "Array of peer addresses. ")
exclusivePtr := flag.Bool("exclusive", false, "If true, we only dial out to special/trusted peers.")
exclusiveInPtr := flag.Bool("exclusive_in", false, "If true, we only dial out to special/trusted peers and disallow all incoming connections.")
deadlinePtr := flag.Int64("deadline", 1, "Deadline for Reads and Writes to conn.")
p2pPtr := flag.Bool("p2p", false, "Test p2p messages (default to false)")
numStampsPtr := flag.Int("numstamps", 1, "Number of timestamps per reply on p2p test. (makes messages big)")
numReplysPtr := flag.Int("numreplies", 1, "Number of replies to any request")
sizePtr := flag.Int("size", 0, "size. We will add a payload of random data of size in K +/- 1K ")
flag.Parse()
numReplies = *numReplysPtr
numStamps = *numStampsPtr
name = *namePtr
port := *networkPortOverridePtr
peers := *peersPtr
exclusive := *exclusivePtr
exclusiveIn := *exclusiveInPtr
logPort = *logportPtr
p2p.NetworkDeadline = time.Duration(*deadlinePtr) * time.Millisecond
isp2p = *p2pPtr
size = *sizePtr * 1024
os.Stderr.WriteString("\nnetTest is a standalone program that generates factomd messages (bounce and bounceResponse)\n ")
os.Stderr.WriteString(" and sends them to other nodes on the network. This allows testing of the network\n ")
os.Stderr.WriteString(" without running all of factomd. Note you can control the size of messages and other\n ")
os.Stderr.WriteString(" variables like the deadline used in the network, and p2p testing.\n\n")
os.Stderr.WriteString("Settings\n")
os.Stderr.WriteString(fmt.Sprintf("%20s -- %s\n", "name", name))
os.Stderr.WriteString(fmt.Sprintf("%20s -- %s\n", "networkPort", port))
os.Stderr.WriteString(fmt.Sprintf("%20s -- %s\n", "peers", peers))
os.Stderr.WriteString(fmt.Sprintf("%20s -- %v\n", "exclusive", exclusive))
os.Stderr.WriteString(fmt.Sprintf("%20s -- %v\n", "exclusiveIn", exclusiveIn))
os.Stderr.WriteString(fmt.Sprintf("%20s -- %v\n", "deadline", p2p.NetworkDeadline.Seconds()))
os.Stderr.WriteString(fmt.Sprintf("%20s -- %v\n", "p2p", isp2p))
os.Stderr.WriteString(fmt.Sprintf("%20s -- %dk\n\n", "size", size/1024))
connectionMetricsChannel := make(chan interface{}, p2p.StandardChannelSize)
ci := p2p.ControllerInit{
Port: port,
PeersFile: "peers.json",
Network: 1,
Exclusive: exclusive,
ExclusiveIn: exclusiveIn,
SeedURL: "",
ConfigPeers: peers,
ConnectionMetricsChannel: connectionMetricsChannel,
}
p2pNetwork := new(p2p.Controller).Init(ci)
p2pNetwork.StartNetwork()
// Setup the proxy (Which translates from network parcels to Factom messages, handling addressing for directed messages)
p2pProxy = new(engine.P2PProxy).Init("testnode", "P2P Network").(*engine.P2PProxy)
p2pProxy.FromNetwork = p2pNetwork.FromNetwork
p2pProxy.ToNetwork = p2pNetwork.ToNetwork
p2pProxy.StartProxy()
}
var cntreq int32
var cntreply int32
// Returns true if message is new
func MsgIsNew(msg interfaces.IMsg) bool {
oldsync.Lock()
defer oldsync.Unlock()
for _, m := range old {
if m[msg.GetHash().Fixed()] != 0 {
return false
}
}
return true
}
var lastTime *time.Time
func SetMsg(msg interfaces.IMsg) {
oldsync.Lock()
defer oldsync.Unlock()
now := time.Now()
if len(old) == 0 || now.After(lastTime.Add(10*time.Second)) {
var nmap []map[[32]byte]int
nmap = append(nmap, make(map[[32]byte]int))
i := len(old)
if i > 9 {
i = 9
}
old = append(nmap, old[:i]...)
lastTime = &now
}
old[0][msg.GetHash().Fixed()] = 1
}
func listen() {
for {
msg, err := p2pProxy.Receive()
if err != nil || msg == nil {
time.Sleep(1 * time.Millisecond)
continue
}
time.Sleep(1 * time.Millisecond)
bounce, ok1 := msg.(*messages.Bounce)
bounceReply, ok2 := msg.(*messages.BounceReply)
if MsgIsNew(msg) {
SetMsg(msg)
fmt.Println(" ", msg.String())
if ok1 && len(bounce.Stamps) < 5 {
if isp2p {
for i := 0; i < numReplies; i++ {
bounceReply = new(messages.BounceReply)
bounceReply.SetPeer2Peer(true)
bounceReply.Number = cntreply
cntreply++
bounceReply.Name = name + "->" + strings.TrimSpace(bounce.Name)
bounceReply.Timestamp = bounce.Timestamp
bounceReply.Stamps = append(bounceReply.Stamps, bounce.Stamps...)
for j := 0; j < numStamps; j++ {
bounceReply.Stamps = append(bounceReply.Stamps, primitives.NewTimestampNow())
}
bounceReply.SetOrigin(bounce.GetOrigin())
bounceReply.SetNetworkOrigin(bounce.GetNetworkOrigin())
SetMsg(msg)
p2pProxy.Send(bounceReply)
p2pSent++
}
p2pRequestReceived++
} else {
bounce.Stamps = append(bounce.Stamps, primitives.NewTimestampNow())
bounce.Number = cntreq
bounce.Name = strings.TrimSpace(bounce.Name) + "-" + name
cntreq++
SetMsg(msg)
p2pProxy.Send(msg)
broadcastReceived++
broadcastSent++
}
}
if false && ok2 && len(bounceReply.Stamps) < 5 {
bounceReply.Stamps = append(bounceReply.Stamps, primitives.NewTimestampNow())
SetMsg(msg)
p2pProxy.Send(msg)
p2pReceived++
p2pSent++
}
} else {
oldcnt++
fmt.Println("OLD:", msg.String())
}
}
}
func main() {
InitNetwork()
time.Sleep(1 * time.Second)
fmt.Println("Starting...")
go listen()
for {
bounce := new(messages.Bounce)
bounce.Number = cntreq
cntreq++
bounce.Name = name
bounce.AddData(size + rand.Int()%1024)
bounce.Timestamp = primitives.NewTimestampNow()
bounce.Stamps = append(bounce.Stamps, primitives.NewTimestampNow())
if isp2p {
bounce.SetPeer2Peer(true)
p2pRequestSent++
} else {
broadcastSent++
}
p2pProxy.Send(bounce)
SetMsg(bounce)
if isp2p {
fmt.Printf("netTest(%s): ::p2p:: request sent: %d request received %d sent: %d received: %d\n",
name,
p2pRequestSent, p2pRequestReceived,
p2pSent, p2pReceived)
} else {
fmt.Printf("netTest(%s): ::: broadcast sent: %d broadcast received: %d\n",
name,
broadcastSent, broadcastReceived)
}
time.Sleep(8 * time.Second)
}
}
// if isp2p {
// fmt.Printf("netTest(%s): Reads: %d errs %d Writes %d errs %d ::p2p:: request sent: %d request received %d sent: %d received: %d\n",
// name,
// p2p.Reads, p2p.ReadsErr,
// p2p.Writes, p2p.WritesErr,
// p2pRequestSent, p2pRequestReceived,
// p2pSent, p2pReceived)
// } else {
// fmt.Printf("netTest(%s): Reads: %d errs %d Writes %d errs %d ::: broadcast sent: %d broadcast received: %d\n",
// name,
// p2p.Reads, p2p.ReadsErr,
// p2p.Writes, p2p.WritesErr,
// broadcastSent, broadcastReceived)
// }