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	PendingL1Txs  int             `json:"pendingL1Transactions"`
 36}
 37
 38// NodePublicConfig is the configuration of the node that is exposed via API
 39type NodePublicConfig struct {
 40	// ForgeDelay in seconds
 41	ForgeDelay float64 `json:"forgeDelay"`
 42}
 43
 44// StateAPI is an object representing the node and network state exposed via the API
 45type StateAPI struct {
 46	// NodePublicConfig is the configuration of the node that is exposed via API
 47	NodePublicConfig  NodePublicConfig         `json:"nodeConfig"`
 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	ForgeDelay float64
 68}
 69
 70// NodeInfo contains information about he node used when serving the API
 71type NodeInfo struct {
 72	ItemID     int         `meddler:"item_id,pk"`
 73	StateAPI   *StateAPI   `meddler:"state,json"`
 74	NodeConfig *NodeConfig `meddler:"config,json"`
 75	Constants  *Constants  `meddler:"constants,json"`
 76}
 77
 78// GetNodeInfo returns the NodeInfo
 79func (hdb *HistoryDB) GetNodeInfo() (*NodeInfo, error) {
 80	ni := &NodeInfo{}
 81	err := meddler.QueryRow(
 82		hdb.dbRead, ni, `SELECT * FROM node_info WHERE item_id = 1;`,
 83	)
 84	return ni, tracerr.Wrap(err)
 85}
 86
 87// GetConstants returns the Constats
 88func (hdb *HistoryDB) GetConstants() (*Constants, error) {
 89	var nodeInfo NodeInfo
 90	err := meddler.QueryRow(
 91		hdb.dbRead, &nodeInfo,
 92		"SELECT constants FROM node_info WHERE item_id = 1;",
 93	)
 94	return nodeInfo.Constants, tracerr.Wrap(err)
 95}
 96
 97// SetConstants sets the Constants
 98func (hdb *HistoryDB) SetConstants(constants *Constants) error {
 99	_constants := struct {
100		Constants *Constants `meddler:"constants,json"`
101	}{constants}
102	values, err := meddler.Default.Values(&_constants, false)
103	if err != nil {
104		return tracerr.Wrap(err)
105	}
106	_, err = hdb.dbWrite.Exec(
107		"UPDATE node_info SET constants = $1 WHERE item_id = 1;",
108		values[0],
109	)
110	return tracerr.Wrap(err)
111}
112
113// GetStateInternalAPI returns the StateAPI
114func (hdb *HistoryDB) GetStateInternalAPI() (*StateAPI, error) {
115	return hdb.getStateAPI(hdb.dbRead)
116}
117
118func (hdb *HistoryDB) getStateAPI(d meddler.DB) (*StateAPI, error) {
119	var nodeInfo NodeInfo
120	err := meddler.QueryRow(
121		d, &nodeInfo,
122		"SELECT state FROM node_info WHERE item_id = 1;",
123	)
124	return nodeInfo.StateAPI, tracerr.Wrap(err)
125}
126
127// SetStateInternalAPI sets the StateAPI
128func (hdb *HistoryDB) SetStateInternalAPI(stateAPI *StateAPI) error {
129	if stateAPI.Network.LastBatch != nil {
130		stateAPI.Network.LastBatch.CollectedFeesAPI =
131			apitypes.NewCollectedFeesAPI(stateAPI.Network.LastBatch.CollectedFeesDB)
132	}
133	_stateAPI := struct {
134		StateAPI *StateAPI `meddler:"state,json"`
135	}{stateAPI}
136	values, err := meddler.Default.Values(&_stateAPI, false)
137	if err != nil {
138		return tracerr.Wrap(err)
139	}
140	_, err = hdb.dbWrite.Exec(
141		"UPDATE node_info SET state = $1 WHERE item_id = 1;",
142		values[0],
143	)
144	return tracerr.Wrap(err)
145}
146
147// GetNodeConfig returns the NodeConfig
148func (hdb *HistoryDB) GetNodeConfig() (*NodeConfig, error) {
149	var nodeInfo NodeInfo
150	err := meddler.QueryRow(
151		hdb.dbRead, &nodeInfo,
152		"SELECT config FROM node_info WHERE item_id = 1;",
153	)
154	return nodeInfo.NodeConfig, tracerr.Wrap(err)
155}
156
157// SetNodeConfig sets the NodeConfig
158func (hdb *HistoryDB) SetNodeConfig(nodeConfig *NodeConfig) error {
159	_nodeConfig := struct {
160		NodeConfig *NodeConfig `meddler:"config,json"`
161	}{nodeConfig}
162	values, err := meddler.Default.Values(&_nodeConfig, false)
163	if err != nil {
164		return tracerr.Wrap(err)
165	}
166	_, err = hdb.dbWrite.Exec(
167		"UPDATE node_info SET config = $1 WHERE item_id = 1;",
168		values[0],
169	)
170	return tracerr.Wrap(err)
171}