Skip to content

Commit

Permalink
slim down tests
Browse files Browse the repository at this point in the history
  • Loading branch information
adiabat committed Nov 14, 2020
1 parent 0d9a417 commit 03a1fb9
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 79 deletions.
49 changes: 4 additions & 45 deletions accumulator/batchproof_test.go
Original file line number Diff line number Diff line change
@@ -1,51 +1,9 @@
package accumulator

import (
"fmt"
"testing"
)

// TestIncompleteBatchProof tests that a incomplete (missing some hashes) batchproof does not pass verification.
func TestIncompleteBatchProof(t *testing.T) {
// Create forest in memory
f := NewForest(nil, false, "", 0)

// last index to be deleted. Same as blockDels
lastIdx := uint64(7)

// Generate adds
adds := make([]Leaf, 8)
adds[0].Hash = Hash{1}
adds[1].Hash = Hash{2}
adds[2].Hash = Hash{3}
adds[3].Hash = Hash{4}
adds[4].Hash = Hash{5}
adds[5].Hash = Hash{6}
adds[6].Hash = Hash{7}
adds[7].Hash = Hash{8}

// Modify with the additions to simulate txos being added
_, err := f.Modify(adds, nil)
if err != nil {
t.Fatal(err)
}

// create blockProof based on the last add in the slice
blockProof, err := f.ProveBatch(
[]Hash{adds[lastIdx].Hash})

if err != nil {
t.Fatal(err)
}

blockProof.Proof = blockProof.Proof[:len(blockProof.Proof)-1]
shouldBeFalse := f.VerifyBatchProof(blockProof)
if shouldBeFalse != false {
t.Fail()
t.Logf("Incomplete proof passes verification")
}
}
// forests don't accept or verify proofs since they have everything.
// TODO rewrite these tests using pollard instead of forest

/*
// TestVerifyBlockProof tests that the computedTop is compared to the top in the
// Utreexo forest.
func TestVerifyBatchProof(t *testing.T) {
Expand Down Expand Up @@ -147,3 +105,4 @@ func TestProofShouldNotValidateAfterNodeDeleted(t *testing.T) {
proofIndex))
}
}
*/
36 changes: 16 additions & 20 deletions accumulator/forest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TestForestAddDel(t *testing.T) {
}

