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.

223 lines
9.2 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/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 historydb.AuctionVariablesAPI `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. assertEqualAuctionVariables(t, *auctionVars, api.status.Auction)
  55. api.SetAuctionVariables(tc.auctionVars)
  56. assertEqualAuctionVariables(t, tc.auctionVars, api.status.Auction)
  57. }
  58. func assertEqualAuctionVariables(t *testing.T, auctionVariables common.AuctionVariables, apiVariables historydb.AuctionVariablesAPI) {
  59. assert.Equal(t, auctionVariables.EthBlockNum, apiVariables.EthBlockNum)
  60. assert.Equal(t, auctionVariables.DonationAddress, apiVariables.DonationAddress)
  61. assert.Equal(t, auctionVariables.BootCoordinator, apiVariables.BootCoordinator)
  62. assert.Equal(t, auctionVariables.BootCoordinatorURL, apiVariables.BootCoordinatorURL)
  63. assert.Equal(t, auctionVariables.DefaultSlotSetBidSlotNum, apiVariables.DefaultSlotSetBidSlotNum)
  64. assert.Equal(t, auctionVariables.ClosedAuctionSlots, apiVariables.ClosedAuctionSlots)
  65. assert.Equal(t, auctionVariables.OpenAuctionSlots, apiVariables.OpenAuctionSlots)
  66. assert.Equal(t, auctionVariables.Outbidding, apiVariables.Outbidding)
  67. assert.Equal(t, auctionVariables.SlotDeadline, apiVariables.SlotDeadline)
  68. for i, slot := range auctionVariables.DefaultSlotSetBid {
  69. assert.Equal(t, apitypes.NewBigIntStr(slot), apiVariables.DefaultSlotSetBid[i])
  70. }
  71. for i, ratio := range auctionVariables.AllocationRatio {
  72. assert.Equal(t, ratio, apiVariables.AllocationRatio[i])
  73. }
  74. }
  75. func TestUpdateNetworkInfo(t *testing.T) {
  76. status := &Network{}
  77. assert.Equal(t, status.LastSyncBlock, api.status.Network.LastSyncBlock)
  78. assert.Equal(t, status.LastBatch, api.status.Network.LastBatch)
  79. assert.Equal(t, status.CurrentSlot, api.status.Network.CurrentSlot)
  80. assert.Equal(t, status.NextForgers, api.status.Network.NextForgers)
  81. lastBlock := tc.blocks[3]
  82. lastBatchNum := common.BatchNum(3)
  83. currentSlotNum := int64(1)
  84. // Generate some bucket_update data
  85. bucketUpdates := []common.BucketUpdate{
  86. {
  87. EthBlockNum: 4,
  88. NumBucket: 0,
  89. BlockStamp: 4,
  90. Withdrawals: big.NewInt(123),
  91. },
  92. {
  93. EthBlockNum: 5,
  94. NumBucket: 2,
  95. BlockStamp: 5,
  96. Withdrawals: big.NewInt(42),
  97. },
  98. {
  99. EthBlockNum: 5,
  100. NumBucket: 2, // Repeated bucket
  101. BlockStamp: 5,
  102. Withdrawals: big.NewInt(43),
  103. },
  104. }
  105. err := api.h.AddBucketUpdatesTest(api.h.DB(), bucketUpdates)
  106. require.NoError(t, err)
  107. err = api.UpdateNetworkInfo(lastBlock, lastBlock, lastBatchNum, currentSlotNum)
  108. assert.NoError(t, err)
  109. assert.Equal(t, lastBlock.Num, api.status.Network.LastSyncBlock)
  110. assert.Equal(t, lastBatchNum, api.status.Network.LastBatch.BatchNum)
  111. assert.Equal(t, currentSlotNum, api.status.Network.CurrentSlot)
  112. assert.Equal(t, int(api.status.Auction.ClosedAuctionSlots)+1, len(api.status.Network.NextForgers))
  113. assert.Equal(t, api.status.Rollup.Buckets[0].Withdrawals, apitypes.NewBigIntStr(big.NewInt(123)))
  114. assert.Equal(t, api.status.Rollup.Buckets[2].Withdrawals, apitypes.NewBigIntStr(big.NewInt(43)))
  115. }
  116. func TestUpdateMetrics(t *testing.T) {
  117. // Update Metrics needs api.status.Network.LastBatch.BatchNum to be updated
  118. lastBlock := tc.blocks[3]
  119. lastBatchNum := common.BatchNum(3)
  120. currentSlotNum := int64(1)
  121. err := api.UpdateNetworkInfo(lastBlock, lastBlock, lastBatchNum, currentSlotNum)
  122. assert.NoError(t, err)
  123. err = api.UpdateMetrics()
  124. assert.NoError(t, err)
  125. assert.Greater(t, api.status.Metrics.TransactionsPerBatch, float64(0))
  126. assert.Greater(t, api.status.Metrics.BatchFrequency, float64(0))
  127. assert.Greater(t, api.status.Metrics.TransactionsPerSecond, float64(0))
  128. assert.Greater(t, api.status.Metrics.TotalAccounts, int64(0))
  129. assert.Greater(t, api.status.Metrics.TotalBJJs, int64(0))
  130. assert.Greater(t, api.status.Metrics.AvgTransactionFee, float64(0))
  131. }
  132. func TestUpdateRecommendedFee(t *testing.T) {
  133. err := api.UpdateRecommendedFee()
  134. assert.NoError(t, err)
  135. var minFeeUSD float64
  136. if api.l2 != nil {
  137. minFeeUSD = api.l2.MinFeeUSD()
  138. }
  139. assert.Greater(t, api.status.RecommendedFee.ExistingAccount, minFeeUSD)
  140. assert.Equal(t, api.status.RecommendedFee.CreatesAccount,
  141. api.status.RecommendedFee.ExistingAccount*createAccountExtraFeePercentage)
  142. assert.Equal(t, api.status.RecommendedFee.CreatesAccountAndRegister,
  143. api.status.RecommendedFee.ExistingAccount*createAccountInternalExtraFeePercentage)
  144. }
  145. func TestGetState(t *testing.T) {
  146. lastBlock := tc.blocks[3]
  147. lastBatchNum := common.BatchNum(3)
  148. currentSlotNum := int64(1)
  149. api.SetRollupVariables(tc.rollupVars)
  150. api.SetWDelayerVariables(tc.wdelayerVars)
  151. api.SetAuctionVariables(tc.auctionVars)
  152. err := api.UpdateNetworkInfo(lastBlock, lastBlock, lastBatchNum, currentSlotNum)
  153. assert.NoError(t, err)
  154. err = api.UpdateMetrics()
  155. assert.NoError(t, err)
  156. err = api.UpdateRecommendedFee()
  157. assert.NoError(t, err)
  158. endpoint := apiURL + "state"
  159. var status testStatus
  160. assert.NoError(t, doGoodReq("GET", endpoint, nil, &status))
  161. // SC vars
  162. // UpdateNetworkInfo will overwrite buckets withdrawal values
  163. // So they won't be checked here, they are checked at
  164. // TestUpdateNetworkInfo
  165. assertEqualRollupVariables(t, tc.rollupVars, status.Rollup, false)
  166. assertEqualAuctionVariables(t, tc.auctionVars, status.Auction)
  167. assert.Equal(t, tc.wdelayerVars, status.WithdrawalDelayer)
  168. // Network
  169. assert.Equal(t, lastBlock.Num, status.Network.LastEthBlock)
  170. assert.Equal(t, lastBlock.Num, status.Network.LastSyncBlock)
  171. // TODO: assert all the batch, not just the batch num
  172. assert.Equal(t, lastBatchNum, status.Network.LastBatch.BatchNum)
  173. assert.Equal(t, currentSlotNum, status.Network.CurrentSlot)
  174. assertNextForgers(t, tc.nextForgers, status.Network.NextForgers)
  175. // Metrics
  176. // TODO: perform real asserts (not just greater than 0)
  177. assert.Greater(t, status.Metrics.TransactionsPerBatch, float64(0))
  178. assert.Greater(t, status.Metrics.BatchFrequency, float64(0))
  179. assert.Greater(t, status.Metrics.TransactionsPerSecond, float64(0))
  180. assert.Greater(t, status.Metrics.TotalAccounts, int64(0))
  181. assert.Greater(t, status.Metrics.TotalBJJs, int64(0))
  182. assert.Greater(t, status.Metrics.AvgTransactionFee, float64(0))
  183. // Recommended fee
  184. // TODO: perform real asserts (not just greater than 0)
  185. assert.Greater(t, status.RecommendedFee.ExistingAccount, float64(0))
  186. assert.Equal(t, status.RecommendedFee.CreatesAccount,
  187. status.RecommendedFee.ExistingAccount*createAccountExtraFeePercentage)
  188. assert.Equal(t, status.RecommendedFee.CreatesAccountAndRegister,
  189. status.RecommendedFee.ExistingAccount*createAccountInternalExtraFeePercentage)
  190. }
  191. func assertNextForgers(t *testing.T, expected, actual []NextForger) {
  192. assert.Equal(t, len(expected), len(actual))
  193. for i := range expected {
  194. // ignore timestamps and other metadata
  195. actual[i].Period.FromTimestamp = expected[i].Period.FromTimestamp
  196. actual[i].Period.ToTimestamp = expected[i].Period.ToTimestamp
  197. actual[i].Coordinator.ItemID = expected[i].Coordinator.ItemID
  198. actual[i].Coordinator.EthBlockNum = expected[i].Coordinator.EthBlockNum
  199. assert.Equal(t, expected[i], actual[i])
  200. }
  201. }