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.

165 lines
6.8 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
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. "testing"
  4. "time"
  5. "github.com/hermeznetwork/hermez-node/common"
  6. "github.com/hermeznetwork/hermez-node/db/historydb"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. const secondsPerBlock = 15
  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 TestNextForgers(t *testing.T) {
  44. // It's assumed that bids for each slot will be received in increasing order
  45. bestBids := make(map[int64]testBid)
  46. for j := range tc.bids {
  47. bestBids[tc.bids[j].SlotNum] = tc.bids[j]
  48. }
  49. lastBlock := tc.blocks[len(tc.blocks)-1]
  50. for i := int64(0); i < tc.slots[len(tc.slots)-1].SlotNum; i++ {
  51. lastClosedSlot := i + int64(api.status.Auction.ClosedAuctionSlots)
  52. nextForgers, err := api.GetNextForgers(tc.blocks[len(tc.blocks)-1], i, lastClosedSlot)
  53. assert.NoError(t, err)
  54. for j := i; j <= lastClosedSlot; j++ {
  55. for q := range nextForgers {
  56. if nextForgers[q].Period.SlotNum == j {
  57. if nextForgers[q].Coordinator.ItemID != 0 {
  58. assert.Equal(t, bestBids[j].Bidder, nextForgers[q].Coordinator.Bidder)
  59. } else {
  60. assert.Equal(t, bootCoordinator.Bidder, nextForgers[q].Coordinator.Bidder)
  61. }
  62. firstBlockSlot, lastBlockSlot := api.getFirstLastBlock(j)
  63. fromTimestamp := lastBlock.Timestamp.Add(time.Second * time.Duration(secondsPerBlock*(firstBlockSlot-lastBlock.Num)))
  64. toTimestamp := lastBlock.Timestamp.Add(time.Second * time.Duration(secondsPerBlock*(lastBlockSlot-lastBlock.Num)))
  65. assert.Equal(t, fromTimestamp.Unix(), nextForgers[q].Period.FromTimestamp.Unix())
  66. assert.Equal(t, toTimestamp.Unix(), nextForgers[q].Period.ToTimestamp.Unix())
  67. }
  68. }
  69. }
  70. }
  71. }
  72. func TestUpdateNetworkInfo(t *testing.T) {
  73. status := &Network{}
  74. assert.Equal(t, status.LastSyncBlock, api.status.Network.LastSyncBlock)
  75. assert.Equal(t, status.LastBatch.BatchNum, api.status.Network.LastBatch.BatchNum)
  76. assert.Equal(t, status.CurrentSlot, api.status.Network.CurrentSlot)
  77. assert.Equal(t, status.NextForgers, api.status.Network.NextForgers)
  78. lastBlock := tc.blocks[3]
  79. lastBatchNum := common.BatchNum(3)
  80. currentSlotNum := int64(1)
  81. err := api.UpdateNetworkInfo(lastBlock, lastBlock, lastBatchNum, currentSlotNum)
  82. assert.NoError(t, err)
  83. assert.Equal(t, lastBlock.Num, api.status.Network.LastSyncBlock)
  84. assert.Equal(t, lastBatchNum, api.status.Network.LastBatch.BatchNum)
  85. assert.Equal(t, currentSlotNum, api.status.Network.CurrentSlot)
  86. assert.Equal(t, int(api.status.Auction.ClosedAuctionSlots)+1, len(api.status.Network.NextForgers))
  87. }
  88. func TestUpdateMetrics(t *testing.T) {
  89. // TODO: Improve checks when til is integrated
  90. // Update Metrics needs api.status.Network.LastBatch.BatchNum to be updated
  91. lastBlock := tc.blocks[3]
  92. lastBatchNum := common.BatchNum(3)
  93. currentSlotNum := int64(1)
  94. err := api.UpdateNetworkInfo(lastBlock, lastBlock, lastBatchNum, currentSlotNum)
  95. assert.NoError(t, err)
  96. err = api.UpdateMetrics()
  97. assert.NoError(t, err)
  98. assert.Greater(t, api.status.Metrics.TransactionsPerBatch, float64(0))
  99. assert.Greater(t, api.status.Metrics.BatchFrequency, float64(0))
  100. assert.Greater(t, api.status.Metrics.TransactionsPerBatch, float64(0))
  101. assert.Greater(t, api.status.Metrics.TotalAccounts, int64(0))
  102. assert.Greater(t, api.status.Metrics.TotalBJJs, int64(0))
  103. assert.Greater(t, api.status.Metrics.AvgTransactionFee, float64(0))
  104. }
  105. func TestUpdateRecommendedFee(t *testing.T) {
  106. err := api.UpdateRecommendedFee()
  107. assert.NoError(t, err)
  108. assert.Greater(t, api.status.RecommendedFee.ExistingAccount, float64(0))
  109. assert.Equal(t, api.status.RecommendedFee.CreatesAccount,
  110. api.status.RecommendedFee.ExistingAccount*createAccountExtraFeePercentage)
  111. assert.Equal(t, api.status.RecommendedFee.CreatesAccountAndRegister,
  112. api.status.RecommendedFee.ExistingAccount*createAccountInternalExtraFeePercentage)
  113. }
  114. func TestGetState(t *testing.T) {
  115. lastBlock := tc.blocks[3]
  116. lastBatchNum := common.BatchNum(3)
  117. currentSlotNum := int64(1)
  118. api.SetRollupVariables(tc.rollupVars)
  119. api.SetWDelayerVariables(tc.wdelayerVars)
  120. api.SetAuctionVariables(tc.auctionVars)
  121. err := api.UpdateNetworkInfo(lastBlock, lastBlock, lastBatchNum, currentSlotNum)
  122. assert.NoError(t, err)
  123. err = api.UpdateMetrics()
  124. assert.NoError(t, err)
  125. err = api.UpdateRecommendedFee()
  126. assert.NoError(t, err)
  127. endpoint := apiURL + "state"
  128. var status testStatus
  129. assert.NoError(t, doGoodReq("GET", endpoint, nil, &status))
  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. assert.Equal(t, lastBlock.Num, status.Network.LastEthBlock)
  134. assert.Equal(t, lastBlock.Num, status.Network.LastSyncBlock)
  135. assert.Equal(t, lastBatchNum, status.Network.LastBatch.BatchNum)
  136. assert.Equal(t, currentSlotNum, status.Network.CurrentSlot)
  137. assert.Equal(t, int(api.status.Auction.ClosedAuctionSlots)+1, len(status.Network.NextForgers))
  138. assert.Greater(t, status.Metrics.TransactionsPerBatch, float64(0))
  139. assert.Greater(t, status.Metrics.BatchFrequency, float64(0))
  140. assert.Greater(t, status.Metrics.TransactionsPerBatch, float64(0))
  141. assert.Greater(t, status.Metrics.TotalAccounts, int64(0))
  142. assert.Greater(t, status.Metrics.TotalBJJs, int64(0))
  143. assert.Greater(t, status.Metrics.AvgTransactionFee, float64(0))
  144. assert.Greater(t, status.RecommendedFee.ExistingAccount, float64(0))
  145. assert.Equal(t, status.RecommendedFee.CreatesAccount,
  146. status.RecommendedFee.ExistingAccount*createAccountExtraFeePercentage)
  147. assert.Equal(t, status.RecommendedFee.CreatesAccountAndRegister,
  148. status.RecommendedFee.ExistingAccount*createAccountInternalExtraFeePercentage)
  149. }