Skip to content

Commit

Permalink
Add test to check that the hashes are correct. Everything seems to be
Browse files Browse the repository at this point in the history
working now (besides proofs, idk if those are correct)
  • Loading branch information
kcalvinalvin committed Apr 4, 2022
1 parent 024b2cb commit b693068
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 29 deletions.
27 changes: 22 additions & 5 deletions accumulator/forest.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ func (f *Forest) removeSwapless(dels []uint64) error {

sortUint64s(dels)

fmt.Println("dels", dels)
//fmt.Println("dels", dels)
for _, del := range dels {
delete(f.positionMap, f.data.read(del).Mini())
//f.data.write(del, empty)
Expand All @@ -222,7 +222,7 @@ func (f *Forest) removeSwapless(dels []uint64) error {

// Calculate which leaves should go where when the deletion happens.
moveRows := Transform(dels, f.numLeaves, f.rows)
fmt.Println("moves", moveRows)
//fmt.Println("moves", moveRows)

// Go through all the moves from bottom to top.
for i := 0; i < len(moveRows); i++ {
Expand All @@ -242,14 +242,16 @@ func (f *Forest) removeSwapless(dels []uint64) error {
// Calculate which nodes need to be hashed again.
dirtyRows := calcDirtyNodes2(moveRows, f.numLeaves, f.rows)

fmt.Println("dirtyRows", dirtyRows)
//fmt.Println("dirtyRows", dirtyRows)

for _, dirtyRow := range dirtyRows {
for currentRow, dirtyRow := range dirtyRows {
for _, dirtyPos := range dirtyRow {
leftChild := child(dirtyPos, f.rows)
rightChild := rightSib(leftChild)

if f.data.read(leftChild) == empty {
fmt.Println("currentRow", currentRow)

fmt.Println("f.rows", f.rows)
fmt.Println("numleaves", f.numLeaves)
fmt.Println(f.SubTreeToString(leftChild))
Expand All @@ -260,7 +262,8 @@ func (f *Forest) removeSwapless(dels []uint64) error {
}

if f.data.read(rightChild) == empty {
//fmt.Println(f.SubTreeToString(rightChild))
fmt.Println("HERE")
fmt.Println(f.SubTreeToString(rightChild))
//fmt.Println(f.ToString())
return fmt.Errorf("removeSwapless error: couldn't hash dirty "+
"position at %d as the rightChild of %d was empty",
Expand All @@ -269,6 +272,20 @@ func (f *Forest) removeSwapless(dels []uint64) error {

hash := parentHash(f.data.read(leftChild), f.data.read(rightChild))
f.data.write(dirtyPos, hash)

// If the dirty position has a parent, then that is also dirty.
if currentRow < int(logicalTreeRows(f.numLeaves)) &&
!isRootPosition(dirtyPos, f.numLeaves, f.rows) {

parentPos := parent(dirtyPos, f.rows)
parentRow := detectRow(parentPos, f.rows)

// Insert in order.
insertSort(&dirtyRows[parentRow], parentPos)

// If it's already there, remove it.
dirtyRows[parentRow] = removeDuplicateInt(dirtyRows[parentRow])
}
}
}

Expand Down
93 changes: 91 additions & 2 deletions accumulator/forest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,11 @@ func TestForestSwaplessSimple(t *testing.T) {
hex.EncodeToString(hash[:]))
}
}

err = f.checkForestHashes()
if err != nil {
t.Fatalf("TestForestSwaplessSimple Fail: test %d failed. err: %v", i, err)
}
}
}

Expand Down Expand Up @@ -563,8 +568,8 @@ func TestSwaplessModify(t *testing.T) {
expectedEmpty: []uint64{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 14, 15,
16, 17, 18, 19, 24, 25},
expectedRoots: []Hash{
stringToHash("7ccdb5eb659438b7cd85f8c46788ecf0" +
"ed07239f8c8bcdc63733917fd7b64c89"),
stringToHash("4010aedd4e90764f805667487be4b48c" +
"9ad3f81a11a827455cf61ddc55f12f05"),
},
},

Expand Down Expand Up @@ -782,6 +787,10 @@ func TestSwaplessModify(t *testing.T) {
hex.EncodeToString(readHash[:]))
}
}
err = forest.checkForestHashes()
if err != nil {
t.Fatalf("TestSwaplessModify Fail: test %s failed. err: %v", test.name, err)
}
}
}
}
Expand Down Expand Up @@ -1082,6 +1091,11 @@ func TestForestSwaplessAddDel(t *testing.T) {
t.Fatalf("TestSwapLessAddDel fail at block %d. Error: %v", b, err)
}
}

