mirror of
https://github.com/arnaucube/go-dvote.git
synced 2026-02-28 05:26:46 +01:00
Publishes batches to ipfs and pins them. Assumes running ipfs daemon on 8080. Moved relay listener to 8090
This commit is contained in:
@@ -44,7 +44,7 @@ func Add(ballot types.Ballot) error {
|
|||||||
//k is []byte 'batch_' + nullifier
|
//k is []byte 'batch_' + nullifier
|
||||||
//v is []byte package
|
//v is []byte package
|
||||||
//returns slice of nullifiers, batch json
|
//returns slice of nullifiers, batch json
|
||||||
func Create() ([]string, []string) {
|
func Fetch() ([]string, []string) {
|
||||||
var n []string
|
var n []string
|
||||||
var b []string
|
var b []string
|
||||||
iter := bdb.Iter()
|
iter := bdb.Iter()
|
||||||
@@ -73,7 +73,7 @@ func Create() ([]string, []string) {
|
|||||||
//move from bdb to rdb once pinned
|
//move from bdb to rdb once pinned
|
||||||
func Compact(n []string) {
|
func Compact(n []string) {
|
||||||
for _, k := range n {
|
for _, k := range n {
|
||||||
fmt.Println(k)
|
//fmt.Println(k)
|
||||||
v, err := bdb.Get([]byte(k))
|
v, err := bdb.Get([]byte(k))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err.Error())
|
fmt.Println(err.Error())
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import (
|
|||||||
shell "github.com/ipfs/go-ipfs-api"
|
shell "github.com/ipfs/go-ipfs-api"
|
||||||
)
|
)
|
||||||
|
|
||||||
func publish(object []byte) string {
|
func Publish(object []byte) string {
|
||||||
sh := shell.NewShell("localhost:5001")
|
sh := shell.NewShell("localhost:5001")
|
||||||
cid, err := sh.Add(bytes.NewBuffer(object))
|
cid, err := sh.Add(bytes.NewBuffer(object))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -18,7 +18,7 @@ func publish(object []byte) string {
|
|||||||
return cid
|
return cid
|
||||||
}
|
}
|
||||||
|
|
||||||
func pin(path string) {
|
func Pin(path string) {
|
||||||
sh := shell.NewShell("localhost:5001")
|
sh := shell.NewShell("localhost:5001")
|
||||||
err := sh.Pin(path)
|
err := sh.Pin(path)
|
||||||
if err != nil{
|
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")
|
sh := shell.NewShell("localhost:5001")
|
||||||
reader, err := sh.Cat(hash)
|
reader, err := sh.Cat(hash)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -75,7 +75,7 @@ func main() {
|
|||||||
i, _ := strconv.Atoi(interval)
|
i, _ := strconv.Atoi(interval)
|
||||||
timer := time.NewTicker(time.Millisecond * time.Duration(i))
|
timer := time.NewTicker(time.Millisecond * time.Duration(i))
|
||||||
rand.Seed(time.Now().UnixNano())
|
rand.Seed(time.Now().UnixNano())
|
||||||
url := "http://localhost:8080/submit"
|
url := "http://localhost:8090/submit"
|
||||||
fmt.Println("URL:>", url)
|
fmt.Println("URL:>", url)
|
||||||
|
|
||||||
for {
|
for {
|
||||||
|
|||||||
26
main.go
26
main.go
@@ -3,9 +3,12 @@ package main
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
"encoding/gob"
|
||||||
|
"bytes"
|
||||||
"github.com/vocdoni/dvote-relay/batch"
|
"github.com/vocdoni/dvote-relay/batch"
|
||||||
"github.com/vocdoni/dvote-relay/net"
|
"github.com/vocdoni/dvote-relay/net"
|
||||||
"github.com/vocdoni/dvote-relay/db"
|
"github.com/vocdoni/dvote-relay/db"
|
||||||
|
"github.com/vocdoni/dvote-relay/data"
|
||||||
)
|
)
|
||||||
|
|
||||||
var dbPath = "~/.dvote/relay.db"
|
var dbPath = "~/.dvote/relay.db"
|
||||||
@@ -35,7 +38,7 @@ func main() {
|
|||||||
batch.BatchSize = batchSize
|
batch.BatchSize = batchSize
|
||||||
|
|
||||||
fmt.Println("Entering main loop")
|
fmt.Println("Entering main loop")
|
||||||
go net.Listen("8080")
|
go net.Listen("8090")
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <- batchTimer.C:
|
case <- batchTimer.C:
|
||||||
@@ -45,12 +48,21 @@ func main() {
|
|||||||
case signal := <-batchSignal:
|
case signal := <-batchSignal:
|
||||||
if signal == true {
|
if signal == true {
|
||||||
fmt.Println("Signal triggered")
|
fmt.Println("Signal triggered")
|
||||||
n, b := batch.Create()
|
ns, bs := batch.Fetch()
|
||||||
fmt.Println("Nullifiers:")
|
buf := &bytes.Buffer{}
|
||||||
fmt.Println(n)
|
gob.NewEncoder(buf).Encode(bs)
|
||||||
fmt.Println("Batch:")
|
bb := buf.Bytes()
|
||||||
fmt.Println(b)
|
cid := data.Publish(bb)
|
||||||
batch.Compact(n)
|
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:
|
default:
|
||||||
continue
|
continue
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ type Envelope struct {
|
|||||||
|
|
||||||
type Batch struct {
|
type Batch struct {
|
||||||
Type string
|
Type string
|
||||||
Nullifiers [][]byte
|
Nullifiers []string
|
||||||
URL string
|
URL string
|
||||||
TXID string
|
TXID string
|
||||||
Nonce []byte
|
Nonce []byte
|
||||||
|
|||||||
Reference in New Issue
Block a user