peer: Remove error return from Connect.
This commit is contained in:
parent
5cbd1f85bf
commit
777ccdade3
4 changed files with 45 additions and 64 deletions
|
@ -39,10 +39,7 @@ func mockRemotePeer() error {
|
||||||
|
|
||||||
// Create and start the inbound peer.
|
// Create and start the inbound peer.
|
||||||
p := peer.NewInboundPeer(peerCfg)
|
p := peer.NewInboundPeer(peerCfg)
|
||||||
if err := p.Connect(conn); err != nil {
|
p.Connect(conn)
|
||||||
fmt.Printf("Connect: error %v\n", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}()
|
}()
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
@ -93,10 +90,7 @@ func Example_newOutboundPeer() {
|
||||||
fmt.Printf("net.Dial: error %v\n", err)
|
fmt.Printf("net.Dial: error %v\n", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err := p.Connect(conn); err != nil {
|
p.Connect(conn)
|
||||||
fmt.Printf("Connect: error %v\n", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Wait for the verack message or timeout in case of failure.
|
// Wait for the verack message or timeout in case of failure.
|
||||||
select {
|
select {
|
||||||
|
|
11
peer/peer.go
11
peer/peer.go
|
@ -775,7 +775,6 @@ func (p *Peer) pushVersionMsg() error {
|
||||||
// recently seen nonces.
|
// recently seen nonces.
|
||||||
nonce, err := wire.RandomUint64()
|
nonce, err := wire.RandomUint64()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
sentNonces.Add(nonce)
|
sentNonces.Add(nonce)
|
||||||
|
@ -1951,10 +1950,10 @@ func (p *Peer) QueueInventory(invVect *wire.InvVect) {
|
||||||
|
|
||||||
// Connect uses the given conn to connect to the peer. Calling this function when
|
// Connect uses the given conn to connect to the peer. Calling this function when
|
||||||
// the peer is already connected will have no effect.
|
// the peer is already connected will have no effect.
|
||||||
func (p *Peer) Connect(conn net.Conn) error {
|
func (p *Peer) Connect(conn net.Conn) {
|
||||||
// Already connected?
|
// Already connected?
|
||||||
if !atomic.CompareAndSwapInt32(&p.connected, 0, 1) {
|
if !atomic.CompareAndSwapInt32(&p.connected, 0, 1) {
|
||||||
return nil
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if p.inbound {
|
if p.inbound {
|
||||||
|
@ -1963,7 +1962,11 @@ func (p *Peer) Connect(conn net.Conn) error {
|
||||||
p.conn = conn
|
p.conn = conn
|
||||||
p.timeConnected = time.Now()
|
p.timeConnected = time.Now()
|
||||||
|
|
||||||
return p.start()
|
go func() {
|
||||||
|
if err := p.start(); err != nil {
|
||||||
|
log.Errorf("Cannot start peer %v: %v", p, err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Connected returns whether or not the peer is currently connected.
|
// Connected returns whether or not the peer is currently connected.
|
||||||
|
|
|
@ -245,16 +245,13 @@ func TestPeerConnection(t *testing.T) {
|
||||||
&conn{raddr: "10.0.0.2:8333"},
|
&conn{raddr: "10.0.0.2:8333"},
|
||||||
)
|
)
|
||||||
inPeer := peer.NewInboundPeer(peerCfg)
|
inPeer := peer.NewInboundPeer(peerCfg)
|
||||||
if err := inPeer.Connect(inConn); err != nil {
|
inPeer.Connect(inConn)
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
outPeer, err := peer.NewOutboundPeer(peerCfg, "10.0.0.2:8333")
|
outPeer, err := peer.NewOutboundPeer(peerCfg, "10.0.0.2:8333")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
if err := outPeer.Connect(outConn); err != nil {
|
outPeer.Connect(outConn)
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := 0; i < 2; i++ {
|
for i := 0; i < 2; i++ {
|
||||||
select {
|
select {
|
||||||
|
@ -274,16 +271,14 @@ func TestPeerConnection(t *testing.T) {
|
||||||
&conn{raddr: "10.0.0.2:8333"},
|
&conn{raddr: "10.0.0.2:8333"},
|
||||||
)
|
)
|
||||||
inPeer := peer.NewInboundPeer(peerCfg)
|
inPeer := peer.NewInboundPeer(peerCfg)
|
||||||
if err := inPeer.Connect(inConn); err != nil {
|
inPeer.Connect(inConn)
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
outPeer, err := peer.NewOutboundPeer(peerCfg, "10.0.0.2:8333")
|
outPeer, err := peer.NewOutboundPeer(peerCfg, "10.0.0.2:8333")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
if err := outPeer.Connect(outConn); err != nil {
|
outPeer.Connect(outConn)
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
for i := 0; i < 2; i++ {
|
for i := 0; i < 2; i++ {
|
||||||
select {
|
select {
|
||||||
case <-verack:
|
case <-verack:
|
||||||
|
@ -393,10 +388,8 @@ func TestPeerListeners(t *testing.T) {
|
||||||
&conn{raddr: "10.0.0.2:8333"},
|
&conn{raddr: "10.0.0.2:8333"},
|
||||||
)
|
)
|
||||||
inPeer := peer.NewInboundPeer(peerCfg)
|
inPeer := peer.NewInboundPeer(peerCfg)
|
||||||
if err := inPeer.Connect(inConn); err != nil {
|
inPeer.Connect(inConn)
|
||||||
t.Errorf("TestPeerListeners: unexpected err %v\n", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
peerCfg.Listeners = peer.MessageListeners{
|
peerCfg.Listeners = peer.MessageListeners{
|
||||||
OnVerAck: func(p *peer.Peer, msg *wire.MsgVerAck) {
|
OnVerAck: func(p *peer.Peer, msg *wire.MsgVerAck) {
|
||||||
verack <- struct{}{}
|
verack <- struct{}{}
|
||||||
|
@ -407,10 +400,8 @@ func TestPeerListeners(t *testing.T) {
|
||||||
t.Errorf("NewOutboundPeer: unexpected err %v\n", err)
|
t.Errorf("NewOutboundPeer: unexpected err %v\n", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err := outPeer.Connect(outConn); err != nil {
|
outPeer.Connect(outConn)
|
||||||
t.Errorf("TestPeerListeners: unexpected err %v\n", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
for i := 0; i < 2; i++ {
|
for i := 0; i < 2; i++ {
|
||||||
select {
|
select {
|
||||||
case <-verack:
|
case <-verack:
|
||||||
|
@ -524,14 +515,11 @@ func TestPeerListeners(t *testing.T) {
|
||||||
|
|
||||||
// TestOutboundPeer tests that the outbound peer works as expected.
|
// TestOutboundPeer tests that the outbound peer works as expected.
|
||||||
func TestOutboundPeer(t *testing.T) {
|
func TestOutboundPeer(t *testing.T) {
|
||||||
// Use a mock NewestBlock func to test errs
|
|
||||||
var errBlockNotFound = errors.New("newest block not found")
|
|
||||||
var mockNewestSha = func() (*wire.ShaHash, int32, error) {
|
|
||||||
return nil, 0, errBlockNotFound
|
|
||||||
}
|
|
||||||
|
|
||||||
peerCfg := &peer.Config{
|
peerCfg := &peer.Config{
|
||||||
NewestBlock: mockNewestSha,
|
NewestBlock: func() (*wire.ShaHash, int32, error) {
|
||||||
|
return nil, 0, errors.New("newest block not found")
|
||||||
|
},
|
||||||
UserAgentName: "peer",
|
UserAgentName: "peer",
|
||||||
UserAgentVersion: "1.0",
|
UserAgentVersion: "1.0",
|
||||||
ChainParams: &chaincfg.MainNetParams,
|
ChainParams: &chaincfg.MainNetParams,
|
||||||
|
@ -547,26 +535,35 @@ func TestOutboundPeer(t *testing.T) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
wantErr := errBlockNotFound
|
// Test trying to connect twice.
|
||||||
if err := p.Connect(c); err != wantErr {
|
p.Connect(c)
|
||||||
t.Errorf("Connect: expected err %v, got %v\n", wantErr, err)
|
p.Connect(c)
|
||||||
return
|
|
||||||
|
disconnected := make(chan struct{})
|
||||||
|
go func() {
|
||||||
|
p.WaitForDisconnect()
|
||||||
|
disconnected <- struct{}{}
|
||||||
|
}()
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-disconnected:
|
||||||
|
case <-time.After(time.Second):
|
||||||
|
t.Fatal("Peer did not automatically disconnect.")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test already connected.
|
if p.Connected() {
|
||||||
if err := p.Connect(c); err != nil {
|
t.Fatalf("Should not be connected as NewestBlock produces error.")
|
||||||
t.Errorf("Connect: unexpected err %v\n", err)
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test Queue Inv
|
// Test Queue Inv
|
||||||
fakeBlockHash := &wire.ShaHash{0: 0x00, 1: 0x01}
|
fakeBlockHash := &wire.ShaHash{0: 0x00, 1: 0x01}
|
||||||
fakeInv := wire.NewInvVect(wire.InvTypeBlock, fakeBlockHash)
|
fakeInv := wire.NewInvVect(wire.InvTypeBlock, fakeBlockHash)
|
||||||
|
|
||||||
|
// Should be noops as the peer could not connect.
|
||||||
p.QueueInventory(fakeInv)
|
p.QueueInventory(fakeInv)
|
||||||
p.AddKnownInventory(fakeInv)
|
p.AddKnownInventory(fakeInv)
|
||||||
p.QueueInventory(fakeInv)
|
p.QueueInventory(fakeInv)
|
||||||
|
|
||||||
// Test Queue Message
|
|
||||||
fakeMsg := wire.NewMsgVerAck()
|
fakeMsg := wire.NewMsgVerAck()
|
||||||
p.QueueMessage(fakeMsg, nil)
|
p.QueueMessage(fakeMsg, nil)
|
||||||
done := make(chan struct{})
|
done := make(chan struct{})
|
||||||
|
@ -591,10 +588,7 @@ func TestOutboundPeer(t *testing.T) {
|
||||||
t.Errorf("NewOutboundPeer: unexpected err - %v\n", err)
|
t.Errorf("NewOutboundPeer: unexpected err - %v\n", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err := p1.Connect(c1); err != nil {
|
p1.Connect(c1)
|
||||||
t.Errorf("Connect: unexpected err %v\n", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Test update latest block
|
// Test update latest block
|
||||||
latestBlockSha, err := wire.NewShaHashFromStr("1a63f9cdff1752e6375c8c76e543a71d239e1a2e5c6db1aa679")
|
latestBlockSha, err := wire.NewShaHashFromStr("1a63f9cdff1752e6375c8c76e543a71d239e1a2e5c6db1aa679")
|
||||||
|
@ -624,10 +618,7 @@ func TestOutboundPeer(t *testing.T) {
|
||||||
t.Errorf("NewOutboundPeer: unexpected err - %v\n", err)
|
t.Errorf("NewOutboundPeer: unexpected err - %v\n", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err := p2.Connect(c2); err != nil {
|
p2.Connect(c2)
|
||||||
t.Errorf("Connect: unexpected err %v\n", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Test PushXXX
|
// Test PushXXX
|
||||||
var addrs []*wire.NetAddress
|
var addrs []*wire.NetAddress
|
||||||
|
|
11
server.go
11
server.go
|
@ -1573,12 +1573,7 @@ func (s *server) listenHandler(listener net.Listener) {
|
||||||
sp := newServerPeer(s, false)
|
sp := newServerPeer(s, false)
|
||||||
sp.Peer = peer.NewInboundPeer(newPeerConfig(sp))
|
sp.Peer = peer.NewInboundPeer(newPeerConfig(sp))
|
||||||
go s.peerDoneHandler(sp)
|
go s.peerDoneHandler(sp)
|
||||||
if err := sp.Connect(conn); err != nil {
|
sp.Connect(conn)
|
||||||
if atomic.LoadInt32(&s.shutdown) == 0 {
|
|
||||||
srvrLog.Errorf("Can't accept connection: %v", err)
|
|
||||||
}
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
s.wg.Done()
|
s.wg.Done()
|
||||||
srvrLog.Tracef("Listener handler done for %s", listener.Addr())
|
srvrLog.Tracef("Listener handler done for %s", listener.Addr())
|
||||||
|
@ -1674,9 +1669,7 @@ func (s *server) establishConn(sp *serverPeer) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := sp.Connect(conn); err != nil {
|
sp.Connect(conn)
|
||||||
return err
|
|
||||||
}
|
|
||||||
srvrLog.Debugf("Connected to %s", sp.Addr())
|
srvrLog.Debugf("Connected to %s", sp.Addr())
|
||||||
s.addrManager.Attempt(sp.NA())
|
s.addrManager.Attempt(sp.NA())
|
||||||
return nil
|
return nil
|
||||||
|
|
Loading…
Reference in a new issue