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.

178 lines
4.4 KiB

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