Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

blockchain: fix bridge node bug when enabling -utreexoproofindexmaxmemory=-1 #199

Merged
merged 2 commits into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions blockchain/utreexoio.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,13 @@ func (m *NodesBackEnd) Length() int {

iter := m.db.NewIterator(nil, nil)
for iter.Next() {
// If the itered key is chainhash.HashSize, it means that the entry is for nodesbackend.
// The relevant key-value pairs for nodesbackend are leafLength.
// Skip it since it's not relevant here.
if len(iter.Key()) == 32 {
value := iter.Value()
if len(value) != leafLength {
continue
}

k, _ := deserializeVLQ(iter.Key())
val, found := m.cache.Get(k)
if found && val.IsRemoved() {
Expand Down Expand Up @@ -214,11 +216,13 @@ func (m *NodesBackEnd) ForEach(fn func(uint64, utreexo.Leaf) error) error {

iter := m.db.NewIterator(nil, nil)
for iter.Next() {
// If the itered key is chainhash.HashSize, it means that the entry is for nodesbackend.
// The relevant key-value pairs for nodesbackend are leafLength.
// Skip it since it's not relevant here.
if len(iter.Key()) == 32 {
value := iter.Value()
if len(value) != leafLength {
continue
}

// Remember that the contents of the returned slice should not be modified, and
// only valid until the next call to Next.
k, _ := deserializeVLQ(iter.Key())
Expand All @@ -228,13 +232,7 @@ func (m *NodesBackEnd) ForEach(fn func(uint64, utreexo.Leaf) error) error {
continue
}

value := iter.Value()
if len(value) != leafLength {
return fmt.Errorf("expected value of length %v but got %v",
leafLength, len(value))
}
v := deserializeLeaf(*(*[leafLength]byte)(value))

err := fn(k, v)
if err != nil {
return err
Expand Down Expand Up @@ -377,7 +375,7 @@ func (m *CachedLeavesBackEnd) Length() int {
})
iter := m.db.NewIterator(nil, nil)
for iter.Next() {
// If the itered key is chainhash.HashSize, it means that the entry is for nodesbackend.
// If the itered key is not chainhash.HashSize, it's not for cachedLeavesBackend.
// Skip it since it's not relevant here.
if len(iter.Key()) != chainhash.HashSize {
continue
Expand Down Expand Up @@ -408,7 +406,7 @@ func (m *CachedLeavesBackEnd) ForEach(fn func(utreexo.Hash, uint64) error) error
})
iter := m.db.NewIterator(nil, nil)
for iter.Next() {
// If the itered key isn't chainhash.HashSize, it means that the entry is for nodesbackend.
// If the itered key is not chainhash.HashSize, it's not for cachedLeavesBackend.
// Skip it since it's not relevant here.
if len(iter.Key()) != chainhash.HashSize {
continue
Expand Down
9 changes: 9 additions & 0 deletions blockchain/utreexoio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ func TestCachedLeavesBackEnd(t *testing.T) {
if err != nil {
t.Fatal(err)
}
err = ldbTx.Put([]byte("utreexostateconsistency"), make([]byte, 40), nil)
if err != nil {
t.Fatal(err)
}
// Close and reopen the backend.
cachedLeavesBackEnd.Flush(ldbTx)
err = ldbTx.Commit()
Expand Down Expand Up @@ -192,6 +196,11 @@ func TestNodesBackEnd(t *testing.T) {
if err != nil {
t.Fatal(err)
}

err = ldbTx.Put([]byte("utreexostateconsistency"), make([]byte, 40), nil)
if err != nil {
t.Fatal(err)
}
// Close and reopen the backend.
nodesBackEnd.Flush(ldbTx)
err = ldbTx.Commit()
Expand Down
Loading