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