Compare commits

..

3 Commits

Author SHA1 Message Date
Svarog
686c25c72d [NOD-1064] Don't send GetBlockInvsMsg with lowHash = nil (#769) 2020-06-21 08:58:29 +03:00
stasatdaglabs
956b6f7d95 [NOD-900] Fix bad key in Seek (#687)
* [NOD-900] Fix Seek not working at expected.

* [NOD-900] Wrap error messages.

* [NOD-900] Use ldbIterator.Key instead of LevelDBCursor.Key.

* [NOD-900] Add a comment.
2020-04-02 17:47:51 +03:00
stasatdaglabs
c1a039de3f [NOD-900] Fix Seek not working as expected (#686)
* [NOD-900] Fix Seek not working at expected.

* [NOD-900] Wrap error messages.
2020-04-02 17:05:58 +03:00
4 changed files with 33 additions and 12 deletions

View File

@@ -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

View File

@@ -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,30 @@ 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
notFoundErr := errors.Wrapf(database.ErrNotFound, "key %s not "+
"found", hex.EncodeToString(key))
found := c.ldbIterator.Seek(key)
if !found {
return notFoundErr
}
// Use c.ldbIterator.Key because c.Key removes the prefix from the key
currentKey := c.ldbIterator.Key()
if currentKey == nil {
return notFoundErr
}
if !bytes.Equal(currentKey, key) {
return notFoundErr
}
return nil
}
// Key returns the key of the current key/value pair, or ErrNotFound if done.

View File

@@ -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
}

View File

@@ -607,7 +607,7 @@ func TestOutboundPeer(t *testing.T) {
t.Errorf("PushAddrMsg: unexpected err %v\n", err)
return
}
if err := p2.PushGetBlockInvsMsg(nil, &daghash.Hash{}); err != nil {
if err := p2.PushGetBlockInvsMsg(&daghash.Hash{}, &daghash.Hash{}); err != nil {
t.Errorf("PushGetBlockInvsMsg: unexpected err %v\n", err)
return
}