Skip to content

Commit

Permalink
chore: use errors.New to replace fmt.Errorf with no parameters
Browse files Browse the repository at this point in the history
Signed-off-by: ChengenH <hce19970702@gmail.com>
  • Loading branch information
ChengenH committed Dec 8, 2024
1 parent a989ed4 commit 0f97ca7
Show file tree
Hide file tree
Showing 11 changed files with 26 additions and 20 deletions.
3 changes: 2 additions & 1 deletion cmd/iaviewer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"os"
"path/filepath"
Expand Down Expand Up @@ -67,7 +68,7 @@ func OpenDB(dir string) (corestore.KVStoreWithBatch, error) {
case strings.HasSuffix(dir, ".db/"):
dir = dir[:len(dir)-4]
default:
return nil, fmt.Errorf("database directory must end with .db")
return nil, errors.New("database directory must end with .db")
}

dir, err := filepath.Abs(dir)
Expand Down
4 changes: 2 additions & 2 deletions mutable_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,7 @@ func (tree *MutableTree) rotateLeft(node *Node) (*Node, error) {
// TODO: optimize balance & rotate
func (tree *MutableTree) balance(node *Node) (newSelf *Node, err error) {
if node.nodeKey != nil {
return nil, fmt.Errorf("unexpected balance() call on persisted node")
return nil, errors.New("unexpected balance() call on persisted node")
}
balance, err := node.calcBalance(tree.ImmutableTree)
if err != nil {
Expand Down Expand Up @@ -1084,7 +1084,7 @@ func (tree *MutableTree) saveNewNodes(version int64) error {
func (tree *MutableTree) SaveChangeSet(cs *ChangeSet) (int64, error) {
// if the tree has uncommitted changes, return error
if tree.root != nil && tree.root.nodeKey == nil {
return 0, fmt.Errorf("cannot save changeset with uncommitted changes")
return 0, errors.New("cannot save changeset with uncommitted changes")
}
for _, pair := range cs.Pairs {
if pair.Delete {
Expand Down
2 changes: 1 addition & 1 deletion nodedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ func (ndb *nodeDB) shouldForceFastStorageUpgrade() (bool, error) {
// saveFastNodeUnlocked saves a FastNode to disk.
func (ndb *nodeDB) saveFastNodeUnlocked(node *fastnode.Node, shouldAddToCache bool) error {
if node.GetKey() == nil {
return fmt.Errorf("cannot have FastNode with a nil value for key")
return errors.New("cannot have FastNode with a nil value for key")
}

// Save node bytes to db.
Expand Down
6 changes: 3 additions & 3 deletions proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ var bufPool = &sync.Pool{

var (
// ErrInvalidProof is returned by Verify when a proof cannot be validated.
ErrInvalidProof = fmt.Errorf("invalid proof")
ErrInvalidProof = errors.New("invalid proof")

// ErrInvalidInputs is returned when the inputs passed to the function are invalid.
ErrInvalidInputs = fmt.Errorf("invalid inputs")
ErrInvalidInputs = errors.New("invalid inputs")

// ErrInvalidRoot is returned when the root passed in does not match the proof's.
ErrInvalidRoot = fmt.Errorf("invalid root")
ErrInvalidRoot = errors.New("invalid root")
)

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

import (
"encoding/binary"
"fmt"

"errors"
ics23 "github.com/cosmos/ics23/go"
)

Expand Down Expand Up @@ -48,7 +47,7 @@ func (t *ImmutableTree) GetNonMembershipProof(key []byte) (*ics23.CommitmentProo
}

if val != nil {
return nil, fmt.Errorf("cannot create NonExistanceProof when Key in State")
return nil, errors.New("cannot create NonExistanceProof when Key in State")
}

nonexist := &ics23.NonExistenceProof{
Expand Down Expand Up @@ -177,7 +176,7 @@ func convertVarIntToBytes(orig int64, buf [binary.MaxVarintLen64]byte) []byte {
// GetProof gets the proof for the given key.
func (t *ImmutableTree) GetProof(key []byte) (*ics23.CommitmentProof, error) {
if t.root == nil {
return nil, fmt.Errorf("cannot generate the proof with nil root")
return nil, errors.New("cannot generate the proof with nil root")
}

exist, err := t.Has(key)
Expand Down
6 changes: 4 additions & 2 deletions v2/export.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package iavl

import "fmt"
import (
"errors"
)

// TraverseOrderType is the type of the order in which the tree is traversed.
type TraverseOrderType uint8
Expand Down Expand Up @@ -98,4 +100,4 @@ func (e *Exporter) Next() (*SnapshotNode, error) {
}
}

var ErrorExportDone = fmt.Errorf("export done")
var ErrorExportDone = errors.New("export done")
3 changes: 2 additions & 1 deletion v2/migrate/v0/migrate_v0.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package v0

import (
"bytes"
"errors"
"fmt"
"sync"

Expand Down Expand Up @@ -111,7 +112,7 @@ func latestVersionCommand() *cobra.Command {
return err
}
if set && version == -1 {
return fmt.Errorf("version must be set")
return errors.New("version must be set")
}
if set {

Expand Down
6 changes: 3 additions & 3 deletions v2/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func (node *Node) right(t *Tree) *Node {
// getLeftNode will never be called on leaf nodes. all tree nodes have 2 children.
func (node *Node) getLeftNode(t *Tree) (*Node, error) {
if node.isLeaf() {
return nil, fmt.Errorf("leaf node has no left node")
return nil, errors.New("leaf node has no left node")
}
if node.leftNode != nil {
return node.leftNode, nil
Expand All @@ -117,7 +117,7 @@ func (node *Node) getLeftNode(t *Tree) (*Node, error) {

func (node *Node) getRightNode(t *Tree) (*Node, error) {
if node.isLeaf() {
return nil, fmt.Errorf("leaf node has no right node")
return nil, errors.New("leaf node has no right node")
}
if node.rightNode != nil {
return node.rightNode, nil
Expand Down Expand Up @@ -158,7 +158,7 @@ func maxInt8(a, b int8) int8 {
// TODO: optimize balance & rotate
func (tree *Tree) balance(node *Node) (newSelf *Node, err error) {
if node.hash != nil {
return nil, fmt.Errorf("unexpected balance() call on persisted node")
return nil, errors.New("unexpected balance() call on persisted node")
}
balance, err := node.calcBalance(tree)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion v2/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package iavl

import (
"bytes"
"errors"
"fmt"
"os"
"strconv"
Expand Down Expand Up @@ -99,7 +100,7 @@ func (opts SqliteDbOptions) EstimateMmapSize() (uint64, error) {
return 0, err
}
if !hasRow {
return 0, fmt.Errorf("no row")
return 0, errors.New("no row")
}
var leafSize int64
err = q.Scan(&leafSize)
Expand Down
3 changes: 2 additions & 1 deletion v2/sqlite_metadata.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package iavl

import (
"errors"
"fmt"
"os"
"sync"
Expand All @@ -19,7 +20,7 @@ type SqliteKVStore struct {

func NewSqliteKVStore(opts SqliteDbOptions) (kv *SqliteKVStore, err error) {
if opts.Path == "" {
return nil, fmt.Errorf("path cannot be empty")
return nil, errors.New("path cannot be empty")
}
if opts.WalSize == 0 {
opts.WalSize = 50 * 1024 * 1024
Expand Down
3 changes: 2 additions & 1 deletion v2/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"crypto/sha256"
"errors"
"fmt"
"os"
"time"
Expand Down Expand Up @@ -102,7 +103,7 @@ func NewTree(sql *SqliteDb, pool *NodePool, opts TreeOptions) *Tree {

func (tree *Tree) LoadVersion(version int64) (err error) {
if tree.sql == nil {
return fmt.Errorf("sql is nil")
return errors.New("sql is nil")
}

tree.workingBytes = 0
Expand Down

0 comments on commit 0f97ca7

Please sign in to comment.