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