package p2p
import (
- "github.com/tendermint/go-crypto"
"io/ioutil"
"os"
"sync"
"testing"
+ "time"
+
+ "github.com/davecgh/go-spew/spew"
+ "github.com/tendermint/go-crypto"
cfg "github.com/bytom/config"
+ dbm "github.com/bytom/database/leveldb"
"github.com/bytom/errors"
conn "github.com/bytom/p2p/connection"
- dbm "github.com/bytom/database/leveldb"
)
var (
defer os.RemoveAll(dirPath)
testDB := dbm.NewDB("testdb", "leveldb", dirPath)
-
- s1 := MakeSwitch(testCfg, testDB, initSwitchFunc)
+ cfg := *testCfg
+ cfg.P2P.ListenAddress = "127.0.1.1:0"
+ swPrivKey := crypto.GenPrivKeyEd25519()
+ cfg.P2P.PrivateKey = swPrivKey.String()
+ s1 := MakeSwitch(&cfg, testDB, swPrivKey, initSwitchFunc)
s1.Start()
defer s1.Stop()
+
// simulate s1 having a public key and creating a remote peer with the same key
- rp := &remotePeer{PrivKey: s1.nodePrivKey, Config: testCfg}
+ rpCfg := *testCfg
+ rp := &remotePeer{PrivKey: s1.nodePrivKey, Config: &rpCfg}
rp.Start()
defer rp.Stop()
if err = s1.DialPeerWithAddress(rp.addr); errors.Root(err) != ErrConnectSelf {
}
//S1 dialing itself ip address
- addr, err := NewNetAddressString("0.0.0.0:46656")
- if err != nil {
- t.Fatal(err)
- }
+ addr := NewNetAddress(s1.listeners[0].(*DefaultListener).NetListener().Addr())
if err := s1.DialPeerWithAddress(addr); errors.Root(err) != ErrConnectSelf {
t.Fatal(err)
defer os.RemoveAll(dirPath)
testDB := dbm.NewDB("testdb", "leveldb", dirPath)
- s1 := MakeSwitch(testCfg, testDB, initSwitchFunc)
+ cfg := *testCfg
+ cfg.P2P.ListenAddress = "127.0.1.1:0"
+ swPrivKey := crypto.GenPrivKeyEd25519()
+ cfg.P2P.PrivateKey = swPrivKey.String()
+ s1 := MakeSwitch(&cfg, testDB, swPrivKey, initSwitchFunc)
s1.Start()
defer s1.Stop()
- rp := &remotePeer{PrivKey: crypto.GenPrivKeyEd25519(), Config: testCfg}
+
+ rpCfg := *testCfg
+ rp := &remotePeer{PrivKey: crypto.GenPrivKeyEd25519(), Config: &rpCfg}
rp.Start()
defer rp.Stop()
s1.AddBannedPeer(rp.addr.IP.String())
defer os.RemoveAll(dirPath)
testDB := dbm.NewDB("testdb", "leveldb", dirPath)
- s1 := MakeSwitch(testCfg, testDB, initSwitchFunc)
+ cfg := *testCfg
+ cfg.P2P.ListenAddress = "127.0.1.1:0"
+ swPrivKey := crypto.GenPrivKeyEd25519()
+ cfg.P2P.PrivateKey = swPrivKey.String()
+ s1 := MakeSwitch(&cfg, testDB, swPrivKey, initSwitchFunc)
s1.Start()
defer s1.Stop()
- rp := &remotePeer{PrivKey: crypto.GenPrivKeyEd25519(), Config: testCfg}
+
+ rpCfg := *testCfg
+ rp := &remotePeer{PrivKey: crypto.GenPrivKeyEd25519(), Config: &rpCfg}
rp.Start()
defer rp.Stop()
+
if err = s1.DialPeerWithAddress(rp.addr); err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dirPath)
testDB := dbm.NewDB("testdb", "leveldb", dirPath)
- s1 := MakeSwitch(testCfg, testDB, initSwitchFunc)
+ cfg := *testCfg
+ cfg.P2P.ListenAddress = "127.0.1.1:0"
+ swPrivKey := crypto.GenPrivKeyEd25519()
+ cfg.P2P.PrivateKey = swPrivKey.String()
+ s1 := MakeSwitch(&cfg, testDB, swPrivKey, initSwitchFunc)
s1.Start()
defer s1.Stop()
- inp := &inboundPeer{PrivKey: crypto.GenPrivKeyEd25519(), config: testCfg}
- addr, err := NewNetAddressString(s1.nodeInfo.ListenAddr)
+ inpCfg := *testCfg
+ inp := &inboundPeer{PrivKey: crypto.GenPrivKeyEd25519(), config: &inpCfg}
+ addr := NewNetAddress(s1.listeners[0].(*DefaultListener).NetListener().Addr())
if err != nil {
t.Fatal(err)
}
+ go inp.dial(addr)
- if err = inp.dial(addr); err != nil {
- t.Fatal(err)
- }
-
- inp1 := &inboundPeer{PrivKey: inp.PrivKey, config: testCfg}
-
- if err = inp1.dial(addr); err != nil {
- t.Fatal(err)
- }
+ inp1Cfg := *testCfg
+ inp1 := &inboundPeer{PrivKey: inp.PrivKey, config: &inp1Cfg}
+ go inp1.dial(addr)
+ time.Sleep(1 * time.Second)
if outbound, inbound, dialing := s1.NumPeers(); outbound+inbound+dialing != 1 {
- t.Fatal("TestDuplicateInBoundPeer peer size error", outbound, inbound, dialing)
+ t.Fatal("TestDuplicateInBoundPeer peer size error want 1, got:", outbound, inbound, dialing, spew.Sdump(s1.peers.lookup))
}
}
testDB := dbm.NewDB("testdb", "leveldb", dirPath)
cfg := *testCfg
cfg.P2P.MaxNumPeers = 2
- s1 := MakeSwitch(&cfg, testDB, initSwitchFunc)
+ cfg.P2P.ListenAddress = "127.0.1.1:0"
+ swPrivKey := crypto.GenPrivKeyEd25519()
+ cfg.P2P.PrivateKey = swPrivKey.String()
+ s1 := MakeSwitch(&cfg, testDB, swPrivKey, initSwitchFunc)
s1.Start()
defer s1.Stop()
- inp := &inboundPeer{PrivKey: crypto.GenPrivKeyEd25519(), config: testCfg}
- addr, err := NewNetAddressString(s1.nodeInfo.ListenAddr)
+ inpCfg := *testCfg
+ inpPrivKey := crypto.GenPrivKeyEd25519()
+ inpCfg.P2P.PrivateKey = inpPrivKey.String()
+ inp := &inboundPeer{PrivKey: inpPrivKey, config: &inpCfg}
+ addr := NewNetAddress(s1.listeners[0].(*DefaultListener).NetListener().Addr())
if err != nil {
t.Fatal(err)
}
+ go inp.dial(addr)
- if err := inp.dial(addr); err != nil {
- t.Fatal(err)
- }
-
- rp := &remotePeer{PrivKey: crypto.GenPrivKeyEd25519(), Config: testCfg}
+ rpCfg := *testCfg
+ rpPrivKey := crypto.GenPrivKeyEd25519()
+ rpCfg.P2P.PrivateKey = rpPrivKey.String()
+ rp := &remotePeer{PrivKey: rpPrivKey, Config: &rpCfg}
rp.Start()
defer rp.Stop()
+
if err := s1.DialPeerWithAddress(rp.addr); err != nil {
t.Fatal(err)
}
- if outbound, inbound, dialing := s1.NumPeers(); outbound+inbound+dialing != 2 {
- t.Fatal("TestAddInboundPeer peer size error")
- }
- inp2 := &inboundPeer{PrivKey: crypto.GenPrivKeyEd25519(), config: testCfg}
+ inp2Cfg := *testCfg
+ inp2PrivKey := crypto.GenPrivKeyEd25519()
+ inp2Cfg.P2P.PrivateKey = inp2PrivKey.String()
+ inp2 := &inboundPeer{PrivKey: inp2PrivKey, config: &inp2Cfg}
+
+ go inp2.dial(addr)
- if err := inp2.dial(addr); err == nil {
- t.Fatal("TestAddInboundPeer MaxNumPeers limit error")
+ time.Sleep(1 * time.Second)
+ if outbound, inbound, dialing := s1.NumPeers(); outbound+inbound+dialing != 2 {
+ t.Fatal("TestAddInboundPeer peer size error want 2 got:", spew.Sdump(s1.peers.lookup))
}
}
testDB := dbm.NewDB("testdb", "leveldb", dirPath)
cfg := *testCfg
cfg.P2P.MaxNumPeers = 2
- s1 := MakeSwitch(&cfg, testDB, initSwitchFunc)
+ cfg.P2P.ListenAddress = "127.0.1.1:0"
+ swPrivKey := crypto.GenPrivKeyEd25519()
+ cfg.P2P.PrivateKey = swPrivKey.String()
+ s1 := MakeSwitch(&cfg, testDB, swPrivKey, initSwitchFunc)
s1.Start()
defer s1.Stop()
- inp := &inboundPeer{PrivKey: crypto.GenPrivKeyEd25519(), config: testCfg}
- addr, err := NewNetAddressString("127.0.0.1:46656")
+ inpCfg := *testCfg
+ inpPrivKey := crypto.GenPrivKeyEd25519()
+ inpCfg.P2P.PrivateKey = inpPrivKey.String()
+ inp := &inboundPeer{PrivKey: inpPrivKey, config: &inpCfg}
+ addr := NewNetAddress(s1.listeners[0].(*DefaultListener).NetListener().Addr())
if err != nil {
t.Fatal(err)
}
+ go inp.dial(addr)
- if err := inp.dial(addr); err != nil {
- t.Fatal(err)
- }
-
- rp := &remotePeer{PrivKey: crypto.GenPrivKeyEd25519(), Config: testCfg}
+ rpCfg := *testCfg
+ rpPrivKey := crypto.GenPrivKeyEd25519()
+ rpCfg.P2P.PrivateKey = rpPrivKey.String()
+ rp := &remotePeer{PrivKey: rpPrivKey, Config: &rpCfg}
rp.Start()
defer rp.Stop()
+
if err := s1.DialPeerWithAddress(rp.addr); err != nil {
t.Fatal(err)
}
-
+ time.Sleep(1 * time.Second)
if outbound, inbound, dialing := s1.NumPeers(); outbound+inbound+dialing != 2 {
- t.Fatal("TestStopPeer peer size error")
+ t.Fatal("TestStopPeer peer size error want 2,got:", spew.Sdump(s1.peers.lookup))
}
s1.StopPeerGracefully(s1.peers.list[0].Key)
if outbound, inbound, dialing := s1.NumPeers(); outbound+inbound+dialing != 1 {
- t.Fatal("TestStopPeer peer size error")
+ t.Fatal("TestStopPeer peer size error,want 1,got:", spew.Sdump(s1.peers.lookup))
}
s1.StopPeerForError(s1.peers.list[0], "stop for test")
if outbound, inbound, dialing := s1.NumPeers(); outbound+inbound+dialing != 0 {
- t.Fatal("TestStopPeer peer size error")
+ t.Fatal("TestStopPeer peer size error,want 0, got:", spew.Sdump(s1.peers.lookup))
}
}
cmn "github.com/tendermint/tmlibs/common"
cfg "github.com/bytom/config"
- "github.com/bytom/errors"
"github.com/bytom/p2p/connection"
"github.com/bytom/p2p/discover"
dbm "github.com/bytom/database/leveldb"
return
}
-// MakeConnectedSwitches switches connected via arbitrary net.Conn; useful for testing
-// Returns n switches, connected according to the connect func.
-// If connect==Connect2Switches, the switches will be fully connected.
-// initSwitch defines how the ith switch should be initialized (ie. with what reactors).
-// NOTE: panics if any switch fails to start.
-func MakeConnectedSwitches(cfg []*cfg.Config, n int, testDB dbm.DB, initSwitch func(*Switch) *Switch, connect func([]*Switch, int, int)) []*Switch {
- if len(cfg) != n {
- panic(errors.New("cfg number error"))
- }
- switches := make([]*Switch, n)
- for i := 0; i < n; i++ {
- switches[i] = MakeSwitch(cfg[i], testDB, initSwitch)
- }
-
- if err := startSwitches(switches); err != nil {
- panic(err)
- }
-
- for i := 0; i < n; i++ {
- for j := i; j < n; j++ {
- connect(switches, i, j)
- }
- }
-
- return switches
-}
-
// Connect2Switches will connect switches i and j via net.Pipe()
// Blocks until a conection is established.
// NOTE: caller ensures i and j are within bounds
return 0
}
-func MakeSwitch(cfg *cfg.Config, testdb dbm.DB, initSwitch func(*Switch) *Switch) *Switch {
+func MakeSwitch(cfg *cfg.Config, testdb dbm.DB, privKey crypto.PrivKeyEd25519, initSwitch func(*Switch) *Switch) *Switch {
// new switch, add reactors
- // TODO: let the config be passed in?
- privKey := crypto.GenPrivKeyEd25519()
l, listenAddr := GetListener(cfg.P2P)
sw, err := newSwitch(cfg, new(mockDiscv), testdb, l, privKey, listenAddr)
if err != nil {