mirror of
https://github.com/kaspanet/kaspad.git
synced 2026-02-27 13:43:19 +00:00
Compare commits
3 Commits
v0.3.0-rc1
...
v0.3.0-dev
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
686c25c72d | ||
|
|
956b6f7d95 | ||
|
|
c1a039de3f |
@@ -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
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user