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.

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