err = f.checkForestHashes()
if err != nil {
t.Fatalf("TestSwapLessAddDel Fail: err: %v", err)
}
}

fmt.Println("swapless allproof", allProof)
Expand Down Expand Up @@ -1527,3 +1541,78 @@ func TestSmallRandomForests(t *testing.T) {
}
}
}

func (f *Forest) checkForestHashes() error {
roots := NewPositionList()
defer roots.Free()

getRootsForwards(f.numLeaves, f.rows, &roots.list)

for _, root := range roots.list {
nodesToCheck := []uint64{root}
topRow := detectRow(root, f.rows)

for row := int(topRow); row >= 0; row-- {
nextNodes := []uint64{}

for _, node := range nodesToCheck {
leftChild := child(node, f.rows)
rightChild := rightSib(leftChild)

leftHash := f.data.read(leftChild)
rightHash := f.data.read(rightChild)

if isRootPosition(node, f.numLeaves, f.rows) {
if row == 0 || leftHash == empty && rightHash == empty {
continue
}
}

//if leftHash == empty || rightHash == empty {
//return fmt.Errorf("One of the children of %d is empty, left %s, right %s",
// node, hex.EncodeToString(leftHash[:]), hex.EncodeToString(rightHash[:]))
//}

calculated := parentHash(leftHash, rightHash)
read := f.data.read(node)

if calculated != read {
return fmt.Errorf("Position %d, calculated %s but read %s", node,
hex.EncodeToString(calculated[:]), hex.EncodeToString(read[:]))
}

if row != 0 {
// Children of the leftChild.
//if f.data.read(child(leftChild, f.rows)) != empty {
//if !f.isLeaf(child(leftChild, f.rows), uint8(row)) {
if !f.isLeaf(leftChild, uint8(row-1)) {
nextNodes = append(nextNodes, leftChild)
//nextNodes = append(nextNodes, child(leftChild, f.rows))
//nextNodes = append(nextNodes, rightSib(child(leftChild, f.rows)))
}
//if !f.isLeaf(rightSib(child(leftChild, f.rows)), uint8(row)) {
// nextNodes = append(nextNodes, rightSib(child(leftChild, f.rows)))
//}

// Children of the rightChild.
//if f.data.read(child(rightChild, f.rows)) != empty {
//if !f.isLeaf(child(rightChild, f.rows), f.rows) {
if !f.isLeaf(rightChild, uint8(row-1)) {
nextNodes = append(nextNodes, rightChild)
//nextNodes = append(nextNodes, child(rightChild, f.rows))
//nextNodes = append(nextNodes, rightSib(child(rightChild, f.rows)))
}
//if f.data.read(rightSib(child(rightChild, f.rows))) != empty {
//if !f.isLeaf(rightSib(child(rightChild, f.rows)), f.rows) {
// nextNodes = append(nextNodes, rightSib(child(rightChild, f.rows)))
//}
}
}

//fmt.Println("llop", nextNodes, nodesToCheck)
nodesToCheck = nextNodes
}
}

return nil
}
42 changes: 20 additions & 22 deletions accumulator/transform.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package accumulator

import (
"fmt"
"sort"
)

Expand All @@ -13,7 +12,7 @@ func Transform(origDels []uint64, numLeaves uint64, forestRows uint8) [][]arrow

deTwin(&dels, forestRows)

fmt.Println("detwined del", dels)
//fmt.Println("detwined del", dels)

// Moves indicate where a leaf should move to next.
moves := make([][]arrow, forestRows+1)
Expand Down Expand Up @@ -150,7 +149,7 @@ func calcDirtyNodes2(moves [][]arrow, numLeaves uint64, forestRows uint8) [][]ui
// Calculate the dirty position.
dirtyPos := parent(move.to, forestRows)

fmt.Println("dirtyPos", dirtyPos)
//fmt.Println("dirtyPos", dirtyPos)

