arnaucube
arnaucube/hermez-nodePublic Self-hosted repository
hermez-node/api/state.go
api/state.goRaw
  1package api
  2
  3import (
  4	"net/http"
  5	"time"
  6
  7	ethCommon "github.com/ethereum/go-ethereum/common"
  8	"github.com/gin-gonic/gin"
  9	"github.com/hermeznetwork/hermez-node/common"
 10	"github.com/hermeznetwork/hermez-node/db/historydb"
 11)
 12
 13// Network define status of the network
 14type Network struct {
 15	LastBlock   int64              `json:"lastBlock"`
 16	LastBatch   historydb.BatchAPI `json:"lastBatch"`
 17	CurrentSlot int64              `json:"currentSlot"`
 18	NextForgers []NextForger       `json:"nextForgers"`
 19}
 20
 21// NextForger  is a representation of the information of a coordinator and the period will forge
 22type NextForger struct {
 23	Coordinator historydb.CoordinatorAPI `json:"coordinator"`
 24	Period      Period                   `json:"period"`
 25}
 26
 27// Period is a representation of a period
 28type Period struct {
 29	SlotNum       int64     `json:"slotNum"`
 30	FromBlock     int64     `json:"fromBlock"`
 31	ToBlock       int64     `json:"toBlock"`
 32	FromTimestamp time.Time `json:"fromTimestamp"`
 33	ToTimestamp   time.Time `json:"toTimestamp"`
 34}
 35
 36var bootCoordinator historydb.CoordinatorAPI = historydb.CoordinatorAPI{
 37	ItemID: 0,
 38	Bidder: ethCommon.HexToAddress("0x111111111111111111111111111111111111111"),
 39	Forger: ethCommon.HexToAddress("0x111111111111111111111111111111111111111"),
 40	URL:    "https://bootCoordinator",
 41}
 42
 43func (a *API) getState(c *gin.Context) {
 44	// TODO: There are no events for the buckets information, so now this information will be 0
 45	c.JSON(http.StatusOK, a.status)
 46}
 47
 48// SC Vars
 49
 50// SetRollupVariables set Status.Rollup variables
 51func (a *API) SetRollupVariables(rollupVariables common.RollupVariables) {
 52	a.status.Rollup = rollupVariables
 53}
 54
 55// SetWDelayerVariables set Status.WithdrawalDelayer variables
 56func (a *API) SetWDelayerVariables(wDelayerVariables common.WDelayerVariables) {
 57	a.status.WithdrawalDelayer = wDelayerVariables
 58}
 59
 60// SetAuctionVariables set Status.Auction variables
 61func (a *API) SetAuctionVariables(auctionVariables common.AuctionVariables) {
 62	a.status.Auction = auctionVariables
 63}
 64
 65// Network
 66
 67// UpdateNetworkInfo update Status.Network information
 68func (a *API) UpdateNetworkInfo(lastBlock common.Block, lastBatchNum common.BatchNum, currentSlot int64) error {
 69	a.status.Network.LastBlock = lastBlock.EthBlockNum
 70	lastBatch, err := a.h.GetBatchAPI(lastBatchNum)
 71	if err != nil {
 72		return err
 73	}
 74	a.status.Network.LastBatch = *lastBatch
 75	a.status.Network.CurrentSlot = currentSlot
 76	lastClosedSlot := currentSlot + int64(a.status.Auction.ClosedAuctionSlots)
 77	nextForgers, err := a.GetNextForgers(lastBlock, currentSlot, lastClosedSlot)
 78	if err != nil {
 79		return err
 80	}
 81	a.status.Network.NextForgers = nextForgers
 82	return nil
 83}
 84
 85// GetNextForgers returns next forgers
 86func (a *API) GetNextForgers(lastBlock common.Block, currentSlot, lastClosedSlot int64) ([]NextForger, error) {
 87	secondsPerBlock := int64(15) //nolint:gomnd
 88	// currentSlot and lastClosedSlot included
 89	limit := uint(lastClosedSlot - currentSlot + 1)
 90	bids, _, err := a.h.GetBestBidsAPI(&currentSlot, &lastClosedSlot, nil, &limit, "ASC")
 91	if err != nil {
 92		return nil, err
 93	}
 94	nextForgers := []NextForger{}
 95	// Create nextForger for each slot
 96	for i := currentSlot; i <= lastClosedSlot; i++ {
 97		fromBlock := i*int64(a.cg.AuctionConstants.BlocksPerSlot) + a.cg.AuctionConstants.GenesisBlockNum
 98		toBlock := (i+1)*int64(a.cg.AuctionConstants.BlocksPerSlot) + a.cg.AuctionConstants.GenesisBlockNum - 1
 99		nextForger := NextForger{
100			Period: Period{
101				SlotNum:       i,
102				FromBlock:     fromBlock,
103				ToBlock:       toBlock,
104				FromTimestamp: lastBlock.Timestamp.Add(time.Second * time.Duration(secondsPerBlock*(fromBlock-lastBlock.EthBlockNum))),
105				ToTimestamp:   lastBlock.Timestamp.Add(time.Second * time.Duration(secondsPerBlock*(toBlock-lastBlock.EthBlockNum))),
106			},
107		}
108		foundBid := false
109		// If there is a bid for a slot, get forger (coordinator)
110		for j := range bids {
111			if bids[j].SlotNum == i {
112				foundBid = true
113				coordinator, err := a.h.GetCoordinatorAPI(bids[j].Bidder)
114				if err != nil {
115					return nil, err
116				}
117				nextForger.Coordinator = *coordinator
118				break
119			}
120		}
121		// If there is no bid, the coordinator that will forge is boot coordinator
122		if !foundBid {
123			nextForger.Coordinator = bootCoordinator
124		}
125		nextForgers = append(nextForgers, nextForger)
126	}
127	return nextForgers, nil
128}
129
130// Metrics
131
132// UpdateMetrics update Status.Metrics information
133func (a *API) UpdateMetrics() error {
134	metrics, err := a.h.GetMetrics(a.status.Network.LastBatch.BatchNum)
135	if err != nil {
136		return err
137	}
138	a.status.Metrics = *metrics
139	return nil
140}
141
142// Recommended fee
143
144// UpdateRecommendedFee update Status.RecommendedFee information
145func (a *API) UpdateRecommendedFee() error {
146	feeExistingAccount, err := a.h.GetAvgTxFee()
147	if err != nil {
148		return err
149	}
150	a.status.RecommendedFee.ExistingAccount = feeExistingAccount
151	a.status.RecommendedFee.CreatesAccount = createAccountExtraFeePercentage * feeExistingAccount
152	a.status.RecommendedFee.CreatesAccountAndRegister = createAccountInternalExtraFeePercentage * feeExistingAccount
153	return nil
154}