From f90de219acd9ec565c43675df7153b6c85d61f58 Mon Sep 17 00:00:00 2001 From: laisolizq Date: Mon, 19 Oct 2020 17:02:29 +0200 Subject: [PATCH] Add config endpoint --- api/api.go | 3 + api/api_test.go | 53 +++++++++++ api/dbtoapistructs.go | 28 ++++++ api/handlers.go | 2 +- api/swagger.yml | 214 +++++++++++++++++++++++++++++------------- eth/auction.go | 12 +-- eth/rollup.go | 18 ++-- eth/wdelayer.go | 6 +- 8 files changed, 253 insertions(+), 83 deletions(-) diff --git a/api/api.go b/api/api.go index 661c362..c52a96f 100644 --- a/api/api.go +++ b/api/api.go @@ -10,6 +10,7 @@ import ( ) var h *historydb.HistoryDB +var cg *configAPI // var s *statedb.StateDB // Not 100% sure if this is needed // var l2 *l2db.L2DB @@ -21,6 +22,7 @@ func SetAPIEndpoints( hdb *historydb.HistoryDB, sdb *statedb.StateDB, l2db *l2db.L2DB, + config *configAPI, ) error { // Check input // TODO: is stateDB only needed for explorer endpoints or for both? @@ -32,6 +34,7 @@ func SetAPIEndpoints( } h = hdb + cg = config // s = sdb // l2 = l2db diff --git a/api/api_test.go b/api/api_test.go index 5194a57..37f2726 100644 --- a/api/api_test.go +++ b/api/api_test.go @@ -24,6 +24,7 @@ import ( "github.com/hermeznetwork/hermez-node/db/historydb" "github.com/hermeznetwork/hermez-node/db/l2db" "github.com/hermeznetwork/hermez-node/db/statedb" + "github.com/hermeznetwork/hermez-node/eth" "github.com/hermeznetwork/hermez-node/log" "github.com/hermeznetwork/hermez-node/test" "github.com/iden3/go-iden3-crypto/babyjub" @@ -130,6 +131,7 @@ func (tx *wrappedL2) L2() *common.L2Tx { } var tc testCommon +var config configAPI func TestMain(m *testing.M) { // Init swagger @@ -159,6 +161,48 @@ func TestMain(m *testing.M) { l2DB := l2db.NewL2DB(database, 10, 100, 24*time.Hour) test.CleanL2DB(l2DB.DB()) + config.RollupConstants.ExchangeMultiplier = eth.RollupConstExchangeMultiplier + config.RollupConstants.ExitIdx = eth.RollupConstExitIDx + config.RollupConstants.ReservedIdx = eth.RollupConstReservedIDx + config.RollupConstants.LimitLoadAmount, _ = new(big.Int).SetString("340282366920938463463374607431768211456", 10) + config.RollupConstants.LimitL2TransferAmount, _ = new(big.Int).SetString("6277101735386680763835789423207666416102355444464034512896", 10) + config.RollupConstants.LimitTokens = eth.RollupConstLimitTokens + config.RollupConstants.L1CoordinatorTotalBytes = eth.RollupConstL1CoordinatorTotalBytes + config.RollupConstants.L1UserTotalBytes = eth.RollupConstL1UserTotalBytes + config.RollupConstants.MaxL1UserTx = eth.RollupConstMaxL1UserTx + config.RollupConstants.MaxL1Tx = eth.RollupConstMaxL1Tx + config.RollupConstants.InputSHAConstantBytes = eth.RollupConstInputSHAConstantBytes + config.RollupConstants.NumBuckets = eth.RollupConstNumBuckets + config.RollupConstants.MaxWithdrawalDelay = eth.RollupConstMaxWithdrawalDelay + var rollupPublicConstants eth.RollupPublicConstants + rollupPublicConstants.AbsoluteMaxL1L2BatchTimeout = 240 + rollupPublicConstants.HermezAuctionContract = ethCommon.HexToAddress("0x500D1d6A4c7D8Ae28240b47c8FCde034D827fD5e") + rollupPublicConstants.HermezGovernanceDAOAddress = ethCommon.HexToAddress("0xeAD9C93b79Ae7C1591b1FB5323BD777E86e150d4") + rollupPublicConstants.SafetyAddress = ethCommon.HexToAddress("0xE5904695748fe4A84b40b3fc79De2277660BD1D3") + rollupPublicConstants.TokenHEZ = ethCommon.HexToAddress("0xf784709d2317D872237C4bC22f867d1BAe2913AB") + rollupPublicConstants.WithdrawDelayerContract = ethCommon.HexToAddress("0xD6C850aeBFDC46D7F4c207e445cC0d6B0919BDBe") + var verifier eth.RollupVerifierStruct + verifier.MaxTx = 512 + verifier.NLevels = 32 + rollupPublicConstants.Verifiers = append(rollupPublicConstants.Verifiers, verifier) + + var auctionConstants eth.AuctionConstants + auctionConstants.BlocksPerSlot = 40 + auctionConstants.GenesisBlockNum = 100 + auctionConstants.GovernanceAddress = ethCommon.HexToAddress("0xeAD9C93b79Ae7C1591b1FB5323BD777E86e150d4") + auctionConstants.InitialMinimalBidding, _ = new(big.Int).SetString("10000000000000000000", 10) + auctionConstants.HermezRollup = ethCommon.HexToAddress("0xEa960515F8b4C237730F028cBAcF0a28E7F45dE0") + auctionConstants.TokenHEZ = ethCommon.HexToAddress("0xf784709d2317D872237C4bC22f867d1BAe2913AB") + + var wdelayerConstants eth.WDelayerConstants + wdelayerConstants.HermezRollup = ethCommon.HexToAddress("0xEa960515F8b4C237730F028cBAcF0a28E7F45dE0") + wdelayerConstants.MaxEmergencyModeTime = uint64(1000000) + wdelayerConstants.MaxWithdrawalDelay = uint64(10000000) + + config.RollupConstants.PublicConstants = rollupPublicConstants + config.AuctionConstants = auctionConstants + config.WDelayerConstants = wdelayerConstants + // Init API api := gin.Default() if err := SetAPIEndpoints( @@ -168,6 +212,7 @@ func TestMain(m *testing.M) { hdb, sdb, l2DB, + &config, ); err != nil { panic(err) } @@ -991,6 +1036,14 @@ func assertTokensAPIs(t *testing.T, expected, actual []tokenAPI) { } } +func TestGetConfig(t *testing.T) { + endpoint := apiURL + "config" + var configTest configAPI + assert.NoError(t, doGoodReq("GET", endpoint, nil, &configTest)) + assert.Equal(t, config, configTest) + assert.Equal(t, cg, &configTest) +} + func doGoodReqPaginated( path, order string, iterStruct db.Paginationer, diff --git a/api/dbtoapistructs.go b/api/dbtoapistructs.go index fba787e..56c7c14 100644 --- a/api/dbtoapistructs.go +++ b/api/dbtoapistructs.go @@ -2,6 +2,7 @@ package api import ( "encoding/base64" + "math/big" "strconv" "time" @@ -9,6 +10,7 @@ import ( "github.com/hermeznetwork/hermez-node/common" "github.com/hermeznetwork/hermez-node/db" "github.com/hermeznetwork/hermez-node/db/historydb" + "github.com/hermeznetwork/hermez-node/eth" "github.com/iden3/go-iden3-crypto/babyjub" "github.com/iden3/go-merkletree" ) @@ -248,3 +250,29 @@ func tokensToAPI(dbTokens []historydb.TokenRead) []tokenAPI { } return apiTokens } + +// Config + +type rollupConstants struct { + PublicConstants eth.RollupPublicConstants `json:"publicConstants"` + MaxFeeIdxCoordinator int `json:"maxFeeIdxCoordinator"` + ReservedIdx int `json:"reservedIdx"` + ExitIdx int `json:"exitIdx"` + LimitLoadAmount *big.Int `json:"limitLoadAmount"` + LimitL2TransferAmount *big.Int `json:"limitL2TransferAmount"` + LimitTokens int `json:"limitTokens"` + L1CoordinatorTotalBytes int `json:"l1CoordinatorTotalBytes"` + L1UserTotalBytes int `json:"l1UserTotalBytes"` + MaxL1UserTx int `json:"maxL1UserTx"` + MaxL1Tx int `json:"maxL1Tx"` + InputSHAConstantBytes int `json:"inputSHAConstantBytes"` + NumBuckets int `json:"numBuckets"` + MaxWithdrawalDelay int `json:"maxWithdrawalDelay"` + ExchangeMultiplier int `json:"exchangeMultiplier"` +} + +type configAPI struct { + RollupConstants rollupConstants `json:"hermez"` + AuctionConstants eth.AuctionConstants `json:"auction"` + WDelayerConstants eth.WDelayerConstants `json:"withdrawalDelayer"` +} diff --git a/api/handlers.go b/api/handlers.go index 14b2a94..cf5c49a 100644 --- a/api/handlers.go +++ b/api/handlers.go @@ -198,7 +198,7 @@ func getState(c *gin.Context) { } func getConfig(c *gin.Context) { - + c.JSON(http.StatusOK, cg) } func getTokens(c *gin.Context) { diff --git a/api/swagger.yml b/api/swagger.yml index 72223ef..e17d98f 100644 --- a/api/swagger.yml +++ b/api/swagger.yml @@ -2330,104 +2330,175 @@ components: type: object description: Constant configuration of the Hermez smart contract. properties: - HEZTokenAddress: - allOf: - - $ref: '#/components/schemas/EthereumAddress' - - description: Ethereum address of the HEZ token. - - example: "0x444dc4262BCDbf85190C01c996b4C06a461d2430" - maxTxVerifiers: + publicConstants: + type: object + description: Public Hermez smart contract constants + properties: + tokenHEZ: + allOf: + - $ref: '#/components/schemas/EthereumAddress' + - description: Ethereum address of the HEZ token. + - example: "0x444dc4262BCDbf85190C01c996b4C06a461d2430" + absoluteMaxL1L2BatchTimeout: + type: integer + description: L1L2 Batch Timeout + example: 240 + verifiers: + type: array + description: List of verifiers struct + items: + type: object + properties: + maxTx: + type: integer + description: Maximum rollup transactions in a batch + example: 512 + nlevels: + type: integer + description: Number of levels of the circuit + example: 32 + required: + - maxTx + - nlevels + additionalProperties: false + hermezAuctionContract: + allOf: + - $ref: '#/components/schemas/EthereumAddress' + - description: Ethereum address of the auction smart contract. + - example: "0x111dc4262BCDbf85190C01c996b4C06a461d2430" + hermezGovernanceDAOAddress: + allOf: + - $ref: '#/components/schemas/EthereumAddress' + - description: Ethereum address of the governanceDAO. + - example: "0x222dc4262BCDbf85190C01c996b4C06a461d2430" + safetyAddress: + allOf: + - $ref: '#/components/schemas/EthereumAddress' + - description: Ethereum address of the safety. + - example: "0x333dc4262BCDbf85190C01c996b4C06a461d2430" + withdrawDelayerContract: + allOf: + - $ref: '#/components/schemas/EthereumAddress' + - description: Ethereum address of the withdraw delayer contracts. + - example: "0x444dc4262BCDbf85190C01c996b4C06a461d2430" + required: + - tokenHEZ + - absoluteMaxL1L2BatchTimeout + - verifiers + - hermezAuctionContract + - hermezGovernanceDAOAddress + - safetyAddress + - withdrawDelayerContract + additionalProperties: false + maxFeeIdxCoordinator: type: integer - description: Maximum transactions of the verifiers. - example: 100 - maxLoadAmount: + description: is the maximum number of tokens the coordinator can use to collect fees. + example: 64 + reservedIdx: + type: integer + description: First 256 indexes reserved, first user index will be the 256. + example: 255 + exitIdx: + type: integer + description: Account index used to indicate that a transaction is an `exit` or `force exit`. + example: 1 + limitLoadAmount: type: integer description: Maximum load amount (L1 to L2) allowed. example: 321 - maxAmount: + limitL2TransferAmount: type: integer description: Maximum amount (L2 to L2) allowed. example: 837 - maxTokens: + limitTokens: type: integer description: Maximum number of different tokens that can be registered in the network. example: 4294967295 - reservedAccountIndex: - type: integer - description: First user index. Bellow this number the indexes are reserved for the protocol. - example: 256 - lastAccountIndex: - type: integer - description: Biggest account index that can be registered. - example: 4294967295 - exitAccountIndex: - type: integer - description: Account index used to indicate that a transaction is an `exit` or `force exit`. - example: 1 - L1CoordinatorBytes: + l1CoordinatorTotalBytes: type: integer description: Number of bytes that a L1 coordinator transaction has ([4 bytes] token + [32 bytes] babyjub + [65 bytes] compressedSignature). - example: 23 - L1UserBytes: - type: integer - description: Number of bytes that a L2 transaction has ([4 bytes] fromIdx + [4 bytes] toIdx + [2 bytes] amountFloat16 + [1 bytes] fee). - example: 32 - L2Bytes: + example: 101 + l1UserTotalBytes: type: integer - description: Number of bytes that a L2 transaction has ([4 bytes] fromIdx + [4 bytes] toIdx + [2 bytes] amountFloat16 + [1 bytes] fee). - example: 33 - maxL1Transactions: + description: Number of bytes that a L1 user transaction has ([20 bytes] fromEthAddr + [32 bytes] fromBjj-compressed + [6 bytes] fromIdx + [2 bytes] loadAmountFloat16 + [2 bytes] amountFloat16 + [4 bytes] tokenId + [6 bytes] toIdx). + example: 72 + maxL1UserTx: type: integer - description: Maximum L1 transactions allowed to be queued in a batch. + description: Maximum L1-user transactions allowed to be queued in a batch. example: 128 - maxL1UserTransactions: + maxL1Tx: type: integer - description: Maximum L1 transactions allowed to be queued in a batch by users (anyone who is not a coordinator). - example: 32 - RField: - allOf: - - $ref: '#/components/schemas/BigInt' - - description: Modulus zkSNARK. - withdrawManager: - type: object - description: Constant configuration of the withdraw manager smart contract. - properties: - noLimit: + description: Maximum L1 transactions allowed to be queued in a batch. + example: 256 + inputSHAConstantBytes: type: integer - description: Reserved bucket index when the token value is 0 USD. - example: 0 - amountOfBuckets: + description: Input SHA constant bytes + example: 18542 + numBuckets: type: integer description: Number of buckets + example: 5 maxWithdrawalDelay: type: integer - description: Maximum delay to withdraw tokens. Time is measured in Ethereum blocks. + description: Maximum delay to withdraw tokens. Time is measured in seconds. + example: 2 * 7 * 24 * 60 * 60 + exchangeMultiplier: + type: integer + description: exchange multiplier + example: 1e14 + required: + - publicConstants + - reservedIdx + - exitIdx + - limitLoadAmount + - limitL2TransferAmount + - limitTokens + - l1CoordinatorTotalBytes + - l1UserTotalBytes + - maxL1UserTx + - maxL1Tx + - inputSHAConstantBytes + - numBuckets + - maxWithdrawalDelay + - exchangeMultiplier + additionalProperties: false auction: type: object description: Constant configuration of the auction smart contract. properties: - HEZTokenAddress: + blocksPerSlot: + type: integer + description: Blocks per slot. + initialMinimalBidding: + type: integer + description: Minimum bid when no one has bid yet. + genesisBlockNum: + allOf: + - $ref: '#/components/schemas/EthBlockNum' + - description: Ethereum block number in which the smart contract starts operating. + tokenHEZ: allOf: - $ref: '#/components/schemas/EthereumAddress' - description: Ethereum address of the HEZ token. - example: "0x333dc4262BCDbf85190C01c996b4C06a461d2430" - rollupAddress: + hermezRollup: allOf: - $ref: '#/components/schemas/EthereumAddress' - description: Ethereum address of the rollup smart contract. - example: "0x222dc4262BCDbf85190C01c996b4C06a461d2430" - genesisBlockNum: + governanceAddress: allOf: - - $ref: '#/components/schemas/EthBlockNum' - - description: Ethereum block number in which the smart contract starts operating. - delayGenesis: - type: integer - description: Time delay between `genesisBlockNum` and the beginning of the first block. Time is measured in Ethereum blocks. - blocksPerSlot: - type: integer - description: Blocks per slot. - initialMinimalBidding: - type: integer - description: Minimum bid when no one has bid yet. + - $ref: '#/components/schemas/EthereumAddress' + - description: Ethereum address of the governance. + - example: "0x444dc4262BCDbf85190C01c996b4C06a461d2430" + required: + - blocksPerSlot + - initialMinimalBidding + - genesisBlockNum + - tokenHEZ + - hermezRollup + - governanceAddress + additionalProperties: false withdrawalDelayer: type: object description: Constant configuration of the withdrawal delayer smart contract. @@ -2440,6 +2511,21 @@ components: type: integer description: Maximum amount of time in which the contract can be in emergency mode. Time is measured in Ethereum blocks. example: 2000 + hermezRollup: + allOf: + - $ref: '#/components/schemas/EthereumAddress' + - description: Ethereum address of the rollup smart contract. + - example: "0x222dc4262BCDbf85190C01c996b4C06a461d2430" + required: + - maxWithdrawalDelay + - maxEmergencyModeTime + - hermezRollup + additionalProperties: false + required: + - hermez + - auction + - withdrawalDelayer + additionalProperties: false Error: type: object description: Error response. diff --git a/eth/auction.go b/eth/auction.go index 714028c..08b29b1 100644 --- a/eth/auction.go +++ b/eth/auction.go @@ -22,18 +22,18 @@ import ( // AuctionConstants are the constants of the Rollup Smart Contract type AuctionConstants struct { // Blocks per slot - BlocksPerSlot uint8 + BlocksPerSlot uint8 `json:"blocksPerSlot"` // Minimum bid when no one has bid yet - InitialMinimalBidding *big.Int + InitialMinimalBidding *big.Int `json:"initialMinimalBidding"` // First block where the first slot begins - GenesisBlockNum int64 + GenesisBlockNum int64 `json:"genesisBlockNum"` // ERC777 token with which the bids will be made - TokenHEZ ethCommon.Address + TokenHEZ ethCommon.Address `json:"tokenHEZ"` // HermezRollup smartcontract address - HermezRollup ethCommon.Address + HermezRollup ethCommon.Address `json:"hermezRollup"` // Hermez Governanze Token smartcontract address who controls some parameters and collects HEZ fee // Only for test - GovernanceAddress ethCommon.Address + GovernanceAddress ethCommon.Address `json:"governanceAddress"` } // SlotState is the state of a slot diff --git a/eth/rollup.go b/eth/rollup.go index 81e2a4b..7f89d70 100644 --- a/eth/rollup.go +++ b/eth/rollup.go @@ -101,13 +101,13 @@ var ( // RollupPublicConstants are the constants of the Rollup Smart Contract type RollupPublicConstants struct { - AbsoluteMaxL1L2BatchTimeout int64 - TokenHEZ ethCommon.Address - Verifiers []RollupVerifierStruct - HermezAuctionContract ethCommon.Address - HermezGovernanceDAOAddress ethCommon.Address - SafetyAddress ethCommon.Address - WithdrawDelayerContract ethCommon.Address + AbsoluteMaxL1L2BatchTimeout int64 `json:"absoluteMaxL1L2BatchTimeout"` + TokenHEZ ethCommon.Address `json:"tokenHEZ"` + Verifiers []RollupVerifierStruct `json:"verifiers"` + HermezAuctionContract ethCommon.Address `json:"hermezAuctionContract"` + HermezGovernanceDAOAddress ethCommon.Address `json:"hermezGovernanceDAOAddress"` + SafetyAddress ethCommon.Address `json:"safetyAddress"` + WithdrawDelayerContract ethCommon.Address `json:"withdrawDelayerContract"` } // RollupVariables are the variables of the Rollup Smart Contract @@ -133,8 +133,8 @@ func NewQueueStruct() *QueueStruct { // RollupVerifierStruct is the information about verifiers of the Rollup Smart Contract type RollupVerifierStruct struct { - MaxTx int64 - NLevels int64 + MaxTx int64 `json:"maxTx"` + NLevels int64 `json:"nlevels"` } // RollupState represents the state of the Rollup in the Smart Contract diff --git a/eth/wdelayer.go b/eth/wdelayer.go index c032b23..e4a96cb 100644 --- a/eth/wdelayer.go +++ b/eth/wdelayer.go @@ -19,11 +19,11 @@ import ( // WDelayerConstants are the constants of the Rollup Smart Contract type WDelayerConstants struct { // Max Withdrawal Delay - MaxWithdrawalDelay uint64 + MaxWithdrawalDelay uint64 `json:"maxWithdrawalDelay"` // Max Emergency mode time - MaxEmergencyModeTime uint64 + MaxEmergencyModeTime uint64 `json:"maxEmergencyModeTime"` // HermezRollup smartcontract address - HermezRollup ethCommon.Address + HermezRollup ethCommon.Address `json:"hermezRollup"` } // DepositState is the state of Deposit