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.

69 lines
1.6 KiB

  1. package synchronizer
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "testing"
  6. "github.com/ethereum/go-ethereum/ethclient"
  7. dbUtils "github.com/hermeznetwork/hermez-node/db"
  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, statedb.TypeSynchronizer, 32)
  19. assert.Nil(t, err)
  20. // Init History DB
  21. pass := os.Getenv("POSTGRES_PASS")
  22. db, err := dbUtils.InitSQLDB(5432, "localhost", "hermez", pass, "hermez")
  23. require.Nil(t, err)
  24. historyDB := historydb.NewHistoryDB(db)
  25. err = historyDB.Reorg(0)
  26. assert.Nil(t, err)
  27. // Init eth client
  28. ehtClientDialURL := os.Getenv("ETHCLIENT_DIAL_URL")
  29. ethClient, err := ethclient.Dial(ehtClientDialURL)
  30. require.Nil(t, err)
  31. client := eth.NewClient(ethClient, nil, nil, nil)
  32. // Create Synchronizer
  33. s := NewSynchronizer(client, historyDB, sdb)
  34. require.NotNil(t, s)
  35. // Test Sync
  36. // err = s.Sync()
  37. // require.Nil(t, err)
  38. // TODO: Reorg will be properly tested once we have the mock ethClient implemented
  39. /*
  40. // Force a Reorg
  41. lastSavedBlock, err := historyDB.GetLastBlock()
  42. require.Nil(t, err)
  43. lastSavedBlock.EthBlockNum++
  44. err = historyDB.AddBlock(lastSavedBlock)
  45. require.Nil(t, err)
  46. lastSavedBlock.EthBlockNum++
  47. err = historyDB.AddBlock(lastSavedBlock)
  48. require.Nil(t, err)
  49. log.Debugf("Wait for the blockchain to generate some blocks...")
  50. time.Sleep(40 * time.Second)
  51. err = s.Sync()
  52. require.Nil(t, err)
  53. */
  54. }