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
 24	Period      Period
 25}
 26
 27// Period is a representation of a period
 28type Period struct {
 29	SlotNum       int64
 30	FromBlock     int64
 31	ToBlock       int64
 32	FromTimestamp time.Time
 33	ToTimestamp   time.Time
 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	c.JSON(http.StatusOK, a.status)
 45}
 46
 47// SC Vars
 48
 49// SetRollupVariables set Status.Rollup variables
 50func (a *API) SetRollupVariables(rollupVariables common.RollupVariables) {
 51	a.status.Rollup = rollupVariables
 52}
 53
 54// SetWDelayerVariables set Status.WithdrawalDelayer variables
 55func (a *API) SetWDelayerVariables(wDelayerVariables common.WDelayerVariables) {
 56	a.status.WithdrawalDelayer = wDelayerVariables
 57}
 58
 59// SetAuctionVariables set Status.Auction variables
 60func (a *API) SetAuctionVariables(auctionVariables common.AuctionVariables) {
 61	a.status.Auction = auctionVariables
 62}
 63
 64// Network
 65
 66// UpdateNetworkInfo update Status.Network information
 67func (a *API) UpdateNetworkInfo(lastBlock common.Block, lastBatchNum common.BatchNum, currentSlot int64) error {
 68	a.status.Network.LastBlock = lastBlock.EthBlockNum
 69	lastBatch, err := a.h.GetBatchAPI(lastBatchNum)
 70	if err != nil {
 71		return err
 72	}
 73	a.status.Network.LastBatch = *lastBatch
 74	a.status.Network.CurrentSlot = currentSlot
 75	lastClosedSlot := currentSlot + int64(a.status.Auction.ClosedAuctionSlots)
 76	nextForgers, err := a.GetNextForgers(lastBlock, currentSlot, lastClosedSlot)
 77	if err != nil {
 78		return err
 79	}
 80	a.status.Network.NextForgers = nextForgers
 81	return nil
 82}
 83
 84// GetNextForgers returns next forgers
 85func (a *API) GetNextForgers(lastBlock common.Block, currentSlot, lastClosedSlot int64) ([]NextForger, error) {
 86	secondsPerBlock := int64(15) //nolint:gomnd
 87	// currentSlot and lastClosedSlot included
 88	limit := uint(lastClosedSlot - currentSlot + 1)
 89	bids, _, err := a.h.GetBestBidsAPI(&currentSlot, &lastClosedSlot, nil, &limit, "ASC")
 90	if err != nil {
 91		return nil, err
 92	}
 93	nextForgers := []NextForger{}
 94	// Create nextForger for each slot
 95	for i := currentSlot; i <= lastClosedSlot; i++ {
 96		fromBlock := i*int64(a.cg.AuctionConstants.BlocksPerSlot) + a.cg.AuctionConstants.GenesisBlockNum
 97		toBlock := (i+1)*int64(a.cg.AuctionConstants.BlocksPerSlot) + a.cg.AuctionConstants.GenesisBlockNum - 1
 98		nextForger := NextForger{
 99			Period: Period{
100				SlotNum:       i,
101				FromBlock:     fromBlock,
102				ToBlock:       toBlock,
103				FromTimestamp: lastBlock.Timestamp.Add(time.Second * time.Duration(secondsPerBlock*(fromBlock-lastBlock.EthBlockNum))),
104				ToTimestamp:   lastBlock.Timestamp.Add(time.Second * time.Duration(secondsPerBlock*(toBlock-lastBlock.EthBlockNum))),
105			},
106		}
107		foundBid := false
108		// If there is a bid for a slot, get forger (coordinator)
109		for j := range bids {
110			if bids[j].SlotNum == i {
111				foundBid = true
112				coordinator, err := a.h.GetCoordinatorAPI(bids[j].Bidder)
113				if err != nil {
114					return nil, err
115				}
116				nextForger.Coordinator = *coordinator
117				break
118			}
119		}
120		// If there is no bid, the coordinator that will forge is boot coordinator
121		if !foundBid {
122			nextForger.Coordinator = bootCoordinator
123		}
124		nextForgers = append(nextForgers, nextForger)
125	}
126	return nextForgers, nil
127}
128
129// Metrics
130
131// UpdateMetrics update Status.Metrics information
132func (a *API) UpdateMetrics() error {
133	metrics, err := a.h.GetMetrics()
134	if err != nil {
135		return err
136	}
137	a.status.Metrics = metrics
138	return nil
139}
140
141// Recommended fee
142
143// UpdateRecommendedFee update Status.RecommendedFee information
144func (a *API) UpdateRecommendedFee() error {
145	return nil
146}