Browse Source

Publishes batches to ipfs and pins them. Assumes running ipfs daemon on 8080. Moved relay listener to 8090

batching_initial_approach
imw 5 years ago
parent
commit
d09af85502
5 changed files with 26 additions and 14 deletions
  1. +2
    -2
      batch/batch.go
  2. +3
    -3
      data/data.go
  3. +1
    -1
      generator/generator.go
  4. +19
    -7
      main.go
  5. +1
    -1
      types/types.go

+ 2
- 2
batch/batch.go

@ -44,7 +44,7 @@ func Add(ballot types.Ballot) error {
//k is []byte 'batch_' + nullifier
//v is []byte package
//returns slice of nullifiers, batch json
func Create() ([]string, []string) {
func Fetch() ([]string, []string) {
var n []string
var b []string
iter := bdb.Iter()
@ -73,7 +73,7 @@ func Create() ([]string, []string) {
//move from bdb to rdb once pinned
func Compact(n []string) {
for _, k := range n {
fmt.Println(k)
//fmt.Println(k)
v, err := bdb.Get([]byte(k))
if err != nil {
fmt.Println(err.Error())

+ 3
- 3
data/data.go

@ -8,7 +8,7 @@ import (
shell "github.com/ipfs/go-ipfs-api"
)
func publish(object []byte) string {
func Publish(object []byte) string {
sh := shell.NewShell("localhost:5001")
cid, err := sh.Add(bytes.NewBuffer(object))
if err != nil {
@ -18,7 +18,7 @@ func publish(object []byte) string {
return cid
}
func pin(path string) {
func Pin(path string) {
sh := shell.NewShell("localhost:5001")
err := sh.Pin(path)
if err != nil{
@ -28,7 +28,7 @@ func pin(path string) {
}
func retrieve(hash string) []byte {
func Retrieve(hash string) []byte {
sh := shell.NewShell("localhost:5001")
reader, err := sh.Cat(hash)
if err != nil {

+ 1
- 1
generator/generator.go

@ -75,7 +75,7 @@ func main() {
i, _ := strconv.Atoi(interval)
timer := time.NewTicker(time.Millisecond * time.Duration(i))
rand.Seed(time.Now().UnixNano())
url := "http://localhost:8080/submit"
url := "http://localhost:8090/submit"
fmt.Println("URL:>", url)
for {

+ 19
- 7
main.go

@ -3,9 +3,12 @@ package main
import (
"fmt"
"time"
"encoding/gob"
"bytes"
"github.com/vocdoni/dvote-relay/batch"
"github.com/vocdoni/dvote-relay/net"
"github.com/vocdoni/dvote-relay/db"
"github.com/vocdoni/dvote-relay/data"
)
var dbPath = "~/.dvote/relay.db"
@ -35,7 +38,7 @@ func main() {
batch.BatchSize = batchSize
fmt.Println("Entering main loop")
go net.Listen("8080")
go net.Listen("8090")
for {
select {
case <- batchTimer.C:
@ -45,12 +48,21 @@ func main() {
case signal := <-batchSignal:
if signal == true {
fmt.Println("Signal triggered")
n, b := batch.Create()
fmt.Println("Nullifiers:")
fmt.Println(n)
fmt.Println("Batch:")
fmt.Println(b)
batch.Compact(n)
ns, bs := batch.Fetch()
buf := &bytes.Buffer{}
gob.NewEncoder(buf).Encode(bs)
bb := buf.Bytes()
cid := data.Publish(bb)
data.Pin(cid)
fmt.Printf("Batch published at: %s \n", cid)
// add to ipfs
// add to chain
// announce to pubsub
//fmt.Println("Nullifiers:")
//fmt.Println(n)
//fmt.Println("Batch:")
//fmt.Println(b)
batch.Compact(ns)
}
default:
continue

+ 1
- 1
types/types.go

@ -22,7 +22,7 @@ type Envelope struct {
type Batch struct {
Type string
Nullifiers [][]byte
Nullifiers []string
URL string
TXID string
Nonce []byte

Loading…
Cancel
Save