Skip to content

Commit a68dcb0

Browse files
authored
Merge pull request #1 from kcalvinalvin/master
Updates to the accumulator
2 parents 95f3f30 + d3ac739 commit a68dcb0

File tree

4 files changed

+182
-199
lines changed

4 files changed

+182
-199
lines changed

accumulator.go

+21-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package utreexo
22

33
import (
4+
"encoding/hex"
45
"fmt"
56
"sort"
67

@@ -54,13 +55,21 @@ func NewAccumulator(full bool) Pollard {
5455
//
5556
// NOTE Modify does NOT do any validation and assumes that all the positions of the leaves
5657
// being deleted have already been verified.
57-
func (p *Pollard) Modify(adds []Leaf, delHashes []Hash, dels []uint64) error {
58+
func (p *Pollard) Modify(adds []Leaf, delHashes []Hash, origDels []uint64) error {
59+
// Make a copy to avoid mutating the deletion slice passed in.
60+
delCount := len(origDels)
61+
dels := make([]uint64, delCount)
62+
copy(dels, origDels)
63+
64+
// Remove the delHashes from the map.
5865
p.deleteFromMap(delHashes)
66+
67+
// Perform the deletion. It's important that this must happen before the addition.
5968
err := p.remove(dels)
6069
if err != nil {
6170
return err
6271
}
63-
p.numDels += uint64(len(dels))
72+
p.numDels += uint64(delCount)
6473

6574
p.add(adds)
6675

@@ -316,11 +325,17 @@ func (p *Pollard) Undo(numAdds uint64, dels []uint64, delHashes []Hash) error {
316325

317326
// undoEmptyRoots places empty roots back in after undoing the additions.
318327
func (p *Pollard) undoEmptyRoots(numAdds uint64, origDels []uint64) error {
328+
if len(p.roots) >= int(numRoots(p.numLeaves)) {
329+
return nil
330+
}
319331
dels := make([]uint64, len(origDels))
320332
copy(dels, origDels)
321333

334+
// Sort before detwining.
335+
slices.Sort(dels)
322336
dels = deTwin(dels, treeRows(p.numLeaves))
323-
for _, del := range dels {
337+
for i := len(dels) - 1; i >= 0; i-- {
338+
del := dels[i]
324339
if isRootPosition(del, p.numLeaves, treeRows(p.numLeaves)) {
325340
tree, _, _, err := detectOffset(del, p.numLeaves)
326341
if err != nil {
@@ -370,7 +385,7 @@ func (p *Pollard) undoSingleAdd() {
370385

371386
func (p *Pollard) undoDels(dels []uint64, delHashes []Hash) error {
372387
if len(dels) != len(delHashes) {
373-
return fmt.Errorf("Got %d dels but %d delHashes",
388+
return fmt.Errorf("Got %d targets to be deleted but have %d hashes",
374389
len(dels), len(delHashes))
375390
}
376391

@@ -416,7 +431,8 @@ func (p *Pollard) undoSingleDel(node *polNode, pos uint64) error {
416431
siblingPos := parent(pos, totalRows)
417432
sibling, aunt, _, err := p.getNode(siblingPos)
418433
if err != nil {
419-
return err
434+
return fmt.Errorf("Couldn't undo %s at position %d, err: %v",
435+
hex.EncodeToString(node.data[:]), pos, err)
420436
}
421437

422438
pHash := calculateParentHash(pos, node, sibling)

0 commit comments

Comments
 (0)