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.

281 lines
9.0 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. package l2db
  2. import (
  3. "math/big"
  4. "time"
  5. ethCommon "github.com/ethereum/go-ethereum/common"
  6. "github.com/hermeznetwork/hermez-node/common"
  7. "github.com/iden3/go-iden3-crypto/babyjub"
  8. "github.com/jmoiron/sqlx"
  9. //nolint:errcheck // driver for postgres DB
  10. _ "github.com/lib/pq"
  11. "github.com/russross/meddler"
  12. )
  13. // TODO(Edu): Check DB consistency while there's concurrent use from Coordinator/TxSelector & API
  14. // L2DB stores L2 txs and authorization registers received by the coordinator and keeps them until they are no longer relevant
  15. // due to them being forged or invalid after a safety period
  16. type L2DB struct {
  17. db *sqlx.DB
  18. safetyPeriod common.BatchNum
  19. ttl time.Duration
  20. maxTxs uint32
  21. }
  22. // NewL2DB creates a L2DB.
  23. // To create it, it's needed db connection, safety period expressed in batches,
  24. // maxTxs that the DB should have and TTL (time to live) for pending txs.
  25. func NewL2DB(db *sqlx.DB, safetyPeriod common.BatchNum, maxTxs uint32, TTL time.Duration) *L2DB {
  26. return &L2DB{
  27. db: db,
  28. safetyPeriod: safetyPeriod,
  29. ttl: TTL,
  30. maxTxs: maxTxs,
  31. }
  32. }
  33. // DB returns a pointer to the L2DB.db. This method should be used only for
  34. // internal testing purposes.
  35. func (l2db *L2DB) DB() *sqlx.DB {
  36. return l2db.db
  37. }
  38. // AddAccountCreationAuth inserts an account creation authorization into the DB
  39. func (l2db *L2DB) AddAccountCreationAuth(auth *common.AccountCreationAuth) error {
  40. return meddler.Insert(l2db.db, "account_creation_auth", auth)
  41. }
  42. // GetAccountCreationAuth returns an account creation authorization into the DB
  43. func (l2db *L2DB) GetAccountCreationAuth(addr ethCommon.Address) (*common.AccountCreationAuth, error) {
  44. auth := new(common.AccountCreationAuth)
  45. return auth, meddler.QueryRow(
  46. l2db.db, auth,
  47. "SELECT * FROM account_creation_auth WHERE eth_addr = $1;",
  48. addr,
  49. )
  50. }
  51. // AddTxTest inserts a tx into the L2DB. This is useful for test purposes,
  52. // but in production txs will only be inserted through the API (method TBD)
  53. func (l2db *L2DB) AddTxTest(tx *common.PoolL2Tx) error {
  54. type withouUSD struct {
  55. TxID common.TxID `meddler:"tx_id"`
  56. FromIdx common.Idx `meddler:"from_idx"`
  57. ToIdx common.Idx `meddler:"to_idx"`
  58. ToEthAddr ethCommon.Address `meddler:"to_eth_addr"`
  59. ToBJJ *babyjub.PublicKey `meddler:"to_bjj"`
  60. TokenID common.TokenID `meddler:"token_id"`
  61. Amount *big.Int `meddler:"amount,bigint"`
  62. AmountFloat float64 `meddler:"amount_f"`
  63. Fee common.FeeSelector `meddler:"fee"`
  64. Nonce common.Nonce `meddler:"nonce"`
  65. State common.PoolL2TxState `meddler:"state"`
  66. Signature *babyjub.Signature `meddler:"signature"`
  67. Timestamp time.Time `meddler:"timestamp,utctime"`
  68. BatchNum common.BatchNum `meddler:"batch_num,zeroisnull"`
  69. RqFromIdx common.Idx `meddler:"rq_from_idx,zeroisnull"`
  70. RqToIdx common.Idx `meddler:"rq_to_idx,zeroisnull"`
  71. RqToEthAddr ethCommon.Address `meddler:"rq_to_eth_addr"`
  72. RqToBJJ *babyjub.PublicKey `meddler:"rq_to_bjj"`
  73. RqTokenID common.TokenID `meddler:"rq_token_id,zeroisnull"`
  74. RqAmount *big.Int `meddler:"rq_amount,bigintnull"`
  75. RqFee common.FeeSelector `meddler:"rq_fee,zeroisnull"`
  76. RqNonce uint64 `meddler:"rq_nonce,zeroisnull"`
  77. Type common.TxType `meddler:"tx_type"`
  78. }
  79. return meddler.Insert(l2db.db, "tx_pool", &withouUSD{
  80. TxID: tx.TxID,
  81. FromIdx: tx.FromIdx,
  82. ToIdx: tx.ToIdx,
  83. ToEthAddr: tx.ToEthAddr,
  84. ToBJJ: tx.ToBJJ,
  85. TokenID: tx.TokenID,
  86. Amount: tx.Amount,
  87. AmountFloat: tx.AmountFloat,
  88. Fee: tx.Fee,
  89. Nonce: tx.Nonce,
  90. State: tx.State,
  91. Signature: tx.Signature,
  92. Timestamp: tx.Timestamp,
  93. BatchNum: tx.BatchNum,
  94. RqFromIdx: tx.RqFromIdx,
  95. RqToIdx: tx.RqToIdx,
  96. RqToEthAddr: tx.RqToEthAddr,
  97. RqToBJJ: tx.RqToBJJ,
  98. RqTokenID: tx.RqTokenID,
  99. RqAmount: tx.RqAmount,
  100. RqFee: tx.RqFee,
  101. RqNonce: tx.RqNonce,
  102. Type: tx.Type,
  103. })
  104. }
  105. // selectPoolTx select part of queries to get common.PoolL2Tx
  106. const selectPoolTx = `SELECT tx_pool.*, token.usd * tx_pool.amount_f AS value_usd,
  107. fee_percentage(tx_pool.fee::NUMERIC) * token.usd * tx_pool.amount_f AS fee_usd, token.usd_update,
  108. token.symbol AS token_symbol FROM tx_pool INNER JOIN token ON tx_pool.token_id = token.token_id `
  109. // GetTx return the specified Tx
  110. func (l2db *L2DB) GetTx(txID common.TxID) (*common.PoolL2Tx, error) {
  111. tx := new(common.PoolL2Tx)
  112. return tx, meddler.QueryRow(
  113. l2db.db, tx,
  114. selectPoolTx+"WHERE tx_id = $1;",
  115. txID,
  116. )
  117. }
  118. // GetPendingTxs return all the pending txs of the L2DB, that have a non NULL AbsoluteFee
  119. func (l2db *L2DB) GetPendingTxs() ([]*common.PoolL2Tx, error) {
  120. var txs []*common.PoolL2Tx
  121. err := meddler.QueryAll(
  122. l2db.db, &txs,
  123. selectPoolTx+"WHERE state = $1 AND token.usd IS NOT NULL",
  124. common.PoolL2TxStatePending,
  125. )
  126. return txs, err
  127. }
  128. // StartForging updates the state of the transactions that will begin the forging process.
  129. // The state of the txs referenced by txIDs will be changed from Pending -> Forging
  130. func (l2db *L2DB) StartForging(txIDs []common.TxID, batchNum common.BatchNum) error {
  131. query, args, err := sqlx.In(
  132. `UPDATE tx_pool
  133. SET state = ?, batch_num = ?
  134. WHERE state = ? AND tx_id IN (?);`,
  135. common.PoolL2TxStateForging,
  136. batchNum,
  137. common.PoolL2TxStatePending,
  138. txIDs,
  139. )
  140. if err != nil {
  141. return err
  142. }
  143. query = l2db.db.Rebind(query)
  144. _, err = l2db.db.Exec(query, args...)
  145. return err
  146. }
  147. // DoneForging updates the state of the transactions that have been forged
  148. // so the state of the txs referenced by txIDs will be changed from Forging -> Forged
  149. func (l2db *L2DB) DoneForging(txIDs []common.TxID, batchNum common.BatchNum) error {
  150. query, args, err := sqlx.In(
  151. `UPDATE tx_pool
  152. SET state = ?, batch_num = ?
  153. WHERE state = ? AND tx_id IN (?);`,
  154. common.PoolL2TxStateForged,
  155. batchNum,
  156. common.PoolL2TxStateForging,
  157. txIDs,
  158. )
  159. if err != nil {
  160. return err
  161. }
  162. query = l2db.db.Rebind(query)
  163. _, err = l2db.db.Exec(query, args...)
  164. return err
  165. }
  166. // InvalidateTxs updates the state of the transactions that are invalid.
  167. // The state of the txs referenced by txIDs will be changed from * -> Invalid
  168. func (l2db *L2DB) InvalidateTxs(txIDs []common.TxID, batchNum common.BatchNum) error {
  169. query, args, err := sqlx.In(
  170. `UPDATE tx_pool
  171. SET state = ?, batch_num = ?
  172. WHERE tx_id IN (?);`,
  173. common.PoolL2TxStateInvalid,
  174. batchNum,
  175. txIDs,
  176. )
  177. if err != nil {
  178. return err
  179. }
  180. query = l2db.db.Rebind(query)
  181. _, err = l2db.db.Exec(query, args...)
  182. return err
  183. }
  184. // CheckNonces invalidate txs with nonces that are smaller or equal than their respective accounts nonces.
  185. // The state of the affected txs will be changed from Pending -> Invalid
  186. func (l2db *L2DB) CheckNonces(updatedAccounts []common.Account, batchNum common.BatchNum) (err error) {
  187. txn, err := l2db.db.Begin()
  188. if err != nil {
  189. return err
  190. }
  191. defer func() {
  192. // Rollback the transaction if there was an error.
  193. if err != nil {
  194. err = txn.Rollback()
  195. }
  196. }()
  197. for i := 0; i < len(updatedAccounts); i++ {
  198. _, err = txn.Exec(
  199. `UPDATE tx_pool
  200. SET state = $1, batch_num = $2
  201. WHERE state = $3 AND from_idx = $4 AND nonce <= $5;`,
  202. common.PoolL2TxStateInvalid,
  203. batchNum,
  204. common.PoolL2TxStatePending,
  205. updatedAccounts[i].Idx,
  206. updatedAccounts[i].Nonce,
  207. )
  208. if err != nil {
  209. return err
  210. }
  211. }
  212. return txn.Commit()
  213. }
  214. // Reorg updates the state of txs that were updated in a batch that has been discarted due to a blockchain reorg.
  215. // The state of the affected txs can change form Forged -> Pending or from Invalid -> Pending
  216. func (l2db *L2DB) Reorg(lastValidBatch common.BatchNum) error {
  217. _, err := l2db.db.Exec(
  218. `UPDATE tx_pool SET batch_num = NULL, state = $1
  219. WHERE (state = $2 OR state = $3) AND batch_num > $4`,
  220. common.PoolL2TxStatePending,
  221. common.PoolL2TxStateForged,
  222. common.PoolL2TxStateInvalid,
  223. lastValidBatch,
  224. )
  225. return err
  226. }
  227. // Purge deletes transactions that have been forged or marked as invalid for longer than the safety period
  228. // it also deletes txs that has been in the L2DB for longer than the ttl if maxTxs has been exceeded
  229. func (l2db *L2DB) Purge(currentBatchNum common.BatchNum) (err error) {
  230. txn, err := l2db.db.Begin()
  231. if err != nil {
  232. return err
  233. }
  234. defer func() {
  235. // Rollback the transaction if there was an error.
  236. if err != nil {
  237. err = txn.Rollback()
  238. }
  239. }()
  240. // Delete pending txs that have been in the pool after the TTL if maxTxs is reached
  241. now := time.Now().UTC().Unix()
  242. _, err = txn.Exec(
  243. `DELETE FROM tx_pool WHERE (SELECT count(*) FROM tx_pool) > $1 AND timestamp < $2`,
  244. l2db.maxTxs,
  245. time.Unix(now-int64(l2db.ttl.Seconds()), 0),
  246. )
  247. if err != nil {
  248. return err
  249. }
  250. // Delete txs that have been marked as forged / invalid after the safety period
  251. _, err = txn.Exec(
  252. `DELETE FROM tx_pool
  253. WHERE batch_num < $1 AND (state = $2 OR state = $3)`,
  254. currentBatchNum-l2db.safetyPeriod,
  255. common.PoolL2TxStateForged,
  256. common.PoolL2TxStateInvalid,
  257. )
  258. if err != nil {
  259. return err
  260. }
  261. return txn.Commit()
  262. }