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.

153 lines
3.9 KiB

  1. package coordinator
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "testing"
  6. "time"
  7. "github.com/hermeznetwork/hermez-node/batchbuilder"
  8. "github.com/hermeznetwork/hermez-node/db/historydb"
  9. "github.com/hermeznetwork/hermez-node/db/l2db"
  10. "github.com/hermeznetwork/hermez-node/db/statedb"
  11. "github.com/hermeznetwork/hermez-node/eth"
  12. "github.com/hermeznetwork/hermez-node/log"
  13. "github.com/hermeznetwork/hermez-node/txselector"
  14. "github.com/stretchr/testify/assert"
  15. "github.com/stretchr/testify/require"
  16. )
  17. func newTestModules(t *testing.T) (*txselector.TxSelector, *batchbuilder.BatchBuilder) { // FUTURE once Synchronizer is ready, should return it also
  18. nLevels := 32
  19. synchDB, err := ioutil.TempDir("", "tmpSynchDB")
  20. require.Nil(t, err)
  21. sdb, err := statedb.NewStateDB(synchDB, true, nLevels)
  22. assert.Nil(t, err)
  23. pass := os.Getenv("POSTGRES_PASS")
  24. l2DB, err := l2db.NewL2DB(5432, "localhost", "hermez", pass, "l2", 10, 512, 24*time.Hour)
  25. require.Nil(t, err)
  26. txselDir, err := ioutil.TempDir("", "tmpTxSelDB")
  27. require.Nil(t, err)
  28. txsel, err := txselector.NewTxSelector(txselDir, sdb, l2DB, 10, 10, 10)
  29. assert.Nil(t, err)
  30. bbDir, err := ioutil.TempDir("", "tmpBatchBuilderDB")
  31. require.Nil(t, err)
  32. bb, err := batchbuilder.NewBatchBuilder(bbDir, sdb, nil, 0, uint64(nLevels))
  33. assert.Nil(t, err)
  34. // l1Txs, coordinatorL1Txs, poolL2Txs := test.GenerateTestTxsFromSet(t, test.SetTest0)
  35. return txsel, bb
  36. }
  37. // CoordNode is an example of a Node that handles the goroutines for the coordinator
  38. type CoordNode struct {
  39. c *Coordinator
  40. stopForge chan bool
  41. stopGetProofCallForge chan bool
  42. stopForgeCallConfirm chan bool
  43. }
  44. func NewCoordNode(c *Coordinator) *CoordNode {
  45. return &CoordNode{
  46. c: c,
  47. }
  48. }
  49. func (cn *CoordNode) Start() {
  50. log.Debugw("Starting CoordNode...")
  51. cn.stopForge = make(chan bool)
  52. cn.stopGetProofCallForge = make(chan bool)
  53. cn.stopForgeCallConfirm = make(chan bool)
  54. batchCh0 := make(chan *BatchInfo)
  55. batchCh1 := make(chan *BatchInfo)
  56. go func() {
  57. for {
  58. select {
  59. case <-cn.stopForge:
  60. return
  61. default:
  62. if forge, err := cn.c.ForgeLoopFn(batchCh0, cn.stopForge); err == ErrStop {
  63. return
  64. } else if err != nil {
  65. log.Errorw("CoordNode ForgeLoopFn", "error", err)
  66. } else if !forge {
  67. time.Sleep(500 * time.Millisecond)
  68. }
  69. }
  70. }
  71. }()
  72. go func() {
  73. for {
  74. select {
  75. case <-cn.stopGetProofCallForge:
  76. return
  77. default:
  78. if err := cn.c.GetProofCallForgeLoopFn(
  79. batchCh0, batchCh1, cn.stopGetProofCallForge); err == ErrStop {
  80. return
  81. } else if err != nil {
  82. log.Errorw("CoordNode GetProofCallForgeLoopFn", "error", err)
  83. }
  84. }
  85. }
  86. }()
  87. go func() {
  88. for {
  89. select {
  90. case <-cn.stopForgeCallConfirm:
  91. return
  92. default:
  93. if err := cn.c.ForgeCallConfirmLoopFn(
  94. batchCh1, cn.stopForgeCallConfirm); err == ErrStop {
  95. return
  96. } else if err != nil {
  97. log.Errorw("CoordNode ForgeCallConfirmLoopFn", "error", err)
  98. }
  99. }
  100. }
  101. }()
  102. }
  103. func (cn *CoordNode) Stop() {
  104. log.Debugw("Stopping CoordNode...")
  105. cn.stopForge <- true
  106. cn.stopGetProofCallForge <- true
  107. cn.stopForgeCallConfirm <- true
  108. }
  109. func TestCoordinator(t *testing.T) {
  110. txsel, bb := newTestModules(t)
  111. conf := Config{}
  112. hdb := &historydb.HistoryDB{}
  113. c := NewCoordinator(conf, hdb, txsel, bb, &eth.Client{})
  114. cn := NewCoordNode(c)
  115. cn.Start()
  116. time.Sleep(1 * time.Second)
  117. // simulate forgeSequence time
  118. log.Info("simulate entering in forge time")
  119. c.isForgeSeq = true
  120. time.Sleep(1 * time.Second)
  121. // simulate going out from forgeSequence
  122. log.Info("simulate going out from forge time")
  123. c.isForgeSeq = false
  124. time.Sleep(1 * time.Second)
  125. // simulate entering forgeSequence time again
  126. log.Info("simulate entering in forge time again")
  127. c.isForgeSeq = true
  128. time.Sleep(1 * time.Second)
  129. // simulate stopping forgerLoop by channel
  130. log.Info("simulate stopping forgerLoop by closing coordinator stopch")
  131. cn.Stop()
  132. time.Sleep(1 * time.Second)
  133. }