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.

98 lines
3.8 KiB

  1. package api
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/hermeznetwork/hermez-node/common"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. const secondsPerBlock = 15
  9. func TestSetRollupVariables(t *testing.T) {
  10. rollupVars := &common.RollupVariables{}
  11. assert.Equal(t, *rollupVars, api.status.Rollup)
  12. api.SetRollupVariables(tc.rollupVars)
  13. assert.Equal(t, tc.rollupVars, api.status.Rollup)
  14. }
  15. func TestSetWDelayerVariables(t *testing.T) {
  16. wdelayerVars := &common.WDelayerVariables{}
  17. assert.Equal(t, *wdelayerVars, api.status.WithdrawalDelayer)
  18. api.SetWDelayerVariables(tc.wdelayerVars)
  19. assert.Equal(t, tc.wdelayerVars, api.status.WithdrawalDelayer)
  20. }
  21. func TestSetAuctionVariables(t *testing.T) {
  22. auctionVars := &common.AuctionVariables{}
  23. assert.Equal(t, *auctionVars, api.status.Auction)
  24. api.SetAuctionVariables(tc.auctionVars)
  25. assert.Equal(t, tc.auctionVars, api.status.Auction)
  26. }
  27. func TestNextForgers(t *testing.T) {
  28. // It's assumed that bids for each slot will be received in increasing order
  29. bestBids := make(map[int64]testBid)
  30. for j := range tc.bids {
  31. bestBids[tc.bids[j].SlotNum] = tc.bids[j]
  32. }
  33. lastBlock := tc.blocks[len(tc.blocks)-1]
  34. for i := int64(0); i < tc.slots[len(tc.slots)-1].SlotNum; i++ {
  35. lastClosedSlot := i + int64(api.status.Auction.ClosedAuctionSlots)
  36. nextForgers, err := api.GetNextForgers(tc.blocks[len(tc.blocks)-1], i, lastClosedSlot)
  37. assert.NoError(t, err)
  38. for j := i; j <= lastClosedSlot; j++ {
  39. for q := range nextForgers {
  40. if nextForgers[q].Period.SlotNum == j {
  41. if nextForgers[q].Coordinator.ItemID != 0 {
  42. assert.Equal(t, bestBids[j].Bidder, nextForgers[q].Coordinator.Bidder)
  43. } else {
  44. assert.Equal(t, bootCoordinator.Bidder, nextForgers[q].Coordinator.Bidder)
  45. }
  46. firstBlockSlot, lastBlockSlot := api.getFirstLastBlock(j)
  47. fromTimestamp := lastBlock.Timestamp.Add(time.Second * time.Duration(secondsPerBlock*(firstBlockSlot-lastBlock.EthBlockNum)))
  48. toTimestamp := lastBlock.Timestamp.Add(time.Second * time.Duration(secondsPerBlock*(lastBlockSlot-lastBlock.EthBlockNum)))
  49. assert.Equal(t, fromTimestamp.Unix(), nextForgers[q].Period.FromTimestamp.Unix())
  50. assert.Equal(t, toTimestamp.Unix(), nextForgers[q].Period.ToTimestamp.Unix())
  51. }
  52. }
  53. }
  54. }
  55. }
  56. func TestUpdateNetworkInfo(t *testing.T) {
  57. status := &Network{}
  58. assert.Equal(t, status.LastBlock, api.status.Network.LastBlock)
  59. assert.Equal(t, status.LastBatch.BatchNum, api.status.Network.LastBatch.BatchNum)
  60. assert.Equal(t, status.CurrentSlot, api.status.Network.CurrentSlot)
  61. assert.Equal(t, status.NextForgers, api.status.Network.NextForgers)
  62. lastBlock := tc.blocks[3]
  63. lastBatchNum := common.BatchNum(3)
  64. currentSlotNum := int64(1)
  65. err := api.UpdateNetworkInfo(lastBlock, lastBatchNum, currentSlotNum)
  66. assert.NoError(t, err)
  67. assert.Equal(t, lastBlock.EthBlockNum, api.status.Network.LastBlock)
  68. assert.Equal(t, lastBatchNum, api.status.Network.LastBatch.BatchNum)
  69. assert.Equal(t, currentSlotNum, api.status.Network.CurrentSlot)
  70. assert.Equal(t, int(api.status.Auction.ClosedAuctionSlots)+1, len(api.status.Network.NextForgers))
  71. }
  72. func TestUpdateMetrics(t *testing.T) {
  73. // TODO: Improve checks when til is integrated
  74. // Update Metrics needs api.status.Network.LastBatch.BatchNum to be updated
  75. lastBlock := tc.blocks[3]
  76. lastBatchNum := common.BatchNum(3)
  77. currentSlotNum := int64(1)
  78. err := api.UpdateNetworkInfo(lastBlock, lastBatchNum, currentSlotNum)
  79. assert.NoError(t, err)
  80. err = api.UpdateMetrics()
  81. assert.NoError(t, err)
  82. assert.Greater(t, api.status.Metrics.TransactionsPerBatch, float64(0))
  83. assert.Greater(t, api.status.Metrics.BatchFrequency, float64(0))
  84. assert.Greater(t, api.status.Metrics.TransactionsPerBatch, float64(0))
  85. assert.Greater(t, api.status.Metrics.TotalAccounts, int64(0))
  86. assert.Greater(t, api.status.Metrics.TotalBJJs, int64(0))
  87. assert.Greater(t, api.status.Metrics.AvgTransactionFee, float64(0))
  88. }