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.

73 lines
1.7 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. require.NotNil(t, s)
  34. // Test Sync
  35. // err = s.Sync()
  36. // require.Nil(t, err)
  37. // TODO: Reorg will be properly tested once we have the mock ethClient implemented
  38. /*
  39. // Force a Reorg
  40. lastSavedBlock, err := historyDB.GetLastBlock()
  41. require.Nil(t, err)
  42. lastSavedBlock.EthBlockNum++
  43. err = historyDB.AddBlock(lastSavedBlock)
  44. require.Nil(t, err)
  45. lastSavedBlock.EthBlockNum++
  46. err = historyDB.AddBlock(lastSavedBlock)
  47. require.Nil(t, err)
  48. log.Debugf("Wait for the blockchain to generate some blocks...")
  49. time.Sleep(40 * time.Second)
  50. err = s.Sync()
  51. require.Nil(t, err)
  52. */
  53. // Close History DB
  54. if err := historyDB.Close(); err != nil {
  55. fmt.Println("Error closing the history DB:", err)
  56. }
  57. }