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.

224 lines
9.1 KiB

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