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