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.

163 lines
6.6 KiB

  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. LastBlock int64 `json:"lastBlock"`
  20. LastBatch testBatch `json:"lastBatch"`
  21. CurrentSlot int64 `json:"currentSlot"`
  22. NextForgers []NextForger `json:"nextForgers"`
  23. }
  24. func TestSetRollupVariables(t *testing.T) {
  25. rollupVars := &common.RollupVariables{}
  26. assert.Equal(t, *rollupVars, api.status.Rollup)
  27. api.SetRollupVariables(tc.rollupVars)
  28. assert.Equal(t, tc.rollupVars, api.status.Rollup)
  29. }
  30. func TestSetWDelayerVariables(t *testing.T) {
  31. wdelayerVars := &common.WDelayerVariables{}
  32. assert.Equal(t, *wdelayerVars, api.status.WithdrawalDelayer)
  33. api.SetWDelayerVariables(tc.wdelayerVars)
  34. assert.Equal(t, tc.wdelayerVars, api.status.WithdrawalDelayer)
  35. }
  36. func TestSetAuctionVariables(t *testing.T) {
  37. auctionVars := &common.AuctionVariables{}
  38. assert.Equal(t, *auctionVars, api.status.Auction)
  39. api.SetAuctionVariables(tc.auctionVars)
  40. assert.Equal(t, tc.auctionVars, api.status.Auction)
  41. }
  42. func TestNextForgers(t *testing.T) {
  43. // It's assumed that bids for each slot will be received in increasing order
  44. bestBids := make(map[int64]testBid)
  45. for j := range tc.bids {
  46. bestBids[tc.bids[j].SlotNum] = tc.bids[j]
  47. }
  48. lastBlock := tc.blocks[len(tc.blocks)-1]
  49. for i := int64(0); i < tc.slots[len(tc.slots)-1].SlotNum; i++ {
  50. lastClosedSlot := i + int64(api.status.Auction.ClosedAuctionSlots)
  51. nextForgers, err := api.GetNextForgers(tc.blocks[len(tc.blocks)-1], i, lastClosedSlot)
  52. assert.NoError(t, err)
  53. for j := i; j <= lastClosedSlot; j++ {
  54. for q := range nextForgers {
  55. if nextForgers[q].Period.SlotNum == j {
  56. if nextForgers[q].Coordinator.ItemID != 0 {
  57. assert.Equal(t, bestBids[j].Bidder, nextForgers[q].Coordinator.Bidder)
  58. } else {
  59. assert.Equal(t, bootCoordinator.Bidder, nextForgers[q].Coordinator.Bidder)
  60. }
  61. firstBlockSlot, lastBlockSlot := api.getFirstLastBlock(j)
  62. fromTimestamp := lastBlock.Timestamp.Add(time.Second * time.Duration(secondsPerBlock*(firstBlockSlot-lastBlock.EthBlockNum)))
  63. toTimestamp := lastBlock.Timestamp.Add(time.Second * time.Duration(secondsPerBlock*(lastBlockSlot-lastBlock.EthBlockNum)))
  64. assert.Equal(t, fromTimestamp.Unix(), nextForgers[q].Period.FromTimestamp.Unix())
  65. assert.Equal(t, toTimestamp.Unix(), nextForgers[q].Period.ToTimestamp.Unix())
  66. }
  67. }
  68. }
  69. }
  70. }
  71. func TestUpdateNetworkInfo(t *testing.T) {
  72. status := &Network{}
  73. assert.Equal(t, status.LastBlock, api.status.Network.LastBlock)
  74. assert.Equal(t, status.LastBatch.BatchNum, api.status.Network.LastBatch.BatchNum)
  75. assert.Equal(t, status.CurrentSlot, api.status.Network.CurrentSlot)
  76. assert.Equal(t, status.NextForgers, api.status.Network.NextForgers)
  77. lastBlock := tc.blocks[3]
  78. lastBatchNum := common.BatchNum(3)
  79. currentSlotNum := int64(1)
  80. err := api.UpdateNetworkInfo(lastBlock, lastBatchNum, currentSlotNum)
  81. assert.NoError(t, err)
  82. assert.Equal(t, lastBlock.EthBlockNum, api.status.Network.LastBlock)
  83. assert.Equal(t, lastBatchNum, api.status.Network.LastBatch.BatchNum)
  84. assert.Equal(t, currentSlotNum, api.status.Network.CurrentSlot)
  85. assert.Equal(t, int(api.status.Auction.ClosedAuctionSlots)+1, len(api.status.Network.NextForgers))
  86. }
  87. func TestUpdateMetrics(t *testing.T) {
  88. // TODO: Improve checks when til is integrated
  89. // Update Metrics needs api.status.Network.LastBatch.BatchNum to be updated
  90. lastBlock := tc.blocks[3]
  91. lastBatchNum := common.BatchNum(3)
  92. currentSlotNum := int64(1)
  93. err := api.UpdateNetworkInfo(lastBlock, lastBatchNum, currentSlotNum)
  94. assert.NoError(t, err)
  95. err = api.UpdateMetrics()
  96. assert.NoError(t, err)
  97. assert.Greater(t, api.status.Metrics.TransactionsPerBatch, float64(0))
  98. assert.Greater(t, api.status.Metrics.BatchFrequency, float64(0))
  99. assert.Greater(t, api.status.Metrics.TransactionsPerBatch, float64(0))
  100. assert.Greater(t, api.status.Metrics.TotalAccounts, int64(0))
  101. assert.Greater(t, api.status.Metrics.TotalBJJs, int64(0))
  102. assert.Greater(t, api.status.Metrics.AvgTransactionFee, float64(0))
  103. }
  104. func TestUpdateRecommendedFee(t *testing.T) {
  105. err := api.UpdateRecommendedFee()
  106. assert.NoError(t, err)
  107. assert.Greater(t, api.status.RecommendedFee.ExistingAccount, float64(0))
  108. assert.Equal(t, api.status.RecommendedFee.CreatesAccount,
  109. api.status.RecommendedFee.ExistingAccount*createAccountExtraFeePercentage)
  110. assert.Equal(t, api.status.RecommendedFee.CreatesAccountAndRegister,
  111. api.status.RecommendedFee.ExistingAccount*createAccountInternalExtraFeePercentage)
  112. }
  113. func TestGetStatus(t *testing.T) {
  114. lastBlock := tc.blocks[3]
  115. lastBatchNum := common.BatchNum(3)
  116. currentSlotNum := int64(1)
  117. api.SetRollupVariables(tc.rollupVars)
  118. api.SetWDelayerVariables(tc.wdelayerVars)
  119. api.SetAuctionVariables(tc.auctionVars)
  120. err := api.UpdateNetworkInfo(lastBlock, lastBatchNum, currentSlotNum)
  121. assert.NoError(t, err)
  122. err = api.UpdateMetrics()
  123. assert.NoError(t, err)
  124. err = api.UpdateRecommendedFee()
  125. assert.NoError(t, err)
  126. endpoint := apiURL + "state"
  127. var status testStatus
  128. assert.NoError(t, doGoodReq("GET", endpoint, nil, &status))
  129. assert.Equal(t, tc.rollupVars, status.Rollup)
  130. assert.Equal(t, tc.auctionVars, status.Auction)
  131. assert.Equal(t, tc.wdelayerVars, status.WithdrawalDelayer)
  132. assert.Equal(t, lastBlock.EthBlockNum, status.Network.LastBlock)
  133. assert.Equal(t, lastBatchNum, status.Network.LastBatch.BatchNum)
  134. assert.Equal(t, currentSlotNum, status.Network.CurrentSlot)
  135. assert.Equal(t, int(api.status.Auction.ClosedAuctionSlots)+1, len(status.Network.NextForgers))
  136. assert.Greater(t, status.Metrics.TransactionsPerBatch, float64(0))
  137. assert.Greater(t, status.Metrics.BatchFrequency, float64(0))
  138. assert.Greater(t, status.Metrics.TransactionsPerBatch, float64(0))
  139. assert.Greater(t, status.Metrics.TotalAccounts, int64(0))
  140. assert.Greater(t, status.Metrics.TotalBJJs, int64(0))
  141. assert.Greater(t, status.Metrics.AvgTransactionFee, float64(0))
  142. assert.Greater(t, status.RecommendedFee.ExistingAccount, float64(0))
  143. assert.Equal(t, status.RecommendedFee.CreatesAccount,
  144. status.RecommendedFee.ExistingAccount*createAccountExtraFeePercentage)
  145. assert.Equal(t, status.RecommendedFee.CreatesAccountAndRegister,
  146. status.RecommendedFee.ExistingAccount*createAccountInternalExtraFeePercentage)
  147. }