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.

90 lines
1.8 KiB

  1. package main
  2. import (
  3. "fmt"
  4. "time"
  5. "encoding/gob"
  6. "bytes"
  7. "os"
  8. "flag"
  9. "github.com/vocdoni/go-dvote/batch"
  10. "github.com/vocdoni/go-dvote/net"
  11. "github.com/vocdoni/go-dvote/db"
  12. "github.com/vocdoni/go-dvote/data"
  13. "github.com/vocdoni/go-dvote/chain"
  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. node, err := chain.Init()
  40. node.Start()
  41. transport, err := net.Init(transportType)
  42. if err != nil {
  43. os.Exit(1)
  44. }
  45. fmt.Println("Entering main loop")
  46. go transport.Listen()
  47. for {
  48. select {
  49. case <- batchTimer.C:
  50. //fmt.Println("Timer triggered")
  51. // //fmt.Println(batch.Create())
  52. //replace with chain link
  53. case signal := <-batchSignal:
  54. if signal == true {
  55. fmt.Println("Signal triggered")
  56. ns, bs := batch.Fetch()
  57. buf := &bytes.Buffer{}
  58. gob.NewEncoder(buf).Encode(bs)
  59. bb := buf.Bytes()
  60. cid := data.Publish(bb)
  61. data.Pin(cid)
  62. fmt.Printf("Batch published at: %s \n", cid)
  63. node.LinkBatch([]byte(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. default:
  73. continue
  74. }
  75. }
  76. }