Skip to content

Commit

Permalink
Add ErrKeyNotFound (cosmos#148)
Browse files Browse the repository at this point in the history
* add new ErrKeyNotFound and handle it
  • Loading branch information
pmareke authored Oct 14, 2021
1 parent eea0a82 commit 7d2f4ae
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 2 deletions.
6 changes: 5 additions & 1 deletion da/mock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package mock

import (
"encoding/binary"
"errors"

"github.com/celestiaorg/optimint/da"
"github.com/celestiaorg/optimint/log"
Expand Down Expand Up @@ -71,9 +72,12 @@ func (m *MockDataAvailabilityLayerClient) SubmitBlock(block *types.Block) da.Res
func (m *MockDataAvailabilityLayerClient) CheckBlockAvailability(header *types.Header) da.ResultCheckBlock {
hash := header.Hash()
_, err := m.dalcKV.Get(hash[:])
if err != nil {
if errors.Is(err, store.ErrKeyNotFound) {
return da.ResultCheckBlock{DAResult: da.DAResult{Code: da.StatusSuccess}, DataAvailable: false}
}
if err != nil {
return da.ResultCheckBlock{DAResult: da.DAResult{Code: da.StatusError, Message: err.Error()}, DataAvailable: false}
}
return da.ResultCheckBlock{DAResult: da.DAResult{Code: da.StatusSuccess}, DataAvailable: true}
}

Expand Down
14 changes: 13 additions & 1 deletion store/badger.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
package store

import "github.com/dgraph-io/badger/v3"
import (
"errors"

"github.com/dgraph-io/badger/v3"
)

var _ KVStore = &BadgerKV{}

var (
// ErrKeyNotFound is returned if key is not found in KVStore.
ErrKeyNotFound = errors.New("key not found")
)

// BadgerKV is a implementation of KVStore using Badger v3.
type BadgerKV struct {
db *badger.DB
Expand All @@ -14,6 +23,9 @@ func (b *BadgerKV) Get(key []byte) ([]byte, error) {
txn := b.db.NewTransaction(false)
defer txn.Discard()
item, err := txn.Get(key)
if errors.Is(err, badger.ErrKeyNotFound) {
return nil, ErrKeyNotFound
}
if err != nil {
return nil, err
}
Expand Down
75 changes: 75 additions & 0 deletions store/badger_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package store

import (
"errors"
"testing"

"github.com/dgraph-io/badger/v3"
)

func TestGetErrors(t *testing.T) {
dalcKV := NewInMemoryKVStore()

tc := []struct {
name string
key []byte
err error
}{
{"empty key", []byte{}, badger.ErrEmptyKey},
{"not found key", []byte("missing key"), ErrKeyNotFound},
}

for _, tt := range tc {
t.Run(tt.name, func(t *testing.T) {
_, err := dalcKV.Get(tt.key)
if !errors.Is(err, tt.err) {
t.Errorf("Invalid err, got: %v expected %v", err, tt.err)
}
})
}
}

func TestSetErrors(t *testing.T) {
dalcKV := NewInMemoryKVStore()

tc := []struct {
name string
key []byte
value []byte
err error
}{
{"empty key", []byte{}, []byte{}, badger.ErrEmptyKey},
{"invalid key", []byte("!badger!key"), []byte("invalid header"), badger.ErrInvalidKey},
}

for _, tt := range tc {
t.Run(tt.name, func(t *testing.T) {
err := dalcKV.Set(tt.key, tt.value)
if !errors.Is(tt.err, err) {
t.Errorf("Invalid err, got: %v expected %v", err, tt.err)
}
})
}
}

func TestDeleteErrors(t *testing.T) {
dalcKV := NewInMemoryKVStore()

tc := []struct {
name string
key []byte
err error
}{
{"empty key", []byte{}, badger.ErrEmptyKey},
{"invalid key", []byte("!badger!key"), badger.ErrInvalidKey},
}

for _, tt := range tc {
t.Run(tt.name, func(t *testing.T) {
err := dalcKV.Delete(tt.key)
if !errors.Is(err, tt.err) {
t.Errorf("Invalid err, got: %v expected %v", err, tt.err)
}
})
}
}

0 comments on commit 7d2f4ae

Please sign in to comment.