arnaucube
arnaucube/hermez-nodePublic Self-hosted repository
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/api/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	PendingL1Txs  int             `json:"pendingL1Transactions"`
 36}
 37
 38// NodePublicInfo is the configuration and metrics of the node that is exposed via API
 39type NodePublicInfo struct {
 40	// ForgeDelay in seconds
 41	ForgeDelay float64 `json:"forgeDelay"`
 42	// PoolLoad amount of transactions in the pool
 43	PoolLoad int64 `json:"poolLoad"`
 44}
 45
 46// StateAPI is an object representing the node and network state exposed via the API
 47type StateAPI struct {
 48	NodePublicInfo    NodePublicInfo           `json:"node"`
 49	Network           NetworkAPI               `json:"network"`
 50	Metrics           MetricsAPI               `json:"metrics"`
 51	Rollup            RollupVariablesAPI       `json:"rollup"`
 52	Auction           AuctionVariablesAPI      `json:"auction"`
 53	WithdrawalDelayer common.WDelayerVariables `json:"withdrawalDelayer"`
 54	RecommendedFee    common.RecommendedFee    `json:"recommendedFee"`
 55}
 56
 57// Constants contains network constants
 58type Constants struct {
 59	common.SCConsts
 60	ChainID       uint16
 61	HermezAddress ethCommon.Address
 62}
 63
 64// NodeConfig contains the node config exposed in the API
 65type NodeConfig struct {
 66	MaxPoolTxs uint32
 67	MinFeeUSD  float64
 68	MaxFeeUSD  float64
 69	ForgeDelay float64
 70}
 71
 72// NodeInfo contains information about he node used when serving the API
 73type NodeInfo struct {
 74	ItemID     int         `meddler:"item_id,pk"`
 75	StateAPI   *StateAPI   `meddler:"state,json"`
 76	NodeConfig *NodeConfig `meddler:"config,json"`
 77	Constants  *Constants  `meddler:"constants,json"`
 78}
 79
 80// GetNodeInfo returns the NodeInfo
 81func (hdb *HistoryDB) GetNodeInfo() (*NodeInfo, error) {
 82	ni := &NodeInfo{}
 83	err := meddler.QueryRow(
 84		hdb.dbRead, ni, `SELECT * FROM node_info WHERE item_id = 1;`,
 85	)
 86	return ni, tracerr.Wrap(err)
 87}
 88
 89// GetConstants returns the Constats
 90func (hdb *HistoryDB) GetConstants() (*Constants, error) {
 91	var nodeInfo NodeInfo
 92	err := meddler.QueryRow(
 93		hdb.dbRead, &nodeInfo,
 94		"SELECT constants FROM node_info WHERE item_id = 1;",
 95	)
 96	return nodeInfo.Constants, tracerr.Wrap(err)
 97}
 98
 99// SetConstants sets the Constants
100func (hdb *HistoryDB) SetConstants(constants *Constants) error {
101	_constants := struct {
102		Constants *Constants `meddler:"constants,json"`
103	}{constants}
104	values, err := meddler.Default.Values(&_constants, false)
105	if err != nil {
106		return tracerr.Wrap(err)
107	}
108	_, err = hdb.dbWrite.Exec(
109		"UPDATE node_info SET constants = $1 WHERE item_id = 1;",
110		values[0],
111	)
112	return tracerr.Wrap(err)
113}
114
115// GetStateInternalAPI returns the StateAPI
116func (hdb *HistoryDB) GetStateInternalAPI() (*StateAPI, error) {
117	return hdb.getStateAPI(hdb.dbRead)
118}
119
120func (hdb *HistoryDB) getStateAPI(d meddler.DB) (*StateAPI, error) {
121	var nodeInfo NodeInfo
122	err := meddler.QueryRow(
123		d, &nodeInfo,
124		"SELECT state FROM node_info WHERE item_id = 1;",
125	)
126	return nodeInfo.StateAPI, tracerr.Wrap(err)
127}
128
129// SetStateInternalAPI sets the StateAPI
130func (hdb *HistoryDB) SetStateInternalAPI(stateAPI *StateAPI) error {
131	if stateAPI.Network.LastBatch != nil {
132		stateAPI.Network.LastBatch.CollectedFeesAPI =
133			apitypes.NewCollectedFeesAPI(stateAPI.Network.LastBatch.CollectedFeesDB)
134	}
135	_stateAPI := struct {
136		StateAPI *StateAPI `meddler:"state,json"`
137	}{stateAPI}
138	values, err := meddler.Default.Values(&_stateAPI, false)
139	if err != nil {
140		return tracerr.Wrap(err)
141	}
142	_, err = hdb.dbWrite.Exec(
143		"UPDATE node_info SET state = $1 WHERE item_id = 1;",
144		values[0],
145	)
146	return tracerr.Wrap(err)
147}
148
149// GetNodeConfig returns the NodeConfig
150func (hdb *HistoryDB) GetNodeConfig() (*NodeConfig, error) {
151	var nodeInfo NodeInfo
152	err := meddler.QueryRow(
153		hdb.dbRead, &nodeInfo,
154		"SELECT config FROM node_info WHERE item_id = 1;",
155	)
156	return nodeInfo.NodeConfig, tracerr.Wrap(err)
157}
158
159// SetNodeConfig sets the NodeConfig
160func (hdb *HistoryDB) SetNodeConfig(nodeConfig *NodeConfig) error {
161	_nodeConfig := struct {
162		NodeConfig *NodeConfig `meddler:"config,json"`
163	}{nodeConfig}
164	values, err := meddler.Default.Values(&_nodeConfig, false)
165	if err != nil {
166		return tracerr.Wrap(err)
167	}
168	_, err = hdb.dbWrite.Exec(
169		"UPDATE node_info SET config = $1 WHERE item_id = 1;",
170		values[0],
171	)
172	return tracerr.Wrap(err)
173}