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.

114 lines
2.7 KiB

  1. package synchronizer
  2. import (
  3. "context"
  4. "io/ioutil"
  5. "math/big"
  6. "os"
  7. "testing"
  8. ethCommon "github.com/ethereum/go-ethereum/common"
  9. dbUtils "github.com/hermeznetwork/hermez-node/db"
  10. "github.com/hermeznetwork/hermez-node/db/historydb"
  11. "github.com/hermeznetwork/hermez-node/db/statedb"
  12. "github.com/hermeznetwork/hermez-node/test"
  13. "github.com/stretchr/testify/assert"
  14. "github.com/stretchr/testify/require"
  15. )
  16. type timer struct {
  17. time int64
  18. }
  19. func (t *timer) Time() int64 {
  20. currentTime := t.time
  21. t.time++
  22. return currentTime
  23. }
  24. func TestSync(t *testing.T) {
  25. // Int State DB
  26. dir, err := ioutil.TempDir("", "tmpdb")
  27. require.Nil(t, err)
  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(0), blocks[0].EthBlockNum)
  51. // Test Sync for a block with new Tokens and L1UserTxs
  52. // accounts := test.GenerateKeys(t, []string{"A", "B", "C", "D"})
  53. l1UserTxs, _, _, _ := test.GenerateTestTxsFromSet(t, `
  54. A (1): 10
  55. A (2): 20
  56. B (1): 5
  57. C (1): 8
  58. D (3): 15
  59. > advance batch
  60. `)
  61. require.Greater(t, len(l1UserTxs[0]), 0)
  62. // require.Greater(t, len(tokens), 0)
  63. for i := 1; i <= 3; i++ {
  64. _, err := client.RollupAddToken(ethCommon.BigToAddress(big.NewInt(int64(i * 10000))))
  65. require.Nil(t, err)
  66. }
  67. for i := range l1UserTxs[0] {
  68. client.CtlAddL1TxUser(&l1UserTxs[0][i])
  69. }
  70. client.CtlMineBlock()
  71. err = s.Sync(context.Background())
  72. require.Nil(t, err)
  73. getTokens, err := s.historyDB.GetTokens()
  74. require.Nil(t, err)
  75. assert.Equal(t, 3, len(getTokens))
  76. // TODO: Reorg will be properly tested once we have the mock ethClient implemented
  77. /*
  78. // Force a Reorg
  79. lastSavedBlock, err := historyDB.GetLastBlock()
  80. require.Nil(t, err)
  81. lastSavedBlock.EthBlockNum++
  82. err = historyDB.AddBlock(lastSavedBlock)
  83. require.Nil(t, err)
  84. lastSavedBlock.EthBlockNum++
  85. err = historyDB.AddBlock(lastSavedBlock)
  86. require.Nil(t, err)
  87. log.Debugf("Wait for the blockchain to generate some blocks...")
  88. time.Sleep(40 * time.Second)
  89. err = s.Sync()
  90. require.Nil(t, err)
  91. */
  92. }