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.

72 lines
1.6 KiB

  1. package synchronizer
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "os"
  6. "testing"
  7. "github.com/ethereum/go-ethereum/ethclient"
  8. "github.com/hermeznetwork/hermez-node/db/historydb"
  9. "github.com/hermeznetwork/hermez-node/db/statedb"
  10. "github.com/hermeznetwork/hermez-node/eth"
  11. "github.com/stretchr/testify/assert"
  12. "github.com/stretchr/testify/require"
  13. )
  14. func Test(t *testing.T) {
  15. // Int State DB
  16. dir, err := ioutil.TempDir("", "tmpdb")
  17. require.Nil(t, err)
  18. sdb, err := statedb.NewStateDB(dir, true, 32)
  19. assert.Nil(t, err)
  20. // Init History DB
  21. pass := os.Getenv("POSTGRES_PASS")
  22. historyDB, err := historydb.NewHistoryDB(5432, "localhost", "hermez", pass, "history")
  23. require.Nil(t, err)
  24. err = historyDB.Reorg(0)
  25. assert.Nil(t, err)
  26. // Init eth client
  27. ehtClientDialURL := os.Getenv("ETHCLIENT_DIAL_URL")
  28. ethClient, err := ethclient.Dial(ehtClientDialURL)
  29. require.Nil(t, err)
  30. client := eth.NewClient(ethClient, nil, nil, nil)
  31. // Create Synchronizer
  32. s := NewSynchronizer(client, historyDB, sdb)
  33. // Test Sync
  34. err = s.Sync()
  35. require.Nil(t, err)
  36. // TODO: Reorg will be properly tested once we have the mock ethClient implemented
  37. /*
  38. // Force a Reorg
  39. lastSavedBlock, err := historyDB.GetLastBlock()
  40. require.Nil(t, err)
  41. lastSavedBlock.EthBlockNum++
  42. err = historyDB.AddBlock(lastSavedBlock)
  43. require.Nil(t, err)
  44. lastSavedBlock.EthBlockNum++
  45. err = historyDB.AddBlock(lastSavedBlock)
  46. require.Nil(t, err)
  47. log.Debugf("Wait for the blockchain to generate some blocks...")
  48. time.Sleep(40 * time.Second)
  49. err = s.Sync()
  50. require.Nil(t, err)
  51. */
  52. // Close History DB
  53. if err := historyDB.Close(); err != nil {
  54. fmt.Println("Error closing the history DB:", err)
  55. }
  56. }