From 170da500dbc1aeabcaee8f6a9fffea32ebeedbdf Mon Sep 17 00:00:00 2001 From: ToniRamirezM Date: Tue, 12 Jan 2021 17:27:30 +0100 Subject: [PATCH] Remove big.Int from state API --- api/api.go | 12 ++++++------ api/state.go | 45 +++++++++++++++++++++++++++++++++++++++++-- api/state_test.go | 38 +++++++++++++++++++++++++++--------- api/swagger.yml | 14 +++++++------- common/ethauction.go | 22 ++++++++++----------- common/ethrollup.go | 28 +++++++++++++-------------- db/historydb/views.go | 25 ++++++++++++++++++++++++ 7 files changed, 135 insertions(+), 49 deletions(-) diff --git a/api/api.go b/api/api.go index 8dd7edc..c05fb35 100644 --- a/api/api.go +++ b/api/api.go @@ -22,12 +22,12 @@ const ( // Status define status of the network type Status struct { sync.RWMutex - Network Network `json:"network"` - Metrics historydb.Metrics `json:"metrics"` - Rollup historydb.RollupVariablesAPI `json:"rollup"` - Auction common.AuctionVariables `json:"auction"` - WithdrawalDelayer common.WDelayerVariables `json:"withdrawalDelayer"` - RecommendedFee common.RecommendedFee `json:"recommendedFee"` + Network Network `json:"network"` + Metrics historydb.Metrics `json:"metrics"` + Rollup historydb.RollupVariablesAPI `json:"rollup"` + Auction historydb.AuctionVariablesAPI `json:"auction"` + WithdrawalDelayer common.WDelayerVariables `json:"withdrawalDelayer"` + RecommendedFee common.RecommendedFee `json:"recommendedFee"` } // API serves HTTP requests to allow external interaction with the Hermez node diff --git a/api/state.go b/api/state.go index 5dbd316..f7b4608 100644 --- a/api/state.go +++ b/api/state.go @@ -2,6 +2,7 @@ package api import ( "database/sql" + "fmt" "math/big" "net/http" "time" @@ -88,7 +89,27 @@ func (a *API) SetWDelayerVariables(wDelayerVariables common.WDelayerVariables) { // SetAuctionVariables set Status.Auction variables func (a *API) SetAuctionVariables(auctionVariables common.AuctionVariables) { a.status.Lock() - a.status.Auction = auctionVariables + var auctionAPI historydb.AuctionVariablesAPI + + auctionAPI.EthBlockNum = auctionVariables.EthBlockNum + auctionAPI.DonationAddress = auctionVariables.DonationAddress + auctionAPI.BootCoordinator = auctionVariables.BootCoordinator + auctionAPI.BootCoordinatorURL = auctionVariables.BootCoordinatorURL + auctionAPI.DefaultSlotSetBidSlotNum = auctionVariables.DefaultSlotSetBidSlotNum + auctionAPI.ClosedAuctionSlots = auctionVariables.ClosedAuctionSlots + auctionAPI.OpenAuctionSlots = auctionVariables.OpenAuctionSlots + auctionAPI.Outbidding = auctionVariables.Outbidding + auctionAPI.SlotDeadline = auctionVariables.SlotDeadline + + for i, slot := range auctionVariables.DefaultSlotSetBid { + auctionAPI.DefaultSlotSetBid[i] = apitypes.NewBigIntStr(slot) + } + + for i, ratio := range auctionVariables.AllocationRatio { + auctionAPI.AllocationRatio[i] = ratio + } + + a.status.Auction = auctionAPI a.status.Unlock() } @@ -148,6 +169,21 @@ func (a *API) UpdateNetworkInfo( return nil } +// apiSlotToBigInts converts from [6]*apitypes.BigIntStr to [6]*big.Int +func apiSlotToBigInts(defaultSlotSetBid [6]*apitypes.BigIntStr) ([6]*big.Int, error) { + var slots [6]*big.Int + + for i, slot := range defaultSlotSetBid { + bigInt, ok := new(big.Int).SetString(string(*slot), 10) + if !ok { + return slots, tracerr.Wrap(fmt.Errorf("can't convert %T into big.Int", slot)) + } + slots[i] = bigInt + } + + return slots, nil +} + // getNextForgers returns next forgers func (a *API) getNextForgers(lastBlock common.Block, currentSlot, lastClosedSlot int64) ([]NextForger, error) { secondsPerBlock := int64(15) //nolint:gomnd @@ -162,8 +198,13 @@ func (a *API) getNextForgers(lastBlock common.Block, currentSlot, lastClosedSlot var minBidInfo []historydb.MinBidInfo if currentSlot >= a.status.Auction.DefaultSlotSetBidSlotNum { // All min bids can be calculated with the last update of AuctionVariables + bigIntSlots, err := apiSlotToBigInts(a.status.Auction.DefaultSlotSetBid) + if err != nil { + return nil, tracerr.Wrap(err) + } + minBidInfo = []historydb.MinBidInfo{{ - DefaultSlotSetBid: a.status.Auction.DefaultSlotSetBid, + DefaultSlotSetBid: bigIntSlots, DefaultSlotSetBidSlotNum: a.status.Auction.DefaultSlotSetBidSlotNum, }} } else { diff --git a/api/state_test.go b/api/state_test.go index c289125..fb7a183 100644 --- a/api/state_test.go +++ b/api/state_test.go @@ -12,12 +12,12 @@ import ( ) type testStatus struct { - Network testNetwork `json:"network"` - Metrics historydb.Metrics `json:"metrics"` - Rollup historydb.RollupVariablesAPI `json:"rollup"` - Auction common.AuctionVariables `json:"auction"` - WithdrawalDelayer common.WDelayerVariables `json:"withdrawalDelayer"` - RecommendedFee common.RecommendedFee `json:"recommendedFee"` + Network testNetwork `json:"network"` + Metrics historydb.Metrics `json:"metrics"` + Rollup historydb.RollupVariablesAPI `json:"rollup"` + Auction historydb.AuctionVariablesAPI `json:"auction"` + WithdrawalDelayer common.WDelayerVariables `json:"withdrawalDelayer"` + RecommendedFee common.RecommendedFee `json:"recommendedFee"` } type testNetwork struct { @@ -59,9 +59,29 @@ func TestSetWDelayerVariables(t *testing.T) { func TestSetAuctionVariables(t *testing.T) { auctionVars := &common.AuctionVariables{} - assert.Equal(t, *auctionVars, api.status.Auction) + assertEqualAuctionVariables(t, *auctionVars, api.status.Auction) api.SetAuctionVariables(tc.auctionVars) - assert.Equal(t, tc.auctionVars, api.status.Auction) + assertEqualAuctionVariables(t, tc.auctionVars, api.status.Auction) +} + +func assertEqualAuctionVariables(t *testing.T, auctionVariables common.AuctionVariables, apiVariables historydb.AuctionVariablesAPI) { + assert.Equal(t, auctionVariables.EthBlockNum, apiVariables.EthBlockNum) + assert.Equal(t, auctionVariables.DonationAddress, apiVariables.DonationAddress) + assert.Equal(t, auctionVariables.BootCoordinator, apiVariables.BootCoordinator) + assert.Equal(t, auctionVariables.BootCoordinatorURL, apiVariables.BootCoordinatorURL) + assert.Equal(t, auctionVariables.DefaultSlotSetBidSlotNum, apiVariables.DefaultSlotSetBidSlotNum) + assert.Equal(t, auctionVariables.ClosedAuctionSlots, apiVariables.ClosedAuctionSlots) + assert.Equal(t, auctionVariables.OpenAuctionSlots, apiVariables.OpenAuctionSlots) + assert.Equal(t, auctionVariables.Outbidding, apiVariables.Outbidding) + assert.Equal(t, auctionVariables.SlotDeadline, apiVariables.SlotDeadline) + + for i, slot := range auctionVariables.DefaultSlotSetBid { + assert.Equal(t, apitypes.NewBigIntStr(slot), apiVariables.DefaultSlotSetBid[i]) + } + + for i, ratio := range auctionVariables.AllocationRatio { + assert.Equal(t, ratio, apiVariables.AllocationRatio[i]) + } } func TestUpdateNetworkInfo(t *testing.T) { @@ -160,7 +180,7 @@ func TestGetState(t *testing.T) { // So they won't be checked here, they are checked at // TestUpdateNetworkInfo assertEqualRollupVariables(t, tc.rollupVars, status.Rollup, false) - assert.Equal(t, tc.auctionVars, status.Auction) + assertEqualAuctionVariables(t, tc.auctionVars, status.Auction) assert.Equal(t, tc.wdelayerVars, status.WithdrawalDelayer) // Network assert.Equal(t, lastBlock.Num, status.Network.LastEthBlock) diff --git a/api/swagger.yml b/api/swagger.yml index c6c4a73..bdd7e18 100644 --- a/api/swagger.yml +++ b/api/swagger.yml @@ -2650,8 +2650,8 @@ components: type: array description: "Initial minimal bid for each auction. Expressed as an array of 6 values. To calculate which value corresponds to each slot: `initialMinimalBidding[slotNum%6]`." items: - type: integer - example: [32,0,68,21,55,99] + type: string + example: ["32","0","68","21","55","99"] defaultSlotSetBidSlotNum: type: integer description: Slot in which the changes will be applied for the first time. @@ -2695,7 +2695,7 @@ components: feeAddToken: type: string description: Fee to pay when registering tokens into the network. - example: 5698 + example: "5698" withdrawalDelay: type: integer description: Withdraw delay in seconds @@ -2709,19 +2709,19 @@ components: ceilUSD: type: string description: Max USD value - example: 1000 + example: "1000" withdrawals: type: string description: Available withdrawals of the bucket - example: 4 + example: "4" blockWithdrawalRate: type: string description: Every `blockWithdrawalRate` blocks add 1 withdrawal - example: 8 + example: "8" maxWithdrawals: type: string description: Max withdrawals the bucket can hold - example: 4 + example: "4" additionalProperties: false required: - ceilUSD diff --git a/common/ethauction.go b/common/ethauction.go index e0f14ba..0292407 100644 --- a/common/ethauction.go +++ b/common/ethauction.go @@ -52,27 +52,27 @@ func (c *AuctionConstants) RelativeBlock(blockNum int64) int64 { // AuctionVariables are the variables of the Auction Smart Contract type AuctionVariables struct { - EthBlockNum int64 `json:"ethereumBlockNum" meddler:"eth_block_num"` + EthBlockNum int64 `meddler:"eth_block_num"` // Donation Address - DonationAddress ethCommon.Address `json:"donationAddress" meddler:"donation_address" validate:"required"` + DonationAddress ethCommon.Address `meddler:"donation_address" validate:"required"` // Boot Coordinator Address - BootCoordinator ethCommon.Address `json:"bootCoordinator" meddler:"boot_coordinator" validate:"required"` + BootCoordinator ethCommon.Address `meddler:"boot_coordinator" validate:"required"` // Boot Coordinator URL - BootCoordinatorURL string `json:"bootCoordinatorUrl" meddler:"boot_coordinator_url" validate:"required"` + BootCoordinatorURL string `meddler:"boot_coordinator_url" validate:"required"` // The minimum bid value in a series of 6 slots - DefaultSlotSetBid [6]*big.Int `json:"defaultSlotSetBid" meddler:"default_slot_set_bid,json" validate:"required"` + DefaultSlotSetBid [6]*big.Int `meddler:"default_slot_set_bid,json" validate:"required"` // SlotNum at which the new default_slot_set_bid applies - DefaultSlotSetBidSlotNum int64 `json:"defaultSlotSetBidSlotNum" meddler:"default_slot_set_bid_slot_num"` + DefaultSlotSetBidSlotNum int64 `meddler:"default_slot_set_bid_slot_num"` // Distance (#slots) to the closest slot to which you can bid ( 2 Slots = 2 * 40 Blocks = 20 min ) - ClosedAuctionSlots uint16 `json:"closedAuctionSlots" meddler:"closed_auction_slots" validate:"required"` + ClosedAuctionSlots uint16 `meddler:"closed_auction_slots" validate:"required"` // Distance (#slots) to the farthest slot to which you can bid (30 days = 4320 slots ) - OpenAuctionSlots uint16 `json:"openAuctionSlots" meddler:"open_auction_slots" validate:"required"` + OpenAuctionSlots uint16 `meddler:"open_auction_slots" validate:"required"` // How the HEZ tokens deposited by the slot winner are distributed (Burn: 40% - Donation: 40% - HGT: 20%) - AllocationRatio [3]uint16 `json:"allocationRatio" meddler:"allocation_ratio,json" validate:"required"` + AllocationRatio [3]uint16 `meddler:"allocation_ratio,json" validate:"required"` // Minimum outbid (percentage) over the previous one to consider it valid - Outbidding uint16 `json:"outbidding" meddler:"outbidding" validate:"required"` + Outbidding uint16 `meddler:"outbidding" validate:"required"` // 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"` + SlotDeadline uint8 `meddler:"slot_deadline" validate:"required"` } // Copy returns a deep copy of the Variables diff --git a/common/ethrollup.go b/common/ethrollup.go index d992847..f612b99 100644 --- a/common/ethrollup.go +++ b/common/ethrollup.go @@ -119,19 +119,19 @@ func (c *RollupConstants) FindVerifierIdx(MaxTx, NLevels int64) (int, error) { // BucketParams are the parameter variables of each Bucket of Rollup Smart // Contract type BucketParams struct { - CeilUSD *big.Int `json:"ceilUSD"` - Withdrawals *big.Int `json:"withdrawals"` - BlockWithdrawalRate *big.Int `json:"blockWithdrawalRate"` - MaxWithdrawals *big.Int `json:"maxWithdrawals"` + CeilUSD *big.Int + Withdrawals *big.Int + BlockWithdrawalRate *big.Int + MaxWithdrawals *big.Int } // BucketUpdate are the bucket updates (tracking the withdrawals value changes) // in Rollup Smart Contract type BucketUpdate struct { - EthBlockNum int64 `json:"ethereumBlockNum" meddler:"eth_block_num"` - NumBucket int `json:"numBucket" meddler:"num_bucket"` - BlockStamp int64 `json:"blockStamp" meddler:"block_stamp"` - Withdrawals *big.Int `json:"withdrawals" meddler:"withdrawals,bigint"` + EthBlockNum int64 `meddler:"eth_block_num"` + NumBucket int `meddler:"num_bucket"` + BlockStamp int64 `meddler:"block_stamp"` + Withdrawals *big.Int `meddler:"withdrawals,bigint"` } // TokenExchange are the exchange value for tokens registered in the Rollup @@ -144,12 +144,12 @@ type TokenExchange struct { // RollupVariables are the variables of the Rollup Smart Contract type RollupVariables struct { - EthBlockNum int64 `json:"ethereumBlockNum" meddler:"eth_block_num"` - FeeAddToken *big.Int `json:"feeAddToken" meddler:"fee_add_token,bigint" validate:"required"` - ForgeL1L2BatchTimeout int64 `json:"forgeL1L2BatchTimeout" meddler:"forge_l1_timeout" validate:"required"` - WithdrawalDelay uint64 `json:"withdrawalDelay" meddler:"withdrawal_delay" validate:"required"` - Buckets [RollupConstNumBuckets]BucketParams `json:"buckets" meddler:"buckets,json"` - SafeMode bool `json:"safeMode" meddler:"safe_mode"` + EthBlockNum int64 `meddler:"eth_block_num"` + FeeAddToken *big.Int `meddler:"fee_add_token,bigint" validate:"required"` + ForgeL1L2BatchTimeout int64 `meddler:"forge_l1_timeout" validate:"required"` + WithdrawalDelay uint64 `meddler:"withdrawal_delay" validate:"required"` + Buckets [RollupConstNumBuckets]BucketParams `meddler:"buckets,json"` + SafeMode bool `meddler:"safe_mode"` } // Copy returns a deep copy of the Variables diff --git a/db/historydb/views.go b/db/historydb/views.go index 7baa378..c0f67ff 100644 --- a/db/historydb/views.go +++ b/db/historydb/views.go @@ -371,3 +371,28 @@ type RollupVariablesAPI struct { Buckets [common.RollupConstNumBuckets]BucketParamsAPI `json:"buckets" meddler:"buckets,json"` SafeMode bool `json:"safeMode" meddler:"safe_mode"` } + +// AuctionVariablesAPI are the variables of the Auction Smart Contract +type AuctionVariablesAPI struct { + EthBlockNum int64 `json:"ethereumBlockNum" meddler:"eth_block_num"` + // Donation Address + DonationAddress ethCommon.Address `json:"donationAddress" meddler:"donation_address" validate:"required"` + // Boot Coordinator Address + BootCoordinator ethCommon.Address `json:"bootCoordinator" meddler:"boot_coordinator" validate:"required"` + // Boot Coordinator URL + BootCoordinatorURL string `json:"bootCoordinatorUrl" meddler:"boot_coordinator_url" validate:"required"` + // The minimum bid value in a series of 6 slots + DefaultSlotSetBid [6]*apitypes.BigIntStr `json:"defaultSlotSetBid" meddler:"default_slot_set_bid,json" validate:"required"` + // SlotNum at which the new default_slot_set_bid applies + DefaultSlotSetBidSlotNum int64 `json:"defaultSlotSetBidSlotNum" meddler:"default_slot_set_bid_slot_num"` + // Distance (#slots) to the closest slot to which you can bid ( 2 Slots = 2 * 40 Blocks = 20 min ) + ClosedAuctionSlots uint16 `json:"closedAuctionSlots" meddler:"closed_auction_slots" validate:"required"` + // Distance (#slots) to the farthest slot to which you can bid (30 days = 4320 slots ) + OpenAuctionSlots uint16 `json:"openAuctionSlots" meddler:"open_auction_slots" validate:"required"` + // How the HEZ tokens deposited by the slot winner are distributed (Burn: 40% - Donation: 40% - HGT: 20%) + AllocationRatio [3]uint16 `json:"allocationRatio" meddler:"allocation_ratio,json" validate:"required"` + // Minimum outbid (percentage) over the previous one to consider it valid + Outbidding uint16 `json:"outbidding" meddler:"outbidding" validate:"required"` + // 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"` +}