Skip to content

Commit 274d27e

Browse files
refactor(fabricx): consistent use of errors package
Signed-off-by: Marcus Brandenburger <bur@zurich.ibm.com>
1 parent c643f48 commit 274d27e

File tree

15 files changed

+107
-109
lines changed

15 files changed

+107
-109
lines changed

platform/fabricx/core/committer/txhandler.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ package committer
88

99
import (
1010
"context"
11-
"fmt"
1211

1312
"github.com/hyperledger-labs/fabric-smart-client/pkg/utils/errors"
1413
"github.com/hyperledger-labs/fabric-smart-client/platform/common/services/logging"
@@ -37,7 +36,7 @@ type handler struct {
3736

3837
func (h *handler) HandleFabricxTransaction(ctx context.Context, blkMetadata *cb.BlockMetadata, tx committer.CommitTx) (*committer.FinalityEvent, error) {
3938
if len(blkMetadata.Metadata) < statusIdx {
40-
return nil, fmt.Errorf("block metadata lacks transaction filter")
39+
return nil, errors.New("block metadata lacks transaction filter")
4140
}
4241

4342
statusCode := protoblocktx.Status(blkMetadata.Metadata[statusIdx][tx.TxNum])
@@ -61,7 +60,7 @@ func (h *handler) HandleFabricxTransaction(ctx context.Context, blkMetadata *cb.
6160
// escaping the switch and discard
6261
break
6362
}
64-
return nil, fmt.Errorf("failed committing transaction [txID=%s]: %w", event.TxID, err)
63+
return nil, errors.Wrapf(err, "committing endorser transaction [txID=%s]", event.TxID)
6564
}
6665
if !processed {
6766
logger.Debugf("TODO: Should we try to get chaincode events?")
@@ -74,7 +73,7 @@ func (h *handler) HandleFabricxTransaction(ctx context.Context, blkMetadata *cb.
7473

7574
logger.Warnf("discarding transaction [txID=%s] [reason=%v]", tx.TxID, statusCode.String())
7675
if err := h.committer.DiscardEndorserTransaction(ctx, event.TxID, tx.BlkNum, tx.Raw, event); err != nil {
77-
return nil, fmt.Errorf("failed discarding transaction [txID=%s]: %w", event.TxID, err)
76+
return nil, errors.Wrapf(err, "discarding endorser transaction [txID=%s]", event.TxID)
7877
}
7978

8079
return event, nil

platform/fabricx/core/finality/config.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ SPDX-License-Identifier: Apache-2.0
77
package finality
88

99
import (
10-
"fmt"
1110
"time"
11+
12+
"github.com/hyperledger-labs/fabric-smart-client/pkg/utils/errors"
1213
)
1314

1415
const DefaultRequestTimeout = 30 * time.Second
@@ -37,7 +38,7 @@ func NewConfig(configService ConfigService) (*Config, error) {
3738

3839
err := configService.UnmarshalKey("notificationService", &config)
3940
if err != nil {
40-
return config, fmt.Errorf("cannot get notify service config: %w", err)
41+
return config, errors.Wrap(err, "unmarshal notificationService")
4142
}
4243

4344
return config, nil

platform/fabricx/core/finality/grpc.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ package finality
88

99
import (
1010
"errors"
11-
"fmt"
1211
"os"
1312
"time"
1413

@@ -18,12 +17,12 @@ import (
1817
"google.golang.org/grpc/credentials/insecure"
1918
)
2019

21-
var ErrInvalidAddress = fmt.Errorf("empty address")
20+
var ErrInvalidAddress = errors.New("empty address")
2221

2322
func GrpcClient(c *Config) (*grpc.ClientConn, error) {
2423
// no endpoints in config
2524
if len(c.Endpoints) != 1 {
26-
return nil, fmt.Errorf("we need a single endpoint")
25+
return nil, errors.New("we need a single endpoint")
2726
}
2827

2928
// currently we only support connections to a single query service

platform/fabricx/core/ledger/ledger.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ package ledger
88

99
import (
1010
"context"
11-
"fmt"
1211
"sync"
1312
"time"
1413

@@ -53,7 +52,7 @@ func (c *ledger) OnBlock(_ context.Context, block *cb.Block) (bool, error) {
5352
for i, tx := range block.Data.Data {
5453
_, _, chdr, err := fabricutils.UnmarshalTx(tx)
5554
if err != nil {
56-
return false, err
55+
return false, errors.Wrapf(err, "unmarshal transaction channel header")
5756
}
5857

5958
statusCode := protoblocktx.Status(block.Metadata.Metadata[cb.BlockMetadataIndex_TRANSACTIONS_FILTER][i])
@@ -89,10 +88,10 @@ func (c *ledger) GetTransactionByID(txID string) (driver.ProcessedTransaction, e
8988
status, ok := c.statuses[txID]
9089
c.mu.RUnlock()
9190
if ok {
92-
logger.Debugf("Transaction [%s] found with status [%d]", txID, int32(status))
91+
logger.Debugf("Transaction [txID=%s] found with status [%d]", txID, int32(status))
9392
return &liteTx{txID: txID, validationCode: status}, nil
9493
}
95-
logger.Warnf("transaction [%s] not found. retrying...", txID)
94+
logger.Warnf("Transaction [txID=%s] not found. retrying...", txID)
9695
time.Sleep(1 * time.Second)
9796
}
9897

@@ -107,13 +106,13 @@ func (c *ledger) GetBlockNumberByTxID(txID string) (uint64, error) {
107106
blockNum, ok := c.blockNums[txID]
108107
c.mu.RUnlock()
109108
if ok {
110-
logger.Debugf("Transaction [%s] found with blockNum [%v]", txID, blockNum)
109+
logger.Debugf("Transaction [txID=%s] found with blockNum [%v]", txID, blockNum)
111110
return blockNum, nil
112111
}
113-
logger.Warnf("transaction [%s] not found. retrying...")
112+
logger.Warnf("Transaction [txID=%s] not found. retrying...", txID)
114113
time.Sleep(1 * time.Second)
115114
}
116-
return 0, fmt.Errorf("transaction [%s] not found", txID)
115+
return 0, errors.Errorf("transaction [txID=%s] not found", txID)
117116
}
118117

119118
func (c *ledger) GetBlockByNumber(number uint64) (driver.Block, error) {

platform/fabricx/core/membership/endpoint.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ SPDX-License-Identifier: Apache-2.0
77
package membership
88

99
import (
10-
"fmt"
1110
"regexp"
1211
"strconv"
12+
13+
"github.com/hyperledger-labs/fabric-smart-client/pkg/utils/errors"
1314
)
1415

1516
const (
@@ -19,7 +20,7 @@ const (
1920

2021
var (
2122
myExp = regexp.MustCompile(`id=(\d+),(` + OrdererBroadcastType + `|` + OrdererDeliverType + `),(.*)`)
22-
ErrInvalidEndpointFormat = fmt.Errorf("invalid endpoint format")
23+
ErrInvalidEndpointFormat = errors.New("invalid endpoint format")
2324
)
2425

2526
type endpoint struct {
@@ -37,7 +38,7 @@ func parseEndpoint(str string) (*endpoint, error) {
3738

3839
id, err := strconv.Atoi(match[1])
3940
if err != nil {
40-
return nil, fmt.Errorf("invalid endpoint id: %w", err)
41+
return nil, errors.Wrap(err, "invalid endpoint id")
4142
}
4243

4344
return &endpoint{

platform/fabricx/core/membership/membership.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ SPDX-License-Identifier: Apache-2.0
77
package membership
88

99
import (
10-
"fmt"
1110
"sync"
1211

1312
"github.com/hyperledger-labs/fabric-smart-client/pkg/utils/errors"
@@ -72,30 +71,30 @@ func (c *Service) DryUpdate(env *cb.Envelope) error {
7271
func (c *Service) validateConfig(env *cb.Envelope) (*channelconfig.Bundle, error) {
7372
payload, err := protoutil.UnmarshalPayload(env.Payload)
7473
if err != nil {
75-
return nil, errors.Wrapf(err, "cannot get payload from config transaction")
74+
return nil, errors.Wrapf(err, "unmarshal common payload")
7675
}
7776

7877
cenv, err := configtx.UnmarshalConfigEnvelope(payload.Data)
7978
if err != nil {
80-
return nil, errors.Wrapf(err, "error unmarshalling config which passed initial validity checks")
79+
return nil, errors.Wrapf(err, "unmarshal config envelope")
8180
}
8281

8382
// check if config tx is valid
8483
if c.channelResources != nil {
8584
v := c.channelResources.ConfigtxValidator()
8685
if err := v.Validate(cenv); err != nil {
87-
return nil, errors.Wrap(err, "failed to validate config transaction")
86+
return nil, errors.Wrap(err, "validate config transaction")
8887
}
8988
}
9089

9190
bundle, err := channelconfig.NewBundle(c.channelID, cenv.Config, factory.GetDefault())
9291
if err != nil {
93-
return nil, errors.Wrapf(err, "failed to build a new bundle")
92+
return nil, errors.Wrapf(err, "build a new bundle")
9493
}
9594

9695
channelconfig.LogSanityChecks(bundle)
9796
if err := capabilitiesSupported(bundle); err != nil {
98-
return nil, err
97+
return nil, errors.Wrapf(err, "check bundle capabilities")
9998
}
10099

101100
return bundle, nil
@@ -108,11 +107,11 @@ func capabilitiesSupported(res channelconfig.Resources) error {
108107
}
109108

110109
if err := ac.Capabilities().Supported(); err != nil {
111-
return errors.Wrapf(err, "[Channel %s] incompatible", res.ConfigtxValidator().ChannelID())
110+
return errors.Wrapf(err, "[Channel %s] application config capabilities incompatible", res.ConfigtxValidator().ChannelID())
112111
}
113112

114113
if err := res.ChannelConfig().Capabilities().Supported(); err != nil {
115-
return errors.Wrapf(err, "[Channel %s] incompatible", res.ConfigtxValidator().ChannelID())
114+
return errors.Wrapf(err, "[Channel %s] channel config capabilities incompatible", res.ConfigtxValidator().ChannelID())
116115
}
117116

118117
return nil
@@ -121,7 +120,7 @@ func capabilitiesSupported(res channelconfig.Resources) error {
121120
func (c *Service) IsValid(identity view.Identity) error {
122121
id, err := c.resources().MSPManager().DeserializeIdentity(identity)
123122
if err != nil {
124-
return errors.Wrapf(err, "failed deserializing identity [%s]", identity.String())
123+
return errors.Wrapf(err, "deserializing identity [%s]", identity.String())
125124
}
126125

127126
return id.Validate()
@@ -130,7 +129,7 @@ func (c *Service) IsValid(identity view.Identity) error {
130129
func (c *Service) GetVerifier(identity view.Identity) (driver.Verifier, error) {
131130
id, err := c.resources().MSPManager().DeserializeIdentity(identity)
132131
if err != nil {
133-
return nil, errors.Wrapf(err, "failed deserializing identity [%s]", identity.String())
132+
return nil, errors.Wrapf(err, "deserializing identity [%s]", identity.String())
134133
}
135134
return id, nil
136135
}
@@ -154,7 +153,7 @@ func (c *Service) GetMSPIDs() []string {
154153
func (c *Service) OrdererConfig(cs driver.ConfigService) (string, []*grpc.ConnectionConfig, error) {
155154
oc, ok := c.resources().OrdererConfig()
156155
if !ok || oc.Organizations() == nil {
157-
return "", nil, fmt.Errorf("orderer config does not exist")
156+
return "", nil, errors.New("orderer config does not exist")
158157
}
159158

160159
tlsEnabled, isSet := cs.OrderingTLSEnabled()
@@ -182,7 +181,7 @@ func (c *Service) OrdererConfig(cs driver.ConfigService) (string, []*grpc.Connec
182181

183182
ep, err := parseEndpoint(epStr)
184183
if err != nil {
185-
return "", nil, fmt.Errorf("cannot parse orderer endpoint [%s]: %w", epStr, err)
184+
return "", nil, errors.Wrapf(err, "parse orderer endpoint [%s]", epStr)
186185
}
187186
logger.Debugf("new OS endpoint: %s", epStr)
188187

platform/fabricx/core/transaction/manager.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ package transaction
88

99
import (
1010
"context"
11-
"fmt"
1211
"sync"
1312

1413
"github.com/hyperledger-labs/fabric-smart-client/pkg/utils/errors"
@@ -46,7 +45,7 @@ func (m *Manager) transactionFactory(transactionType driver.TransactionType) (dr
4645
logger.Debugf("transactionFactory called with transactionType [%v]", transactionType)
4746
f, ok := m.transactionFactories.Load(transactionType)
4847
if !ok {
49-
return nil, fmt.Errorf("no transaction factory found for transaction type %v", transactionType)
48+
return nil, errors.Errorf("no transaction factory found for transaction type %v", transactionType)
5049
}
5150

5251
txFactory, ok := f.(driver.TransactionFactory)

platform/fabricx/core/transaction/rwset/loader.go

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ package rwset
88

99
import (
1010
"context"
11-
"fmt"
1211
"strings"
1312

1413
"github.com/hyperledger-labs/fabric-smart-client/pkg/utils/errors"
@@ -54,7 +53,7 @@ func NewLoader(
5453

5554
func (c *Loader) AddHandlerProvider(headerType cb.HeaderType, handlerProvider driver.RWSetPayloadHandlerProvider) error {
5655
if handler, ok := c.handlers[headerType]; ok {
57-
return fmt.Errorf("handler %T already defined for header type %v", handler, headerType)
56+
return errors.Errorf("handler %T already defined for header type %v", handler, headerType)
5857
}
5958

6059
c.handlers[headerType] = handlerProvider(c.Network, c.Channel, c.Vault)
@@ -67,30 +66,34 @@ func (c *Loader) GetRWSetFromEvn(ctx context.Context, txID driver2.TxID) (driver
6766
defer span.AddEvent("end_get_rwset_from_evn")
6867

6968
if !c.EnvelopeService.Exists(ctx, txID) {
70-
return nil, nil, fmt.Errorf("envelope does not exists for [%s]", txID)
69+
return nil, nil, errors.Errorf("envelope does not exists for [txID=%s]", txID)
7170
}
7271

7372
rawEnv, err := c.EnvelopeService.LoadEnvelope(ctx, txID)
7473
if err != nil {
75-
return nil, nil, fmt.Errorf("cannot load envelope [%s]: %w", txID, err)
74+
return nil, nil, errors.Errorf("load envelope [txID=%s]", txID)
7675
}
7776

7877
_, payl, chdr, err := fabricutils.UnmarshalTx(rawEnv)
7978
if err != nil {
80-
return nil, nil, err
79+
return nil, nil, errors.Wrap(err, "unmarshal payload and channel header")
80+
}
81+
82+
if txID != chdr.TxId {
83+
return nil, nil, errors.Errorf("txID mismatch in channel header, expected=%s, actual=%s", txID, chdr.TxId)
8184
}
8285

8386
rws, err := c.Vault.NewRWSetFromBytes(ctx, chdr.TxId, payl.Data)
8487
if err != nil {
85-
return nil, nil, fmt.Errorf("failed to create new rws for [%s]: %w", chdr.TxId, err)
88+
return nil, nil, errors.Wrapf(err, "create new rws for [txID=%s]", chdr.TxId)
8689
}
8790

8891
var function string
8992
if anyKeyContains(rws, "initialized") {
9093
function = "init"
9194
}
9295

93-
logger.Debugf("retrieved processed transaction from env [%s,%s]", txID, function)
96+
logger.Debugf("retrieved processed transaction from envelope [txID=%s] [function=%s]", txID, function)
9497
pt := &processedTransaction{
9598
network: c.Network,
9699
channel: chdr.ChannelId,
@@ -107,22 +110,22 @@ func (c *Loader) GetRWSetFromETx(ctx context.Context, txID driver2.TxID) (driver
107110
defer span.AddEvent("end_get_rwset_from_etx")
108111

109112
if !c.TransactionService.Exists(ctx, txID) {
110-
return nil, nil, fmt.Errorf("transaction does not exists for [%s]", txID)
113+
return nil, nil, errors.Errorf("transaction does not exists for [txID=%s]", txID)
111114
}
112115

113116
raw, err := c.TransactionService.LoadTransaction(ctx, txID)
114117
if err != nil {
115-
return nil, nil, fmt.Errorf("cannot load etx [%s]: %w", txID, err)
118+
return nil, nil, errors.Wrapf(err, "cannot load etx [txID=%s]", txID)
116119
}
117120

118121
tx, err := c.TransactionManager.NewTransactionFromBytes(ctx, c.Channel, raw)
119122
if err != nil {
120-
return nil, nil, err
123+
return nil, nil, errors.Wrap(err, "new transaction from bytes")
121124
}
122125

123126
rws, err := tx.GetRWSet()
124127
if err != nil {
125-
return nil, nil, err
128+
return nil, nil, errors.Wrap(err, "get rwset")
126129
}
127130

128131
return rws, tx, nil
@@ -133,24 +136,24 @@ func (c *Loader) GetInspectingRWSetFromEvn(ctx context.Context, txID driver2.TxI
133136
span.AddEvent("start_get_inspecting_rwset_from_evn")
134137
defer span.AddEvent("end_get_inspecting_rwset_from_evn")
135138

136-
logger.Debugf("retrieve rwset from envelope [%s,%s]", c.Channel, txID)
139+
logger.Debugf("retrieve rwset from envelope [channel=%s] [txID=%s]", c.Channel, txID)
137140

138141
_, payl, chdr, err := fabricutils.UnmarshalTx(envelopeRaw)
139142
if err != nil {
140-
return nil, nil, errors.Wrapf(err, "cannot unmarshal envelope [%s]", txID)
143+
return nil, nil, errors.Wrapf(err, "cannot unmarshal envelope [txID=%s]", txID)
141144
}
142145

143146
rws, err := c.Vault.InspectRWSet(ctx, payl.Data)
144147
if err != nil {
145-
return nil, nil, errors.Wrapf(err, "cannot inspect rwset for [%s]", txID)
148+
return nil, nil, errors.Wrapf(err, "cannot inspect rwset for [txID=%s]", txID)
146149
}
147150

148151
var function string
149152
if anyKeyContains(rws, "initialized") {
150153
function = "init"
151154
}
152155

153-
logger.Debugf("retrieved inspecting processed transaction from env [%s,%s]", txID, function)
156+
logger.Debugf("retrieved inspecting processed transaction from env [txID=%s] [function=%s]", txID, function)
154157
pt := &processedTransaction{
155158
network: c.Network,
156159
channel: chdr.ChannelId,

0 commit comments

Comments
 (0)