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.

85 lines
1.7 KiB

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