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
5.2 KiB

  1. package api
  2. import (
  3. "fmt"
  4. "testing"
  5. "time"
  6. ethCommon "github.com/ethereum/go-ethereum/common"
  7. "github.com/hermeznetwork/hermez-node/common"
  8. "github.com/hermeznetwork/hermez-node/db"
  9. "github.com/hermeznetwork/hermez-node/db/historydb"
  10. "github.com/mitchellh/copystructure"
  11. "github.com/stretchr/testify/assert"
  12. )
  13. type testBid struct {
  14. ItemID int `json:"itemId"`
  15. SlotNum int64 `json:"slotNum"`
  16. BidValue string `json:"bidValue"`
  17. EthBlockNum int64 `json:"ethereumBlockNum"`
  18. Bidder ethCommon.Address `json:"bidderAddr"`
  19. Forger ethCommon.Address `json:"forgerAddr"`
  20. URL string `json:"URL"`
  21. Timestamp time.Time `json:"timestamp"`
  22. }
  23. type testBidsResponse struct {
  24. Bids []testBid `json:"bids"`
  25. Pagination *db.Pagination `json:"pagination"`
  26. }
  27. func (t testBidsResponse) GetPagination() *db.Pagination {
  28. if t.Bids[0].ItemID < t.Bids[len(t.Bids)-1].ItemID {
  29. t.Pagination.FirstReturnedItem = t.Bids[0].ItemID
  30. t.Pagination.LastReturnedItem = t.Bids[len(t.Bids)-1].ItemID
  31. } else {
  32. t.Pagination.LastReturnedItem = t.Bids[0].ItemID
  33. t.Pagination.FirstReturnedItem = t.Bids[len(t.Bids)-1].ItemID
  34. }
  35. return t.Pagination
  36. }
  37. func (t testBidsResponse) Len() int {
  38. return len(t.Bids)
  39. }
  40. func genTestBids(blocks []common.Block, coordinators []historydb.CoordinatorAPI, bids []common.Bid) []testBid {
  41. tBids := []testBid{}
  42. for _, bid := range bids {
  43. block := getBlockByNum(bid.EthBlockNum, blocks)
  44. coordinator := getCoordinatorByBidder(bid.Bidder, coordinators)
  45. tBid := testBid{
  46. SlotNum: bid.SlotNum,
  47. BidValue: bid.BidValue.String(),
  48. EthBlockNum: bid.EthBlockNum,
  49. Bidder: bid.Bidder,
  50. Forger: coordinator.Forger,
  51. URL: coordinator.URL,
  52. Timestamp: block.Timestamp,
  53. }
  54. tBids = append(tBids, tBid)
  55. }
  56. return tBids
  57. }
  58. func TestGetBids(t *testing.T) {
  59. endpoint := apiURL + "bids"
  60. fetchedBids := []testBid{}
  61. appendIter := func(intr interface{}) {
  62. for i := 0; i < len(intr.(*testBidsResponse).Bids); i++ {
  63. tmp, err := copystructure.Copy(intr.(*testBidsResponse).Bids[i])
  64. if err != nil {
  65. panic(err)
  66. }
  67. fetchedBids = append(fetchedBids, tmp.(testBid))
  68. }
  69. }
  70. limit := 3
  71. // bidderAddress
  72. fetchedBids = []testBid{}
  73. bidderAddress := tc.bids[3].Bidder
  74. path := fmt.Sprintf("%s?bidderAddr=%s&limit=%d&fromItem=", endpoint, bidderAddress.String(), limit)
  75. err := doGoodReqPaginated(path, historydb.OrderAsc, &testBidsResponse{}, appendIter)
  76. assert.NoError(t, err)
  77. bidderAddrBids := []testBid{}
  78. for i := 0; i < len(tc.bids); i++ {
  79. if tc.bids[i].Bidder == bidderAddress {
  80. bidderAddrBids = append(bidderAddrBids, tc.bids[i])
  81. }
  82. }
  83. assertBids(t, bidderAddrBids, fetchedBids)
  84. // slotNum
  85. fetchedBids = []testBid{}
  86. slotNum := tc.bids[3].SlotNum
  87. path = fmt.Sprintf("%s?slotNum=%d&limit=%d&fromItem=", endpoint, slotNum, limit)
  88. err = doGoodReqPaginated(path, historydb.OrderAsc, &testBidsResponse{}, appendIter)
  89. assert.NoError(t, err)
  90. slotNumBids := []testBid{}
  91. for i := 0; i < len(tc.bids); i++ {
  92. if tc.bids[i].SlotNum == slotNum {
  93. slotNumBids = append(slotNumBids, tc.bids[i])
  94. }
  95. }
  96. assertBids(t, slotNumBids, fetchedBids)
  97. // slotNum, in reverse order
  98. fetchedBids = []testBid{}
  99. path = fmt.Sprintf("%s?slotNum=%d&limit=%d&fromItem=", endpoint, slotNum, limit)
  100. err = doGoodReqPaginated(path, historydb.OrderDesc, &testBidsResponse{}, appendIter)
  101. assert.NoError(t, err)
  102. flippedBids := []testBid{}
  103. for i := len(slotNumBids) - 1; i >= 0; i-- {
  104. flippedBids = append(flippedBids, slotNumBids[i])
  105. }
  106. assertBids(t, flippedBids, fetchedBids)
  107. // Mixed filters
  108. fetchedBids = []testBid{}
  109. bidderAddress = tc.bids[9].Bidder
  110. slotNum = tc.bids[4].SlotNum
  111. path = fmt.Sprintf("%s?bidderAddr=%s&slotNum=%d&limit=%d&fromItem=", endpoint, bidderAddress.String(), slotNum, limit)
  112. err = doGoodReqPaginated(path, historydb.OrderAsc, &testBidsResponse{}, appendIter)
  113. assert.NoError(t, err)
  114. slotNumBidderAddrBids := []testBid{}
  115. for i := 0; i < len(tc.bids); i++ {
  116. if tc.bids[i].Bidder == bidderAddress && tc.bids[i].SlotNum == slotNum {
  117. slotNumBidderAddrBids = append(slotNumBidderAddrBids, tc.bids[i])
  118. }
  119. }
  120. assertBids(t, slotNumBidderAddrBids, fetchedBids)
  121. // 400
  122. // No filters
  123. path = fmt.Sprintf("%s?limit=%d&fromItem=", endpoint, limit)
  124. err = doBadReq("GET", path, nil, 400)
  125. assert.NoError(t, err)
  126. // Invalid slotNum
  127. path = fmt.Sprintf("%s?slotNum=%d", endpoint, -2)
  128. err = doBadReq("GET", path, nil, 400)
  129. assert.NoError(t, err)
  130. // Invalid bidderAddress
  131. path = fmt.Sprintf("%s?bidderAddr=%s", endpoint, "0xG0000001")
  132. err = doBadReq("GET", path, nil, 400)
  133. assert.NoError(t, err)
  134. // 404
  135. path = fmt.Sprintf("%s?slotNum=%d&bidderAddr=%s", endpoint, tc.bids[0].SlotNum, tc.bids[1].Bidder.String())
  136. err = doBadReq("GET", path, nil, 404)
  137. assert.NoError(t, err)
  138. }
  139. func assertBids(t *testing.T, expected, actual []testBid) {
  140. assert.Equal(t, len(expected), len(actual))
  141. for i := 0; i < len(expected); i++ {
  142. assertBid(t, expected[i], actual[i])
  143. }
  144. }
  145. func assertBid(t *testing.T, expected, actual testBid) {
  146. assert.Equal(t, expected.Timestamp.Unix(), actual.Timestamp.Unix())
  147. expected.Timestamp = actual.Timestamp
  148. actual.ItemID = expected.ItemID
  149. assert.Equal(t, expected, actual)
  150. }