Skip to content

Commit

Permalink
Merge pull request #199 from kcalvinalvin/2024-09-02-bridge-bug-fix
Browse files Browse the repository at this point in the history
blockchain: fix bridge node bug when enabling -utreexoproofindexmaxmemory=-1
  • Loading branch information
kcalvinalvin authored Sep 2, 2024
2 parents 3235c70 + d63b75d commit 87a8b19
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
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

0 comments on commit 87a8b19

Please sign in to comment.