Skip to content

Commit

Permalink
[FAB-5764] Errors handling - 2
Browse files Browse the repository at this point in the history
All packages under gossip/gossip.
New errors created using github.com/pkg/errors, stack trace added to
returned errors using WithStack.
In case of returned error was incorporated in new one, now it is
wrapped using Wrap.
%+v added to each logger call that output error.

Change-Id: I77189061dbd2305addab80e35fb495a23d9ab010
Signed-off-by: Gennady Laventman <gennady@il.ibm.com>
  • Loading branch information
gennadylaventman committed Aug 22, 2017
1 parent 6925648 commit 30927d7
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 51 deletions.
5 changes: 3 additions & 2 deletions gossip/gossip/batcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ SPDX-License-Identifier: Apache-2.0
package gossip

import (
"fmt"
"sync"
"sync/atomic"
"time"

"github.com/pkg/errors"
)

type emitBatchCallback func([]interface{})
Expand All @@ -36,7 +37,7 @@ type batchingEmitter interface {
// cb: a callback that is called in order for the forwarding to take place
func newBatchingEmitter(iterations, burstSize int, latency time.Duration, cb emitBatchCallback) batchingEmitter {
if iterations < 0 {
panic(fmt.Errorf("Got a negative iterations number"))
panic(errors.Errorf("Got a negative iterations number"))
}

p := &batchingEmitterImpl{
Expand Down
20 changes: 10 additions & 10 deletions gossip/gossip/certstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package gossip

import (
"bytes"
"fmt"
"sync"

"github.com/hyperledger/fabric/gossip/api"
Expand All @@ -18,6 +17,7 @@ import (
"github.com/hyperledger/fabric/gossip/util"
proto "github.com/hyperledger/fabric/protos/gossip"
"github.com/op/go-logging"
"github.com/pkg/errors"
)

// certStore supports pull dissemination of identity messages
Expand All @@ -43,20 +43,20 @@ func newCertStore(puller pull.Mediator, idMapper identity.Mapper, selfIdentity a
}

if err := certStore.idMapper.Put(selfPKIID, selfIdentity); err != nil {
certStore.logger.Panic("Failed associating self PKIID to cert:", err)
certStore.logger.Panicf("Failed associating self PKIID to cert: %+v", errors.WithStack(err))
}

selfIdMsg, err := certStore.createIdentityMessage()
if err != nil {
logger.Panic("Failed creating self identity message:", err)
certStore.logger.Panicf("Failed creating self identity message: %+v", errors.WithStack(err))
}
puller.Add(selfIdMsg)
puller.RegisterMsgHook(pull.RequestMsgType, func(_ []string, msgs []*proto.SignedGossipMessage, _ proto.ReceivedMessage) {
for _, msg := range msgs {
pkiID := common.PKIidType(msg.GetPeerIdentity().PkiId)
cert := api.PeerIdentityType(msg.GetPeerIdentity().Cert)
if err := certStore.idMapper.Put(pkiID, cert); err != nil {
certStore.logger.Warning("Failed adding identity", cert, ", reason:", err)
certStore.logger.Warningf("Failed adding identity %v, reason %+v", cert, errors.WithStack(err))
}
}
})
Expand All @@ -68,15 +68,15 @@ func (cs *certStore) handleMessage(msg proto.ReceivedMessage) {
for _, env := range update.Data {
m, err := env.ToGossipMessage()
if err != nil {
cs.logger.Warning("Data update contains an invalid message:", err)
cs.logger.Warningf("Data update contains an invalid message: %+v", errors.WithStack(err))
return
}
if !m.IsIdentityMsg() {
cs.logger.Warning("Got a non-identity message:", m, "aborting")
return
}
if err := cs.validateIdentityMsg(m); err != nil {
cs.logger.Warning("Failed validating identity message:", err)
cs.logger.Warningf("Failed validating identity message: %+v", errors.WithStack(err))
return
}
}
Expand All @@ -87,14 +87,14 @@ func (cs *certStore) handleMessage(msg proto.ReceivedMessage) {
func (cs *certStore) validateIdentityMsg(msg *proto.SignedGossipMessage) error {
idMsg := msg.GetPeerIdentity()
if idMsg == nil {
return fmt.Errorf("Identity empty: %+v", msg)
return errors.Errorf("Identity empty: %+v", msg)
}
pkiID := idMsg.PkiId
cert := idMsg.Cert
calculatedPKIID := cs.mcs.GetPKIidOfCert(api.PeerIdentityType(cert))
claimedPKIID := common.PKIidType(pkiID)
if !bytes.Equal(calculatedPKIID, claimedPKIID) {
return fmt.Errorf("Calculated pkiID doesn't match identity: calculated: %v, claimedPKI-ID: %v", calculatedPKIID, claimedPKIID)
return errors.Errorf("Calculated pkiID doesn't match identity: calculated: %v, claimedPKI-ID: %v", calculatedPKIID, claimedPKIID)
}

verifier := func(peerIdentity []byte, signature, message []byte) error {
Expand All @@ -103,7 +103,7 @@ func (cs *certStore) validateIdentityMsg(msg *proto.SignedGossipMessage) error {

err := msg.Verify(cert, verifier)
if err != nil {
return fmt.Errorf("Failed verifying message: %v", err)
return errors.Wrap(err, "Failed verifying message")
}

return cs.mcs.ValidateIdentity(api.PeerIdentityType(idMsg.Cert))
Expand All @@ -130,7 +130,7 @@ func (cs *certStore) createIdentityMessage() (*proto.SignedGossipMessage, error)
GossipMessage: m,
}
_, err := sMsg.Sign(signer)
return sMsg, err
return sMsg, errors.WithStack(err)
}

func (cs *certStore) listRevokedPeers(isSuspected api.PeerSuspector) []common.PKIidType {
Expand Down
18 changes: 9 additions & 9 deletions gossip/gossip/channel/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/hyperledger/fabric/gossip/util"
proto "github.com/hyperledger/fabric/protos/gossip"
"github.com/op/go-logging"
"github.com/pkg/errors"
)

// Config is a configuration item
Expand Down Expand Up @@ -220,7 +221,7 @@ func NewGossipChannel(pkiID common.PKIidType, org api.OrgIdentityType, mcs api.M
return false
}
if err := gc.mcs.VerifyByChannel(chainID, peerIdentity, msg.Signature, msg.Payload); err != nil {
gc.logger.Warning("Peer", peerIdentity, "isn't eligible for channel", string(chainID), ":", err)
gc.logger.Warningf("Peer %v isn't eligible for channel %s : %+v", peerIdentity, string(chainID), errors.WithStack(err))
return false
}
return true
Expand Down Expand Up @@ -285,7 +286,7 @@ func (gc *gossipChannel) GetPeers() []discovery.NetworkMember {
func (gc *gossipChannel) requestStateInfo() {
req, err := gc.createStateInfoRequest()
if err != nil {
gc.logger.Warning("Failed creating SignedGossipMessage:", err)
gc.logger.Warningf("Failed creating SignedGossipMessage: %+v", errors.WithStack(err))
return
}
endpoints := filter.SelectPeers(gc.GetConf().PullPeerNum, gc.GetMembership(), gc.IsMemberInChan)
Expand Down Expand Up @@ -347,7 +348,7 @@ func (gc *gossipChannel) createBlockPuller() pull.Mediator {
for i := range digests {
seqNum, err := strconv.ParseUint(digests[i], 10, 64)
if err != nil {
gc.logger.Warning("Can't parse digest", digests[i], "err", err)
gc.logger.Warningf("Can't parse digest %s : %+v", digests[i], errors.WithStack(err))
continue
}
if seqNum >= height {
Expand Down Expand Up @@ -521,7 +522,7 @@ func (gc *gossipChannel) HandleMessage(msg proto.ReceivedMessage) {
for _, item := range m.GetDataUpdate().Data {
gMsg, err := item.ToGossipMessage()
if err != nil {
gc.logger.Warning("Data update contains an invalid message:", err)
gc.logger.Warningf("Data update contains an invalid message: %+v", errors.WithStack(err))
return
}
if !bytes.Equal(gMsg.Channel, []byte(gc.chainID)) {
Expand Down Expand Up @@ -563,7 +564,7 @@ func (gc *gossipChannel) handleStateInfSnapshot(m *proto.GossipMessage, sender c
for _, envelope := range m.GetStateSnapshot().Elements {
stateInf, err := envelope.ToGossipMessage()
if err != nil {
gc.logger.Warning("Channel", chanName, ": StateInfo snapshot contains an invalid message:", err)
gc.logger.Warningf("Channel %s : StateInfo snapshot contains an invalid message: %+v", chanName, errors.WithStack(err))
return
}
if !stateInf.IsStateInfoMsg() {
Expand Down Expand Up @@ -593,8 +594,7 @@ func (gc *gossipChannel) handleStateInfSnapshot(m *proto.GossipMessage, sender c
}
err = gc.ValidateStateInfoMessage(stateInf)
if err != nil {
gc.logger.Warning("Channel", chanName, ": Failed validating state info message:",
stateInf, ":", err, "sent from", sender)
gc.logger.Warningf("Channel %s: Failed validating state info message: %v sent from %v : %+v", chanName, stateInf, sender, errors.WithStack(err))
return
}

Expand Down Expand Up @@ -622,7 +622,7 @@ func (gc *gossipChannel) verifyBlock(msg *proto.GossipMessage, sender common.PKI
rawBlock := payload.Data
err := gc.mcs.VerifyBlock(msg.Channel, seqNum, rawBlock)
if err != nil {
gc.logger.Warning("Received fabricated block from", sender, "in DataUpdate:", err)
gc.logger.Warningf("Received fabricated block from %v in DataUpdate: %+v", sender, errors.WithStack(err))
return false
}
return true
Expand Down Expand Up @@ -728,7 +728,7 @@ func (gc *gossipChannel) UpdateStateInfo(msg *proto.SignedGossipMessage) {

nodeMeta, err := common.FromBytes(msg.GetStateInfo().Metadata)
if err != nil {
gc.logger.Warning("Can't extract ledger height from metadata", err)
gc.logger.Warningf("Can't extract ledger height from metadata %+v", errors.WithStack(err))
return
}
gc.ledgerHeight = nodeMeta.LedgerHeight
Expand Down
Loading

0 comments on commit 30927d7

Please sign in to comment.