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.

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