Skip to content

Commit

Permalink
Merge pull request #165 from Eriner:errors
Browse files Browse the repository at this point in the history
Replacement of pkg/errors with stdlib errors
  • Loading branch information
Goober the Friendly Robutt committed Apr 13, 2021
2 parents eb7b110 + 1d7f779 commit 36e1081
Show file tree
Hide file tree
Showing 21 changed files with 161 additions and 186 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ go 1.13
require (
filippo.io/edwards25519 v1.0.0-beta.2
github.com/aead/chacha20 v0.0.0-20180709150244-8b13a72661da
github.com/pkg/errors v0.9.1
gitlab.com/NebulousLabs/Sia v1.5.4
gitlab.com/NebulousLabs/encoding v0.0.0-20200604091946-456c3dc907fe
gitlab.com/NebulousLabs/log v0.0.0-20200604091839-0ba4a941cdc2
Expand Down
59 changes: 0 additions & 59 deletions go.sum

Large diffs are not rendered by default.

9 changes: 6 additions & 3 deletions hostdb/hostdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import (
"crypto/ed25519"
"encoding/hex"
"encoding/json"
"fmt"
"net"
"strings"
"time"

"github.com/pkg/errors"
"gitlab.com/NebulousLabs/Sia/modules"
"gitlab.com/NebulousLabs/Sia/types"
"lukechampine.com/us/renterhost"
Expand Down Expand Up @@ -134,7 +134,7 @@ func Scan(ctx context.Context, addr modules.NetAddress, pubkey HostPublicKey) (h
err := func() error {
s, err := renterhost.NewRenterSession(conn, pubkey.Ed25519())
if err != nil {
return errors.Wrap(err, "could not initiate RPC session")
return fmt.Errorf("could not initiate RPC session: %w", err)
}
defer s.Close()
var resp renterhost.RPCSettingsResponse
Expand All @@ -147,7 +147,10 @@ func Scan(ctx context.Context, addr modules.NetAddress, pubkey HostPublicKey) (h
}
return nil
}()
ch <- res{host, errors.Wrap(err, "could not read signed host settings")}
if err != nil {
err = fmt.Errorf("could not read signed host settings: %w", err)
}
ch <- res{host, err}
}()
select {
case <-ctx.Done():
Expand Down
9 changes: 5 additions & 4 deletions renter/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package renter

import (
"bytes"
"errors"
"fmt"
"io"

"github.com/pkg/errors"
"gitlab.com/NebulousLabs/Sia/modules"
"lukechampine.com/us/hostdb"
"lukechampine.com/us/merkle"
Expand Down Expand Up @@ -128,7 +129,7 @@ func (d *ShardDownloader) CopySection(w io.Writer, offset, length int64) error {
// slice is only valid until the next call to DownloadAndDecrypt.
func (d *ShardDownloader) DownloadAndDecrypt(chunkIndex int64) ([]byte, error) {
if chunkIndex >= int64(len(d.Slices)) {
return nil, errors.Errorf("unknown chunk index %v", chunkIndex)
return nil, fmt.Errorf("unknown chunk index %v", chunkIndex)
}
s := d.Slices[chunkIndex]
offset := s.SegmentIndex * merkle.SegmentSize
Expand Down Expand Up @@ -167,12 +168,12 @@ func NewShardDownloader(m *MetaFile, c Contract, hkr HostKeyResolver) (*ShardDow
// get host IP
hostIP, err := hkr.ResolveHostKey(c.HostKey)
if err != nil {
return nil, errors.Wrapf(err, "%v: could not resolve host key", hostKey.ShortKey())
return nil, fmt.Errorf("%v: could not resolve host key: %w", hostKey.ShortKey(), err)
}
// create downloader
d, err := proto.NewSession(hostIP, c.HostKey, c.ID, c.RenterKey, 0)
if err != nil {
return nil, errors.Wrapf(err, "%v: could not initiate download protocol with host", hostKey.ShortKey())
return nil, fmt.Errorf("%v: could not initiate download protocol with host: %w", hostKey.ShortKey(), err)
}
return &ShardDownloader{
Downloader: d,
Expand Down
72 changes: 37 additions & 35 deletions renter/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ import (
"encoding/binary"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"strings"
"time"
"unsafe"

"github.com/aead/chacha20/chacha"
"github.com/pkg/errors"

"gitlab.com/NebulousLabs/Sia/crypto"
"lukechampine.com/frand"
"lukechampine.com/us/hostdb"
Expand Down Expand Up @@ -115,11 +117,11 @@ func (s *KeySeed) XORKeyStream(msg []byte, nonce []byte, startIndex uint64) {
func (m *MetaIndex) Validate() error {
switch {
case m.Version != MetaFileVersion:
return errors.Errorf("incompatible version (%v, want %v)", m.Version, MetaFileVersion)
return fmt.Errorf("incompatible version (%v, want %v)", m.Version, MetaFileVersion)
case m.MinShards == 0:
return errors.Errorf("MinShards cannot be 0")
return fmt.Errorf("MinShards cannot be 0")
case m.MinShards > len(m.Hosts):
return errors.Errorf("MinShards (%v) must not exceed number of hosts (%v)", m.Version, len(m.Hosts))
return fmt.Errorf("MinShards (%v) must not exceed number of hosts (%v)", m.Version, len(m.Hosts))
}
return nil
}
Expand Down Expand Up @@ -197,12 +199,12 @@ func NewMetaFile(mode os.FileMode, size int64, hosts []hostdb.HostPublicKey, min
func WriteMetaFile(filename string, m *MetaFile) error {
// validate before writing
if err := validateShards(m.Shards); err != nil {
return errors.Wrap(err, "invalid shards")
return fmt.Errorf("invalid shards: %w", err)
}

f, err := os.Create(filename + "_tmp")
if err != nil {
return errors.Wrap(err, "could not create archive")
return fmt.Errorf("could not create archive: %w", err)
}
defer f.Close()
zip := gzip.NewWriter(f)
Expand All @@ -216,9 +218,9 @@ func WriteMetaFile(filename string, m *MetaFile) error {
Mode: 0666,
})
if err != nil {
return errors.Wrap(err, "could not write index header")
return fmt.Errorf("could not write index header: %w", err)
} else if _, err = tw.Write(index); err != nil {
return errors.Wrap(err, "could not write index")
return fmt.Errorf("could not write index: %w", err)
}

// write shards
Expand All @@ -230,30 +232,30 @@ func WriteMetaFile(filename string, m *MetaFile) error {
Mode: 0666,
})
if err != nil {
return errors.Wrap(err, "could not write shard header")
return fmt.Errorf("could not write shard header: %w", err)
}
for _, ss := range m.Shards[i] {
copy(encSlice, ss.MerkleRoot[:])
binary.LittleEndian.PutUint32(encSlice[32:], ss.SegmentIndex)
binary.LittleEndian.PutUint32(encSlice[36:], ss.NumSegments)
copy(encSlice[40:], ss.Nonce[:])
if _, err = tw.Write(encSlice); err != nil {
return errors.Wrap(err, "could not add shard to archive")
return fmt.Errorf("could not add shard to archive: %w", err)
}
}
}

// flush, close, and atomically rename
if err := tw.Close(); err != nil {
return errors.Wrap(err, "could not write tar data")
return fmt.Errorf("could not write tar data: %w", err)
} else if err := zip.Close(); err != nil {
return errors.Wrap(err, "could not write gzip data")
return fmt.Errorf("could not write gzip data: %w", err)
} else if err := f.Sync(); err != nil {
return errors.Wrap(err, "could not sync archive file")
return fmt.Errorf("could not sync archive file: %w", err)
} else if err := f.Close(); err != nil {
return errors.Wrap(err, "could not close archive file")
return fmt.Errorf("could not close archive file: %w", err)
} else if err := os.Rename(filename+"_tmp", filename); err != nil {
return errors.Wrap(err, "could not atomically replace archive file")
return fmt.Errorf("could not atomically replace archive file: %w", err)
}

return nil
Expand All @@ -263,12 +265,12 @@ func WriteMetaFile(filename string, m *MetaFile) error {
func ReadMetaFile(filename string) (*MetaFile, error) {
f, err := os.Open(filename)
if err != nil {
return nil, errors.Wrap(err, "could not open archive")
return nil, fmt.Errorf("could not open archive: %w", err)
}
defer f.Close()
zip, err := gzip.NewReader(f)
if err != nil {
return nil, errors.Wrap(err, "could not read gzip header")
return nil, fmt.Errorf("could not read gzip header: %w", err)
}
tr := tar.NewReader(zip)

Expand All @@ -282,21 +284,21 @@ func ReadMetaFile(filename string) (*MetaFile, error) {
}
break
} else if err != nil {
return nil, errors.Wrap(err, "could not read archive entry")
return nil, fmt.Errorf("could not read archive entry: %w", err)
}

if hdr.Name == indexFilename {
// read index
if err = json.NewDecoder(tr).Decode(&m.MetaIndex); err != nil {
return nil, errors.Wrap(err, "could not decode index")
return nil, fmt.Errorf("could not decode index: %w", err)
}
} else {
// read shard
shard := make([]SectorSlice, hdr.Size/SectorSliceSize)
buf := make([]byte, SectorSliceSize)
for i := range shard {
if _, err := io.ReadFull(tr, buf); err != nil {
return nil, errors.Wrap(err, "could not read shard")
return nil, fmt.Errorf("could not read shard: %w", err)
}
copy(shard[i].MerkleRoot[:], buf[:32])
shard[i].SegmentIndex = binary.LittleEndian.Uint32(buf[32:36])
Expand All @@ -310,25 +312,25 @@ func ReadMetaFile(filename string) (*MetaFile, error) {
}
}
if err := zip.Close(); err != nil {
return nil, errors.Wrap(err, "archive is corrupted")
return nil, fmt.Errorf("archive is corrupted: %w", err)
}

// now that we have the index and all shards in memory, order the shards
// according the Hosts list in the index
if len(shards) != len(m.Hosts) {
return nil, errors.Errorf("invalid metafile: number of shards (%v) does not match number of hosts (%v)", len(shards), len(m.Hosts))
return nil, fmt.Errorf("invalid metafile: number of shards (%v) does not match number of hosts (%v)", len(shards), len(m.Hosts))
}
m.Shards = make([][]SectorSlice, len(m.Hosts))
for hpk, shard := range shards {
i := m.HostIndex(hpk)
if i == -1 {
return nil, errors.Errorf("invalid shard filename: host %q not present in index", hpk)
return nil, fmt.Errorf("invalid shard filename: host %q not present in index", hpk)
}
m.Shards[i] = shard
}

if err := validateShards(m.Shards); err != nil {
return nil, errors.Wrap(err, "invalid shards")
return nil, fmt.Errorf("invalid shards: %w", err)
}

return m, nil
Expand All @@ -338,13 +340,13 @@ func ReadMetaFile(filename string) (*MetaFile, error) {
func ReadMetaIndex(filename string) (MetaIndex, error) {
f, err := os.Open(filename)
if err != nil {
return MetaIndex{}, errors.Wrap(err, "could not open archive")
return MetaIndex{}, fmt.Errorf("could not open archive: %w", err)
}
defer f.Close()

zip, err := gzip.NewReader(f)
if err != nil {
return MetaIndex{}, errors.Wrap(err, "could not read gzip header")
return MetaIndex{}, fmt.Errorf("could not read gzip header: %w", err)
}
defer zip.Close()

Expand All @@ -355,13 +357,13 @@ func ReadMetaIndex(filename string) (MetaIndex, error) {
if err == io.EOF {
break
} else if err != nil {
return MetaIndex{}, errors.Wrap(err, "could not read archive entry")
return MetaIndex{}, fmt.Errorf("could not read archive entry: %w", err)
} else if hdr.Name != indexFilename {
continue // skip entry
}

if err := json.NewDecoder(tr).Decode(&index); err != nil {
return MetaIndex{}, errors.Wrap(err, "could not decode index")
return MetaIndex{}, fmt.Errorf("could not decode index: %w", err)
}
// done
return index, nil
Expand Down Expand Up @@ -394,13 +396,13 @@ func MetaFileCanDownload(filename string) (bool, error) {
func readMetaFileShards(filename string) (MetaIndex, int, error) {
f, err := os.Open(filename)
if err != nil {
return MetaIndex{}, 0, errors.Wrap(err, "could not open archive")
return MetaIndex{}, 0, fmt.Errorf("could not open archive: %w", err)
}
defer f.Close()

zip, err := gzip.NewReader(f)
if err != nil {
return MetaIndex{}, 0, errors.Wrap(err, "could not read gzip header")
return MetaIndex{}, 0, fmt.Errorf("could not read gzip header: %w", err)
}
defer zip.Close()

Expand All @@ -413,11 +415,11 @@ func readMetaFileShards(filename string) (MetaIndex, int, error) {
if err == io.EOF {
break
} else if err != nil {
return MetaIndex{}, 0, errors.Wrap(err, "could not read archive entry")
return MetaIndex{}, 0, fmt.Errorf("could not read archive entry: %w", err)
}
if hdr.Name == indexFilename {
if err := json.NewDecoder(tr).Decode(&index); err != nil {
return MetaIndex{}, 0, errors.Wrap(err, "could not decode index")
return MetaIndex{}, 0, fmt.Errorf("could not decode index: %w", err)
}
haveIndex = true
} else {
Expand All @@ -427,7 +429,7 @@ func readMetaFileShards(filename string) (MetaIndex, int, error) {
buf := make([]byte, SectorSliceSize)
for i := 0; i < numSlices; i++ {
if _, err := io.ReadFull(tr, buf); err != nil {
return MetaIndex{}, 0, errors.Wrap(err, "could not read shard")
return MetaIndex{}, 0, fmt.Errorf("could not read shard: %w", err)
}
numSegments += int64(binary.LittleEndian.Uint32(buf[36:40]))
}
Expand All @@ -438,7 +440,7 @@ func readMetaFileShards(filename string) (MetaIndex, int, error) {
return MetaIndex{}, 0, errors.New("archive does not contain an index")
}
if err := index.Validate(); err != nil {
return MetaIndex{}, 0, errors.Wrap(err, "invalid index")
return MetaIndex{}, 0, fmt.Errorf("invalid index: %w", err)
}

// count full shards
Expand All @@ -462,7 +464,7 @@ func validateShards(shards [][]SectorSlice) error {
for j := 1; j < len(shards); j++ {
s2 := shards[j][chunkIndex]
if s.NumSegments != s2.NumSegments {
return errors.Errorf("shards %v and %v differ at chunk %v", 0, j, chunkIndex)
return fmt.Errorf("shards %v and %v differ at chunk %v", 0, j, chunkIndex)
}
}
}
Expand Down
9 changes: 5 additions & 4 deletions renter/proto/formcontract.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package proto

import (
"crypto/ed25519"
"errors"
"fmt"
"math/big"
"time"

"github.com/pkg/errors"
"gitlab.com/NebulousLabs/Sia/crypto"
"gitlab.com/NebulousLabs/Sia/types"
"lukechampine.com/us/ed25519hash"
Expand Down Expand Up @@ -41,7 +42,7 @@ func (s *Session) FormContract(w Wallet, tpool TransactionPool, key ed25519.Priv
// get a renter address for the file contract's valid/missed outputs
refundAddr, err := w.Address()
if err != nil {
return ContractRevision{}, nil, errors.Wrap(err, "could not get an address to use")
return ContractRevision{}, nil, fmt.Errorf("could not get an address to use: %w", err)
}

// create unlock conditions
Expand Down Expand Up @@ -104,7 +105,7 @@ func (s *Session) FormContract(w Wallet, tpool TransactionPool, key ed25519.Priv
// tax, and a transaction fee.
_, maxFee, err := tpool.FeeEstimate()
if err != nil {
return ContractRevision{}, nil, errors.Wrap(err, "could not estimate transaction fee")
return ContractRevision{}, nil, fmt.Errorf("could not estimate transaction fee: %w", err)
}
fee := maxFee.Mul64(estTxnSize)
totalCost := renterPayout.Add(s.host.ContractPrice).Add(types.Tax(startHeight, fc.Payout)).Add(fee)
Expand Down Expand Up @@ -154,7 +155,7 @@ func (s *Session) FormContract(w Wallet, tpool TransactionPool, key ed25519.Priv
txn.TransactionSignatures = addedSignatures
err = w.SignTransaction(&txn, toSign)
if err != nil {
err = errors.Wrap(err, "failed to sign transaction")
err = fmt.Errorf("failed to sign transaction: %w", err)
s.sess.WriteResponse(nil, errors.New("internal error")) // don't want to reveal too much
return ContractRevision{}, nil, err
}
Expand Down
Loading

0 comments on commit 36e1081

Please sign in to comment.