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.

89 lines
2.0 KiB

  1. package main
  2. import (
  3. "bytes"
  4. "encoding/gob"
  5. "flag"
  6. "fmt"
  7. "os"
  8. "time"
  9. "github.com/vocdoni/go-dvote/batch"
  10. "github.com/vocdoni/go-dvote/data"
  11. "github.com/vocdoni/go-dvote/db"
  12. "github.com/vocdoni/go-dvote/net"
  13. "github.com/vocdoni/go-dvote/types"
  14. )
  15. var dbPath = "~/.dvote/relay.db"
  16. var batchSeconds = 10 //seconds
  17. var batchSize = 3 //packets
  18. var err error
  19. var batchTimer *time.Ticker
  20. var batchSignal chan bool
  21. var signal bool
  22. var transportType net.TransportID
  23. func main() {
  24. db, err := db.NewLevelDbStorage(dbPath, false)
  25. if err != nil {
  26. panic(err)
  27. }
  28. defer db.Close()
  29. batch.Setup(db)
  30. //gather transport type flag
  31. var transportIDString string
  32. flag.StringVar(&transportIDString, "transport", "PubSub", "Transport must be one of: PubSub, HTTP")
  33. flag.Parse()
  34. transportType = net.TransportIDFromString(transportIDString)
  35. batchTimer = time.NewTicker(time.Second * time.Duration(batchSeconds))
  36. batchSignal = make(chan bool)
  37. batch.BatchSignal = batchSignal
  38. batch.BatchSize = batchSize
  39. fmt.Println("Entering main loop")
  40. transport, err := net.Init(transportType)
  41. listenerOutput := make(chan types.Message, 10)
  42. listenerErrors := make(chan error)
  43. if err != nil {
  44. os.Exit(1)
  45. }
  46. go transport.Listen(listenerOutput, listenerErrors)
  47. go batch.Recieve(listenerOutput)
  48. for {
  49. select {
  50. case <-batchTimer.C:
  51. fmt.Println("Timer triggered")
  52. // fmt.Println(batch.Create())
  53. //replace with chain link
  54. case signal := <-batchSignal:
  55. if signal == true {
  56. fmt.Println("Signal triggered")
  57. ns, bs := batch.Fetch()
  58. buf := &bytes.Buffer{}
  59. gob.NewEncoder(buf).Encode(bs)
  60. bb := buf.Bytes()
  61. cid := data.Publish(bb)
  62. data.Pin(cid)
  63. fmt.Printf("Batch published at: %s \n", cid)
  64. // add to chain
  65. // announce to pubsub
  66. //fmt.Println("Nullifiers:")
  67. //fmt.Println(n)
  68. //fmt.Println("Batch:")
  69. //fmt.Println(b)
  70. batch.Compact(ns)
  71. }
  72. case listenError := <-listenerErrors:
  73. fmt.Println(listenError)
  74. default:
  75. continue
  76. }
  77. }
  78. }