func TestCowForestAddDelComp(t *testing.T) {
numAdds := uint32(1000)
numAdds := uint32(100)

tmpDir := os.TempDir()
cowF := NewForest(nil, false, tmpDir, 500)
Expand All @@ -61,7 +61,7 @@ func TestCowForestAddDelComp(t *testing.T) {
sc := NewSimChain(0x07)
sc.lookahead = 400

for b := 0; b <= 1000; b++ {
for b := 0; b <= 100; b++ {
adds, _, delHashes := sc.NextBlock(numAdds)

cowBP, err := cowF.ProveBatch(delHashes)
Expand Down Expand Up @@ -89,7 +89,8 @@ func TestCowForestAddDelComp(t *testing.T) {
if err != nil {
panic(err)
}
cowstring := fmt.Sprintf("nl %d %s\n", cowF.numLeaves, cowF.ToString())
cowstring :=
fmt.Sprintf("nl %d %s\n", cowF.numLeaves, cowF.ToString())
cowFile.WriteString(cowstring)

memFile, err := os.OpenFile("memlog",
Expand All @@ -98,7 +99,8 @@ func TestCowForestAddDelComp(t *testing.T) {
panic(err)
}

memstring := fmt.Sprintf("nl %d %s\n", memF.numLeaves, memF.ToString())
memstring :=
fmt.Sprintf("nl %d %s\n", memF.numLeaves, memF.ToString())
memFile.WriteString(memstring)
s := fmt.Sprintf("forests are not equal\n")
s += fmt.Sprintf("forestRows in f: %d\n: ", cowF.rows)
Expand Down Expand Up @@ -134,8 +136,8 @@ func TestCowForestAddDelComp(t *testing.T) {
}
}

// checkIfEqual checks if the forest differ returns true for equal and if not, returns
// the positions and the hashes
// checkIfEqual checks if the forest differ returns true for equal and if not,
// returns the positions and the hashes
func checkIfEqual(cowF, memF *Forest) (bool, []uint64, []Hash) {
cowFH := cowF.rows
memFH := memF.rows
Expand All @@ -159,7 +161,8 @@ func checkIfEqual(cowF, memF *Forest) (bool, []uint64, []Hash) {
memH := memF.data.read(uint64(pos))
cowH := cowF.data.read(uint64(pos))
if memH != cowH {
s := fmt.Sprintf("hashes aren't equal at gpos: %d "+"mem: %x cow: %x\n", pos, memH, cowH)
s := fmt.Sprintf("hashes aren't equal at gpos: %d "+
"mem: %x cow: %x\n", pos, memH, cowH)
panic(s)
}
}
Expand Down Expand Up @@ -193,7 +196,7 @@ func TestCowForestAddDel(t *testing.T) {
t.Fatal(err)
}

fmt.Printf("nl %d %s\n", cowF.numLeaves, cowF.ToString())
// fmt.Printf("nl %d %s\n", cowF.numLeaves, cowF.ToString())
}
}

Expand Down Expand Up @@ -328,18 +331,13 @@ func addDelFullBatchProof(nAdds, nDels int) error {
}

// get block proof
bp, err := f.ProveBatch(addHashes[:nDels])
_, err = f.ProveBatch(addHashes[:nDels])
if err != nil {
return err
}
// check block proof. Note this doesn't delete anything, just proves inclusion
worked, _, _ := verifyBatchProof(bp, f.getRoots(), f.numLeaves, nil)
// worked := f.VerifyBatchProof(bp)

if !worked {
return fmt.Errorf("VerifyBatchProof failed")
}
fmt.Printf("VerifyBatchProof worked\n")
// TODO: make a pollard so that the proof can be verified

return nil
}

Expand Down Expand Up @@ -434,17 +432,15 @@ func TestSmallRandomForests(t *testing.T) {
// If the tree we filled isn't empty, and contains a node we didn't delete,
// we should be able to make a proof for that leaf
if atLeastOneLeafRemains {
blockProof, err := f.ProveBatch(
_, err := f.ProveBatch(
[]Hash{
chosenUndeletedLeaf.Hash,
})
if err != nil {
t.Fatalf("proveblock failed proving existing leaf: %v", err)
}

if !(f.VerifyBatchProof(blockProof)) {
t.Fatal("verifyblockproof failed verifying proof for existing leaf")
}
// TODO verify created proof
}
}
}
26 changes: 13 additions & 13 deletions accumulator/pollard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func TestPollardRand(t *testing.T) {
for z := 0; z < 30; z++ {
for z := 0; z < 10; z++ {
// z := 11221
// z := 55
rand.Seed(int64(z))
Expand Down Expand Up @@ -78,8 +78,8 @@ func pollardRandomRemember(blocks int32) error {
for b := int32(0); b < blocks; b++ {
adds, _, delHashes := sn.NextBlock(rand.Uint32() & 0xff)

fmt.Printf("\t\t\tstart block %d del %d add %d - %s\n",
sn.blockHeight, len(delHashes), len(adds), p.Stats())
// fmt.Printf("\t\t\tstart block %d del %d add %d - %s\n",
// sn.blockHeight, len(delHashes), len(adds), p.Stats())

// get proof for these deletions (with respect to prev block)
bp, err := f.ProveBatch(delHashes)
Expand All @@ -91,7 +91,7 @@ func pollardRandomRemember(blocks int32) error {
if err != nil {
return err
}
fmt.Printf("del %v\n", bp.Targets)
// fmt.Printf("del %v\n", bp.Targets)

// apply adds and deletes to the bridge node (could do this whenever)
_, err = f.Modify(adds, bp.Targets)
Expand Down Expand Up @@ -124,9 +124,9 @@ func pollardRandomRemember(blocks int32) error {
return err
}

fmt.Printf("pol postadd %s", p.ToString())
// fmt.Printf("pol postadd %s", p.ToString())

fmt.Printf("frs postadd %s", f.ToString())
// fmt.Printf("frs postadd %s", f.ToString())

// check all leaves match
if !p.equalToForestIfThere(f) {
Expand All @@ -141,15 +141,15 @@ func pollardRandomRemember(blocks int32) error {
return fmt.Errorf("block %d full %d tops, pol %d tops",
sn.blockHeight, len(fullTops), len(polTops))
}
fmt.Printf("top matching: ")
// fmt.Printf("top matching: ")
for i, ft := range fullTops {
fmt.Printf("f %04x p %04x ", ft[:4], polTops[i][:4])
// fmt.Printf("f %04x p %04x ", ft[:4], polTops[i][:4])
if ft != polTops[i] {
return fmt.Errorf("block %d top %d mismatch, full %x pol %x",
sn.blockHeight, i, ft[:4], polTops[i][:4])
}
}
fmt.Printf("\n")
// fmt.Printf("\n")
}

return nil
Expand Down Expand Up @@ -187,7 +187,7 @@ func fixedPollard(leaves int32) error {
if err != nil {
return err
}
fmt.Printf("forest post del %s", f.ToString())
// fmt.Printf("forest post del %s", f.ToString())

var p Pollard

Expand All @@ -196,7 +196,7 @@ func fixedPollard(leaves int32) error {
return err
}

fmt.Printf("pollard post add %s", p.ToString())
// fmt.Printf("pollard post add %s", p.ToString())

err = p.rem2(dels)
if err != nil {
Expand All @@ -207,9 +207,9 @@ func fixedPollard(leaves int32) error {
if err != nil {
return err
}
fmt.Printf("forest post del %s", f.ToString())
// fmt.Printf("forest post del %s", f.ToString())

fmt.Printf("pollard post del %s", p.ToString())
// fmt.Printf("pollard post del %s", p.ToString())

if !p.equalToForest(f) {
return fmt.Errorf("p != f (leaves)")
Expand Down
2 changes: 1 addition & 1 deletion accumulator/undo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestUndoFixed(t *testing.T) {

func TestUndoRandom(t *testing.T) {

for z := int64(0); z < 100; z++ {
for z := int64(0); z < 5; z++ {
// z := int64(11)
rand.Seed(z)
err := undoOnceRandom(20)
Expand Down

0 comments on commit 03a1fb9

Please sign in to comment.