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.

285 lines
9.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. type testFullBatch struct {
  47. Batch testBatch `json:"batch"`
  48. Txs []testTx `json:"transactions"`
  49. }
  50. func genTestBatches(
  51. blocks []common.Block,
  52. cBatches []common.Batch,
  53. txs []testTx,
  54. ) ([]testBatch, []testFullBatch) {
  55. tBatches := []testBatch{}
  56. for _, cBatch := range cBatches {
  57. block := common.Block{}
  58. found := false
  59. for _, b := range blocks {
  60. if b.EthBlockNum == cBatch.EthBlockNum {
  61. block = b
  62. found = true
  63. break
  64. }
  65. }
  66. if !found {
  67. panic("block not found")
  68. }
  69. collectedFees := make(map[common.TokenID]string)
  70. for k, v := range cBatch.CollectedFees {
  71. collectedFees[k] = v.String()
  72. }
  73. tBatch := testBatch{
  74. BatchNum: cBatch.BatchNum,
  75. EthBlockNum: cBatch.EthBlockNum,
  76. EthBlockHash: block.Hash,
  77. Timestamp: block.Timestamp,
  78. ForgerAddr: cBatch.ForgerAddr,
  79. CollectedFees: collectedFees,
  80. TotalFeesUSD: cBatch.TotalFeesUSD,
  81. StateRoot: cBatch.StateRoot.String(),
  82. NumAccounts: cBatch.NumAccounts,
  83. ExitRoot: cBatch.ExitRoot.String(),
  84. ForgeL1TxsNum: cBatch.ForgeL1TxsNum,
  85. SlotNum: cBatch.SlotNum,
  86. }
  87. tBatches = append(tBatches, tBatch)
  88. }
  89. fullBatches := []testFullBatch{}
  90. for i := 0; i < len(tBatches); i++ {
  91. forgedTxs := []testTx{}
  92. for j := 0; j < len(txs); j++ {
  93. if txs[j].BatchNum != nil && *txs[j].BatchNum == tBatches[i].BatchNum {
  94. forgedTxs = append(forgedTxs, txs[j])
  95. }
  96. }
  97. fullBatches = append(fullBatches, testFullBatch{
  98. Batch: tBatches[i],
  99. Txs: forgedTxs,
  100. })
  101. }
  102. return tBatches, fullBatches
  103. }
  104. func TestGetBatches(t *testing.T) {
  105. endpoint := apiURL + "batches"
  106. fetchedBatches := []testBatch{}
  107. appendIter := func(intr interface{}) {
  108. for i := 0; i < len(intr.(*testBatchesResponse).Batches); i++ {
  109. tmp, err := copystructure.Copy(intr.(*testBatchesResponse).Batches[i])
  110. if err != nil {
  111. panic(err)
  112. }
  113. fetchedBatches = append(fetchedBatches, tmp.(testBatch))
  114. }
  115. }
  116. // Get all (no filters)
  117. limit := 3
  118. path := fmt.Sprintf("%s?limit=%d&fromItem=", endpoint, limit)
  119. err := doGoodReqPaginated(path, historydb.OrderAsc, &testBatchesResponse{}, appendIter)
  120. assert.NoError(t, err)
  121. assertBatches(t, tc.batches, fetchedBatches)
  122. // minBatchNum
  123. fetchedBatches = []testBatch{}
  124. limit = 2
  125. minBatchNum := tc.batches[len(tc.batches)/2].BatchNum
  126. path = fmt.Sprintf("%s?minBatchNum=%d&limit=%d&fromItem=", endpoint, minBatchNum, limit)
  127. err = doGoodReqPaginated(path, historydb.OrderAsc, &testBatchesResponse{}, appendIter)
  128. assert.NoError(t, err)
  129. minBatchNumBatches := []testBatch{}
  130. for i := 0; i < len(tc.batches); i++ {
  131. if tc.batches[i].BatchNum > minBatchNum {
  132. minBatchNumBatches = append(minBatchNumBatches, tc.batches[i])
  133. }
  134. }
  135. assertBatches(t, minBatchNumBatches, fetchedBatches)
  136. // maxBatchNum
  137. fetchedBatches = []testBatch{}
  138. limit = 1
  139. maxBatchNum := tc.batches[len(tc.batches)/2].BatchNum
  140. path = fmt.Sprintf("%s?maxBatchNum=%d&limit=%d&fromItem=", endpoint, maxBatchNum, limit)
  141. err = doGoodReqPaginated(path, historydb.OrderAsc, &testBatchesResponse{}, appendIter)
  142. assert.NoError(t, err)
  143. maxBatchNumBatches := []testBatch{}
  144. for i := 0; i < len(tc.batches); i++ {
  145. if tc.batches[i].BatchNum < maxBatchNum {
  146. maxBatchNumBatches = append(maxBatchNumBatches, tc.batches[i])
  147. }
  148. }
  149. assertBatches(t, maxBatchNumBatches, fetchedBatches)
  150. // slotNum
  151. fetchedBatches = []testBatch{}
  152. limit = 5
  153. slotNum := tc.batches[len(tc.batches)/2].SlotNum
  154. path = fmt.Sprintf("%s?slotNum=%d&limit=%d&fromItem=", endpoint, slotNum, limit)
  155. err = doGoodReqPaginated(path, historydb.OrderAsc, &testBatchesResponse{}, appendIter)
  156. assert.NoError(t, err)
  157. slotNumBatches := []testBatch{}
  158. for i := 0; i < len(tc.batches); i++ {
  159. if tc.batches[i].SlotNum == slotNum {
  160. slotNumBatches = append(slotNumBatches, tc.batches[i])
  161. }
  162. }
  163. assertBatches(t, slotNumBatches, fetchedBatches)
  164. // forgerAddr
  165. fetchedBatches = []testBatch{}
  166. limit = 10
  167. forgerAddr := tc.batches[len(tc.batches)/2].ForgerAddr
  168. path = fmt.Sprintf("%s?forgerAddr=%s&limit=%d&fromItem=", endpoint, forgerAddr.String(), limit)
  169. err = doGoodReqPaginated(path, historydb.OrderAsc, &testBatchesResponse{}, appendIter)
  170. assert.NoError(t, err)
  171. forgerAddrBatches := []testBatch{}
  172. for i := 0; i < len(tc.batches); i++ {
  173. if tc.batches[i].ForgerAddr == forgerAddr {
  174. forgerAddrBatches = append(forgerAddrBatches, tc.batches[i])
  175. }
  176. }
  177. assertBatches(t, forgerAddrBatches, fetchedBatches)
  178. // All, in reverse order
  179. fetchedBatches = []testBatch{}
  180. limit = 6
  181. path = fmt.Sprintf("%s?limit=%d&fromItem=", endpoint, limit)
  182. err = doGoodReqPaginated(path, historydb.OrderDesc, &testBatchesResponse{}, appendIter)
  183. assert.NoError(t, err)
  184. flippedBatches := []testBatch{}
  185. for i := len(tc.batches) - 1; i >= 0; i-- {
  186. flippedBatches = append(flippedBatches, tc.batches[i])
  187. }
  188. assertBatches(t, flippedBatches, fetchedBatches)
  189. // Mixed filters
  190. fetchedBatches = []testBatch{}
  191. limit = 1
  192. maxBatchNum = tc.batches[len(tc.batches)-len(tc.batches)/4].BatchNum
  193. minBatchNum = tc.batches[len(tc.batches)/4].BatchNum
  194. path = fmt.Sprintf("%s?minBatchNum=%d&maxBatchNum=%d&limit=%d&fromItem=", endpoint, minBatchNum, maxBatchNum, limit)
  195. err = doGoodReqPaginated(path, historydb.OrderAsc, &testBatchesResponse{}, appendIter)
  196. assert.NoError(t, err)
  197. minMaxBatchNumBatches := []testBatch{}
  198. for i := 0; i < len(tc.batches); i++ {
  199. if tc.batches[i].BatchNum < maxBatchNum && tc.batches[i].BatchNum > minBatchNum {
  200. minMaxBatchNumBatches = append(minMaxBatchNumBatches, tc.batches[i])
  201. }
  202. }
  203. assertBatches(t, minMaxBatchNumBatches, fetchedBatches)
  204. // 400
  205. // Invalid minBatchNum
  206. path = fmt.Sprintf("%s?minBatchNum=%d", endpoint, -2)
  207. err = doBadReq("GET", path, nil, 400)
  208. assert.NoError(t, err)
  209. // Invalid forgerAddr
  210. path = fmt.Sprintf("%s?forgerAddr=%s", endpoint, "0xG0000001")
  211. err = doBadReq("GET", path, nil, 400)
  212. assert.NoError(t, err)
  213. // 404
  214. path = fmt.Sprintf("%s?slotNum=%d&minBatchNum=%d", endpoint, 1, 25)
  215. err = doBadReq("GET", path, nil, 404)
  216. assert.NoError(t, err)
  217. }
  218. func TestGetBatch(t *testing.T) {
  219. endpoint := apiURL + "batches/"
  220. for _, batch := range tc.batches {
  221. fetchedBatch := testBatch{}
  222. assert.NoError(
  223. t, doGoodReq(
  224. "GET",
  225. endpoint+strconv.Itoa(int(batch.BatchNum)),
  226. nil, &fetchedBatch,
  227. ),
  228. )
  229. assertBatch(t, batch, fetchedBatch)
  230. }
  231. // 400
  232. assert.NoError(t, doBadReq("GET", endpoint+"foo", nil, 400))
  233. // 404
  234. assert.NoError(t, doBadReq("GET", endpoint+"99999", nil, 404))
  235. }
  236. func TestGetFullBatch(t *testing.T) {
  237. endpoint := apiURL + "full-batches/"
  238. for _, fullBatch := range tc.fullBatches {
  239. fetchedFullBatch := testFullBatch{}
  240. assert.NoError(
  241. t, doGoodReq(
  242. "GET",
  243. endpoint+strconv.Itoa(int(fullBatch.Batch.BatchNum)),
  244. nil, &fetchedFullBatch,
  245. ),
  246. )
  247. assertBatch(t, fullBatch.Batch, fetchedFullBatch.Batch)
  248. assertTxs(t, fullBatch.Txs, fetchedFullBatch.Txs)
  249. }
  250. // 400
  251. assert.NoError(t, doBadReq("GET", endpoint+"foo", nil, 400))
  252. // 404
  253. assert.NoError(t, doBadReq("GET", endpoint+"99999", nil, 404))
  254. }
  255. func assertBatches(t *testing.T, expected, actual []testBatch) {
  256. assert.Equal(t, len(expected), len(actual))
  257. for i := 0; i < len(expected); i++ {
  258. assertBatch(t, expected[i], actual[i])
  259. }
  260. }
  261. func assertBatch(t *testing.T, expected, actual testBatch) {
  262. assert.Equal(t, expected.Timestamp.Unix(), actual.Timestamp.Unix())
  263. expected.Timestamp = actual.Timestamp
  264. actual.ItemID = expected.ItemID
  265. assert.Equal(t, expected, actual)
  266. }