From 60e221f372497906ca22b80b30389fb159bb859c Mon Sep 17 00:00:00 2001 From: Daniel T <30197399+danwt@users.noreply.github.com> Date: Wed, 22 May 2024 08:02:34 +0100 Subject: [PATCH] fix(code standards): use gerr err not found (#878) --- block/produce.go | 4 +++- indexers/blockindexer/kv/kv.go | 4 +++- store/badger.go | 7 +++---- store/badger_test.go | 4 +++- store/store_test.go | 4 +++- 5 files changed, 15 insertions(+), 8 deletions(-) diff --git a/block/produce.go b/block/produce.go index cdec66517..3b5962204 100644 --- a/block/produce.go +++ b/block/produce.go @@ -6,6 +6,8 @@ import ( "fmt" "time" + "github.com/dymensionxyz/dymint/gerr" + "github.com/dymensionxyz/dymint/store" "github.com/dymensionxyz/dymint/types" @@ -123,7 +125,7 @@ func (m *Manager) produceBlock(allowEmpty bool) (*types.Block, *types.Commit, er return nil, nil, fmt.Errorf("load commit after load block: height: %d: %w: %w", newHeight, err, ErrNonRecoverable) } m.logger.Info("Using pending block.", "height", newHeight) - } else if !errors.Is(err, store.ErrKeyNotFound) { + } else if !errors.Is(err, gerr.ErrNotFound) { return nil, nil, fmt.Errorf("load block: height: %d: %w: %w", newHeight, err, ErrNonRecoverable) } else { block = m.Executor.CreateBlock(newHeight, lastCommit, lastHeaderHash, m.State, m.Conf.BlockBatchMaxSizeBytes) diff --git a/indexers/blockindexer/kv/kv.go b/indexers/blockindexer/kv/kv.go index 6e771d772..9e42dba12 100644 --- a/indexers/blockindexer/kv/kv.go +++ b/indexers/blockindexer/kv/kv.go @@ -8,6 +8,8 @@ import ( "strconv" "strings" + "github.com/dymensionxyz/dymint/gerr" + "github.com/google/orderedcode" abci "github.com/tendermint/tendermint/abci/types" @@ -43,7 +45,7 @@ func (idx *BlockerIndexer) Has(height int64) (bool, error) { } _, err = idx.store.Get(key) - if errors.Is(err, store.ErrKeyNotFound) { + if errors.Is(err, gerr.ErrNotFound) { return false, nil } return err == nil, err diff --git a/store/badger.go b/store/badger.go index 1589ea03d..596b2e3c1 100644 --- a/store/badger.go +++ b/store/badger.go @@ -3,6 +3,8 @@ package store import ( "errors" + "github.com/dymensionxyz/dymint/gerr" + "github.com/dgraph-io/badger/v3" ) @@ -11,9 +13,6 @@ var ( _ KVBatch = &BadgerBatch{} ) -// ErrKeyNotFound is returned if key is not found in KVStore. -var ErrKeyNotFound = errors.New("key not found") - // BadgerKV is a implementation of KVStore using Badger v3. type BadgerKV struct { db *badger.DB @@ -25,7 +24,7 @@ func (b *BadgerKV) Get(key []byte) ([]byte, error) { defer txn.Discard() item, err := txn.Get(key) if errors.Is(err, badger.ErrKeyNotFound) { - return nil, ErrKeyNotFound + return nil, gerr.ErrNotFound } if err != nil { return nil, err diff --git a/store/badger_test.go b/store/badger_test.go index c2be027f9..3f77a7cee 100644 --- a/store/badger_test.go +++ b/store/badger_test.go @@ -4,6 +4,8 @@ import ( "errors" "testing" + "github.com/dymensionxyz/dymint/gerr" + "github.com/dgraph-io/badger/v3" ) @@ -16,7 +18,7 @@ func TestGetErrors(t *testing.T) { err error }{ {"empty key", []byte{}, badger.ErrEmptyKey}, - {"not found key", []byte("missing key"), ErrKeyNotFound}, + {"not found key", []byte("missing key"), gerr.ErrNotFound}, } for _, tt := range tc { diff --git a/store/store_test.go b/store/store_test.go index 8176df30d..bac1b47ce 100644 --- a/store/store_test.go +++ b/store/store_test.go @@ -4,6 +4,8 @@ import ( "os" "testing" + "github.com/dymensionxyz/dymint/gerr" + abcitypes "github.com/tendermint/tendermint/abci/types" tmstate "github.com/tendermint/tendermint/proto/tendermint/state" @@ -186,7 +188,7 @@ func TestBatch(t *testing.T) { assert.NoError(err) resp, err := s.LoadBlockResponses(1) - assert.EqualError(err, "retrieve block results from height 1: key not found") // TODO: use errors.Is + assert.Error(err, gerr.ErrNotFound) assert.Nil(resp) err = batch.Commit()