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.

335 lines
16 KiB

3 years ago
3 years ago
3 years ago
  1. package historydb
  2. import (
  3. "encoding/json"
  4. "math/big"
  5. "time"
  6. ethCommon "github.com/ethereum/go-ethereum/common"
  7. "github.com/hermeznetwork/hermez-node/apitypes"
  8. "github.com/hermeznetwork/hermez-node/common"
  9. "github.com/iden3/go-iden3-crypto/babyjub"
  10. "github.com/iden3/go-merkletree"
  11. )
  12. // TxAPI is a representation of a generic Tx with additional information
  13. // required by the API, and extracted by joining block and token tables
  14. type TxAPI struct {
  15. // Generic
  16. IsL1 bool `meddler:"is_l1"`
  17. TxID common.TxID `meddler:"id"`
  18. ItemID uint64 `meddler:"item_id"`
  19. Type common.TxType `meddler:"type"`
  20. Position int `meddler:"position"`
  21. FromIdx *apitypes.HezIdx `meddler:"from_idx"`
  22. FromEthAddr *apitypes.HezEthAddr `meddler:"from_eth_addr"`
  23. FromBJJ *apitypes.HezBJJ `meddler:"from_bjj"`
  24. ToIdx apitypes.HezIdx `meddler:"to_idx"`
  25. ToEthAddr *apitypes.HezEthAddr `meddler:"to_eth_addr"`
  26. ToBJJ *apitypes.HezBJJ `meddler:"to_bjj"`
  27. Amount apitypes.BigIntStr `meddler:"amount"`
  28. HistoricUSD *float64 `meddler:"amount_usd"`
  29. BatchNum *common.BatchNum `meddler:"batch_num"` // batchNum in which this tx was forged. If the tx is L2, this must be != 0
  30. EthBlockNum int64 `meddler:"eth_block_num"` // Ethereum Block Number in which this L1Tx was added to the queue
  31. // L1
  32. ToForgeL1TxsNum *int64 `meddler:"to_forge_l1_txs_num"` // toForgeL1TxsNum in which the tx was forged / will be forged
  33. UserOrigin *bool `meddler:"user_origin"` // true if the tx was originated by a user, false if it was aoriginated by a coordinator. Note that this differ from the spec for implementation simplification purpposes
  34. DepositAmount *apitypes.BigIntStr `meddler:"deposit_amount"`
  35. HistoricDepositAmountUSD *float64 `meddler:"deposit_amount_usd"`
  36. // L2
  37. Fee *common.FeeSelector `meddler:"fee"`
  38. HistoricFeeUSD *float64 `meddler:"fee_usd"`
  39. Nonce *common.Nonce `meddler:"nonce"`
  40. // API extras
  41. Timestamp time.Time `meddler:"timestamp,utctime"`
  42. TotalItems uint64 `meddler:"total_items"`
  43. FirstItem uint64 `meddler:"first_item"`
  44. LastItem uint64 `meddler:"last_item"`
  45. TokenID common.TokenID `meddler:"token_id"`
  46. TokenItemID uint64 `meddler:"token_item_id"`
  47. TokenEthBlockNum int64 `meddler:"token_block"`
  48. TokenEthAddr ethCommon.Address `meddler:"eth_addr"`
  49. TokenName string `meddler:"name"`
  50. TokenSymbol string `meddler:"symbol"`
  51. TokenDecimals uint64 `meddler:"decimals"`
  52. TokenUSD *float64 `meddler:"usd"`
  53. TokenUSDUpdate *time.Time `meddler:"usd_update"`
  54. }
  55. // MarshalJSON is used to neast some of the fields of TxAPI
  56. // without the need of auxiliar structs
  57. func (tx TxAPI) MarshalJSON() ([]byte, error) {
  58. jsonTx := map[string]interface{}{
  59. "id": tx.TxID,
  60. "itemId": tx.ItemID,
  61. "type": tx.Type,
  62. "position": tx.Position,
  63. "fromAccountIndex": tx.FromIdx,
  64. "fromHezEthereumAddress": tx.FromEthAddr,
  65. "fromBJJ": tx.FromBJJ,
  66. "toAccountIndex": tx.ToIdx,
  67. "toHezEthereumAddress": tx.ToEthAddr,
  68. "toBJJ": tx.ToBJJ,
  69. "amount": tx.Amount,
  70. "batchNum": tx.BatchNum,
  71. "historicUSD": tx.HistoricUSD,
  72. "timestamp": tx.Timestamp,
  73. "L1Info": nil,
  74. "L2Info": nil,
  75. "token": map[string]interface{}{
  76. "id": tx.TokenID,
  77. "itemId": tx.TokenItemID,
  78. "ethereumBlockNum": tx.TokenEthBlockNum,
  79. "ethereumAddress": tx.TokenEthAddr,
  80. "name": tx.TokenName,
  81. "symbol": tx.TokenSymbol,
  82. "decimals": tx.TokenDecimals,
  83. "USD": tx.TokenUSD,
  84. "fiatUpdate": tx.TokenUSDUpdate,
  85. },
  86. }
  87. if tx.IsL1 {
  88. jsonTx["L1orL2"] = "L1"
  89. jsonTx["L1Info"] = map[string]interface{}{
  90. "toForgeL1TransactionsNum": tx.ToForgeL1TxsNum,
  91. "userOrigin": tx.UserOrigin,
  92. "depositAmount": tx.DepositAmount,
  93. "historicDepositAmountUSD": tx.HistoricDepositAmountUSD,
  94. "ethereumBlockNum": tx.EthBlockNum,
  95. }
  96. } else {
  97. jsonTx["L1orL2"] = "L2"
  98. jsonTx["L2Info"] = map[string]interface{}{
  99. "fee": tx.Fee,
  100. "historicFeeUSD": tx.HistoricFeeUSD,
  101. "nonce": tx.Nonce,
  102. }
  103. }
  104. return json.Marshal(jsonTx)
  105. }
  106. // txWrite is an representatiion that merges common.L1Tx and common.L2Tx
  107. // in order to perform inserts into tx table
  108. // EffectiveAmount and LoadEffectiveAmount are not set since they have default values in the DB
  109. type txWrite struct {
  110. // Generic
  111. IsL1 bool `meddler:"is_l1"`
  112. TxID common.TxID `meddler:"id"`
  113. Type common.TxType `meddler:"type"`
  114. Position int `meddler:"position"`
  115. FromIdx *common.Idx `meddler:"from_idx"`
  116. ToIdx common.Idx `meddler:"to_idx"`
  117. Amount *big.Int `meddler:"amount,bigint"`
  118. AmountFloat float64 `meddler:"amount_f"`
  119. TokenID common.TokenID `meddler:"token_id"`
  120. BatchNum *common.BatchNum `meddler:"batch_num"` // batchNum in which this tx was forged. If the tx is L2, this must be != 0
  121. EthBlockNum int64 `meddler:"eth_block_num"` // Ethereum Block Number in which this L1Tx was added to the queue
  122. // L1
  123. ToForgeL1TxsNum *int64 `meddler:"to_forge_l1_txs_num"` // toForgeL1TxsNum in which the tx was forged / will be forged
  124. UserOrigin *bool `meddler:"user_origin"` // true if the tx was originated by a user, false if it was aoriginated by a coordinator. Note that this differ from the spec for implementation simplification purpposes
  125. FromEthAddr *ethCommon.Address `meddler:"from_eth_addr"`
  126. FromBJJ *babyjub.PublicKey `meddler:"from_bjj"`
  127. DepositAmount *big.Int `meddler:"deposit_amount,bigintnull"`
  128. DepositAmountFloat *float64 `meddler:"deposit_amount_f"`
  129. // L2
  130. Fee *common.FeeSelector `meddler:"fee"`
  131. Nonce *common.Nonce `meddler:"nonce"`
  132. }
  133. // TokenWithUSD add USD info to common.Token
  134. type TokenWithUSD struct {
  135. ItemID uint64 `json:"itemId" meddler:"item_id"`
  136. TokenID common.TokenID `json:"id" meddler:"token_id"`
  137. EthBlockNum int64 `json:"ethereumBlockNum" meddler:"eth_block_num"` // Ethereum block number in which this token was registered
  138. EthAddr ethCommon.Address `json:"ethereumAddress" meddler:"eth_addr"`
  139. Name string `json:"name" meddler:"name"`
  140. Symbol string `json:"symbol" meddler:"symbol"`
  141. Decimals uint64 `json:"decimals" meddler:"decimals"`
  142. USD *float64 `json:"USD" meddler:"usd"`
  143. USDUpdate *time.Time `json:"fiatUpdate" meddler:"usd_update,utctime"`
  144. TotalItems uint64 `json:"-" meddler:"total_items"`
  145. FirstItem uint64 `json:"-" meddler:"first_item"`
  146. LastItem uint64 `json:"-" meddler:"last_item"`
  147. }
  148. // ExitAPI is a representation of a exit with additional information
  149. // required by the API, and extracted by joining token table
  150. type ExitAPI struct {
  151. ItemID uint64 `meddler:"item_id"`
  152. BatchNum common.BatchNum `meddler:"batch_num"`
  153. AccountIdx apitypes.HezIdx `meddler:"account_idx"`
  154. MerkleProof *merkletree.CircomVerifierProof `meddler:"merkle_proof,json"`
  155. Balance apitypes.BigIntStr `meddler:"balance"`
  156. InstantWithdrawn *int64 `meddler:"instant_withdrawn"`
  157. DelayedWithdrawRequest *int64 `meddler:"delayed_withdraw_request"`
  158. DelayedWithdrawn *int64 `meddler:"delayed_withdrawn"`
  159. TotalItems uint64 `meddler:"total_items"`
  160. FirstItem uint64 `meddler:"first_item"`
  161. LastItem uint64 `meddler:"last_item"`
  162. TokenID common.TokenID `meddler:"token_id"`
  163. TokenItemID uint64 `meddler:"token_item_id"`
  164. TokenEthBlockNum int64 `meddler:"token_block"`
  165. TokenEthAddr ethCommon.Address `meddler:"eth_addr"`
  166. TokenName string `meddler:"name"`
  167. TokenSymbol string `meddler:"symbol"`
  168. TokenDecimals uint64 `meddler:"decimals"`
  169. TokenUSD *float64 `meddler:"usd"`
  170. TokenUSDUpdate *time.Time `meddler:"usd_update"`
  171. }
  172. // MarshalJSON is used to neast some of the fields of ExitAPI
  173. // without the need of auxiliar structs
  174. func (e ExitAPI) MarshalJSON() ([]byte, error) {
  175. siblings := []string{}
  176. for i := 0; i < len(e.MerkleProof.Siblings); i++ {
  177. siblings = append(siblings, e.MerkleProof.Siblings[i].String())
  178. }
  179. return json.Marshal(map[string]interface{}{
  180. "itemId": e.ItemID,
  181. "batchNum": e.BatchNum,
  182. "accountIndex": e.AccountIdx,
  183. "merkleProof": map[string]interface{}{
  184. "Root": e.MerkleProof.Root.String(),
  185. "Siblings": siblings,
  186. "OldKey": e.MerkleProof.OldKey.String(),
  187. "OldValue": e.MerkleProof.OldValue.String(),
  188. "IsOld0": e.MerkleProof.IsOld0,
  189. "Key": e.MerkleProof.Key.String(),
  190. "Value": e.MerkleProof.Value.String(),
  191. "Fnc": e.MerkleProof.Fnc,
  192. },
  193. "balance": e.Balance,
  194. "instantWithdrawn": e.InstantWithdrawn,
  195. "delayedWithdrawRequest": e.DelayedWithdrawRequest,
  196. "delayedWithdrawn": e.DelayedWithdrawn,
  197. "token": map[string]interface{}{
  198. "id": e.TokenID,
  199. "itemId": e.TokenItemID,
  200. "ethereumBlockNum": e.TokenEthBlockNum,
  201. "ethereumAddress": e.TokenEthAddr,
  202. "name": e.TokenName,
  203. "symbol": e.TokenSymbol,
  204. "decimals": e.TokenDecimals,
  205. "USD": e.TokenUSD,
  206. "fiatUpdate": e.TokenUSDUpdate,
  207. },
  208. })
  209. }
  210. // CoordinatorAPI is a representation of a coordinator with additional information
  211. // required by the API
  212. type CoordinatorAPI struct {
  213. ItemID uint64 `json:"itemId" meddler:"item_id"`
  214. Bidder ethCommon.Address `json:"bidderAddr" meddler:"bidder_addr"`
  215. Forger ethCommon.Address `json:"forgerAddr" meddler:"forger_addr"`
  216. EthBlockNum int64 `json:"ethereumBlock" meddler:"eth_block_num"`
  217. URL string `json:"URL" meddler:"url"`
  218. TotalItems uint64 `json:"-" meddler:"total_items"`
  219. FirstItem uint64 `json:"-" meddler:"first_item"`
  220. LastItem uint64 `json:"-" meddler:"last_item"`
  221. }
  222. // AccountAPI is a representation of a account with additional information
  223. // required by the API
  224. type AccountAPI struct {
  225. ItemID uint64 `meddler:"item_id"`
  226. Idx apitypes.HezIdx `meddler:"idx"`
  227. BatchNum common.BatchNum `meddler:"batch_num"`
  228. PublicKey apitypes.HezBJJ `meddler:"bjj"`
  229. EthAddr apitypes.HezEthAddr `meddler:"eth_addr"`
  230. Nonce common.Nonce `meddler:"-"` // max of 40 bits used
  231. Balance *apitypes.BigIntStr `meddler:"-"` // max of 192 bits used
  232. TotalItems uint64 `meddler:"total_items"`
  233. FirstItem uint64 `meddler:"first_item"`
  234. LastItem uint64 `meddler:"last_item"`
  235. TokenID common.TokenID `meddler:"token_id"`
  236. TokenItemID int `meddler:"token_item_id"`
  237. TokenEthBlockNum int64 `meddler:"token_block"`
  238. TokenEthAddr ethCommon.Address `meddler:"token_eth_addr"`
  239. TokenName string `meddler:"name"`
  240. TokenSymbol string `meddler:"symbol"`
  241. TokenDecimals uint64 `meddler:"decimals"`
  242. TokenUSD *float64 `meddler:"usd"`
  243. TokenUSDUpdate *time.Time `meddler:"usd_update"`
  244. }
  245. // MarshalJSON is used to neast some of the fields of AccountAPI
  246. // without the need of auxiliar structs
  247. func (account AccountAPI) MarshalJSON() ([]byte, error) {
  248. jsonAccount := map[string]interface{}{
  249. "itemId": account.ItemID,
  250. "accountIndex": account.Idx,
  251. "nonce": account.Nonce,
  252. "balance": account.Balance,
  253. "bjj": account.PublicKey,
  254. "hezEthereumAddress": account.EthAddr,
  255. "token": map[string]interface{}{
  256. "id": account.TokenID,
  257. "itemId": account.TokenItemID,
  258. "ethereumBlockNum": account.TokenEthBlockNum,
  259. "ethereumAddress": account.TokenEthAddr,
  260. "name": account.TokenName,
  261. "symbol": account.TokenSymbol,
  262. "decimals": account.TokenDecimals,
  263. "USD": account.TokenUSD,
  264. "fiatUpdate": account.TokenUSDUpdate,
  265. },
  266. }
  267. return json.Marshal(jsonAccount)
  268. }
  269. // BatchAPI is a representation of a batch with additional information
  270. // required by the API, and extracted by joining block table
  271. type BatchAPI struct {
  272. ItemID uint64 `json:"itemId" meddler:"item_id"`
  273. BatchNum common.BatchNum `json:"batchNum" meddler:"batch_num"`
  274. EthBlockNum int64 `json:"ethereumBlockNum" meddler:"eth_block_num"`
  275. EthBlockHash ethCommon.Hash `json:"ethereumBlockHash" meddler:"hash"`
  276. Timestamp time.Time `json:"timestamp" meddler:"timestamp,utctime"`
  277. ForgerAddr ethCommon.Address `json:"forgerAddr" meddler:"forger_addr"`
  278. CollectedFees apitypes.CollectedFees `json:"collectedFees" meddler:"fees_collected,json"`
  279. TotalFeesUSD *float64 `json:"historicTotalCollectedFeesUSD" meddler:"total_fees_usd"`
  280. StateRoot apitypes.BigIntStr `json:"stateRoot" meddler:"state_root"`
  281. NumAccounts int `json:"numAccounts" meddler:"num_accounts"`
  282. ExitRoot apitypes.BigIntStr `json:"exitRoot" meddler:"exit_root"`
  283. ForgeL1TxsNum *int64 `json:"forgeL1TransactionsNum" meddler:"forge_l1_txs_num"`
  284. SlotNum int64 `json:"slotNum" meddler:"slot_num"`
  285. TotalItems uint64 `json:"-" meddler:"total_items"`
  286. FirstItem uint64 `json:"-" meddler:"first_item"`
  287. LastItem uint64 `json:"-" meddler:"last_item"`
  288. }
  289. // Metrics define metrics of the network
  290. type Metrics struct {
  291. TransactionsPerBatch float64 `json:"transactionsPerBatch"`
  292. BatchFrequency float64 `json:"batchFrequency"`
  293. TransactionsPerSecond float64 `json:"transactionsPerSecond"`
  294. TotalAccounts int64 `json:"totalAccounts" meddler:"total_accounts"`
  295. TotalBJJs int64 `json:"totalBJJs" meddler:"total_bjjs"`
  296. AvgTransactionFee float64 `json:"avgTransactionFee"`
  297. }
  298. // MetricsTotals is used to get temporal information from HistoryDB
  299. // to calculate data to be stored into the Metrics struct
  300. type MetricsTotals struct {
  301. TotalTransactions uint64 `meddler:"total_txs"`
  302. FirstBatchNum common.BatchNum `meddler:"batch_num"`
  303. TotalBatches int64 `meddler:"total_batches"`
  304. TotalFeesUSD float64 `meddler:"total_fees"`
  305. }
  306. // BidAPI is a representation of a bid with additional information
  307. // required by the API
  308. type BidAPI struct {
  309. ItemID uint64 `json:"itemId" meddler:"item_id"`
  310. SlotNum int64 `json:"slotNum" meddler:"slot_num"`
  311. BidValue apitypes.BigIntStr `json:"bidValue" meddler:"bid_value"`
  312. EthBlockNum int64 `json:"ethereumBlockNum" meddler:"eth_block_num"`
  313. Bidder ethCommon.Address `json:"bidderAddr" meddler:"bidder_addr"`
  314. Forger ethCommon.Address `json:"forgerAddr" meddler:"forger_addr"`
  315. URL string `json:"URL" meddler:"url"`
  316. Timestamp time.Time `json:"timestamp" meddler:"timestamp,utctime"`
  317. TotalItems uint64 `json:"-" meddler:"total_items"`
  318. FirstItem uint64 `json:"-" meddler:"first_item"`
  319. LastItem uint64 `json:"-" meddler:"last_item"`
  320. }