diff --git a/database/cursor.go b/database/cursor.go index 2d5911292..d7fc1af4c 100644 --- a/database/cursor.go +++ b/database/cursor.go @@ -11,8 +11,9 @@ type Cursor interface { First() bool // Seek moves the iterator to the first key/value pair whose key is greater - // than or equal to the given key. It returns whether such pair exist. - Seek(key []byte) (bool, error) + // than or equal to the given key. It returns ErrNotFound if such pair does not + // exist. + Seek(key []byte) error // Key returns the key of the current key/value pair, or ErrNotFound if done. // Note that the key is trimmed to not include the prefix the cursor was opened diff --git a/database/ffldb/ldb/cursor.go b/database/ffldb/ldb/cursor.go index 73a90fc89..43118a8c0 100644 --- a/database/ffldb/ldb/cursor.go +++ b/database/ffldb/ldb/cursor.go @@ -2,6 +2,7 @@ package ldb import ( "bytes" + "encoding/hex" "github.com/kaspanet/kaspad/database" "github.com/pkg/errors" "github.com/syndtr/goleveldb/leveldb/iterator" @@ -45,12 +46,28 @@ func (c *LevelDBCursor) First() bool { } // Seek moves the iterator to the first key/value pair whose key is greater -// than or equal to the given key. It returns whether such pair exist. -func (c *LevelDBCursor) Seek(key []byte) (bool, error) { +// than or equal to the given key. It returns ErrNotFound if such pair does not +// exist. +func (c *LevelDBCursor) Seek(key []byte) error { if c.isClosed { - return false, errors.New("cannot seek a closed cursor") + return errors.New("cannot seek a closed cursor") } - return c.ldbIterator.Seek(key), nil + + found := c.ldbIterator.Seek(key) + if !found { + return errors.Wrapf(database.ErrNotFound, "key %s not "+ + "found", hex.EncodeToString(key)) + } + currentKey, err := c.Key() + if err != nil { + return err + } + if !bytes.Equal(currentKey, key) { + return errors.Wrapf(database.ErrNotFound, "key %s not "+ + "found", hex.EncodeToString(key)) + } + + return nil } // Key returns the key of the current key/value pair, or ErrNotFound if done. diff --git a/dbaccess/blockindex.go b/dbaccess/blockindex.go index 9f8dde79a..213b7c208 100644 --- a/dbaccess/blockindex.go +++ b/dbaccess/blockindex.go @@ -43,15 +43,16 @@ func BlockIndexCursorFrom(context Context, blockIndexKey []byte) (database.Curso return nil, err } - found, err := cursor.Seek(blockIndexKey) - if err != nil { - return nil, err - } - if !found { + key := blockIndexBucket.Key(blockIndexKey) + err = cursor.Seek(key) + if IsNotFoundError(err) { cursor.Close() return nil, errors.Wrapf(database.ErrNotFound, "entry not found for %s", hex.EncodeToString(blockIndexKey)) } + if err != nil { + return nil, err + } return cursor, nil }