hermez-node/api/api_test.go
api/api_test.goRaw
1package api
2
3import (
4 "context"
5 "encoding/json"
6 "errors"
7 "fmt"
8 "io"
9 "io/ioutil"
10 "math/big"
11 "net/http"
12 "os"
13 "strconv"
14 "testing"
15 "time"
16
17 ethCommon "github.com/ethereum/go-ethereum/common"
18 swagger "github.com/getkin/kin-openapi/openapi3filter"
19 "github.com/gin-gonic/gin"
20 "github.com/hermeznetwork/hermez-node/common"
21 "github.com/hermeznetwork/hermez-node/db"
22 "github.com/hermeznetwork/hermez-node/db/historydb"
23 "github.com/hermeznetwork/hermez-node/db/l2db"
24 "github.com/hermeznetwork/hermez-node/db/statedb"
25 "github.com/hermeznetwork/hermez-node/log"
26 "github.com/hermeznetwork/hermez-node/test"
27 "github.com/hermeznetwork/hermez-node/test/til"
28 "github.com/hermeznetwork/tracerr"
29)
30
31// Pendinger is an interface that allows getting last returned item ID and PendingItems to be used for building fromItem
32// when testing paginated endpoints.
33type Pendinger interface {
34 GetPending() (pendingItems, lastItemID uint64)
35 Len() int
36 New() Pendinger
37}
38
39const apiPort = ":4010"
40const apiURL = "http://localhost" + apiPort + "/"
41
42var SetBlockchain = `
43 Type: Blockchain
44
45 AddToken(1)
46 AddToken(2)
47 AddToken(3)
48 AddToken(4)
49 AddToken(5)
50 AddToken(6)
51 AddToken(7)
52 AddToken(8)
53 > block
54
55 // Coordinator accounts, Idxs: 256, 257
56 CreateAccountCoordinator(0) Coord
57 CreateAccountCoordinator(1) Coord
58
59 // close Block:0, Batch:0
60 > batch
61
62 CreateAccountDeposit(0) A: 11111111100000000000
63 CreateAccountDeposit(1) C: 22222222200000000000
64 CreateAccountCoordinator(0) C
65
66 // close Block:0, Batch:1
67 > batchL1
68 // Expected balances:
69 // Coord(0): 0, Coord(1): 0
70 // C(0): 0
71
72 CreateAccountDeposit(1) A: 33333333300000000000
73
74 // close Block:0, Batch:2
75 > batchL1
76
77 // close Block:0, Batch:3
78 > batchL1
79
80 CreateAccountDepositTransfer(0) B-A: 44444444400000000000, 123444444400000000000
81
82 // close Block:0, Batch:4
83 > batchL1
84 CreateAccountDeposit(0) D: 55555555500000000000
85
86 // close Block:0, Batch:5
87 > batchL1
88
89 CreateAccountCoordinator(1) B
90
91 Transfer(1) A-B: 11111100000000000 (2)
92 Transfer(0) B-C: 22222200000000000 (3)
93
94 // close Block:0, Batch:6
95 > batchL1 // forge L1User{1}, forge L1Coord{2}, forge L2{2}
96
97 Deposit(0) C: 66666666600000000000
98 DepositTransfer(0) C-D: 77777777700000000000, 12377777700000000000
99
100 Transfer(0) A-B: 33333300000000000 (111)
101 Transfer(0) C-A: 44444400000000000 (222)
102 Transfer(1) B-C: 55555500000000000 (123)
103 Exit(0) A: 66666600000000000 (44)
104
105 ForceTransfer(0) D-B: 77777700000000000
106 ForceExit(0) B: 88888800000000000
107
108 // close Block:0, Batch:7
109 > batchL1
110 > block
111
112 Transfer(0) D-A: 99999900000000000 (77)
113 Transfer(0) B-D: 12312300000000000 (55)
114
115 // close Block:1, Batch:0
116 > batchL1
117
118 CreateAccountCoordinator(0) F
119
120 CreateAccountCoordinator(0) G
121 CreateAccountCoordinator(0) H
122 CreateAccountCoordinator(0) I
123 CreateAccountCoordinator(0) J
124 CreateAccountCoordinator(0) K
125 CreateAccountCoordinator(0) L
126 CreateAccountCoordinator(0) M
127 CreateAccountCoordinator(0) N
128 CreateAccountCoordinator(0) O
129 CreateAccountCoordinator(0) P
130
131 CreateAccountCoordinator(5) G
132 CreateAccountCoordinator(5) H
133 CreateAccountCoordinator(5) I
134 CreateAccountCoordinator(5) J
135 CreateAccountCoordinator(5) K
136 CreateAccountCoordinator(5) L
137 CreateAccountCoordinator(5) M
138 CreateAccountCoordinator(5) N
139 CreateAccountCoordinator(5) O
140 CreateAccountCoordinator(5) P
141
142 CreateAccountCoordinator(2) G
143 CreateAccountCoordinator(2) H
144 CreateAccountCoordinator(2) I
145 CreateAccountCoordinator(2) J
146 CreateAccountCoordinator(2) K
147 CreateAccountCoordinator(2) L
148 CreateAccountCoordinator(2) M
149 CreateAccountCoordinator(2) N
150 CreateAccountCoordinator(2) O
151 CreateAccountCoordinator(2) P
152
153
154 > batch
155 > block
156 > batch
157 > block
158 > batch
159 > block
160`
161
162type testCommon struct {
163 blocks []common.Block
164 tokens []historydb.TokenWithUSD
165 batches []testBatch
166 fullBatches []testFullBatch
167 coordinators []historydb.CoordinatorAPI
168 accounts []testAccount
169 txs []testTx
170 exits []testExit
171 poolTxsToSend []testPoolTxSend
172 poolTxsToReceive []testPoolTxReceive
173 auths []testAuth
174 router *swagger.Router
175 bids []testBid
176 slots []testSlot
177 auctionVars common.AuctionVariables
178 rollupVars common.RollupVariables
179 wdelayerVars common.WDelayerVariables
180}
181
182var tc testCommon
183var config configAPI
184var api *API
185
186// TestMain initializes the API server, and fill HistoryDB and StateDB with fake data,
187// emulating the task of the synchronizer in order to have data to be returned
188// by the API endpoints that will be tested
189func TestMain(m *testing.M) {
190 // Initializations
191 // Swagger
192 router := swagger.NewRouter().WithSwaggerFromFile("./swagger.yml")
193 // HistoryDB
194 pass := os.Getenv("POSTGRES_PASS")
195
196 database, err := db.InitSQLDB(5432, "localhost", "hermez", pass, "hermez")
197 if err != nil {
198 panic(err)
199 }
200 hdb := historydb.NewHistoryDB(database)
201 if err != nil {
202 panic(err)
203 }
204 // StateDB
205 dir, err := ioutil.TempDir("", "tmpdb")
206 if err != nil {
207 panic(err)
208 }
209 defer func() {
210 if err := os.RemoveAll(dir); err != nil {
211 panic(err)
212 }
213 }()
214 chainID := uint16(0)
215 sdb, err := statedb.NewStateDB(dir, statedb.TypeTxSelector, 0, chainID)
216 if err != nil {
217 panic(err)
218 }
219 // L2DB
220 l2DB := l2db.NewL2DB(database, 10, 100, 24*time.Hour)
221 test.WipeDB(l2DB.DB()) // this will clean HistoryDB and L2DB
222
223 // Config (smart contract constants)
224 _config := getConfigTest(chainID)
225 config = configAPI{
226 RollupConstants: *newRollupConstants(_config.RollupConstants),
227 AuctionConstants: _config.AuctionConstants,
228 WDelayerConstants: _config.WDelayerConstants,
229 }
230
231 // API
232 apiGin := gin.Default()
233 api, err = NewAPI(
234 true,
235 true,
236 apiGin,
237 hdb,
238 sdb,
239 l2DB,
240 &_config,
241 )
242 if err != nil {
243 panic(err)
244 }
245 // Start server
246 server := &http.Server{Addr: apiPort, Handler: apiGin}
247 go func() {
248 if err := server.ListenAndServe(); err != nil && tracerr.Unwrap(err) != http.ErrServerClosed {
249 panic(err)
250 }
251 }()
252
253 // Reset DB
254 test.WipeDB(api.h.DB())
255
256 // Genratre blockchain data with til
257 tcc := til.NewContext(chainID, common.RollupConstMaxL1UserTx)
258 tilCfgExtra := til.ConfigExtra{
259 BootCoordAddr: ethCommon.HexToAddress("0xE39fEc6224708f0772D2A74fd3f9055A90E0A9f2"),
260 CoordUser: "Coord",
261 }
262 blocksData, err := tcc.GenerateBlocks(SetBlockchain)
263 if err != nil {
264 panic(err)
265 }
266 err = tcc.FillBlocksExtra(blocksData, &tilCfgExtra)
267 if err != nil {
268 panic(err)
269 }
270 AddAditionalInformation(blocksData)
271 // Generate L2 Txs with til
272 commonPoolTxs, err := tcc.GeneratePoolL2Txs(til.SetPoolL2MinimumFlow0)
273 if err != nil {
274 panic(err)
275 }
276
277 // Extract til generated data, and add it to HistoryDB
278 var commonBlocks []common.Block
279 var commonBatches []common.Batch
280 var commonAccounts []common.Account
281 var commonExitTree []common.ExitInfo
282 var commonL1Txs []common.L1Tx
283 var commonL2Txs []common.L2Tx
284 // Add ETH token at the beginning of the array
285 testTokens := []historydb.TokenWithUSD{}
286 ethUSD := float64(500)
287 ethNow := time.Now()
288 testTokens = append(testTokens, historydb.TokenWithUSD{
289 TokenID: test.EthToken.TokenID,
290 EthBlockNum: test.EthToken.EthBlockNum,
291 EthAddr: test.EthToken.EthAddr,
292 Name: test.EthToken.Name,
293 Symbol: test.EthToken.Symbol,
294 Decimals: test.EthToken.Decimals,
295 USD: ðUSD,
296 USDUpdate: ðNow,
297 })
298 err = api.h.UpdateTokenValue(test.EthToken.Symbol, ethUSD)
299 if err != nil {
300 panic(err)
301 }
302 for _, block := range blocksData {
303 // Insert block into HistoryDB
304 // nolint reason: block is used as read only in the function
305 if err := api.h.AddBlockSCData(&block); err != nil { //nolint:gosec
306 panic(err)
307 }
308 // Extract data
309 commonBlocks = append(commonBlocks, block.Block)
310 for i, tkn := range block.Rollup.AddedTokens {
311 token := historydb.TokenWithUSD{
312 TokenID: tkn.TokenID,
313 EthBlockNum: tkn.EthBlockNum,
314 EthAddr: tkn.EthAddr,
315 Name: tkn.Name,
316 Symbol: tkn.Symbol,
317 Decimals: tkn.Decimals,
318 }
319 value := float64(i + 423)
320 now := time.Now().UTC()
321 token.USD = &value
322 token.USDUpdate = &now
323 // Set value in DB
324 err = api.h.UpdateTokenValue(token.Symbol, value)
325 if err != nil {
326 panic(err)
327 }
328 testTokens = append(testTokens, token)
329 }
330 // Set USD value for tokens in DB
331 commonL1Txs = append(commonL1Txs, block.Rollup.L1UserTxs...)
332 for _, batch := range block.Rollup.Batches {
333 commonL2Txs = append(commonL2Txs, batch.L2Txs...)
334 for i := range batch.CreatedAccounts {
335 batch.CreatedAccounts[i].Nonce = common.Nonce(i)
336 commonAccounts = append(commonAccounts, batch.CreatedAccounts[i])
337 }
338 commonBatches = append(commonBatches, batch.Batch)
339 commonExitTree = append(commonExitTree, batch.ExitTree...)
340 commonL1Txs = append(commonL1Txs, batch.L1CoordinatorTxs...)
341 }
342 }
343
344 // lastBlockNum2 := blocksData[len(blocksData)-1].Block.EthBlockNum
345
346 // Add accounts to StateDB
347 for i := 0; i < len(commonAccounts); i++ {
348 if _, err := api.s.CreateAccount(commonAccounts[i].Idx, &commonAccounts[i]); err != nil {
349 panic(err)
350 }
351 }
352
353 // Generate Coordinators and add them to HistoryDB
354 const nCoords = 10
355 commonCoords := test.GenCoordinators(nCoords, commonBlocks)
356 if err := api.h.AddCoordinators(commonCoords); err != nil {
357 panic(err)
358 }
359
360 // Generate Bids and add them to HistoryDB
361 const nBids = 20
362 commonBids := test.GenBids(nBids, commonBlocks, commonCoords)
363 if err = api.h.AddBids(commonBids); err != nil {
364 panic(err)
365 }
366
367 // Generate SC vars and add them to HistoryDB (if needed)
368 var defaultSlotSetBid [6]*big.Int = [6]*big.Int{big.NewInt(10), big.NewInt(10), big.NewInt(10), big.NewInt(10), big.NewInt(10), big.NewInt(10)}
369 auctionVars := common.AuctionVariables{
370 EthBlockNum: int64(2),
371 DonationAddress: ethCommon.HexToAddress("0x1111111111111111111111111111111111111111"),
372 DefaultSlotSetBid: defaultSlotSetBid,
373 Outbidding: uint16(1),
374 SlotDeadline: uint8(20),
375 BootCoordinator: ethCommon.HexToAddress("0x1111111111111111111111111111111111111111"),
376 BootCoordinatorURL: "https://boot.coordinator.io",
377 ClosedAuctionSlots: uint16(2),
378 OpenAuctionSlots: uint16(5),
379 }
380
381 var buckets [common.RollupConstNumBuckets]common.BucketParams
382 for i := range buckets {
383 buckets[i].CeilUSD = big.NewInt(int64(i) * 10)
384 buckets[i].Withdrawals = big.NewInt(int64(i) * 100)
385 buckets[i].BlockWithdrawalRate = big.NewInt(int64(i) * 1000)
386 buckets[i].MaxWithdrawals = big.NewInt(int64(i) * 10000)
387 }
388
389 rollupVars := common.RollupVariables{
390 EthBlockNum: int64(3),
391 FeeAddToken: big.NewInt(100),
392 ForgeL1L2BatchTimeout: int64(44),
393 WithdrawalDelay: uint64(3000),
394 Buckets: buckets,
395 SafeMode: false,
396 }
397
398 wdelayerVars := common.WDelayerVariables{
399 WithdrawalDelay: uint64(3000),
400 }
401
402 err = api.h.AddAuctionVars(&auctionVars)
403 if err != nil {
404 panic(err)
405 }
406
407 // Generate test data, as expected to be received/sended from/to the API
408 testCoords := genTestCoordinators(commonCoords)
409 testBids := genTestBids(commonBlocks, testCoords, commonBids)
410 testExits := genTestExits(commonExitTree, testTokens, commonAccounts)
411 testTxs := genTestTxs(commonL1Txs, commonL2Txs, commonAccounts, testTokens, commonBlocks)
412 testBatches, testFullBatches := genTestBatches(commonBlocks, commonBatches, testTxs)
413 poolTxsToSend, poolTxsToReceive := genTestPoolTxs(commonPoolTxs, testTokens, commonAccounts)
414 tc = testCommon{
415 blocks: commonBlocks,
416 tokens: testTokens,
417 batches: testBatches,
418 fullBatches: testFullBatches,
419 coordinators: testCoords,
420 accounts: genTestAccounts(commonAccounts, testTokens),
421 txs: testTxs,
422 exits: testExits,
423 poolTxsToSend: poolTxsToSend,
424 poolTxsToReceive: poolTxsToReceive,
425 auths: genTestAuths(test.GenAuths(5, _config.ChainID, _config.HermezAddress)),
426 router: router,
427 bids: testBids,
428 slots: api.genTestSlots(
429 20,
430 commonBlocks[len(commonBlocks)-1].Num,
431 testBids,
432 auctionVars,
433 ),
434 auctionVars: auctionVars,
435 rollupVars: rollupVars,
436 wdelayerVars: wdelayerVars,
437 }
438
439 // Fake server
440 if os.Getenv("FAKE_SERVER") == "yes" {
441 for {
442 log.Info("Running fake server at " + apiURL + " until ^C is received")
443 time.Sleep(30 * time.Second)
444 }
445 }
446 // Run tests
447 result := m.Run()
448 // Stop server
449 if err := server.Shutdown(context.Background()); err != nil {
450 panic(err)
451 }
452 if err := database.Close(); err != nil {
453 panic(err)
454 }
455 if err := os.RemoveAll(dir); err != nil {
456 panic(err)
457 }
458 os.Exit(result)
459}
460
461func doGoodReqPaginated(
462 path, order string,
463 iterStruct Pendinger,
464 appendIter func(res interface{}),
465) error {
466 var next uint64
467 firstIte := true
468 expectedTotal := 0
469 totalReceived := 0
470 for {
471 // Calculate fromItem
472 iterPath := path
473 if !firstIte {
474 iterPath += "&fromItem=" + strconv.Itoa(int(next))
475 }
476 // Call API to get this iteration items
477 iterStruct = iterStruct.New()
478 if err := doGoodReq(
479 "GET", iterPath+"&order="+order, nil,
480 iterStruct,
481 ); err != nil {
482 return tracerr.Wrap(err)
483 }
484 appendIter(iterStruct)
485 // Keep iterating?
486 remaining, lastID := iterStruct.GetPending()
487 if remaining == 0 {
488 break
489 }
490 if order == historydb.OrderDesc {
491 next = lastID - 1
492 } else {
493 next = lastID + 1
494 }
495 // Check that the expected amount of items is consistent across iterations
496 totalReceived += iterStruct.Len()
497 if firstIte {
498 firstIte = false
499 expectedTotal = totalReceived + int(remaining)
500 }
501 if expectedTotal != totalReceived+int(remaining) {
502 panic(fmt.Sprintf(
503 "pagination error, totalReceived + remaining should be %d, but is %d",
504 expectedTotal, totalReceived+int(remaining),
505 ))
506 }
507 }
508 return nil
509}
510
511func doGoodReq(method, path string, reqBody io.Reader, returnStruct interface{}) error {
512 ctx := context.Background()
513 client := &http.Client{}
514 httpReq, err := http.NewRequest(method, path, reqBody)
515 if err != nil {
516 return tracerr.Wrap(err)
517 }
518 if reqBody != nil {
519 httpReq.Header.Add("Content-Type", "application/json")
520 }
521 route, pathParams, err := tc.router.FindRoute(httpReq.Method, httpReq.URL)
522 if err != nil {
523 return tracerr.Wrap(err)
524 }
525 // Validate request against swagger spec
526 requestValidationInput := &swagger.RequestValidationInput{
527 Request: httpReq,
528 PathParams: pathParams,
529 Route: route,
530 }
531 if err := swagger.ValidateRequest(ctx, requestValidationInput); err != nil {
532 return tracerr.Wrap(err)
533 }
534 // Do API call
535 resp, err := client.Do(httpReq)
536 if err != nil {
537 return tracerr.Wrap(err)
538 }
539 if resp.Body == nil && returnStruct != nil {
540 return tracerr.Wrap(errors.New("Nil body"))
541 }
542 //nolint
543 defer resp.Body.Close()
544 body, err := ioutil.ReadAll(resp.Body)
545 if err != nil {
546 return tracerr.Wrap(err)
547 }
548 if resp.StatusCode != 200 {
549 return tracerr.Wrap(fmt.Errorf("%d response. Body: %s", resp.StatusCode, string(body)))
550 }
551 if returnStruct == nil {
552 return nil
553 }
554 // Unmarshal body into return struct
555 if err := json.Unmarshal(body, returnStruct); err != nil {
556 log.Error("invalid json: " + string(body))
557 log.Error(err)
558 return tracerr.Wrap(err)
559 }
560 // log.Info(string(body))
561 // Validate response against swagger spec
562 responseValidationInput := &swagger.ResponseValidationInput{
563 RequestValidationInput: requestValidationInput,
564 Status: resp.StatusCode,
565 Header: resp.Header,
566 }
567 responseValidationInput = responseValidationInput.SetBodyBytes(body)
568 return swagger.ValidateResponse(ctx, responseValidationInput)
569}
570
571func doBadReq(method, path string, reqBody io.Reader, expectedResponseCode int) error {
572 ctx := context.Background()
573 client := &http.Client{}
574 httpReq, _ := http.NewRequest(method, path, reqBody)
575 route, pathParams, err := tc.router.FindRoute(httpReq.Method, httpReq.URL)
576 if err != nil {
577 return tracerr.Wrap(err)
578 }
579 // Validate request against swagger spec
580 requestValidationInput := &swagger.RequestValidationInput{
581 Request: httpReq,
582 PathParams: pathParams,
583 Route: route,
584 }
585 if err := swagger.ValidateRequest(ctx, requestValidationInput); err != nil {
586 if expectedResponseCode != 400 {
587 return tracerr.Wrap(err)
588 }
589 log.Warn("The request does not match the API spec")
590 }
591 // Do API call
592 resp, err := client.Do(httpReq)
593 if err != nil {
594 return tracerr.Wrap(err)
595 }
596 if resp.Body == nil {
597 return tracerr.Wrap(errors.New("Nil body"))
598 }
599 //nolint
600 defer resp.Body.Close()
601 body, err := ioutil.ReadAll(resp.Body)
602 if err != nil {
603 return tracerr.Wrap(err)
604 }
605 if resp.StatusCode != expectedResponseCode {
606 return tracerr.Wrap(fmt.Errorf("Unexpected response code: %d. Body: %s", resp.StatusCode, string(body)))
607 }
608 // Validate response against swagger spec
609 responseValidationInput := &swagger.ResponseValidationInput{
610 RequestValidationInput: requestValidationInput,
611 Status: resp.StatusCode,
612 Header: resp.Header,
613 }
614 responseValidationInput = responseValidationInput.SetBodyBytes(body)
615 return swagger.ValidateResponse(ctx, responseValidationInput)
616}
617
618// test helpers
619
620func getTimestamp(blockNum int64, blocks []common.Block) time.Time {
621 for i := 0; i < len(blocks); i++ {
622 if blocks[i].Num == blockNum {
623 return blocks[i].Timestamp
624 }
625 }
626 panic("timesamp not found")
627}
628
629func getTokenByID(id common.TokenID, tokens []historydb.TokenWithUSD) historydb.TokenWithUSD {
630 for i := 0; i < len(tokens); i++ {
631 if tokens[i].TokenID == id {
632 return tokens[i]
633 }
634 }
635 panic("token not found")
636}
637
638func getTokenByIdx(idx common.Idx, tokens []historydb.TokenWithUSD, accs []common.Account) historydb.TokenWithUSD {
639 for _, acc := range accs {
640 if idx == acc.Idx {
641 return getTokenByID(acc.TokenID, tokens)
642 }
643 }
644 panic("token not found")
645}
646
647func getAccountByIdx(idx common.Idx, accs []common.Account) *common.Account {
648 for _, acc := range accs {
649 if acc.Idx == idx {
650 return &acc
651 }
652 }
653 panic("account not found")
654}
655
656func getBlockByNum(ethBlockNum int64, blocks []common.Block) common.Block {
657 for _, b := range blocks {
658 if b.Num == ethBlockNum {
659 return b
660 }
661 }
662 panic("block not found")
663}
664
665func getCoordinatorByBidder(bidder ethCommon.Address, coordinators []historydb.CoordinatorAPI) historydb.CoordinatorAPI {
666 for _, c := range coordinators {
667 if c.Bidder == bidder {
668 return c
669 }
670 }
671 panic("coordinator not found")
672}