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.

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