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 rollupVars := common.RollupVariables{
378 EthBlockNum: int64(3),
379 FeeAddToken: big.NewInt(100),
380 ForgeL1L2BatchTimeout: int64(44),
381 WithdrawalDelay: uint64(3000),
382 }
383
384 wdelayerVars := common.WDelayerVariables{
385 WithdrawalDelay: uint64(3000),
386 }
387
388 err = api.h.AddAuctionVars(&auctionVars)
389 if err != nil {
390 panic(err)
391 }
392
393 // Generate test data, as expected to be received/sended from/to the API
394 testCoords := genTestCoordinators(commonCoords)
395 testBids := genTestBids(commonBlocks, testCoords, commonBids)
396 testExits := genTestExits(commonExitTree, testTokens, commonAccounts)
397 testTxs := genTestTxs(commonL1Txs, commonL2Txs, commonAccounts, testTokens, commonBlocks)
398 testBatches, testFullBatches := genTestBatches(commonBlocks, commonBatches, testTxs)
399 poolTxsToSend, poolTxsToReceive := genTestPoolTxs(commonPoolTxs, testTokens, commonAccounts)
400 tc = testCommon{
401 blocks: commonBlocks,
402 tokens: testTokens,
403 batches: testBatches,
404 fullBatches: testFullBatches,
405 coordinators: testCoords,
406 accounts: genTestAccounts(commonAccounts, testTokens),
407 txs: testTxs,
408 exits: testExits,
409 poolTxsToSend: poolTxsToSend,
410 poolTxsToReceive: poolTxsToReceive,
411 auths: genTestAuths(test.GenAuths(5)),
412 router: router,
413 bids: testBids,
414 slots: api.genTestSlots(
415 20,
416 commonBlocks[len(commonBlocks)-1].Num,
417 testBids,
418 auctionVars,
419 ),
420 auctionVars: auctionVars,
421 rollupVars: rollupVars,
422 wdelayerVars: wdelayerVars,
423 }
424
425 // Fake server
426 if os.Getenv("FAKE_SERVER") == "yes" {
427 for {
428 log.Info("Running fake server at " + apiURL + " until ^C is received")
429 time.Sleep(30 * time.Second)
430 }
431 }
432 // Run tests
433 result := m.Run()
434 // Stop server
435 if err := server.Shutdown(context.Background()); err != nil {
436 panic(err)
437 }
438 if err := database.Close(); err != nil {
439 panic(err)
440 }
441 if err := os.RemoveAll(dir); err != nil {
442 panic(err)
443 }
444 os.Exit(result)
445}
446
447func doGoodReqPaginated(
448 path, order string,
449 iterStruct Pendinger,
450 appendIter func(res interface{}),
451) error {
452 var next uint64
453 firstIte := true
454 expectedTotal := 0
455 totalReceived := 0
456 for {
457 // Calculate fromItem
458 iterPath := path
459 if !firstIte {
460 iterPath += "&fromItem=" + strconv.Itoa(int(next))
461 }
462 // Call API to get this iteration items
463 iterStruct = iterStruct.New()
464 if err := doGoodReq(
465 "GET", iterPath+"&order="+order, nil,
466 iterStruct,
467 ); err != nil {
468 return tracerr.Wrap(err)
469 }
470 appendIter(iterStruct)
471 // Keep iterating?
472 remaining, lastID := iterStruct.GetPending()
473 if remaining == 0 {
474 break
475 }
476 if order == historydb.OrderDesc {
477 next = lastID - 1
478 } else {
479 next = lastID + 1
480 }
481 // Check that the expected amount of items is consistent across iterations
482 totalReceived += iterStruct.Len()
483 if firstIte {
484 firstIte = false
485 expectedTotal = totalReceived + int(remaining)
486 }
487 if expectedTotal != totalReceived+int(remaining) {
488 panic(fmt.Sprintf(
489 "pagination error, totalReceived + remaining should be %d, but is %d",
490 expectedTotal, totalReceived+int(remaining),
491 ))
492 }
493 }
494 return nil
495}
496
497func doGoodReq(method, path string, reqBody io.Reader, returnStruct interface{}) error {
498 ctx := context.Background()
499 client := &http.Client{}
500 httpReq, err := http.NewRequest(method, path, reqBody)
501 if err != nil {
502 return tracerr.Wrap(err)
503 }
504 if reqBody != nil {
505 httpReq.Header.Add("Content-Type", "application/json")
506 }
507 route, pathParams, err := tc.router.FindRoute(httpReq.Method, httpReq.URL)
508 if err != nil {
509 return tracerr.Wrap(err)
510 }
511 // Validate request against swagger spec
512 requestValidationInput := &swagger.RequestValidationInput{
513 Request: httpReq,
514 PathParams: pathParams,
515 Route: route,
516 }
517 if err := swagger.ValidateRequest(ctx, requestValidationInput); err != nil {
518 return tracerr.Wrap(err)
519 }
520 // Do API call
521 resp, err := client.Do(httpReq)
522 if err != nil {
523 return tracerr.Wrap(err)
524 }
525 if resp.Body == nil && returnStruct != nil {
526 return tracerr.Wrap(errors.New("Nil body"))
527 }
528 //nolint
529 defer resp.Body.Close()
530 body, err := ioutil.ReadAll(resp.Body)
531 if err != nil {
532 return tracerr.Wrap(err)
533 }
534 if resp.StatusCode != 200 {
535 return tracerr.Wrap(fmt.Errorf("%d response. Body: %s", resp.StatusCode, string(body)))
536 }
537 if returnStruct == nil {
538 return nil
539 }
540 // Unmarshal body into return struct
541 if err := json.Unmarshal(body, returnStruct); err != nil {
542 log.Error("invalid json: " + string(body))
543 log.Error(err)
544 return tracerr.Wrap(err)
545 }
546 // log.Info(string(body))
547 // Validate response against swagger spec
548 responseValidationInput := &swagger.ResponseValidationInput{
549 RequestValidationInput: requestValidationInput,
550 Status: resp.StatusCode,
551 Header: resp.Header,
552 }
553 responseValidationInput = responseValidationInput.SetBodyBytes(body)
554 return swagger.ValidateResponse(ctx, responseValidationInput)
555}
556
557func doBadReq(method, path string, reqBody io.Reader, expectedResponseCode int) error {
558 ctx := context.Background()
559 client := &http.Client{}
560 httpReq, _ := http.NewRequest(method, path, reqBody)
561 route, pathParams, err := tc.router.FindRoute(httpReq.Method, httpReq.URL)
562 if err != nil {
563 return tracerr.Wrap(err)
564 }
565 // Validate request against swagger spec
566 requestValidationInput := &swagger.RequestValidationInput{
567 Request: httpReq,
568 PathParams: pathParams,
569 Route: route,
570 }
571 if err := swagger.ValidateRequest(ctx, requestValidationInput); err != nil {
572 if expectedResponseCode != 400 {
573 return tracerr.Wrap(err)
574 }
575 log.Warn("The request does not match the API spec")
576 }
577 // Do API call
578 resp, err := client.Do(httpReq)
579 if err != nil {
580 return tracerr.Wrap(err)
581 }
582 if resp.Body == nil {
583 return tracerr.Wrap(errors.New("Nil body"))
584 }
585 //nolint
586 defer resp.Body.Close()
587 body, err := ioutil.ReadAll(resp.Body)
588 if err != nil {
589 return tracerr.Wrap(err)
590 }
591 if resp.StatusCode != expectedResponseCode {
592 return tracerr.Wrap(fmt.Errorf("Unexpected response code: %d. Body: %s", resp.StatusCode, string(body)))
593 }
594 // Validate response against swagger spec
595 responseValidationInput := &swagger.ResponseValidationInput{
596 RequestValidationInput: requestValidationInput,
597 Status: resp.StatusCode,
598 Header: resp.Header,
599 }
600 responseValidationInput = responseValidationInput.SetBodyBytes(body)
601 return swagger.ValidateResponse(ctx, responseValidationInput)
602}
603
604// test helpers
605
606func getTimestamp(blockNum int64, blocks []common.Block) time.Time {
607 for i := 0; i < len(blocks); i++ {
608 if blocks[i].Num == blockNum {
609 return blocks[i].Timestamp
610 }
611 }
612 panic("timesamp not found")
613}
614
615func getTokenByID(id common.TokenID, tokens []historydb.TokenWithUSD) historydb.TokenWithUSD {
616 for i := 0; i < len(tokens); i++ {
617 if tokens[i].TokenID == id {
618 return tokens[i]
619 }
620 }
621 panic("token not found")
622}
623
624func getTokenByIdx(idx common.Idx, tokens []historydb.TokenWithUSD, accs []common.Account) historydb.TokenWithUSD {
625 for _, acc := range accs {
626 if idx == acc.Idx {
627 return getTokenByID(acc.TokenID, tokens)
628 }
629 }
630 panic("token not found")
631}
632
633func getAccountByIdx(idx common.Idx, accs []common.Account) *common.Account {
634 for _, acc := range accs {
635 if acc.Idx == idx {
636 return &acc
637 }
638 }
639 panic("account not found")
640}
641
642func getBlockByNum(ethBlockNum int64, blocks []common.Block) common.Block {
643 for _, b := range blocks {
644 if b.Num == ethBlockNum {
645 return b
646 }
647 }
648 panic("block not found")
649}
650
651func getCoordinatorByBidder(bidder ethCommon.Address, coordinators []historydb.CoordinatorAPI) historydb.CoordinatorAPI {
652 for _, c := range coordinators {
653 if c.Bidder == bidder {
654 return c
655 }
656 }
657 panic("coordinator not found")
658}