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.

151 lines
6.0 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
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 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. // Update Metrics needs api.status.Network.LastBatch.BatchNum to be updated
  59. lastBlock := tc.blocks[3]
  60. lastBatchNum := common.BatchNum(3)
  61. currentSlotNum := int64(1)
  62. err := api.UpdateNetworkInfo(lastBlock, lastBlock, lastBatchNum, currentSlotNum)
  63. assert.NoError(t, err)
  64. err = api.UpdateMetrics()
  65. assert.NoError(t, err)
  66. assert.Greater(t, api.status.Metrics.TransactionsPerBatch, float64(0))
  67. assert.Greater(t, api.status.Metrics.BatchFrequency, float64(0))
  68. assert.Greater(t, api.status.Metrics.TransactionsPerSecond, float64(0))
  69. assert.Greater(t, api.status.Metrics.TotalAccounts, int64(0))
  70. assert.Greater(t, api.status.Metrics.TotalBJJs, int64(0))
  71. assert.Greater(t, api.status.Metrics.AvgTransactionFee, float64(0))
  72. }
  73. func TestUpdateRecommendedFee(t *testing.T) {
  74. err := api.UpdateRecommendedFee()
  75. assert.NoError(t, err)
  76. assert.Greater(t, api.status.RecommendedFee.ExistingAccount, float64(0))
  77. assert.Equal(t, api.status.RecommendedFee.CreatesAccount,
  78. api.status.RecommendedFee.ExistingAccount*createAccountExtraFeePercentage)
  79. assert.Equal(t, api.status.RecommendedFee.CreatesAccountAndRegister,
  80. api.status.RecommendedFee.ExistingAccount*createAccountInternalExtraFeePercentage)
  81. }
  82. func TestGetState(t *testing.T) {
  83. lastBlock := tc.blocks[3]
  84. lastBatchNum := common.BatchNum(3)
  85. currentSlotNum := int64(1)
  86. api.SetRollupVariables(tc.rollupVars)
  87. api.SetWDelayerVariables(tc.wdelayerVars)
  88. api.SetAuctionVariables(tc.auctionVars)
  89. err := api.UpdateNetworkInfo(lastBlock, lastBlock, lastBatchNum, currentSlotNum)
  90. assert.NoError(t, err)
  91. err = api.UpdateMetrics()
  92. assert.NoError(t, err)
  93. err = api.UpdateRecommendedFee()
  94. assert.NoError(t, err)
  95. endpoint := apiURL + "state"
  96. var status testStatus
  97. assert.NoError(t, doGoodReq("GET", endpoint, nil, &status))
  98. // SC vars
  99. assert.Equal(t, tc.rollupVars, status.Rollup)
  100. assert.Equal(t, tc.auctionVars, status.Auction)
  101. assert.Equal(t, tc.wdelayerVars, status.WithdrawalDelayer)
  102. // Network
  103. assert.Equal(t, lastBlock.Num, status.Network.LastEthBlock)
  104. assert.Equal(t, lastBlock.Num, status.Network.LastSyncBlock)
  105. // TODO: assert all the batch, not just the batch num
  106. assert.Equal(t, lastBatchNum, status.Network.LastBatch.BatchNum)
  107. assert.Equal(t, currentSlotNum, status.Network.CurrentSlot)
  108. assertNextForgers(t, tc.nextForgers, status.Network.NextForgers)
  109. // Metrics
  110. // TODO: perform real asserts (not just greater than 0)
  111. assert.Greater(t, status.Metrics.TransactionsPerBatch, float64(0))
  112. assert.Greater(t, status.Metrics.BatchFrequency, float64(0))
  113. assert.Greater(t, status.Metrics.TransactionsPerSecond, float64(0))
  114. assert.Greater(t, status.Metrics.TotalAccounts, int64(0))
  115. assert.Greater(t, status.Metrics.TotalBJJs, int64(0))
  116. assert.Greater(t, status.Metrics.AvgTransactionFee, float64(0))
  117. // Recommended fee
  118. // TODO: perform real asserts (not just greater than 0)
  119. assert.Greater(t, status.RecommendedFee.ExistingAccount, float64(0))
  120. assert.Equal(t, status.RecommendedFee.CreatesAccount,
  121. status.RecommendedFee.ExistingAccount*createAccountExtraFeePercentage)
  122. assert.Equal(t, status.RecommendedFee.CreatesAccountAndRegister,
  123. status.RecommendedFee.ExistingAccount*createAccountInternalExtraFeePercentage)
  124. }
  125. func assertNextForgers(t *testing.T, expected, actual []NextForger) {
  126. assert.Equal(t, len(expected), len(actual))
  127. for i := range expected {
  128. // ignore timestamps and other metadata
  129. actual[i].Period.FromTimestamp = expected[i].Period.FromTimestamp
  130. actual[i].Period.ToTimestamp = expected[i].Period.ToTimestamp
  131. actual[i].Coordinator.ItemID = expected[i].Coordinator.ItemID
  132. actual[i].Coordinator.EthBlockNum = expected[i].Coordinator.EthBlockNum
  133. assert.Equal(t, expected[i], actual[i])
  134. }
  135. }