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.

299 lines
9.8 KiB

  1. package api
  2. import (
  3. "fmt"
  4. "strconv"
  5. "testing"
  6. "github.com/hermeznetwork/hermez-node/common"
  7. "github.com/hermeznetwork/hermez-node/db/historydb"
  8. "github.com/mitchellh/copystructure"
  9. "github.com/stretchr/testify/assert"
  10. )
  11. type testSlot struct {
  12. ItemID uint64 `json:"itemId"`
  13. SlotNum int64 `json:"slotNum"`
  14. FirstBlock int64 `json:"firstBlock"`
  15. LastBlock int64 `json:"lastBlock"`
  16. OpenAuction bool `json:"openAuction"`
  17. WinnerBid *testBid `json:"winnerBid"`
  18. }
  19. type testSlotsResponse struct {
  20. Slots []testSlot `json:"slots"`
  21. PendingItems uint64 `json:"pendingItems"`
  22. }
  23. func (t testSlotsResponse) GetPending() (pendingItems, lastItemID uint64) {
  24. pendingItems = t.PendingItems
  25. lastItemID = t.Slots[len(t.Slots)-1].ItemID
  26. return pendingItems, lastItemID
  27. }
  28. func (t testSlotsResponse) Len() int {
  29. return len(t.Slots)
  30. }
  31. func (a *API) genTestSlots(nSlots int, lastBlockNum int64, bids []testBid, auctionVars common.AuctionVariables) []testSlot {
  32. tSlots := []testSlot{}
  33. bestBids := make(map[int64]testBid)
  34. // It's assumed that bids for each slot will be received in increasing order
  35. for i := range bids {
  36. bestBids[bids[i].SlotNum] = bids[i]
  37. }
  38. for i := int64(0); i < int64(nSlots); i++ {
  39. bid, ok := bestBids[i]
  40. firstBlock, lastBlock := a.getFirstLastBlock(int64(i))
  41. tSlot := testSlot{
  42. SlotNum: int64(i),
  43. FirstBlock: firstBlock,
  44. LastBlock: lastBlock,
  45. OpenAuction: a.isOpenAuction(lastBlockNum, int64(i), auctionVars),
  46. }
  47. if ok {
  48. tSlot.WinnerBid = &bid
  49. }
  50. tSlots = append(tSlots, tSlot)
  51. }
  52. return tSlots
  53. }
  54. func (a *API) getEmptyTestSlot(slotNum int64) testSlot {
  55. firstBlock, lastBlock := a.getFirstLastBlock(slotNum)
  56. slot := testSlot{
  57. SlotNum: slotNum,
  58. FirstBlock: firstBlock,
  59. LastBlock: lastBlock,
  60. OpenAuction: false,
  61. WinnerBid: nil,
  62. }
  63. return slot
  64. }
  65. func TestGetSlot(t *testing.T) {
  66. endpoint := apiURL + "slots/"
  67. for _, slot := range tc.slots {
  68. fetchedSlot := testSlot{}
  69. assert.NoError(
  70. t, doGoodReq(
  71. "GET",
  72. endpoint+strconv.Itoa(int(slot.SlotNum)),
  73. nil, &fetchedSlot,
  74. ),
  75. )
  76. assertSlot(t, slot, fetchedSlot)
  77. }
  78. // Slot with WinnerBid == nil
  79. slotNum := int64(15)
  80. fetchedSlot := testSlot{}
  81. assert.NoError(
  82. t, doGoodReq(
  83. "GET",
  84. endpoint+strconv.Itoa(int(slotNum)),
  85. nil, &fetchedSlot,
  86. ),
  87. )
  88. emptySlot := api.getEmptyTestSlot(slotNum)
  89. assertSlot(t, emptySlot, fetchedSlot)
  90. // Invalid slotNum
  91. path := endpoint + strconv.Itoa(-2)
  92. err := doBadReq("GET", path, nil, 400)
  93. assert.NoError(t, err)
  94. }
  95. func TestGetSlots(t *testing.T) {
  96. endpoint := apiURL + "slots"
  97. fetchedSlots := []testSlot{}
  98. appendIter := func(intr interface{}) {
  99. for i := 0; i < len(intr.(*testSlotsResponse).Slots); i++ {
  100. tmp, err := copystructure.Copy(intr.(*testSlotsResponse).Slots[i])
  101. if err != nil {
  102. panic(err)
  103. }
  104. fetchedSlots = append(fetchedSlots, tmp.(testSlot))
  105. }
  106. }
  107. // All slots with maxSlotNum filter
  108. maxSlotNum := tc.slots[len(tc.slots)-1].SlotNum + 5
  109. limit := 1
  110. path := fmt.Sprintf("%s?maxSlotNum=%d&limit=%d&fromItem=", endpoint, maxSlotNum, limit)
  111. err := doGoodReqPaginated(path, historydb.OrderAsc, &testSlotsResponse{}, appendIter)
  112. assert.NoError(t, err)
  113. allSlots := tc.slots
  114. for i := tc.slots[len(tc.slots)-1].SlotNum; i < maxSlotNum; i++ {
  115. emptySlot := api.getEmptyTestSlot(i + 1)
  116. allSlots = append(allSlots, emptySlot)
  117. }
  118. assertSlots(t, allSlots, fetchedSlots)
  119. // All slots with maxSlotNum filter, in reverse order
  120. fetchedSlots = []testSlot{}
  121. limit = 3
  122. path = fmt.Sprintf("%s?maxSlotNum=%d&limit=%d&fromItem=", endpoint, maxSlotNum, limit)
  123. err = doGoodReqPaginated(path, historydb.OrderDesc, &testSlotsResponse{}, appendIter)
  124. assert.NoError(t, err)
  125. flippedAllSlots := []testSlot{}
  126. for i := len(allSlots) - 1; i >= 0; i-- {
  127. flippedAllSlots = append(flippedAllSlots, allSlots[i])
  128. }
  129. assertSlots(t, flippedAllSlots, fetchedSlots)
  130. // maxSlotNum & wonByEthereumAddress
  131. fetchedSlots = []testSlot{}
  132. limit = 1
  133. bidderAddr := tc.coordinators[2].Bidder
  134. path = fmt.Sprintf("%s?maxSlotNum=%d&wonByEthereumAddress=%s&limit=%d&fromItem=", endpoint, maxSlotNum, bidderAddr.String(), limit)
  135. err = doGoodReqPaginated(path, historydb.OrderAsc, &testSlotsResponse{}, appendIter)
  136. assert.NoError(t, err)
  137. bidderAddressSlots := []testSlot{}
  138. for i := 0; i < len(tc.slots); i++ {
  139. if tc.slots[i].WinnerBid != nil {
  140. if tc.slots[i].WinnerBid.Bidder == bidderAddr {
  141. bidderAddressSlots = append(bidderAddressSlots, tc.slots[i])
  142. }
  143. }
  144. }
  145. assertSlots(t, bidderAddressSlots, fetchedSlots)
  146. // maxSlotNum & wonByEthereumAddress, in reverse order
  147. fetchedSlots = []testSlot{}
  148. limit = 1
  149. path = fmt.Sprintf("%s?maxSlotNum=%d&wonByEthereumAddress=%s&limit=%d&fromItem=", endpoint, maxSlotNum, bidderAddr.String(), limit)
  150. err = doGoodReqPaginated(path, historydb.OrderDesc, &testSlotsResponse{}, appendIter)
  151. assert.NoError(t, err)
  152. flippedBidderAddressSlots := []testSlot{}
  153. for i := len(bidderAddressSlots) - 1; i >= 0; i-- {
  154. flippedBidderAddressSlots = append(flippedBidderAddressSlots, bidderAddressSlots[i])
  155. }
  156. assertSlots(t, flippedBidderAddressSlots, fetchedSlots)
  157. // finishedAuction
  158. fetchedSlots = []testSlot{}
  159. limit = 15
  160. path = fmt.Sprintf("%s?finishedAuction=%t&limit=%d&fromItem=", endpoint, true, limit)
  161. err = doGoodReqPaginated(path, historydb.OrderAsc, &testSlotsResponse{}, appendIter)
  162. assert.NoError(t, err)
  163. currentSlot := api.getCurrentSlot(tc.blocks[len(tc.blocks)-1].EthBlockNum)
  164. finishedAuctionSlots := []testSlot{}
  165. for i := 0; i < len(tc.slots); i++ {
  166. finishAuction := currentSlot + int64(tc.auctionVars.ClosedAuctionSlots)
  167. if tc.slots[i].SlotNum <= finishAuction {
  168. finishedAuctionSlots = append(finishedAuctionSlots, tc.slots[i])
  169. } else {
  170. break
  171. }
  172. }
  173. assertSlots(t, finishedAuctionSlots, fetchedSlots)
  174. //minSlot + maxSlot
  175. limit = 10
  176. minSlotNum := tc.slots[3].SlotNum
  177. maxSlotNum = tc.slots[len(tc.slots)-1].SlotNum - 1
  178. fetchedSlots = []testSlot{}
  179. path = fmt.Sprintf("%s?maxSlotNum=%d&minSlotNum=%d&limit=%d&fromItem=", endpoint, maxSlotNum, minSlotNum, limit)
  180. err = doGoodReqPaginated(path, historydb.OrderAsc, &testSlotsResponse{}, appendIter)
  181. assert.NoError(t, err)
  182. minMaxBatchNumSlots := []testSlot{}
  183. for i := 0; i < len(tc.slots); i++ {
  184. if tc.slots[i].SlotNum >= minSlotNum && tc.slots[i].SlotNum <= maxSlotNum {
  185. minMaxBatchNumSlots = append(minMaxBatchNumSlots, tc.slots[i])
  186. }
  187. }
  188. assertSlots(t, minMaxBatchNumSlots, fetchedSlots)
  189. //minSlot + maxSlot
  190. limit = 15
  191. minSlotNum = tc.slots[0].SlotNum
  192. maxSlotNum = tc.slots[0].SlotNum
  193. fetchedSlots = []testSlot{}
  194. path = fmt.Sprintf("%s?maxSlotNum=%d&minSlotNum=%d&limit=%d&fromItem=", endpoint, maxSlotNum, minSlotNum, limit)
  195. err = doGoodReqPaginated(path, historydb.OrderAsc, &testSlotsResponse{}, appendIter)
  196. assert.NoError(t, err)
  197. minMaxBatchNumSlots = []testSlot{}
  198. for i := 0; i < len(tc.slots); i++ {
  199. if tc.slots[i].SlotNum >= minSlotNum && tc.slots[i].SlotNum <= maxSlotNum {
  200. minMaxBatchNumSlots = append(minMaxBatchNumSlots, tc.slots[i])
  201. }
  202. }
  203. assertSlots(t, minMaxBatchNumSlots, fetchedSlots)
  204. // Only empty Slots
  205. limit = 2
  206. minSlotNum = tc.slots[len(tc.slots)-1].SlotNum + 1
  207. maxSlotNum = tc.slots[len(tc.slots)-1].SlotNum + 5
  208. fetchedSlots = []testSlot{}
  209. path = fmt.Sprintf("%s?maxSlotNum=%d&minSlotNum=%d&limit=%d&fromItem=", endpoint, maxSlotNum, minSlotNum, limit)
  210. err = doGoodReqPaginated(path, historydb.OrderAsc, &testSlotsResponse{}, appendIter)
  211. assert.NoError(t, err)
  212. emptySlots := []testSlot{}
  213. for i := 0; i < len(allSlots); i++ {
  214. if allSlots[i].SlotNum >= minSlotNum && allSlots[i].SlotNum <= maxSlotNum {
  215. emptySlots = append(emptySlots, allSlots[i])
  216. }
  217. }
  218. assertSlots(t, emptySlots, fetchedSlots)
  219. // Only empty Slots, in reverse order
  220. limit = 4
  221. minSlotNum = tc.slots[len(tc.slots)-1].SlotNum + 1
  222. maxSlotNum = tc.slots[len(tc.slots)-1].SlotNum + 5
  223. fetchedSlots = []testSlot{}
  224. path = fmt.Sprintf("%s?maxSlotNum=%d&minSlotNum=%d&limit=%d&fromItem=", endpoint, maxSlotNum, minSlotNum, limit)
  225. err = doGoodReqPaginated(path, historydb.OrderDesc, &testSlotsResponse{}, appendIter)
  226. assert.NoError(t, err)
  227. flippedEmptySlots := []testSlot{}
  228. for i := 0; i < len(flippedAllSlots); i++ {
  229. if flippedAllSlots[i].SlotNum >= minSlotNum && flippedAllSlots[i].SlotNum <= maxSlotNum {
  230. flippedEmptySlots = append(flippedEmptySlots, flippedAllSlots[i])
  231. }
  232. }
  233. assertSlots(t, flippedEmptySlots, fetchedSlots)
  234. // 400
  235. // No filters
  236. path = fmt.Sprintf("%s?limit=%d&fromItem=", endpoint, limit)
  237. err = doBadReq("GET", path, nil, 400)
  238. assert.NoError(t, err)
  239. // Invalid maxSlotNum
  240. path = fmt.Sprintf("%s?maxSlotNum=%d", endpoint, -2)
  241. err = doBadReq("GET", path, nil, 400)
  242. assert.NoError(t, err)
  243. // Invalid wonByEthereumAddress
  244. path = fmt.Sprintf("%s?maxSlotNum=%d&wonByEthereumAddress=%s", endpoint, maxSlotNum, "0xG0000001")
  245. err = doBadReq("GET", path, nil, 400)
  246. assert.NoError(t, err)
  247. // Invalid minSlotNum / maxSlotNum (minSlotNum > maxSlotNum)
  248. maxSlotNum = tc.slots[1].SlotNum
  249. minSlotNum = tc.slots[4].SlotNum
  250. path = fmt.Sprintf("%s?maxSlotNum=%d&minSlotNum=%d&limit=%d&fromItem=", endpoint, maxSlotNum, minSlotNum, limit)
  251. err = doBadReq("GET", path, nil, 400)
  252. assert.NoError(t, err)
  253. // 404
  254. maxSlotNum = tc.slots[1].SlotNum
  255. path = fmt.Sprintf("%s?maxSlotNum=%d&wonByEthereumAddress=%s&limit=%d&fromItem=", endpoint, maxSlotNum, tc.coordinators[3].Bidder.String(), limit)
  256. err = doBadReq("GET", path, nil, 404)
  257. assert.NoError(t, err)
  258. }
  259. func assertSlots(t *testing.T, expected, actual []testSlot) {
  260. assert.Equal(t, len(expected), len(actual))
  261. for i := 0; i < len(expected); i++ {
  262. assertSlot(t, expected[i], actual[i])
  263. }
  264. }
  265. func assertSlot(t *testing.T, expected, actual testSlot) {
  266. if actual.WinnerBid != nil {
  267. assert.Equal(t, expected.WinnerBid.Timestamp.Unix(), actual.WinnerBid.Timestamp.Unix())
  268. expected.WinnerBid.Timestamp = actual.WinnerBid.Timestamp
  269. actual.WinnerBid.ItemID = expected.WinnerBid.ItemID
  270. }
  271. actual.ItemID = expected.ItemID
  272. assert.Equal(t, expected, actual)
  273. }