mirror of
https://github.com/arnaucube/hermez-node.git
synced 2026-02-07 03:16:45 +01:00
Merge pull request #476 from hermeznetwork/feature/api-big-ints-removal
Remove bigints from API
This commit is contained in:
@@ -24,7 +24,7 @@ type Status struct {
|
|||||||
sync.RWMutex
|
sync.RWMutex
|
||||||
Network Network `json:"network"`
|
Network Network `json:"network"`
|
||||||
Metrics historydb.Metrics `json:"metrics"`
|
Metrics historydb.Metrics `json:"metrics"`
|
||||||
Rollup common.RollupVariables `json:"rollup"`
|
Rollup historydb.RollupVariablesAPI `json:"rollup"`
|
||||||
Auction common.AuctionVariables `json:"auction"`
|
Auction common.AuctionVariables `json:"auction"`
|
||||||
WithdrawalDelayer common.WDelayerVariables `json:"withdrawalDelayer"`
|
WithdrawalDelayer common.WDelayerVariables `json:"withdrawalDelayer"`
|
||||||
RecommendedFee common.RecommendedFee `json:"recommendedFee"`
|
RecommendedFee common.RecommendedFee `json:"recommendedFee"`
|
||||||
|
|||||||
19
api/state.go
19
api/state.go
@@ -8,6 +8,7 @@ import (
|
|||||||
|
|
||||||
ethCommon "github.com/ethereum/go-ethereum/common"
|
ethCommon "github.com/ethereum/go-ethereum/common"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/hermeznetwork/hermez-node/apitypes"
|
||||||
"github.com/hermeznetwork/hermez-node/common"
|
"github.com/hermeznetwork/hermez-node/common"
|
||||||
"github.com/hermeznetwork/hermez-node/db/historydb"
|
"github.com/hermeznetwork/hermez-node/db/historydb"
|
||||||
"github.com/hermeznetwork/tracerr"
|
"github.com/hermeznetwork/tracerr"
|
||||||
@@ -57,7 +58,23 @@ func (a *API) getState(c *gin.Context) {
|
|||||||
// SetRollupVariables set Status.Rollup variables
|
// SetRollupVariables set Status.Rollup variables
|
||||||
func (a *API) SetRollupVariables(rollupVariables common.RollupVariables) {
|
func (a *API) SetRollupVariables(rollupVariables common.RollupVariables) {
|
||||||
a.status.Lock()
|
a.status.Lock()
|
||||||
a.status.Rollup = rollupVariables
|
var rollupVAPI historydb.RollupVariablesAPI
|
||||||
|
rollupVAPI.EthBlockNum = rollupVariables.EthBlockNum
|
||||||
|
rollupVAPI.FeeAddToken = apitypes.NewBigIntStr(rollupVariables.FeeAddToken)
|
||||||
|
rollupVAPI.ForgeL1L2BatchTimeout = rollupVariables.ForgeL1L2BatchTimeout
|
||||||
|
rollupVAPI.WithdrawalDelay = rollupVariables.WithdrawalDelay
|
||||||
|
|
||||||
|
for i, bucket := range rollupVariables.Buckets {
|
||||||
|
var apiBucket historydb.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)
|
||||||
|
rollupVAPI.Buckets[i] = apiBucket
|
||||||
|
}
|
||||||
|
|
||||||
|
rollupVAPI.SafeMode = rollupVariables.SafeMode
|
||||||
|
a.status.Rollup = rollupVAPI
|
||||||
a.status.Unlock()
|
a.status.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"math/big"
|
"math/big"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/hermeznetwork/hermez-node/apitypes"
|
||||||
"github.com/hermeznetwork/hermez-node/common"
|
"github.com/hermeznetwork/hermez-node/common"
|
||||||
"github.com/hermeznetwork/hermez-node/db/historydb"
|
"github.com/hermeznetwork/hermez-node/db/historydb"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
@@ -13,7 +14,7 @@ import (
|
|||||||
type testStatus struct {
|
type testStatus struct {
|
||||||
Network testNetwork `json:"network"`
|
Network testNetwork `json:"network"`
|
||||||
Metrics historydb.Metrics `json:"metrics"`
|
Metrics historydb.Metrics `json:"metrics"`
|
||||||
Rollup common.RollupVariables `json:"rollup"`
|
Rollup historydb.RollupVariablesAPI `json:"rollup"`
|
||||||
Auction common.AuctionVariables `json:"auction"`
|
Auction common.AuctionVariables `json:"auction"`
|
||||||
WithdrawalDelayer common.WDelayerVariables `json:"withdrawalDelayer"`
|
WithdrawalDelayer common.WDelayerVariables `json:"withdrawalDelayer"`
|
||||||
RecommendedFee common.RecommendedFee `json:"recommendedFee"`
|
RecommendedFee common.RecommendedFee `json:"recommendedFee"`
|
||||||
@@ -29,9 +30,24 @@ type testNetwork struct {
|
|||||||
|
|
||||||
func TestSetRollupVariables(t *testing.T) {
|
func TestSetRollupVariables(t *testing.T) {
|
||||||
rollupVars := &common.RollupVariables{}
|
rollupVars := &common.RollupVariables{}
|
||||||
assert.Equal(t, *rollupVars, api.status.Rollup)
|
assertEqualRollupVariables(t, *rollupVars, api.status.Rollup, true)
|
||||||
api.SetRollupVariables(tc.rollupVars)
|
api.SetRollupVariables(tc.rollupVars)
|
||||||
assert.Equal(t, tc.rollupVars, api.status.Rollup)
|
assertEqualRollupVariables(t, tc.rollupVars, api.status.Rollup, true)
|
||||||
|
}
|
||||||
|
|
||||||
|
func assertEqualRollupVariables(t *testing.T, rollupVariables common.RollupVariables, apiVariables historydb.RollupVariablesAPI, checkBuckets bool) {
|
||||||
|
assert.Equal(t, apitypes.NewBigIntStr(rollupVariables.FeeAddToken), apiVariables.FeeAddToken)
|
||||||
|
assert.Equal(t, rollupVariables.ForgeL1L2BatchTimeout, apiVariables.ForgeL1L2BatchTimeout)
|
||||||
|
assert.Equal(t, rollupVariables.WithdrawalDelay, apiVariables.WithdrawalDelay)
|
||||||
|
assert.Equal(t, rollupVariables.SafeMode, apiVariables.SafeMode)
|
||||||
|
if checkBuckets {
|
||||||
|
for i, bucket := range rollupVariables.Buckets {
|
||||||
|
assert.Equal(t, apitypes.NewBigIntStr(bucket.BlockWithdrawalRate), apiVariables.Buckets[i].BlockWithdrawalRate)
|
||||||
|
assert.Equal(t, apitypes.NewBigIntStr(bucket.CeilUSD), apiVariables.Buckets[i].CeilUSD)
|
||||||
|
assert.Equal(t, apitypes.NewBigIntStr(bucket.MaxWithdrawals), apiVariables.Buckets[i].MaxWithdrawals)
|
||||||
|
assert.Equal(t, apitypes.NewBigIntStr(bucket.Withdrawals), apiVariables.Buckets[i].Withdrawals)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSetWDelayerVariables(t *testing.T) {
|
func TestSetWDelayerVariables(t *testing.T) {
|
||||||
@@ -88,8 +104,8 @@ func TestUpdateNetworkInfo(t *testing.T) {
|
|||||||
assert.Equal(t, lastBatchNum, api.status.Network.LastBatch.BatchNum)
|
assert.Equal(t, lastBatchNum, api.status.Network.LastBatch.BatchNum)
|
||||||
assert.Equal(t, currentSlotNum, api.status.Network.CurrentSlot)
|
assert.Equal(t, currentSlotNum, api.status.Network.CurrentSlot)
|
||||||
assert.Equal(t, int(api.status.Auction.ClosedAuctionSlots)+1, len(api.status.Network.NextForgers))
|
assert.Equal(t, int(api.status.Auction.ClosedAuctionSlots)+1, len(api.status.Network.NextForgers))
|
||||||
assert.Equal(t, api.status.Rollup.Buckets[0].Withdrawals, big.NewInt(123))
|
assert.Equal(t, api.status.Rollup.Buckets[0].Withdrawals, apitypes.NewBigIntStr(big.NewInt(123)))
|
||||||
assert.Equal(t, api.status.Rollup.Buckets[2].Withdrawals, big.NewInt(43))
|
assert.Equal(t, api.status.Rollup.Buckets[2].Withdrawals, apitypes.NewBigIntStr(big.NewInt(43)))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUpdateMetrics(t *testing.T) {
|
func TestUpdateMetrics(t *testing.T) {
|
||||||
@@ -141,10 +157,9 @@ func TestGetState(t *testing.T) {
|
|||||||
|
|
||||||
// SC vars
|
// SC vars
|
||||||
// UpdateNetworkInfo will overwrite buckets withdrawal values
|
// UpdateNetworkInfo will overwrite buckets withdrawal values
|
||||||
// So we restore them before comparing, they are checked at
|
// So they won't be checked here, they are checked at
|
||||||
// TestUpdateNetworkInfo
|
// TestUpdateNetworkInfo
|
||||||
status.Rollup.Buckets = tc.rollupVars.Buckets
|
assertEqualRollupVariables(t, tc.rollupVars, status.Rollup, false)
|
||||||
assert.Equal(t, tc.rollupVars, status.Rollup)
|
|
||||||
assert.Equal(t, tc.auctionVars, status.Auction)
|
assert.Equal(t, tc.auctionVars, status.Auction)
|
||||||
assert.Equal(t, tc.wdelayerVars, status.WithdrawalDelayer)
|
assert.Equal(t, tc.wdelayerVars, status.WithdrawalDelayer)
|
||||||
// Network
|
// Network
|
||||||
|
|||||||
@@ -2693,7 +2693,7 @@ components:
|
|||||||
description: Max Ethereum blocks after the last L1-L2-batch, when exceeds the timeout only L1-L2-batch are allowed.
|
description: Max Ethereum blocks after the last L1-L2-batch, when exceeds the timeout only L1-L2-batch are allowed.
|
||||||
example: 5
|
example: 5
|
||||||
feeAddToken:
|
feeAddToken:
|
||||||
type: integer
|
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:
|
||||||
@@ -2707,19 +2707,19 @@ components:
|
|||||||
type: object
|
type: object
|
||||||
properties:
|
properties:
|
||||||
ceilUSD:
|
ceilUSD:
|
||||||
type: integer
|
type: string
|
||||||
description: Max USD value
|
description: Max USD value
|
||||||
example: 1000
|
example: 1000
|
||||||
withdrawals:
|
withdrawals:
|
||||||
type: integer
|
type: string
|
||||||
description: Available withdrawals of the bucket
|
description: Available withdrawals of the bucket
|
||||||
example: 4
|
example: 4
|
||||||
blockWithdrawalRate:
|
blockWithdrawalRate:
|
||||||
type: integer
|
type: string
|
||||||
description: Every `blockWithdrawalRate` blocks add 1 withdrawal
|
description: Every `blockWithdrawalRate` blocks add 1 withdrawal
|
||||||
example: 8
|
example: 8
|
||||||
maxWithdrawals:
|
maxWithdrawals:
|
||||||
type: integer
|
type: string
|
||||||
description: Max withdrawals the bucket can hold
|
description: Max withdrawals the bucket can hold
|
||||||
example: 4
|
example: 4
|
||||||
additionalProperties: false
|
additionalProperties: false
|
||||||
|
|||||||
@@ -1380,8 +1380,8 @@ func (hdb *HistoryDB) GetAllBucketUpdates() ([]common.BucketUpdate, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetBucketUpdates retrieves latest values for each bucket
|
// GetBucketUpdates retrieves latest values for each bucket
|
||||||
func (hdb *HistoryDB) GetBucketUpdates() ([]common.BucketUpdate, error) {
|
func (hdb *HistoryDB) GetBucketUpdates() ([]BucketUpdateAPI, error) {
|
||||||
var bucketUpdates []*common.BucketUpdate
|
var bucketUpdates []*BucketUpdateAPI
|
||||||
err := meddler.QueryAll(
|
err := meddler.QueryAll(
|
||||||
hdb.db, &bucketUpdates,
|
hdb.db, &bucketUpdates,
|
||||||
`SELECT num_bucket, withdrawals FROM bucket_update
|
`SELECT num_bucket, withdrawals FROM bucket_update
|
||||||
@@ -1389,7 +1389,7 @@ func (hdb *HistoryDB) GetBucketUpdates() ([]common.BucketUpdate, error) {
|
|||||||
group by num_bucket)
|
group by num_bucket)
|
||||||
ORDER BY num_bucket ASC;`,
|
ORDER BY num_bucket ASC;`,
|
||||||
)
|
)
|
||||||
return db.SlicePtrsToSlice(bucketUpdates).([]common.BucketUpdate), tracerr.Wrap(err)
|
return db.SlicePtrsToSlice(bucketUpdates).([]BucketUpdateAPI), tracerr.Wrap(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (hdb *HistoryDB) addTokenExchanges(d meddler.DB, tokenExchanges []common.TokenExchange) error {
|
func (hdb *HistoryDB) addTokenExchanges(d meddler.DB, tokenExchanges []common.TokenExchange) error {
|
||||||
|
|||||||
@@ -343,3 +343,31 @@ type MinBidInfo struct {
|
|||||||
DefaultSlotSetBid [6]*big.Int `json:"defaultSlotSetBid" meddler:"default_slot_set_bid,json" validate:"required"`
|
DefaultSlotSetBid [6]*big.Int `json:"defaultSlotSetBid" meddler:"default_slot_set_bid,json" validate:"required"`
|
||||||
DefaultSlotSetBidSlotNum int64 `json:"-" meddler:"default_slot_set_bid_slot_num"`
|
DefaultSlotSetBidSlotNum int64 `json:"-" meddler:"default_slot_set_bid_slot_num"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// BucketUpdateAPI are the bucket updates (tracking the withdrawals value changes)
|
||||||
|
// in Rollup Smart Contract
|
||||||
|
type BucketUpdateAPI struct {
|
||||||
|
EthBlockNum int64 `json:"ethereumBlockNum" meddler:"eth_block_num"`
|
||||||
|
NumBucket int `json:"numBucket" meddler:"num_bucket"`
|
||||||
|
BlockStamp int64 `json:"blockStamp" meddler:"block_stamp"`
|
||||||
|
Withdrawals *apitypes.BigIntStr `json:"withdrawals" meddler:"withdrawals"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// BucketParamsAPI are the parameter variables of each Bucket of Rollup Smart
|
||||||
|
// Contract
|
||||||
|
type BucketParamsAPI struct {
|
||||||
|
CeilUSD *apitypes.BigIntStr `json:"ceilUSD"`
|
||||||
|
Withdrawals *apitypes.BigIntStr `json:"withdrawals"`
|
||||||
|
BlockWithdrawalRate *apitypes.BigIntStr `json:"blockWithdrawalRate"`
|
||||||
|
MaxWithdrawals *apitypes.BigIntStr `json:"maxWithdrawals"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// RollupVariablesAPI are the variables of the Rollup Smart Contract
|
||||||
|
type RollupVariablesAPI struct {
|
||||||
|
EthBlockNum int64 `json:"ethereumBlockNum" meddler:"eth_block_num"`
|
||||||
|
FeeAddToken *apitypes.BigIntStr `json:"feeAddToken" meddler:"fee_add_token" validate:"required"`
|
||||||
|
ForgeL1L2BatchTimeout int64 `json:"forgeL1L2BatchTimeout" meddler:"forge_l1_timeout" validate:"required"`
|
||||||
|
WithdrawalDelay uint64 `json:"withdrawalDelay" meddler:"withdrawal_delay" validate:"required"`
|
||||||
|
Buckets [common.RollupConstNumBuckets]BucketParamsAPI `json:"buckets" meddler:"buckets,json"`
|
||||||
|
SafeMode bool `json:"safeMode" meddler:"safe_mode"`
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user