mirror of
https://github.com/fluencelabs/tendermint
synced 2025-06-25 10:41:41 +00:00
MakeConnectedSwitches function
This commit is contained in:
42
switch.go
42
switch.go
@ -415,3 +415,45 @@ type SwitchEventDonePeer struct {
|
||||
Peer *Peer
|
||||
Error interface{}
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------
|
||||
// Switches connected via arbitrary net.Conn; useful for testing
|
||||
|
||||
// Returns n fully connected switches.
|
||||
// initSwitch defines how the ith switch should be initialized (ie. with what reactors).
|
||||
func MakeConnectedSwitches(n int, initSwitch func(int, *Switch) *Switch, connPipe func() (net.Conn, net.Conn)) []*Switch {
|
||||
switches := make([]*Switch, n)
|
||||
for i := 0; i < n; i++ {
|
||||
switches[i] = makeSwitch(i, "testing", "123.123.123", initSwitch)
|
||||
}
|
||||
|
||||
for i := 0; i < n; i++ {
|
||||
switchI := switches[i]
|
||||
for j := i; j < n; j++ {
|
||||
switchJ := switches[j]
|
||||
c1, c2 := connPipe()
|
||||
go switchI.AddPeerWithConnection(c1, false) // AddPeer is blocking, requires handshake.
|
||||
go switchJ.AddPeerWithConnection(c2, true)
|
||||
}
|
||||
}
|
||||
// Wait for things to happen, peers to get added...
|
||||
time.Sleep(100 * time.Millisecond * time.Duration(n*n))
|
||||
|
||||
return switches
|
||||
}
|
||||
|
||||
func makeSwitch(i int, network, version string, initSwitch func(int, *Switch) *Switch) *Switch {
|
||||
privKey := crypto.GenPrivKeyEd25519()
|
||||
// new switch, add reactors
|
||||
// TODO: let the config be passed in?
|
||||
s := initSwitch(i, NewSwitch(cfg.NewMapConfig(nil)))
|
||||
s.SetNodeInfo(&NodeInfo{
|
||||
PubKey: privKey.PubKey().(crypto.PubKeyEd25519),
|
||||
Moniker: Fmt("switch%d", i),
|
||||
Network: network,
|
||||
Version: version,
|
||||
})
|
||||
s.SetNodePrivKey(privKey)
|
||||
s.Start() // start switch and reactors
|
||||
return s
|
||||
}
|
||||
|
Reference in New Issue
Block a user