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.

161 lines
5.0 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 (t testBidsResponse) New() Pendinger { return &testBidsResponse{} }
  35. func genTestBids(blocks []common.Block, coordinators []historydb.CoordinatorAPI, bids []common.Bid) []testBid {
  36. tBids := []testBid{}
  37. for _, bid := range bids {
  38. block := getBlockByNum(bid.EthBlockNum, blocks)
  39. coordinator := getCoordinatorByBidder(bid.Bidder, coordinators)
  40. tBid := testBid{
  41. SlotNum: bid.SlotNum,
  42. BidValue: bid.BidValue.String(),
  43. EthBlockNum: bid.EthBlockNum,
  44. Bidder: bid.Bidder,
  45. Forger: coordinator.Forger,
  46. URL: coordinator.URL,
  47. Timestamp: block.Timestamp,
  48. }
  49. tBids = append(tBids, tBid)
  50. }
  51. return tBids
  52. }
  53. func TestGetBids(t *testing.T) {
  54. endpoint := apiURL + "bids"
  55. fetchedBids := []testBid{}
  56. appendIter := func(intr interface{}) {
  57. for i := 0; i < len(intr.(*testBidsResponse).Bids); i++ {
  58. tmp, err := copystructure.Copy(intr.(*testBidsResponse).Bids[i])
  59. if err != nil {
  60. panic(err)
  61. }
  62. fetchedBids = append(fetchedBids, tmp.(testBid))
  63. }
  64. }
  65. limit := 3
  66. // bidderAddress
  67. fetchedBids = []testBid{}
  68. bidderAddress := tc.bids[3].Bidder
  69. path := fmt.Sprintf("%s?bidderAddr=%s&limit=%d", endpoint, bidderAddress.String(), limit)
  70. err := doGoodReqPaginated(path, historydb.OrderAsc, &testBidsResponse{}, appendIter)
  71. assert.NoError(t, err)
  72. bidderAddrBids := []testBid{}
  73. for i := 0; i < len(tc.bids); i++ {
  74. if tc.bids[i].Bidder == bidderAddress {
  75. bidderAddrBids = append(bidderAddrBids, tc.bids[i])
  76. }
  77. }
  78. assertBids(t, bidderAddrBids, fetchedBids)
  79. // slotNum
  80. fetchedBids = []testBid{}
  81. slotNum := tc.bids[3].SlotNum
  82. path = fmt.Sprintf("%s?slotNum=%d&limit=%d", endpoint, slotNum, limit)
  83. err = doGoodReqPaginated(path, historydb.OrderAsc, &testBidsResponse{}, appendIter)
  84. assert.NoError(t, err)
  85. slotNumBids := []testBid{}
  86. for i := 0; i < len(tc.bids); i++ {
  87. if tc.bids[i].SlotNum == slotNum {
  88. slotNumBids = append(slotNumBids, tc.bids[i])
  89. }
  90. }
  91. assertBids(t, slotNumBids, fetchedBids)
  92. // slotNum, in reverse order
  93. fetchedBids = []testBid{}
  94. path = fmt.Sprintf("%s?slotNum=%d&limit=%d", endpoint, slotNum, limit)
  95. err = doGoodReqPaginated(path, historydb.OrderDesc, &testBidsResponse{}, appendIter)
  96. assert.NoError(t, err)
  97. flippedBids := []testBid{}
  98. for i := len(slotNumBids) - 1; i >= 0; i-- {
  99. flippedBids = append(flippedBids, slotNumBids[i])
  100. }
  101. assertBids(t, flippedBids, fetchedBids)
  102. // Mixed filters
  103. fetchedBids = []testBid{}
  104. bidderAddress = tc.bids[1].Bidder
  105. slotNum = tc.bids[1].SlotNum
  106. path = fmt.Sprintf("%s?bidderAddr=%s&slotNum=%d&limit=%d", endpoint, bidderAddress.String(), slotNum, limit)
  107. err = doGoodReqPaginated(path, historydb.OrderAsc, &testBidsResponse{}, appendIter)
  108. assert.NoError(t, err)
  109. slotNumBidderAddrBids := []testBid{}
  110. for i := 0; i < len(tc.bids); i++ {
  111. if tc.bids[i].Bidder == bidderAddress && tc.bids[i].SlotNum == slotNum {
  112. slotNumBidderAddrBids = append(slotNumBidderAddrBids, tc.bids[i])
  113. }
  114. }
  115. assertBids(t, slotNumBidderAddrBids, fetchedBids)
  116. // 400
  117. // No filters
  118. path = fmt.Sprintf("%s?limit=%d", endpoint, limit)
  119. err = doBadReq("GET", path, nil, 400)
  120. assert.NoError(t, err)
  121. // Invalid slotNum
  122. path = fmt.Sprintf("%s?slotNum=%d", endpoint, -2)
  123. err = doBadReq("GET", path, nil, 400)
  124. assert.NoError(t, err)
  125. // Invalid bidderAddress
  126. path = fmt.Sprintf("%s?bidderAddr=%s", endpoint, "0xG0000001")
  127. err = doBadReq("GET", path, nil, 400)
  128. assert.NoError(t, err)
  129. // 404
  130. path = fmt.Sprintf("%s?slotNum=%d&bidderAddr=%s", endpoint, tc.bids[0].SlotNum, tc.bids[1].Bidder.String())
  131. err = doBadReq("GET", path, nil, 404)
  132. assert.NoError(t, err)
  133. }
  134. func assertBids(t *testing.T, expected, actual []testBid) {
  135. assert.Equal(t, len(expected), len(actual))
  136. for i := 0; i < len(expected); i++ {
  137. assertBid(t, expected[i], actual[i])
  138. }
  139. }
  140. func assertBid(t *testing.T, expected, actual testBid) {
  141. assert.Equal(t, expected.Timestamp.Unix(), actual.Timestamp.Unix())
  142. expected.Timestamp = actual.Timestamp
  143. actual.ItemID = expected.ItemID
  144. assert.Equal(t, expected, actual)
  145. }