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.

281 lines
8.8 KiB

Update coordinator, call all api update functions - Common: - Rename Block.EthBlockNum to Block.Num to avoid unneeded repetition - API: - Add UpdateNetworkInfoBlock to update just block information, to be used when the node is not yet synchronized - Node: - Call API.UpdateMetrics and UpdateRecommendedFee in a loop, with configurable time intervals - Synchronizer: - When mapping events by TxHash, use an array to support the possibility of multiple calls of the same function happening in the same transaction (for example, a smart contract in a single transaction could call withdraw with delay twice, which would generate 2 withdraw events, and 2 deposit events). - In Stats, keep entire LastBlock instead of just the blockNum - In Stats, add lastL1BatchBlock - Test Stats and SCVars - Coordinator: - Enable writing the BatchInfo in every step of the pipeline to disk (with JSON text files) for debugging purposes. - Move the Pipeline functionality from the Coordinator to its own struct (Pipeline) - Implement shouldL1lL2Batch - In TxManager, implement logic to perform several attempts when doing ethereum node RPC calls before considering the error. (Both for calls to forgeBatch and transaction receipt) - In TxManager, reorganize the flow and note the specific points in which actions are made when err != nil - HistoryDB: - Implement GetLastL1BatchBlockNum: returns the blockNum of the latest forged l1Batch, to help the coordinator decide when to forge an L1Batch. - EthereumClient and test.Client: - Update EthBlockByNumber to return the last block when the passed number is -1.
3 years ago
  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. func (t testBatchesResponse) New() Pendinger { return &testBatchesResponse{} }
  41. type testFullBatch struct {
  42. Batch testBatch `json:"batch"`
  43. Txs []testTx `json:"transactions"`
  44. }
  45. func genTestBatches(
  46. blocks []common.Block,
  47. cBatches []common.Batch,
  48. txs []testTx,
  49. ) ([]testBatch, []testFullBatch) {
  50. tBatches := []testBatch{}
  51. for i := 0; i < len(cBatches); i++ {
  52. block := common.Block{}
  53. found := false
  54. for _, b := range blocks {
  55. if b.Num == cBatches[i].EthBlockNum {
  56. block = b
  57. found = true
  58. break
  59. }
  60. }
  61. if !found {
  62. panic("block not found")
  63. }
  64. collectedFees := make(map[common.TokenID]string)
  65. for k, v := range cBatches[i].CollectedFees {
  66. collectedFees[k] = v.String()
  67. }
  68. tBatch := testBatch{
  69. BatchNum: cBatches[i].BatchNum,
  70. EthBlockNum: cBatches[i].EthBlockNum,
  71. EthBlockHash: block.Hash,
  72. Timestamp: block.Timestamp,
  73. ForgerAddr: cBatches[i].ForgerAddr,
  74. CollectedFees: collectedFees,
  75. TotalFeesUSD: cBatches[i].TotalFeesUSD,
  76. StateRoot: cBatches[i].StateRoot.String(),
  77. NumAccounts: cBatches[i].NumAccounts,
  78. ExitRoot: cBatches[i].ExitRoot.String(),
  79. ForgeL1TxsNum: cBatches[i].ForgeL1TxsNum,
  80. SlotNum: cBatches[i].SlotNum,
  81. }
  82. tBatches = append(tBatches, tBatch)
  83. }
  84. fullBatches := []testFullBatch{}
  85. for i := 0; i < len(tBatches); i++ {
  86. forgedTxs := []testTx{}
  87. for j := 0; j < len(txs); j++ {
  88. if txs[j].BatchNum != nil && *txs[j].BatchNum == tBatches[i].BatchNum {
  89. forgedTxs = append(forgedTxs, txs[j])
  90. }
  91. }
  92. fullBatches = append(fullBatches, testFullBatch{
  93. Batch: tBatches[i],
  94. Txs: forgedTxs,
  95. })
  96. }
  97. return tBatches, fullBatches
  98. }
  99. func TestGetBatches(t *testing.T) {
  100. endpoint := apiURL + "batches"
  101. fetchedBatches := []testBatch{}
  102. appendIter := func(intr interface{}) {
  103. for i := 0; i < len(intr.(*testBatchesResponse).Batches); i++ {
  104. tmp, err := copystructure.Copy(intr.(*testBatchesResponse).Batches[i])
  105. if err != nil {
  106. panic(err)
  107. }
  108. fetchedBatches = append(fetchedBatches, tmp.(testBatch))
  109. }
  110. }
  111. // Get all (no filters)
  112. limit := 3
  113. path := fmt.Sprintf("%s?limit=%d", endpoint, limit)
  114. err := doGoodReqPaginated(path, historydb.OrderAsc, &testBatchesResponse{}, appendIter)
  115. assert.NoError(t, err)
  116. assertBatches(t, tc.batches, fetchedBatches)
  117. // minBatchNum
  118. fetchedBatches = []testBatch{}
  119. limit = 2
  120. minBatchNum := tc.batches[len(tc.batches)/2].BatchNum
  121. path = fmt.Sprintf("%s?minBatchNum=%d&limit=%d", endpoint, minBatchNum, limit)
  122. err = doGoodReqPaginated(path, historydb.OrderAsc, &testBatchesResponse{}, appendIter)
  123. assert.NoError(t, err)
  124. minBatchNumBatches := []testBatch{}
  125. for i := 0; i < len(tc.batches); i++ {
  126. if tc.batches[i].BatchNum > minBatchNum {
  127. minBatchNumBatches = append(minBatchNumBatches, tc.batches[i])
  128. }
  129. }
  130. assertBatches(t, minBatchNumBatches, fetchedBatches)
  131. // maxBatchNum
  132. fetchedBatches = []testBatch{}
  133. limit = 1
  134. maxBatchNum := tc.batches[len(tc.batches)/2].BatchNum
  135. path = fmt.Sprintf("%s?maxBatchNum=%d&limit=%d", endpoint, maxBatchNum, limit)
  136. err = doGoodReqPaginated(path, historydb.OrderAsc, &testBatchesResponse{}, appendIter)
  137. assert.NoError(t, err)
  138. maxBatchNumBatches := []testBatch{}
  139. for i := 0; i < len(tc.batches); i++ {
  140. if tc.batches[i].BatchNum < maxBatchNum {
  141. maxBatchNumBatches = append(maxBatchNumBatches, tc.batches[i])
  142. }
  143. }
  144. assertBatches(t, maxBatchNumBatches, fetchedBatches)
  145. // slotNum
  146. fetchedBatches = []testBatch{}
  147. limit = 5
  148. slotNum := tc.batches[len(tc.batches)/2].SlotNum
  149. path = fmt.Sprintf("%s?slotNum=%d&limit=%d", endpoint, slotNum, limit)
  150. err = doGoodReqPaginated(path, historydb.OrderAsc, &testBatchesResponse{}, appendIter)
  151. assert.NoError(t, err)
  152. slotNumBatches := []testBatch{}
  153. for i := 0; i < len(tc.batches); i++ {
  154. if tc.batches[i].SlotNum == slotNum {
  155. slotNumBatches = append(slotNumBatches, tc.batches[i])
  156. }
  157. }
  158. assertBatches(t, slotNumBatches, fetchedBatches)
  159. // forgerAddr
  160. fetchedBatches = []testBatch{}
  161. limit = 10
  162. forgerAddr := tc.batches[len(tc.batches)/2].ForgerAddr
  163. path = fmt.Sprintf("%s?forgerAddr=%s&limit=%d", endpoint, forgerAddr.String(), limit)
  164. err = doGoodReqPaginated(path, historydb.OrderAsc, &testBatchesResponse{}, appendIter)
  165. assert.NoError(t, err)
  166. forgerAddrBatches := []testBatch{}
  167. for i := 0; i < len(tc.batches); i++ {
  168. if tc.batches[i].ForgerAddr == forgerAddr {
  169. forgerAddrBatches = append(forgerAddrBatches, tc.batches[i])
  170. }
  171. }
  172. assertBatches(t, forgerAddrBatches, fetchedBatches)
  173. // All, in reverse order
  174. fetchedBatches = []testBatch{}
  175. limit = 6
  176. path = fmt.Sprintf("%s?limit=%d", endpoint, limit)
  177. err = doGoodReqPaginated(path, historydb.OrderDesc, &testBatchesResponse{}, appendIter)
  178. assert.NoError(t, err)
  179. flippedBatches := []testBatch{}
  180. for i := len(tc.batches) - 1; i >= 0; i-- {
  181. flippedBatches = append(flippedBatches, tc.batches[i])
  182. }
  183. assertBatches(t, flippedBatches, fetchedBatches)
  184. // Mixed filters
  185. fetchedBatches = []testBatch{}
  186. limit = 1
  187. maxBatchNum = tc.batches[len(tc.batches)-len(tc.batches)/4].BatchNum
  188. minBatchNum = tc.batches[len(tc.batches)/4].BatchNum
  189. path = fmt.Sprintf("%s?minBatchNum=%d&maxBatchNum=%d&limit=%d", endpoint, minBatchNum, maxBatchNum, limit)
  190. err = doGoodReqPaginated(path, historydb.OrderAsc, &testBatchesResponse{}, appendIter)
  191. assert.NoError(t, err)
  192. minMaxBatchNumBatches := []testBatch{}
  193. for i := 0; i < len(tc.batches); i++ {
  194. if tc.batches[i].BatchNum < maxBatchNum && tc.batches[i].BatchNum > minBatchNum {
  195. minMaxBatchNumBatches = append(minMaxBatchNumBatches, tc.batches[i])
  196. }
  197. }
  198. assertBatches(t, minMaxBatchNumBatches, fetchedBatches)
  199. // 400
  200. // Invalid minBatchNum
  201. path = fmt.Sprintf("%s?minBatchNum=%d", endpoint, -2)
  202. err = doBadReq("GET", path, nil, 400)
  203. assert.NoError(t, err)
  204. // Invalid forgerAddr
  205. path = fmt.Sprintf("%s?forgerAddr=%s", endpoint, "0xG0000001")
  206. err = doBadReq("GET", path, nil, 400)
  207. assert.NoError(t, err)
  208. // 404
  209. path = fmt.Sprintf("%s?slotNum=%d&minBatchNum=%d", endpoint, 1, 25)
  210. err = doBadReq("GET", path, nil, 404)
  211. assert.NoError(t, err)
  212. }
  213. func TestGetBatch(t *testing.T) {
  214. endpoint := apiURL + "batches/"
  215. for _, batch := range tc.batches {
  216. fetchedBatch := testBatch{}
  217. assert.NoError(
  218. t, doGoodReq(
  219. "GET",
  220. endpoint+strconv.Itoa(int(batch.BatchNum)),
  221. nil, &fetchedBatch,
  222. ),
  223. )
  224. assertBatch(t, batch, fetchedBatch)
  225. }
  226. // 400
  227. assert.NoError(t, doBadReq("GET", endpoint+"foo", nil, 400))
  228. // 404
  229. assert.NoError(t, doBadReq("GET", endpoint+"99999", nil, 404))
  230. }
  231. func TestGetFullBatch(t *testing.T) {
  232. endpoint := apiURL + "full-batches/"
  233. for _, fullBatch := range tc.fullBatches {
  234. fetchedFullBatch := testFullBatch{}
  235. assert.NoError(
  236. t, doGoodReq(
  237. "GET",
  238. endpoint+strconv.Itoa(int(fullBatch.Batch.BatchNum)),
  239. nil, &fetchedFullBatch,
  240. ),
  241. )
  242. assertBatch(t, fullBatch.Batch, fetchedFullBatch.Batch)
  243. assertTxs(t, fullBatch.Txs, fetchedFullBatch.Txs)
  244. }
  245. // 400
  246. assert.NoError(t, doBadReq("GET", endpoint+"foo", nil, 400))
  247. // 404
  248. assert.NoError(t, doBadReq("GET", endpoint+"99999", nil, 404))
  249. }
  250. func assertBatches(t *testing.T, expected, actual []testBatch) {
  251. assert.Equal(t, len(expected), len(actual))
  252. for i := 0; i < len(expected); i++ {
  253. assertBatch(t, expected[i], actual[i])
  254. }
  255. }
  256. func assertBatch(t *testing.T, expected, actual testBatch) {
  257. assert.Equal(t, expected.Timestamp.Unix(), actual.Timestamp.Unix())
  258. expected.Timestamp = actual.Timestamp
  259. actual.ItemID = expected.ItemID
  260. assert.Equal(t, expected, actual)
  261. }