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