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.

219 lines
9.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.
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 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. assert.Greater(t, api.status.RecommendedFee.ExistingAccount, float64(0))
  136. assert.Equal(t, api.status.RecommendedFee.CreatesAccount,
  137. api.status.RecommendedFee.ExistingAccount*createAccountExtraFeePercentage)
  138. assert.Equal(t, api.status.RecommendedFee.CreatesAccountAndRegister,
  139. api.status.RecommendedFee.ExistingAccount*createAccountInternalExtraFeePercentage)
  140. }
  141. func TestGetState(t *testing.T) {
  142. lastBlock := tc.blocks[3]
  143. lastBatchNum := common.BatchNum(3)
  144. currentSlotNum := int64(1)
  145. api.SetRollupVariables(tc.rollupVars)
  146. api.SetWDelayerVariables(tc.wdelayerVars)
  147. api.SetAuctionVariables(tc.auctionVars)
  148. err := api.UpdateNetworkInfo(lastBlock, lastBlock, lastBatchNum, currentSlotNum)
  149. assert.NoError(t, err)
  150. err = api.UpdateMetrics()
  151. assert.NoError(t, err)
  152. err = api.UpdateRecommendedFee()
  153. assert.NoError(t, err)
  154. endpoint := apiURL + "state"
  155. var status testStatus
  156. assert.NoError(t, doGoodReq("GET", endpoint, nil, &status))
  157. // SC vars
  158. // UpdateNetworkInfo will overwrite buckets withdrawal values
  159. // So they won't be checked here, they are checked at
  160. // TestUpdateNetworkInfo
  161. assertEqualRollupVariables(t, tc.rollupVars, status.Rollup, false)
  162. assertEqualAuctionVariables(t, tc.auctionVars, status.Auction)
  163. assert.Equal(t, tc.wdelayerVars, status.WithdrawalDelayer)
  164. // Network
  165. assert.Equal(t, lastBlock.Num, status.Network.LastEthBlock)
  166. assert.Equal(t, lastBlock.Num, status.Network.LastSyncBlock)
  167. // TODO: assert all the batch, not just the batch num
  168. assert.Equal(t, lastBatchNum, status.Network.LastBatch.BatchNum)
  169. assert.Equal(t, currentSlotNum, status.Network.CurrentSlot)
  170. assertNextForgers(t, tc.nextForgers, status.Network.NextForgers)
  171. // Metrics
  172. // TODO: perform real asserts (not just greater than 0)
  173. assert.Greater(t, status.Metrics.TransactionsPerBatch, float64(0))
  174. assert.Greater(t, status.Metrics.BatchFrequency, float64(0))
  175. assert.Greater(t, status.Metrics.TransactionsPerSecond, float64(0))
  176. assert.Greater(t, status.Metrics.TotalAccounts, int64(0))
  177. assert.Greater(t, status.Metrics.TotalBJJs, int64(0))
  178. assert.Greater(t, status.Metrics.AvgTransactionFee, float64(0))
  179. // Recommended fee
  180. // TODO: perform real asserts (not just greater than 0)
  181. assert.Greater(t, status.RecommendedFee.ExistingAccount, float64(0))
  182. assert.Equal(t, status.RecommendedFee.CreatesAccount,
  183. status.RecommendedFee.ExistingAccount*createAccountExtraFeePercentage)
  184. assert.Equal(t, status.RecommendedFee.CreatesAccountAndRegister,
  185. status.RecommendedFee.ExistingAccount*createAccountInternalExtraFeePercentage)
  186. }
  187. func assertNextForgers(t *testing.T, expected, actual []NextForger) {
  188. assert.Equal(t, len(expected), len(actual))
  189. for i := range expected {
  190. // ignore timestamps and other metadata
  191. actual[i].Period.FromTimestamp = expected[i].Period.FromTimestamp
  192. actual[i].Period.ToTimestamp = expected[i].Period.ToTimestamp
  193. actual[i].Coordinator.ItemID = expected[i].Coordinator.ItemID
  194. actual[i].Coordinator.EthBlockNum = expected[i].Coordinator.EthBlockNum
  195. assert.Equal(t, expected[i], actual[i])
  196. }
  197. }