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.

152 lines
6.1 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.
3 years ago
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.
3 years ago
  1. package api
  2. import (
  3. "testing"
  4. "github.com/hermeznetwork/hermez-node/common"
  5. "github.com/hermeznetwork/hermez-node/db/historydb"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. type testStatus struct {
  9. Network testNetwork `json:"network"`
  10. Metrics historydb.Metrics `json:"metrics"`
  11. Rollup common.RollupVariables `json:"rollup"`
  12. Auction common.AuctionVariables `json:"auction"`
  13. WithdrawalDelayer common.WDelayerVariables `json:"withdrawalDelayer"`
  14. RecommendedFee common.RecommendedFee `json:"recommendedFee"`
  15. }
  16. type testNetwork struct {
  17. LastEthBlock int64 `json:"lastEthereumBlock"`
  18. LastSyncBlock int64 `json:"lastSynchedBlock"`
  19. LastBatch testBatch `json:"lastBatch"`
  20. CurrentSlot int64 `json:"currentSlot"`
  21. NextForgers []NextForger `json:"nextForgers"`
  22. }
  23. func TestSetRollupVariables(t *testing.T) {
  24. rollupVars := &common.RollupVariables{}
  25. assert.Equal(t, *rollupVars, api.status.Rollup)
  26. api.SetRollupVariables(tc.rollupVars)
  27. assert.Equal(t, tc.rollupVars, api.status.Rollup)
  28. }
  29. func TestSetWDelayerVariables(t *testing.T) {
  30. wdelayerVars := &common.WDelayerVariables{}
  31. assert.Equal(t, *wdelayerVars, api.status.WithdrawalDelayer)
  32. api.SetWDelayerVariables(tc.wdelayerVars)
  33. assert.Equal(t, tc.wdelayerVars, api.status.WithdrawalDelayer)
  34. }
  35. func TestSetAuctionVariables(t *testing.T) {
  36. auctionVars := &common.AuctionVariables{}
  37. assert.Equal(t, *auctionVars, api.status.Auction)
  38. api.SetAuctionVariables(tc.auctionVars)
  39. assert.Equal(t, tc.auctionVars, api.status.Auction)
  40. }
  41. func TestUpdateNetworkInfo(t *testing.T) {
  42. status := &Network{}
  43. assert.Equal(t, status.LastSyncBlock, api.status.Network.LastSyncBlock)
  44. assert.Equal(t, status.LastBatch, api.status.Network.LastBatch)
  45. assert.Equal(t, status.CurrentSlot, api.status.Network.CurrentSlot)
  46. assert.Equal(t, status.NextForgers, api.status.Network.NextForgers)
  47. lastBlock := tc.blocks[3]
  48. lastBatchNum := common.BatchNum(3)
  49. currentSlotNum := int64(1)
  50. err := api.UpdateNetworkInfo(lastBlock, lastBlock, lastBatchNum, currentSlotNum)
  51. assert.NoError(t, err)
  52. assert.Equal(t, lastBlock.Num, api.status.Network.LastSyncBlock)
  53. assert.Equal(t, lastBatchNum, api.status.Network.LastBatch.BatchNum)
  54. assert.Equal(t, currentSlotNum, api.status.Network.CurrentSlot)
  55. assert.Equal(t, int(api.status.Auction.ClosedAuctionSlots)+1, len(api.status.Network.NextForgers))
  56. }
  57. func TestUpdateMetrics(t *testing.T) {
  58. // TODO: Improve checks when til is integrated
  59. // Update Metrics needs api.status.Network.LastBatch.BatchNum to be updated
  60. lastBlock := tc.blocks[3]
  61. lastBatchNum := common.BatchNum(3)
  62. currentSlotNum := int64(1)
  63. err := api.UpdateNetworkInfo(lastBlock, lastBlock, lastBatchNum, currentSlotNum)
  64. assert.NoError(t, err)
  65. err = api.UpdateMetrics()
  66. assert.NoError(t, err)
  67. assert.Greater(t, api.status.Metrics.TransactionsPerBatch, float64(0))
  68. assert.Greater(t, api.status.Metrics.BatchFrequency, float64(0))
  69. assert.Greater(t, api.status.Metrics.TransactionsPerBatch, float64(0))
  70. assert.Greater(t, api.status.Metrics.TotalAccounts, int64(0))
  71. assert.Greater(t, api.status.Metrics.TotalBJJs, int64(0))
  72. assert.Greater(t, api.status.Metrics.AvgTransactionFee, float64(0))
  73. }
  74. func TestUpdateRecommendedFee(t *testing.T) {
  75. err := api.UpdateRecommendedFee()
  76. assert.NoError(t, err)
  77. assert.Greater(t, api.status.RecommendedFee.ExistingAccount, float64(0))
  78. assert.Equal(t, api.status.RecommendedFee.CreatesAccount,
  79. api.status.RecommendedFee.ExistingAccount*createAccountExtraFeePercentage)
  80. assert.Equal(t, api.status.RecommendedFee.CreatesAccountAndRegister,
  81. api.status.RecommendedFee.ExistingAccount*createAccountInternalExtraFeePercentage)
  82. }
  83. func TestGetState(t *testing.T) {
  84. lastBlock := tc.blocks[3]
  85. lastBatchNum := common.BatchNum(3)
  86. currentSlotNum := int64(1)
  87. api.SetRollupVariables(tc.rollupVars)
  88. api.SetWDelayerVariables(tc.wdelayerVars)
  89. api.SetAuctionVariables(tc.auctionVars)
  90. err := api.UpdateNetworkInfo(lastBlock, lastBlock, lastBatchNum, currentSlotNum)
  91. assert.NoError(t, err)
  92. err = api.UpdateMetrics()
  93. assert.NoError(t, err)
  94. err = api.UpdateRecommendedFee()
  95. assert.NoError(t, err)
  96. endpoint := apiURL + "state"
  97. var status testStatus
  98. assert.NoError(t, doGoodReq("GET", endpoint, nil, &status))
  99. // SC vars
  100. assert.Equal(t, tc.rollupVars, status.Rollup)
  101. assert.Equal(t, tc.auctionVars, status.Auction)
  102. assert.Equal(t, tc.wdelayerVars, status.WithdrawalDelayer)
  103. // Network
  104. assert.Equal(t, lastBlock.Num, status.Network.LastEthBlock)
  105. assert.Equal(t, lastBlock.Num, status.Network.LastSyncBlock)
  106. // TODO: assert all the batch, not just the batch num
  107. assert.Equal(t, lastBatchNum, status.Network.LastBatch.BatchNum)
  108. assert.Equal(t, currentSlotNum, status.Network.CurrentSlot)
  109. assertNextForgers(t, tc.nextForgers, status.Network.NextForgers)
  110. // Metrics
  111. // TODO: perform real asserts (not just greater than 0)
  112. assert.Greater(t, status.Metrics.TransactionsPerBatch, float64(0))
  113. assert.Greater(t, status.Metrics.BatchFrequency, float64(0))
  114. assert.Greater(t, status.Metrics.TransactionsPerBatch, float64(0))
  115. assert.Greater(t, status.Metrics.TotalAccounts, int64(0))
  116. assert.Greater(t, status.Metrics.TotalBJJs, int64(0))
  117. assert.Greater(t, status.Metrics.AvgTransactionFee, float64(0))
  118. // Recommended fee
  119. // TODO: perform real asserts (not just greater than 0)
  120. assert.Greater(t, status.RecommendedFee.ExistingAccount, float64(0))
  121. assert.Equal(t, status.RecommendedFee.CreatesAccount,
  122. status.RecommendedFee.ExistingAccount*createAccountExtraFeePercentage)
  123. assert.Equal(t, status.RecommendedFee.CreatesAccountAndRegister,
  124. status.RecommendedFee.ExistingAccount*createAccountInternalExtraFeePercentage)
  125. }
  126. func assertNextForgers(t *testing.T, expected, actual []NextForger) {
  127. assert.Equal(t, len(expected), len(actual))
  128. for i := range expected {
  129. // ignore timestamps and other metadata
  130. actual[i].Period.FromTimestamp = expected[i].Period.FromTimestamp
  131. actual[i].Period.ToTimestamp = expected[i].Period.ToTimestamp
  132. actual[i].Coordinator.ItemID = expected[i].Coordinator.ItemID
  133. actual[i].Coordinator.EthBlockNum = expected[i].Coordinator.EthBlockNum
  134. assert.Equal(t, expected[i], actual[i])
  135. }
  136. }