Skip to content

Commit

Permalink
chore(pkg/erros): change errors.Errorf to errors.Wrapf and errors.Wrap
Browse files Browse the repository at this point in the history
  • Loading branch information
developerfred committed Nov 19, 2020
1 parent ab318f7 commit 8ef2340
Show file tree
Hide file tree
Showing 22 changed files with 98 additions and 98 deletions.
2 changes: 1 addition & 1 deletion cmd/tellor/argTypes.go
Expand Up @@ -41,7 +41,7 @@ type ETHAddress struct {
func (a *ETHAddress) Set(v string) error {
valid := common.IsHexAddress(v)
if !valid {
return errors.Errorf("%s is not a valid etherum address format", v)
return errors.Errorf("%s valid etherum address format", v)
}
a.addr = common.HexToAddress(v)
return nil
Expand Down
4 changes: 2 additions & 2 deletions pkg/apiOracle/valueOracle.go
Expand Up @@ -110,14 +110,14 @@ func EnsureValueOracle() error {
if os.IsNotExist(err) {
exists = false
} else {
return errors.Errorf("file %s stat error: %v", historyPath, err)
return errors.Wrapf(err, "stats for file: %v", historyPath)
}
}

if exists {
byteValue, err := ioutil.ReadFile(historyPath)
if err != nil {
return errors.Errorf("failed to read psr file @ %s: %v", historyPath, err)
return errors.Wrapf(err, "failed to read psr file @: %v", historyPath)
}
err = json.Unmarshal(byteValue, &valueHistory)
if err != nil {
Expand Down
24 changes: 12 additions & 12 deletions pkg/config/config.go
Expand Up @@ -132,7 +132,7 @@ const PrivateKeyEnvName = "ETH_PRIVATE_KEY"
func ParseConfig(path string) error {
data, err := ioutil.ReadFile(path)
if err != nil {
return errors.Errorf("failed to open config file %s: %v", path, err)
return errors.Wrapf(err, "to open config file: %v", path)
}

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

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

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

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

func validateConfig(cfg *Config) error {
b, err := hex.DecodeString(cfg.PublicAddress)
if err != nil || len(b) != 20 {
return errors.Errorf("expecting 40 hex character public address, got \"%s\"", cfg.PublicAddress)
return errors.Wrapf(err, "expecting 40 hex character public address, got \"%s\"", cfg.PublicAddress)
}
if cfg.EnablePoolWorker {
if len(cfg.Worker) == 0 {
Expand All @@ -189,18 +189,18 @@ func validateConfig(cfg *Config) error {
} else {
b, err = hex.DecodeString(cfg.PrivateKey)
if err != nil || len(b) != 32 {
return errors.Errorf("expecting 64 hex character private key, got \"%s\"", cfg.PrivateKey)
return errors.Wrapf(err, "expecting 64 hex character private key, got \"%s\"", cfg.PrivateKey)
}
if len(cfg.ContractAddress) != 42 {
return errors.Errorf("expecting 40 hex character contract address, got \"%s\"", cfg.ContractAddress)
return errors.Wrapf(err, "expecting 40 hex character contract address, got \"%s\"", cfg.ContractAddress)
}
b, err = hex.DecodeString(cfg.ContractAddress[2:])
if err != nil || len(b) != 20 {
return errors.Errorf("expecting 40 hex character contract address, got \"%s\"", cfg.ContractAddress)
return errors.Wrapf(err, "expecting 40 hex character contract address, got \"%s\"", cfg.ContractAddress)
}

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

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

Expand Down
4 changes: 2 additions & 2 deletions pkg/db/remoteDataProxy.go
Expand Up @@ -215,7 +215,7 @@ func (i *remoteImpl) BatchGet(keys []string) (map[string][]byte, error) {
return nil, err
}
if len(remResp.errorMsg) > 0 {
return nil, errors.Errorf(remResp.errorMsg)
return nil, errors.Wrapf(err, remResp.errorMsg)
}
return remResp.dbVals, nil
}
Expand Down Expand Up @@ -254,7 +254,7 @@ func (i *remoteImpl) BatchPut(keys []string, values [][]byte) (map[string][]byte
return nil, err
}
if len(remResp.errorMsg) > 0 {
return nil, errors.Errorf(remResp.errorMsg)
return nil, errors.Wrapf(err, remResp.errorMsg)
}
return remResp.dbVals, nil
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/db/remoteRequest.go
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, errors.Errorf("Could not generate a signature for hash: %v", hash)
return nil, errors.Wrapf(err, "Could not generate a signature for hash: %v", hash)
}
return &requestPayload{dbKeys: dbKeys, dbValues: values, timestamp: t, sig: sig}, nil
}
Expand Down
38 changes: 19 additions & 19 deletions pkg/ops/disputeOps.go
Expand Up @@ -42,13 +42,13 @@ func Dispute(requestId *big.Int, timestamp *big.Int, minerIndex *big.Int, ctx co

balance, err := instance.BalanceOf(nil, addr)
if err != nil {
return errors.Errorf("failed to fetch balance: %s", err.Error())
return errors.Wrap(err, "to fetch balance")
}
var asBytes32 [32]byte
copy(asBytes32[:], "0x8b75eb45d88e80f0e4ec77d23936268694c0e7ac2e0c9085c5c6bdfcfbc49239") // keccak256(disputeFee).
disputeCost, err := instance.GetUintVar(nil, asBytes32)
if err != nil {
return errors.Errorf("failed to get dispute cost: %s", err)
return errors.Wrap(err, "to get dispute cost")
}

if balance.Cmp(disputeCost) < 0 {
Expand All @@ -59,13 +59,13 @@ func Dispute(requestId *big.Int, timestamp *big.Int, minerIndex *big.Int, ctx co

auth, err := PrepareEthTransaction(ctx)
if err != nil {
return errors.Errorf("failed to prepare ethereum transaction: %s", err.Error())
return errors.Wrapf(err, "to prepare ethereum transaction")
}

instance2 := ctx.Value(tellorCommon.ContractsTellorContextKey).(*tellor.Tellor)
tx, err := instance2.BeginDispute(auth, requestId, timestamp, minerIndex)
if err != nil {
return errors.Errorf("failed to send dispute txn: %s", err.Error())
return errors.Wrap(err, "to send dispute txn")
}
fmt.Printf("dispute started with txn: %s\n", tx.Hash().Hex())
return nil
Expand All @@ -77,7 +77,7 @@ func Vote(_disputeId *big.Int, _supportsDispute bool, ctx context.Context) error
addr := ctx.Value(tellorCommon.PublicAddress).(common.Address)
voted, err := instanceGetter.DidVote(nil, _disputeId, addr)
if err != nil {
return errors.Errorf("failed to check if you've already voted: %v", err)
return errors.Wrapf(err, "to check if you've already voted")
}
if voted {
fmt.Printf("You have already voted on this dispute\n")
Expand All @@ -88,11 +88,11 @@ func Vote(_disputeId *big.Int, _supportsDispute bool, ctx context.Context) error

auth, err := PrepareEthTransaction(ctx)
if err != nil {
return errors.Errorf("failed to prepare ethereum transaction: %s", err.Error())
return errors.Wrapf(err, "to prepare ethereum transaction")
}
tx, err := instanceTellor.Vote(auth, _disputeId, _supportsDispute)
if err != nil {
return errors.Errorf("failed to submit vote transaction: %s", err.Error())
return errors.Wrapf(err, "to submit vote transaction")
}

fmt.Printf("Vote submitted with transaction %s\n", tx.Hash().Hex())
Expand All @@ -103,7 +103,7 @@ func getNonceSubmissions(ctx context.Context, valueBlock *big.Int, dispute *tell
instance := ctx.Value(tellorCommon.ContractsGetterContextKey).(*getter.TellorGetters)
tokenAbi, err := abi.JSON(strings.NewReader(tellor.TellorLibraryABI))
if err != nil {
return nil, errors.Errorf("failed to parse abi: %v", err)
return nil, errors.Wrap(err, "to parse abi")
}
contractAddress := ctx.Value(tellorCommon.ContractAddress).(common.Address)
client := ctx.Value(tellorCommon.ClientContextKey).(rpc.ETHClient)
Expand All @@ -113,12 +113,12 @@ func getNonceSubmissions(ctx context.Context, valueBlock *big.Int, dispute *tell

allVals, err := instance.GetSubmissionsByTimestamp(nil, dispute.RequestId, dispute.Timestamp)
if err != nil {
return nil, errors.Errorf("failed to get other submitted values for dispute: %v", err)
return nil, errors.Wrap(err, "to get other submitted values for dispute")
}

allAddrs, err := instance.GetMinersByRequestIdAndTimestamp(nil, dispute.RequestId, dispute.Timestamp)
if err != nil {
return nil, errors.Errorf("failed to get miner addresses for dispute: %v", err)
return nil, errors.Wrap(err, "to get miner addresses for dispute")
}

const blockStep = 100
Expand All @@ -137,18 +137,18 @@ func getNonceSubmissions(ctx context.Context, valueBlock *big.Int, dispute *tell

logs, err := client.FilterLogs(ctx, query)
if err != nil {
return nil, errors.Errorf("failed to get nonce logs: %v", err)
return nil, errors.Wrap(err, "to get nonce logs")
}

for _, l := range logs {
nonceSubmit := tellor.TellorLibraryNonceSubmitted{}
err := bar.UnpackLog(&nonceSubmit, "NonceSubmitted", l)
if err != nil {
return nil, errors.Errorf("failed to unpack into object: %v", err)
return nil, errors.Wrap(err, "to unpack into object")
}
header, err := client.HeaderByNumber(ctx, big.NewInt(int64(l.BlockNumber)))
if err != nil {
return nil, errors.Errorf("failed to get nonce block header: %v", err)
return nil, errors.Wrap(err, "to get nonce block header")
}
for i := 0; i < 5; i++ {
if nonceSubmit.Miner == allAddrs[i] {
Expand Down Expand Up @@ -177,7 +177,7 @@ func List(ctx context.Context, logger log.Logger) error {
cfg := config.GetConfig()
tokenAbi, err := abi.JSON(strings.NewReader(tellor.TellorDisputeABI))
if err != nil {
return errors.Errorf("failed to parse abi: %v", err)
return errors.Wrap(err, "to parse abi")
}
contractAddress := ctx.Value(tellorCommon.ContractAddress).(common.Address)
client := ctx.Value(tellorCommon.ClientContextKey).(rpc.ETHClient)
Expand All @@ -187,7 +187,7 @@ func List(ctx context.Context, logger log.Logger) error {

header, err := client.HeaderByNumber(ctx, nil)
if err != nil {
return errors.Errorf("failed to get latest eth block header: %v", err)
return errors.Wrap(err, "to get latest eth block header")
}

startBlock := big.NewInt(10e3 * 14)
Expand All @@ -202,7 +202,7 @@ func List(ctx context.Context, logger log.Logger) error {

logs, err := client.FilterLogs(ctx, query)
if err != nil {
return errors.Errorf("failed to filter eth logs: %v", err)
return errors.Wrap(err, "to filter eth logs")
}

instance := ctx.Value(tellorCommon.ContractsGetterContextKey).(*getter.TellorGetters)
Expand All @@ -213,11 +213,11 @@ func List(ctx context.Context, logger log.Logger) error {
dispute := tellor.TellorDisputeNewDispute{}
err := bar.UnpackLog(&dispute, "NewDispute", rawDispute)
if err != nil {
return errors.Errorf("failed to unpack dispute event from logs: %v", err)
return errors.Wrap(err, "to unpack dispute event from logs")
}
_, executed, votePassed, _, reportedAddr, reportingMiner, _, uintVars, currTally, err := instance.GetAllDisputeVars(nil, dispute.DisputeId)
if err != nil {
return errors.Errorf("failed to get dispute details: %v", err)
return errors.Wrap(err, "to get dispute details")
}

votingEnds := time.Unix(uintVars[3].Int64(), 0)
Expand Down Expand Up @@ -245,7 +245,7 @@ func List(ctx context.Context, logger log.Logger) error {

allSubmitted, err := getNonceSubmissions(ctx, uintVars[5], &dispute)
if err != nil {
return errors.Errorf("failed to get the values submitted by other miners for the disputed block: %v", err)
return errors.Wrapf(err, "to get the values submitted by other miners for the disputed block")
}
disputedValTime := allSubmitted[uintVars[6].Uint64()].Created

Expand Down
2 changes: 1 addition & 1 deletion pkg/ops/miningManager.go
Expand Up @@ -71,7 +71,7 @@ func CreateMiningManager(

group, err := pow.SetupMiningGroup(cfg)
if err != nil {
return nil, errors.Errorf("failed to setup miners: %s", err.Error())
return nil, errors.Wrap(err, "to setup miners")
}

client, err := rpc.NewClient(cfg.NodeURL)
Expand Down

0 comments on commit 8ef2340

Please sign in to comment.