From 01ec075b357e8b7e645babaef9be433bbd07a127 Mon Sep 17 00:00:00 2001 From: arnaucube Date: Wed, 21 Jul 2021 11:57:18 +0200 Subject: [PATCH] Update missing errors management --- addbatch_test.go | 13 ++++++++++++- tree.go | 16 ++++++++++++---- vt.go | 26 +++++++++++++++++++------- 3 files changed, 43 insertions(+), 12 deletions(-) diff --git a/addbatch_test.go b/addbatch_test.go index 1e89a57..858a68a 100644 --- a/addbatch_test.go +++ b/addbatch_test.go @@ -952,10 +952,21 @@ func TestAddKeysWithEmptyValues(t *testing.T) { 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) c.Assert(proofV, qt.DeepEquals, values[9]) diff --git a/tree.go b/tree.go index 81c0dd9..895a09d 100644 --- a/tree.go +++ b/tree.go @@ -151,13 +151,16 @@ func (t *Tree) AddBatch(keys, values [][]byte) ([]int, error) { 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 @@ func (t *Tree) AddBatch(keys, values [][]byte) ([]int, error) { // 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 @@ func (t *Tree) loadVT() (vt, error) { } 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 @@ func (t *Tree) Add(k, v []byte) 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 { diff --git a/vt.go b/vt.go index a2152fd..5358585 100644 --- a/vt.go +++ b/vt.go @@ -160,7 +160,10 @@ func (t *vt) addBatch(ks, vs [][]byte) ([]int, error) { } } 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 @@ func (n *node) getNodesAtLevel(currLvl, l int) ([]*node, error) { 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 @@ func (t *vt) computeHashes() ([][2][]byte, error) { 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 @@ func (t *vt) computeHashes() ([][2][]byte, error) { 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 @@ -329,6 +335,12 @@ func (t *vt) computeHashes() ([][2][]byte, error) { } 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 @@ func (n *node) add(p *params, currLvl int, leaf *node) error { 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 @@ func (n *node) computeHashes(currLvl, maxLvl int, p *params, pairs [][2][]byte) 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