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.

399 lines
21 KiB

4 years ago
4 years ago
4 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. AmountSuccess bool `meddler:"amount_success"`
  37. DepositAmountSuccess bool `meddler:"deposit_amount_success"`
  38. // L2
  39. Fee *common.FeeSelector `meddler:"fee"`
  40. HistoricFeeUSD *float64 `meddler:"fee_usd"`
  41. Nonce *common.Nonce `meddler:"nonce"`
  42. // API extras
  43. Timestamp time.Time `meddler:"timestamp,utctime"`
  44. TotalItems uint64 `meddler:"total_items"`
  45. FirstItem uint64 `meddler:"first_item"`
  46. LastItem uint64 `meddler:"last_item"`
  47. TokenID common.TokenID `meddler:"token_id"`
  48. TokenItemID uint64 `meddler:"token_item_id"`
  49. TokenEthBlockNum int64 `meddler:"token_block"`
  50. TokenEthAddr ethCommon.Address `meddler:"eth_addr"`
  51. TokenName string `meddler:"name"`
  52. TokenSymbol string `meddler:"symbol"`
  53. TokenDecimals uint64 `meddler:"decimals"`
  54. TokenUSD *float64 `meddler:"usd"`
  55. TokenUSDUpdate *time.Time `meddler:"usd_update"`
  56. }
  57. // MarshalJSON is used to neast some of the fields of TxAPI
  58. // without the need of auxiliar structs
  59. func (tx TxAPI) MarshalJSON() ([]byte, error) {
  60. jsonTx := map[string]interface{}{
  61. "id": tx.TxID,
  62. "itemId": tx.ItemID,
  63. "type": tx.Type,
  64. "position": tx.Position,
  65. "fromAccountIndex": tx.FromIdx,
  66. "fromHezEthereumAddress": tx.FromEthAddr,
  67. "fromBJJ": tx.FromBJJ,
  68. "toAccountIndex": tx.ToIdx,
  69. "toHezEthereumAddress": tx.ToEthAddr,
  70. "toBJJ": tx.ToBJJ,
  71. "amount": tx.Amount,
  72. "batchNum": tx.BatchNum,
  73. "historicUSD": tx.HistoricUSD,
  74. "timestamp": tx.Timestamp,
  75. "L1Info": nil,
  76. "L2Info": nil,
  77. "token": map[string]interface{}{
  78. "id": tx.TokenID,
  79. "itemId": tx.TokenItemID,
  80. "ethereumBlockNum": tx.TokenEthBlockNum,
  81. "ethereumAddress": tx.TokenEthAddr,
  82. "name": tx.TokenName,
  83. "symbol": tx.TokenSymbol,
  84. "decimals": tx.TokenDecimals,
  85. "USD": tx.TokenUSD,
  86. "fiatUpdate": tx.TokenUSDUpdate,
  87. },
  88. }
  89. if tx.IsL1 {
  90. jsonTx["L1orL2"] = "L1"
  91. amountSuccess := tx.AmountSuccess
  92. depositAmountSuccess := tx.DepositAmountSuccess
  93. if tx.BatchNum == nil {
  94. amountSuccess = false
  95. depositAmountSuccess = false
  96. }
  97. jsonTx["L1Info"] = map[string]interface{}{
  98. "toForgeL1TransactionsNum": tx.ToForgeL1TxsNum,
  99. "userOrigin": tx.UserOrigin,
  100. "depositAmount": tx.DepositAmount,
  101. "amountSuccess": amountSuccess,
  102. "depositAmountSuccess": depositAmountSuccess,
  103. "historicDepositAmountUSD": tx.HistoricDepositAmountUSD,
  104. "ethereumBlockNum": tx.EthBlockNum,
  105. }
  106. } else {
  107. jsonTx["L1orL2"] = "L2"
  108. jsonTx["L2Info"] = map[string]interface{}{
  109. "fee": tx.Fee,
  110. "historicFeeUSD": tx.HistoricFeeUSD,
  111. "nonce": tx.Nonce,
  112. }
  113. }
  114. return json.Marshal(jsonTx)
  115. }
  116. // txWrite is an representatiion that merges common.L1Tx and common.L2Tx
  117. // in order to perform inserts into tx table
  118. // EffectiveAmount and EffectiveDepositAmount are not set since they have default values in the DB
  119. type txWrite struct {
  120. // Generic
  121. IsL1 bool `meddler:"is_l1"`
  122. TxID common.TxID `meddler:"id"`
  123. Type common.TxType `meddler:"type"`
  124. Position int `meddler:"position"`
  125. FromIdx *common.Idx `meddler:"from_idx"`
  126. EffectiveFromIdx *common.Idx `meddler:"effective_from_idx"`
  127. ToIdx common.Idx `meddler:"to_idx"`
  128. Amount *big.Int `meddler:"amount,bigint"`
  129. AmountFloat float64 `meddler:"amount_f"`
  130. TokenID common.TokenID `meddler:"token_id"`
  131. BatchNum *common.BatchNum `meddler:"batch_num"` // batchNum in which this tx was forged. If the tx is L2, this must be != 0
  132. EthBlockNum int64 `meddler:"eth_block_num"` // Ethereum Block Number in which this L1Tx was added to the queue
  133. // L1
  134. ToForgeL1TxsNum *int64 `meddler:"to_forge_l1_txs_num"` // toForgeL1TxsNum in which the tx was forged / will be forged
  135. 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
  136. FromEthAddr *ethCommon.Address `meddler:"from_eth_addr"`
  137. FromBJJ *babyjub.PublicKeyComp `meddler:"from_bjj"`
  138. DepositAmount *big.Int `meddler:"deposit_amount,bigintnull"`
  139. DepositAmountFloat *float64 `meddler:"deposit_amount_f"`
  140. // L2
  141. Fee *common.FeeSelector `meddler:"fee"`
  142. Nonce *common.Nonce `meddler:"nonce"`
  143. }
  144. // TokenWithUSD add USD info to common.Token
  145. type TokenWithUSD struct {
  146. ItemID uint64 `json:"itemId" meddler:"item_id"`
  147. TokenID common.TokenID `json:"id" meddler:"token_id"`
  148. EthBlockNum int64 `json:"ethereumBlockNum" meddler:"eth_block_num"` // Ethereum block number in which this token was registered
  149. EthAddr ethCommon.Address `json:"ethereumAddress" meddler:"eth_addr"`
  150. Name string `json:"name" meddler:"name"`
  151. Symbol string `json:"symbol" meddler:"symbol"`
  152. Decimals uint64 `json:"decimals" meddler:"decimals"`
  153. USD *float64 `json:"USD" meddler:"usd"`
  154. USDUpdate *time.Time `json:"fiatUpdate" meddler:"usd_update,utctime"`
  155. TotalItems uint64 `json:"-" meddler:"total_items"`
  156. FirstItem uint64 `json:"-" meddler:"first_item"`
  157. LastItem uint64 `json:"-" meddler:"last_item"`
  158. }
  159. // ExitAPI is a representation of a exit with additional information
  160. // required by the API, and extracted by joining token table
  161. type ExitAPI struct {
  162. ItemID uint64 `meddler:"item_id"`
  163. BatchNum common.BatchNum `meddler:"batch_num"`
  164. AccountIdx apitypes.HezIdx `meddler:"account_idx"`
  165. EthAddr *apitypes.HezEthAddr `meddler:"eth_addr"`
  166. BJJ *apitypes.HezBJJ `meddler:"bjj"`
  167. MerkleProof *merkletree.CircomVerifierProof `meddler:"merkle_proof,json"`
  168. Balance apitypes.BigIntStr `meddler:"balance"`
  169. InstantWithdrawn *int64 `meddler:"instant_withdrawn"`
  170. DelayedWithdrawRequest *int64 `meddler:"delayed_withdraw_request"`
  171. DelayedWithdrawn *int64 `meddler:"delayed_withdrawn"`
  172. TotalItems uint64 `meddler:"total_items"`
  173. FirstItem uint64 `meddler:"first_item"`
  174. LastItem uint64 `meddler:"last_item"`
  175. TokenID common.TokenID `meddler:"token_id"`
  176. TokenItemID uint64 `meddler:"token_item_id"`
  177. TokenEthBlockNum int64 `meddler:"token_block"`
  178. TokenEthAddr ethCommon.Address `meddler:"token_eth_addr"`
  179. TokenName string `meddler:"name"`
  180. TokenSymbol string `meddler:"symbol"`
  181. TokenDecimals uint64 `meddler:"decimals"`
  182. TokenUSD *float64 `meddler:"usd"`
  183. TokenUSDUpdate *time.Time `meddler:"usd_update"`
  184. }
  185. // MarshalJSON is used to neast some of the fields of ExitAPI
  186. // without the need of auxiliar structs
  187. func (e ExitAPI) MarshalJSON() ([]byte, error) {
  188. return json.Marshal(map[string]interface{}{
  189. "itemId": e.ItemID,
  190. "batchNum": e.BatchNum,
  191. "accountIndex": e.AccountIdx,
  192. "bjj": e.BJJ,
  193. "hezEthereumAddress": e.EthAddr,
  194. "merkleProof": e.MerkleProof,
  195. "balance": e.Balance,
  196. "instantWithdraw": e.InstantWithdrawn,
  197. "delayedWithdrawRequest": e.DelayedWithdrawRequest,
  198. "delayedWithdraw": e.DelayedWithdrawn,
  199. "token": map[string]interface{}{
  200. "id": e.TokenID,
  201. "itemId": e.TokenItemID,
  202. "ethereumBlockNum": e.TokenEthBlockNum,
  203. "ethereumAddress": e.TokenEthAddr,
  204. "name": e.TokenName,
  205. "symbol": e.TokenSymbol,
  206. "decimals": e.TokenDecimals,
  207. "USD": e.TokenUSD,
  208. "fiatUpdate": e.TokenUSDUpdate,
  209. },
  210. })
  211. }
  212. // CoordinatorAPI is a representation of a coordinator with additional information
  213. // required by the API
  214. type CoordinatorAPI struct {
  215. ItemID uint64 `json:"itemId" meddler:"item_id"`
  216. Bidder ethCommon.Address `json:"bidderAddr" meddler:"bidder_addr"`
  217. Forger ethCommon.Address `json:"forgerAddr" meddler:"forger_addr"`
  218. EthBlockNum int64 `json:"ethereumBlock" meddler:"eth_block_num"`
  219. URL string `json:"URL" meddler:"url"`
  220. TotalItems uint64 `json:"-" meddler:"total_items"`
  221. FirstItem uint64 `json:"-" meddler:"first_item"`
  222. LastItem uint64 `json:"-" meddler:"last_item"`
  223. }
  224. // AccountAPI is a representation of a account with additional information
  225. // required by the API
  226. type AccountAPI struct {
  227. ItemID uint64 `meddler:"item_id"`
  228. Idx apitypes.HezIdx `meddler:"idx"`
  229. BatchNum common.BatchNum `meddler:"batch_num"`
  230. PublicKey apitypes.HezBJJ `meddler:"bjj"`
  231. EthAddr apitypes.HezEthAddr `meddler:"eth_addr"`
  232. Nonce common.Nonce `meddler:"-"` // max of 40 bits used
  233. Balance *apitypes.BigIntStr `meddler:"-"` // max of 192 bits used
  234. TotalItems uint64 `meddler:"total_items"`
  235. FirstItem uint64 `meddler:"first_item"`
  236. LastItem uint64 `meddler:"last_item"`
  237. TokenID common.TokenID `meddler:"token_id"`
  238. TokenItemID int `meddler:"token_item_id"`
  239. TokenEthBlockNum int64 `meddler:"token_block"`
  240. TokenEthAddr ethCommon.Address `meddler:"token_eth_addr"`
  241. TokenName string `meddler:"name"`
  242. TokenSymbol string `meddler:"symbol"`
  243. TokenDecimals uint64 `meddler:"decimals"`
  244. TokenUSD *float64 `meddler:"usd"`
  245. TokenUSDUpdate *time.Time `meddler:"usd_update"`
  246. }
  247. // MarshalJSON is used to neast some of the fields of AccountAPI
  248. // without the need of auxiliar structs
  249. func (account AccountAPI) MarshalJSON() ([]byte, error) {
  250. jsonAccount := map[string]interface{}{
  251. "itemId": account.ItemID,
  252. "accountIndex": account.Idx,
  253. "nonce": account.Nonce,
  254. "balance": account.Balance,
  255. "bjj": account.PublicKey,
  256. "hezEthereumAddress": account.EthAddr,
  257. "token": map[string]interface{}{
  258. "id": account.TokenID,
  259. "itemId": account.TokenItemID,
  260. "ethereumBlockNum": account.TokenEthBlockNum,
  261. "ethereumAddress": account.TokenEthAddr,
  262. "name": account.TokenName,
  263. "symbol": account.TokenSymbol,
  264. "decimals": account.TokenDecimals,
  265. "USD": account.TokenUSD,
  266. "fiatUpdate": account.TokenUSDUpdate,
  267. },
  268. }
  269. return json.Marshal(jsonAccount)
  270. }
  271. // BatchAPI is a representation of a batch with additional information
  272. // required by the API, and extracted by joining block table
  273. type BatchAPI struct {
  274. ItemID uint64 `json:"itemId" meddler:"item_id"`
  275. BatchNum common.BatchNum `json:"batchNum" meddler:"batch_num"`
  276. EthBlockNum int64 `json:"ethereumBlockNum" meddler:"eth_block_num"`
  277. EthBlockHash ethCommon.Hash `json:"ethereumBlockHash" meddler:"hash"`
  278. Timestamp time.Time `json:"timestamp" meddler:"timestamp,utctime"`
  279. ForgerAddr ethCommon.Address `json:"forgerAddr" meddler:"forger_addr"`
  280. CollectedFees apitypes.CollectedFees `json:"collectedFees" meddler:"fees_collected,json"`
  281. TotalFeesUSD *float64 `json:"historicTotalCollectedFeesUSD" meddler:"total_fees_usd"`
  282. StateRoot apitypes.BigIntStr `json:"stateRoot" meddler:"state_root"`
  283. NumAccounts int `json:"numAccounts" meddler:"num_accounts"`
  284. ExitRoot apitypes.BigIntStr `json:"exitRoot" meddler:"exit_root"`
  285. ForgeL1TxsNum *int64 `json:"forgeL1TransactionsNum" meddler:"forge_l1_txs_num"`
  286. SlotNum int64 `json:"slotNum" meddler:"slot_num"`
  287. ForgedTxs int `json:"forgedTransactions" meddler:"forged_txs"`
  288. TotalItems uint64 `json:"-" meddler:"total_items"`
  289. FirstItem uint64 `json:"-" meddler:"first_item"`
  290. LastItem uint64 `json:"-" meddler:"last_item"`
  291. }
  292. // Metrics define metrics of the network
  293. type Metrics struct {
  294. TransactionsPerBatch float64 `json:"transactionsPerBatch"`
  295. BatchFrequency float64 `json:"batchFrequency"`
  296. TransactionsPerSecond float64 `json:"transactionsPerSecond"`
  297. TotalAccounts int64 `json:"totalAccounts" meddler:"total_accounts"`
  298. TotalBJJs int64 `json:"totalBJJs" meddler:"total_bjjs"`
  299. AvgTransactionFee float64 `json:"avgTransactionFee"`
  300. }
  301. // MetricsTotals is used to get temporal information from HistoryDB
  302. // to calculate data to be stored into the Metrics struct
  303. type MetricsTotals struct {
  304. TotalTransactions uint64 `meddler:"total_txs"`
  305. FirstBatchNum common.BatchNum `meddler:"batch_num"`
  306. TotalBatches int64 `meddler:"total_batches"`
  307. TotalFeesUSD float64 `meddler:"total_fees"`
  308. MinTimestamp time.Time `meddler:"min_timestamp,utctime"`
  309. MaxTimestamp time.Time `meddler:"max_timestamp,utctime"`
  310. }
  311. // BidAPI is a representation of a bid with additional information
  312. // required by the API
  313. type BidAPI struct {
  314. ItemID uint64 `json:"itemId" meddler:"item_id"`
  315. SlotNum int64 `json:"slotNum" meddler:"slot_num"`
  316. BidValue apitypes.BigIntStr `json:"bidValue" meddler:"bid_value"`
  317. EthBlockNum int64 `json:"ethereumBlockNum" meddler:"eth_block_num"`
  318. Bidder ethCommon.Address `json:"bidderAddr" meddler:"bidder_addr"`
  319. Forger ethCommon.Address `json:"forgerAddr" meddler:"forger_addr"`
  320. URL string `json:"URL" meddler:"url"`
  321. Timestamp time.Time `json:"timestamp" meddler:"timestamp,utctime"`
  322. TotalItems uint64 `json:"-" meddler:"total_items"`
  323. FirstItem uint64 `json:"-" meddler:"first_item"`
  324. LastItem uint64 `json:"-" meddler:"last_item"`
  325. }
  326. // MinBidInfo gives information of the minum bid for specific slot(s)
  327. type MinBidInfo struct {
  328. DefaultSlotSetBid [6]*big.Int `json:"defaultSlotSetBid" meddler:"default_slot_set_bid,json" validate:"required"`
  329. DefaultSlotSetBidSlotNum int64 `json:"-" meddler:"default_slot_set_bid_slot_num"`
  330. }
  331. // BucketUpdateAPI are the bucket updates (tracking the withdrawals value changes)
  332. // in Rollup Smart Contract
  333. type BucketUpdateAPI struct {
  334. EthBlockNum int64 `json:"ethereumBlockNum" meddler:"eth_block_num"`
  335. NumBucket int `json:"numBucket" meddler:"num_bucket"`
  336. BlockStamp int64 `json:"blockStamp" meddler:"block_stamp"`
  337. Withdrawals *apitypes.BigIntStr `json:"withdrawals" meddler:"withdrawals"`
  338. }
  339. // BucketParamsAPI are the parameter variables of each Bucket of Rollup Smart
  340. // Contract
  341. type BucketParamsAPI struct {
  342. CeilUSD *apitypes.BigIntStr `json:"ceilUSD"`
  343. Withdrawals *apitypes.BigIntStr `json:"withdrawals"`
  344. BlockWithdrawalRate *apitypes.BigIntStr `json:"blockWithdrawalRate"`
  345. MaxWithdrawals *apitypes.BigIntStr `json:"maxWithdrawals"`
  346. }
  347. // RollupVariablesAPI are the variables of the Rollup Smart Contract
  348. type RollupVariablesAPI struct {
  349. EthBlockNum int64 `json:"ethereumBlockNum" meddler:"eth_block_num"`
  350. FeeAddToken *apitypes.BigIntStr `json:"feeAddToken" meddler:"fee_add_token" validate:"required"`
  351. ForgeL1L2BatchTimeout int64 `json:"forgeL1L2BatchTimeout" meddler:"forge_l1_timeout" validate:"required"`
  352. WithdrawalDelay uint64 `json:"withdrawalDelay" meddler:"withdrawal_delay" validate:"required"`
  353. Buckets [common.RollupConstNumBuckets]BucketParamsAPI `json:"buckets" meddler:"buckets,json"`
  354. SafeMode bool `json:"safeMode" meddler:"safe_mode"`
  355. }
  356. // AuctionVariablesAPI are the variables of the Auction Smart Contract
  357. type AuctionVariablesAPI struct {
  358. EthBlockNum int64 `json:"ethereumBlockNum" meddler:"eth_block_num"`
  359. // DonationAddress Address where the donations will be sent
  360. DonationAddress ethCommon.Address `json:"donationAddress" meddler:"donation_address" validate:"required"`
  361. // BootCoordinator Address of the boot coordinator
  362. BootCoordinator ethCommon.Address `json:"bootCoordinator" meddler:"boot_coordinator" validate:"required"`
  363. // BootCoordinatorURL URL of the boot coordinator
  364. BootCoordinatorURL string `json:"bootCoordinatorUrl" meddler:"boot_coordinator_url" validate:"required"`
  365. // DefaultSlotSetBid The minimum bid value in a series of 6 slots
  366. DefaultSlotSetBid [6]*apitypes.BigIntStr `json:"defaultSlotSetBid" meddler:"default_slot_set_bid,json" validate:"required"`
  367. // DefaultSlotSetBidSlotNum SlotNum at which the new default_slot_set_bid applies
  368. DefaultSlotSetBidSlotNum int64 `json:"defaultSlotSetBidSlotNum" meddler:"default_slot_set_bid_slot_num"`
  369. // ClosedAuctionSlots Distance (#slots) to the closest slot to which you can bid ( 2 Slots = 2 * 40 Blocks = 20 min )
  370. ClosedAuctionSlots uint16 `json:"closedAuctionSlots" meddler:"closed_auction_slots" validate:"required"`
  371. // OpenAuctionSlots Distance (#slots) to the farthest slot to which you can bid (30 days = 4320 slots )
  372. OpenAuctionSlots uint16 `json:"openAuctionSlots" meddler:"open_auction_slots" validate:"required"`
  373. // AllocationRatio How the HEZ tokens deposited by the slot winner are distributed (Burn: 40% - Donation: 40% - HGT: 20%)
  374. AllocationRatio [3]uint16 `json:"allocationRatio" meddler:"allocation_ratio,json" validate:"required"`
  375. // Outbidding Minimum outbid (percentage) over the previous one to consider it valid
  376. Outbidding uint16 `json:"outbidding" meddler:"outbidding" validate:"required"`
  377. // SlotDeadline Number of blocks at the end of a slot in which any coordinator can forge if the winner has not forged one before
  378. SlotDeadline uint8 `json:"slotDeadline" meddler:"slot_deadline" validate:"required"`
  379. }