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.

293 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. "errors"
  4. "math/big"
  5. "time"
  6. ethCommon "github.com/ethereum/go-ethereum/common"
  7. "github.com/hermeznetwork/hermez-node/common"
  8. "github.com/hermeznetwork/hermez-node/db"
  9. "github.com/hermeznetwork/hermez-node/log"
  10. "github.com/iden3/go-iden3-crypto/babyjub"
  11. "github.com/jmoiron/sqlx"
  12. //nolint:errcheck // driver for postgres DB
  13. _ "github.com/lib/pq"
  14. "github.com/russross/meddler"
  15. )
  16. // TODO(Edu): Check DB consistency while there's concurrent use from Coordinator/TxSelector & API
  17. // L2DB stores L2 txs and authorization registers received by the coordinator and keeps them until they are no longer relevant
  18. // due to them being forged or invalid after a safety period
  19. type L2DB struct {
  20. db *sqlx.DB
  21. safetyPeriod common.BatchNum
  22. ttl time.Duration
  23. maxTxs uint32
  24. }
  25. // NewL2DB creates a L2DB.
  26. // To create it, it's needed db connection, safety period expressed in batches,
  27. // maxTxs that the DB should have and TTL (time to live) for pending txs.
  28. func NewL2DB(db *sqlx.DB, safetyPeriod common.BatchNum, maxTxs uint32, TTL time.Duration) *L2DB {
  29. return &L2DB{
  30. db: db,
  31. safetyPeriod: safetyPeriod,
  32. ttl: TTL,
  33. maxTxs: maxTxs,
  34. }
  35. }
  36. // DB returns a pointer to the L2DB.db. This method should be used only for
  37. // internal testing purposes.
  38. func (l2db *L2DB) DB() *sqlx.DB {
  39. return l2db.db
  40. }
  41. // AddAccountCreationAuth inserts an account creation authorization into the DB
  42. func (l2db *L2DB) AddAccountCreationAuth(auth *common.AccountCreationAuth) error {
  43. return meddler.Insert(l2db.db, "account_creation_auth", auth)
  44. }
  45. // GetAccountCreationAuth returns an account creation authorization into the DB
  46. func (l2db *L2DB) GetAccountCreationAuth(addr *ethCommon.Address) (*common.AccountCreationAuth, error) {
  47. if addr == nil {
  48. return nil, errors.New("addr cannot be nil")
  49. }
  50. auth := new(common.AccountCreationAuth)
  51. return auth, meddler.QueryRow(
  52. l2db.db, auth,
  53. "SELECT * FROM account_creation_auth WHERE eth_addr = $1;",
  54. addr,
  55. )
  56. }
  57. // AddTxTest inserts a tx into the L2DB. This is useful for test purposes,
  58. // but in production txs will only be inserted through the API (method TBD)
  59. func (l2db *L2DB) AddTxTest(tx *common.PoolL2Tx) error {
  60. type withouUSD struct {
  61. TxID common.TxID `meddler:"tx_id"`
  62. FromIdx common.Idx `meddler:"from_idx"`
  63. ToIdx *common.Idx `meddler:"to_idx"`
  64. ToEthAddr *ethCommon.Address `meddler:"to_eth_addr"`
  65. ToBJJ *babyjub.PublicKey `meddler:"to_bjj"`
  66. TokenID common.TokenID `meddler:"token_id"`
  67. Amount *big.Int `meddler:"amount,bigint"`
  68. AmountFloat float64 `meddler:"amount_f"`
  69. Fee common.FeeSelector `meddler:"fee"`
  70. Nonce common.Nonce `meddler:"nonce"`
  71. State common.PoolL2TxState `meddler:"state"`
  72. Signature *babyjub.Signature `meddler:"signature"`
  73. Timestamp time.Time `meddler:"timestamp,utctime"`
  74. BatchNum *common.BatchNum `meddler:"batch_num"`
  75. RqFromIdx *common.Idx `meddler:"rq_from_idx"`
  76. RqToIdx *common.Idx `meddler:"rq_to_idx"`
  77. RqToEthAddr *ethCommon.Address `meddler:"rq_to_eth_addr"`
  78. RqToBJJ *babyjub.PublicKey `meddler:"rq_to_bjj"`
  79. RqTokenID *common.TokenID `meddler:"rq_token_id"`
  80. RqAmount *big.Int `meddler:"rq_amount,bigintnull"`
  81. RqFee *common.FeeSelector `meddler:"rq_fee"`
  82. RqNonce *uint64 `meddler:"rq_nonce"`
  83. Type common.TxType `meddler:"tx_type"`
  84. }
  85. return meddler.Insert(l2db.db, "tx_pool", &withouUSD{
  86. TxID: tx.TxID,
  87. FromIdx: tx.FromIdx,
  88. ToIdx: tx.ToIdx,
  89. ToEthAddr: tx.ToEthAddr,
  90. ToBJJ: tx.ToBJJ,
  91. TokenID: tx.TokenID,
  92. Amount: tx.Amount,
  93. AmountFloat: tx.AmountFloat,
  94. Fee: tx.Fee,
  95. Nonce: tx.Nonce,
  96. State: tx.State,
  97. Signature: tx.Signature,
  98. Timestamp: tx.Timestamp,
  99. BatchNum: tx.BatchNum,
  100. RqFromIdx: tx.RqFromIdx,
  101. RqToIdx: tx.RqToIdx,
  102. RqToEthAddr: tx.RqToEthAddr,
  103. RqToBJJ: tx.RqToBJJ,
  104. RqTokenID: tx.RqTokenID,
  105. RqAmount: tx.RqAmount,
  106. RqFee: tx.RqFee,
  107. RqNonce: tx.RqNonce,
  108. Type: tx.Type,
  109. })
  110. }
  111. // selectPoolTx select part of queries to get common.PoolL2Tx
  112. const selectPoolTx = `SELECT tx_pool.*, token.usd * tx_pool.amount_f AS value_usd,
  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) (*common.PoolL2Tx, error) {
  117. tx := new(common.PoolL2Tx)
  118. return tx, meddler.QueryRow(
  119. l2db.db, tx,
  120. selectPoolTx+"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. selectPoolTx+"WHERE state = $1 AND token.usd IS NOT NULL",
  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. }