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.

71 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. fmt.Println("Entering main loop")
  31. go net.Listen("8090")
  32. for {
  33. select {
  34. case <- batchTimer.C:
  35. fmt.Println("Timer triggered")
  36. // fmt.Println(batch.Create())
  37. //replace with chain link
  38. case signal := <-batchSignal:
  39. if signal == true {
  40. fmt.Println("Signal triggered")
  41. ns, bs := batch.Fetch()
  42. buf := &bytes.Buffer{}
  43. gob.NewEncoder(buf).Encode(bs)
  44. bb := buf.Bytes()
  45. cid := data.Publish(bb)
  46. data.Pin(cid)
  47. fmt.Printf("Batch published at: %s \n", cid)
  48. // add to ipfs
  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. }