|
@ -1,10 +1,7 @@ |
|
|
package p2p |
|
|
package p2p |
|
|
|
|
|
|
|
|
import ( |
|
|
import ( |
|
|
"fmt" |
|
|
|
|
|
golog "log" |
|
|
|
|
|
"net" |
|
|
"net" |
|
|
"time" |
|
|
|
|
|
|
|
|
|
|
|
crypto "github.com/tendermint/go-crypto" |
|
|
crypto "github.com/tendermint/go-crypto" |
|
|
cmn "github.com/tendermint/tmlibs/common" |
|
|
cmn "github.com/tendermint/tmlibs/common" |
|
@ -14,8 +11,6 @@ import ( |
|
|
"github.com/tendermint/tendermint/p2p/conn" |
|
|
"github.com/tendermint/tendermint/p2p/conn" |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
const testCh = 0x01 |
|
|
|
|
|
|
|
|
|
|
|
func AddPeerToSwitch(sw *Switch, peer Peer) { |
|
|
func AddPeerToSwitch(sw *Switch, peer Peer) { |
|
|
sw.peers.Add(peer) |
|
|
sw.peers.Add(peer) |
|
|
} |
|
|
} |
|
@ -86,37 +81,7 @@ func Connect2Switches(switches []*Switch, i, j int) { |
|
|
switchI := switches[i] |
|
|
switchI := switches[i] |
|
|
switchJ := switches[j] |
|
|
switchJ := switches[j] |
|
|
|
|
|
|
|
|
p1 := &remotePeer{ |
|
|
|
|
|
Config: switchJ.peerConfig, |
|
|
|
|
|
PrivKey: switchJ.nodeKey.PrivKey, |
|
|
|
|
|
channels: switchJ.NodeInfo().Channels, |
|
|
|
|
|
} |
|
|
|
|
|
p1.Start() |
|
|
|
|
|
|
|
|
|
|
|
c1, err := net.DialTimeout( |
|
|
|
|
|
"tcp", |
|
|
|
|
|
fmt.Sprintf("%s:%d", p1.addr.IP.String(), p1.addr.Port), |
|
|
|
|
|
100*time.Millisecond, |
|
|
|
|
|
) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
panic(err) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
p2 := &remotePeer{ |
|
|
|
|
|
Config: switchI.peerConfig, |
|
|
|
|
|
PrivKey: switchI.nodeKey.PrivKey, |
|
|
|
|
|
channels: switchI.NodeInfo().Channels, |
|
|
|
|
|
} |
|
|
|
|
|
p2.Start() |
|
|
|
|
|
|
|
|
|
|
|
c2, err := net.DialTimeout( |
|
|
|
|
|
"tcp", |
|
|
|
|
|
fmt.Sprintf("%s:%d", p2.addr.IP.String(), p2.addr.Port), |
|
|
|
|
|
100*time.Millisecond, |
|
|
|
|
|
) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
panic(err) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
c1, c2 := conn.NetPipe() |
|
|
|
|
|
|
|
|
doneCh := make(chan struct{}) |
|
|
doneCh := make(chan struct{}) |
|
|
go func() { |
|
|
go func() { |
|
@ -188,77 +153,3 @@ func MakeSwitch(cfg *cfg.P2PConfig, i int, network, version string, initSwitch f |
|
|
sw.SetNodeKey(nodeKey) |
|
|
sw.SetNodeKey(nodeKey) |
|
|
return sw |
|
|
return sw |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
type remotePeer struct { |
|
|
|
|
|
PrivKey crypto.PrivKey |
|
|
|
|
|
Config *PeerConfig |
|
|
|
|
|
addr *NetAddress |
|
|
|
|
|
quit chan struct{} |
|
|
|
|
|
channels cmn.HexBytes |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (rp *remotePeer) Addr() *NetAddress { |
|
|
|
|
|
return rp.addr |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (rp *remotePeer) ID() ID { |
|
|
|
|
|
return PubKeyToID(rp.PrivKey.PubKey()) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (rp *remotePeer) Start() { |
|
|
|
|
|
l, e := net.Listen("tcp", "127.0.0.1:0") // any available address
|
|
|
|
|
|
if e != nil { |
|
|
|
|
|
golog.Fatalf("net.Listen tcp :0: %+v", e) |
|
|
|
|
|
} |
|
|
|
|
|
rp.addr = NewNetAddress(PubKeyToID(rp.PrivKey.PubKey()), l.Addr()) |
|
|
|
|
|
rp.quit = make(chan struct{}) |
|
|
|
|
|
if rp.channels == nil { |
|
|
|
|
|
rp.channels = []byte{testCh} |
|
|
|
|
|
} |
|
|
|
|
|
go rp.accept(l) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (rp *remotePeer) Stop() { |
|
|
|
|
|
close(rp.quit) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (rp *remotePeer) accept(l net.Listener) { |
|
|
|
|
|
conns := []net.Conn{} |
|
|
|
|
|
|
|
|
|
|
|
for { |
|
|
|
|
|
conn, err := l.Accept() |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
golog.Fatalf("Failed to accept conn: %+v", err) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pc, err := newInboundPeerConn(conn, rp.Config, rp.PrivKey) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
golog.Fatalf("Failed to create a peer: %+v", err) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
_, err = pc.HandshakeTimeout(NodeInfo{ |
|
|
|
|
|
ID: rp.Addr().ID, |
|
|
|
|
|
Moniker: "remote_peer", |
|
|
|
|
|
Network: "localhost", |
|
|
|
|
|
Version: "123.123.123", |
|
|
|
|
|
ListenAddr: l.Addr().String(), |
|
|
|
|
|
Channels: rp.channels, |
|
|
|
|
|
}, 1*time.Second) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
golog.Fatalf("Failed to perform handshake: %+v", err) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
conns = append(conns, conn) |
|
|
|
|
|
|
|
|
|
|
|
select { |
|
|
|
|
|
case <-rp.quit: |
|
|
|
|
|
for _, conn := range conns { |
|
|
|
|
|
if err := conn.Close(); err != nil { |
|
|
|
|
|
golog.Fatal(err) |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
return |
|
|
|
|
|
default: |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|