Skip to content

Commit

Permalink
flup
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Hamilton <[email protected]>
  • Loading branch information
Eriner committed Apr 13, 2021
1 parent ebfaa53 commit 80aa5fb
Show file tree
Hide file tree
Showing 17 changed files with 94 additions and 95 deletions.
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
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/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
github.com/prometheus/client_golang v0.9.3/go.mod h1:/TN21ttK/J9q6uSwhBd54HahCDft0ttaMvbicHlPoso=
Expand Down
7 changes: 4 additions & 3 deletions renter/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package renter

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

Expand All @@ -14,7 +15,7 @@ import (

// ErrBadChecksum indicates that a piece of sector data failed checksum
// validation.
var ErrBadChecksum = fmt.Errorf("sector data failed checksum validation")
var ErrBadChecksum = errors.New("sector data failed checksum validation")

// A HostKeyResolver resolves a host's public key to the most recent
// NetAddress it announced on the blockchain.
Expand All @@ -40,7 +41,7 @@ type cryptWriter struct {

func calcSections(slices []SectorSlice, offset, length int64) ([]renterhost.RPCReadRequestSection, error) {
if offset < 0 || length < 0 {
return nil, fmt.Errorf("offset and length must be positive")
return nil, errors.New("offset and length must be positive")
} else if len(slices) == 0 || length == 0 {
return nil, nil
}
Expand Down Expand Up @@ -76,7 +77,7 @@ func calcSections(slices []SectorSlice, offset, length int64) ([]renterhost.RPCR
slices = slices[1:]
}
if length > 0 {
return nil, fmt.Errorf("offset+length is out of bounds")
return nil, errors.New("offset+length is out of bounds")
}
// trim final section
if length < 0 {
Expand Down
11 changes: 6 additions & 5 deletions renter/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"encoding/binary"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -83,12 +84,12 @@ func (s KeySeed) MarshalJSON() ([]byte, error) {
// UnmarshalJSON implements the json.Unmarshaler interface.
func (s *KeySeed) UnmarshalJSON(b []byte) error {
if len(b) < 1 {
return fmt.Errorf("wrong seed length")
return errors.New("wrong seed length")
}
if n, err := hex.Decode(s[:], b[1:len(b)-1]); err != nil {
return err
} else if n != len(s) {
return fmt.Errorf("wrong seed length")
return errors.New("wrong seed length")
}
return nil
}
Expand Down Expand Up @@ -279,7 +280,7 @@ func ReadMetaFile(filename string) (*MetaFile, error) {
hdr, err := tr.Next()
if err == io.EOF {
if m.Version == 0 {
return nil, fmt.Errorf("archive is missing an index")
return nil, errors.New("archive is missing an index")
}
break
} else if err != nil {
Expand Down Expand Up @@ -367,7 +368,7 @@ func ReadMetaIndex(filename string) (MetaIndex, error) {
// done
return index, nil
}
return MetaIndex{}, fmt.Errorf("archive is missing an index")
return MetaIndex{}, errors.New("archive is missing an index")
}

// MetaFileFullyUploaded reads a metafile archive and reports whether it has
Expand Down Expand Up @@ -436,7 +437,7 @@ func readMetaFileShards(filename string) (MetaIndex, int, error) {
}
}
if !haveIndex {
return MetaIndex{}, 0, fmt.Errorf("archive does not contain an index")
return MetaIndex{}, 0, errors.New("archive does not contain an index")
}
if err := index.Validate(); err != nil {
return MetaIndex{}, 0, fmt.Errorf("invalid index: %w", err)
Expand Down
5 changes: 3 additions & 2 deletions renter/proto/formcontract.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package proto

import (
"crypto/ed25519"
"errors"
"fmt"
"math/big"
"time"
Expand Down Expand Up @@ -36,7 +37,7 @@ func FormContract(w Wallet, tpool TransactionPool, key ed25519.PrivateKey, host
func (s *Session) FormContract(w Wallet, tpool TransactionPool, key ed25519.PrivateKey, renterPayout types.Currency, startHeight, endHeight types.BlockHeight) (_ ContractRevision, _ []types.Transaction, err error) {
defer wrapErr(&err, "FormContract")
if endHeight < startHeight {
return ContractRevision{}, nil, fmt.Errorf("end height must be greater than start height")
return ContractRevision{}, nil, errors.New("end height must be greater than start height")
}
// get a renter address for the file contract's valid/missed outputs
refundAddr, err := w.Address()
Expand Down Expand Up @@ -155,7 +156,7 @@ func (s *Session) FormContract(w Wallet, tpool TransactionPool, key ed25519.Priv
err = w.SignTransaction(&txn, toSign)
if err != nil {
err = fmt.Errorf("failed to sign transaction: %w", err)
s.sess.WriteResponse(nil, fmt.Errorf("internal error")) // don't want to reveal too much
s.sess.WriteResponse(nil, errors.New("internal error")) // don't want to reveal too much
return ContractRevision{}, nil, err
}

Expand Down
5 changes: 3 additions & 2 deletions renter/proto/renew.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package proto

import (
"crypto/ed25519"
"errors"
"fmt"
"math"
"time"
Expand Down Expand Up @@ -35,7 +36,7 @@ func RenewContract(w Wallet, tpool TransactionPool, id types.FileContractID, key
func (s *Session) RenewContract(w Wallet, tpool TransactionPool, renterPayout types.Currency, startHeight, endHeight types.BlockHeight) (_ ContractRevision, _ []types.Transaction, err error) {
defer wrapErr(&err, "RenewContract")
if endHeight < startHeight {
return ContractRevision{}, nil, fmt.Errorf("end height must be greater than start height")
return ContractRevision{}, nil, errors.New("end height must be greater than start height")
}
// get a renter address for the file contract's valid/missed outputs
refundAddr, err := w.Address()
Expand Down Expand Up @@ -84,7 +85,7 @@ func (s *Session) RenewContract(w Wallet, tpool TransactionPool, renterPayout ty
hostValidPayout := s.host.ContractPrice.Add(basePrice).Add(totalCollateral)
voidMissedPayout := basePrice.Add(baseCollateral)
if hostValidPayout.Cmp(voidMissedPayout) < 0 {
return ContractRevision{}, nil, fmt.Errorf("host's settings are unsatisfiable")
return ContractRevision{}, nil, errors.New("host's settings are unsatisfiable")
}
hostMissedPayout := hostValidPayout.Sub(voidMissedPayout)

Expand Down
47 changes: 19 additions & 28 deletions renter/proto/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,47 +26,38 @@ import (
var (
// ErrInsufficientFunds is returned by various RPCs when the renter is
// unable to provide sufficient payment to the host.
ErrInsufficientFunds = fmt.Errorf("insufficient funds")
ErrInsufficientFunds = errors.New("insufficient funds")

// ErrInvalidMerkleProof is returned by various RPCs when the host supplies
// an invalid Merkle proof.
ErrInvalidMerkleProof = fmt.Errorf("host supplied invalid Merkle proof")
ErrInvalidMerkleProof = errors.New("host supplied invalid Merkle proof")

// ErrContractLocked is returned by the Lock RPC when the contract in
// question is already locked by another party. This is a transient error;
// the caller should retry later.
ErrContractLocked = fmt.Errorf("contract is locked by another party")
ErrContractLocked = errors.New("contract is locked by another party")

// ErrNoContractLocked is returned by RPCs that require a locked contract
// when no contract is locked.
ErrNoContractLocked = fmt.Errorf("no contract locked")
ErrNoContractLocked = errors.New("no contract locked")

// ErrContractFinalized is returned by the Lock RPC when the contract in
// question has reached its maximum revision number, meaning the contract
// can no longer be revised.
ErrContractFinalized = fmt.Errorf("contract cannot be revised further")
ErrContractFinalized = errors.New("contract cannot be revised further")
)

// wrapResponseErr formats RPC response errors nicely, wrapping them in either
// readCtx or rejectCtx depending on whether we encountered an I/O error or the
// host sent an explicit error message.
func wrapResponseErr(err error, readCtx, rejectCtx string) error {
// the original implementation (pkg/errors.Wrap() returns nil on nil err, unlike fmt.Errorf
if err == nil {
return nil
}
// we need the lowest error in the stack - the origin/"cause"
for {
uerr := errors.Unwrap(err)
if uerr == nil {
break
}
err = uerr
}
if _, ok := err.(*renterhost.RPCError); ok {
if errors.As(err, new(*renterhost.RPCError)) {
return fmt.Errorf("%s: %w", rejectCtx, err)
}
return fmt.Errorf("%s: %w", readCtx, err)
if err != nil {
return fmt.Errorf("%s: %w", readCtx, err)
}
return nil
}

type statsConn struct {
Expand Down Expand Up @@ -226,9 +217,9 @@ func (s *Session) Lock(id types.FileContractID, key ed25519.PrivateKey, timeout
}
revHash := renterhost.HashRevision(resp.Revision)
if !ed25519hash.Verify(ed25519hash.ExtractPublicKey(key), revHash, resp.Signatures[0].Signature) {
return fmt.Errorf("renter's signature on claimed revision is invalid")
return errors.New("renter's signature on claimed revision is invalid")
} else if !ed25519hash.Verify(s.host.PublicKey.Ed25519(), revHash, resp.Signatures[1].Signature) {
return fmt.Errorf("host's signature on claimed revision is invalid")
return errors.New("host's signature on claimed revision is invalid")
}
if !resp.Acquired {
return ErrContractLocked
Expand All @@ -253,7 +244,7 @@ func (s *Session) Unlock() (err error) {
defer wrapErr(&err, "Unlock")
defer s.collectStats(renterhost.RPCUnlockID, &err)()
if s.key == nil {
return fmt.Errorf("no contract locked")
return errors.New("no contract locked")
}
s.extendBandwidthDeadline(renterhost.MinMessageSize, renterhost.MinMessageSize)
if err := s.sess.WriteRequest(renterhost.RPCUnlockID, nil); err != nil {
Expand Down Expand Up @@ -289,7 +280,7 @@ func (s *Session) SectorRoots(offset, n int) (_ []crypto.Hash, err error) {
} else if !s.isRevisable() {
return nil, ErrContractFinalized
} else if offset < 0 || n < 0 || offset+n > s.rev.NumSectors() {
return nil, fmt.Errorf("requested range is out-of-bounds")
return nil, errors.New("requested range is out-of-bounds")
} else if n == 0 {
return nil, nil
}
Expand Down Expand Up @@ -334,7 +325,7 @@ func (s *Session) SectorRoots(offset, n int) (_ []crypto.Hash, err error) {

// verify the host signature
if !ed25519hash.Verify(s.host.PublicKey.Ed25519(), revisionHash, resp.Signature) {
return nil, fmt.Errorf("host's signature is invalid")
return nil, errors.New("host's signature is invalid")
}
s.rev.Revision = rev
s.rev.Signatures[0].Signature = req.Signature
Expand Down Expand Up @@ -464,7 +455,7 @@ func (s *Session) Read(w io.Writer, sections []renterhost.RPCReadRequestSection)
if _, err := io.ReadFull(msgReader, lenbuf); err != nil {
return fmt.Errorf("couldn't read data len: %w", err)
} else if binary.LittleEndian.Uint64(lenbuf) != uint64(sec.Length) {
return fmt.Errorf("host sent wrong amount of sector data")
return errors.New("host sent wrong amount of sector data")
}
proofStart := int(sec.Offset) / merkle.SegmentSize
proofEnd := int(sec.Offset+sec.Length) / merkle.SegmentSize
Expand All @@ -480,7 +471,7 @@ func (s *Session) Read(w io.Writer, sections []renterhost.RPCReadRequestSection)
return fmt.Errorf("couldn't read proof len: %w", err)
}
if binary.LittleEndian.Uint64(lenbuf) != uint64(merkle.ProofSize(merkle.SegmentsPerSector, proofStart, proofEnd)) {
return fmt.Errorf("invalid proof size")
return errors.New("invalid proof size")
}
proof := make([]crypto.Hash, binary.LittleEndian.Uint64(lenbuf))
for i := range proof {
Expand Down Expand Up @@ -514,7 +505,7 @@ func (s *Session) Read(w io.Writer, sections []renterhost.RPCReadRequestSection)

// verify the host signature
if !ed25519hash.Verify(s.host.PublicKey.Ed25519(), revisionHash, hostSig) {
return fmt.Errorf("host's signature is invalid")
return errors.New("host's signature is invalid")
}
s.rev.Revision = rev
s.rev.Signatures[0].Signature = renterSig
Expand Down Expand Up @@ -662,7 +653,7 @@ func (s *Session) Write(actions []renterhost.RPCWriteAction) (err error) {

// verify the host signature
if !ed25519hash.Verify(s.host.PublicKey.Ed25519(), revisionHash, hostSig.Signature) {
return fmt.Errorf("host's signature is invalid")
return errors.New("host's signature is invalid")
}
s.rev.Revision = rev
s.rev.Signatures[0].Signature = renterSig.Signature
Expand Down
5 changes: 3 additions & 2 deletions renter/renterutil/fileops.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package renterutil

import (
"bytes"
"errors"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -181,7 +182,7 @@ func (fs *PseudoFS) fillSectors(f *openMetaFile) error {
if _, ok := fs.sectors[hostKey]; !ok {
missingHostErrs = append(missingHostErrs, &HostError{
HostKey: hostKey,
Err: fmt.Errorf("not in filesystem's host set"),
Err: errors.New("not in filesystem's host set"),
})
}
}
Expand Down Expand Up @@ -352,7 +353,7 @@ func (fs *PseudoFS) fileSeek(f *openMetaFile, offset int64, whence int) (int64,
newOffset = f.filesize() - offset
}
if newOffset < 0 {
return 0, fmt.Errorf("seek position cannot be negative")
return 0, errors.New("seek position cannot be negative")
}
f.offset = newOffset
return f.offset, nil
Expand Down
15 changes: 8 additions & 7 deletions renter/renterutil/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package renterutil // import "lukechampine.com/us/renter/renterutil"

import (
"bytes"
"errors"
"fmt"
"io"
"os"
Expand All @@ -22,7 +23,7 @@ const metafileExt = ".usa"

// ErrInvalidFileDescriptor is returned when I/O is attempted on an unknown file
// descriptor.
var ErrInvalidFileDescriptor = fmt.Errorf("invalid file descriptor")
var ErrInvalidFileDescriptor = errors.New("invalid file descriptor")

// helper type to implement os.FileInfo for metafiles
type pseudoFileInfo struct {
Expand Down Expand Up @@ -176,7 +177,7 @@ func (fs *PseudoFS) OpenFile(name string, flag int, perm os.FileMode, minShards
var m *renter.MetaFile
if flag&os.O_CREATE == os.O_CREATE {
if len(fs.hosts.sessions) < minShards {
return nil, fmt.Errorf("minShards cannot be greater than the number of hosts")
return nil, errors.New("minShards cannot be greater than the number of hosts")
}
if flag&os.O_TRUNC == os.O_TRUNC {
// remove existing file
Expand Down Expand Up @@ -494,19 +495,19 @@ type PseudoFile struct {
}

// ErrNotWriteable is returned for write operations on read-only files.
var ErrNotWriteable = fmt.Errorf("file is not writeable")
var ErrNotWriteable = errors.New("file is not writeable")

// ErrNotReadable is returned for read operations on write-only files.
var ErrNotReadable = fmt.Errorf("file is not readable")
var ErrNotReadable = errors.New("file is not readable")

// ErrAppendOnly is returned for seek operations on append-only files.
var ErrAppendOnly = fmt.Errorf("file is append-only")
var ErrAppendOnly = errors.New("file is append-only")

// ErrDirectory is returned for operations that are not valid for directories.
var ErrDirectory = fmt.Errorf("file is a directory")
var ErrDirectory = errors.New("file is a directory")

// ErrNotDirectory is returned for operations that are not valid for files.
var ErrNotDirectory = fmt.Errorf("file is not a directory")
var ErrNotDirectory = errors.New("file is not a directory")

const rwmask = os.O_RDONLY | os.O_WRONLY | os.O_RDWR

Expand Down
11 changes: 2 additions & 9 deletions renter/renterutil/filesystem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,8 @@ func TestHostErrorSet(t *testing.T) {
}
// should get a HostErrorSet when we sync
err = pf.Sync()
for {
uerr := errors.Unwrap(err)
if uerr == nil {
break
}
err = uerr
}
hes, ok := err.(HostErrorSet)
if !ok || hes == nil {
var hes HostErrorSet
if !errors.As(err, &hes) {
t.Fatal("expected HostSetError, got", err)
} else if len(hes) != 3 {
t.Fatal("expected HostSetError to have three hosts, got", len(hes))
Expand Down
7 changes: 5 additions & 2 deletions renter/renterutil/hostset.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package renterutil

import (
"errors"
"fmt"
"strings"
"sync"
Expand All @@ -12,8 +13,10 @@ import (
"lukechampine.com/us/renter/proto"
)

var errNoHost = fmt.Errorf("no record of that host")
var errHostAcquired = fmt.Errorf("host is currently acquired")
var (
errNoHost = errors.New("no record of that host")
errHostAcquired = errors.New("host is currently acquired")
)

// A HostError associates an error with a given host.
type HostError struct {
Expand Down
Loading

0 comments on commit 80aa5fb

Please sign in to comment.