From ce6a271448de12b21c3446910ed001b6dc82d0ca Mon Sep 17 00:00:00 2001 From: ToniRamirezM Date: Tue, 12 Jan 2021 11:05:13 +0100 Subject: [PATCH] Remove bigints from API --- api/api.go | 12 ++++++------ api/state.go | 19 +++++++++++++++++- api/state_test.go | 41 ++++++++++++++++++++++++++------------- api/swagger.yml | 10 +++++----- db/historydb/historydb.go | 6 +++--- db/historydb/views.go | 28 ++++++++++++++++++++++++++ 6 files changed, 88 insertions(+), 28 deletions(-) diff --git a/api/api.go b/api/api.go index 5f73adb..8dd7edc 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 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 diff --git a/api/state.go b/api/state.go index aa5133b..5dbd316 100644 --- a/api/state.go +++ b/api/state.go @@ -8,6 +8,7 @@ import ( 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 @@ func (a *API) getState(c *gin.Context) { // 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() } diff --git a/api/state_test.go b/api/state_test.go index c63a637..c289125 100644 --- a/api/state_test.go +++ b/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 @@ import ( ) 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 @@ type testNetwork struct { 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 @@ func TestUpdateNetworkInfo(t *testing.T) { 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 @@ func TestGetState(t *testing.T) { // 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 diff --git a/api/swagger.yml b/api/swagger.yml index ee82afa..c6c4a73 100644 --- a/api/swagger.yml +++ b/api/swagger.yml @@ -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. example: 5 feeAddToken: - type: integer + type: string description: Fee to pay when registering tokens into the network. example: 5698 withdrawalDelay: @@ -2707,19 +2707,19 @@ components: 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 diff --git a/db/historydb/historydb.go b/db/historydb/historydb.go index bbc779c..eb6ec49 100644 --- a/db/historydb/historydb.go +++ b/db/historydb/historydb.go @@ -1380,8 +1380,8 @@ func (hdb *HistoryDB) GetAllBucketUpdates() ([]common.BucketUpdate, error) { } // 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 @@ func (hdb *HistoryDB) GetBucketUpdates() ([]common.BucketUpdate, error) { 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 { diff --git a/db/historydb/views.go b/db/historydb/views.go index 9d31982..7baa378 100644 --- a/db/historydb/views.go +++ b/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"` +}