@@ -9,7 +9,6 @@ package comm
9
9
import (
10
10
"bytes"
11
11
"crypto/tls"
12
- "errors"
13
12
"fmt"
14
13
"net"
15
14
"reflect"
@@ -23,6 +22,7 @@ import (
23
22
"github.com/hyperledger/fabric/gossip/util"
24
23
proto "github.com/hyperledger/fabric/protos/gossip"
25
24
"github.com/op/go-logging"
25
+ "github.com/pkg/errors"
26
26
"github.com/spf13/viper"
27
27
"golang.org/x/net/context"
28
28
"google.golang.org/grpc"
@@ -106,7 +106,7 @@ func NewCommInstance(s *grpc.Server, cert *tls.Certificate, idStore identity.Map
106
106
dialOpts = append (dialOpts , grpc .WithTimeout (util .GetDurationOrDefault ("peer.gossip.dialTimeout" , defDialTimeout )))
107
107
commInst , err := NewCommInstanceWithServer (- 1 , idStore , peerIdentity , secureDialOpts , dialOpts ... )
108
108
if err != nil {
109
- return nil , err
109
+ return nil , errors . WithStack ( err )
110
110
}
111
111
112
112
if cert != nil {
@@ -163,7 +163,7 @@ func (c *commImpl) createConnection(endpoint string, expectedPKIID common.PKIidT
163
163
dialOpts = append (dialOpts , c .opts ... )
164
164
cc , err = grpc .Dial (endpoint , dialOpts ... )
165
165
if err != nil {
166
- return nil , err
166
+ return nil , errors . WithStack ( err )
167
167
}
168
168
169
169
cl := proto .NewGossipClient (cc )
@@ -172,7 +172,7 @@ func (c *commImpl) createConnection(endpoint string, expectedPKIID common.PKIidT
172
172
defer cancel ()
173
173
if _ , err = cl .Ping (ctx , & proto.Empty {}); err != nil {
174
174
cc .Close ()
175
- return nil , err
175
+ return nil , errors . WithStack ( err )
176
176
}
177
177
178
178
ctx , cf := context .WithCancel (context .Background ())
@@ -204,10 +204,10 @@ func (c *commImpl) createConnection(endpoint string, expectedPKIID common.PKIidT
204
204
conn .handler = h
205
205
return conn , nil
206
206
}
207
- c .logger .Warning ("Authentication failed:" , err )
207
+ c .logger .Warningf ("Authentication failed: %+v " , err )
208
208
}
209
209
cc .Close ()
210
- return nil , err
210
+ return nil , errors . WithStack ( err )
211
211
}
212
212
213
213
func (c * commImpl ) Send (msg * proto.SignedGossipMessage , peers ... * RemotePeer ) {
@@ -235,13 +235,15 @@ func (c *commImpl) sendToEndpoint(peer *RemotePeer, msg *proto.SignedGossipMessa
235
235
conn , err := c .connStore .getConnection (peer )
236
236
if err == nil {
237
237
disConnectOnErr := func (err error ) {
238
- c .logger .Warning (peer , "isn't responsive:" , err )
238
+ err = errors .WithStack (err )
239
+ c .logger .Warningf ("%v isn't responsive: %+v" , peer , err )
239
240
c .disconnect (peer .PKIID )
240
241
}
241
242
conn .send (msg , disConnectOnErr )
242
243
return
243
244
}
244
- c .logger .Warning ("Failed obtaining connection for" , peer , "reason:" , err )
245
+ err = errors .WithStack (err )
246
+ c .logger .Warningf ("Failed obtaining connection for %v reason: %+v" , peer , err )
245
247
c .disconnect (peer .PKIID )
246
248
}
247
249
@@ -263,15 +265,15 @@ func (c *commImpl) Probe(remotePeer *RemotePeer) error {
263
265
264
266
cc , err := grpc .Dial (remotePeer .Endpoint , dialOpts ... )
265
267
if err != nil {
266
- c .logger .Debug ("Returning" , err )
268
+ c .logger .Debugf ("Returning %v " , err )
267
269
return err
268
270
}
269
271
defer cc .Close ()
270
272
cl := proto .NewGossipClient (cc )
271
273
ctx , cancel := context .WithTimeout (context .Background (), defConnTimeout )
272
274
defer cancel ()
273
275
_ , err = cl .Ping (ctx , & proto.Empty {})
274
- c .logger .Debug ("Returning" , err )
276
+ c .logger .Debugf ("Returning %v " , err )
275
277
return err
276
278
}
277
279
@@ -300,11 +302,11 @@ func (c *commImpl) Handshake(remotePeer *RemotePeer) (api.PeerIdentityType, erro
300
302
}
301
303
connInfo , err := c .authenticateRemotePeer (stream )
302
304
if err != nil {
303
- c .logger .Warning ("Authentication failed:" , err )
305
+ c .logger .Warningf ("Authentication failed: %v " , err )
304
306
return nil , err
305
307
}
306
308
if len (remotePeer .PKIID ) > 0 && ! bytes .Equal (connInfo .ID , remotePeer .PKIID ) {
307
- return nil , errors . New ("PKI-ID of remote peer doesn't match expected PKI-ID" )
309
+ return nil , fmt . Errorf ("PKI-ID of remote peer doesn't match expected PKI-ID" )
308
310
}
309
311
return connInfo .Identity , nil
310
312
}
@@ -424,7 +426,7 @@ func (c *commImpl) authenticateRemotePeer(stream stream) (*proto.ConnectionInfo,
424
426
// TLS enabled but not detected on other side
425
427
if useTLS && len (remoteCertHash ) == 0 {
426
428
c .logger .Warningf ("%s didn't send TLS certificate" , remoteAddress )
427
- return nil , errors . New ("No TLS certificate" )
429
+ return nil , fmt . Errorf ("No TLS certificate" )
428
430
}
429
431
430
432
cMsg , err = c .createConnectionMsg (c .PKIID , c .selfCertHash , c .peerIdentity , signer )
@@ -442,18 +444,18 @@ func (c *commImpl) authenticateRemotePeer(stream stream) (*proto.ConnectionInfo,
442
444
receivedMsg := m .GetConn ()
443
445
if receivedMsg == nil {
444
446
c .logger .Warning ("Expected connection message from" , remoteAddress , "but got" , receivedMsg )
445
- return nil , errors . New ("Wrong type" )
447
+ return nil , fmt . Errorf ("Wrong type" )
446
448
}
447
449
448
450
if receivedMsg .PkiId == nil {
449
451
c .logger .Warning ("%s didn't send a pkiID" , remoteAddress )
450
- return nil , errors . New ("No PKI-ID" )
452
+ return nil , fmt . Errorf ("No PKI-ID" )
451
453
}
452
454
453
455
c .logger .Debug ("Received" , receivedMsg , "from" , remoteAddress )
454
456
err = c .idMapper .Put (receivedMsg .PkiId , receivedMsg .Identity )
455
457
if err != nil {
456
- c .logger .Warning ("Identity store rejected" , remoteAddress , ":" , err )
458
+ c .logger .Warningf ("Identity store rejected %s : %v" , remoteAddress , err )
457
459
return nil , err
458
460
}
459
461
@@ -468,15 +470,15 @@ func (c *commImpl) authenticateRemotePeer(stream stream) (*proto.ConnectionInfo,
468
470
// If the remote peer sent its TLS certificate, make sure it actually matches the TLS cert
469
471
// that the peer used.
470
472
if ! bytes .Equal (remoteCertHash , receivedMsg .TlsCertHash ) {
471
- return nil , fmt .Errorf ("Expected %v in remote hash of TLS cert, but got %v" , remoteCertHash , receivedMsg .TlsCertHash )
473
+ return nil , errors .Errorf ("Expected %v in remote hash of TLS cert, but got %v" , remoteCertHash , receivedMsg .TlsCertHash )
472
474
}
473
475
verifier := func (peerIdentity []byte , signature , message []byte ) error {
474
476
pkiID := c .idMapper .GetPKIidOfCert (api .PeerIdentityType (peerIdentity ))
475
477
return c .idMapper .Verify (pkiID , signature , message )
476
478
}
477
479
err = m .Verify (receivedMsg .Identity , verifier )
478
480
if err != nil {
479
- c .logger .Error ("Failed verifying signature from" , remoteAddress , ":" , err )
481
+ c .logger .Error ("Failed verifying signature from %s : %v" , remoteAddress , err )
480
482
return nil , err
481
483
}
482
484
connInfo .Auth = & proto.AuthInfo {
@@ -496,7 +498,7 @@ func (c *commImpl) GossipStream(stream proto.Gossip_GossipStreamServer) error {
496
498
}
497
499
connInfo , err := c .authenticateRemotePeer (stream )
498
500
if err != nil {
499
- c .logger .Error ("Authentication failed:" , err )
501
+ c .logger .Errorf ("Authentication failed: %v " , err )
500
502
return err
501
503
}
502
504
c .logger .Debug ("Servicing" , extractRemoteAddress (stream ))
@@ -564,16 +566,16 @@ func readWithTimeout(stream interface{}, timeout time.Duration, address string)
564
566
incChan <- msg
565
567
}
566
568
} else {
567
- panic (fmt .Errorf ("Stream isn't a GossipStreamServer or a GossipStreamClient, but %v. Aborting" , reflect .TypeOf (stream )))
569
+ panic (errors .Errorf ("Stream isn't a GossipStreamServer or a GossipStreamClient, but %v. Aborting" , reflect .TypeOf (stream )))
568
570
}
569
571
}()
570
572
select {
571
573
case <- time .NewTicker (timeout ).C :
572
- return nil , fmt .Errorf ("Timed out waiting for connection message from %s" , address )
574
+ return nil , errors .Errorf ("Timed out waiting for connection message from %s" , address )
573
575
case m := <- incChan :
574
576
return m , nil
575
577
case err := <- errChan :
576
- return nil , err
578
+ return nil , errors . WithStack ( err )
577
579
}
578
580
}
579
581
@@ -593,7 +595,7 @@ func (c *commImpl) createConnectionMsg(pkiID common.PKIidType, certHash []byte,
593
595
GossipMessage : m ,
594
596
}
595
597
_ , err := sMsg .Sign (signer )
596
- return sMsg , err
598
+ return sMsg , errors . WithStack ( err )
597
599
}
598
600
599
601
type stream interface {
0 commit comments