@ -0,0 +1,35 @@ |
|||||
|
package batch |
||||
|
|
||||
|
import ( |
||||
|
"github.com/syndtr/goleveldb/leveldb" |
||||
|
// "encoding/json"
|
||||
|
"fmt" |
||||
|
"github.com/vocdoni/dvote-relay/types" |
||||
|
) |
||||
|
|
||||
|
//add (queue for counting)
|
||||
|
func Add(p types.Packet, bdb *leveldb.DB) error { |
||||
|
err := bdb.Put([]byte(fmt.Sprintf("%v", p.Nullifier)),[]byte(fmt.Sprintf("%v", p)), nil) |
||||
|
if err != nil { |
||||
|
return err |
||||
|
} |
||||
|
return nil |
||||
|
} |
||||
|
|
||||
|
//create (return batch)
|
||||
|
func Create(bdb *leveldb.DB) []byte { |
||||
|
var b []byte |
||||
|
|
||||
|
iter := bdb.NewIterator(nil, nil) |
||||
|
for iter.Next() { |
||||
|
err := iter.Error() |
||||
|
if err != nil { |
||||
|
panic(err) |
||||
|
} |
||||
|
//put p in batch
|
||||
|
//db.Put(iter.Key(), iter.Val(), nil)
|
||||
|
//bdb.Delete(iter.Key(), nil)
|
||||
|
} |
||||
|
iter.Release() |
||||
|
return b |
||||
|
} |
@ -0,0 +1 @@ |
|||||
|
package batch |
@ -0,0 +1,54 @@ |
|||||
|
package main |
||||
|
|
||||
|
import ( |
||||
|
"fmt" |
||||
|
"github.com/syndtr/goleveldb/leveldb" |
||||
|
"time" |
||||
|
"github.com/vocdoni/dvote-relay/batch" |
||||
|
) |
||||
|
|
||||
|
var dbPath = "$HOME/.dvote/relay.db" |
||||
|
var bdbPath = "$HOME/.dvote/batch.db" |
||||
|
var batchSeconds = 3 //seconds
|
||||
|
|
||||
|
var err error |
||||
|
var db *leveldb.DB |
||||
|
var bdb *leveldb.DB |
||||
|
var batchTimer *time.Ticker |
||||
|
var batchSignal chan int |
||||
|
|
||||
|
|
||||
|
func setup() { |
||||
|
db, err = leveldb.OpenFile(dbPath, nil) |
||||
|
if err != nil { |
||||
|
panic(err) |
||||
|
} |
||||
|
defer db.Close() |
||||
|
|
||||
|
bdb, err = leveldb.OpenFile(bdbPath, nil) |
||||
|
if err != nil { |
||||
|
panic(err) |
||||
|
} |
||||
|
defer bdb.Close() |
||||
|
|
||||
|
batchTimer = time.NewTicker(time.Second * time.Duration(batchSeconds)) |
||||
|
//create batch signal channel
|
||||
|
} |
||||
|
|
||||
|
func main() { |
||||
|
setup() |
||||
|
fmt.Println("Entering main loop") |
||||
|
for { |
||||
|
select { |
||||
|
case <- batchTimer.C: |
||||
|
fmt.Println("Timer triggered") |
||||
|
fmt.Println(batch.Create(bdb)) |
||||
|
//replace with chain link
|
||||
|
case <- batchSignal: |
||||
|
fmt.Println("Signal triggered") |
||||
|
fmt.Println(batch.Create(bdb)) |
||||
|
// case <- stopSignal:
|
||||
|
// return
|
||||
|
} |
||||
|
} |
||||
|
} |
@ -0,0 +1,30 @@ |
|||||
|
package types |
||||
|
|
||||
|
import ( |
||||
|
"time" |
||||
|
) |
||||
|
|
||||
|
type Submission struct { |
||||
|
Type string |
||||
|
Nonce []byte |
||||
|
KeyProof []byte |
||||
|
Package []byte |
||||
|
Timestamp time.Time |
||||
|
} |
||||
|
|
||||
|
type Batch struct { |
||||
|
Type string |
||||
|
Nullifiers [][]byte |
||||
|
URL string |
||||
|
TXID string |
||||
|
Nonce []byte |
||||
|
Signature string |
||||
|
} |
||||
|
|
||||
|
type Packet struct { |
||||
|
PID uint |
||||
|
Nullifier string |
||||
|
Vote string |
||||
|
Franchise string |
||||
|
} |
||||
|
|