You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

173 lines
5.1 KiB

Allow serving API only via new cli command - Add new command to the cli/node: `serveapi` that alows serving the API just by connecting to the PostgreSQL database. The mode flag should me passed in order to select whether we are connecting to a synchronizer database or a coordinator database. If `coord` is chosen as mode, the coordinator endpoints can be activated in order to allow inserting l2txs and authorizations into the L2DB. Summary of the implementation details - New SQL table with 3 columns (plus `item_id` pk). The table only contains a single row with `item_id` = 1. Columns: - state: historydb.StateAPI in JSON. This is the struct that is served via the `/state` API endpoint. The node will periodically update this struct and store it int he DB. The api server will query it from the DB to serve it. - config: historydb.NodeConfig in JSON. This struct contains node configuration parameters that the API needs to be aware of. It's updated once every time the node starts. - constants: historydb.Constants in JSON. This struct contains all the hermez network constants gathered via the ethereum client by the node. It's written once every time the node starts. - The HistoryDB contains methods to get and update each one of these columns individually. - The HistoryDB contains all methods that query the DB and prepare objects that will appear in the StateAPI endpoint. - The configuration used in for the `serveapi` cli/node command is defined in `config.APIServer`, and is a subset of `node.Config` in order to allow reusing the same configuration file of the node if desired. - A new object is introduced in the api: `StateAPIUpdater`, which contains all the necessary information to update the StateAPI in the DB periodically by the node. - Moved the types `SCConsts`, `SCVariables` and `SCVariablesPtr` from `syncrhonizer` to `common` for convenience.
3 years ago
Allow serving API only via new cli command - Add new command to the cli/node: `serveapi` that alows serving the API just by connecting to the PostgreSQL database. The mode flag should me passed in order to select whether we are connecting to a synchronizer database or a coordinator database. If `coord` is chosen as mode, the coordinator endpoints can be activated in order to allow inserting l2txs and authorizations into the L2DB. Summary of the implementation details - New SQL table with 3 columns (plus `item_id` pk). The table only contains a single row with `item_id` = 1. Columns: - state: historydb.StateAPI in JSON. This is the struct that is served via the `/state` API endpoint. The node will periodically update this struct and store it int he DB. The api server will query it from the DB to serve it. - config: historydb.NodeConfig in JSON. This struct contains node configuration parameters that the API needs to be aware of. It's updated once every time the node starts. - constants: historydb.Constants in JSON. This struct contains all the hermez network constants gathered via the ethereum client by the node. It's written once every time the node starts. - The HistoryDB contains methods to get and update each one of these columns individually. - The HistoryDB contains all methods that query the DB and prepare objects that will appear in the StateAPI endpoint. - The configuration used in for the `serveapi` cli/node command is defined in `config.APIServer`, and is a subset of `node.Config` in order to allow reusing the same configuration file of the node if desired. - A new object is introduced in the api: `StateAPIUpdater`, which contains all the necessary information to update the StateAPI in the DB periodically by the node. - Moved the types `SCConsts`, `SCVariables` and `SCVariablesPtr` from `syncrhonizer` to `common` for convenience.
3 years ago
Allow serving API only via new cli command - Add new command to the cli/node: `serveapi` that alows serving the API just by connecting to the PostgreSQL database. The mode flag should me passed in order to select whether we are connecting to a synchronizer database or a coordinator database. If `coord` is chosen as mode, the coordinator endpoints can be activated in order to allow inserting l2txs and authorizations into the L2DB. Summary of the implementation details - New SQL table with 3 columns (plus `item_id` pk). The table only contains a single row with `item_id` = 1. Columns: - state: historydb.StateAPI in JSON. This is the struct that is served via the `/state` API endpoint. The node will periodically update this struct and store it int he DB. The api server will query it from the DB to serve it. - config: historydb.NodeConfig in JSON. This struct contains node configuration parameters that the API needs to be aware of. It's updated once every time the node starts. - constants: historydb.Constants in JSON. This struct contains all the hermez network constants gathered via the ethereum client by the node. It's written once every time the node starts. - The HistoryDB contains methods to get and update each one of these columns individually. - The HistoryDB contains all methods that query the DB and prepare objects that will appear in the StateAPI endpoint. - The configuration used in for the `serveapi` cli/node command is defined in `config.APIServer`, and is a subset of `node.Config` in order to allow reusing the same configuration file of the node if desired. - A new object is introduced in the api: `StateAPIUpdater`, which contains all the necessary information to update the StateAPI in the DB periodically by the node. - Moved the types `SCConsts`, `SCVariables` and `SCVariablesPtr` from `syncrhonizer` to `common` for convenience.
3 years ago
Allow serving API only via new cli command - Add new command to the cli/node: `serveapi` that alows serving the API just by connecting to the PostgreSQL database. The mode flag should me passed in order to select whether we are connecting to a synchronizer database or a coordinator database. If `coord` is chosen as mode, the coordinator endpoints can be activated in order to allow inserting l2txs and authorizations into the L2DB. Summary of the implementation details - New SQL table with 3 columns (plus `item_id` pk). The table only contains a single row with `item_id` = 1. Columns: - state: historydb.StateAPI in JSON. This is the struct that is served via the `/state` API endpoint. The node will periodically update this struct and store it int he DB. The api server will query it from the DB to serve it. - config: historydb.NodeConfig in JSON. This struct contains node configuration parameters that the API needs to be aware of. It's updated once every time the node starts. - constants: historydb.Constants in JSON. This struct contains all the hermez network constants gathered via the ethereum client by the node. It's written once every time the node starts. - The HistoryDB contains methods to get and update each one of these columns individually. - The HistoryDB contains all methods that query the DB and prepare objects that will appear in the StateAPI endpoint. - The configuration used in for the `serveapi` cli/node command is defined in `config.APIServer`, and is a subset of `node.Config` in order to allow reusing the same configuration file of the node if desired. - A new object is introduced in the api: `StateAPIUpdater`, which contains all the necessary information to update the StateAPI in the DB periodically by the node. - Moved the types `SCConsts`, `SCVariables` and `SCVariablesPtr` from `syncrhonizer` to `common` for convenience.
3 years ago
Allow serving API only via new cli command - Add new command to the cli/node: `serveapi` that alows serving the API just by connecting to the PostgreSQL database. The mode flag should me passed in order to select whether we are connecting to a synchronizer database or a coordinator database. If `coord` is chosen as mode, the coordinator endpoints can be activated in order to allow inserting l2txs and authorizations into the L2DB. Summary of the implementation details - New SQL table with 3 columns (plus `item_id` pk). The table only contains a single row with `item_id` = 1. Columns: - state: historydb.StateAPI in JSON. This is the struct that is served via the `/state` API endpoint. The node will periodically update this struct and store it int he DB. The api server will query it from the DB to serve it. - config: historydb.NodeConfig in JSON. This struct contains node configuration parameters that the API needs to be aware of. It's updated once every time the node starts. - constants: historydb.Constants in JSON. This struct contains all the hermez network constants gathered via the ethereum client by the node. It's written once every time the node starts. - The HistoryDB contains methods to get and update each one of these columns individually. - The HistoryDB contains all methods that query the DB and prepare objects that will appear in the StateAPI endpoint. - The configuration used in for the `serveapi` cli/node command is defined in `config.APIServer`, and is a subset of `node.Config` in order to allow reusing the same configuration file of the node if desired. - A new object is introduced in the api: `StateAPIUpdater`, which contains all the necessary information to update the StateAPI in the DB periodically by the node. - Moved the types `SCConsts`, `SCVariables` and `SCVariablesPtr` from `syncrhonizer` to `common` for convenience.
3 years ago
Allow serving API only via new cli command - Add new command to the cli/node: `serveapi` that alows serving the API just by connecting to the PostgreSQL database. The mode flag should me passed in order to select whether we are connecting to a synchronizer database or a coordinator database. If `coord` is chosen as mode, the coordinator endpoints can be activated in order to allow inserting l2txs and authorizations into the L2DB. Summary of the implementation details - New SQL table with 3 columns (plus `item_id` pk). The table only contains a single row with `item_id` = 1. Columns: - state: historydb.StateAPI in JSON. This is the struct that is served via the `/state` API endpoint. The node will periodically update this struct and store it int he DB. The api server will query it from the DB to serve it. - config: historydb.NodeConfig in JSON. This struct contains node configuration parameters that the API needs to be aware of. It's updated once every time the node starts. - constants: historydb.Constants in JSON. This struct contains all the hermez network constants gathered via the ethereum client by the node. It's written once every time the node starts. - The HistoryDB contains methods to get and update each one of these columns individually. - The HistoryDB contains all methods that query the DB and prepare objects that will appear in the StateAPI endpoint. - The configuration used in for the `serveapi` cli/node command is defined in `config.APIServer`, and is a subset of `node.Config` in order to allow reusing the same configuration file of the node if desired. - A new object is introduced in the api: `StateAPIUpdater`, which contains all the necessary information to update the StateAPI in the DB periodically by the node. - Moved the types `SCConsts`, `SCVariables` and `SCVariablesPtr` from `syncrhonizer` to `common` for convenience.
3 years ago
Allow serving API only via new cli command - Add new command to the cli/node: `serveapi` that alows serving the API just by connecting to the PostgreSQL database. The mode flag should me passed in order to select whether we are connecting to a synchronizer database or a coordinator database. If `coord` is chosen as mode, the coordinator endpoints can be activated in order to allow inserting l2txs and authorizations into the L2DB. Summary of the implementation details - New SQL table with 3 columns (plus `item_id` pk). The table only contains a single row with `item_id` = 1. Columns: - state: historydb.StateAPI in JSON. This is the struct that is served via the `/state` API endpoint. The node will periodically update this struct and store it int he DB. The api server will query it from the DB to serve it. - config: historydb.NodeConfig in JSON. This struct contains node configuration parameters that the API needs to be aware of. It's updated once every time the node starts. - constants: historydb.Constants in JSON. This struct contains all the hermez network constants gathered via the ethereum client by the node. It's written once every time the node starts. - The HistoryDB contains methods to get and update each one of these columns individually. - The HistoryDB contains all methods that query the DB and prepare objects that will appear in the StateAPI endpoint. - The configuration used in for the `serveapi` cli/node command is defined in `config.APIServer`, and is a subset of `node.Config` in order to allow reusing the same configuration file of the node if desired. - A new object is introduced in the api: `StateAPIUpdater`, which contains all the necessary information to update the StateAPI in the DB periodically by the node. - Moved the types `SCConsts`, `SCVariables` and `SCVariablesPtr` from `syncrhonizer` to `common` for convenience.
3 years ago
Allow serving API only via new cli command - Add new command to the cli/node: `serveapi` that alows serving the API just by connecting to the PostgreSQL database. The mode flag should me passed in order to select whether we are connecting to a synchronizer database or a coordinator database. If `coord` is chosen as mode, the coordinator endpoints can be activated in order to allow inserting l2txs and authorizations into the L2DB. Summary of the implementation details - New SQL table with 3 columns (plus `item_id` pk). The table only contains a single row with `item_id` = 1. Columns: - state: historydb.StateAPI in JSON. This is the struct that is served via the `/state` API endpoint. The node will periodically update this struct and store it int he DB. The api server will query it from the DB to serve it. - config: historydb.NodeConfig in JSON. This struct contains node configuration parameters that the API needs to be aware of. It's updated once every time the node starts. - constants: historydb.Constants in JSON. This struct contains all the hermez network constants gathered via the ethereum client by the node. It's written once every time the node starts. - The HistoryDB contains methods to get and update each one of these columns individually. - The HistoryDB contains all methods that query the DB and prepare objects that will appear in the StateAPI endpoint. - The configuration used in for the `serveapi` cli/node command is defined in `config.APIServer`, and is a subset of `node.Config` in order to allow reusing the same configuration file of the node if desired. - A new object is introduced in the api: `StateAPIUpdater`, which contains all the necessary information to update the StateAPI in the DB periodically by the node. - Moved the types `SCConsts`, `SCVariables` and `SCVariablesPtr` from `syncrhonizer` to `common` for convenience.
3 years ago
  1. package historydb
  2. import (
  3. "time"
  4. ethCommon "github.com/ethereum/go-ethereum/common"
  5. "github.com/hermeznetwork/hermez-node/api/apitypes"
  6. "github.com/hermeznetwork/hermez-node/common"
  7. "github.com/hermeznetwork/tracerr"
  8. "github.com/russross/meddler"
  9. )
  10. // Period represents a time period in ethereum
  11. type Period struct {
  12. SlotNum int64 `json:"slotNum"`
  13. FromBlock int64 `json:"fromBlock"`
  14. ToBlock int64 `json:"toBlock"`
  15. FromTimestamp time.Time `json:"fromTimestamp"`
  16. ToTimestamp time.Time `json:"toTimestamp"`
  17. }
  18. // NextForgerAPI represents the next forger exposed via the API
  19. type NextForgerAPI struct {
  20. Coordinator CoordinatorAPI `json:"coordinator"`
  21. Period Period `json:"period"`
  22. }
  23. // NetworkAPI is the network state exposed via the API
  24. type NetworkAPI struct {
  25. LastEthBlock int64 `json:"lastEthereumBlock"`
  26. LastSyncBlock int64 `json:"lastSynchedBlock"`
  27. LastBatch *BatchAPI `json:"lastBatch"`
  28. CurrentSlot int64 `json:"currentSlot"`
  29. NextForgers []NextForgerAPI `json:"nextForgers"`
  30. PendingL1Txs int `json:"pendingL1Transactions"`
  31. }
  32. // NodePublicInfo is the configuration and metrics of the node that is exposed via API
  33. type NodePublicInfo struct {
  34. // ForgeDelay in seconds
  35. ForgeDelay float64 `json:"forgeDelay"`
  36. // PoolLoad amount of transactions in the pool
  37. PoolLoad int64 `json:"poolLoad"`
  38. }
  39. // StateAPI is an object representing the node and network state exposed via the API
  40. type StateAPI struct {
  41. NodePublicInfo NodePublicInfo `json:"node"`
  42. Network NetworkAPI `json:"network"`
  43. Metrics MetricsAPI `json:"metrics"`
  44. Rollup RollupVariablesAPI `json:"rollup"`
  45. Auction AuctionVariablesAPI `json:"auction"`
  46. WithdrawalDelayer common.WDelayerVariables `json:"withdrawalDelayer"`
  47. RecommendedFee common.RecommendedFee `json:"recommendedFee"`
  48. }
  49. // Constants contains network constants
  50. type Constants struct {
  51. common.SCConsts
  52. ChainID uint16
  53. HermezAddress ethCommon.Address
  54. }
  55. // NodeConfig contains the node config exposed in the API
  56. type NodeConfig struct {
  57. MaxPoolTxs uint32
  58. MinFeeUSD float64
  59. MaxFeeUSD float64
  60. ForgeDelay float64
  61. }
  62. // NodeInfo contains information about he node used when serving the API
  63. type NodeInfo struct {
  64. ItemID int `meddler:"item_id,pk"`
  65. StateAPI *StateAPI `meddler:"state,json"`
  66. NodeConfig *NodeConfig `meddler:"config,json"`
  67. Constants *Constants `meddler:"constants,json"`
  68. }
  69. // GetNodeInfo returns the NodeInfo
  70. func (hdb *HistoryDB) GetNodeInfo() (*NodeInfo, error) {
  71. ni := &NodeInfo{}
  72. err := meddler.QueryRow(
  73. hdb.dbRead, ni, `SELECT * FROM node_info WHERE item_id = 1;`,
  74. )
  75. return ni, tracerr.Wrap(err)
  76. }
  77. // GetConstants returns the Constats
  78. func (hdb *HistoryDB) GetConstants() (*Constants, error) {
  79. var nodeInfo NodeInfo
  80. err := meddler.QueryRow(
  81. hdb.dbRead, &nodeInfo,
  82. "SELECT constants FROM node_info WHERE item_id = 1;",
  83. )
  84. return nodeInfo.Constants, tracerr.Wrap(err)
  85. }
  86. // SetConstants sets the Constants
  87. func (hdb *HistoryDB) SetConstants(constants *Constants) error {
  88. _constants := struct {
  89. Constants *Constants `meddler:"constants,json"`
  90. }{constants}
  91. values, err := meddler.Default.Values(&_constants, false)
  92. if err != nil {
  93. return tracerr.Wrap(err)
  94. }
  95. _, err = hdb.dbWrite.Exec(
  96. "UPDATE node_info SET constants = $1 WHERE item_id = 1;",
  97. values[0],
  98. )
  99. return tracerr.Wrap(err)
  100. }
  101. // GetStateInternalAPI returns the StateAPI
  102. func (hdb *HistoryDB) GetStateInternalAPI() (*StateAPI, error) {
  103. return hdb.getStateAPI(hdb.dbRead)
  104. }
  105. func (hdb *HistoryDB) getStateAPI(d meddler.DB) (*StateAPI, error) {
  106. var nodeInfo NodeInfo
  107. err := meddler.QueryRow(
  108. d, &nodeInfo,
  109. "SELECT state FROM node_info WHERE item_id = 1;",
  110. )
  111. return nodeInfo.StateAPI, tracerr.Wrap(err)
  112. }
  113. // SetStateInternalAPI sets the StateAPI
  114. func (hdb *HistoryDB) SetStateInternalAPI(stateAPI *StateAPI) error {
  115. if stateAPI.Network.LastBatch != nil {
  116. stateAPI.Network.LastBatch.CollectedFeesAPI =
  117. apitypes.NewCollectedFeesAPI(stateAPI.Network.LastBatch.CollectedFeesDB)
  118. }
  119. _stateAPI := struct {
  120. StateAPI *StateAPI `meddler:"state,json"`
  121. }{stateAPI}
  122. values, err := meddler.Default.Values(&_stateAPI, false)
  123. if err != nil {
  124. return tracerr.Wrap(err)
  125. }
  126. _, err = hdb.dbWrite.Exec(
  127. "UPDATE node_info SET state = $1 WHERE item_id = 1;",
  128. values[0],
  129. )
  130. return tracerr.Wrap(err)
  131. }
  132. // GetNodeConfig returns the NodeConfig
  133. func (hdb *HistoryDB) GetNodeConfig() (*NodeConfig, error) {
  134. var nodeInfo NodeInfo
  135. err := meddler.QueryRow(
  136. hdb.dbRead, &nodeInfo,
  137. "SELECT config FROM node_info WHERE item_id = 1;",
  138. )
  139. return nodeInfo.NodeConfig, tracerr.Wrap(err)
  140. }
  141. // SetNodeConfig sets the NodeConfig
  142. func (hdb *HistoryDB) SetNodeConfig(nodeConfig *NodeConfig) error {
  143. _nodeConfig := struct {
  144. NodeConfig *NodeConfig `meddler:"config,json"`
  145. }{nodeConfig}
  146. values, err := meddler.Default.Values(&_nodeConfig, false)
  147. if err != nil {
  148. return tracerr.Wrap(err)
  149. }
  150. _, err = hdb.dbWrite.Exec(
  151. "UPDATE node_info SET config = $1 WHERE item_id = 1;",
  152. values[0],
  153. )
  154. return tracerr.Wrap(err)
  155. }