mirror of
https://github.com/arnaucube/hermez-node.git
synced 2026-02-07 03:16:45 +01:00
Remove big.Int from state API
This commit is contained in:
12
api/api.go
12
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
|
||||
|
||||
45
api/state.go
45
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 {
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user