Browse Source

Merge pull request #478 from hermeznetwork/feature/api-bid-big-int

Remove big.Int from state API
feature/sql-semaphore1
a_bennassar 3 years ago
committed by GitHub
parent
commit
1550b69f26
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 135 additions and 49 deletions
  1. +6
    -6
      api/api.go
  2. +43
    -2
      api/state.go
  3. +29
    -9
      api/state_test.go
  4. +7
    -7
      api/swagger.yml
  5. +11
    -11
      common/ethauction.go
  6. +14
    -14
      common/ethrollup.go
  7. +25
    -0
      db/historydb/views.go

+ 6
- 6
api/api.go

@ -22,12 +22,12 @@ const (
// Status define status of the network // Status define status of the network
type Status struct { type Status struct {
sync.RWMutex 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 // API serves HTTP requests to allow external interaction with the Hermez node

+ 43
- 2
api/state.go

@ -2,6 +2,7 @@ package api
import ( import (
"database/sql" "database/sql"
"fmt"
"math/big" "math/big"
"net/http" "net/http"
"time" "time"
@ -88,7 +89,27 @@ func (a *API) SetWDelayerVariables(wDelayerVariables common.WDelayerVariables) {
// SetAuctionVariables set Status.Auction variables // SetAuctionVariables set Status.Auction variables
func (a *API) SetAuctionVariables(auctionVariables common.AuctionVariables) { func (a *API) SetAuctionVariables(auctionVariables common.AuctionVariables) {
a.status.Lock() 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() a.status.Unlock()
} }
@ -148,6 +169,21 @@ func (a *API) UpdateNetworkInfo(
return nil 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 // getNextForgers returns next forgers
func (a *API) getNextForgers(lastBlock common.Block, currentSlot, lastClosedSlot int64) ([]NextForger, error) { func (a *API) getNextForgers(lastBlock common.Block, currentSlot, lastClosedSlot int64) ([]NextForger, error) {
secondsPerBlock := int64(15) //nolint:gomnd secondsPerBlock := int64(15) //nolint:gomnd
@ -162,8 +198,13 @@ func (a *API) getNextForgers(lastBlock common.Block, currentSlot, lastClosedSlot
var minBidInfo []historydb.MinBidInfo var minBidInfo []historydb.MinBidInfo
if currentSlot >= a.status.Auction.DefaultSlotSetBidSlotNum { if currentSlot >= a.status.Auction.DefaultSlotSetBidSlotNum {
// All min bids can be calculated with the last update of AuctionVariables // 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{{ minBidInfo = []historydb.MinBidInfo{{
DefaultSlotSetBid: a.status.Auction.DefaultSlotSetBid,
DefaultSlotSetBid: bigIntSlots,
DefaultSlotSetBidSlotNum: a.status.Auction.DefaultSlotSetBidSlotNum, DefaultSlotSetBidSlotNum: a.status.Auction.DefaultSlotSetBidSlotNum,
}} }}
} else { } else {

+ 29
- 9
api/state_test.go

@ -12,12 +12,12 @@ import (
) )
type testStatus struct { 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 { type testNetwork struct {
@ -59,9 +59,29 @@ func TestSetWDelayerVariables(t *testing.T) {
func TestSetAuctionVariables(t *testing.T) { func TestSetAuctionVariables(t *testing.T) {
auctionVars := &common.AuctionVariables{} auctionVars := &common.AuctionVariables{}
assert.Equal(t, *auctionVars, api.status.Auction)
assertEqualAuctionVariables(t, *auctionVars, api.status.Auction)
api.SetAuctionVariables(tc.auctionVars) 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) { 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 // So they won't be checked here, they are checked at
// TestUpdateNetworkInfo // TestUpdateNetworkInfo
assertEqualRollupVariables(t, tc.rollupVars, status.Rollup, false) 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) assert.Equal(t, tc.wdelayerVars, status.WithdrawalDelayer)
// Network // Network
assert.Equal(t, lastBlock.Num, status.Network.LastEthBlock) assert.Equal(t, lastBlock.Num, status.Network.LastEthBlock)

+ 7
- 7
api/swagger.yml

@ -2650,8 +2650,8 @@ components:
type: array 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]`." 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: items:
type: integer
example: [32,0,68,21,55,99]
type: string
example: ["32","0","68","21","55","99"]
defaultSlotSetBidSlotNum: defaultSlotSetBidSlotNum:
type: integer type: integer
description: Slot in which the changes will be applied for the first time. description: Slot in which the changes will be applied for the first time.
@ -2695,7 +2695,7 @@ components:
feeAddToken: feeAddToken:
type: string type: string
description: Fee to pay when registering tokens into the network. description: Fee to pay when registering tokens into the network.
example: 5698
example: "5698"
withdrawalDelay: withdrawalDelay:
type: integer type: integer
description: Withdraw delay in seconds description: Withdraw delay in seconds
@ -2709,19 +2709,19 @@ components:
ceilUSD: ceilUSD:
type: string type: string
description: Max USD value description: Max USD value
example: 1000
example: "1000"
withdrawals: withdrawals:
type: string type: string
description: Available withdrawals of the bucket description: Available withdrawals of the bucket
example: 4
example: "4"
blockWithdrawalRate: blockWithdrawalRate:
type: string type: string
description: Every `blockWithdrawalRate` blocks add 1 withdrawal description: Every `blockWithdrawalRate` blocks add 1 withdrawal
example: 8
example: "8"
maxWithdrawals: maxWithdrawals:
type: string type: string
description: Max withdrawals the bucket can hold description: Max withdrawals the bucket can hold
example: 4
example: "4"
additionalProperties: false additionalProperties: false
required: required:
- ceilUSD - ceilUSD

+ 11
- 11
common/ethauction.go

@ -52,27 +52,27 @@ func (c *AuctionConstants) RelativeBlock(blockNum int64) int64 {
// AuctionVariables are the variables of the Auction Smart Contract // AuctionVariables are the variables of the Auction Smart Contract
type AuctionVariables struct { type AuctionVariables struct {
EthBlockNum int64 `json:"ethereumBlockNum" meddler:"eth_block_num"`
EthBlockNum int64 `meddler:"eth_block_num"`
// Donation Address // Donation Address
DonationAddress ethCommon.Address `json:"donationAddress" meddler:"donation_address" validate:"required"`
DonationAddress ethCommon.Address `meddler:"donation_address" validate:"required"`
// Boot Coordinator Address // Boot Coordinator Address
BootCoordinator ethCommon.Address `json:"bootCoordinator" meddler:"boot_coordinator" validate:"required"`
BootCoordinator ethCommon.Address `meddler:"boot_coordinator" validate:"required"`
// Boot Coordinator URL // 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 // 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 // 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 ) // 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 ) // 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%) // 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 // 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 // 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 // Copy returns a deep copy of the Variables

+ 14
- 14
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 // BucketParams are the parameter variables of each Bucket of Rollup Smart
// Contract // Contract
type BucketParams struct { 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) // BucketUpdate are the bucket updates (tracking the withdrawals value changes)
// in Rollup Smart Contract // in Rollup Smart Contract
type BucketUpdate struct { 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 // 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 // RollupVariables are the variables of the Rollup Smart Contract
type RollupVariables struct { 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 // Copy returns a deep copy of the Variables

+ 25
- 0
db/historydb/views.go

@ -371,3 +371,28 @@ type RollupVariablesAPI struct {
Buckets [common.RollupConstNumBuckets]BucketParamsAPI `json:"buckets" meddler:"buckets,json"` Buckets [common.RollupConstNumBuckets]BucketParamsAPI `json:"buckets" meddler:"buckets,json"`
SafeMode bool `json:"safeMode" meddler:"safe_mode"` 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"`
}

Loading…
Cancel
Save