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.

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