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.

130 lines
4.4 KiB

  1. package api
  2. import (
  3. "fmt"
  4. "testing"
  5. "github.com/hermeznetwork/hermez-node/common"
  6. "github.com/hermeznetwork/hermez-node/db/historydb"
  7. "github.com/mitchellh/copystructure"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. type testCoordinatorsResponse struct {
  11. Coordinators []historydb.CoordinatorAPI `json:"coordinators"`
  12. PendingItems uint64 `json:"pendingItems"`
  13. }
  14. func (t testCoordinatorsResponse) GetPending() (pendingItems, lastItemID uint64) {
  15. if len(t.Coordinators) == 0 {
  16. return 0, 0
  17. }
  18. pendingItems = t.PendingItems
  19. lastItemID = t.Coordinators[len(t.Coordinators)-1].ItemID
  20. return pendingItems, lastItemID
  21. }
  22. func (t *testCoordinatorsResponse) Len() int { return len(t.Coordinators) }
  23. func (t testCoordinatorsResponse) New() Pendinger { return &testCoordinatorsResponse{} }
  24. func genTestCoordinators(coordinators []common.Coordinator) []historydb.CoordinatorAPI {
  25. testCoords := []historydb.CoordinatorAPI{}
  26. for i := 0; i < len(coordinators); i++ {
  27. alreadyRegistered := false
  28. for j := 0; j < len(testCoords); j++ {
  29. // coordinator already registered
  30. if coordinators[i].Bidder == testCoords[j].Bidder {
  31. alreadyRegistered = true
  32. if coordinators[i].EthBlockNum > testCoords[j].EthBlockNum {
  33. // This occurrence is more updated, delete current and append new
  34. testCoords = append(testCoords[:j], testCoords[j+1:]...)
  35. alreadyRegistered = false
  36. }
  37. break
  38. }
  39. }
  40. if !alreadyRegistered {
  41. testCoords = append(testCoords, historydb.CoordinatorAPI{
  42. Bidder: coordinators[i].Bidder,
  43. Forger: coordinators[i].Forger,
  44. EthBlockNum: coordinators[i].EthBlockNum,
  45. URL: coordinators[i].URL,
  46. })
  47. }
  48. }
  49. return testCoords
  50. }
  51. func TestGetCoordinators(t *testing.T) {
  52. endpoint := apiURL + "coordinators"
  53. fetchedCoordinators := []historydb.CoordinatorAPI{}
  54. appendIter := func(intr interface{}) {
  55. for i := 0; i < len(intr.(*testCoordinatorsResponse).Coordinators); i++ {
  56. tmp, err := copystructure.Copy(intr.(*testCoordinatorsResponse).Coordinators[i])
  57. if err != nil {
  58. panic(err)
  59. }
  60. fetchedCoordinators = append(fetchedCoordinators, tmp.(historydb.CoordinatorAPI))
  61. }
  62. }
  63. // All
  64. limit := 5
  65. path := fmt.Sprintf("%s?limit=%d", endpoint, limit)
  66. err := doGoodReqPaginated(path, historydb.OrderAsc, &testCoordinatorsResponse{}, appendIter)
  67. assert.NoError(t, err)
  68. assertCoordinators(t, tc.coordinators, fetchedCoordinators)
  69. // All in reverse order
  70. fetchedCoordinators = []historydb.CoordinatorAPI{}
  71. err = doGoodReqPaginated(path, historydb.OrderDesc, &testCoordinatorsResponse{}, appendIter)
  72. assert.NoError(t, err)
  73. reversedCoordinators := []historydb.CoordinatorAPI{}
  74. for i := 0; i < len(tc.coordinators); i++ {
  75. reversedCoordinators = append(reversedCoordinators, tc.coordinators[len(tc.coordinators)-1-i])
  76. }
  77. assertCoordinators(t, reversedCoordinators, fetchedCoordinators)
  78. for _, filteredCoord := range tc.coordinators {
  79. // By bidder
  80. fetchedCoordinators = []historydb.CoordinatorAPI{}
  81. err = doGoodReqPaginated(
  82. fmt.Sprintf(path+"&bidderAddr=%s", filteredCoord.Bidder.String()),
  83. historydb.OrderAsc, &testCoordinatorsResponse{}, appendIter,
  84. )
  85. assert.NoError(t, err)
  86. assertCoordinators(t, []historydb.CoordinatorAPI{filteredCoord}, fetchedCoordinators)
  87. // By forger
  88. fetchedCoordinators = []historydb.CoordinatorAPI{}
  89. err = doGoodReqPaginated(
  90. fmt.Sprintf(path+"&forgerAddr=%s", filteredCoord.Forger.String()),
  91. historydb.OrderAsc, &testCoordinatorsResponse{}, appendIter,
  92. )
  93. assert.NoError(t, err)
  94. assertCoordinators(t, []historydb.CoordinatorAPI{filteredCoord}, fetchedCoordinators)
  95. }
  96. // Empty array
  97. fetchedCoordinators = []historydb.CoordinatorAPI{}
  98. path = fmt.Sprintf("%s?bidderAddr=0xaa942cfcd25ad4d90a62358b0dd84f33b398262a", endpoint)
  99. err = doGoodReqPaginated(path, historydb.OrderDesc, &testCoordinatorsResponse{}, appendIter)
  100. assert.NoError(t, err)
  101. assertCoordinators(t, []historydb.CoordinatorAPI{}, fetchedCoordinators)
  102. // 400
  103. path = fmt.Sprintf("%s?bidderAddr=0x001", endpoint)
  104. err = doBadReq("GET", path, nil, 400)
  105. assert.NoError(t, err)
  106. }
  107. func assertCoordinator(t *testing.T, expected, actual historydb.CoordinatorAPI) {
  108. actual.ItemID = 0
  109. assert.Equal(t, expected, actual)
  110. }
  111. func assertCoordinators(t *testing.T, expected, actual []historydb.CoordinatorAPI) {
  112. assert.Equal(t, len(expected), len(actual))
  113. for i := 0; i < len(expected); i++ {
  114. assertCoordinator(t, expected[i], actual[i])
  115. }
  116. }