Skip to content

Commit

Permalink
FAB-6676 Serviceability - update protos/utils errors
Browse files Browse the repository at this point in the history
This CR updates the errors in the protos/utils package
to use vendored errors package. It also cleans up a few
areas of the code to avoid stacking redundant error messages
and to make error messages clearer (in particular, the config
update envelope error messages made little sense).

Change-Id: I2bf8015b3897be9e9d4b434b01952a331a62fe6e
Signed-off-by: Will Lahti <[email protected]>
  • Loading branch information
wlahti committed Jul 19, 2018
1 parent b915efb commit 9d938b8
Show file tree
Hide file tree
Showing 14 changed files with 353 additions and 346 deletions.
2 changes: 1 addition & 1 deletion common/configtx/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ func (vi *ValidatorImpl) computeUpdateResult(updatedConfig map[string]comparable

func envelopeToConfigUpdate(configtx *cb.Envelope) (*cb.ConfigUpdateEnvelope, error) {
configUpdateEnv := &cb.ConfigUpdateEnvelope{}
_, err := utils.UnmarshalEnvelopeOfTypes(configtx, []cb.HeaderType{cb.HeaderType_CONFIG_UPDATE}, configUpdateEnv)
_, err := utils.UnmarshalEnvelopeOfType(configtx, cb.HeaderType_CONFIG_UPDATE, configUpdateEnv)
if err != nil {
return nil, err
}
Expand Down
8 changes: 6 additions & 2 deletions common/crypto/random.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ limitations under the License.

package crypto

import "crypto/rand"
import (
"crypto/rand"

"github.com/pkg/errors"
)

const (
// NonceSize is the default NonceSize
Expand All @@ -30,7 +34,7 @@ func GetRandomBytes(len int) ([]byte, error) {
// TODO: rand could fill less bytes then len
_, err := rand.Read(key)
if err != nil {
return nil, err
return nil, errors.Wrap(err, "error getting random bytes")
}

return key, nil
Expand Down
6 changes: 3 additions & 3 deletions core/admin/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func TestValidateStructureRequestBadInput(t *testing.T) {
op, sd, err = validateStructure(ctx, &common.Envelope{})
assert.Nil(t, op)
assert.Nil(t, sd)
assert.Contains(t, err.Error(), "Envelope must have a Header")
assert.Contains(t, err.Error(), "envelope must have a Header")

pl := &common.Payload{}
pl.Header = &common.Header{
Expand All @@ -73,7 +73,7 @@ func TestValidateStructureRequestBadInput(t *testing.T) {
op, sd, err = validateStructure(ctx, &common.Envelope{Payload: plBytes})
assert.Nil(t, op)
assert.Nil(t, sd)
assert.Contains(t, err.Error(), "Invalid ChannelHeader")
assert.Contains(t, err.Error(), "error unmarshaling ChannelHeader")

ch := &common.ChannelHeader{
Type: int32(common.HeaderType_PEER_ADMIN_OPERATION),
Expand Down Expand Up @@ -122,7 +122,7 @@ func TestValidateStructureRequestBadInput(t *testing.T) {
op, sd, err = validateStructure(ctx, &common.Envelope{Payload: plBytes})
assert.Nil(t, op)
assert.Nil(t, sd)
assert.Contains(t, err.Error(), "Error unmarshaling message")
assert.Contains(t, err.Error(), "error unmarshaling message")
}

func TestValidateStructureRequestGoodInput(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion core/scc/lscc/lscc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ func TestDeploy(t *testing.T) {

res = stub.MockInvokeWithSignedProposal("1", [][]byte{[]byte("deploy"), []byte("chain"), []byte("barf")}, nil)
assert.NotEqual(t, int32(shim.OK), res.Status)
assert.Equal(t, "unexpected EOF", res.Message)
assert.Equal(t, "error unmarshaling ChaincodeDeploymentSpec: unexpected EOF", res.Message)

testDeploy(t, "example02", "1.0", path, false, false, true, "", scc, stub, nil)
testDeploy(t, "example02", "1.0", path, false, false, true, "chaincode with name 'example02' already exists", scc, stub, nil)
Expand Down
4 changes: 2 additions & 2 deletions orderer/common/msgprocessor/systemchannel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestProcessSystemChannelNormalMsg(t *testing.T) {
ms := &mockSystemChannelFilterSupport{}
_, err := NewSystemChannel(ms, mscs, nil).ProcessNormalMsg(&cb.Envelope{})
assert.NotNil(t, err)
assert.Regexp(t, "no header was set", err.Error())
assert.Regexp(t, "header not set", err.Error())
})
t.Run("Mismatched channel ID", func(t *testing.T) {
mscs := &mockSystemChannelSupport{}
Expand Down Expand Up @@ -83,7 +83,7 @@ func TestSystemChannelConfigUpdateMsg(t *testing.T) {
ms := &mockSystemChannelFilterSupport{}
_, _, err := NewSystemChannel(ms, mscs, NewRuleSet([]Rule{AcceptRule})).ProcessConfigUpdateMsg(&cb.Envelope{})
assert.NotNil(t, err)
assert.Regexp(t, "no header was set", err.Error())
assert.Regexp(t, "header not set", err.Error())
})
t.Run("NormalUpdate", func(t *testing.T) {
mscs := &mockSystemChannelSupport{}
Expand Down
2 changes: 1 addition & 1 deletion peer/chaincode/upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func TestUpgradeCmdEndorseFail(t *testing.T) {
"-v", "anotherversion", "-c", "{\"Function\":\"init\",\"Args\": [\"param\",\"1\"]}"}
cmd.SetArgs(args)

expectErrMsg := fmt.Sprintf("could not assemble transaction, err Proposal response was not successful, error code %d, msg %s", errCode, errMsg)
expectErrMsg := fmt.Sprintf("could not assemble transaction, err proposal response was not successful, error code %d, msg %s", errCode, errMsg)
if err := cmd.Execute(); err == nil {
t.Errorf("Run chaincode upgrade cmd error:%v", err)
} else {
Expand Down
54 changes: 25 additions & 29 deletions protos/utils/blockutils.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,19 @@
/*
Copyright IBM Corp. 2016 All Rights Reserved.
Copyright IBM Corp. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
SPDX-License-Identifier: Apache-2.0
*/

package utils

import (
"fmt"

"github.com/golang/protobuf/proto"
cb "github.com/hyperledger/fabric/protos/common"
"github.com/pkg/errors"
)

// GetChainIDFromBlockBytes returns chain ID given byte array which represents the block
// GetChainIDFromBlockBytes returns chain ID given byte array which represents
// the block
func GetChainIDFromBlockBytes(bytes []byte) (string, error) {
block, err := GetBlockFromBlockBytes(bytes)
if err != nil {
Expand All @@ -36,20 +26,20 @@ func GetChainIDFromBlockBytes(bytes []byte) (string, error) {
// GetChainIDFromBlock returns chain ID in the block
func GetChainIDFromBlock(block *cb.Block) (string, error) {
if block == nil || block.Data == nil || block.Data.Data == nil || len(block.Data.Data) == 0 {
return "", fmt.Errorf("failed to retrieve channel id - block is empty")
return "", errors.Errorf("failed to retrieve channel id - block is empty")
}
var err error
envelope := &cb.Envelope{}
if err = proto.Unmarshal(block.Data.Data[0], envelope); err != nil {
return "", fmt.Errorf("error reconstructing envelope(%s)", err)
envelope, err := GetEnvelopeFromBlock(block.Data.Data[0])
if err != nil {
return "", err
}
payload := &cb.Payload{}
if err = proto.Unmarshal(envelope.Payload, payload); err != nil {
return "", fmt.Errorf("error reconstructing payload(%s)", err)
payload, err := GetPayload(envelope)
if err != nil {
return "", err
}

if payload.Header == nil {
return "", fmt.Errorf("failed to retrieve channel id - payload header is empty")
return "", errors.Errorf("failed to retrieve channel id - payload header is empty")
}
chdr, err := UnmarshalChannelHeader(payload.Header.ChannelHeader)
if err != nil {
Expand All @@ -64,12 +54,13 @@ func GetMetadataFromBlock(block *cb.Block, index cb.BlockMetadataIndex) (*cb.Met
md := &cb.Metadata{}
err := proto.Unmarshal(block.Metadata.Metadata[index], md)
if err != nil {
return nil, err
return nil, errors.Wrapf(err, "error unmarshaling metadata from block at index [%s]", index)
}
return md, nil
}

// GetMetadataFromBlockOrPanic retrieves metadata at the specified index, or panics on error.
// GetMetadataFromBlockOrPanic retrieves metadata at the specified index, or
// panics on error
func GetMetadataFromBlockOrPanic(block *cb.Block, index cb.BlockMetadataIndex) *cb.Metadata {
md, err := GetMetadataFromBlock(block, index)
if err != nil {
Expand All @@ -78,7 +69,8 @@ func GetMetadataFromBlockOrPanic(block *cb.Block, index cb.BlockMetadataIndex) *
return md
}

// GetLastConfigIndexFromBlock retrieves the index of the last config block as encoded in the block metadata
// GetLastConfigIndexFromBlock retrieves the index of the last config block as
// encoded in the block metadata
func GetLastConfigIndexFromBlock(block *cb.Block) (uint64, error) {
md, err := GetMetadataFromBlock(block, cb.BlockMetadataIndex_LAST_CONFIG)
if err != nil {
Expand All @@ -87,12 +79,13 @@ func GetLastConfigIndexFromBlock(block *cb.Block) (uint64, error) {
lc := &cb.LastConfig{}
err = proto.Unmarshal(md.Value, lc)
if err != nil {
return 0, err
return 0, errors.Wrap(err, "error unmarshaling LastConfig")
}
return lc.Index, nil
}

// GetLastConfigIndexFromBlockOrPanic retrieves the index of the last config block as encoded in the block metadata, or panics on error.
// GetLastConfigIndexFromBlockOrPanic retrieves the index of the last config
// block as encoded in the block metadata, or panics on error
func GetLastConfigIndexFromBlockOrPanic(block *cb.Block) uint64 {
index, err := GetLastConfigIndexFromBlock(block)
if err != nil {
Expand All @@ -105,7 +98,10 @@ func GetLastConfigIndexFromBlockOrPanic(block *cb.Block) uint64 {
func GetBlockFromBlockBytes(blockBytes []byte) (*cb.Block, error) {
block := &cb.Block{}
err := proto.Unmarshal(blockBytes, block)
return block, err
if err != nil {
return block, errors.Wrap(err, "error unmarshaling block")
}
return block, nil
}

// CopyBlockMetadata copies metadata from one block into another
Expand Down
15 changes: 2 additions & 13 deletions protos/utils/blockutils_test.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,9 @@
/*
Copyright IBM Corp. 2016 All Rights Reserved.
Copyright IBM Corp. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
SPDX-License-Identifier: Apache-2.0
*/

// This package provides unit tests for blocks
package utils_test

import (
Expand Down
Loading

0 comments on commit 9d938b8

Please sign in to comment.