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 sdb, err := statedb.NewStateDB(dir, statedb.TypeTxSelector, 0)
215 if err != nil {
216 panic(err)
217 }
218 // L2DB
219 l2DB := l2db.NewL2DB(database, 10, 100, 24*time.Hour)
220 test.WipeDB(l2DB.DB()) // this will clean HistoryDB and L2DB
221
222 // Config (smart contract constants)
223 _config := getConfigTest()
224 config = configAPI{
225 RollupConstants: *newRollupConstants(_config.RollupConstants),
226 AuctionConstants: _config.AuctionConstants,
227 WDelayerConstants: _config.WDelayerConstants,
228 }
229
230 // API
231 apiGin := gin.Default()
232 api, err = NewAPI(
233 true,
234 true,
235 apiGin,
236 hdb,
237 sdb,
238 l2DB,
239 &_config,
240 )
241 if err != nil {
242 panic(err)
243 }
244 // Start server
245 server := &http.Server{Addr: apiPort, Handler: apiGin}
246 go func() {
247 if err := server.ListenAndServe(); err != nil && tracerr.Unwrap(err) != http.ErrServerClosed {
248 panic(err)
249 }
250 }()
251
252 // Reset DB
253 test.WipeDB(api.h.DB())
254
255 // Genratre blockchain data with til
256 tcc := til.NewContext(common.RollupConstMaxL1UserTx)
257 tilCfgExtra := til.ConfigExtra{
258 BootCoordAddr: ethCommon.HexToAddress("0xE39fEc6224708f0772D2A74fd3f9055A90E0A9f2"),
259 CoordUser: "Coord",
260 }
261 blocksData, err := tcc.GenerateBlocks(SetBlockchain)
262 if err != nil {
263 panic(err)
264 }
265 err = tcc.FillBlocksExtra(blocksData, &tilCfgExtra)
266 if err != nil {
267 panic(err)
268 }
269 AddAditionalInformation(blocksData)
270 // Generate L2 Txs with til
271 commonPoolTxs, err := tcc.GeneratePoolL2Txs(til.SetPoolL2MinimumFlow0)
272 if err != nil {
273 panic(err)
274 }
275
276 // Extract til generated data, and add it to HistoryDB
277 var commonBlocks []common.Block
278 var commonBatches []common.Batch
279 var commonAccounts []common.Account
280 var commonExitTree []common.ExitInfo
281 var commonL1Txs []common.L1Tx
282 var commonL2Txs []common.L2Tx
283 // Add ETH token at the beginning of the array
284 testTokens := []historydb.TokenWithUSD{}
285 ethUSD := float64(500)
286 ethNow := time.Now()
287 testTokens = append(testTokens, historydb.TokenWithUSD{
288 TokenID: test.EthToken.TokenID,
289 EthBlockNum: test.EthToken.EthBlockNum,
290 EthAddr: test.EthToken.EthAddr,
291 Name: test.EthToken.Name,
292 Symbol: test.EthToken.Symbol,
293 Decimals: test.EthToken.Decimals,
294 USD: ðUSD,
295 USDUpdate: ðNow,
296 })
297 err = api.h.UpdateTokenValue(test.EthToken.Symbol, ethUSD)
298 if err != nil {
299 panic(err)
300 }
301 for _, block := range blocksData {
302 // Insert block into HistoryDB
303 // nolint reason: block is used as read only in the function
304 if err := api.h.AddBlockSCData(&block); err != nil { //nolint:gosec
305 panic(err)
306 }
307 // Extract data
308 commonBlocks = append(commonBlocks, block.Block)
309 for i, tkn := range block.Rollup.AddedTokens {
310 token := historydb.TokenWithUSD{
311 TokenID: tkn.TokenID,
312 EthBlockNum: tkn.EthBlockNum,
313 EthAddr: tkn.EthAddr,
314 Name: tkn.Name,
315 Symbol: tkn.Symbol,
316 Decimals: tkn.Decimals,
317 }
318 value := float64(i + 423)
319 now := time.Now().UTC()
320 token.USD = &value
321 token.USDUpdate = &now
322 // Set value in DB
323 err = api.h.UpdateTokenValue(token.Symbol, value)
324 if err != nil {
325 panic(err)
326 }
327 testTokens = append(testTokens, token)
328 }
329 // Set USD value for tokens in DB
330 commonL1Txs = append(commonL1Txs, block.Rollup.L1UserTxs...)
331 for _, batch := range block.Rollup.Batches {
332 commonL2Txs = append(commonL2Txs, batch.L2Txs...)
333 commonAccounts = append(commonAccounts, batch.CreatedAccounts...)
334 commonBatches = append(commonBatches, batch.Batch)
335 commonExitTree = append(commonExitTree, batch.ExitTree...)
336 commonL1Txs = append(commonL1Txs, batch.L1CoordinatorTxs...)
337 }
338 }
339
340 // lastBlockNum2 := blocksData[len(blocksData)-1].Block.EthBlockNum
341
342 // Add accounts to StateDB
343 for i := 0; i < len(commonAccounts); i++ {
344 if _, err := api.s.CreateAccount(commonAccounts[i].Idx, &commonAccounts[i]); err != nil {
345 panic(err)
346 }
347 }
348
349 // Generate Coordinators and add them to HistoryDB
350 const nCoords = 10
351 commonCoords := test.GenCoordinators(nCoords, commonBlocks)
352 if err := api.h.AddCoordinators(commonCoords); err != nil {
353 panic(err)
354 }
355
356 // Generate Bids and add them to HistoryDB
357 const nBids = 20
358 commonBids := test.GenBids(nBids, commonBlocks, commonCoords)
359 if err = api.h.AddBids(commonBids); err != nil {
360 panic(err)
361 }
362
363 // Generate SC vars and add them to HistoryDB (if needed)
364 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)}
365 auctionVars := common.AuctionVariables{
366 EthBlockNum: int64(2),
367 DonationAddress: ethCommon.HexToAddress("0x1111111111111111111111111111111111111111"),
368 DefaultSlotSetBid: defaultSlotSetBid,
369 Outbidding: uint16(1),
370 SlotDeadline: uint8(20),
371 BootCoordinator: ethCommon.HexToAddress("0x1111111111111111111111111111111111111111"),
372 BootCoordinatorURL: "https://boot.coordinator.io",
373 ClosedAuctionSlots: uint16(2),
374 OpenAuctionSlots: uint16(5),
375 }
376
377 var buckets [common.RollupConstNumBuckets]common.BucketParams
378 for i := range buckets {
379 buckets[i].CeilUSD = big.NewInt(int64(i) * 10)
380 buckets[i].Withdrawals = big.NewInt(int64(i) * 100)
381 buckets[i].BlockWithdrawalRate = big.NewInt(int64(i) * 1000)
382 buckets[i].MaxWithdrawals = big.NewInt(int64(i) * 10000)
383 }
384
385 rollupVars := common.RollupVariables{
386 EthBlockNum: int64(3),
387 FeeAddToken: big.NewInt(100),
388 ForgeL1L2BatchTimeout: int64(44),
389 WithdrawalDelay: uint64(3000),
390 Buckets: buckets,
391 SafeMode: false,
392 }
393
394 wdelayerVars := common.WDelayerVariables{
395 WithdrawalDelay: uint64(3000),
396 }
397
398 err = api.h.AddAuctionVars(&auctionVars)
399 if err != nil {
400 panic(err)
401 }
402
403 // Generate test data, as expected to be received/sended from/to the API
404 testCoords := genTestCoordinators(commonCoords)
405 testBids := genTestBids(commonBlocks, testCoords, commonBids)
406 testExits := genTestExits(commonExitTree, testTokens, commonAccounts)
407 testTxs := genTestTxs(commonL1Txs, commonL2Txs, commonAccounts, testTokens, commonBlocks)
408 testBatches, testFullBatches := genTestBatches(commonBlocks, commonBatches, testTxs)
409 poolTxsToSend, poolTxsToReceive := genTestPoolTxs(commonPoolTxs, testTokens, commonAccounts)
410 tc = testCommon{
411 blocks: commonBlocks,
412 tokens: testTokens,
413 batches: testBatches,
414 fullBatches: testFullBatches,
415 coordinators: testCoords,
416 accounts: genTestAccounts(commonAccounts, testTokens),
417 txs: testTxs,
418 exits: testExits,
419 poolTxsToSend: poolTxsToSend,
420 poolTxsToReceive: poolTxsToReceive,
421 auths: genTestAuths(test.GenAuths(5)),
422 router: router,
423 bids: testBids,
424 slots: api.genTestSlots(
425 20,
426 commonBlocks[len(commonBlocks)-1].Num,
427 testBids,
428 auctionVars,
429 ),
430 auctionVars: auctionVars,
431 rollupVars: rollupVars,
432 wdelayerVars: wdelayerVars,
433 }
434
435 // Fake server
436 if os.Getenv("FAKE_SERVER") == "yes" {
437 for {
438 log.Info("Running fake server at " + apiURL + " until ^C is received")
439 time.Sleep(30 * time.Second)
440 }
441 }
442 // Run tests
443 result := m.Run()
444 // Stop server
445 if err := server.Shutdown(context.Background()); err != nil {
446 panic(err)
447 }
448 if err := database.Close(); err != nil {
449 panic(err)
450 }
451 if err := os.RemoveAll(dir); err != nil {
452 panic(err)
453 }
454 os.Exit(result)
455}
456
457func doGoodReqPaginated(
458 path, order string,
459 iterStruct Pendinger,
460 appendIter func(res interface{}),
461) error {
462 var next uint64
463 firstIte := true
464 expectedTotal := 0
465 totalReceived := 0
466 for {
467 // Calculate fromItem
468 iterPath := path
469 if !firstIte {
470 iterPath += "&fromItem=" + strconv.Itoa(int(next))
471 }
472 // Call API to get this iteration items
473 iterStruct = iterStruct.New()
474 if err := doGoodReq(
475 "GET", iterPath+"&order="+order, nil,
476 iterStruct,
477 ); err != nil {
478 return tracerr.Wrap(err)
479 }
480 appendIter(iterStruct)
481 // Keep iterating?
482 remaining, lastID := iterStruct.GetPending()
483 if remaining == 0 {
484 break
485 }
486 if order == historydb.OrderDesc {
487 next = lastID - 1
488 } else {
489 next = lastID + 1
490 }
491 // Check that the expected amount of items is consistent across iterations
492 totalReceived += iterStruct.Len()
493 if firstIte {
494 firstIte = false
495 expectedTotal = totalReceived + int(remaining)
496 }
497 if expectedTotal != totalReceived+int(remaining) {
498 panic(fmt.Sprintf(
499 "pagination error, totalReceived + remaining should be %d, but is %d",
500 expectedTotal, totalReceived+int(remaining),
501 ))
502 }
503 }
504 return nil
505}
506
507func doGoodReq(method, path string, reqBody io.Reader, returnStruct interface{}) error {
508 ctx := context.Background()
509 client := &http.Client{}
510 httpReq, err := http.NewRequest(method, path, reqBody)
511 if err != nil {
512 return tracerr.Wrap(err)
513 }
514 if reqBody != nil {
515 httpReq.Header.Add("Content-Type", "application/json")
516 }
517 route, pathParams, err := tc.router.FindRoute(httpReq.Method, httpReq.URL)
518 if err != nil {
519 return tracerr.Wrap(err)
520 }
521 // Validate request against swagger spec
522 requestValidationInput := &swagger.RequestValidationInput{
523 Request: httpReq,
524 PathParams: pathParams,
525 Route: route,
526 }
527 if err := swagger.ValidateRequest(ctx, requestValidationInput); err != nil {
528 return tracerr.Wrap(err)
529 }
530 // Do API call
531 resp, err := client.Do(httpReq)
532 if err != nil {
533 return tracerr.Wrap(err)
534 }
535 if resp.Body == nil && returnStruct != nil {
536 return tracerr.Wrap(errors.New("Nil body"))
537 }
538 //nolint
539 defer resp.Body.Close()
540 body, err := ioutil.ReadAll(resp.Body)
541 if err != nil {
542 return tracerr.Wrap(err)
543 }
544 if resp.StatusCode != 200 {
545 return tracerr.Wrap(fmt.Errorf("%d response. Body: %s", resp.StatusCode, string(body)))
546 }
547 if returnStruct == nil {
548 return nil
549 }
550 // Unmarshal body into return struct
551 if err := json.Unmarshal(body, returnStruct); err != nil {
552 log.Error("invalid json: " + string(body))
553 log.Error(err)
554 return tracerr.Wrap(err)
555 }
556 // log.Info(string(body))
557 // Validate response against swagger spec
558 responseValidationInput := &swagger.ResponseValidationInput{
559 RequestValidationInput: requestValidationInput,
560 Status: resp.StatusCode,
561 Header: resp.Header,
562 }
563 responseValidationInput = responseValidationInput.SetBodyBytes(body)
564 return swagger.ValidateResponse(ctx, responseValidationInput)
565}
566
567func doBadReq(method, path string, reqBody io.Reader, expectedResponseCode int) error {
568 ctx := context.Background()
569 client := &http.Client{}
570 httpReq, _ := http.NewRequest(method, path, reqBody)
571 route, pathParams, err := tc.router.FindRoute(httpReq.Method, httpReq.URL)
572 if err != nil {
573 return tracerr.Wrap(err)
574 }
575 // Validate request against swagger spec
576 requestValidationInput := &swagger.RequestValidationInput{
577 Request: httpReq,
578 PathParams: pathParams,
579 Route: route,
580 }
581 if err := swagger.ValidateRequest(ctx, requestValidationInput); err != nil {
582 if expectedResponseCode != 400 {
583 return tracerr.Wrap(err)
584 }
585 log.Warn("The request does not match the API spec")
586 }
587 // Do API call
588 resp, err := client.Do(httpReq)
589 if err != nil {
590 return tracerr.Wrap(err)
591 }
592 if resp.Body == nil {
593 return tracerr.Wrap(errors.New("Nil body"))
594 }
595 //nolint
596 defer resp.Body.Close()
597 body, err := ioutil.ReadAll(resp.Body)
598 if err != nil {
599 return tracerr.Wrap(err)
600 }
601 if resp.StatusCode != expectedResponseCode {
602 return tracerr.Wrap(fmt.Errorf("Unexpected response code: %d. Body: %s", resp.StatusCode, string(body)))
603 }
604 // Validate response against swagger spec
605 responseValidationInput := &swagger.ResponseValidationInput{
606 RequestValidationInput: requestValidationInput,
607 Status: resp.StatusCode,
608 Header: resp.Header,
609 }
610 responseValidationInput = responseValidationInput.SetBodyBytes(body)
611 return swagger.ValidateResponse(ctx, responseValidationInput)
612}
613
614// test helpers
615
616func getTimestamp(blockNum int64, blocks []common.Block) time.Time {
617 for i := 0; i < len(blocks); i++ {
618 if blocks[i].Num == blockNum {
619 return blocks[i].Timestamp
620 }
621 }
622 panic("timesamp not found")
623}
624
625func getTokenByID(id common.TokenID, tokens []historydb.TokenWithUSD) historydb.TokenWithUSD {
626 for i := 0; i < len(tokens); i++ {
627 if tokens[i].TokenID == id {
628 return tokens[i]
629 }
630 }
631 panic("token not found")
632}
633
634func getTokenByIdx(idx common.Idx, tokens []historydb.TokenWithUSD, accs []common.Account) historydb.TokenWithUSD {
635 for _, acc := range accs {
636 if idx == acc.Idx {
637 return getTokenByID(acc.TokenID, tokens)
638 }
639 }
640 panic("token not found")
641}
642
643func getAccountByIdx(idx common.Idx, accs []common.Account) *common.Account {
644 for _, acc := range accs {
645 if acc.Idx == idx {
646 return &acc
647 }
648 }
649 panic("account not found")
650}
651
652func getBlockByNum(ethBlockNum int64, blocks []common.Block) common.Block {
653 for _, b := range blocks {
654 if b.Num == ethBlockNum {
655 return b
656 }
657 }
658 panic("block not found")
659}
660
661func getCoordinatorByBidder(bidder ethCommon.Address, coordinators []historydb.CoordinatorAPI) historydb.CoordinatorAPI {
662 for _, c := range coordinators {
663 if c.Bidder == bidder {
664 return c
665 }
666 }
667 panic("coordinator not found")
668}