From d4f6926311a865743d5b234d99f635005604adbc Mon Sep 17 00:00:00 2001 From: Eduard S Date: Wed, 3 Mar 2021 14:37:41 +0100 Subject: [PATCH] WIP --- api/state_test.go | 12 +- db/historydb/historydb_test.go | 338 +++++++++++++++++---------------- db/historydb/nodeinfo.go | 96 +++++----- db/historydb/views.go | 46 +++++ db/migrations/0001.sql | 1 + node/node.go | 12 +- 6 files changed, 281 insertions(+), 224 deletions(-) diff --git a/api/state_test.go b/api/state_test.go index aa0b297..812e29f 100644 --- a/api/state_test.go +++ b/api/state_test.go @@ -29,7 +29,7 @@ type testNetwork struct { } func TestSetRollupVariables(t *testing.T) { - api.h.SetRollupVariables(tc.rollupVars) + api.h.SetRollupVariables(&tc.rollupVars) ni, err := api.h.GetNodeInfoAPI() assert.NoError(t, err) assertEqualRollupVariables(t, tc.rollupVars, ni.StateAPI.Rollup, true) @@ -51,14 +51,14 @@ func assertEqualRollupVariables(t *testing.T, rollupVariables common.RollupVaria } func TestSetWDelayerVariables(t *testing.T) { - api.h.SetWDelayerVariables(tc.wdelayerVars) + 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) { - api.h.SetAuctionVariables(tc.auctionVars) + api.h.SetAuctionVariables(&tc.auctionVars) ni, err := api.h.GetNodeInfoAPI() assert.NoError(t, err) assertEqualAuctionVariables(t, tc.auctionVars, ni.StateAPI.Auction) @@ -165,9 +165,9 @@ func TestGetState(t *testing.T) { lastBlock := tc.blocks[3] lastBatchNum := common.BatchNum(12) currentSlotNum := int64(1) - api.h.SetRollupVariables(tc.rollupVars) - api.h.SetWDelayerVariables(tc.wdelayerVars) - api.h.SetAuctionVariables(tc.auctionVars) + 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.h.UpdateMetrics() diff --git a/db/historydb/historydb_test.go b/db/historydb/historydb_test.go index 3aa7282..b9627e7 100644 --- a/db/historydb/historydb_test.go +++ b/db/historydb/historydb_test.go @@ -1114,166 +1114,166 @@ func TestAddEscapeHatchWithdrawals(t *testing.T) { assert.Equal(t, escapeHatchWithdrawals, dbEscapeHatchWithdrawals) } -func TestGetMetricsAPI(t *testing.T) { - test.WipeDB(historyDB.DB()) - set := ` - Type: Blockchain - - AddToken(1) - - CreateAccountDeposit(1) A: 1000 // numTx=1 - CreateAccountDeposit(1) B: 2000 // numTx=2 - CreateAccountDeposit(1) C: 3000 //numTx=3 - - // block 0 is stored as default in the DB - // block 1 does not exist - > batchL1 // numBatches=1 - > batchL1 // numBatches=2 - > block // blockNum=2 - - Transfer(1) C-A : 10 (1) // numTx=4 - > batch // numBatches=3 - > block // blockNum=3 - Transfer(1) B-C : 10 (1) // numTx=5 - > batch // numBatches=5 - > block // blockNum=4 - Transfer(1) A-B : 10 (1) // numTx=6 - > batch // numBatches=5 - > block // blockNum=5 - Transfer(1) A-B : 10 (1) // numTx=7 - > batch // numBatches=6 - > block // blockNum=6 - ` - - const numBatches int = 6 - const numTx int = 7 - const blockNum = 6 - 1 - - tc := til.NewContext(uint16(0), common.RollupConstMaxL1UserTx) - tilCfgExtra := til.ConfigExtra{ - BootCoordAddr: ethCommon.HexToAddress("0xE39fEc6224708f0772D2A74fd3f9055A90E0A9f2"), - CoordUser: "A", - } - blocks, err := tc.GenerateBlocks(set) - require.NoError(t, err) - err = tc.FillBlocksExtra(blocks, &tilCfgExtra) - require.NoError(t, err) - - // Sanity check - require.Equal(t, blockNum, len(blocks)) - - // Adding one batch per block - // batch frequency can be chosen - const frequency int = 15 - - for i := range blocks { - blocks[i].Block.Timestamp = time.Now().Add(-time.Second * time.Duration(frequency*(len(blocks)-i))) - err = historyDB.AddBlockSCData(&blocks[i]) - assert.NoError(t, err) - } - - res, err := historyDBWithACC.GetMetricsAPI(common.BatchNum(numBatches)) - assert.NoError(t, err) - - assert.Equal(t, float64(numTx)/float64(numBatches), res.TransactionsPerBatch) - - // Frequency is not exactly the desired one, some decimals may appear - // There is a -2 as time for first and last batch is not taken into account - assert.InEpsilon(t, float64(frequency)*float64(numBatches-2)/float64(numBatches), res.BatchFrequency, 0.01) - assert.InEpsilon(t, float64(numTx)/float64(frequency*blockNum-frequency), res.TransactionsPerSecond, 0.01) - assert.Equal(t, int64(3), res.TotalAccounts) - assert.Equal(t, int64(3), res.TotalBJJs) - // Til does not set fees - assert.Equal(t, float64(0), res.AvgTransactionFee) -} - -func TestGetMetricsAPIMoreThan24Hours(t *testing.T) { - test.WipeDB(historyDB.DB()) - - testUsersLen := 3 - var set []til.Instruction - for user := 0; user < testUsersLen; user++ { - set = append(set, til.Instruction{ - Typ: common.TxTypeCreateAccountDeposit, - TokenID: common.TokenID(0), - DepositAmount: big.NewInt(1000000), - Amount: big.NewInt(0), - From: fmt.Sprintf("User%02d", user), - }) - set = append(set, til.Instruction{Typ: til.TypeNewBlock}) - } - set = append(set, til.Instruction{Typ: til.TypeNewBatchL1}) - set = append(set, til.Instruction{Typ: til.TypeNewBatchL1}) - set = append(set, til.Instruction{Typ: til.TypeNewBlock}) - - // Transfers - const numBlocks int = 30 - for x := 0; x < numBlocks; x++ { - set = append(set, til.Instruction{ - Typ: common.TxTypeTransfer, - TokenID: common.TokenID(0), - DepositAmount: big.NewInt(1), - Amount: big.NewInt(0), - From: "User00", - To: "User01", - }) - set = append(set, til.Instruction{Typ: til.TypeNewBatch}) - set = append(set, til.Instruction{Typ: til.TypeNewBlock}) - } - - var chainID uint16 = 0 - tc := til.NewContext(chainID, common.RollupConstMaxL1UserTx) - blocks, err := tc.GenerateBlocksFromInstructions(set) - assert.NoError(t, err) - - tilCfgExtra := til.ConfigExtra{ - CoordUser: "A", - } - err = tc.FillBlocksExtra(blocks, &tilCfgExtra) - require.NoError(t, err) - - const numBatches int = 2 + numBlocks - const blockNum = 4 + numBlocks - - // Sanity check - require.Equal(t, blockNum, len(blocks)) - - // Adding one batch per block - // batch frequency can be chosen - const blockTime time.Duration = 3600 * time.Second - now := time.Now() - require.NoError(t, err) - - for i := range blocks { - blocks[i].Block.Timestamp = now.Add(-time.Duration(len(blocks)-1-i) * blockTime) - err = historyDB.AddBlockSCData(&blocks[i]) - assert.NoError(t, err) - } - - res, err := historyDBWithACC.GetMetricsAPI(common.BatchNum(numBatches)) - assert.NoError(t, err) - - assert.InEpsilon(t, 1.0, res.TransactionsPerBatch, 0.1) - - assert.InEpsilon(t, res.BatchFrequency, float64(blockTime/time.Second), 0.1) - assert.InEpsilon(t, 1.0/float64(blockTime/time.Second), res.TransactionsPerSecond, 0.1) - assert.Equal(t, int64(3), res.TotalAccounts) - assert.Equal(t, int64(3), res.TotalBJJs) - // Til does not set fees - assert.Equal(t, float64(0), res.AvgTransactionFee) -} - -func TestGetMetricsAPIEmpty(t *testing.T) { - test.WipeDB(historyDB.DB()) - _, err := historyDBWithACC.GetMetricsAPI(0) - assert.NoError(t, err) -} - -func TestGetAvgTxFeeEmpty(t *testing.T) { - test.WipeDB(historyDB.DB()) - _, err := historyDBWithACC.GetAvgTxFeeAPI() - assert.NoError(t, err) -} +// func TestGetMetricsAPI(t *testing.T) { +// test.WipeDB(historyDB.DB()) +// set := ` +// Type: Blockchain +// +// AddToken(1) +// +// CreateAccountDeposit(1) A: 1000 // numTx=1 +// CreateAccountDeposit(1) B: 2000 // numTx=2 +// CreateAccountDeposit(1) C: 3000 //numTx=3 +// +// // block 0 is stored as default in the DB +// // block 1 does not exist +// > batchL1 // numBatches=1 +// > batchL1 // numBatches=2 +// > block // blockNum=2 +// +// Transfer(1) C-A : 10 (1) // numTx=4 +// > batch // numBatches=3 +// > block // blockNum=3 +// Transfer(1) B-C : 10 (1) // numTx=5 +// > batch // numBatches=5 +// > block // blockNum=4 +// Transfer(1) A-B : 10 (1) // numTx=6 +// > batch // numBatches=5 +// > block // blockNum=5 +// Transfer(1) A-B : 10 (1) // numTx=7 +// > batch // numBatches=6 +// > block // blockNum=6 +// ` +// +// const numBatches int = 6 +// const numTx int = 7 +// const blockNum = 6 - 1 +// +// tc := til.NewContext(uint16(0), common.RollupConstMaxL1UserTx) +// tilCfgExtra := til.ConfigExtra{ +// BootCoordAddr: ethCommon.HexToAddress("0xE39fEc6224708f0772D2A74fd3f9055A90E0A9f2"), +// CoordUser: "A", +// } +// blocks, err := tc.GenerateBlocks(set) +// require.NoError(t, err) +// err = tc.FillBlocksExtra(blocks, &tilCfgExtra) +// require.NoError(t, err) +// +// // Sanity check +// require.Equal(t, blockNum, len(blocks)) +// +// // Adding one batch per block +// // batch frequency can be chosen +// const frequency int = 15 +// +// for i := range blocks { +// blocks[i].Block.Timestamp = time.Now().Add(-time.Second * time.Duration(frequency*(len(blocks)-i))) +// err = historyDB.AddBlockSCData(&blocks[i]) +// assert.NoError(t, err) +// } +// +// res, err := historyDBWithACC.GetMetricsAPI(common.BatchNum(numBatches)) +// assert.NoError(t, err) +// +// assert.Equal(t, float64(numTx)/float64(numBatches), res.TransactionsPerBatch) +// +// // Frequency is not exactly the desired one, some decimals may appear +// // There is a -2 as time for first and last batch is not taken into account +// assert.InEpsilon(t, float64(frequency)*float64(numBatches-2)/float64(numBatches), res.BatchFrequency, 0.01) +// assert.InEpsilon(t, float64(numTx)/float64(frequency*blockNum-frequency), res.TransactionsPerSecond, 0.01) +// assert.Equal(t, int64(3), res.TotalAccounts) +// assert.Equal(t, int64(3), res.TotalBJJs) +// // Til does not set fees +// assert.Equal(t, float64(0), res.AvgTransactionFee) +// } +// +// func TestGetMetricsAPIMoreThan24Hours(t *testing.T) { +// test.WipeDB(historyDB.DB()) +// +// testUsersLen := 3 +// var set []til.Instruction +// for user := 0; user < testUsersLen; user++ { +// set = append(set, til.Instruction{ +// Typ: common.TxTypeCreateAccountDeposit, +// TokenID: common.TokenID(0), +// DepositAmount: big.NewInt(1000000), +// Amount: big.NewInt(0), +// From: fmt.Sprintf("User%02d", user), +// }) +// set = append(set, til.Instruction{Typ: til.TypeNewBlock}) +// } +// set = append(set, til.Instruction{Typ: til.TypeNewBatchL1}) +// set = append(set, til.Instruction{Typ: til.TypeNewBatchL1}) +// set = append(set, til.Instruction{Typ: til.TypeNewBlock}) +// +// // Transfers +// const numBlocks int = 30 +// for x := 0; x < numBlocks; x++ { +// set = append(set, til.Instruction{ +// Typ: common.TxTypeTransfer, +// TokenID: common.TokenID(0), +// DepositAmount: big.NewInt(1), +// Amount: big.NewInt(0), +// From: "User00", +// To: "User01", +// }) +// set = append(set, til.Instruction{Typ: til.TypeNewBatch}) +// set = append(set, til.Instruction{Typ: til.TypeNewBlock}) +// } +// +// var chainID uint16 = 0 +// tc := til.NewContext(chainID, common.RollupConstMaxL1UserTx) +// blocks, err := tc.GenerateBlocksFromInstructions(set) +// assert.NoError(t, err) +// +// tilCfgExtra := til.ConfigExtra{ +// CoordUser: "A", +// } +// err = tc.FillBlocksExtra(blocks, &tilCfgExtra) +// require.NoError(t, err) +// +// const numBatches int = 2 + numBlocks +// const blockNum = 4 + numBlocks +// +// // Sanity check +// require.Equal(t, blockNum, len(blocks)) +// +// // Adding one batch per block +// // batch frequency can be chosen +// const blockTime time.Duration = 3600 * time.Second +// now := time.Now() +// require.NoError(t, err) +// +// for i := range blocks { +// blocks[i].Block.Timestamp = now.Add(-time.Duration(len(blocks)-1-i) * blockTime) +// err = historyDB.AddBlockSCData(&blocks[i]) +// assert.NoError(t, err) +// } +// +// res, err := historyDBWithACC.GetMetricsAPI(common.BatchNum(numBatches)) +// assert.NoError(t, err) +// +// assert.InEpsilon(t, 1.0, res.TransactionsPerBatch, 0.1) +// +// assert.InEpsilon(t, res.BatchFrequency, float64(blockTime/time.Second), 0.1) +// assert.InEpsilon(t, 1.0/float64(blockTime/time.Second), res.TransactionsPerSecond, 0.1) +// assert.Equal(t, int64(3), res.TotalAccounts) +// assert.Equal(t, int64(3), res.TotalBJJs) +// // Til does not set fees +// assert.Equal(t, float64(0), res.AvgTransactionFee) +// } +// +// func TestGetMetricsAPIEmpty(t *testing.T) { +// test.WipeDB(historyDB.DB()) +// _, err := historyDBWithACC.GetMetricsAPI(0) +// assert.NoError(t, err) +// } +// +// func TestGetAvgTxFeeEmpty(t *testing.T) { +// test.WipeDB(historyDB.DB()) +// _, err := historyDBWithACC.GetAvgTxFeeAPI() +// assert.NoError(t, err) +// } func TestGetLastL1TxsNum(t *testing.T) { test.WipeDB(historyDB.DB()) @@ -1460,3 +1460,21 @@ func setTestBlocks(from, to int64) []common.Block { } return blocks } + +func TestNodeInfo(t *testing.T) { + test.WipeDB(historyDB.DB()) + + clientSetup := test.NewClientSetupExample() + constants := &Constants{ + RollupConstants: *clientSetup.RollupConstants, + AuctionConstants: *clientSetup.AuctionConstants, + WDelayerConstants: *clientSetup.WDelayerConstants, + ChainID: 42, + HermezAddress: clientSetup.AuctionConstants.HermezRollup, + } + err := historyDB.SetConstants(constants) + require.NoError(t, err) + dbConstants, err := historyDB.GetConstants() + require.NoError(t, err) + assert.Equal(t, constants, dbConstants) +} diff --git a/db/historydb/nodeinfo.go b/db/historydb/nodeinfo.go index b29eba8..5d3b461 100644 --- a/db/historydb/nodeinfo.go +++ b/db/historydb/nodeinfo.go @@ -61,11 +61,13 @@ type Constants struct { ChainID uint16 HermezAddress ethCommon.Address } + type NodeInfo struct { - MaxPoolTxs uint32 `meddler:"max_pool_txs"` - MinFeeUSD float64 `meddler:"min_fee"` - StateAPI StateAPI `meddler:"state,json"` - Constants Constants `meddler:"constants,json"` + ItemID int `meddler:"item_id"` + MaxPoolTxs *uint32 `meddler:"max_pool_txs"` + MinFeeUSD *float64 `meddler:"min_fee"` + StateAPI *StateAPI `meddler:"state,json"` + Constants *Constants `meddler:"constants,json"` } func (hdb *HistoryDB) GetNodeInfo() (*NodeInfo, error) { @@ -76,73 +78,63 @@ func (hdb *HistoryDB) GetNodeInfo() (*NodeInfo, error) { return ni, tracerr.Wrap(err) } +func (hdb *HistoryDB) SetConstants(constants *Constants) error { + _constants := struct { + Constants *Constants `meddler:"constants,json"` + }{constants} + values, err := meddler.Default.Values(&_constants, false) + if err != nil { + return tracerr.Wrap(err) + } + _, err = hdb.dbWrite.Exec( + "UPDATE node_info SET constants = $1 WHERE item_id = 1;", + values[0], + ) + return tracerr.Wrap(err) +} + +func (hdb *HistoryDB) GetConstants() (*Constants, error) { + var nodeInfo NodeInfo + err := meddler.QueryRow( + hdb.dbRead, &nodeInfo, + "SELECT constants FROM node_info WHERE item_id = 1;", + ) + return nodeInfo.Constants, tracerr.Wrap(err) +} + func (hdb *HistoryDB) SetInitialNodeInfo(maxPoolTxs uint32, minFeeUSD float64, constants *Constants) error { ni := &NodeInfo{ - MaxPoolTxs: maxPoolTxs, - MinFeeUSD: minFeeUSD, - Constants: *constants, + MaxPoolTxs: &maxPoolTxs, + MinFeeUSD: &minFeeUSD, + Constants: constants, } return tracerr.Wrap(meddler.Insert(hdb.dbWrite, "node_info", ni)) } // SetRollupVariables set Status.Rollup variables -func (hdb *HistoryDB) SetRollupVariables(rollupVariables common.RollupVariables) error { +func (hdb *HistoryDB) SetRollupVariables(rollupVariables *common.RollupVariables) error { setUpdatedNodeInfo := func(txn *sqlx.Tx, ni *NodeInfo) error { - var rollupVars RollupVariablesAPI - rollupVars.EthBlockNum = rollupVariables.EthBlockNum - rollupVars.FeeAddToken = apitypes.NewBigIntStr(rollupVariables.FeeAddToken) - rollupVars.ForgeL1L2BatchTimeout = rollupVariables.ForgeL1L2BatchTimeout - rollupVars.WithdrawalDelay = rollupVariables.WithdrawalDelay - - for i, bucket := range rollupVariables.Buckets { - var apiBucket BucketParamsAPI - apiBucket.CeilUSD = apitypes.NewBigIntStr(bucket.CeilUSD) - apiBucket.Withdrawals = apitypes.NewBigIntStr(bucket.Withdrawals) - apiBucket.BlockWithdrawalRate = apitypes.NewBigIntStr(bucket.BlockWithdrawalRate) - apiBucket.MaxWithdrawals = apitypes.NewBigIntStr(bucket.MaxWithdrawals) - rollupVars.Buckets[i] = apiBucket - } - - rollupVars.SafeMode = rollupVariables.SafeMode - ni.StateAPI.Rollup = rollupVars + rollupVars := NewRollupVariablesAPI(rollupVariables) + ni.StateAPI.Rollup = *rollupVars return nil } return hdb.updateNodeInfo(setUpdatedNodeInfo) } // SetWDelayerVariables set Status.WithdrawalDelayer variables -func (hdb *HistoryDB) SetWDelayerVariables(wDelayerVariables common.WDelayerVariables) error { +func (hdb *HistoryDB) SetWDelayerVariables(wDelayerVariables *common.WDelayerVariables) error { setUpdatedNodeInfo := func(txn *sqlx.Tx, ni *NodeInfo) error { - ni.StateAPI.WithdrawalDelayer = wDelayerVariables + ni.StateAPI.WithdrawalDelayer = *wDelayerVariables return nil } return hdb.updateNodeInfo(setUpdatedNodeInfo) } // SetAuctionVariables set Status.Auction variables -func (hdb *HistoryDB) SetAuctionVariables(auctionVariables common.AuctionVariables) error { +func (hdb *HistoryDB) SetAuctionVariables(auctionVariables *common.AuctionVariables) error { setUpdatedNodeInfo := func(txn *sqlx.Tx, ni *NodeInfo) error { - var auctionVars AuctionVariablesAPI - - auctionVars.EthBlockNum = auctionVariables.EthBlockNum - auctionVars.DonationAddress = auctionVariables.DonationAddress - auctionVars.BootCoordinator = auctionVariables.BootCoordinator - auctionVars.BootCoordinatorURL = auctionVariables.BootCoordinatorURL - auctionVars.DefaultSlotSetBidSlotNum = auctionVariables.DefaultSlotSetBidSlotNum - auctionVars.ClosedAuctionSlots = auctionVariables.ClosedAuctionSlots - auctionVars.OpenAuctionSlots = auctionVariables.OpenAuctionSlots - auctionVars.Outbidding = auctionVariables.Outbidding - auctionVars.SlotDeadline = auctionVariables.SlotDeadline - - for i, slot := range auctionVariables.DefaultSlotSetBid { - auctionVars.DefaultSlotSetBid[i] = apitypes.NewBigIntStr(slot) - } - - for i, ratio := range auctionVariables.AllocationRatio { - auctionVars.AllocationRatio[i] = ratio - } - - ni.StateAPI.Auction = auctionVars + auctionVars := NewAuctionVariablesAPI(auctionVariables) + ni.StateAPI.Auction = *auctionVars return nil } return hdb.updateNodeInfo(setUpdatedNodeInfo) @@ -473,11 +465,11 @@ func (hdb *HistoryDB) UpdateRecommendedFee() error { avgTransactionFee = 0 } ni.StateAPI.RecommendedFee.ExistingAccount = - math.Max(avgTransactionFee, ni.MinFeeUSD) + math.Max(avgTransactionFee, *ni.MinFeeUSD) ni.StateAPI.RecommendedFee.CreatesAccount = - math.Max(createAccountExtraFeePercentage*avgTransactionFee, ni.MinFeeUSD) + math.Max(createAccountExtraFeePercentage*avgTransactionFee, *ni.MinFeeUSD) ni.StateAPI.RecommendedFee.CreatesAccountAndRegister = - math.Max(createAccountInternalExtraFeePercentage*avgTransactionFee, ni.MinFeeUSD) + math.Max(createAccountInternalExtraFeePercentage*avgTransactionFee, *ni.MinFeeUSD) return nil } return hdb.updateNodeInfo(setUpdatedNodeInfo) diff --git a/db/historydb/views.go b/db/historydb/views.go index e7bebe5..a0489a8 100644 --- a/db/historydb/views.go +++ b/db/historydb/views.go @@ -363,6 +363,27 @@ type RollupVariablesAPI struct { SafeMode bool `json:"safeMode" meddler:"safe_mode"` } +// NewRollupVariablesAPI creates a RollupVariablesAPI from common.RollupVariables +func NewRollupVariablesAPI(rollupVariables *common.RollupVariables) *RollupVariablesAPI { + rollupVars := RollupVariablesAPI{ + EthBlockNum: rollupVariables.EthBlockNum, + FeeAddToken: apitypes.NewBigIntStr(rollupVariables.FeeAddToken), + ForgeL1L2BatchTimeout: rollupVariables.ForgeL1L2BatchTimeout, + WithdrawalDelay: rollupVariables.WithdrawalDelay, + SafeMode: rollupVariables.SafeMode, + } + + for i, bucket := range rollupVariables.Buckets { + rollupVars.Buckets[i] = BucketParamsAPI{ + CeilUSD: apitypes.NewBigIntStr(bucket.CeilUSD), + Withdrawals: apitypes.NewBigIntStr(bucket.Withdrawals), + BlockWithdrawalRate: apitypes.NewBigIntStr(bucket.BlockWithdrawalRate), + MaxWithdrawals: apitypes.NewBigIntStr(bucket.MaxWithdrawals), + } + } + return &rollupVars +} + // AuctionVariablesAPI are the variables of the Auction Smart Contract type AuctionVariablesAPI struct { EthBlockNum int64 `json:"ethereumBlockNum" meddler:"eth_block_num"` @@ -387,3 +408,28 @@ type AuctionVariablesAPI struct { // SlotDeadline Number of blocks at the end of a slot in which any coordinator can forge if the winner has not forged one before SlotDeadline uint8 `json:"slotDeadline" meddler:"slot_deadline" validate:"required"` } + +// NewAuctionVariablesAPI creates a AuctionVariablesAPI from common.AuctionVariables +func NewAuctionVariablesAPI(auctionVariables *common.AuctionVariables) *AuctionVariablesAPI { + auctionVars := AuctionVariablesAPI{ + EthBlockNum: auctionVariables.EthBlockNum, + DonationAddress: auctionVariables.DonationAddress, + BootCoordinator: auctionVariables.BootCoordinator, + BootCoordinatorURL: auctionVariables.BootCoordinatorURL, + DefaultSlotSetBidSlotNum: auctionVariables.DefaultSlotSetBidSlotNum, + ClosedAuctionSlots: auctionVariables.ClosedAuctionSlots, + OpenAuctionSlots: auctionVariables.OpenAuctionSlots, + Outbidding: auctionVariables.Outbidding, + SlotDeadline: auctionVariables.SlotDeadline, + } + + for i, slot := range auctionVariables.DefaultSlotSetBid { + auctionVars.DefaultSlotSetBid[i] = apitypes.NewBigIntStr(slot) + } + + for i, ratio := range auctionVariables.AllocationRatio { + auctionVars.AllocationRatio[i] = ratio + } + + return &auctionVars +} diff --git a/db/migrations/0001.sql b/db/migrations/0001.sql index 729d105..0024ed7 100644 --- a/db/migrations/0001.sql +++ b/db/migrations/0001.sql @@ -668,6 +668,7 @@ CREATE TABLE node_info ( min_fee NUMERIC, -- L2DB config constants BYTEA -- info of the network that is constant ); +INSERT INTO node_info(item_id) VALUES (1); -- Always have a single row that we will update -- +migrate Down -- triggers diff --git a/node/node.go b/node/node.go index de5118d..a14f02a 100644 --- a/node/node.go +++ b/node/node.go @@ -627,13 +627,13 @@ func (n *Node) handleNewBlock(ctx context.Context, stats *synchronizer.Stats, va } if n.nodeAPI != nil { if vars.Rollup != nil { - n.historyDB.SetRollupVariables(*vars.Rollup) + n.historyDB.SetRollupVariables(vars.Rollup) } if vars.Auction != nil { - n.historyDB.SetAuctionVariables(*vars.Auction) + n.historyDB.SetAuctionVariables(vars.Auction) } if vars.WDelayer != nil { - n.historyDB.SetWDelayerVariables(*vars.WDelayer) + n.historyDB.SetWDelayerVariables(vars.WDelayer) } if stats.Synced() { @@ -660,9 +660,9 @@ func (n *Node) handleReorg(ctx context.Context, stats *synchronizer.Stats, vars }) } vars = n.sync.SCVars() - n.historyDB.SetRollupVariables(*vars.Rollup) - n.historyDB.SetAuctionVariables(*vars.Auction) - n.historyDB.SetWDelayerVariables(*vars.WDelayer) + n.historyDB.SetRollupVariables(vars.Rollup) + n.historyDB.SetAuctionVariables(vars.Auction) + n.historyDB.SetWDelayerVariables(vars.WDelayer) n.historyDB.UpdateNetworkInfoBlock( stats.Eth.LastBlock, stats.Sync.LastBlock, )