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.

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