hermez-node/db/historydb/nodeinfo.go
db/historydb/nodeinfo.goRaw
1package historydb
2
3import (
4 "time"
5
6 ethCommon "github.com/ethereum/go-ethereum/common"
7 "github.com/hermeznetwork/hermez-node/apitypes"
8 "github.com/hermeznetwork/hermez-node/common"
9 "github.com/hermeznetwork/tracerr"
10 "github.com/russross/meddler"
11)
12
13// Period represents a time period in ethereum
14type Period struct {
15 SlotNum int64 `json:"slotNum"`
16 FromBlock int64 `json:"fromBlock"`
17 ToBlock int64 `json:"toBlock"`
18 FromTimestamp time.Time `json:"fromTimestamp"`
19 ToTimestamp time.Time `json:"toTimestamp"`
20}
21
22// NextForgerAPI represents the next forger exposed via the API
23type NextForgerAPI struct {
24 Coordinator CoordinatorAPI `json:"coordinator"`
25 Period Period `json:"period"`
26}
27
28// NetworkAPI is the network state exposed via the API
29type NetworkAPI struct {
30 LastEthBlock int64 `json:"lastEthereumBlock"`
31 LastSyncBlock int64 `json:"lastSynchedBlock"`
32 LastBatch *BatchAPI `json:"lastBatch"`
33 CurrentSlot int64 `json:"currentSlot"`
34 NextForgers []NextForgerAPI `json:"nextForgers"`
35}
36
37// NodePublicConfig is the configuration of the node that is exposed via API
38type NodePublicConfig struct {
39 // ForgeDelay in seconds
40 ForgeDelay float64 `json:"forgeDelay"`
41}
42
43// StateAPI is an object representing the node and network state exposed via the API
44type StateAPI struct {
45 // NodePublicConfig is the configuration of the node that is exposed via API
46 NodePublicConfig NodePublicConfig `json:"nodeConfig"`
47 Network NetworkAPI `json:"network"`
48 Metrics MetricsAPI `json:"metrics"`
49 Rollup RollupVariablesAPI `json:"rollup"`
50 Auction AuctionVariablesAPI `json:"auction"`
51 WithdrawalDelayer common.WDelayerVariables `json:"withdrawalDelayer"`
52 RecommendedFee common.RecommendedFee `json:"recommendedFee"`
53}
54
55// Constants contains network constants
56type Constants struct {
57 common.SCConsts
58 ChainID uint16
59 HermezAddress ethCommon.Address
60}
61
62// NodeConfig contains the node config exposed in the API
63type NodeConfig struct {
64 MaxPoolTxs uint32
65 MinFeeUSD float64
66 ForgeDelay float64
67}
68
69// NodeInfo contains information about he node used when serving the API
70type NodeInfo struct {
71 ItemID int `meddler:"item_id,pk"`
72 StateAPI *StateAPI `meddler:"state,json"`
73 NodeConfig *NodeConfig `meddler:"config,json"`
74 Constants *Constants `meddler:"constants,json"`
75}
76
77// GetNodeInfo returns the NodeInfo
78func (hdb *HistoryDB) GetNodeInfo() (*NodeInfo, error) {
79 ni := &NodeInfo{}
80 err := meddler.QueryRow(
81 hdb.dbRead, ni, `SELECT * FROM node_info WHERE item_id = 1;`,
82 )
83 return ni, tracerr.Wrap(err)
84}
85
86// GetConstants returns the Constats
87func (hdb *HistoryDB) GetConstants() (*Constants, error) {
88 var nodeInfo NodeInfo
89 err := meddler.QueryRow(
90 hdb.dbRead, &nodeInfo,
91 "SELECT constants FROM node_info WHERE item_id = 1;",
92 )
93 return nodeInfo.Constants, tracerr.Wrap(err)
94}
95
96// SetConstants sets the Constants
97func (hdb *HistoryDB) SetConstants(constants *Constants) error {
98 _constants := struct {
99 Constants *Constants `meddler:"constants,json"`
100 }{constants}
101 values, err := meddler.Default.Values(&_constants, false)
102 if err != nil {
103 return tracerr.Wrap(err)
104 }
105 _, err = hdb.dbWrite.Exec(
106 "UPDATE node_info SET constants = $1 WHERE item_id = 1;",
107 values[0],
108 )
109 return tracerr.Wrap(err)
110}
111
112// GetStateInternalAPI returns the StateAPI
113func (hdb *HistoryDB) GetStateInternalAPI() (*StateAPI, error) {
114 return hdb.getStateAPI(hdb.dbRead)
115}
116
117func (hdb *HistoryDB) getStateAPI(d meddler.DB) (*StateAPI, error) {
118 var nodeInfo NodeInfo
119 err := meddler.QueryRow(
120 d, &nodeInfo,
121 "SELECT state FROM node_info WHERE item_id = 1;",
122 )
123 return nodeInfo.StateAPI, tracerr.Wrap(err)
124}
125
126// SetStateInternalAPI sets the StateAPI
127func (hdb *HistoryDB) SetStateInternalAPI(stateAPI *StateAPI) error {
128 if stateAPI.Network.LastBatch != nil {
129 stateAPI.Network.LastBatch.CollectedFeesAPI =
130 apitypes.NewCollectedFeesAPI(stateAPI.Network.LastBatch.CollectedFeesDB)
131 }
132 _stateAPI := struct {
133 StateAPI *StateAPI `meddler:"state,json"`
134 }{stateAPI}
135 values, err := meddler.Default.Values(&_stateAPI, false)
136 if err != nil {
137 return tracerr.Wrap(err)
138 }
139 _, err = hdb.dbWrite.Exec(
140 "UPDATE node_info SET state = $1 WHERE item_id = 1;",
141 values[0],
142 )
143 return tracerr.Wrap(err)
144}
145
146// GetNodeConfig returns the NodeConfig
147func (hdb *HistoryDB) GetNodeConfig() (*NodeConfig, error) {
148 var nodeInfo NodeInfo
149 err := meddler.QueryRow(
150 hdb.dbRead, &nodeInfo,
151 "SELECT config FROM node_info WHERE item_id = 1;",
152 )
153 return nodeInfo.NodeConfig, tracerr.Wrap(err)
154}
155
156// SetNodeConfig sets the NodeConfig
157func (hdb *HistoryDB) SetNodeConfig(nodeConfig *NodeConfig) error {
158 _nodeConfig := struct {
159 NodeConfig *NodeConfig `meddler:"config,json"`
160 }{nodeConfig}
161 values, err := meddler.Default.Values(&_nodeConfig, false)
162 if err != nil {
163 return tracerr.Wrap(err)
164 }
165 _, err = hdb.dbWrite.Exec(
166 "UPDATE node_info SET config = $1 WHERE item_id = 1;",
167 values[0],
168 )
169 return tracerr.Wrap(err)
170}