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.

118 lines
2.8 KiB

  1. package synchronizer
  2. import (
  3. "context"
  4. "io/ioutil"
  5. "os"
  6. "testing"
  7. ethCommon "github.com/ethereum/go-ethereum/common"
  8. dbUtils "github.com/hermeznetwork/hermez-node/db"
  9. "github.com/hermeznetwork/hermez-node/db/historydb"
  10. "github.com/hermeznetwork/hermez-node/db/statedb"
  11. "github.com/hermeznetwork/hermez-node/test"
  12. "github.com/stretchr/testify/assert"
  13. "github.com/stretchr/testify/require"
  14. )
  15. type timer struct {
  16. time int64
  17. }
  18. func (t *timer) Time() int64 {
  19. currentTime := t.time
  20. t.time++
  21. return currentTime
  22. }
  23. func TestSync(t *testing.T) {
  24. // Int State DB
  25. dir, err := ioutil.TempDir("", "tmpdb")
  26. require.Nil(t, err)
  27. defer assert.Nil(t, os.RemoveAll(dir))
  28. stateDB, err := statedb.NewStateDB(dir, statedb.TypeSynchronizer, 32)
  29. assert.Nil(t, err)
  30. // Init History DB
  31. pass := os.Getenv("POSTGRES_PASS")
  32. db, err := dbUtils.InitSQLDB(5432, "localhost", "hermez", pass, "hermez")
  33. require.Nil(t, err)
  34. historyDB := historydb.NewHistoryDB(db)
  35. // Clear DB
  36. err = historyDB.Reorg(-1)
  37. assert.Nil(t, err)
  38. // Init eth client
  39. var timer timer
  40. clientSetup := test.NewClientSetupExample()
  41. client := test.NewClient(true, &timer, &ethCommon.Address{}, clientSetup)
  42. // Create Synchronizer
  43. s, err := NewSynchronizer(client, historyDB, stateDB)
  44. require.Nil(t, err)
  45. // Test Sync for ethereum genesis block
  46. err = s.Sync(context.Background())
  47. require.Nil(t, err)
  48. blocks, err := s.historyDB.GetBlocks(0, 9999)
  49. require.Nil(t, err)
  50. assert.Equal(t, int64(1), blocks[0].EthBlockNum)
  51. // TODO once Til is completed
  52. /*
  53. // Test Sync for a block with new Tokens and L1UserTxs
  54. // accounts := test.GenerateKeys(t, []string{"A", "B", "C", "D"})
  55. l1UserTxs, _, _, _ := test.GenerateTestTxsFromSet(t, `
  56. A (1): 10
  57. A (2): 20
  58. B (1): 5
  59. C (1): 8
  60. D (3): 15
  61. > advance batch
  62. `)
  63. require.Greater(t, len(l1UserTxs[0]), 0)
  64. // require.Greater(t, len(tokens), 0)
  65. for i := 1; i <= 3; i++ {
  66. _, err := client.RollupAddToken(ethCommon.BigToAddress(big.NewInt(int64(i*10000))),
  67. clientSetup.RollupVariables.FeeAddToken)
  68. require.Nil(t, err)
  69. }
  70. for i := range l1UserTxs[0] {
  71. client.CtlAddL1TxUser(&l1UserTxs[0][i])
  72. }
  73. client.CtlMineBlock()
  74. err = s.Sync(context.Background())
  75. require.Nil(t, err)
  76. getTokens, err := s.historyDB.GetTokens()
  77. require.Nil(t, err)
  78. assert.Equal(t, 3, len(getTokens))
  79. */
  80. // TODO: Reorg will be properly tested once we have the mock ethClient implemented
  81. /*
  82. // Force a Reorg
  83. lastSavedBlock, err := historyDB.GetLastBlock()
  84. require.Nil(t, err)
  85. lastSavedBlock.EthBlockNum++
  86. err = historyDB.AddBlock(lastSavedBlock)
  87. require.Nil(t, err)
  88. lastSavedBlock.EthBlockNum++
  89. err = historyDB.AddBlock(lastSavedBlock)
  90. require.Nil(t, err)
  91. log.Debugf("Wait for the blockchain to generate some blocks...")
  92. time.Sleep(40 * time.Second)
  93. err = s.Sync()
  94. require.Nil(t, err)
  95. */
  96. }