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.

215 lines
6.0 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
  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. type 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. type NextForgerAPI struct {
  21. Coordinator CoordinatorAPI `json:"coordinator"`
  22. Period Period `json:"period"`
  23. }
  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. // NodePublicConfig is the configuration of the node that is exposed via API
  32. type NodePublicConfig struct {
  33. // ForgeDelay in seconds
  34. ForgeDelay float64 `json:"forgeDelay"`
  35. }
  36. type StateAPI struct {
  37. // NodePublicConfig is the configuration of the node that is exposed via API
  38. NodePublicConfig NodePublicConfig `json:"nodeConfig"`
  39. Network NetworkAPI `json:"network"`
  40. Metrics MetricsAPI `json:"metrics"`
  41. Rollup RollupVariablesAPI `json:"rollup"`
  42. Auction AuctionVariablesAPI `json:"auction"`
  43. WithdrawalDelayer common.WDelayerVariables `json:"withdrawalDelayer"`
  44. RecommendedFee common.RecommendedFee `json:"recommendedFee"`
  45. }
  46. type Constants struct {
  47. // RollupConstants common.RollupConstants
  48. // AuctionConstants common.AuctionConstants
  49. // WDelayerConstants common.WDelayerConstants
  50. common.SCConsts
  51. ChainID uint16
  52. HermezAddress ethCommon.Address
  53. }
  54. type NodeConfig struct {
  55. MaxPoolTxs uint32 `meddler:"max_pool_txs"`
  56. MinFeeUSD float64 `meddler:"min_fee"`
  57. }
  58. type NodeInfo struct {
  59. ItemID int `meddler:"item_id,pk"`
  60. APIState *StateAPI `meddler:"state,json"`
  61. NodeConfig *NodeConfig `meddler:"config,json"`
  62. Constants *Constants `meddler:"constants,json"`
  63. }
  64. func (hdb *HistoryDB) GetNodeInfo() (*NodeInfo, error) {
  65. ni := &NodeInfo{}
  66. err := meddler.QueryRow(
  67. hdb.dbRead, ni, `SELECT * FROM node_info WHERE item_id = 1;`,
  68. )
  69. return ni, tracerr.Wrap(err)
  70. }
  71. func (hdb *HistoryDB) GetConstants() (*Constants, error) {
  72. var nodeInfo NodeInfo
  73. err := meddler.QueryRow(
  74. hdb.dbRead, &nodeInfo,
  75. "SELECT constants FROM node_info WHERE item_id = 1;",
  76. )
  77. return nodeInfo.Constants, tracerr.Wrap(err)
  78. }
  79. func (hdb *HistoryDB) SetConstants(constants *Constants) error {
  80. _constants := struct {
  81. Constants *Constants `meddler:"constants,json"`
  82. }{constants}
  83. values, err := meddler.Default.Values(&_constants, false)
  84. if err != nil {
  85. return tracerr.Wrap(err)
  86. }
  87. _, err = hdb.dbWrite.Exec(
  88. "UPDATE node_info SET constants = $1 WHERE item_id = 1;",
  89. values[0],
  90. )
  91. return tracerr.Wrap(err)
  92. }
  93. func (hdb *HistoryDB) GetStateInternalAPI() (*StateAPI, error) {
  94. return hdb.getStateAPI(hdb.dbRead)
  95. }
  96. func (hdb *HistoryDB) getStateAPI(d meddler.DB) (*StateAPI, error) {
  97. var nodeInfo NodeInfo
  98. err := meddler.QueryRow(
  99. d, &nodeInfo,
  100. "SELECT state FROM node_info WHERE item_id = 1;",
  101. )
  102. return nodeInfo.APIState, tracerr.Wrap(err)
  103. }
  104. func (hdb *HistoryDB) SetAPIState(apiState *StateAPI) error {
  105. _apiState := struct {
  106. APIState *StateAPI `meddler:"state,json"`
  107. }{apiState}
  108. values, err := meddler.Default.Values(&_apiState, false)
  109. if err != nil {
  110. return tracerr.Wrap(err)
  111. }
  112. _, err = hdb.dbWrite.Exec(
  113. "UPDATE node_info SET state = $1 WHERE item_id = 1;",
  114. values[0],
  115. )
  116. return tracerr.Wrap(err)
  117. }
  118. func (hdb *HistoryDB) GetNodeConfig() (*NodeConfig, error) {
  119. var nodeInfo NodeInfo
  120. err := meddler.QueryRow(
  121. hdb.dbRead, &nodeInfo,
  122. "SELECT config FROM node_info WHERE item_id = 1;",
  123. )
  124. return nodeInfo.NodeConfig, tracerr.Wrap(err)
  125. }
  126. func (hdb *HistoryDB) SetNodeConfig(nodeConfig *NodeConfig) error {
  127. _nodeConfig := struct {
  128. NodeConfig *NodeConfig `meddler:"config,json"`
  129. }{nodeConfig}
  130. values, err := meddler.Default.Values(&_nodeConfig, false)
  131. if err != nil {
  132. return tracerr.Wrap(err)
  133. }
  134. _, err = hdb.dbWrite.Exec(
  135. "UPDATE config SET state = $1 WHERE item_id = 1;",
  136. values[0],
  137. )
  138. return tracerr.Wrap(err)
  139. }
  140. // func (hdb *HistoryDB) SetInitialNodeInfo(maxPoolTxs uint32, minFeeUSD float64, constants *Constants) error {
  141. // ni := &NodeInfo{
  142. // MaxPoolTxs: &maxPoolTxs,
  143. // MinFeeUSD: &minFeeUSD,
  144. // Constants: constants,
  145. // }
  146. // return tracerr.Wrap(meddler.Insert(hdb.dbWrite, "node_info", ni))
  147. // }
  148. // apiSlotToBigInts converts from [6]*apitypes.BigIntStr to [6]*big.Int
  149. // func apiSlotToBigInts(defaultSlotSetBid [6]*apitypes.BigIntStr) ([6]*big.Int, error) {
  150. // var slots [6]*big.Int
  151. //
  152. // for i, slot := range defaultSlotSetBid {
  153. // bigInt, ok := new(big.Int).SetString(string(*slot), 10)
  154. // if !ok {
  155. // return slots, tracerr.Wrap(fmt.Errorf("can't convert %T into big.Int", slot))
  156. // }
  157. // slots[i] = bigInt
  158. // }
  159. //
  160. // return slots, nil
  161. // }
  162. // func (hdb *HistoryDB) updateNodeInfo(setUpdatedNodeInfo func(*sqlx.Tx, *NodeInfo) error) error {
  163. // // Create a SQL transaction or read and update atomicaly
  164. // txn, err := hdb.dbWrite.Beginx()
  165. // if err != nil {
  166. // return tracerr.Wrap(err)
  167. // }
  168. // defer func() {
  169. // if err != nil {
  170. // db.Rollback(txn)
  171. // }
  172. // }()
  173. // // Read current node info
  174. // ni := &NodeInfo{}
  175. // if err := meddler.QueryRow(
  176. // txn, ni, "SELECT * FROM node_info;",
  177. // ); err != nil {
  178. // return tracerr.Wrap(err)
  179. // }
  180. // // Update NodeInfo struct
  181. // if err := setUpdatedNodeInfo(txn, ni); err != nil {
  182. // return tracerr.Wrap(err)
  183. // }
  184. // // Update NodeInfo at DB
  185. // if _, err := txn.Exec("DELETE FROM node_info;"); err != nil {
  186. // return tracerr.Wrap(err)
  187. // }
  188. // if err := meddler.Insert(txn, "node_info", ni); err != nil {
  189. // return tracerr.Wrap(err)
  190. // }
  191. // // Commit NodeInfo update
  192. // return tracerr.Wrap(txn.Commit())
  193. // }