Skip to content

Commit

Permalink
[FAB-4035] Improve handling error in orderer
Browse files Browse the repository at this point in the history
Return Broadcast status in SendBroadcast and improve handling
error in SendBroadcast method

Change-Id: I2c750a16393d42bb15418da295d0e88780f3a465
Signed-off-by: Firas Qutishat <[email protected]>
  • Loading branch information
fqutishat committed May 19, 2017
1 parent 83acc41 commit b8813fe
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 17 deletions.
2 changes: 1 addition & 1 deletion fabric-client/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -1181,7 +1181,7 @@ func (c *chain) BroadcastEnvelope(envelope *SignedEnvelope) ([]*TransactionRespo
var transactionResponse *TransactionResponse

logger.Debugf("Broadcasting envelope to orderer :%s\n", orderer.GetURL())
if err := orderer.SendBroadcast(envelope); err != nil {
if err, _ := orderer.SendBroadcast(envelope); err != nil {
logger.Debugf("Receive Error Response from orderer :%v\n", err)
transactionResponse = &TransactionResponse{orderer.GetURL(),
fmt.Errorf("Error calling orderer '%s': %s", orderer.GetURL(), err)}
Expand Down
2 changes: 1 addition & 1 deletion fabric-client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ func (c *client) CreateChannel(request *CreateChannelRequest) (Chain, error) {
err.Error())
}
// Send request
err = request.Orderer.SendBroadcast(&SignedEnvelope{
err, _ = request.Orderer.SendBroadcast(&SignedEnvelope{
Signature: signedEnvelope.Signature,
Payload: signedEnvelope.Payload,
})
Expand Down
4 changes: 2 additions & 2 deletions fabric-client/mockorderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ func (o *mockOrderer) GetURL() string {
}

// SendBroadcast mocks sending a broadcast by sending nothing nowhere
func (o *mockOrderer) SendBroadcast(envelope *SignedEnvelope) error {
return o.MockError
func (o *mockOrderer) SendBroadcast(envelope *SignedEnvelope) (error, *common.Status) {
return o.MockError, nil
}

// SendBroadcast mocks sending a deliver request to the ordering service
Expand Down
36 changes: 23 additions & 13 deletions fabric-client/orderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ package fabricclient
import (
"crypto/x509"
"fmt"
"io"
"strings"
"time"

"github.com/hyperledger/fabric-sdk-go/config"
Expand All @@ -38,7 +36,7 @@ import (
// HFC sends a block of transactions of endorsed proposals requiring ordering.
type Orderer interface {
GetURL() string
SendBroadcast(envelope *SignedEnvelope) error
SendBroadcast(envelope *SignedEnvelope) (error, *common.Status)
SendDeliver(envelope *SignedEnvelope) (chan *common.Block, chan error)
}

Expand Down Expand Up @@ -81,45 +79,51 @@ func (o *orderer) GetURL() string {
}

// SendBroadcast Send the created transaction to Orderer.
func (o *orderer) SendBroadcast(envelope *SignedEnvelope) error {
func (o *orderer) SendBroadcast(envelope *SignedEnvelope) (error, *common.Status) {
conn, err := grpc.Dial(o.url, o.grpcDialOption...)
if err != nil {
return err
return err, nil
}
defer conn.Close()

broadcastStream, err := ab.NewAtomicBroadcastClient(conn).Broadcast(context.Background())
if err != nil {
return fmt.Errorf("Error Create NewAtomicBroadcastClient %v", err)
return fmt.Errorf("Error Create NewAtomicBroadcastClient %v", err), nil
}
done := make(chan bool)
var broadcastErr error
var broadcastStatus *common.Status = nil

go func() {
for {
broadcastResponse, err := broadcastStream.Recv()
logger.Debugf("Orderer.broadcastStream - response:%v, error:%v\n", broadcastResponse, err)
if err != nil {
if strings.Contains(err.Error(), io.EOF.Error()) {
done <- true
return
}
broadcastErr = fmt.Errorf("error broadcast response : %v", err)
continue
done <- true
return
}
broadcastStatus = &broadcastResponse.Status
if broadcastResponse.Status == common.Status_SUCCESS {
done <- true
return
}
if broadcastResponse.Status != common.Status_SUCCESS {
broadcastErr = fmt.Errorf("broadcast response is not success : %v", broadcastResponse.Status)
done <- true
return
}
}
}()
if err := broadcastStream.Send(&common.Envelope{
Payload: envelope.Payload,
Signature: envelope.Signature,
}); err != nil {
return fmt.Errorf("Failed to send a envelope to orderer: %v", err)
return fmt.Errorf("Failed to send a envelope to orderer: %v", err), nil
}
broadcastStream.CloseSend()
<-done
return broadcastErr
return broadcastErr, broadcastStatus
}

// SendDeliver sends a deliver request to the ordering service and returns the
Expand Down Expand Up @@ -175,6 +179,12 @@ func (o *orderer) SendDeliver(envelope *SignedEnvelope) (chan *common.Block,
close(responses)
return
}
if t.Status != common.Status_SUCCESS {
errors <- fmt.Errorf("Got error status from ordering service: %s",
t.Status)
return
}

// Response is a requested block
case *ab.DeliverResponse_Block:
logger.Debug("Received block from ordering service")
Expand Down

0 comments on commit b8813fe

Please sign in to comment.