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.

184 lines
6.9 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. "math/big"
  4. "testing"
  5. "github.com/hermeznetwork/hermez-node/common"
  6. "github.com/hermeznetwork/hermez-node/db/historydb"
  7. "github.com/stretchr/testify/assert"
  8. "github.com/stretchr/testify/require"
  9. )
  10. type testStatus struct {
  11. Network testNetwork `json:"network"`
  12. Metrics historydb.Metrics `json:"metrics"`
  13. Rollup common.RollupVariables `json:"rollup"`
  14. Auction common.AuctionVariables `json:"auction"`
  15. WithdrawalDelayer common.WDelayerVariables `json:"withdrawalDelayer"`
  16. RecommendedFee common.RecommendedFee `json:"recommendedFee"`
  17. }
  18. type testNetwork struct {
  19. LastEthBlock int64 `json:"lastEthereumBlock"`
  20. LastSyncBlock int64 `json:"lastSynchedBlock"`
  21. LastBatch testBatch `json:"lastBatch"`
  22. CurrentSlot int64 `json:"currentSlot"`
  23. NextForgers []NextForger `json:"nextForgers"`
  24. }
  25. func TestSetRollupVariables(t *testing.T) {
  26. rollupVars := &common.RollupVariables{}
  27. assert.Equal(t, *rollupVars, api.status.Rollup)
  28. api.SetRollupVariables(tc.rollupVars)
  29. assert.Equal(t, tc.rollupVars, api.status.Rollup)
  30. }
  31. func TestSetWDelayerVariables(t *testing.T) {
  32. wdelayerVars := &common.WDelayerVariables{}
  33. assert.Equal(t, *wdelayerVars, api.status.WithdrawalDelayer)
  34. api.SetWDelayerVariables(tc.wdelayerVars)
  35. assert.Equal(t, tc.wdelayerVars, api.status.WithdrawalDelayer)
  36. }
  37. func TestSetAuctionVariables(t *testing.T) {
  38. auctionVars := &common.AuctionVariables{}
  39. assert.Equal(t, *auctionVars, api.status.Auction)
  40. api.SetAuctionVariables(tc.auctionVars)
  41. assert.Equal(t, tc.auctionVars, api.status.Auction)
  42. }
  43. func TestUpdateNetworkInfo(t *testing.T) {
  44. status := &Network{}
  45. assert.Equal(t, status.LastSyncBlock, api.status.Network.LastSyncBlock)
  46. assert.Equal(t, status.LastBatch, api.status.Network.LastBatch)
  47. assert.Equal(t, status.CurrentSlot, api.status.Network.CurrentSlot)
  48. assert.Equal(t, status.NextForgers, api.status.Network.NextForgers)
  49. lastBlock := tc.blocks[3]
  50. lastBatchNum := common.BatchNum(3)
  51. currentSlotNum := int64(1)
  52. // Generate some bucket_update data
  53. bucketUpdates := []common.BucketUpdate{
  54. {
  55. EthBlockNum: 4,
  56. NumBucket: 0,
  57. BlockStamp: 4,
  58. Withdrawals: big.NewInt(123),
  59. },
  60. {
  61. EthBlockNum: 5,
  62. NumBucket: 2,
  63. BlockStamp: 5,
  64. Withdrawals: big.NewInt(42),
  65. },
  66. {
  67. EthBlockNum: 5,
  68. NumBucket: 2, // Repeated bucket
  69. BlockStamp: 5,
  70. Withdrawals: big.NewInt(43),
  71. },
  72. }
  73. err := api.h.AddBucketUpdatesTest(api.h.DB(), bucketUpdates)
  74. require.NoError(t, err)
  75. err = api.UpdateNetworkInfo(lastBlock, lastBlock, lastBatchNum, currentSlotNum)
  76. assert.NoError(t, err)
  77. assert.Equal(t, lastBlock.Num, api.status.Network.LastSyncBlock)
  78. assert.Equal(t, lastBatchNum, api.status.Network.LastBatch.BatchNum)
  79. assert.Equal(t, currentSlotNum, api.status.Network.CurrentSlot)
  80. assert.Equal(t, int(api.status.Auction.ClosedAuctionSlots)+1, len(api.status.Network.NextForgers))
  81. assert.Equal(t, api.status.Rollup.Buckets[0].Withdrawals, big.NewInt(123))
  82. assert.Equal(t, api.status.Rollup.Buckets[2].Withdrawals, big.NewInt(43))
  83. }
  84. func TestUpdateMetrics(t *testing.T) {
  85. // Update Metrics needs api.status.Network.LastBatch.BatchNum to be updated
  86. lastBlock := tc.blocks[3]
  87. lastBatchNum := common.BatchNum(3)
  88. currentSlotNum := int64(1)
  89. err := api.UpdateNetworkInfo(lastBlock, lastBlock, lastBatchNum, currentSlotNum)
  90. assert.NoError(t, err)
  91. err = api.UpdateMetrics()
  92. assert.NoError(t, err)
  93. assert.Greater(t, api.status.Metrics.TransactionsPerBatch, float64(0))
  94. assert.Greater(t, api.status.Metrics.BatchFrequency, float64(0))
  95. assert.Greater(t, api.status.Metrics.TransactionsPerSecond, float64(0))
  96. assert.Greater(t, api.status.Metrics.TotalAccounts, int64(0))
  97. assert.Greater(t, api.status.Metrics.TotalBJJs, int64(0))
  98. assert.Greater(t, api.status.Metrics.AvgTransactionFee, float64(0))
  99. }
  100. func TestUpdateRecommendedFee(t *testing.T) {
  101. err := api.UpdateRecommendedFee()
  102. assert.NoError(t, err)
  103. assert.Greater(t, api.status.RecommendedFee.ExistingAccount, float64(0))
  104. assert.Equal(t, api.status.RecommendedFee.CreatesAccount,
  105. api.status.RecommendedFee.ExistingAccount*createAccountExtraFeePercentage)
  106. assert.Equal(t, api.status.RecommendedFee.CreatesAccountAndRegister,
  107. api.status.RecommendedFee.ExistingAccount*createAccountInternalExtraFeePercentage)
  108. }
  109. func TestGetState(t *testing.T) {
  110. lastBlock := tc.blocks[3]
  111. lastBatchNum := common.BatchNum(3)
  112. currentSlotNum := int64(1)
  113. api.SetRollupVariables(tc.rollupVars)
  114. api.SetWDelayerVariables(tc.wdelayerVars)
  115. api.SetAuctionVariables(tc.auctionVars)
  116. err := api.UpdateNetworkInfo(lastBlock, lastBlock, lastBatchNum, currentSlotNum)
  117. assert.NoError(t, err)
  118. err = api.UpdateMetrics()
  119. assert.NoError(t, err)
  120. err = api.UpdateRecommendedFee()
  121. assert.NoError(t, err)
  122. endpoint := apiURL + "state"
  123. var status testStatus
  124. assert.NoError(t, doGoodReq("GET", endpoint, nil, &status))
  125. // SC vars
  126. // UpdateNetworkInfo will overwrite buckets withdrawal values
  127. // So we restore them before comparing, they are checked at
  128. // TestUpdateNetworkInfo
  129. status.Rollup.Buckets = tc.rollupVars.Buckets
  130. assert.Equal(t, tc.rollupVars, status.Rollup)
  131. assert.Equal(t, tc.auctionVars, status.Auction)
  132. assert.Equal(t, tc.wdelayerVars, status.WithdrawalDelayer)
  133. // Network
  134. assert.Equal(t, lastBlock.Num, status.Network.LastEthBlock)
  135. assert.Equal(t, lastBlock.Num, status.Network.LastSyncBlock)
  136. // TODO: assert all the batch, not just the batch num
  137. assert.Equal(t, lastBatchNum, status.Network.LastBatch.BatchNum)
  138. assert.Equal(t, currentSlotNum, status.Network.CurrentSlot)
  139. assertNextForgers(t, tc.nextForgers, status.Network.NextForgers)
  140. // Metrics
  141. // TODO: perform real asserts (not just greater than 0)
  142. assert.Greater(t, status.Metrics.TransactionsPerBatch, float64(0))
  143. assert.Greater(t, status.Metrics.BatchFrequency, float64(0))
  144. assert.Greater(t, status.Metrics.TransactionsPerSecond, float64(0))
  145. assert.Greater(t, status.Metrics.TotalAccounts, int64(0))
  146. assert.Greater(t, status.Metrics.TotalBJJs, int64(0))
  147. assert.Greater(t, status.Metrics.AvgTransactionFee, float64(0))
  148. // Recommended fee
  149. // TODO: perform real asserts (not just greater than 0)
  150. assert.Greater(t, status.RecommendedFee.ExistingAccount, float64(0))
  151. assert.Equal(t, status.RecommendedFee.CreatesAccount,
  152. status.RecommendedFee.ExistingAccount*createAccountExtraFeePercentage)
  153. assert.Equal(t, status.RecommendedFee.CreatesAccountAndRegister,
  154. status.RecommendedFee.ExistingAccount*createAccountInternalExtraFeePercentage)
  155. }
  156. func assertNextForgers(t *testing.T, expected, actual []NextForger) {
  157. assert.Equal(t, len(expected), len(actual))
  158. for i := range expected {
  159. // ignore timestamps and other metadata
  160. actual[i].Period.FromTimestamp = expected[i].Period.FromTimestamp
  161. actual[i].Period.ToTimestamp = expected[i].Period.ToTimestamp
  162. actual[i].Coordinator.ItemID = expected[i].Coordinator.ItemID
  163. actual[i].Coordinator.EthBlockNum = expected[i].Coordinator.EthBlockNum
  164. assert.Equal(t, expected[i], actual[i])
  165. }
  166. }