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.

197 lines
5.1 KiB

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