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.

85 lines
3.0 KiB

  1. package l2db
  2. import (
  3. ethCommon "github.com/ethereum/go-ethereum/common"
  4. "github.com/hermeznetwork/hermez-node/common"
  5. "github.com/hermeznetwork/tracerr"
  6. "github.com/russross/meddler"
  7. )
  8. // AddAccountCreationAuthAPI inserts an account creation authorization into the DB
  9. func (l2db *L2DB) AddAccountCreationAuthAPI(auth *common.AccountCreationAuth) error {
  10. cancel, err := l2db.apiConnCon.Acquire()
  11. defer cancel()
  12. if err != nil {
  13. return tracerr.Wrap(err)
  14. }
  15. defer l2db.apiConnCon.Release()
  16. return l2db.AddAccountCreationAuth(auth)
  17. }
  18. // GetAccountCreationAuthAPI returns an account creation authorization from the DB
  19. func (l2db *L2DB) GetAccountCreationAuthAPI(addr ethCommon.Address) (*AccountCreationAuthAPI, error) {
  20. cancel, err := l2db.apiConnCon.Acquire()
  21. defer cancel()
  22. if err != nil {
  23. return nil, tracerr.Wrap(err)
  24. }
  25. defer l2db.apiConnCon.Release()
  26. auth := new(AccountCreationAuthAPI)
  27. return auth, tracerr.Wrap(meddler.QueryRow(
  28. l2db.db, auth,
  29. "SELECT * FROM account_creation_auth WHERE eth_addr = $1;",
  30. addr,
  31. ))
  32. }
  33. // AddTxAPI inserts a tx to the pool
  34. func (l2db *L2DB) AddTxAPI(tx *PoolL2TxWrite) error {
  35. cancel, err := l2db.apiConnCon.Acquire()
  36. defer cancel()
  37. if err != nil {
  38. return tracerr.Wrap(err)
  39. }
  40. defer l2db.apiConnCon.Release()
  41. row := l2db.db.QueryRow(
  42. "SELECT COUNT(*) FROM tx_pool WHERE state = $1;",
  43. common.PoolL2TxStatePending,
  44. )
  45. var totalTxs uint32
  46. if err := row.Scan(&totalTxs); err != nil {
  47. return tracerr.Wrap(err)
  48. }
  49. if totalTxs >= l2db.maxTxs {
  50. return tracerr.New(
  51. "The pool is at full capacity. More transactions are not accepted currently",
  52. )
  53. }
  54. return tracerr.Wrap(meddler.Insert(l2db.db, "tx_pool", tx))
  55. }
  56. // selectPoolTxAPI select part of queries to get PoolL2TxRead
  57. const selectPoolTxAPI = `SELECT tx_pool.tx_id, hez_idx(tx_pool.from_idx, token.symbol) AS from_idx, tx_pool.effective_from_eth_addr,
  58. tx_pool.effective_from_bjj, hez_idx(tx_pool.to_idx, token.symbol) AS to_idx, tx_pool.effective_to_eth_addr,
  59. tx_pool.effective_to_bjj, tx_pool.token_id, tx_pool.amount, tx_pool.fee, tx_pool.nonce,
  60. tx_pool.state, tx_pool.info, tx_pool.signature, tx_pool.timestamp, tx_pool.batch_num, hez_idx(tx_pool.rq_from_idx, token.symbol) AS rq_from_idx,
  61. hez_idx(tx_pool.rq_to_idx, token.symbol) AS rq_to_idx, tx_pool.rq_to_eth_addr, tx_pool.rq_to_bjj, tx_pool.rq_token_id, tx_pool.rq_amount,
  62. tx_pool.rq_fee, tx_pool.rq_nonce, tx_pool.tx_type,
  63. token.item_id AS token_item_id, token.eth_block_num, token.eth_addr, token.name, token.symbol, token.decimals, token.usd, token.usd_update
  64. FROM tx_pool INNER JOIN token ON tx_pool.token_id = token.token_id `
  65. // GetTxAPI return the specified Tx in PoolTxAPI format
  66. func (l2db *L2DB) GetTxAPI(txID common.TxID) (*PoolTxAPI, error) {
  67. cancel, err := l2db.apiConnCon.Acquire()
  68. defer cancel()
  69. if err != nil {
  70. return nil, tracerr.Wrap(err)
  71. }
  72. defer l2db.apiConnCon.Release()
  73. tx := new(PoolTxAPI)
  74. return tx, tracerr.Wrap(meddler.QueryRow(
  75. l2db.db, tx,
  76. selectPoolTxAPI+"WHERE tx_id = $1;",
  77. txID,
  78. ))
  79. }