// No dirty positions if the node is moving to a root position.
if isRootPosition(move.to, numLeaves, forestRows) {
Expand All @@ -167,12 +166,12 @@ func calcDirtyNodes2(moves [][]arrow, numLeaves uint64, forestRows uint8) [][]ui
toRow := detectRow(compMove.to, forestRows)

for currentRow := fromRow; currentRow < toRow; currentRow++ {
fmt.Printf("compMove.from %d, compmove row %d, dirtyPos %d\n", compMove.from, currentRow, dirtyPos)
//fmt.Printf("compMove.from %d, compmove row %d, dirtyPos %d\n", compMove.from, currentRow, dirtyPos)

fmt.Println("dirtypos before", dirtyPos)
//fmt.Println("dirtypos before", dirtyPos)
dirtyPos = calcNextPosition(dirtyPos, numLeaves, currentRow, forestRows)

fmt.Println("dirtypos after", dirtyPos)
//fmt.Println("dirtypos after", dirtyPos)
}

//delRow := detectRow(compMove.from, forestRows)
Expand All @@ -184,8 +183,8 @@ func calcDirtyNodes2(moves [][]arrow, numLeaves uint64, forestRows uint8) [][]ui
//fmt.Println("dirtypos after", dirtyPos)
} else {
if dirtyPos == compMove.from {
fmt.Printf("dirtyPos %d same as compMove.from. change to compMove.to %d\n",
dirtyPos, compMove.to)
//fmt.Printf("dirtyPos %d same as compMove.from. change to compMove.to %d\n",
// dirtyPos, compMove.to)
dirtyPos = compMove.to
}
}
Expand All @@ -204,46 +203,46 @@ func calcDirtyNodes2(moves [][]arrow, numLeaves uint64, forestRows uint8) [][]ui
}

func calcNextPosition(position, numLeaves uint64, delRow, forestRows uint8) uint64 {
fmt.Println("calcNextPosition", position)
//fmt.Println("calcNextPosition", position)
returnPos := getRootPosition(position, numLeaves, forestRows)

//subTreeRows := detectSubTreeRows(position, numLeaves, forestRows)
subTreeRows := detectRow(getRootPosition(position, numLeaves, forestRows), forestRows)
fmt.Printf("subTreeRows %d, forestRows %d, numLeaves %d\n", subTreeRows, forestRows, numLeaves)
//fmt.Printf("subTreeRows %d, forestRows %d, numLeaves %d\n", subTreeRows, forestRows, numLeaves)

positionRow := detectRow(position, forestRows)
startRow := int(subTreeRows) - int(positionRow)

origDelRow := delRow
//origDelRow := delRow
delRow = delRow - positionRow
fmt.Printf("delrow before %d, after %d, positionRow %d\n", origDelRow, delRow, positionRow)
//fmt.Printf("delrow before %d, after %d, positionRow %d\n", origDelRow, delRow, positionRow)

if positionRow > 0 {
beforePos := position
//beforePos := position
mask := (1 << uint64(subTreeRows-positionRow)) - uint64(1)
position = position & mask

fmt.Println(position)
//fmt.Println(position)

fmt.Printf("pos from %d to %d with mask %d, subTreeRow %d, positionRow %d\n",
beforePos, position, mask, subTreeRows, positionRow)
//fmt.Printf("pos from %d to %d with mask %d, subTreeRow %d, positionRow %d\n",
// beforePos, position, mask, subTreeRows, positionRow)
}

for i := int(startRow) - 1; i >= 0; i-- {
// Skip the bit field operation for this row.
if i == int(delRow) {
fmt.Println("skipping row", i)
//fmt.Println("skipping row", i)
continue
}
mask := uint64(1 << i)
fmt.Println("mask", mask)
//fmt.Println("mask", mask)

// 1 means right
if (position & mask) == mask {
fmt.Println("right")
//fmt.Println("right")
returnPos = rightChild(returnPos, forestRows)
} else {
fmt.Println("left")
//fmt.Println("left")
returnPos = child(returnPos, forestRows)
}
}
Expand Down Expand Up @@ -279,8 +278,7 @@ func isRootPosition(position, numLeaves uint64, forestRows uint8) bool {
rootPresent := numLeaves&(1<<row) != 0
rootPos := rootPosition(numLeaves, row, forestRows)

fmt.Printf("pos %d, row %d, rootPresent %v, rootPos %d\n",
position, numLeaves, rootPresent, rootPos)
//fmt.Printf("pos %d, row %d, rootPresent %v, rootPos %d\n",position, numLeaves, rootPresent, rootPos)

return rootPresent && rootPos == position
}
Expand Down
9 changes: 9 additions & 0 deletions accumulator/transform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ import (
"golang.org/x/exp/slices"
)

func TestInsertSort(t *testing.T) {
dels := []uint64{1, 2, 2, 3}
insertSort(&dels, 1)

fmt.Println(dels)
dels = removeDuplicateInt(dels)
fmt.Println(dels)
}

func TestExTwin(t *testing.T) {

fmt.Printf("%d\n", rootPosition(15, 0, 4))
Expand Down

0 comments on commit b693068

Please sign in to comment.