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.

243 lines
8.0 KiB

  1. package api
  2. import (
  3. "fmt"
  4. "strconv"
  5. "testing"
  6. "time"
  7. ethCommon "github.com/ethereum/go-ethereum/common"
  8. "github.com/hermeznetwork/hermez-node/common"
  9. "github.com/hermeznetwork/hermez-node/db"
  10. "github.com/hermeznetwork/hermez-node/db/historydb"
  11. "github.com/mitchellh/copystructure"
  12. "github.com/stretchr/testify/assert"
  13. )
  14. type testBatch struct {
  15. ItemID int `json:"itemId"`
  16. BatchNum common.BatchNum `json:"batchNum"`
  17. EthBlockNum int64 `json:"ethereumBlockNum"`
  18. EthBlockHash ethCommon.Hash `json:"ethereumBlockHash"`
  19. Timestamp time.Time `json:"timestamp"`
  20. ForgerAddr ethCommon.Address `json:"forgerAddr"`
  21. CollectedFees map[common.TokenID]string `json:"collectedFees"`
  22. TotalFeesUSD *float64 `json:"historicTotalCollectedFeesUSD"`
  23. StateRoot string `json:"stateRoot"`
  24. NumAccounts int `json:"numAccounts"`
  25. ExitRoot string `json:"exitRoot"`
  26. ForgeL1TxsNum *int64 `json:"forgeL1TransactionsNum"`
  27. SlotNum int64 `json:"slotNum"`
  28. }
  29. type testBatchesResponse struct {
  30. Batches []testBatch `json:"batches"`
  31. Pagination *db.Pagination `json:"pagination"`
  32. }
  33. func (t testBatchesResponse) GetPagination() *db.Pagination {
  34. if t.Batches[0].ItemID < t.Batches[len(t.Batches)-1].ItemID {
  35. t.Pagination.FirstReturnedItem = t.Batches[0].ItemID
  36. t.Pagination.LastReturnedItem = t.Batches[len(t.Batches)-1].ItemID
  37. } else {
  38. t.Pagination.LastReturnedItem = t.Batches[0].ItemID
  39. t.Pagination.FirstReturnedItem = t.Batches[len(t.Batches)-1].ItemID
  40. }
  41. return t.Pagination
  42. }
  43. func (t testBatchesResponse) Len() int {
  44. return len(t.Batches)
  45. }
  46. func genTestBatches(blocks []common.Block, cBatches []common.Batch) []testBatch {
  47. tBatches := []testBatch{}
  48. for _, cBatch := range cBatches {
  49. block := common.Block{}
  50. found := false
  51. for _, b := range blocks {
  52. if b.EthBlockNum == cBatch.EthBlockNum {
  53. block = b
  54. found = true
  55. break
  56. }
  57. }
  58. if !found {
  59. panic("block not found")
  60. }
  61. collectedFees := make(map[common.TokenID]string)
  62. for k, v := range cBatch.CollectedFees {
  63. collectedFees[k] = v.String()
  64. }
  65. tBatch := testBatch{
  66. BatchNum: cBatch.BatchNum,
  67. EthBlockNum: cBatch.EthBlockNum,
  68. EthBlockHash: block.Hash,
  69. Timestamp: block.Timestamp,
  70. ForgerAddr: cBatch.ForgerAddr,
  71. CollectedFees: collectedFees,
  72. TotalFeesUSD: cBatch.TotalFeesUSD,
  73. StateRoot: cBatch.StateRoot.String(),
  74. NumAccounts: cBatch.NumAccounts,
  75. ExitRoot: cBatch.ExitRoot.String(),
  76. ForgeL1TxsNum: cBatch.ForgeL1TxsNum,
  77. SlotNum: cBatch.SlotNum,
  78. }
  79. tBatches = append(tBatches, tBatch)
  80. }
  81. return tBatches
  82. }
  83. func TestGetBatches(t *testing.T) {
  84. endpoint := apiURL + "batches"
  85. fetchedBatches := []testBatch{}
  86. appendIter := func(intr interface{}) {
  87. for i := 0; i < len(intr.(*testBatchesResponse).Batches); i++ {
  88. tmp, err := copystructure.Copy(intr.(*testBatchesResponse).Batches[i])
  89. if err != nil {
  90. panic(err)
  91. }
  92. fetchedBatches = append(fetchedBatches, tmp.(testBatch))
  93. }
  94. }
  95. // Get all (no filters)
  96. limit := 3
  97. path := fmt.Sprintf("%s?limit=%d&fromItem=", endpoint, limit)
  98. err := doGoodReqPaginated(path, historydb.OrderAsc, &testBatchesResponse{}, appendIter)
  99. assert.NoError(t, err)
  100. assertBatches(t, tc.batches, fetchedBatches)
  101. // minBatchNum
  102. fetchedBatches = []testBatch{}
  103. limit = 2
  104. minBatchNum := tc.batches[len(tc.batches)/2].BatchNum
  105. path = fmt.Sprintf("%s?minBatchNum=%d&limit=%d&fromItem=", endpoint, minBatchNum, limit)
  106. err = doGoodReqPaginated(path, historydb.OrderAsc, &testBatchesResponse{}, appendIter)
  107. assert.NoError(t, err)
  108. minBatchNumBatches := []testBatch{}
  109. for i := 0; i < len(tc.batches); i++ {
  110. if tc.batches[i].BatchNum > minBatchNum {
  111. minBatchNumBatches = append(minBatchNumBatches, tc.batches[i])
  112. }
  113. }
  114. assertBatches(t, minBatchNumBatches, fetchedBatches)
  115. // maxBatchNum
  116. fetchedBatches = []testBatch{}
  117. limit = 1
  118. maxBatchNum := tc.batches[len(tc.batches)/2].BatchNum
  119. path = fmt.Sprintf("%s?maxBatchNum=%d&limit=%d&fromItem=", endpoint, maxBatchNum, limit)
  120. err = doGoodReqPaginated(path, historydb.OrderAsc, &testBatchesResponse{}, appendIter)
  121. assert.NoError(t, err)
  122. maxBatchNumBatches := []testBatch{}
  123. for i := 0; i < len(tc.batches); i++ {
  124. if tc.batches[i].BatchNum < maxBatchNum {
  125. maxBatchNumBatches = append(maxBatchNumBatches, tc.batches[i])
  126. }
  127. }
  128. assertBatches(t, maxBatchNumBatches, fetchedBatches)
  129. // slotNum
  130. fetchedBatches = []testBatch{}
  131. limit = 5
  132. slotNum := tc.batches[len(tc.batches)/2].SlotNum
  133. path = fmt.Sprintf("%s?slotNum=%d&limit=%d&fromItem=", endpoint, slotNum, limit)
  134. err = doGoodReqPaginated(path, historydb.OrderAsc, &testBatchesResponse{}, appendIter)
  135. assert.NoError(t, err)
  136. slotNumBatches := []testBatch{}
  137. for i := 0; i < len(tc.batches); i++ {
  138. if tc.batches[i].SlotNum == slotNum {
  139. slotNumBatches = append(slotNumBatches, tc.batches[i])
  140. }
  141. }
  142. assertBatches(t, slotNumBatches, fetchedBatches)
  143. // forgerAddr
  144. fetchedBatches = []testBatch{}
  145. limit = 10
  146. forgerAddr := tc.batches[len(tc.batches)/2].ForgerAddr
  147. path = fmt.Sprintf("%s?forgerAddr=%s&limit=%d&fromItem=", endpoint, forgerAddr.String(), limit)
  148. err = doGoodReqPaginated(path, historydb.OrderAsc, &testBatchesResponse{}, appendIter)
  149. assert.NoError(t, err)
  150. forgerAddrBatches := []testBatch{}
  151. for i := 0; i < len(tc.batches); i++ {
  152. if tc.batches[i].ForgerAddr == forgerAddr {
  153. forgerAddrBatches = append(forgerAddrBatches, tc.batches[i])
  154. }
  155. }
  156. assertBatches(t, forgerAddrBatches, fetchedBatches)
  157. // All, in reverse order
  158. fetchedBatches = []testBatch{}
  159. limit = 6
  160. path = fmt.Sprintf("%s?limit=%d&fromItem=", endpoint, limit)
  161. err = doGoodReqPaginated(path, historydb.OrderDesc, &testBatchesResponse{}, appendIter)
  162. assert.NoError(t, err)
  163. flippedBatches := []testBatch{}
  164. for i := len(tc.batches) - 1; i >= 0; i-- {
  165. flippedBatches = append(flippedBatches, tc.batches[i])
  166. }
  167. assertBatches(t, flippedBatches, fetchedBatches)
  168. // Mixed filters
  169. fetchedBatches = []testBatch{}
  170. limit = 1
  171. maxBatchNum = tc.batches[len(tc.batches)-len(tc.batches)/4].BatchNum
  172. minBatchNum = tc.batches[len(tc.batches)/4].BatchNum
  173. path = fmt.Sprintf("%s?minBatchNum=%d&maxBatchNum=%d&limit=%d&fromItem=", endpoint, minBatchNum, maxBatchNum, limit)
  174. err = doGoodReqPaginated(path, historydb.OrderAsc, &testBatchesResponse{}, appendIter)
  175. assert.NoError(t, err)
  176. minMaxBatchNumBatches := []testBatch{}
  177. for i := 0; i < len(tc.batches); i++ {
  178. if tc.batches[i].BatchNum < maxBatchNum && tc.batches[i].BatchNum > minBatchNum {
  179. minMaxBatchNumBatches = append(minMaxBatchNumBatches, tc.batches[i])
  180. }
  181. }
  182. assertBatches(t, minMaxBatchNumBatches, fetchedBatches)
  183. // 400
  184. // Invalid minBatchNum
  185. path = fmt.Sprintf("%s?minBatchNum=%d", endpoint, -2)
  186. err = doBadReq("GET", path, nil, 400)
  187. assert.NoError(t, err)
  188. // Invalid forgerAddr
  189. path = fmt.Sprintf("%s?forgerAddr=%s", endpoint, "0xG0000001")
  190. err = doBadReq("GET", path, nil, 400)
  191. assert.NoError(t, err)
  192. // 404
  193. path = fmt.Sprintf("%s?slotNum=%d&minBatchNum=%d", endpoint, 1, 25)
  194. err = doBadReq("GET", path, nil, 404)
  195. assert.NoError(t, err)
  196. }
  197. func TestGetBatch(t *testing.T) {
  198. endpoint := apiURL + "batches/"
  199. for _, batch := range tc.batches {
  200. fetchedBatch := testBatch{}
  201. assert.NoError(
  202. t, doGoodReq(
  203. "GET",
  204. endpoint+strconv.Itoa(int(batch.BatchNum)),
  205. nil, &fetchedBatch,
  206. ),
  207. )
  208. assertBatch(t, batch, fetchedBatch)
  209. }
  210. // 400
  211. assert.NoError(t, doBadReq("GET", endpoint+"foo", nil, 400))
  212. // 404
  213. assert.NoError(t, doBadReq("GET", endpoint+"99999", nil, 404))
  214. }
  215. func assertBatches(t *testing.T, expected, actual []testBatch) {
  216. assert.Equal(t, len(expected), len(actual))
  217. for i := 0; i < len(expected); i++ {
  218. assertBatch(t, expected[i], actual[i])
  219. }
  220. }
  221. func assertBatch(t *testing.T, expected, actual testBatch) {
  222. assert.Equal(t, expected.Timestamp.Unix(), actual.Timestamp.Unix())
  223. expected.Timestamp = actual.Timestamp
  224. actual.ItemID = expected.ItemID
  225. assert.Equal(t, expected, actual)
  226. }