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// UpdateNetworkInfoBlock update Status.Network block related information
69func (a *API) UpdateNetworkInfoBlock(
70 lastEthBlock, lastSyncBlock common.Block,
71) {
72 a.status.Network.LastSyncBlock = lastSyncBlock.Num
73 a.status.Network.LastEthBlock = lastEthBlock.Num
74}
75
76// UpdateNetworkInfo update Status.Network information
77func (a *API) UpdateNetworkInfo(
78 lastEthBlock, lastSyncBlock common.Block,
79 lastBatchNum common.BatchNum, currentSlot int64,
80) error {
81 a.status.Network.LastSyncBlock = lastSyncBlock.Num
82 a.status.Network.LastEthBlock = lastEthBlock.Num
83 lastBatch, err := a.h.GetBatchAPI(lastBatchNum)
84 if err != nil {
85 return err
86 }
87 a.status.Network.LastBatch = *lastBatch
88 a.status.Network.CurrentSlot = currentSlot
89 lastClosedSlot := currentSlot + int64(a.status.Auction.ClosedAuctionSlots)
90 nextForgers, err := a.GetNextForgers(lastSyncBlock, currentSlot, lastClosedSlot)
91 if err != nil {
92 return err
93 }
94 a.status.Network.NextForgers = nextForgers
95 return nil
96}
97
98// GetNextForgers returns next forgers
99func (a *API) GetNextForgers(lastBlock common.Block, currentSlot, lastClosedSlot int64) ([]NextForger, error) {
100 secondsPerBlock := int64(15) //nolint:gomnd
101 // currentSlot and lastClosedSlot included
102 limit := uint(lastClosedSlot - currentSlot + 1)
103 bids, _, err := a.h.GetBestBidsAPI(¤tSlot, &lastClosedSlot, nil, &limit, "ASC")
104 if err != nil {
105 return nil, err
106 }
107 nextForgers := []NextForger{}
108 // Create nextForger for each slot
109 for i := currentSlot; i <= lastClosedSlot; i++ {
110 fromBlock := i*int64(a.cg.AuctionConstants.BlocksPerSlot) + a.cg.AuctionConstants.GenesisBlockNum
111 toBlock := (i+1)*int64(a.cg.AuctionConstants.BlocksPerSlot) + a.cg.AuctionConstants.GenesisBlockNum - 1
112 nextForger := NextForger{
113 Period: Period{
114 SlotNum: i,
115 FromBlock: fromBlock,
116 ToBlock: toBlock,
117 FromTimestamp: lastBlock.Timestamp.Add(time.Second * time.Duration(secondsPerBlock*(fromBlock-lastBlock.Num))),
118 ToTimestamp: lastBlock.Timestamp.Add(time.Second * time.Duration(secondsPerBlock*(toBlock-lastBlock.Num))),
119 },
120 }
121 foundBid := false
122 // If there is a bid for a slot, get forger (coordinator)
123 for j := range bids {
124 if bids[j].SlotNum == i {
125 foundBid = true
126 coordinator, err := a.h.GetCoordinatorAPI(bids[j].Bidder)
127 if err != nil {
128 return nil, err
129 }
130 nextForger.Coordinator = *coordinator
131 break
132 }
133 }
134 // If there is no bid, the coordinator that will forge is boot coordinator
135 if !foundBid {
136 nextForger.Coordinator = bootCoordinator
137 }
138 nextForgers = append(nextForgers, nextForger)
139 }
140 return nextForgers, nil
141}
142
143// Metrics
144
145// UpdateMetrics update Status.Metrics information
146func (a *API) UpdateMetrics() error {
147 metrics, err := a.h.GetMetrics(a.status.Network.LastBatch.BatchNum)
148 if err != nil {
149 return err
150 }
151 a.status.Metrics = *metrics
152 return nil
153}
154
155// Recommended fee
156
157// UpdateRecommendedFee update Status.RecommendedFee information
158func (a *API) UpdateRecommendedFee() error {
159 feeExistingAccount, err := a.h.GetAvgTxFee()
160 if err != nil {
161 return err
162 }
163 a.status.RecommendedFee.ExistingAccount = feeExistingAccount
164 a.status.RecommendedFee.CreatesAccount = createAccountExtraFeePercentage * feeExistingAccount
165 a.status.RecommendedFee.CreatesAccountAndRegister = createAccountInternalExtraFeePercentage * feeExistingAccount
166 return nil
167}