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.

88 lines
2.3 KiB

Update coordinator, call all api update functions - Common: - Rename Block.EthBlockNum to Block.Num to avoid unneeded repetition - API: - Add UpdateNetworkInfoBlock to update just block information, to be used when the node is not yet synchronized - Node: - Call API.UpdateMetrics and UpdateRecommendedFee in a loop, with configurable time intervals - Synchronizer: - When mapping events by TxHash, use an array to support the possibility of multiple calls of the same function happening in the same transaction (for example, a smart contract in a single transaction could call withdraw with delay twice, which would generate 2 withdraw events, and 2 deposit events). - In Stats, keep entire LastBlock instead of just the blockNum - In Stats, add lastL1BatchBlock - Test Stats and SCVars - Coordinator: - Enable writing the BatchInfo in every step of the pipeline to disk (with JSON text files) for debugging purposes. - Move the Pipeline functionality from the Coordinator to its own struct (Pipeline) - Implement shouldL1lL2Batch - In TxManager, implement logic to perform several attempts when doing ethereum node RPC calls before considering the error. (Both for calls to forgeBatch and transaction receipt) - In TxManager, reorganize the flow and note the specific points in which actions are made when err != nil - HistoryDB: - Implement GetLastL1BatchBlockNum: returns the blockNum of the latest forged l1Batch, to help the coordinator decide when to forge an L1Batch. - EthereumClient and test.Client: - Update EthBlockByNumber to return the last block when the passed number is -1.
4 years ago
  1. package priceupdater
  2. import (
  3. "context"
  4. "os"
  5. "testing"
  6. ethCommon "github.com/ethereum/go-ethereum/common"
  7. "github.com/hermeznetwork/hermez-node/common"
  8. dbUtils "github.com/hermeznetwork/hermez-node/db"
  9. "github.com/hermeznetwork/hermez-node/db/historydb"
  10. "github.com/hermeznetwork/hermez-node/test"
  11. "github.com/stretchr/testify/assert"
  12. "github.com/stretchr/testify/require"
  13. )
  14. var historyDB *historydb.HistoryDB
  15. func TestMain(m *testing.M) {
  16. // Init DB
  17. pass := os.Getenv("POSTGRES_PASS")
  18. db, err := dbUtils.InitSQLDB(5432, "localhost", "hermez", pass, "hermez")
  19. if err != nil {
  20. panic(err)
  21. }
  22. historyDB = historydb.NewHistoryDB(db, db, nil)
  23. // Clean DB
  24. test.WipeDB(historyDB.DB())
  25. // Populate DB
  26. // Gen blocks and add them to DB
  27. blocks := test.GenBlocks(1, 2)
  28. err = historyDB.AddBlocks(blocks)
  29. if err != nil {
  30. panic(err)
  31. }
  32. // Gen tokens and add them to DB
  33. tokens := []common.Token{}
  34. tokens = append(tokens, common.Token{
  35. TokenID: 1,
  36. EthBlockNum: blocks[0].Num,
  37. EthAddr: ethCommon.HexToAddress("0x6b175474e89094c44da98b954eedeac495271d0f"),
  38. Name: "DAI",
  39. Symbol: "DAI",
  40. Decimals: 18,
  41. })
  42. err = historyDB.AddTokens(tokens)
  43. if err != nil {
  44. panic(err)
  45. }
  46. result := m.Run()
  47. os.Exit(result)
  48. }
  49. func TestPriceUpdaterBitfinex(t *testing.T) {
  50. // Init price updater
  51. pu, err := NewPriceUpdater("https://api-pub.bitfinex.com/v2/", APITypeBitFinexV2, historyDB)
  52. require.NoError(t, err)
  53. // Update token list
  54. assert.NoError(t, pu.UpdateTokenList())
  55. // Update prices
  56. pu.UpdatePrices(context.Background())
  57. assertTokenHasPriceAndClean(t)
  58. }
  59. func TestPriceUpdaterCoingecko(t *testing.T) {
  60. // Init price updater
  61. pu, err := NewPriceUpdater("https://api.coingecko.com/api/v3/", APITypeCoingeckoV3, historyDB)
  62. require.NoError(t, err)
  63. // Update token list
  64. assert.NoError(t, pu.UpdateTokenList())
  65. // Update prices
  66. pu.UpdatePrices(context.Background())
  67. assertTokenHasPriceAndClean(t)
  68. }
  69. func assertTokenHasPriceAndClean(t *testing.T) {
  70. // Check that prices have been updated
  71. fetchedTokens, err := historyDB.GetTokensTest()
  72. require.NoError(t, err)
  73. // TokenID 0 (ETH) is always on the DB
  74. assert.Equal(t, 2, len(fetchedTokens))
  75. for _, token := range fetchedTokens {
  76. require.NotNil(t, token.USD)
  77. require.NotNil(t, token.USDUpdate)
  78. assert.Greater(t, *token.USD, 0.0)
  79. }
  80. }