Skip to content
This repository has been archived by the owner on Sep 21, 2022. It is now read-only.

Commit

Permalink
Merge pull request #283 from developerfred/replace-fmt-to-erros
Browse files Browse the repository at this point in the history
chore: Replace fmt.Errorf to errors.Errorf
  • Loading branch information
themandalore committed Nov 18, 2020
2 parents 9238ffc + db54c29 commit 68939ca
Show file tree
Hide file tree
Showing 25 changed files with 139 additions and 132 deletions.
8 changes: 4 additions & 4 deletions cmd/tellor/argTypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
package main

import (
"fmt"
"math/big"

"github.com/ethereum/go-ethereum/common"
"github.com/pkg/errors"
"github.com/tellor-io/TellorMiner/pkg/util"
)

Expand Down Expand Up @@ -41,7 +41,7 @@ type ETHAddress struct {
func (a *ETHAddress) Set(v string) error {
valid := common.IsHexAddress(v)
if !valid {
return fmt.Errorf("%s is not a valid etherum address format", v)
return errors.Errorf("%s is not a valid etherum address format", v)
}
a.addr = common.HexToAddress(v)
return nil
Expand All @@ -63,10 +63,10 @@ func (b *EthereumInt) Set(v string) error {
g := new(big.Int)
_, ok := g.SetString(v, 10)
if !ok {
return fmt.Errorf("%s is not a valid integer", v)
return errors.Errorf("%s is not a valid integer", v)
}
if len(g.Bytes()) > 32 {
return fmt.Errorf("%s is larger than 256 bits", v)
return errors.Errorf("%s is larger than 256 bits", v)
}
b.Int = g
return nil
Expand Down
8 changes: 4 additions & 4 deletions pkg/apiOracle/valueOracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ package apiOracle

import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sync"
"time"

"github.com/pkg/errors"
"github.com/tellor-io/TellorMiner/pkg/config"
"github.com/tellor-io/TellorMiner/pkg/util"
)
Expand Down Expand Up @@ -110,18 +110,18 @@ func EnsureValueOracle() error {
if os.IsNotExist(err) {
exists = false
} else {
return fmt.Errorf("file %s stat error: %v", historyPath, err)
return errors.Errorf("file %s stat error: %v", historyPath, err)
}
}

if exists {
byteValue, err := ioutil.ReadFile(historyPath)
if err != nil {
return fmt.Errorf("failed to read psr file @ %s: %v", historyPath, err)
return errors.Errorf("failed to read psr file @ %s: %v", historyPath, err)
}
err = json.Unmarshal(byteValue, &valueHistory)
if err != nil {
return fmt.Errorf("failed to unmarshal saved")
return errors.Errorf("failed to unmarshal saved")
}
} else {
valueHistory = make(map[string]*Window)
Expand Down
31 changes: 15 additions & 16 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package config
import (
"encoding/hex"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path"
Expand Down Expand Up @@ -41,7 +40,7 @@ func (d *Duration) UnmarshalJSON(b []byte) error {
d.Duration = dur
return nil
default:
return fmt.Errorf("invalid duration")
return errors.Errorf("invalid duration")
}
}

Expand Down Expand Up @@ -133,7 +132,7 @@ const PrivateKeyEnvName = "ETH_PRIVATE_KEY"
func ParseConfig(path string) error {
data, err := ioutil.ReadFile(path)
if err != nil {
return fmt.Errorf("failed to open config file %s: %v", path, err)
return errors.Errorf("failed to open config file %s: %v", path, err)
}

return ParseConfigBytes(data)
Expand All @@ -142,7 +141,7 @@ func ParseConfig(path string) error {
func ParseConfigBytes(data []byte) error {
err := json.Unmarshal(data, &config)
if err != nil {
return fmt.Errorf("failed to parse json: %s", err.Error())
return errors.Errorf("failed to parse json: %s", err.Error())
}
// Check if the env is already set, only try loading .env if its not there.
if config.PrivateKey == "" {
Expand All @@ -153,7 +152,7 @@ func ParseConfigBytes(data []byte) error {

config.PrivateKey = os.Getenv(PrivateKeyEnvName)
if config.PrivateKey == "" {
return fmt.Errorf("missing ethereum wallet private key environment variable '%s'", PrivateKeyEnvName)
return errors.Errorf("missing ethereum wallet private key environment variable '%s'", PrivateKeyEnvName)
}
}

Expand All @@ -170,38 +169,38 @@ func ParseConfigBytes(data []byte) error {

err = validateConfig(&config)
if err != nil {
return fmt.Errorf("validation failed: %s", err)
return errors.Errorf("validation failed: %s", err)
}
return nil
}

func validateConfig(cfg *Config) error {
b, err := hex.DecodeString(cfg.PublicAddress)
if err != nil || len(b) != 20 {
return fmt.Errorf("expecting 40 hex character public address, got \"%s\"", cfg.PublicAddress)
return errors.Errorf("expecting 40 hex character public address, got \"%s\"", cfg.PublicAddress)
}
if cfg.EnablePoolWorker {
if len(cfg.Worker) == 0 {
return fmt.Errorf("worker name required for pool")
return errors.Errorf("worker name required for pool")
}
if len(cfg.Password) == 0 {
return fmt.Errorf("password name required for pool")
return errors.Errorf("password name required for pool")
}
} else {
b, err = hex.DecodeString(cfg.PrivateKey)
if err != nil || len(b) != 32 {
return fmt.Errorf("expecting 64 hex character private key, got \"%s\"", cfg.PrivateKey)
return errors.Errorf("expecting 64 hex character private key, got \"%s\"", cfg.PrivateKey)
}
if len(cfg.ContractAddress) != 42 {
return fmt.Errorf("expecting 40 hex character contract address, got \"%s\"", cfg.ContractAddress)
return errors.Errorf("expecting 40 hex character contract address, got \"%s\"", cfg.ContractAddress)
}
b, err = hex.DecodeString(cfg.ContractAddress[2:])
if err != nil || len(b) != 20 {
return fmt.Errorf("expecting 40 hex character contract address, got \"%s\"", cfg.ContractAddress)
return errors.Errorf("expecting 40 hex character contract address, got \"%s\"", cfg.ContractAddress)
}

if cfg.GasMultiplier < 0 || cfg.GasMultiplier > 20 {
return fmt.Errorf("gas multiplier out of range [0, 20] %f", cfg.GasMultiplier)
return errors.Errorf("gas multiplier out of range [0, 20] %f", cfg.GasMultiplier)
}
}

Expand All @@ -210,13 +209,13 @@ func validateConfig(cfg *Config) error {
continue
}
if gpuConfig.Count == 0 {
return fmt.Errorf("gpu '%s' requires 'count' > 0", name)
return errors.Errorf("gpu '%s' requires 'count' > 0", name)
}
if gpuConfig.GroupSize == 0 {
return fmt.Errorf("gpu '%s' requires 'groupSize' > 0", name)
return errors.Errorf("gpu '%s' requires 'groupSize' > 0", name)
}
if gpuConfig.Groups == 0 {
return fmt.Errorf("gpu '%s' requires 'groups' > 0", name)
return errors.Errorf("gpu '%s' requires 'groups' > 0", name)
}
}

Expand Down
7 changes: 3 additions & 4 deletions pkg/db/localDataProxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
package db

import (
"fmt"

"github.com/pkg/errors"
"github.com/tellor-io/TellorMiner/pkg/util"
)

Expand Down Expand Up @@ -56,7 +55,7 @@ func (l *localProxy) Put(key string, value []byte) (map[string][]byte, error) {
func (l *localProxy) BatchPut(keys []string, values [][]byte) (map[string][]byte, error) {

if len(values) > 0 && len(keys) != len(values) {
return nil, fmt.Errorf("Keys and values must have same array dimensions")
return nil, errors.Errorf("Keys and values must have same array dimensions")
}
for idx, k := range keys {
err := l.localDB.Put(k, values[idx])
Expand All @@ -69,5 +68,5 @@ func (l *localProxy) BatchPut(keys []string, values [][]byte) (map[string][]byte
}

func (l *localProxy) IncomingRequest(data []byte) ([]byte, error) {
return nil, fmt.Errorf("Local proxy should never be called with incoming requests")
return nil, errors.Errorf("Local proxy should never be called with incoming requests")
}
11 changes: 6 additions & 5 deletions pkg/db/remoteDataProxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
lru "github.com/hashicorp/golang-lru"
"github.com/pkg/errors"
"github.com/tellor-io/TellorMiner/pkg/config"
"github.com/tellor-io/TellorMiner/pkg/util"
)
Expand Down Expand Up @@ -214,7 +215,7 @@ func (i *remoteImpl) BatchGet(keys []string) (map[string][]byte, error) {
return nil, err
}
if len(remResp.errorMsg) > 0 {
return nil, fmt.Errorf(remResp.errorMsg)
return nil, errors.Errorf(remResp.errorMsg)
}
return remResp.dbVals, nil
}
Expand Down Expand Up @@ -253,7 +254,7 @@ func (i *remoteImpl) BatchPut(keys []string, values [][]byte) (map[string][]byte
return nil, err
}
if len(remResp.errorMsg) > 0 {
return nil, fmt.Errorf(remResp.errorMsg)
return nil, errors.Errorf(remResp.errorMsg)
}
return remResp.dbVals, nil
}
Expand All @@ -272,20 +273,20 @@ func (i *remoteImpl) Verify(hash []byte, timestamp int64, sig []byte) error {
rdbLog.Debug("Verifying signature from %v request against whitelist: %v", ashex, i.whitelist[ashex])
if !i.whitelist[ashex] {
rdbLog.Warn("Unauthorized miner detected with address: %v", ashex)
return fmt.Errorf("Unauthorized")
return errors.Errorf("Unauthorized")
}

cache := i.wlHistory[ashex]
if cache == nil {
return fmt.Errorf("No history found for address")
return errors.Errorf("No history found for address")
}
if cache.Contains(timestamp) {
rdbLog.Debug("Miner %v already made request at %v", ashex, timestamp)
expr := time.Unix(timestamp+_validityThreshold, 0)
now := time.Now()
if now.After(expr) {
rdbLog.Warn("Request time %v expired (%v)", time.Unix(timestamp, 0), now)
return fmt.Errorf("Request expired")
return errors.Errorf("Request expired")
}
rdbLog.Debug("Time of last request: %v compared to %v", expr, now)

Expand Down
12 changes: 6 additions & 6 deletions pkg/db/remoteRequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ package db

import (
"bytes"
"fmt"
"io"
"time"

"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/log"
"github.com/pkg/errors"
"github.com/tellor-io/TellorMiner/pkg/util"
)

Expand Down Expand Up @@ -72,7 +72,7 @@ func createRequest(dbKeys []string, values [][]byte, signer RequestSigner) (*req
}
if sig == nil {
log.Error("Signature was not generated")
return nil, fmt.Errorf("Could not generate a signature for hash: %v", hash)
return nil, errors.Errorf("Could not generate a signature for hash: %v", hash)
}
return &requestPayload{dbKeys: dbKeys, dbValues: values, timestamp: t, sig: sig}, nil
}
Expand All @@ -87,7 +87,7 @@ func encodeKeysValuesAndTime(buf *bytes.Buffer, dbKeys []string, values [][]byte
}
if dbKeys == nil {
rrlog.Error("No keys to encode")
return fmt.Errorf("No keys to encode")
return errors.Errorf("No keys to encode")
}

rrlog.Debug("Encoding dbKeys")
Expand Down Expand Up @@ -163,10 +163,10 @@ func encodeRequest(r *requestPayload) ([]byte, error) {
buf := new(bytes.Buffer)

if r.dbKeys == nil || len(r.dbKeys) == 0 {
return nil, fmt.Errorf("No keys in request. No point in making a request if there are no keys")
return nil, errors.Errorf("No keys in request. No point in making a request if there are no keys")
}
if r.sig == nil {
return nil, fmt.Errorf("Cannot encode a request without a signature attached")
return nil, errors.Errorf("Cannot encode a request without a signature attached")
}

//capture keys and timestamp
Expand Down Expand Up @@ -195,7 +195,7 @@ func decodeRequest(data []byte, validator RequestValidator) (*requestPayload, er
return nil, err
}
if len(keys) == 0 {
return nil, fmt.Errorf("No dbKeys in incoming request")
return nil, errors.Errorf("No dbKeys in incoming request")
}
sig, err := decodeBytes(buf)
if err != nil {
Expand Down

0 comments on commit 68939ca

Please sign in to comment.