arnaucube
arnaucube/hermez-nodePublic Self-hosted repository
Merge pull request #476 from hermeznetwork/feature/api-big-ints-removal

Remove bigints from API
a_bennassar arnaubennassar5@gmail.com
Tue, 12 Jan 2021 15:35:16 +0100
commit

a5643c16f6762fe47ff214730a7eff30386f352a

parent

dade77046b2044362ca73b76ce179ebf585d1024

M api/api.go → api/api.go

@@ -22,12 +22,12 @@

// Status define status of the network type Status struct { sync.RWMutex - Network Network `json:"network"` - Metrics historydb.Metrics `json:"metrics"` - Rollup common.RollupVariables `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 common.AuctionVariables `json:"auction"` + WithdrawalDelayer common.WDelayerVariables `json:"withdrawalDelayer"` + RecommendedFee common.RecommendedFee `json:"recommendedFee"` } // API serves HTTP requests to allow external interaction with the Hermez node
M api/state.go → api/state.go

@@ -8,6 +8,7 @@ "time"

ethCommon "github.com/ethereum/go-ethereum/common" "github.com/gin-gonic/gin" + "github.com/hermeznetwork/hermez-node/apitypes" "github.com/hermeznetwork/hermez-node/common" "github.com/hermeznetwork/hermez-node/db/historydb" "github.com/hermeznetwork/tracerr"

@@ -57,7 +58,23 @@

// SetRollupVariables set Status.Rollup variables func (a *API) SetRollupVariables(rollupVariables common.RollupVariables) { 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() }
M api/state_test.go → api/state_test.go

@@ -4,6 +4,7 @@ import (

"math/big" "testing" + "github.com/hermeznetwork/hermez-node/apitypes" "github.com/hermeznetwork/hermez-node/common" "github.com/hermeznetwork/hermez-node/db/historydb" "github.com/stretchr/testify/assert"

@@ -11,12 +12,12 @@ "github.com/stretchr/testify/require"

) type testStatus struct { - Network testNetwork `json:"network"` - Metrics historydb.Metrics `json:"metrics"` - Rollup common.RollupVariables `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 common.AuctionVariables `json:"auction"` + WithdrawalDelayer common.WDelayerVariables `json:"withdrawalDelayer"` + RecommendedFee common.RecommendedFee `json:"recommendedFee"` } type testNetwork struct {

@@ -29,9 +30,24 @@ }

func TestSetRollupVariables(t *testing.T) { rollupVars := &common.RollupVariables{} - assert.Equal(t, *rollupVars, api.status.Rollup) + assertEqualRollupVariables(t, *rollupVars, api.status.Rollup, true) 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) {

@@ -88,8 +104,8 @@ assert.Equal(t, lastBlock.Num, api.status.Network.LastSyncBlock)

assert.Equal(t, lastBatchNum, api.status.Network.LastBatch.BatchNum) 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, api.status.Rollup.Buckets[0].Withdrawals, big.NewInt(123)) - assert.Equal(t, api.status.Rollup.Buckets[2].Withdrawals, big.NewInt(43)) + assert.Equal(t, api.status.Rollup.Buckets[0].Withdrawals, apitypes.NewBigIntStr(big.NewInt(123))) + assert.Equal(t, api.status.Rollup.Buckets[2].Withdrawals, apitypes.NewBigIntStr(big.NewInt(43))) } func TestUpdateMetrics(t *testing.T) {

@@ -141,10 +157,9 @@ assert.NoError(t, doGoodReq("GET", endpoint, nil, &status))

// SC vars // 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 - status.Rollup.Buckets = tc.rollupVars.Buckets - assert.Equal(t, tc.rollupVars, status.Rollup) + assertEqualRollupVariables(t, tc.rollupVars, status.Rollup, false) assert.Equal(t, tc.auctionVars, status.Auction) assert.Equal(t, tc.wdelayerVars, status.WithdrawalDelayer) // Network
M api/swagger.yml → api/swagger.yml

@@ -2693,7 +2693,7 @@ type: integer

description: Max Ethereum blocks after the last L1-L2-batch, when exceeds the timeout only L1-L2-batch are allowed. example: 5 feeAddToken: - type: integer + type: string description: Fee to pay when registering tokens into the network. example: 5698 withdrawalDelay:

@@ -2707,19 +2707,19 @@ items:

type: object properties: ceilUSD: - type: integer + type: string description: Max USD value example: 1000 withdrawals: - type: integer + type: string description: Available withdrawals of the bucket example: 4 blockWithdrawalRate: - type: integer + type: string description: Every `blockWithdrawalRate` blocks add 1 withdrawal example: 8 maxWithdrawals: - type: integer + type: string description: Max withdrawals the bucket can hold example: 4 additionalProperties: false
M db/historydb/historydb.go → db/historydb/historydb.go

@@ -1380,8 +1380,8 @@ return db.SlicePtrsToSlice(bucketUpdates).([]common.BucketUpdate), tracerr.Wrap(err)

} // GetBucketUpdates retrieves latest values for each bucket -func (hdb *HistoryDB) GetBucketUpdates() ([]common.BucketUpdate, error) { - var bucketUpdates []*common.BucketUpdate +func (hdb *HistoryDB) GetBucketUpdates() ([]BucketUpdateAPI, error) { + var bucketUpdates []*BucketUpdateAPI err := meddler.QueryAll( hdb.db, &bucketUpdates, `SELECT num_bucket, withdrawals FROM bucket_update

@@ -1389,7 +1389,7 @@ WHERE item_id in(SELECT max(item_id) FROM bucket_update

group by num_bucket) 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 {
M db/historydb/views.go → db/historydb/views.go

@@ -343,3 +343,31 @@ type MinBidInfo struct {

DefaultSlotSetBid [6]*big.Int `json:"defaultSlotSetBid" meddler:"default_slot_set_bid,json" validate:"required"` 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"` +}