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 a.status.RLock()
47 status := a.status //nolint
48 a.status.RUnlock()
49 c.JSON(http.StatusOK, status) //nolint
50}
51
52// SC Vars
53
54// SetRollupVariables set Status.Rollup variables
55func (a *API) SetRollupVariables(rollupVariables common.RollupVariables) {
56 a.status.Lock()
57 a.status.Rollup = rollupVariables
58 a.status.Unlock()
59}
60
61// SetWDelayerVariables set Status.WithdrawalDelayer variables
62func (a *API) SetWDelayerVariables(wDelayerVariables common.WDelayerVariables) {
63 a.status.Lock()
64 a.status.WithdrawalDelayer = wDelayerVariables
65 a.status.Unlock()
66}
67
68// SetAuctionVariables set Status.Auction variables
69func (a *API) SetAuctionVariables(auctionVariables common.AuctionVariables) {
70 a.status.Lock()
71 a.status.Auction = auctionVariables
72 a.status.Unlock()
73}
74
75// Network
76
77// UpdateNetworkInfoBlock update Status.Network block related information
78func (a *API) UpdateNetworkInfoBlock(
79 lastEthBlock, lastSyncBlock common.Block,
80) {
81 a.status.Network.LastSyncBlock = lastSyncBlock.Num
82 a.status.Network.LastEthBlock = lastEthBlock.Num
83}
84
85// UpdateNetworkInfo update Status.Network information
86func (a *API) UpdateNetworkInfo(
87 lastEthBlock, lastSyncBlock common.Block,
88 lastBatchNum common.BatchNum, currentSlot int64,
89) error {
90 lastBatch, err := a.h.GetBatchAPI(lastBatchNum)
91 if err != nil {
92 return err
93 }
94 lastClosedSlot := currentSlot + int64(a.status.Auction.ClosedAuctionSlots)
95 nextForgers, err := a.getNextForgers(lastSyncBlock, currentSlot, lastClosedSlot)
96 if err != nil {
97 return err
98 }
99 a.status.Lock()
100 a.status.Network.LastSyncBlock = lastSyncBlock.Num
101 a.status.Network.LastEthBlock = lastEthBlock.Num
102 a.status.Network.LastBatch = *lastBatch
103 a.status.Network.CurrentSlot = currentSlot
104 a.status.Network.NextForgers = nextForgers
105 a.status.Unlock()
106 return nil
107}
108
109// getNextForgers returns next forgers
110func (a *API) getNextForgers(lastBlock common.Block, currentSlot, lastClosedSlot int64) ([]NextForger, error) {
111 secondsPerBlock := int64(15) //nolint:gomnd
112 // currentSlot and lastClosedSlot included
113 limit := uint(lastClosedSlot - currentSlot + 1)
114 bids, _, err := a.h.GetBestBidsAPI(¤tSlot, &lastClosedSlot, nil, &limit, "ASC")
115 if err != nil {
116 return nil, err
117 }
118 nextForgers := []NextForger{}
119 // Create nextForger for each slot
120 for i := currentSlot; i <= lastClosedSlot; i++ {
121 fromBlock := i*int64(a.cg.AuctionConstants.BlocksPerSlot) + a.cg.AuctionConstants.GenesisBlockNum
122 toBlock := (i+1)*int64(a.cg.AuctionConstants.BlocksPerSlot) + a.cg.AuctionConstants.GenesisBlockNum - 1
123 nextForger := NextForger{
124 Period: Period{
125 SlotNum: i,
126 FromBlock: fromBlock,
127 ToBlock: toBlock,
128 FromTimestamp: lastBlock.Timestamp.Add(time.Second * time.Duration(secondsPerBlock*(fromBlock-lastBlock.Num))),
129 ToTimestamp: lastBlock.Timestamp.Add(time.Second * time.Duration(secondsPerBlock*(toBlock-lastBlock.Num))),
130 },
131 }
132 foundBid := false
133 // If there is a bid for a slot, get forger (coordinator)
134 for j := range bids {
135 if bids[j].SlotNum == i {
136 foundBid = true
137 coordinator, err := a.h.GetCoordinatorAPI(bids[j].Bidder)
138 if err != nil {
139 return nil, err
140 }
141 nextForger.Coordinator = *coordinator
142 break
143 }
144 }
145 // If there is no bid, the coordinator that will forge is boot coordinator
146 if !foundBid {
147 nextForger.Coordinator = bootCoordinator
148 }
149 nextForgers = append(nextForgers, nextForger)
150 }
151 return nextForgers, nil
152}
153
154// Metrics
155
156// UpdateMetrics update Status.Metrics information
157func (a *API) UpdateMetrics() error {
158 a.status.RLock()
159 batchNum := a.status.Network.LastBatch.BatchNum
160 a.status.RUnlock()
161 metrics, err := a.h.GetMetrics(batchNum)
162 if err != nil {
163 return err
164 }
165 a.status.Lock()
166 a.status.Metrics = *metrics
167 a.status.Unlock()
168 return nil
169}
170
171// Recommended fee
172
173// UpdateRecommendedFee update Status.RecommendedFee information
174func (a *API) UpdateRecommendedFee() error {
175 feeExistingAccount, err := a.h.GetAvgTxFee()
176 if err != nil {
177 return err
178 }
179 a.status.Lock()
180 a.status.RecommendedFee.ExistingAccount = feeExistingAccount
181 a.status.RecommendedFee.CreatesAccount = createAccountExtraFeePercentage * feeExistingAccount
182 a.status.RecommendedFee.CreatesAccountAndRegister = createAccountInternalExtraFeePercentage * feeExistingAccount
183 a.status.Unlock()
184 return nil
185}