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.

295 lines
9.3 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. // AddTx inserts a tx to the pool
  53. func (l2db *L2DB) AddTx(tx *PoolL2TxWrite) error {
  54. return meddler.Insert(l2db.db, "tx_pool", tx)
  55. }
  56. // AddTxTest inserts a tx into the L2DB. This is useful for test purposes,
  57. // but in production txs will only be inserted through the API
  58. func (l2db *L2DB) AddTxTest(tx *common.PoolL2Tx) error {
  59. // transform tx from *common.PoolL2Tx to PoolL2TxWrite
  60. insertTx := &PoolL2TxWrite{
  61. TxID: tx.TxID,
  62. FromIdx: tx.FromIdx,
  63. ToBJJ: tx.ToBJJ,
  64. TokenID: tx.TokenID,
  65. Amount: tx.Amount,
  66. Fee: tx.Fee,
  67. Nonce: tx.Nonce,
  68. State: tx.State,
  69. Signature: tx.Signature,
  70. RqToBJJ: tx.RqToBJJ,
  71. RqAmount: tx.RqAmount,
  72. Type: tx.Type,
  73. }
  74. if tx.ToIdx != 0 {
  75. insertTx.ToIdx = &tx.ToIdx
  76. }
  77. nilAddr := ethCommon.BigToAddress(big.NewInt(0))
  78. if tx.ToEthAddr != nilAddr {
  79. insertTx.ToEthAddr = &tx.ToEthAddr
  80. }
  81. if tx.RqFromIdx != 0 {
  82. insertTx.RqFromIdx = &tx.RqFromIdx
  83. }
  84. if tx.RqToIdx != 0 { // if true, all Rq... fields must be different to nil
  85. insertTx.RqToIdx = &tx.RqToIdx
  86. insertTx.RqTokenID = &tx.RqTokenID
  87. insertTx.RqFee = &tx.RqFee
  88. insertTx.RqNonce = &tx.RqNonce
  89. }
  90. if tx.RqToEthAddr != nilAddr {
  91. insertTx.RqToEthAddr = &tx.RqToEthAddr
  92. }
  93. f := new(big.Float).SetInt(tx.Amount)
  94. amountF, _ := f.Float64()
  95. insertTx.AmountFloat = amountF
  96. // insert tx
  97. return meddler.Insert(l2db.db, "tx_pool", insertTx)
  98. }
  99. // selectPoolTxRead select part of queries to get PoolL2TxRead
  100. const selectPoolTxRead = `SELECT tx_pool.tx_id, tx_pool.from_idx, tx_pool.to_idx, tx_pool.to_eth_addr,
  101. tx_pool.to_bjj, tx_pool.token_id, tx_pool.amount, tx_pool.fee, tx_pool.nonce,
  102. tx_pool.state, tx_pool.signature, tx_pool.timestamp, tx_pool.batch_num, tx_pool.rq_from_idx,
  103. 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,
  104. tx_pool.rq_fee, tx_pool.rq_nonce, tx_pool.tx_type,
  105. token.eth_block_num, token.eth_addr, token.name, token.symbol, token.decimals, token.usd, token.usd_update
  106. FROM tx_pool INNER JOIN token ON tx_pool.token_id = token.token_id `
  107. // selectPoolTxCommon select part of queries to get common.PoolL2Tx
  108. const selectPoolTxCommon = `SELECT tx_pool.tx_id, tx_pool.from_idx, tx_pool.to_idx, tx_pool.to_eth_addr,
  109. tx_pool.to_bjj, tx_pool.token_id, tx_pool.amount, tx_pool.fee, tx_pool.nonce,
  110. tx_pool.state, tx_pool.signature, tx_pool.timestamp, tx_pool.rq_from_idx,
  111. 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,
  112. tx_pool.rq_fee, tx_pool.rq_nonce, tx_pool.tx_type,
  113. fee_percentage(tx_pool.fee::NUMERIC) * token.usd * tx_pool.amount_f AS fee_usd, token.usd_update
  114. FROM tx_pool INNER JOIN token ON tx_pool.token_id = token.token_id `
  115. // GetTx return the specified Tx
  116. func (l2db *L2DB) GetTx(txID common.TxID) (*PoolL2TxRead, error) {
  117. tx := new(PoolL2TxRead)
  118. return tx, meddler.QueryRow(
  119. l2db.db, tx,
  120. selectPoolTxRead+"WHERE tx_id = $1;",
  121. txID,
  122. )
  123. }
  124. // GetPendingTxs return all the pending txs of the L2DB, that have a non NULL AbsoluteFee
  125. func (l2db *L2DB) GetPendingTxs() ([]common.PoolL2Tx, error) {
  126. var txs []*common.PoolL2Tx
  127. err := meddler.QueryAll(
  128. l2db.db, &txs,
  129. selectPoolTxCommon+"WHERE state = $1",
  130. common.PoolL2TxStatePending,
  131. )
  132. return db.SlicePtrsToSlice(txs).([]common.PoolL2Tx), err
  133. }
  134. // StartForging updates the state of the transactions that will begin the forging process.
  135. // The state of the txs referenced by txIDs will be changed from Pending -> Forging
  136. func (l2db *L2DB) StartForging(txIDs []common.TxID, batchNum common.BatchNum) error {
  137. query, args, err := sqlx.In(
  138. `UPDATE tx_pool
  139. SET state = ?, batch_num = ?
  140. WHERE state = ? AND tx_id IN (?);`,
  141. common.PoolL2TxStateForging,
  142. batchNum,
  143. common.PoolL2TxStatePending,
  144. txIDs,
  145. )
  146. if err != nil {
  147. return err
  148. }
  149. query = l2db.db.Rebind(query)
  150. _, err = l2db.db.Exec(query, args...)
  151. return err
  152. }
  153. // DoneForging updates the state of the transactions that have been forged
  154. // so the state of the txs referenced by txIDs will be changed from Forging -> Forged
  155. func (l2db *L2DB) DoneForging(txIDs []common.TxID, batchNum common.BatchNum) error {
  156. query, args, err := sqlx.In(
  157. `UPDATE tx_pool
  158. SET state = ?, batch_num = ?
  159. WHERE state = ? AND tx_id IN (?);`,
  160. common.PoolL2TxStateForged,
  161. batchNum,
  162. common.PoolL2TxStateForging,
  163. txIDs,
  164. )
  165. if err != nil {
  166. return err
  167. }
  168. query = l2db.db.Rebind(query)
  169. _, err = l2db.db.Exec(query, args...)
  170. return err
  171. }
  172. // InvalidateTxs updates the state of the transactions that are invalid.
  173. // The state of the txs referenced by txIDs will be changed from * -> Invalid
  174. func (l2db *L2DB) InvalidateTxs(txIDs []common.TxID, batchNum common.BatchNum) error {
  175. query, args, err := sqlx.In(
  176. `UPDATE tx_pool
  177. SET state = ?, batch_num = ?
  178. WHERE tx_id IN (?);`,
  179. common.PoolL2TxStateInvalid,
  180. batchNum,
  181. txIDs,
  182. )
  183. if err != nil {
  184. return err
  185. }
  186. query = l2db.db.Rebind(query)
  187. _, err = l2db.db.Exec(query, args...)
  188. return err
  189. }
  190. // CheckNonces invalidate txs with nonces that are smaller or equal than their respective accounts nonces.
  191. // The state of the affected txs will be changed from Pending -> Invalid
  192. func (l2db *L2DB) CheckNonces(updatedAccounts []common.Account, batchNum common.BatchNum) (err error) {
  193. txn, err := l2db.db.Begin()
  194. if err != nil {
  195. return err
  196. }
  197. defer func() {
  198. // Rollback the transaction if there was an error.
  199. if err != nil {
  200. errRollback := txn.Rollback()
  201. if errRollback != nil {
  202. log.Errorw("Rollback", "err", errRollback)
  203. }
  204. }
  205. }()
  206. for i := 0; i < len(updatedAccounts); i++ {
  207. _, err = txn.Exec(
  208. `UPDATE tx_pool
  209. SET state = $1, batch_num = $2
  210. WHERE state = $3 AND from_idx = $4 AND nonce <= $5;`,
  211. common.PoolL2TxStateInvalid,
  212. batchNum,
  213. common.PoolL2TxStatePending,
  214. updatedAccounts[i].Idx,
  215. updatedAccounts[i].Nonce,
  216. )
  217. if err != nil {
  218. return err
  219. }
  220. }
  221. return txn.Commit()
  222. }
  223. // Reorg updates the state of txs that were updated in a batch that has been discarted due to a blockchain reorg.
  224. // The state of the affected txs can change form Forged -> Pending or from Invalid -> Pending
  225. func (l2db *L2DB) Reorg(lastValidBatch common.BatchNum) error {
  226. _, err := l2db.db.Exec(
  227. `UPDATE tx_pool SET batch_num = NULL, state = $1
  228. WHERE (state = $2 OR state = $3) AND batch_num > $4`,
  229. common.PoolL2TxStatePending,
  230. common.PoolL2TxStateForged,
  231. common.PoolL2TxStateInvalid,
  232. lastValidBatch,
  233. )
  234. return err
  235. }
  236. // Purge deletes transactions that have been forged or marked as invalid for longer than the safety period
  237. // it also deletes txs that has been in the L2DB for longer than the ttl if maxTxs has been exceeded
  238. func (l2db *L2DB) Purge(currentBatchNum common.BatchNum) (err error) {
  239. txn, err := l2db.db.Begin()
  240. if err != nil {
  241. return err
  242. }
  243. defer func() {
  244. // Rollback the transaction if there was an error.
  245. if err != nil {
  246. errRollback := txn.Rollback()
  247. if errRollback != nil {
  248. log.Errorw("Rollback", "err", errRollback)
  249. }
  250. }
  251. }()
  252. // Delete pending txs that have been in the pool after the TTL if maxTxs is reached
  253. now := time.Now().UTC().Unix()
  254. _, err = txn.Exec(
  255. `DELETE FROM tx_pool WHERE (SELECT count(*) FROM tx_pool) > $1 AND timestamp < $2`,
  256. l2db.maxTxs,
  257. time.Unix(now-int64(l2db.ttl.Seconds()), 0),
  258. )
  259. if err != nil {
  260. return err
  261. }
  262. // Delete txs that have been marked as forged / invalid after the safety period
  263. _, err = txn.Exec(
  264. `DELETE FROM tx_pool
  265. WHERE batch_num < $1 AND (state = $2 OR state = $3)`,
  266. currentBatchNum-l2db.safetyPeriod,
  267. common.PoolL2TxStateForged,
  268. common.PoolL2TxStateInvalid,
  269. )
  270. if err != nil {
  271. return err
  272. }
  273. return txn.Commit()
  274. }