arnaucube
arnaucube/arboPublic Self-hosted repository
Update missing errors management
arnaucube root@arnaucube.com
Wed, 21 Jul 2021 11:57:18 +0200
commit

01ec075b357e8b7e645babaef9be433bbd07a127

parent

df896f6e961a91df593f723039d6f253c392bd06

3 files changed, 43 insertions(+), 12 deletions(-)

jump to
M addbatch_test.go → addbatch_test.go

@@ -952,9 +952,20 @@

indexes, err := tree2.AddBatch(keys, values) c.Assert(err, qt.IsNil) c.Check(len(indexes), qt.Equals, 0) - // check that both trees roots are equal checkRoots(c, tree, tree2) + + // use tree3 to add nil value array + database3, err := db.NewBadgerDB(c.TempDir()) + c.Assert(err, qt.IsNil) + tree3, err := NewTree(database3, 100, HashFunctionPoseidon) + c.Assert(err, qt.IsNil) + defer tree3.db.Close() //nolint:errcheck + + indexes, err = tree3.AddBatch(keys, nil) + c.Assert(err, qt.IsNil) + c.Check(len(indexes), qt.Equals, 0) + checkRoots(c, tree, tree3) kAux, proofV, siblings, existence, err := tree2.GenProof(keys[9]) c.Assert(err, qt.IsNil)
M tree.go → tree.go

@@ -151,13 +151,16 @@ if err != nil {

return nil, err } - // TODO check validity of keys & values for Tree.hashFunction + // TODO check validity of keys & values for Tree.hashFunction (maybe do + // not add the checks, as would need more time, and this could be + // checked/ensured before calling this method) + e := []byte{} // equal the number of keys & values if len(keys) > len(values) { // add missing values for i := len(values); i < len(keys); i++ { - values = append(values, emptyValue) + values = append(values, e) } } else if len(keys) < len(values) { // crop extra values

@@ -172,7 +175,9 @@

// once the VirtualTree is build, compute the hashes pairs, err := vt.computeHashes() if err != nil { - // TODO currently invalids in computeHashes are not counted + // currently invalids in computeHashes are not counted, + // but should not be needed, as if there is an error there is + // nothing stored in the db and the error is returned return nil, err } t.root = vt.root.h

@@ -214,6 +219,7 @@ return

} leafK, leafV := ReadLeafValue(v) if err := vt.add(0, leafK, leafV); err != nil { + // TODO instead of panic, return this error panic(err) } })

@@ -232,7 +238,9 @@ var err error

t.dbBatch = t.db.NewBatch() t.batchMemory = make(map[[bmKeySize]byte]kv) // TODO TMP - // TODO check validity of key & value for Tree.hashFunction + // TODO check validity of key & value for Tree.hashFunction (maybe do + // not add the checks, as would need more time, and this could be + // checked/ensured before calling this method) err = t.add(0, k, v) // add from level 0 if err != nil {
M vt.go → vt.go

@@ -160,7 +160,10 @@ return nil, err

} } if len(nodesAtL) != nCPU { - panic("should not happen") // TODO TMP + return nil, fmt.Errorf("This error should not be reached."+ + " len(nodesAtL) != nCPU, len(nodesAtL)=%d, nCPU=%d."+ + " Please report it in a new issue:"+ + " https://github.com/vocdoni/arbo/issues/new", len(nodesAtL), nCPU) } subRoots := make([]*node, nCPU)

@@ -224,7 +227,10 @@ if currLvl == l && typ != vtEmpty {

return []*node{n}, nil } if currLvl >= l { - panic("should not reach this point") // TODO TMP + return nil, fmt.Errorf("This error should not be reached."+ + " currLvl >= l, currLvl=%d, l=%d."+ + " Please report it in a new issue:"+ + " https://github.com/vocdoni/arbo/issues/new", currLvl, l) } var nodes []*node

@@ -306,6 +312,7 @@ }

subRoots := make([]*node, nCPU) bucketPairs := make([][][2][]byte, nCPU) dbgStatsPerBucket := make([]*dbgStats, nCPU) + errs := make([]error, nCPU) var wg sync.WaitGroup wg.Add(nCPU)

@@ -318,8 +325,7 @@

bucketPairs[cpu], err = bucketVT.root.computeHashes(l, t.params.maxLevels, bucketVT.params, bucketPairs[cpu]) if err != nil { - // TODO WIP - panic("TODO: " + err.Error()) + errs[cpu] = err } subRoots[cpu] = bucketVT.root

@@ -328,6 +334,12 @@ wg.Done()

}(i) } wg.Wait() + + for i := 0; i < len(errs); i++ { + if errs[i] != nil { + return nil, errs[i] + } + } for i := 0; i < len(dbgStatsPerBucket); i++ { t.params.dbg.add(dbgStatsPerBucket[i])

@@ -441,9 +453,9 @@ if err := n.downUntilDivergence(p, currLvl, oldLeaf, leaf); err != nil {

return err } case vtEmpty: - panic(fmt.Errorf("EMPTY %v", n)) // TODO TMP + return fmt.Errorf("virtual tree node.add() with empty node %v", n) default: - return fmt.Errorf("ERR") // TODO TMP + return fmt.Errorf("virtual tree node.add() with unknown node type %v", n) } return nil

@@ -586,7 +598,7 @@ kv := [2][]byte{k, v}

pairs = append(pairs, kv) case vtEmpty: default: - return nil, fmt.Errorf("ERR:n.computeHashes type (%d) no match", t) // TODO TMP + return nil, fmt.Errorf("error: n.computeHashes type (%d) no match", t) } return pairs, nil