You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
1.4 KiB

  1. package main
  2. import (
  3. "fmt"
  4. "time"
  5. "encoding/gob"
  6. "bytes"
  7. "github.com/vocdoni/dvote-relay/batch"
  8. "github.com/vocdoni/dvote-relay/net"
  9. "github.com/vocdoni/dvote-relay/db"
  10. "github.com/vocdoni/dvote-relay/data"
  11. )
  12. var dbPath = "~/.dvote/relay.db"
  13. var batchSeconds = 10 //seconds
  14. var batchSize = 3 //packets
  15. var err error
  16. var batchTimer *time.Ticker
  17. var batchSignal chan bool
  18. var signal bool
  19. func main() {
  20. db, err := db.NewLevelDbStorage(dbPath, false)
  21. if err != nil {
  22. panic(err)
  23. }
  24. defer db.Close()
  25. batch.Setup(db)
  26. batchTimer = time.NewTicker(time.Second * time.Duration(batchSeconds))
  27. batchSignal = make(chan bool)
  28. batch.BatchSignal = batchSignal
  29. batch.BatchSize = batchSize
  30. topic := "vocdoni_pubsub_testing"
  31. fmt.Println("Entering main loop")
  32. go net.Sub(topic)
  33. for {
  34. select {
  35. case <- batchTimer.C:
  36. fmt.Println("Timer triggered")
  37. // fmt.Println(batch.Create())
  38. //replace with chain link
  39. case signal := <-batchSignal:
  40. if signal == true {
  41. fmt.Println("Signal triggered")
  42. ns, bs := batch.Fetch()
  43. buf := &bytes.Buffer{}
  44. gob.NewEncoder(buf).Encode(bs)
  45. bb := buf.Bytes()
  46. cid := data.Publish(bb)
  47. data.Pin(cid)
  48. fmt.Printf("Batch published at: %s \n", cid)
  49. // add to chain
  50. // announce to pubsub
  51. //fmt.Println("Nullifiers:")
  52. //fmt.Println(n)
  53. //fmt.Println("Batch:")
  54. //fmt.Println(b)
  55. batch.Compact(ns)
  56. }
  57. default:
  58. continue
  59. }
  60. }
  61. }