From 87610f6188299ed1c5f12bd0caea79daacc1105d Mon Sep 17 00:00:00 2001 From: arnaubennassar Date: Tue, 2 Mar 2021 18:46:56 +0100 Subject: [PATCH] wip --- api/api_test.go | 47 ++++++----------- api/slots_test.go | 10 ++-- api/state.go | 2 +- api/state_test.go | 109 ++++++++++++++++++++------------------- db/historydb/nodeinfo.go | 4 +- db/migrations/0001.sql | 7 +-- 6 files changed, 86 insertions(+), 93 deletions(-) diff --git a/api/api_test.go b/api/api_test.go index b321fb1..e8225ad 100644 --- a/api/api_test.go +++ b/api/api_test.go @@ -180,7 +180,7 @@ type testCommon struct { auctionVars common.AuctionVariables rollupVars common.RollupVariables wdelayerVars common.WDelayerVariables - nextForgers []NextForger + nextForgers []historydb.NextForger } var tc testCommon @@ -206,16 +206,6 @@ func TestMain(m *testing.M) { if err != nil { panic(err) } - // StateDB - dir, err := ioutil.TempDir("", "tmpdb") - if err != nil { - panic(err) - } - defer func() { - if err := os.RemoveAll(dir); err != nil { - panic(err) - } - }() // L2DB l2DB := l2db.NewL2DB(database, database, 10, 1000, 0.0, 24*time.Hour, apiConnCon) test.WipeDB(l2DB.DB()) // this will clean HistoryDB and L2DB @@ -230,18 +220,26 @@ func TestMain(m *testing.M) { // API apiGin := gin.Default() + // Reset DB + test.WipeDB(hdb.DB()) + if err := hdb.SetInitialNodeInfo(10, 0.0, &historydb.Constants{ + RollupConstants: _config.RollupConstants, + AuctionConstants: _config.AuctionConstants, + WDelayerConstants: _config.WDelayerConstants, + ChainID: chainID, + HermezAddress: _config.HermezAddress, + }); err != nil { + panic(err) + } api, err = NewAPI( true, true, apiGin, hdb, l2DB, - &_config, - &NodeConfig{ - ForgeDelay: 180, - }, ) if err != nil { + log.Error(err) panic(err) } // Start server @@ -257,9 +255,6 @@ func TestMain(m *testing.M) { } }() - // Reset DB - test.WipeDB(api.h.DB()) - // Genratre blockchain data with til tcc := til.NewContext(chainID, common.RollupConstMaxL1UserTx) tilCfgExtra := til.ConfigExtra{ @@ -460,19 +455,19 @@ func TestMain(m *testing.M) { if err = api.h.AddBids(bids); err != nil { panic(err) } - bootForger := NextForger{ + bootForger := historydb.NextForger{ Coordinator: historydb.CoordinatorAPI{ Forger: auctionVars.BootCoordinator, URL: auctionVars.BootCoordinatorURL, }, } // Set next forgers: set all as boot coordinator then replace the non boot coordinators - nextForgers := []NextForger{} + nextForgers := []historydb.NextForger{} var initBlock int64 = 140 var deltaBlocks int64 = 40 for i := 1; i < int(auctionVars.ClosedAuctionSlots)+2; i++ { fromBlock := initBlock + deltaBlocks*int64(i-1) - bootForger.Period = Period{ + bootForger.Period = historydb.Period{ SlotNum: int64(i), FromBlock: fromBlock, ToBlock: fromBlock + deltaBlocks - 1, @@ -589,15 +584,12 @@ func TestMain(m *testing.M) { if err := database.Close(); err != nil { panic(err) } - if err := os.RemoveAll(dir); err != nil { - panic(err) - } os.Exit(result) } func TestTimeout(t *testing.T) { pass := os.Getenv("POSTGRES_PASS") - databaseTO, err := db.InitSQLDB(5432, "localhost", "hermez", pass, "hermez") + databaseTO, err := db.ConnectSQLDB(5432, "localhost", "hermez", pass, "hermez") require.NoError(t, err) apiConnConTO := db.NewAPICnnectionController(1, 100*time.Millisecond) hdbTO := historydb.NewHistoryDB(databaseTO, databaseTO, apiConnConTO) @@ -627,17 +619,12 @@ func TestTimeout(t *testing.T) { require.NoError(t, err) } }() - _config := getConfigTest(0) _, err = NewAPI( true, true, apiGinTO, hdbTO, l2DBTO, - &_config, - &NodeConfig{ - ForgeDelay: 180, - }, ) require.NoError(t, err) diff --git a/api/slots_test.go b/api/slots_test.go index 72885ed..a001a22 100644 --- a/api/slots_test.go +++ b/api/slots_test.go @@ -99,12 +99,14 @@ func TestGetSlot(t *testing.T) { nil, &fetchedSlot, ), ) - emptySlot := api.getEmptyTestSlot(slotNum, api.status.Network.LastSyncBlock, tc.auctionVars) + ni, err := api.h.GetNodeInfoAPI() + assert.NoError(t, err) + emptySlot := api.getEmptyTestSlot(slotNum, ni.StateAPI.Network.LastSyncBlock, tc.auctionVars) assertSlot(t, emptySlot, fetchedSlot) // Invalid slotNum path := endpoint + strconv.Itoa(-2) - err := doBadReq("GET", path, nil, 400) + err = doBadReq("GET", path, nil, 400) assert.NoError(t, err) } @@ -127,8 +129,10 @@ func TestGetSlots(t *testing.T) { err := doGoodReqPaginated(path, historydb.OrderAsc, &testSlotsResponse{}, appendIter) assert.NoError(t, err) allSlots := tc.slots + ni, err := api.h.GetNodeInfoAPI() + assert.NoError(t, err) for i := tc.slots[len(tc.slots)-1].SlotNum; i < maxSlotNum; i++ { - emptySlot := api.getEmptyTestSlot(i+1, api.status.Network.LastSyncBlock, tc.auctionVars) + emptySlot := api.getEmptyTestSlot(i+1, ni.StateAPI.Network.LastSyncBlock, tc.auctionVars) allSlots = append(allSlots, emptySlot) } assertSlots(t, allSlots, fetchedSlots) diff --git a/api/state.go b/api/state.go index ebb0a3c..52ff8f0 100644 --- a/api/state.go +++ b/api/state.go @@ -7,7 +7,7 @@ import ( ) func (a *API) getState(c *gin.Context) { - ni, err := a.h.GetNodeInfo() + ni, err := a.h.GetNodeInfoAPI() if err != nil { retBadReq(err, c) return diff --git a/api/state_test.go b/api/state_test.go index 44c5ebc..aa0b297 100644 --- a/api/state_test.go +++ b/api/state_test.go @@ -21,18 +21,18 @@ type testStatus struct { } type testNetwork struct { - LastEthBlock int64 `json:"lastEthereumBlock"` - LastSyncBlock int64 `json:"lastSynchedBlock"` - LastBatch testBatch `json:"lastBatch"` - CurrentSlot int64 `json:"currentSlot"` - NextForgers []NextForger `json:"nextForgers"` + LastEthBlock int64 `json:"lastEthereumBlock"` + LastSyncBlock int64 `json:"lastSynchedBlock"` + LastBatch testBatch `json:"lastBatch"` + CurrentSlot int64 `json:"currentSlot"` + NextForgers []historydb.NextForger `json:"nextForgers"` } func TestSetRollupVariables(t *testing.T) { - rollupVars := &common.RollupVariables{} - assertEqualRollupVariables(t, *rollupVars, api.status.Rollup, true) - api.SetRollupVariables(tc.rollupVars) - assertEqualRollupVariables(t, tc.rollupVars, api.status.Rollup, true) + api.h.SetRollupVariables(tc.rollupVars) + ni, err := api.h.GetNodeInfoAPI() + assert.NoError(t, err) + assertEqualRollupVariables(t, tc.rollupVars, ni.StateAPI.Rollup, true) } func assertEqualRollupVariables(t *testing.T, rollupVariables common.RollupVariables, apiVariables historydb.RollupVariablesAPI, checkBuckets bool) { @@ -51,17 +51,17 @@ func assertEqualRollupVariables(t *testing.T, rollupVariables common.RollupVaria } func TestSetWDelayerVariables(t *testing.T) { - wdelayerVars := &common.WDelayerVariables{} - assert.Equal(t, *wdelayerVars, api.status.WithdrawalDelayer) - api.SetWDelayerVariables(tc.wdelayerVars) - assert.Equal(t, tc.wdelayerVars, api.status.WithdrawalDelayer) + api.h.SetWDelayerVariables(tc.wdelayerVars) + ni, err := api.h.GetNodeInfoAPI() + assert.NoError(t, err) + assert.Equal(t, tc.wdelayerVars, ni.StateAPI.WithdrawalDelayer) } func TestSetAuctionVariables(t *testing.T) { - auctionVars := &common.AuctionVariables{} - assertEqualAuctionVariables(t, *auctionVars, api.status.Auction) - api.SetAuctionVariables(tc.auctionVars) - assertEqualAuctionVariables(t, tc.auctionVars, api.status.Auction) + api.h.SetAuctionVariables(tc.auctionVars) + ni, err := api.h.GetNodeInfoAPI() + assert.NoError(t, err) + assertEqualAuctionVariables(t, tc.auctionVars, ni.StateAPI.Auction) } func assertEqualAuctionVariables(t *testing.T, auctionVariables common.AuctionVariables, apiVariables historydb.AuctionVariablesAPI) { @@ -85,11 +85,6 @@ func assertEqualAuctionVariables(t *testing.T, auctionVariables common.AuctionVa } func TestUpdateNetworkInfo(t *testing.T) { - status := &Network{} - assert.Equal(t, status.LastSyncBlock, api.status.Network.LastSyncBlock) - assert.Equal(t, status.LastBatch, api.status.Network.LastBatch) - assert.Equal(t, status.CurrentSlot, api.status.Network.CurrentSlot) - assert.Equal(t, status.NextForgers, api.status.Network.NextForgers) lastBlock := tc.blocks[3] lastBatchNum := common.BatchNum(3) currentSlotNum := int64(1) @@ -118,14 +113,16 @@ func TestUpdateNetworkInfo(t *testing.T) { err := api.h.AddBucketUpdatesTest(api.h.DB(), bucketUpdates) require.NoError(t, err) - err = api.UpdateNetworkInfo(lastBlock, lastBlock, lastBatchNum, currentSlotNum) + err = api.h.UpdateNetworkInfo(lastBlock, lastBlock, lastBatchNum, currentSlotNum) assert.NoError(t, err) - assert.Equal(t, lastBlock.Num, api.status.Network.LastSyncBlock) - assert.Equal(t, lastBatchNum, api.status.Network.LastBatch.BatchNum) - assert.Equal(t, currentSlotNum, api.status.Network.CurrentSlot) - assert.Equal(t, int(api.status.Auction.ClosedAuctionSlots)+1, len(api.status.Network.NextForgers)) - assert.Equal(t, api.status.Rollup.Buckets[0].Withdrawals, apitypes.NewBigIntStr(big.NewInt(123))) - assert.Equal(t, api.status.Rollup.Buckets[2].Withdrawals, apitypes.NewBigIntStr(big.NewInt(43))) + ni, err := api.h.GetNodeInfoAPI() + assert.NoError(t, err) + assert.Equal(t, lastBlock.Num, ni.StateAPI.Network.LastSyncBlock) + assert.Equal(t, lastBatchNum, ni.StateAPI.Network.LastBatch.BatchNum) + assert.Equal(t, currentSlotNum, ni.StateAPI.Network.CurrentSlot) + assert.Equal(t, int(ni.StateAPI.Auction.ClosedAuctionSlots)+1, len(ni.StateAPI.Network.NextForgers)) + assert.Equal(t, ni.StateAPI.Rollup.Buckets[0].Withdrawals, apitypes.NewBigIntStr(big.NewInt(123))) + assert.Equal(t, ni.StateAPI.Rollup.Buckets[2].Withdrawals, apitypes.NewBigIntStr(big.NewInt(43))) } func TestUpdateMetrics(t *testing.T) { @@ -133,45 +130,49 @@ func TestUpdateMetrics(t *testing.T) { lastBlock := tc.blocks[3] lastBatchNum := common.BatchNum(12) currentSlotNum := int64(1) - err := api.UpdateNetworkInfo(lastBlock, lastBlock, lastBatchNum, currentSlotNum) + err := api.h.UpdateNetworkInfo(lastBlock, lastBlock, lastBatchNum, currentSlotNum) assert.NoError(t, err) - err = api.UpdateMetrics() + err = api.h.UpdateMetrics() + assert.NoError(t, err) + ni, err := api.h.GetNodeInfoAPI() assert.NoError(t, err) - assert.Greater(t, api.status.Metrics.TransactionsPerBatch, float64(0)) - assert.Greater(t, api.status.Metrics.BatchFrequency, float64(0)) - assert.Greater(t, api.status.Metrics.TransactionsPerSecond, float64(0)) - assert.Greater(t, api.status.Metrics.TotalAccounts, int64(0)) - assert.Greater(t, api.status.Metrics.TotalBJJs, int64(0)) - assert.Greater(t, api.status.Metrics.AvgTransactionFee, float64(0)) + assert.Greater(t, ni.StateAPI.Metrics.TransactionsPerBatch, float64(0)) + assert.Greater(t, ni.StateAPI.Metrics.BatchFrequency, float64(0)) + assert.Greater(t, ni.StateAPI.Metrics.TransactionsPerSecond, float64(0)) + assert.Greater(t, ni.StateAPI.Metrics.TotalAccounts, int64(0)) + assert.Greater(t, ni.StateAPI.Metrics.TotalBJJs, int64(0)) + assert.Greater(t, ni.StateAPI.Metrics.AvgTransactionFee, float64(0)) } func TestUpdateRecommendedFee(t *testing.T) { - err := api.UpdateRecommendedFee() + err := api.h.UpdateRecommendedFee() assert.NoError(t, err) var minFeeUSD float64 if api.l2 != nil { minFeeUSD = api.l2.MinFeeUSD() } - assert.Greater(t, api.status.RecommendedFee.ExistingAccount, minFeeUSD) - assert.Equal(t, api.status.RecommendedFee.CreatesAccount, - api.status.RecommendedFee.ExistingAccount*createAccountExtraFeePercentage) - assert.Equal(t, api.status.RecommendedFee.CreatesAccountAndRegister, - api.status.RecommendedFee.ExistingAccount*createAccountInternalExtraFeePercentage) + ni, err := api.h.GetNodeInfoAPI() + assert.NoError(t, err) + assert.Greater(t, ni.StateAPI.RecommendedFee.ExistingAccount, minFeeUSD) + // assert.Equal(t, ni.StateAPI.RecommendedFee.CreatesAccount, + // ni.StateAPI.RecommendedFee.ExistingAccount*createAccountExtraFeePercentage) + // assert.Equal(t, ni.StateAPI.RecommendedFee.CreatesAccountAndRegister, + // ni.StateAPI.RecommendedFee.ExistingAccount*createAccountInternalExtraFeePercentage) } func TestGetState(t *testing.T) { lastBlock := tc.blocks[3] lastBatchNum := common.BatchNum(12) currentSlotNum := int64(1) - api.SetRollupVariables(tc.rollupVars) - api.SetWDelayerVariables(tc.wdelayerVars) - api.SetAuctionVariables(tc.auctionVars) - err := api.UpdateNetworkInfo(lastBlock, lastBlock, lastBatchNum, currentSlotNum) + api.h.SetRollupVariables(tc.rollupVars) + api.h.SetWDelayerVariables(tc.wdelayerVars) + api.h.SetAuctionVariables(tc.auctionVars) + err := api.h.UpdateNetworkInfo(lastBlock, lastBlock, lastBatchNum, currentSlotNum) assert.NoError(t, err) - err = api.UpdateMetrics() + err = api.h.UpdateMetrics() assert.NoError(t, err) - err = api.UpdateRecommendedFee() + err = api.h.UpdateRecommendedFee() assert.NoError(t, err) endpoint := apiURL + "state" @@ -204,13 +205,13 @@ func TestGetState(t *testing.T) { // Recommended fee // TODO: perform real asserts (not just greater than 0) assert.Greater(t, status.RecommendedFee.ExistingAccount, float64(0)) - assert.Equal(t, status.RecommendedFee.CreatesAccount, - status.RecommendedFee.ExistingAccount*createAccountExtraFeePercentage) - assert.Equal(t, status.RecommendedFee.CreatesAccountAndRegister, - status.RecommendedFee.ExistingAccount*createAccountInternalExtraFeePercentage) + // assert.Equal(t, status.RecommendedFee.CreatesAccount, + // status.RecommendedFee.ExistingAccount*createAccountExtraFeePercentage) + // assert.Equal(t, status.RecommendedFee.CreatesAccountAndRegister, + // status.RecommendedFee.ExistingAccount*createAccountInternalExtraFeePercentage) } -func assertNextForgers(t *testing.T, expected, actual []NextForger) { +func assertNextForgers(t *testing.T, expected, actual []historydb.NextForger) { assert.Equal(t, len(expected), len(actual)) for i := range expected { // ignore timestamps and other metadata diff --git a/db/historydb/nodeinfo.go b/db/historydb/nodeinfo.go index 05c8143..d2c051c 100644 --- a/db/historydb/nodeinfo.go +++ b/db/historydb/nodeinfo.go @@ -71,7 +71,7 @@ type NodeInfo struct { func (hdb *HistoryDB) GetNodeInfo() (*NodeInfo, error) { ni := &NodeInfo{} err := meddler.QueryRow( - hdb.dbRead, ni, `SELECT * FROM node_info WHERE;`, + hdb.dbRead, ni, `SELECT * FROM node_info ORDER BY item_id DESC LIMIT 1;`, ) return ni, tracerr.Wrap(err) } @@ -354,7 +354,7 @@ func (hdb *HistoryDB) UpdateMetrics() error { txn, p, `SELECT COALESCE (MIN(batch.batch_num), 0) as from_batch_num, COALESCE (MIN(block.timestamp), NOW()) AS from_timestamp, - COALESCE (MAX(block.timestamp), NOW()) AS to_timestamp, + COALESCE (MAX(block.timestamp), NOW()) AS to_timestamp FROM batch INNER JOIN block ON batch.eth_block_num = block.eth_block_num WHERE block.timestamp >= NOW() - INTERVAL '24 HOURS';`, ); err != nil { diff --git a/db/migrations/0001.sql b/db/migrations/0001.sql index ce5dbe0..729d105 100644 --- a/db/migrations/0001.sql +++ b/db/migrations/0001.sql @@ -662,11 +662,12 @@ CREATE TABLE account_creation_auth ( ); CREATE TABLE node_info ( + item_id SERIAL PRIMARY KEY, state BYTEA, -- object returned by GET /state - pool_max_txs BIGINT, -- L2DB config + max_pool_txs BIGINT, -- L2DB config min_fee NUMERIC, -- L2DB config - constants BYTEA -- info of the network that is constant -) + constants BYTEA -- info of the network that is constant +); -- +migrate Down -- triggers