mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 10:52:25 +00:00
les, core/state/snapshot: iterator fixes
This commit is contained in:
parent
9779087655
commit
573f62f0b0
4 changed files with 38 additions and 45 deletions
|
|
@ -19,6 +19,8 @@ package rawdb
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/ethereum/go-ethereum/ethdb"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestTableDatabase(t *testing.T) { testTableDatabase(t, "prefix") }
|
func TestTableDatabase(t *testing.T) { testTableDatabase(t, "prefix") }
|
||||||
|
|
@ -96,49 +98,31 @@ func testTableDatabase(t *testing.T, prefix string) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
check := func(iter ethdb.Iterator, expCount, index int) {
|
||||||
|
count := 0
|
||||||
|
for iter.Next() {
|
||||||
|
key, value := iter.Key(), iter.Value()
|
||||||
|
if !bytes.Equal(key, entries[index].key) {
|
||||||
|
t.Fatalf("Key mismatch: want=%v, got=%v", entries[index].key, key)
|
||||||
|
}
|
||||||
|
if !bytes.Equal(value, entries[index].value) {
|
||||||
|
t.Fatalf("Value mismatch: want=%v, got=%v", entries[index].value, value)
|
||||||
|
}
|
||||||
|
index += 1
|
||||||
|
count++
|
||||||
|
}
|
||||||
|
if count != expCount {
|
||||||
|
t.Fatalf("Wrong number of elems, exp %d got %d", expCount, count)
|
||||||
|
}
|
||||||
|
iter.Release()
|
||||||
|
}
|
||||||
// Test iterators
|
// Test iterators
|
||||||
iter := db.NewIterator()
|
check(db.NewIterator(), 6, 0)
|
||||||
var index int
|
|
||||||
for iter.Next() {
|
|
||||||
key, value := iter.Key(), iter.Value()
|
|
||||||
if !bytes.Equal(key, entries[index].key) {
|
|
||||||
t.Fatalf("Key mismatch: want=%v, got=%v", entries[index].key, key)
|
|
||||||
}
|
|
||||||
if !bytes.Equal(value, entries[index].value) {
|
|
||||||
t.Fatalf("Value mismatch: want=%v, got=%v", entries[index].value, value)
|
|
||||||
}
|
|
||||||
index += 1
|
|
||||||
}
|
|
||||||
iter.Release()
|
|
||||||
|
|
||||||
// Test iterators with prefix
|
// Test iterators with prefix
|
||||||
iter = db.NewIteratorWithPrefix([]byte{0xff, 0xff})
|
check(db.NewIteratorWith([]byte{0xff, 0xff}, nil), 3, 3)
|
||||||
index = 3
|
|
||||||
for iter.Next() {
|
|
||||||
key, value := iter.Key(), iter.Value()
|
|
||||||
if !bytes.Equal(key, entries[index].key) {
|
|
||||||
t.Fatalf("Key mismatch: want=%v, got=%v", entries[index].key, key)
|
|
||||||
}
|
|
||||||
if !bytes.Equal(value, entries[index].value) {
|
|
||||||
t.Fatalf("Value mismatch: want=%v, got=%v", entries[index].value, value)
|
|
||||||
}
|
|
||||||
index += 1
|
|
||||||
}
|
|
||||||
iter.Release()
|
|
||||||
|
|
||||||
// Test iterators with start point
|
// Test iterators with start point
|
||||||
//iter = db.NewIteratorWithStart([]byte{0xff, 0xff, 0x02})
|
check(db.NewIteratorWith(nil, []byte{0xff, 0xff, 0x02}), 2, 4)
|
||||||
iter = db.NewIteratorWith(nil,[]byte{0xff, 0xff, 0x02})
|
// Test iterators with prefix and start point
|
||||||
index = 4
|
check(db.NewIteratorWith([]byte{0xee}, nil), 0, 0)
|
||||||
for iter.Next() {
|
check(db.NewIteratorWith(nil, []byte{0x00}), 6, 0)
|
||||||
key, value := iter.Key(), iter.Value()
|
|
||||||
if !bytes.Equal(key, entries[index].key) {
|
|
||||||
t.Fatalf("Key mismatch: want=%v, got=%v", entries[index].key, key)
|
|
||||||
}
|
|
||||||
if !bytes.Equal(value, entries[index].value) {
|
|
||||||
t.Fatalf("Value mismatch: want=%v, got=%v", entries[index].value, value)
|
|
||||||
}
|
|
||||||
index += 1
|
|
||||||
}
|
|
||||||
iter.Release()
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -113,7 +113,7 @@ func wipeKeyRange(db ethdb.KeyValueStore, kind string, prefix []byte, keylen int
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
batch.Reset()
|
batch.Reset()
|
||||||
seekPos := bytes.Trim(key, prefix)
|
seekPos := key[len(prefix):]
|
||||||
it = db.NewIteratorWith(prefix, seekPos)
|
it = db.NewIteratorWith(prefix, seekPos)
|
||||||
|
|
||||||
if time.Since(logged) > 8*time.Second {
|
if time.Since(logged) > 8*time.Second {
|
||||||
|
|
|
||||||
|
|
@ -691,6 +691,14 @@ func (db *nodeDB) close() {
|
||||||
close(db.closeCh)
|
close(db.closeCh)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (db *nodeDB) getPrefix(neg bool) []byte {
|
||||||
|
prefix := positiveBalancePrefix
|
||||||
|
if neg {
|
||||||
|
prefix = negativeBalancePrefix
|
||||||
|
}
|
||||||
|
return append(db.verbuf[:], prefix...)
|
||||||
|
}
|
||||||
|
|
||||||
func (db *nodeDB) key(id []byte, neg bool) []byte {
|
func (db *nodeDB) key(id []byte, neg bool) []byte {
|
||||||
prefix := positiveBalancePrefix
|
prefix := positiveBalancePrefix
|
||||||
if neg {
|
if neg {
|
||||||
|
|
@ -761,7 +769,8 @@ func (db *nodeDB) getPosBalanceIDs(start, stop enode.ID, maxCount int) (result [
|
||||||
if maxCount <= 0 {
|
if maxCount <= 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
it := db.db.NewIteratorWithStart(db.key(start.Bytes(), false))
|
prefix := db.getPrefix(false)
|
||||||
|
it := db.db.NewIteratorWith(prefix, start.Bytes())
|
||||||
defer it.Release()
|
defer it.Release()
|
||||||
for i := len(stop[:]) - 1; i >= 0; i-- {
|
for i := len(stop[:]) - 1; i >= 0; i-- {
|
||||||
stop[i]--
|
stop[i]--
|
||||||
|
|
|
||||||
|
|
@ -116,7 +116,7 @@ func (b *SyncBloom) init(database ethdb.Iteratee) {
|
||||||
key := common.CopyBytes(it.Key())
|
key := common.CopyBytes(it.Key())
|
||||||
|
|
||||||
it.Release()
|
it.Release()
|
||||||
it = database.NewIteratorWithStart(key)
|
it = database.NewIteratorWith(nil, key)
|
||||||
|
|
||||||
log.Info("Initializing fast sync bloom", "items", b.bloom.N(), "errorrate", b.errorRate(), "elapsed", common.PrettyDuration(time.Since(start)))
|
log.Info("Initializing fast sync bloom", "items", b.bloom.N(), "errorrate", b.errorRate(), "elapsed", common.PrettyDuration(time.Since(start)))
|
||||||
swap = time.Now()
|
swap = time.Now()
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue