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.

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