X-Git-Url: http://git.osdn.net/view?a=blobdiff_plain;f=p2p%2Ftest_util.go;h=1d95a8b085fe50ae81695472ad1b0b8f15dbe05e;hb=08fde427c7be4d48d29fbff0bb363604d874daa6;hp=88c088dbaaf9b6313b1c33d081adce889ddcead3;hpb=bd3f0bbdc4a75bdb313da0815feab25eff8e99ec;p=bytom%2Fbytom.git diff --git a/p2p/test_util.go b/p2p/test_util.go index 88c088db..1d95a8b0 100644 --- a/p2p/test_util.go +++ b/p2p/test_util.go @@ -1,13 +1,16 @@ package p2p import ( - "math/rand" "net" + log "github.com/sirupsen/logrus" "github.com/tendermint/go-crypto" cmn "github.com/tendermint/tmlibs/common" cfg "github.com/bytom/config" + dbm "github.com/bytom/database/leveldb" + "github.com/bytom/p2p/connection" + "github.com/bytom/p2p/discover/dht" ) //PanicOnAddPeerErr add peer error @@ -22,7 +25,7 @@ func CreateRandomPeer(outbound bool) *Peer { NodeInfo: &NodeInfo{ ListenAddr: netAddr.DialString(), }, - mconn: &MConnection{}, + mconn: &connection.MConnection{}, } return p } @@ -42,30 +45,6 @@ func CreateRoutableAddr() (addr string, netAddr *NetAddress) { 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.P2PConfig, n int, initSwitch func(int, *Switch) *Switch, connect func([]*Switch, int, int)) []*Switch { - switches := make([]*Switch, n) - for i := 0; i < n; i++ { - switches[i] = MakeSwitch(cfg, i, "testing", "123.123.123", 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 @@ -102,19 +81,21 @@ func startSwitches(switches []*Switch) error { return nil } -func MakeSwitch(cfg *cfg.P2PConfig, i int, network, version string, initSwitch func(int, *Switch) *Switch) *Switch { - privKey := crypto.GenPrivKeyEd25519() +type mockDiscv struct { +} + +func (m *mockDiscv) ReadRandomNodes(buf []*dht.Node) (n int) { + return 0 +} + +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? - s := initSwitch(i, NewSwitch(cfg, nil, nil)) - s.SetNodeInfo(&NodeInfo{ - PubKey: privKey.PubKey().Unwrap().(crypto.PubKeyEd25519), - Moniker: cmn.Fmt("switch%d", i), - Network: network, - Version: version, - RemoteAddr: cmn.Fmt("%v:%v", network, rand.Intn(64512)+1023), - ListenAddr: cmn.Fmt("%v:%v", network, rand.Intn(64512)+1023), - }) - s.SetNodePrivKey(privKey) + l, listenAddr := GetListener(cfg.P2P) + sw, err := newSwitch(cfg, new(mockDiscv), nil, testdb, l, privKey, listenAddr) + if err != nil { + log.Errorf("create switch error: %s", err) + return nil + } + s := initSwitch(sw) return s }