Skip to content

Commit

Permalink
census: addbatch now uses add with a loop
Browse files Browse the repository at this point in the history
Signed-off-by: p4u <[email protected]>
  • Loading branch information
p4u committed Sep 28, 2023
1 parent ef28a9c commit d9c252e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 22 deletions.
5 changes: 3 additions & 2 deletions api/censuses.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,13 +257,14 @@ func (a *API) censusAddHandler(msg *apirest.APIdata, ctx *httprouter.HTTPContext
}

// add the keys and values to the tree in a single transaction
log.Infow("adding participants to census", "census", hex.EncodeToString(censusID), "count", len(keys))
failed, err := ref.Tree().AddBatch(keys, values)
if err != nil {
return ErrCantAddKeyAndValueToTree.WithErr(err)
}
log.Infof("added %d keys to census %x", len(keys), censusID)
if len(failed) > 0 {
log.Warnf("failed participants %v", failed)
log.Warnw("failed to add some participants to census",
"census", hex.EncodeToString(censusID), "count", len(failed))
}

return ctx.Send(deprecationMsg, apirest.HTTPstatusOK)
Expand Down
37 changes: 18 additions & 19 deletions censustree/censustree.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package censustree

import (
"encoding/hex"
"errors"
"fmt"
"math/big"
Expand Down Expand Up @@ -236,32 +237,30 @@ func (t *Tree) updateCensusWeight(wTx db.WriteTx, w []byte) error {

// AddBatch wraps t.tree.AddBatch while acquiring the lock.
func (t *Tree) AddBatch(keys, values [][]byte) ([]int, error) {
if len(keys) != len(values) {
return nil, fmt.Errorf("keys and values must have the same length")
}
t.Lock()
defer t.Unlock()
wTx := t.tree.DB().WriteTx()
defer wTx.Discard()

invalids, err := t.tree.AddBatch(wTx, keys, values)
if err != nil {
return invalids, fmt.Errorf("addBatch failed: %w", err)
}

// The census weight update should be done only for the
// censuses that have weight.
if values != nil {
// get value of all added keys
addedWeight := big.NewInt(0)
for i := 0; i < len(values); i++ {
addedWeight = new(big.Int).Add(addedWeight, t.BytesToBigInt(values[i]))
}
// remove weight of invalid leafs
for i := 0; i < len(invalids); i++ {
addedWeight = new(big.Int).Sub(addedWeight, t.BytesToBigInt(values[invalids[i]]))
var invalids []int
weight := big.NewInt(0)
for i := 0; i < len(keys); i++ {
if err := t.tree.Add(wTx, keys[i], values[i]); err != nil {
log.Debugw("could not add key to census",
"key", hex.EncodeToString(keys[i]),
"value", hex.EncodeToString(values[i]),
"err", err)
invalids = append(invalids, i)
continue
}
weight = new(big.Int).Add(weight, t.BytesToBigInt(values[i]))
}

if err := t.updateCensusWeight(wTx, t.BigIntToBytes(addedWeight)); err != nil {
return nil, err
}
if err := t.updateCensusWeight(wTx, t.BigIntToBytes(weight)); err != nil {
return nil, err
}

return invalids, wTx.Commit()
Expand Down
2 changes: 1 addition & 1 deletion censustree/censustree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ func TestGetCensusWeight(t *testing.T) {
keys = append(keys, []byte("keysWithoutWeight" + strconv.Itoa(i))[:DefaultMaxKeyLen])
}

invalids, err = tree.AddBatch(keys, nil)
invalids, err = tree.AddBatch(keys, make([][]byte, len(keys)))
qt.Assert(t, err, qt.IsNil)
qt.Assert(t, len(invalids), qt.Equals, 0)

Expand Down

0 comments on commit d9c252e

Please sign in to comment.