Skip to content

Commit

Permalink
refactoring for (#592)
Browse files Browse the repository at this point in the history
delete archived package github.com/pkg/errors

Signed-off-by: Fedor Partanskiy <[email protected]>
  • Loading branch information
pfi79 authored May 21, 2024
1 parent 31fe7de commit 0fcb06e
Show file tree
Hide file tree
Showing 25 changed files with 84 additions and 797 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ go 1.22.3

require (
github.com/golang/protobuf v1.5.4
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.9.0
go.uber.org/zap v1.27.0
golang.org/x/sync v0.7.0
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
Expand Down
2 changes: 1 addition & 1 deletion internal/bft/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package bft_test

import (
"errors"
"os"
"sync"
"sync/atomic"
Expand All @@ -20,7 +21,6 @@ import (
"github.com/hyperledger-labs/SmartBFT/pkg/types"
"github.com/hyperledger-labs/SmartBFT/pkg/wal"
protos "github.com/hyperledger-labs/SmartBFT/smartbftprotos"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"go.uber.org/zap"
Expand Down
18 changes: 9 additions & 9 deletions internal/bft/requestpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ package bft
import (
"container/list"
"context"
"errors"
"fmt"
"sync"
"time"

"github.com/hyperledger-labs/SmartBFT/pkg/api"
"github.com/hyperledger-labs/SmartBFT/pkg/metrics/disabled"
"github.com/hyperledger-labs/SmartBFT/pkg/types"
"github.com/pkg/errors"
"golang.org/x/sync/semaphore"
)

Expand All @@ -27,10 +27,10 @@ const (
)

var (
ErrReqAlreadyExists = fmt.Errorf("request already exists")
ErrReqAlreadyProcessed = fmt.Errorf("request already processed")
ErrRequestTooBig = fmt.Errorf("submitted request is too big")
ErrSubmitTimeout = fmt.Errorf("timeout submitting to request pool")
ErrReqAlreadyExists = errors.New("request already exists")
ErrReqAlreadyProcessed = errors.New("request already processed")
ErrRequestTooBig = errors.New("submitted request is too big")
ErrSubmitTimeout = errors.New("timeout submitting to request pool")
)

//go:generate mockery -dir . -name RequestTimeoutHandler -case underscore -output ./mocks/
Expand Down Expand Up @@ -191,7 +191,7 @@ func (rp *Pool) isClosed() bool {
func (rp *Pool) Submit(request []byte) error {
reqInfo := rp.inspector.RequestID(request)
if rp.isClosed() {
return errors.Errorf("pool closed, request rejected: %s", reqInfo)
return fmt.Errorf("pool closed, request rejected: %s", reqInfo)
}

if uint64(len(request)) > rp.options.RequestMaxBytes {
Expand Down Expand Up @@ -227,7 +227,7 @@ func (rp *Pool) Submit(request []byte) error {
rp.metrics.CountOfFailAddRequestToPool.With(
rp.metrics.LabelsForWith("reason", api.ReasonSemaphoreAcquireFail)...,
).Add(1)
return errors.Wrapf(err, "acquiring semaphore for request: %s", reqInfo)
return fmt.Errorf("acquiring semaphore for request: %s: %w", reqInfo, err)
}

reqCopy := append(make([]byte, 0), request...)
Expand Down Expand Up @@ -308,7 +308,7 @@ func (rp *Pool) NextRequests(maxCount int, maxSizeBytes uint64, check bool) (bat
var totalSize uint64
batch = make([][]byte, 0, count)
element := rp.fifo.Front()
for i := 0; i < count; i++ {
for range count {
req := element.Value.(*requestItem).request
reqLen := uint64(len(req))
if totalSize+reqLen > maxSizeBytes {
Expand Down Expand Up @@ -380,7 +380,7 @@ func (rp *Pool) RemoveRequest(requestInfo types.RequestInfo) error {
rp.moveToDelSlice(requestInfo)
errStr := fmt.Sprintf("request %s is not in the pool at remove time", requestInfo)
rp.logger.Debugf(errStr)
return fmt.Errorf(errStr)
return errors.New(errStr)
}

rp.deleteRequest(element, requestInfo)
Expand Down
2 changes: 1 addition & 1 deletion internal/bft/requestpool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"bytes"
"crypto/rand"
"encoding/binary"
"errors"
"fmt"
"sync"
"testing"
Expand All @@ -17,7 +18,6 @@ import (
"github.com/hyperledger-labs/SmartBFT/internal/bft"
"github.com/hyperledger-labs/SmartBFT/internal/bft/mocks"
"github.com/hyperledger-labs/SmartBFT/pkg/types"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"go.uber.org/zap"
Expand Down
14 changes: 7 additions & 7 deletions internal/bft/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
package bft

import (
"errors"
"fmt"

"github.com/golang/protobuf/proto"
"github.com/hyperledger-labs/SmartBFT/pkg/api"
"github.com/hyperledger-labs/SmartBFT/pkg/types"
protos "github.com/hyperledger-labs/SmartBFT/smartbftprotos"
"github.com/pkg/errors"
)

type StateRecorder struct {
Expand Down Expand Up @@ -84,7 +84,7 @@ func (ps *PersistedState) LoadNewViewIfApplicable() (*types.ViewAndSeq, error) {
lastPersistedMessage := &protos.SavedMessage{}
if err := proto.Unmarshal(lastEntry, lastPersistedMessage); err != nil {
ps.Logger.Errorf("Failed unmarshaling last entry from WAL: %v", err)
return nil, errors.Wrap(err, "failed unmarshaling last entry from WAL")
return nil, fmt.Errorf("failed unmarshaling last entry from WAL: %w", err)
}
if newViewMsg := lastPersistedMessage.GetNewView(); newViewMsg != nil {
ps.Logger.Infof("last entry in WAL is a newView record")
Expand All @@ -103,7 +103,7 @@ func (ps *PersistedState) LoadViewChangeIfApplicable() (*protos.ViewChange, erro
lastPersistedMessage := &protos.SavedMessage{}
if err := proto.Unmarshal(lastEntry, lastPersistedMessage); err != nil {
ps.Logger.Errorf("Failed unmarshaling last entry from WAL: %v", err)
return nil, errors.Wrap(err, "failed unmarshaling last entry from WAL")
return nil, fmt.Errorf("failed unmarshaling last entry from WAL: %w", err)
}
if viewChangeMsg := lastPersistedMessage.GetViewChange(); viewChangeMsg != nil {
ps.Logger.Infof("last entry in WAL is a viewChange message")
Expand All @@ -128,7 +128,7 @@ func (ps *PersistedState) Restore(v *View) error {
lastPersistedMessage := &protos.SavedMessage{}
if err := proto.Unmarshal(lastEntry, lastPersistedMessage); err != nil {
ps.Logger.Errorf("Failed unmarshaling last entry from WAL: %v", err)
return errors.Wrap(err, "failed unmarshaling last entry from WAL")
return fmt.Errorf("failed unmarshaling last entry from WAL: %w", err)
}

if proposed := lastPersistedMessage.GetProposedRecord(); proposed != nil {
Expand All @@ -149,7 +149,7 @@ func (ps *PersistedState) Restore(v *View) error {
return nil
}

return errors.Errorf("unrecognized record: %v", lastPersistedMessage)
return fmt.Errorf("unrecognized record: %v", lastPersistedMessage)
}

func (ps *PersistedState) recoverProposed(lastPersistedMessage *protos.ProposedRecord, v *View) error {
Expand Down Expand Up @@ -184,12 +184,12 @@ func (ps *PersistedState) recoverProposed(lastPersistedMessage *protos.ProposedR
func (ps *PersistedState) recoverPrepared(lastPersistedMessage *protos.Message, v *View, entries [][]byte) error {
// Last entry is a commit, so we should have not pruned the previous pre-prepare
if len(entries) < 2 {
return fmt.Errorf("last message is a commit, but expected to also have a matching pre-prepare")
return errors.New("last message is a commit, but expected to also have a matching pre-prepare")
}
prePrepareMsg := &protos.SavedMessage{}
if err := proto.Unmarshal(entries[len(entries)-2], prePrepareMsg); err != nil {
ps.Logger.Errorf("Failed unmarshaling second last entry from WAL: %v", err)
return errors.Wrap(err, "failed unmarshaling last entry from WAL")
return fmt.Errorf("failed unmarshaling last entry from WAL: %w", err)
}

prePrepareFromWAL := prePrepareMsg.GetProposedRecord().GetPrePrepare()
Expand Down
3 changes: 1 addition & 2 deletions internal/bft/statecollector.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,7 @@ func (s *StateCollector) collectedEnoughEqualVotes() *types.ViewAndSeq {
return nil
}
votesMap := make(map[types.ViewAndSeq]uint64)
num := len(s.responses.votes)
for i := 0; i < num; i++ {
for range len(s.responses.votes) {
vote := <-s.responses.votes
response := vote.GetStateTransferResponse()
if response == nil {
Expand Down
4 changes: 2 additions & 2 deletions internal/bft/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func getLeaderID(
return nodes[view%n]
}

for i := 0; i < len(nodes); i++ {
for i := range len(nodes) {
index := (view + (decisionsInView / decisionsPerLeader)) + uint64(i)
node := nodes[index%n]
_, exists := blackListed[node]
Expand Down Expand Up @@ -552,7 +552,7 @@ func equalIntLists(a, b []uint64) bool {
return false
}

for i := 0; i < len(a); i++ {
for i := range len(a) {
if a[i] != b[i] {
return false
}
Expand Down
18 changes: 9 additions & 9 deletions internal/bft/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package bft

import (
"bytes"
"errors"
"fmt"
"sync"
"sync/atomic"
Expand All @@ -16,7 +17,6 @@ import (
"github.com/hyperledger-labs/SmartBFT/pkg/api"
"github.com/hyperledger-labs/SmartBFT/pkg/types"
protos "github.com/hyperledger-labs/SmartBFT/smartbftprotos"
"github.com/pkg/errors"
)

// Phase indicates the status of the view
Expand Down Expand Up @@ -597,7 +597,7 @@ func (v *View) verifyProposal(proposal types.Proposal, prevCommits []*protos.Sig
// Check that the metadata contains a digest of the previous commit signatures
prevCommitDigest := CommitSignaturesDigest(prevCommits)
if !bytes.Equal(prevCommitDigest, md.PrevCommitSignatureDigest) && v.DecisionsPerLeader > 0 {
return nil, errors.Errorf("prev commit signatures received from leader mismatches the metadata digest")
return nil, errors.New("prev commit signatures received from leader mismatches the metadata digest")
}

return requests, nil
Expand Down Expand Up @@ -634,11 +634,11 @@ func (v *View) verifyPrevCommitSignatures(prevCommitSignatures []*protos.Signatu
Value: sig.Value,
}, prevProp)
if err != nil {
return nil, errors.Errorf("failed verifying consenter signature of %d: %v", sig.Signer, err)
return nil, fmt.Errorf("failed verifying consenter signature of %d: %w", sig.Signer, err)
}
prpf := &protos.PreparesFrom{}
if err = proto.Unmarshal(aux, prpf); err != nil {
return nil, errors.Errorf("failed unmarshaling auxiliary input from %d: %v", sig.Signer, err)
return nil, fmt.Errorf("failed unmarshaling auxiliary input from %d: %w", sig.Signer, err)
}
prepareAcknowledgements[sig.Signer] = prpf
}
Expand All @@ -651,7 +651,7 @@ func (v *View) verifyBlacklist(prevCommitSignatures []*protos.Signature, currVer
v.Logger.Debugf("DecisionsPerLeader is 0, hence leader rotation is inactive")
if len(pendingBlacklist) > 0 {
v.Logger.Warnf("Blacklist cannot be non-empty (%v) if rotation is inactive", pendingBlacklist)
return errors.Errorf("rotation is inactive but blacklist is not empty: %v", pendingBlacklist)
return fmt.Errorf("rotation is inactive but blacklist is not empty: %v", pendingBlacklist)
}
return nil
}
Expand All @@ -666,7 +666,7 @@ func (v *View) verifyBlacklist(prevCommitSignatures []*protos.Signature, currVer
if prevPropRaw.VerificationSequence != currVerificationSeq {
// If there has been a reconfiguration, black list should remain the same
if !equalIntLists(prevProposalMetadata.BlackList, pendingBlacklist) {
return errors.Errorf("blacklist changed (%v --> %v) during reconfiguration", prevProposalMetadata.BlackList, pendingBlacklist)
return fmt.Errorf("blacklist changed (%v --> %v) during reconfiguration", prevProposalMetadata.BlackList, pendingBlacklist)
}
v.Logger.Infof("Skipping verifying prev commits due to verification sequence advancing from %d to %d",
prevPropRaw.VerificationSequence, currVerificationSeq)
Expand All @@ -676,7 +676,7 @@ func (v *View) verifyBlacklist(prevCommitSignatures []*protos.Signature, currVer
if v.MembershipNotifier != nil && v.MembershipNotifier.MembershipChange() {
// If there has been a membership change, black list should remain the same
if !equalIntLists(prevProposalMetadata.BlackList, pendingBlacklist) {
return errors.Errorf("blacklist changed (%v --> %v) during membership change", prevProposalMetadata.BlackList, pendingBlacklist)
return fmt.Errorf("blacklist changed (%v --> %v) during membership change", prevProposalMetadata.BlackList, pendingBlacklist)
}
v.Logger.Infof("Skipping verifying prev commits due to membership change")
return nil
Expand All @@ -685,7 +685,7 @@ func (v *View) verifyBlacklist(prevCommitSignatures []*protos.Signature, currVer
_, f := computeQuorum(v.N)

if v.blacklistingSupported(f, myLastCommitSignatures) && len(prevCommitSignatures) < len(myLastCommitSignatures) {
return errors.Errorf("only %d out of %d required previous commits is included in pre-prepare",
return fmt.Errorf("only %d out of %d required previous commits is included in pre-prepare",
len(prevCommitSignatures), len(myLastCommitSignatures))
}

Expand All @@ -709,7 +709,7 @@ func (v *View) verifyBlacklist(prevCommitSignatures []*protos.Signature, currVer

expectedBlacklist := blacklist.computeUpdate()
if !equalIntLists(pendingBlacklist, expectedBlacklist) {
return errors.Errorf("proposed blacklist %v differs from expected %v blacklist", pendingBlacklist, expectedBlacklist)
return fmt.Errorf("proposed blacklist %v differs from expected %v blacklist", pendingBlacklist, expectedBlacklist)
}

return nil
Expand Down
Loading

0 comments on commit 0fcb06e

Please sign in to comment.