go-dvote/batch/batch.go
batch/batch.goRaw
1package batch
2
3import (
4 "fmt"
5 "github.com/vocdoni/dvote-relay/types"
6 "github.com/vocdoni/dvote-relay/db"
7)
8
9var rdb *db.LevelDbStorage
10var bdb *db.LevelDbStorage
11var BatchSignal chan bool
12var BatchSize int
13var currentBatchSize int
14var err error
15
16func Setup(l *db.LevelDbStorage) {
17 rdb = l.WithPrefix([]byte("relay"))
18 bdb = l.WithPrefix([]byte("batch"))
19 fmt.Println("Batch storage:")
20 fmt.Println(bdb)
21}
22
23
24//add (queue for counting)
25func Add(p types.Packet) error {
26 fmt.Println(bdb)
27 err := bdb.Put([]byte(fmt.Sprintf("%v", p.Nullifier)),[]byte(fmt.Sprintf("%v", p)))
28 if err != nil {
29 return err
30 }
31
32 currentBatchSize++
33 if currentBatchSize >= BatchSize {
34 BatchSignal <- true
35 }
36 return nil
37}
38
39//create (return batch)
40func Create() []byte {
41 var b []byte
42
43 iter := bdb.Iter()
44 for iter.Next() {
45 //k := iter.Key()
46 //v := iter.Value()
47 err := iter.Error()
48 if err != nil {
49 panic(err)
50 }
51 //put p in batch
52 //rdb.Put(iter.Key(), iter.Val(), nil)
53 //bdb.Delete(iter.Key(), nil)
54 }
55 iter.Release()
56 return b
57}