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.

169 lines
4.9 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. package historydb
  2. import (
  3. "time"
  4. ethCommon "github.com/ethereum/go-ethereum/common"
  5. "github.com/hermeznetwork/hermez-node/common"
  6. "github.com/hermeznetwork/tracerr"
  7. "github.com/russross/meddler"
  8. )
  9. const (
  10. createAccountExtraFeePercentage float64 = 2
  11. createAccountInternalExtraFeePercentage float64 = 2.5
  12. )
  13. // Period represents a time period in ethereum
  14. type 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. // NextForgerAPI represents the next forger exposed via the API
  22. type NextForgerAPI struct {
  23. Coordinator CoordinatorAPI `json:"coordinator"`
  24. Period Period `json:"period"`
  25. }
  26. // NetworkAPI is the network state exposed via the API
  27. type NetworkAPI struct {
  28. LastEthBlock int64 `json:"lastEthereumBlock"`
  29. LastSyncBlock int64 `json:"lastSynchedBlock"`
  30. LastBatch *BatchAPI `json:"lastBatch"`
  31. CurrentSlot int64 `json:"currentSlot"`
  32. NextForgers []NextForgerAPI `json:"nextForgers"`
  33. }
  34. // NodePublicConfig is the configuration of the node that is exposed via API
  35. type NodePublicConfig struct {
  36. // ForgeDelay in seconds
  37. ForgeDelay float64 `json:"forgeDelay"`
  38. }
  39. // StateAPI is an object representing the node and network state exposed via the API
  40. type StateAPI struct {
  41. // NodePublicConfig is the configuration of the node that is exposed via API
  42. NodePublicConfig NodePublicConfig `json:"nodeConfig"`
  43. Network NetworkAPI `json:"network"`
  44. Metrics MetricsAPI `json:"metrics"`
  45. Rollup RollupVariablesAPI `json:"rollup"`
  46. Auction AuctionVariablesAPI `json:"auction"`
  47. WithdrawalDelayer common.WDelayerVariables `json:"withdrawalDelayer"`
  48. RecommendedFee common.RecommendedFee `json:"recommendedFee"`
  49. }
  50. // Constants contains network constants
  51. type Constants struct {
  52. common.SCConsts
  53. ChainID uint16
  54. HermezAddress ethCommon.Address
  55. }
  56. // NodeConfig contains the node config exposed in the API
  57. type NodeConfig struct {
  58. MaxPoolTxs uint32 `meddler:"max_pool_txs"`
  59. MinFeeUSD float64 `meddler:"min_fee"`
  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. _stateAPI := struct {
  115. StateAPI *StateAPI `meddler:"state,json"`
  116. }{stateAPI}
  117. values, err := meddler.Default.Values(&_stateAPI, false)
  118. if err != nil {
  119. return tracerr.Wrap(err)
  120. }
  121. _, err = hdb.dbWrite.Exec(
  122. "UPDATE node_info SET state = $1 WHERE item_id = 1;",
  123. values[0],
  124. )
  125. return tracerr.Wrap(err)
  126. }
  127. // GetNodeConfig returns the NodeConfig
  128. func (hdb *HistoryDB) GetNodeConfig() (*NodeConfig, error) {
  129. var nodeInfo NodeInfo
  130. err := meddler.QueryRow(
  131. hdb.dbRead, &nodeInfo,
  132. "SELECT config FROM node_info WHERE item_id = 1;",
  133. )
  134. return nodeInfo.NodeConfig, tracerr.Wrap(err)
  135. }
  136. // SetNodeConfig sets the NodeConfig
  137. func (hdb *HistoryDB) SetNodeConfig(nodeConfig *NodeConfig) error {
  138. _nodeConfig := struct {
  139. NodeConfig *NodeConfig `meddler:"config,json"`
  140. }{nodeConfig}
  141. values, err := meddler.Default.Values(&_nodeConfig, false)
  142. if err != nil {
  143. return tracerr.Wrap(err)
  144. }
  145. _, err = hdb.dbWrite.Exec(
  146. "UPDATE node_info SET config = $1 WHERE item_id = 1;",
  147. values[0],
  148. )
  149. return tracerr.Wrap(err)
  150. }