Allow serving API only via new cli commandfeature/update-smart-contracts
@ -1,320 +1,16 @@ |
|||||
package api |
package api |
||||
|
|
||||
import ( |
import ( |
||||
"database/sql" |
|
||||
"fmt" |
|
||||
"math" |
|
||||
"math/big" |
|
||||
"net/http" |
"net/http" |
||||
"time" |
|
||||
|
|
||||
"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/db/historydb" |
|
||||
"github.com/hermeznetwork/tracerr" |
|
||||
) |
) |
||||
|
|
||||
// Network define status of the network
|
|
||||
type Network struct { |
|
||||
LastEthBlock int64 `json:"lastEthereumBlock"` |
|
||||
LastSyncBlock int64 `json:"lastSynchedBlock"` |
|
||||
LastBatch *historydb.BatchAPI `json:"lastBatch"` |
|
||||
CurrentSlot int64 `json:"currentSlot"` |
|
||||
NextForgers []NextForger `json:"nextForgers"` |
|
||||
} |
|
||||
|
|
||||
// NodeConfig is the configuration of the node that is exposed via API
|
|
||||
type NodeConfig struct { |
|
||||
// ForgeDelay in seconds
|
|
||||
ForgeDelay float64 `json:"forgeDelay"` |
|
||||
} |
|
||||
|
|
||||
// NextForger is a representation of the information of a coordinator and the period will forge
|
|
||||
type NextForger struct { |
|
||||
Coordinator historydb.CoordinatorAPI `json:"coordinator"` |
|
||||
Period Period `json:"period"` |
|
||||
} |
|
||||
|
|
||||
// Period is a representation of a period
|
|
||||
type Period struct { |
|
||||
SlotNum int64 `json:"slotNum"` |
|
||||
FromBlock int64 `json:"fromBlock"` |
|
||||
ToBlock int64 `json:"toBlock"` |
|
||||
FromTimestamp time.Time `json:"fromTimestamp"` |
|
||||
ToTimestamp time.Time `json:"toTimestamp"` |
|
||||
} |
|
||||
|
|
||||
func (a *API) getState(c *gin.Context) { |
func (a *API) getState(c *gin.Context) { |
||||
// TODO: There are no events for the buckets information, so now this information will be 0
|
|
||||
a.status.RLock() |
|
||||
status := a.status //nolint
|
|
||||
a.status.RUnlock() |
|
||||
c.JSON(http.StatusOK, status) //nolint
|
|
||||
} |
|
||||
|
|
||||
// SC Vars
|
|
||||
|
|
||||
// SetRollupVariables set Status.Rollup variables
|
|
||||
func (a *API) SetRollupVariables(rollupVariables common.RollupVariables) { |
|
||||
a.status.Lock() |
|
||||
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() |
|
||||
} |
|
||||
|
|
||||
// SetWDelayerVariables set Status.WithdrawalDelayer variables
|
|
||||
func (a *API) SetWDelayerVariables(wDelayerVariables common.WDelayerVariables) { |
|
||||
a.status.Lock() |
|
||||
a.status.WithdrawalDelayer = wDelayerVariables |
|
||||
a.status.Unlock() |
|
||||
} |
|
||||
|
|
||||
// SetAuctionVariables set Status.Auction variables
|
|
||||
func (a *API) SetAuctionVariables(auctionVariables common.AuctionVariables) { |
|
||||
a.status.Lock() |
|
||||
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() |
|
||||
} |
|
||||
|
|
||||
// Network
|
|
||||
|
|
||||
// UpdateNetworkInfoBlock update Status.Network block related information
|
|
||||
func (a *API) UpdateNetworkInfoBlock( |
|
||||
lastEthBlock, lastSyncBlock common.Block, |
|
||||
) { |
|
||||
a.status.Network.LastSyncBlock = lastSyncBlock.Num |
|
||||
a.status.Network.LastEthBlock = lastEthBlock.Num |
|
||||
} |
|
||||
|
|
||||
// UpdateNetworkInfo update Status.Network information
|
|
||||
func (a *API) UpdateNetworkInfo( |
|
||||
lastEthBlock, lastSyncBlock common.Block, |
|
||||
lastBatchNum common.BatchNum, currentSlot int64, |
|
||||
) error { |
|
||||
lastBatch, err := a.h.GetBatchAPI(lastBatchNum) |
|
||||
if tracerr.Unwrap(err) == sql.ErrNoRows { |
|
||||
lastBatch = nil |
|
||||
} else if err != nil { |
|
||||
return tracerr.Wrap(err) |
|
||||
} |
|
||||
lastClosedSlot := currentSlot + int64(a.status.Auction.ClosedAuctionSlots) |
|
||||
nextForgers, err := a.getNextForgers(lastSyncBlock, currentSlot, lastClosedSlot) |
|
||||
if tracerr.Unwrap(err) == sql.ErrNoRows { |
|
||||
nextForgers = nil |
|
||||
} else if err != nil { |
|
||||
return tracerr.Wrap(err) |
|
||||
} |
|
||||
a.status.Lock() |
|
||||
a.status.Network.LastSyncBlock = lastSyncBlock.Num |
|
||||
a.status.Network.LastEthBlock = lastEthBlock.Num |
|
||||
a.status.Network.LastBatch = lastBatch |
|
||||
a.status.Network.CurrentSlot = currentSlot |
|
||||
a.status.Network.NextForgers = nextForgers |
|
||||
|
|
||||
// Update buckets withdrawals
|
|
||||
bucketsUpdate, err := a.h.GetBucketUpdatesAPI() |
|
||||
if tracerr.Unwrap(err) == sql.ErrNoRows { |
|
||||
bucketsUpdate = nil |
|
||||
} else if err != nil { |
|
||||
return tracerr.Wrap(err) |
|
||||
} |
|
||||
|
|
||||
for i, bucketParams := range a.status.Rollup.Buckets { |
|
||||
for _, bucketUpdate := range bucketsUpdate { |
|
||||
if bucketUpdate.NumBucket == i { |
|
||||
bucketParams.Withdrawals = bucketUpdate.Withdrawals |
|
||||
a.status.Rollup.Buckets[i] = bucketParams |
|
||||
break |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
a.status.Unlock() |
|
||||
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
|
|
||||
// currentSlot and lastClosedSlot included
|
|
||||
limit := uint(lastClosedSlot - currentSlot + 1) |
|
||||
bids, _, err := a.h.GetBestBidsAPI(¤tSlot, &lastClosedSlot, nil, &limit, "ASC") |
|
||||
if err != nil && tracerr.Unwrap(err) != sql.ErrNoRows { |
|
||||
return nil, tracerr.Wrap(err) |
|
||||
} |
|
||||
nextForgers := []NextForger{} |
|
||||
// Get min bid info
|
|
||||
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: bigIntSlots, |
|
||||
DefaultSlotSetBidSlotNum: a.status.Auction.DefaultSlotSetBidSlotNum, |
|
||||
}} |
|
||||
} else { |
|
||||
// Get all the relevant updates from the DB
|
|
||||
minBidInfo, err = a.h.GetAuctionVarsUntilSetSlotNumAPI(lastClosedSlot, int(lastClosedSlot-currentSlot)+1) |
|
||||
if err != nil { |
|
||||
return nil, tracerr.Wrap(err) |
|
||||
} |
|
||||
} |
|
||||
// Create nextForger for each slot
|
|
||||
for i := currentSlot; i <= lastClosedSlot; i++ { |
|
||||
fromBlock := i*int64(a.cg.AuctionConstants.BlocksPerSlot) + a.cg.AuctionConstants.GenesisBlockNum |
|
||||
toBlock := (i+1)*int64(a.cg.AuctionConstants.BlocksPerSlot) + a.cg.AuctionConstants.GenesisBlockNum - 1 |
|
||||
nextForger := NextForger{ |
|
||||
Period: Period{ |
|
||||
SlotNum: i, |
|
||||
FromBlock: fromBlock, |
|
||||
ToBlock: toBlock, |
|
||||
FromTimestamp: lastBlock.Timestamp.Add(time.Second * time.Duration(secondsPerBlock*(fromBlock-lastBlock.Num))), |
|
||||
ToTimestamp: lastBlock.Timestamp.Add(time.Second * time.Duration(secondsPerBlock*(toBlock-lastBlock.Num))), |
|
||||
}, |
|
||||
} |
|
||||
foundForger := false |
|
||||
// If there is a bid for a slot, get forger (coordinator)
|
|
||||
for j := range bids { |
|
||||
slotNum := bids[j].SlotNum |
|
||||
if slotNum == i { |
|
||||
// There's a bid for the slot
|
|
||||
// Check if the bid is greater than the minimum required
|
|
||||
for i := 0; i < len(minBidInfo); i++ { |
|
||||
// Find the most recent update
|
|
||||
if slotNum >= minBidInfo[i].DefaultSlotSetBidSlotNum { |
|
||||
// Get min bid
|
|
||||
minBidSelector := slotNum % int64(len(a.status.Auction.DefaultSlotSetBid)) |
|
||||
minBid := minBidInfo[i].DefaultSlotSetBid[minBidSelector] |
|
||||
// Check if the bid has beaten the minimum
|
|
||||
bid, ok := new(big.Int).SetString(string(bids[j].BidValue), 10) |
|
||||
if !ok { |
|
||||
return nil, tracerr.New("Wrong bid value, error parsing it as big.Int") |
|
||||
} |
|
||||
if minBid.Cmp(bid) == 1 { |
|
||||
// Min bid is greater than bid, the slot will be forged by boot coordinator
|
|
||||
break |
|
||||
} |
|
||||
foundForger = true |
|
||||
break |
|
||||
} |
|
||||
} |
|
||||
if !foundForger { // There is no bid or it's smaller than the minimum
|
|
||||
break |
|
||||
} |
|
||||
coordinator, err := a.h.GetCoordinatorAPI(bids[j].Bidder) |
|
||||
if err != nil { |
|
||||
return nil, tracerr.Wrap(err) |
|
||||
} |
|
||||
nextForger.Coordinator = *coordinator |
|
||||
break |
|
||||
} |
|
||||
} |
|
||||
// If there is no bid, the coordinator that will forge is boot coordinator
|
|
||||
if !foundForger { |
|
||||
nextForger.Coordinator = historydb.CoordinatorAPI{ |
|
||||
Forger: a.status.Auction.BootCoordinator, |
|
||||
URL: a.status.Auction.BootCoordinatorURL, |
|
||||
} |
|
||||
} |
|
||||
nextForgers = append(nextForgers, nextForger) |
|
||||
} |
|
||||
return nextForgers, nil |
|
||||
} |
|
||||
|
|
||||
// Metrics
|
|
||||
|
|
||||
// UpdateMetrics update Status.Metrics information
|
|
||||
func (a *API) UpdateMetrics() error { |
|
||||
a.status.RLock() |
|
||||
if a.status.Network.LastBatch == nil { |
|
||||
a.status.RUnlock() |
|
||||
return nil |
|
||||
} |
|
||||
batchNum := a.status.Network.LastBatch.BatchNum |
|
||||
a.status.RUnlock() |
|
||||
metrics, err := a.h.GetMetricsAPI(batchNum) |
|
||||
|
stateAPI, err := a.h.GetStateAPI() |
||||
if err != nil { |
if err != nil { |
||||
return tracerr.Wrap(err) |
|
||||
} |
|
||||
a.status.Lock() |
|
||||
a.status.Metrics = *metrics |
|
||||
a.status.Unlock() |
|
||||
return nil |
|
||||
} |
|
||||
|
|
||||
// Recommended fee
|
|
||||
|
|
||||
// UpdateRecommendedFee update Status.RecommendedFee information
|
|
||||
func (a *API) UpdateRecommendedFee() error { |
|
||||
feeExistingAccount, err := a.h.GetAvgTxFeeAPI() |
|
||||
if err != nil { |
|
||||
return tracerr.Wrap(err) |
|
||||
} |
|
||||
var minFeeUSD float64 |
|
||||
if a.l2 != nil { |
|
||||
minFeeUSD = a.l2.MinFeeUSD() |
|
||||
|
retBadReq(err, c) |
||||
|
return |
||||
} |
} |
||||
a.status.Lock() |
|
||||
a.status.RecommendedFee.ExistingAccount = |
|
||||
math.Max(feeExistingAccount, minFeeUSD) |
|
||||
a.status.RecommendedFee.CreatesAccount = |
|
||||
math.Max(createAccountExtraFeePercentage*feeExistingAccount, minFeeUSD) |
|
||||
a.status.RecommendedFee.CreatesAccountAndRegister = |
|
||||
math.Max(createAccountInternalExtraFeePercentage*feeExistingAccount, minFeeUSD) |
|
||||
a.status.Unlock() |
|
||||
return nil |
|
||||
|
c.JSON(http.StatusOK, stateAPI) |
||||
} |
} |
@ -0,0 +1,24 @@ |
|||||
|
[API] |
||||
|
Address = "localhost:8386" |
||||
|
Explorer = true |
||||
|
MaxSQLConnections = 10 |
||||
|
SQLConnectionTimeout = "2s" |
||||
|
|
||||
|
[PostgreSQL] |
||||
|
PortWrite = 5432 |
||||
|
HostWrite = "localhost" |
||||
|
UserWrite = "hermez" |
||||
|
PasswordWrite = "yourpasswordhere" |
||||
|
NameWrite = "hermez" |
||||
|
|
||||
|
[Coordinator.L2DB] |
||||
|
SafetyPeriod = 10 |
||||
|
MaxTxs = 512 |
||||
|
TTL = "24h" |
||||
|
PurgeBatchDelay = 10 |
||||
|
InvalidateBatchDelay = 20 |
||||
|
PurgeBlockDelay = 10 |
||||
|
InvalidateBlockDelay = 20 |
||||
|
|
||||
|
[Coordinator.API] |
||||
|
Coordinator = true |
@ -0,0 +1,33 @@ |
|||||
|
package common |
||||
|
|
||||
|
// SCVariables joins all the smart contract variables in a single struct
|
||||
|
type SCVariables struct { |
||||
|
Rollup RollupVariables `validate:"required"` |
||||
|
Auction AuctionVariables `validate:"required"` |
||||
|
WDelayer WDelayerVariables `validate:"required"` |
||||
|
} |
||||
|
|
||||
|
// AsPtr returns the SCVariables as a SCVariablesPtr using pointers to the
|
||||
|
// original SCVariables
|
||||
|
func (v *SCVariables) AsPtr() *SCVariablesPtr { |
||||
|
return &SCVariablesPtr{ |
||||
|
Rollup: &v.Rollup, |
||||
|
Auction: &v.Auction, |
||||
|
WDelayer: &v.WDelayer, |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// SCVariablesPtr joins all the smart contract variables as pointers in a single
|
||||
|
// struct
|
||||
|
type SCVariablesPtr struct { |
||||
|
Rollup *RollupVariables `validate:"required"` |
||||
|
Auction *AuctionVariables `validate:"required"` |
||||
|
WDelayer *WDelayerVariables `validate:"required"` |
||||
|
} |
||||
|
|
||||
|
// SCConsts joins all the smart contract constants in a single struct
|
||||
|
type SCConsts struct { |
||||
|
Rollup RollupConstants |
||||
|
Auction AuctionConstants |
||||
|
WDelayer WDelayerConstants |
||||
|
} |
@ -0,0 +1,165 @@ |
|||||
|
package historydb |
||||
|
|
||||
|
import ( |
||||
|
"time" |
||||
|
|
||||
|
ethCommon "github.com/ethereum/go-ethereum/common" |
||||
|
"github.com/hermeznetwork/hermez-node/common" |
||||
|
"github.com/hermeznetwork/tracerr" |
||||
|
"github.com/russross/meddler" |
||||
|
) |
||||
|
|
||||
|
// Period represents a time period in ethereum
|
||||
|
type Period struct { |
||||
|
SlotNum int64 `json:"slotNum"` |
||||
|
FromBlock int64 `json:"fromBlock"` |
||||
|
ToBlock int64 `json:"toBlock"` |
||||
|
FromTimestamp time.Time `json:"fromTimestamp"` |
||||
|
ToTimestamp time.Time `json:"toTimestamp"` |
||||
|
} |
||||
|
|
||||
|
// NextForgerAPI represents the next forger exposed via the API
|
||||
|
type NextForgerAPI struct { |
||||
|
Coordinator CoordinatorAPI `json:"coordinator"` |
||||
|
Period Period `json:"period"` |
||||
|
} |
||||
|
|
||||
|
// NetworkAPI is the network state exposed via the API
|
||||
|
type NetworkAPI struct { |
||||
|
LastEthBlock int64 `json:"lastEthereumBlock"` |
||||
|
LastSyncBlock int64 `json:"lastSynchedBlock"` |
||||
|
LastBatch *BatchAPI `json:"lastBatch"` |
||||
|
CurrentSlot int64 `json:"currentSlot"` |
||||
|
NextForgers []NextForgerAPI `json:"nextForgers"` |
||||
|
} |
||||
|
|
||||
|
// NodePublicConfig is the configuration of the node that is exposed via API
|
||||
|
type NodePublicConfig struct { |
||||
|
// ForgeDelay in seconds
|
||||
|
ForgeDelay float64 `json:"forgeDelay"` |
||||
|
} |
||||
|
|
||||
|
// StateAPI is an object representing the node and network state exposed via the API
|
||||
|
type StateAPI struct { |
||||
|
// NodePublicConfig is the configuration of the node that is exposed via API
|
||||
|
NodePublicConfig NodePublicConfig `json:"nodeConfig"` |
||||
|
Network NetworkAPI `json:"network"` |
||||
|
Metrics MetricsAPI `json:"metrics"` |
||||
|
Rollup RollupVariablesAPI `json:"rollup"` |
||||
|
Auction AuctionVariablesAPI `json:"auction"` |
||||
|
WithdrawalDelayer common.WDelayerVariables `json:"withdrawalDelayer"` |
||||
|
RecommendedFee common.RecommendedFee `json:"recommendedFee"` |
||||
|
} |
||||
|
|
||||
|
// Constants contains network constants
|
||||
|
type Constants struct { |
||||
|
common.SCConsts |
||||
|
ChainID uint16 |
||||
|
HermezAddress ethCommon.Address |
||||
|
} |
||||
|
|
||||
|
// NodeConfig contains the node config exposed in the API
|
||||
|
type NodeConfig struct { |
||||
|
MaxPoolTxs uint32 |
||||
|
MinFeeUSD float64 |
||||
|
ForgeDelay float64 |
||||
|
} |
||||
|
|
||||
|
// NodeInfo contains information about he node used when serving the API
|
||||
|
type NodeInfo struct { |
||||
|
ItemID int `meddler:"item_id,pk"` |
||||
|
StateAPI *StateAPI `meddler:"state,json"` |
||||
|
NodeConfig *NodeConfig `meddler:"config,json"` |
||||
|
Constants *Constants `meddler:"constants,json"` |
||||
|
} |
||||
|
|
||||
|
// GetNodeInfo returns the NodeInfo
|
||||
|
func (hdb *HistoryDB) GetNodeInfo() (*NodeInfo, error) { |
||||
|
ni := &NodeInfo{} |
||||
|
err := meddler.QueryRow( |
||||
|
hdb.dbRead, ni, `SELECT * FROM node_info WHERE item_id = 1;`, |
||||
|
) |
||||
|
return ni, tracerr.Wrap(err) |
||||
|
} |
||||
|
|
||||
|
// GetConstants returns the Constats
|
||||
|
func (hdb *HistoryDB) GetConstants() (*Constants, error) { |
||||
|
var nodeInfo NodeInfo |
||||
|
err := meddler.QueryRow( |
||||
|
hdb.dbRead, &nodeInfo, |
||||
|
"SELECT constants FROM node_info WHERE item_id = 1;", |
||||
|
) |
||||
|
return nodeInfo.Constants, tracerr.Wrap(err) |
||||
|
} |
||||
|
|
||||
|
// SetConstants sets the Constants
|
||||
|
func (hdb *HistoryDB) SetConstants(constants *Constants) error { |
||||
|
_constants := struct { |
||||
|
Constants *Constants `meddler:"constants,json"` |
||||
|
}{constants} |
||||
|
values, err := meddler.Default.Values(&_constants, false) |
||||
|
if err != nil { |
||||
|
return tracerr.Wrap(err) |
||||
|
} |
||||
|
_, err = hdb.dbWrite.Exec( |
||||
|
"UPDATE node_info SET constants = $1 WHERE item_id = 1;", |
||||
|
values[0], |
||||
|
) |
||||
|
return tracerr.Wrap(err) |
||||
|
} |
||||
|
|
||||
|
// GetStateInternalAPI returns the StateAPI
|
||||
|
func (hdb *HistoryDB) GetStateInternalAPI() (*StateAPI, error) { |
||||
|
return hdb.getStateAPI(hdb.dbRead) |
||||
|
} |
||||
|
|
||||
|
func (hdb *HistoryDB) getStateAPI(d meddler.DB) (*StateAPI, error) { |
||||
|
var nodeInfo NodeInfo |
||||
|
err := meddler.QueryRow( |
||||
|
d, &nodeInfo, |
||||
|
"SELECT state FROM node_info WHERE item_id = 1;", |
||||
|
) |
||||
|
return nodeInfo.StateAPI, tracerr.Wrap(err) |
||||
|
} |
||||
|
|
||||
|
// SetStateInternalAPI sets the StateAPI
|
||||
|
func (hdb *HistoryDB) SetStateInternalAPI(stateAPI *StateAPI) error { |
||||
|
_stateAPI := struct { |
||||
|
StateAPI *StateAPI `meddler:"state,json"` |
||||
|
}{stateAPI} |
||||
|
values, err := meddler.Default.Values(&_stateAPI, false) |
||||
|
if err != nil { |
||||
|
return tracerr.Wrap(err) |
||||
|
} |
||||
|
_, err = hdb.dbWrite.Exec( |
||||
|
"UPDATE node_info SET state = $1 WHERE item_id = 1;", |
||||
|
values[0], |
||||
|
) |
||||
|
return tracerr.Wrap(err) |
||||
|
} |
||||
|
|
||||
|
// GetNodeConfig returns the NodeConfig
|
||||
|
func (hdb *HistoryDB) GetNodeConfig() (*NodeConfig, error) { |
||||
|
var nodeInfo NodeInfo |
||||
|
err := meddler.QueryRow( |
||||
|
hdb.dbRead, &nodeInfo, |
||||
|
"SELECT config FROM node_info WHERE item_id = 1;", |
||||
|
) |
||||
|
return nodeInfo.NodeConfig, tracerr.Wrap(err) |
||||
|
} |
||||
|
|
||||
|
// SetNodeConfig sets the NodeConfig
|
||||
|
func (hdb *HistoryDB) SetNodeConfig(nodeConfig *NodeConfig) error { |
||||
|
_nodeConfig := struct { |
||||
|
NodeConfig *NodeConfig `meddler:"config,json"` |
||||
|
}{nodeConfig} |
||||
|
values, err := meddler.Default.Values(&_nodeConfig, false) |
||||
|
if err != nil { |
||||
|
return tracerr.Wrap(err) |
||||
|
} |
||||
|
_, err = hdb.dbWrite.Exec( |
||||
|
"UPDATE node_info SET config = $1 WHERE item_id = 1;", |
||||
|
values[0], |
||||
|
) |
||||
|
return tracerr.Wrap(err) |
||||
|
} |
@ -0,0 +1,155 @@ |
|||||
|
package stateapiupdater |
||||
|
|
||||
|
import ( |
||||
|
"database/sql" |
||||
|
"sync" |
||||
|
|
||||
|
"github.com/hermeznetwork/hermez-node/common" |
||||
|
"github.com/hermeznetwork/hermez-node/db/historydb" |
||||
|
"github.com/hermeznetwork/tracerr" |
||||
|
) |
||||
|
|
||||
|
// Updater is an utility object to facilitate updating the StateAPI
|
||||
|
type Updater struct { |
||||
|
hdb *historydb.HistoryDB |
||||
|
state historydb.StateAPI |
||||
|
config historydb.NodeConfig |
||||
|
vars common.SCVariablesPtr |
||||
|
consts historydb.Constants |
||||
|
rw sync.RWMutex |
||||
|
} |
||||
|
|
||||
|
// NewUpdater creates a new Updater
|
||||
|
func NewUpdater(hdb *historydb.HistoryDB, config *historydb.NodeConfig, vars *common.SCVariables, |
||||
|
consts *historydb.Constants) *Updater { |
||||
|
u := Updater{ |
||||
|
hdb: hdb, |
||||
|
config: *config, |
||||
|
consts: *consts, |
||||
|
state: historydb.StateAPI{ |
||||
|
NodePublicConfig: historydb.NodePublicConfig{ |
||||
|
ForgeDelay: config.ForgeDelay, |
||||
|
}, |
||||
|
}, |
||||
|
} |
||||
|
u.SetSCVars(vars.AsPtr()) |
||||
|
return &u |
||||
|
} |
||||
|
|
||||
|
// Store the State in the HistoryDB
|
||||
|
func (u *Updater) Store() error { |
||||
|
u.rw.RLock() |
||||
|
defer u.rw.RUnlock() |
||||
|
return tracerr.Wrap(u.hdb.SetStateInternalAPI(&u.state)) |
||||
|
} |
||||
|
|
||||
|
// SetSCVars sets the smart contract vars (ony updates those that are not nil)
|
||||
|
func (u *Updater) SetSCVars(vars *common.SCVariablesPtr) { |
||||
|
u.rw.Lock() |
||||
|
defer u.rw.Unlock() |
||||
|
if vars.Rollup != nil { |
||||
|
u.vars.Rollup = vars.Rollup |
||||
|
rollupVars := historydb.NewRollupVariablesAPI(u.vars.Rollup) |
||||
|
u.state.Rollup = *rollupVars |
||||
|
} |
||||
|
if vars.Auction != nil { |
||||
|
u.vars.Auction = vars.Auction |
||||
|
auctionVars := historydb.NewAuctionVariablesAPI(u.vars.Auction) |
||||
|
u.state.Auction = *auctionVars |
||||
|
} |
||||
|
if vars.WDelayer != nil { |
||||
|
u.vars.WDelayer = vars.WDelayer |
||||
|
u.state.WithdrawalDelayer = *u.vars.WDelayer |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// UpdateRecommendedFee update Status.RecommendedFee information
|
||||
|
func (u *Updater) UpdateRecommendedFee() error { |
||||
|
recommendedFee, err := u.hdb.GetRecommendedFee(u.config.MinFeeUSD) |
||||
|
if err != nil { |
||||
|
return tracerr.Wrap(err) |
||||
|
} |
||||
|
u.rw.Lock() |
||||
|
u.state.RecommendedFee = *recommendedFee |
||||
|
u.rw.Unlock() |
||||
|
return nil |
||||
|
} |
||||
|
|
||||
|
// UpdateMetrics update Status.Metrics information
|
||||
|
func (u *Updater) UpdateMetrics() error { |
||||
|
u.rw.RLock() |
||||
|
lastBatch := u.state.Network.LastBatch |
||||
|
u.rw.RUnlock() |
||||
|
if lastBatch == nil { |
||||
|
return nil |
||||
|
} |
||||
|
lastBatchNum := lastBatch.BatchNum |
||||
|
metrics, err := u.hdb.GetMetricsInternalAPI(lastBatchNum) |
||||
|
if err != nil { |
||||
|
return tracerr.Wrap(err) |
||||
|
} |
||||
|
u.rw.Lock() |
||||
|
u.state.Metrics = *metrics |
||||
|
u.rw.Unlock() |
||||
|
return nil |
||||
|
} |
||||
|
|
||||
|
// UpdateNetworkInfoBlock update Status.Network block related information
|
||||
|
func (u *Updater) UpdateNetworkInfoBlock(lastEthBlock, lastSyncBlock common.Block) { |
||||
|
u.rw.Lock() |
||||
|
u.state.Network.LastSyncBlock = lastSyncBlock.Num |
||||
|
u.state.Network.LastEthBlock = lastEthBlock.Num |
||||
|
u.rw.Unlock() |
||||
|
} |
||||
|
|
||||
|
// UpdateNetworkInfo update Status.Network information
|
||||
|
func (u *Updater) UpdateNetworkInfo( |
||||
|
lastEthBlock, lastSyncBlock common.Block, |
||||
|
lastBatchNum common.BatchNum, currentSlot int64, |
||||
|
) error { |
||||
|
// Get last batch in API format
|
||||
|
lastBatch, err := u.hdb.GetBatchInternalAPI(lastBatchNum) |
||||
|
if tracerr.Unwrap(err) == sql.ErrNoRows { |
||||
|
lastBatch = nil |
||||
|
} else if err != nil { |
||||
|
return tracerr.Wrap(err) |
||||
|
} |
||||
|
u.rw.RLock() |
||||
|
auctionVars := u.vars.Auction |
||||
|
u.rw.RUnlock() |
||||
|
// Get next forgers
|
||||
|
lastClosedSlot := currentSlot + int64(auctionVars.ClosedAuctionSlots) |
||||
|
nextForgers, err := u.hdb.GetNextForgersInternalAPI(auctionVars, &u.consts.Auction, |
||||
|
lastSyncBlock, currentSlot, lastClosedSlot) |
||||
|
if tracerr.Unwrap(err) == sql.ErrNoRows { |
||||
|
nextForgers = nil |
||||
|
} else if err != nil { |
||||
|
return tracerr.Wrap(err) |
||||
|
} |
||||
|
|
||||
|
bucketUpdates, err := u.hdb.GetBucketUpdatesInternalAPI() |
||||
|
if err == sql.ErrNoRows { |
||||
|
bucketUpdates = nil |
||||
|
} else if err != nil { |
||||
|
return tracerr.Wrap(err) |
||||
|
} |
||||
|
|
||||
|
u.rw.Lock() |
||||
|
// Update NodeInfo struct
|
||||
|
for i, bucketParams := range u.state.Rollup.Buckets { |
||||
|
for _, bucketUpdate := range bucketUpdates { |
||||
|
if bucketUpdate.NumBucket == i { |
||||
|
bucketParams.Withdrawals = bucketUpdate.Withdrawals |
||||
|
u.state.Rollup.Buckets[i] = bucketParams |
||||
|
break |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
u.state.Network.LastSyncBlock = lastSyncBlock.Num |
||||
|
u.state.Network.LastEthBlock = lastEthBlock.Num |
||||
|
u.state.Network.LastBatch = lastBatch |
||||
|
u.state.Network.CurrentSlot = currentSlot |
||||
|
u.state.Network.NextForgers = nextForgers |
||||
|
u.rw.Unlock() |
||||
|
return nil |
||||
|
} |