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.

174 lines
9.6 KiB

  1. package historydb
  2. import (
  3. "math/big"
  4. "time"
  5. ethCommon "github.com/ethereum/go-ethereum/common"
  6. "github.com/hermeznetwork/hermez-node/apitypes"
  7. "github.com/hermeznetwork/hermez-node/common"
  8. "github.com/iden3/go-iden3-crypto/babyjub"
  9. "github.com/iden3/go-merkletree"
  10. )
  11. // HistoryTx is a representation of a generic Tx with additional information
  12. // required by the API, and extracted by joining block and token tables
  13. type HistoryTx struct {
  14. // Generic
  15. IsL1 bool `meddler:"is_l1"`
  16. TxID common.TxID `meddler:"id"`
  17. ItemID int `meddler:"item_id"`
  18. Type common.TxType `meddler:"type"`
  19. Position int `meddler:"position"`
  20. FromIdx *common.Idx `meddler:"from_idx"`
  21. ToIdx common.Idx `meddler:"to_idx"`
  22. Amount *big.Int `meddler:"amount,bigint"`
  23. HistoricUSD *float64 `meddler:"amount_usd"`
  24. BatchNum *common.BatchNum `meddler:"batch_num"` // batchNum in which this tx was forged. If the tx is L2, this must be != 0
  25. EthBlockNum int64 `meddler:"eth_block_num"` // Ethereum Block Number in which this L1Tx was added to the queue
  26. // L1
  27. ToForgeL1TxsNum *int64 `meddler:"to_forge_l1_txs_num"` // toForgeL1TxsNum in which the tx was forged / will be forged
  28. 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
  29. FromEthAddr *ethCommon.Address `meddler:"from_eth_addr"`
  30. FromBJJ *babyjub.PublicKey `meddler:"from_bjj"`
  31. LoadAmount *big.Int `meddler:"load_amount,bigintnull"`
  32. // LoadAmountFloat *float64 `meddler:"load_amount_f"`
  33. HistoricLoadAmountUSD *float64 `meddler:"load_amount_usd"`
  34. // L2
  35. Fee *common.FeeSelector `meddler:"fee"`
  36. HistoricFeeUSD *float64 `meddler:"fee_usd"`
  37. Nonce *common.Nonce `meddler:"nonce"`
  38. // API extras
  39. Timestamp time.Time `meddler:"timestamp,utctime"`
  40. TotalItems int `meddler:"total_items"`
  41. FirstItem int `meddler:"first_item"`
  42. LastItem int `meddler:"last_item"`
  43. TokenID common.TokenID `meddler:"token_id"`
  44. TokenEthBlockNum int64 `meddler:"token_block"`
  45. TokenEthAddr ethCommon.Address `meddler:"eth_addr"`
  46. TokenName string `meddler:"name"`
  47. TokenSymbol string `meddler:"symbol"`
  48. TokenDecimals uint64 `meddler:"decimals"`
  49. TokenUSD *float64 `meddler:"usd"`
  50. TokenUSDUpdate *time.Time `meddler:"usd_update"`
  51. }
  52. // txWrite is an representatiion that merges common.L1Tx and common.L2Tx
  53. // in order to perform inserts into tx table
  54. type txWrite struct {
  55. // Generic
  56. IsL1 bool `meddler:"is_l1"`
  57. TxID common.TxID `meddler:"id"`
  58. Type common.TxType `meddler:"type"`
  59. Position int `meddler:"position"`
  60. FromIdx *common.Idx `meddler:"from_idx"`
  61. ToIdx common.Idx `meddler:"to_idx"`
  62. Amount *big.Int `meddler:"amount,bigint"`
  63. AmountFloat float64 `meddler:"amount_f"`
  64. TokenID common.TokenID `meddler:"token_id"`
  65. BatchNum *common.BatchNum `meddler:"batch_num"` // batchNum in which this tx was forged. If the tx is L2, this must be != 0
  66. EthBlockNum int64 `meddler:"eth_block_num"` // Ethereum Block Number in which this L1Tx was added to the queue
  67. // L1
  68. ToForgeL1TxsNum *int64 `meddler:"to_forge_l1_txs_num"` // toForgeL1TxsNum in which the tx was forged / will be forged
  69. 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
  70. FromEthAddr *ethCommon.Address `meddler:"from_eth_addr"`
  71. FromBJJ *babyjub.PublicKey `meddler:"from_bjj"`
  72. LoadAmount *big.Int `meddler:"load_amount,bigintnull"`
  73. LoadAmountFloat *float64 `meddler:"load_amount_f"`
  74. // L2
  75. Fee *common.FeeSelector `meddler:"fee"`
  76. Nonce *common.Nonce `meddler:"nonce"`
  77. }
  78. // TokenWithUSD add USD info to common.Token
  79. type TokenWithUSD struct {
  80. ItemID int `json:"itemId" meddler:"item_id"`
  81. TokenID common.TokenID `json:"id" meddler:"token_id"`
  82. EthBlockNum int64 `json:"ethereumBlockNum" meddler:"eth_block_num"` // Ethereum block number in which this token was registered
  83. EthAddr ethCommon.Address `json:"ethereumAddress" meddler:"eth_addr"`
  84. Name string `json:"name" meddler:"name"`
  85. Symbol string `json:"symbol" meddler:"symbol"`
  86. Decimals uint64 `json:"decimals" meddler:"decimals"`
  87. USD *float64 `json:"USD" meddler:"usd"`
  88. USDUpdate *time.Time `json:"fiatUpdate" meddler:"usd_update,utctime"`
  89. TotalItems int `json:"-" meddler:"total_items"`
  90. FirstItem int `json:"-" meddler:"first_item"`
  91. LastItem int `json:"-" meddler:"last_item"`
  92. }
  93. // HistoryExit is a representation of a exit with additional information
  94. // required by the API, and extracted by joining token table
  95. type HistoryExit struct {
  96. ItemID int `meddler:"item_id"`
  97. BatchNum common.BatchNum `meddler:"batch_num"`
  98. AccountIdx common.Idx `meddler:"account_idx"`
  99. MerkleProof *merkletree.CircomVerifierProof `meddler:"merkle_proof,json"`
  100. Balance *big.Int `meddler:"balance,bigint"`
  101. InstantWithdrawn *int64 `meddler:"instant_withdrawn"`
  102. DelayedWithdrawRequest *int64 `meddler:"delayed_withdraw_request"`
  103. DelayedWithdrawn *int64 `meddler:"delayed_withdrawn"`
  104. TotalItems int `meddler:"total_items"`
  105. FirstItem int `meddler:"first_item"`
  106. LastItem int `meddler:"last_item"`
  107. TokenID common.TokenID `meddler:"token_id"`
  108. TokenEthBlockNum int64 `meddler:"token_block"`
  109. TokenEthAddr ethCommon.Address `meddler:"eth_addr"`
  110. TokenName string `meddler:"name"`
  111. TokenSymbol string `meddler:"symbol"`
  112. TokenDecimals uint64 `meddler:"decimals"`
  113. TokenUSD *float64 `meddler:"usd"`
  114. TokenUSDUpdate *time.Time `meddler:"usd_update"`
  115. }
  116. // CoordinatorAPI is a representation of a coordinator with additional information
  117. // required by the API
  118. type CoordinatorAPI struct {
  119. ItemID int `json:"itemId" meddler:"item_id"`
  120. Bidder ethCommon.Address `json:"bidderAddr" meddler:"bidder_addr"`
  121. Forger ethCommon.Address `json:"forgerAddr" meddler:"forger_addr"`
  122. EthBlockNum int64 `json:"ethereumBlock" meddler:"eth_block_num"`
  123. URL string `json:"URL" meddler:"url"`
  124. TotalItems int `json:"-" meddler:"total_items"`
  125. FirstItem int `json:"-" meddler:"first_item"`
  126. LastItem int `json:"-" meddler:"last_item"`
  127. }
  128. // BatchAPI is a representation of a batch with additional information
  129. // required by the API, and extracted by joining block table
  130. type BatchAPI struct {
  131. ItemID int `json:"itemId" meddler:"item_id"`
  132. BatchNum common.BatchNum `json:"batchNum" meddler:"batch_num"`
  133. EthBlockNum int64 `json:"ethereumBlockNum" meddler:"eth_block_num"`
  134. EthBlockHash ethCommon.Hash `json:"ethereumBlockHash" meddler:"hash"`
  135. Timestamp time.Time `json:"timestamp" meddler:"timestamp,utctime"`
  136. ForgerAddr ethCommon.Address `json:"forgerAddr" meddler:"forger_addr"`
  137. CollectedFees apitypes.CollectedFees `json:"collectedFees" meddler:"fees_collected,json"`
  138. // CollectedFees map[common.TokenID]*big.Int `json:"collectedFees" meddler:"fees_collected,json"`
  139. TotalFeesUSD *float64 `json:"historicTotalCollectedFeesUSD" meddler:"total_fees_usd"`
  140. StateRoot apitypes.BigIntStr `json:"stateRoot" meddler:"state_root"`
  141. NumAccounts int `json:"numAccounts" meddler:"num_accounts"`
  142. ExitRoot apitypes.BigIntStr `json:"exitRoot" meddler:"exit_root"`
  143. ForgeL1TxsNum *int64 `json:"forgeL1TransactionsNum" meddler:"forge_l1_txs_num"`
  144. SlotNum int64 `json:"slotNum" meddler:"slot_num"`
  145. TotalItems int `json:"-" meddler:"total_items"`
  146. FirstItem int `json:"-" meddler:"first_item"`
  147. LastItem int `json:"-" meddler:"last_item"`
  148. }
  149. // Network define status of the network
  150. type Network struct {
  151. LastBlock int64 `json:"lastBlock"`
  152. LastBatch BatchAPI `json:"lastBatch"`
  153. CurrentSlot int64 `json:"currentSlot"`
  154. NextForgers []CoordinatorAPI `json:"nextForgers"`
  155. }
  156. // Metrics define metrics of the network
  157. type Metrics struct {
  158. TransactionsPerBatch float64 `json:"transactionsPerBatch"`
  159. BatchFrequency float64 `json:"batchFrequency"`
  160. TransactionsPerSecond float64 `json:"transactionsPerSecond"`
  161. TotalAccounts int64 `json:"totalAccounts"`
  162. TotalBJJs int64 `json:"totalBJJs"`
  163. AvgTransactionFee float64 `json:"avgTransactionFee"`
  164. }