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.

359 lines
11 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
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. "fmt"
  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/tracerr"
  10. "github.com/jmoiron/sqlx"
  11. //nolint:errcheck // driver for postgres DB
  12. _ "github.com/lib/pq"
  13. "github.com/russross/meddler"
  14. )
  15. // TODO(Edu): Check DB consistency while there's concurrent use from Coordinator/TxSelector & API
  16. // L2DB stores L2 txs and authorization registers received by the coordinator and keeps them until they are no longer relevant
  17. // due to them being forged or invalid after a safety period
  18. type L2DB struct {
  19. db *sqlx.DB
  20. safetyPeriod common.BatchNum
  21. ttl time.Duration
  22. maxTxs uint32 // limit of txs that are accepted in the pool
  23. minFeeUSD float64
  24. apiConnCon *db.APIConnectionController
  25. }
  26. // NewL2DB creates a L2DB.
  27. // To create it, it's needed db connection, safety period expressed in batches,
  28. // maxTxs that the DB should have and TTL (time to live) for pending txs.
  29. func NewL2DB(
  30. db *sqlx.DB,
  31. safetyPeriod common.BatchNum,
  32. maxTxs uint32,
  33. minFeeUSD float64,
  34. TTL time.Duration,
  35. apiConnCon *db.APIConnectionController,
  36. ) *L2DB {
  37. return &L2DB{
  38. db: db,
  39. safetyPeriod: safetyPeriod,
  40. ttl: TTL,
  41. maxTxs: maxTxs,
  42. minFeeUSD: minFeeUSD,
  43. apiConnCon: apiConnCon,
  44. }
  45. }
  46. // DB returns a pointer to the L2DB.db. This method should be used only for
  47. // internal testing purposes.
  48. func (l2db *L2DB) DB() *sqlx.DB {
  49. return l2db.db
  50. }
  51. // AddAccountCreationAuth inserts an account creation authorization into the DB
  52. func (l2db *L2DB) AddAccountCreationAuth(auth *common.AccountCreationAuth) error {
  53. _, err := l2db.db.Exec(
  54. `INSERT INTO account_creation_auth (eth_addr, bjj, signature)
  55. VALUES ($1, $2, $3);`,
  56. auth.EthAddr, auth.BJJ, auth.Signature,
  57. )
  58. return tracerr.Wrap(err)
  59. }
  60. // GetAccountCreationAuth returns an account creation authorization from the DB
  61. func (l2db *L2DB) GetAccountCreationAuth(addr ethCommon.Address) (*common.AccountCreationAuth, error) {
  62. auth := new(common.AccountCreationAuth)
  63. return auth, tracerr.Wrap(meddler.QueryRow(
  64. l2db.db, auth,
  65. "SELECT * FROM account_creation_auth WHERE eth_addr = $1;",
  66. addr,
  67. ))
  68. }
  69. // UpdateTxsInfo updates the parameter Info of the pool transactions
  70. func (l2db *L2DB) UpdateTxsInfo(txs []common.PoolL2Tx) error {
  71. if len(txs) == 0 {
  72. return nil
  73. }
  74. type txUpdate struct {
  75. ID common.TxID `db:"id"`
  76. Info string `db:"info"`
  77. }
  78. txUpdates := make([]txUpdate, len(txs))
  79. for i := range txs {
  80. txUpdates[i] = txUpdate{ID: txs[i].TxID, Info: txs[i].Info}
  81. }
  82. const query string = `
  83. UPDATE tx_pool SET
  84. info = tx_update.info
  85. FROM (VALUES
  86. (NULL::::BYTEA, NULL::::VARCHAR),
  87. (:id, :info)
  88. ) as tx_update (id, info)
  89. WHERE tx_pool.tx_id = tx_update.id;
  90. `
  91. if len(txUpdates) > 0 {
  92. if _, err := sqlx.NamedExec(l2db.db, query, txUpdates); err != nil {
  93. return tracerr.Wrap(err)
  94. }
  95. }
  96. return nil
  97. }
  98. // NewPoolL2TxWriteFromPoolL2Tx creates a new PoolL2TxWrite from a PoolL2Tx
  99. func NewPoolL2TxWriteFromPoolL2Tx(tx *common.PoolL2Tx) *PoolL2TxWrite {
  100. // transform tx from *common.PoolL2Tx to PoolL2TxWrite
  101. insertTx := &PoolL2TxWrite{
  102. TxID: tx.TxID,
  103. FromIdx: tx.FromIdx,
  104. TokenID: tx.TokenID,
  105. Amount: tx.Amount,
  106. Fee: tx.Fee,
  107. Nonce: tx.Nonce,
  108. State: common.PoolL2TxStatePending,
  109. Signature: tx.Signature,
  110. RqAmount: tx.RqAmount,
  111. Type: tx.Type,
  112. }
  113. if tx.ToIdx != 0 {
  114. insertTx.ToIdx = &tx.ToIdx
  115. }
  116. nilAddr := ethCommon.BigToAddress(big.NewInt(0))
  117. if tx.ToEthAddr != nilAddr {
  118. insertTx.ToEthAddr = &tx.ToEthAddr
  119. }
  120. if tx.RqFromIdx != 0 {
  121. insertTx.RqFromIdx = &tx.RqFromIdx
  122. }
  123. if tx.RqToIdx != 0 { // if true, all Rq... fields must be different to nil
  124. insertTx.RqToIdx = &tx.RqToIdx
  125. insertTx.RqTokenID = &tx.RqTokenID
  126. insertTx.RqFee = &tx.RqFee
  127. insertTx.RqNonce = &tx.RqNonce
  128. }
  129. if tx.RqToEthAddr != nilAddr {
  130. insertTx.RqToEthAddr = &tx.RqToEthAddr
  131. }
  132. if tx.ToBJJ != common.EmptyBJJComp {
  133. insertTx.ToBJJ = &tx.ToBJJ
  134. }
  135. if tx.RqToBJJ != common.EmptyBJJComp {
  136. insertTx.RqToBJJ = &tx.RqToBJJ
  137. }
  138. f := new(big.Float).SetInt(tx.Amount)
  139. amountF, _ := f.Float64()
  140. insertTx.AmountFloat = amountF
  141. return insertTx
  142. }
  143. // AddTxTest inserts a tx into the L2DB. This is useful for test purposes,
  144. // but in production txs will only be inserted through the API
  145. func (l2db *L2DB) AddTxTest(tx *common.PoolL2Tx) error {
  146. insertTx := NewPoolL2TxWriteFromPoolL2Tx(tx)
  147. // insert tx
  148. return tracerr.Wrap(meddler.Insert(l2db.db, "tx_pool", insertTx))
  149. }
  150. // selectPoolTxCommon select part of queries to get common.PoolL2Tx
  151. const selectPoolTxCommon = `SELECT tx_pool.tx_id, from_idx, to_idx, tx_pool.to_eth_addr,
  152. tx_pool.to_bjj, tx_pool.token_id, tx_pool.amount, tx_pool.fee, tx_pool.nonce,
  153. tx_pool.state, tx_pool.info, tx_pool.signature, tx_pool.timestamp, rq_from_idx,
  154. rq_to_idx, tx_pool.rq_to_eth_addr, tx_pool.rq_to_bjj, tx_pool.rq_token_id, tx_pool.rq_amount,
  155. tx_pool.rq_fee, tx_pool.rq_nonce, tx_pool.tx_type,
  156. (fee_percentage(tx_pool.fee::NUMERIC) * token.usd * tx_pool.amount_f) /
  157. (10.0 ^ token.decimals::NUMERIC) AS fee_usd, token.usd_update
  158. FROM tx_pool INNER JOIN token ON tx_pool.token_id = token.token_id `
  159. // GetTx return the specified Tx in common.PoolL2Tx format
  160. func (l2db *L2DB) GetTx(txID common.TxID) (*common.PoolL2Tx, error) {
  161. tx := new(common.PoolL2Tx)
  162. return tx, tracerr.Wrap(meddler.QueryRow(
  163. l2db.db, tx,
  164. selectPoolTxCommon+"WHERE tx_id = $1;",
  165. txID,
  166. ))
  167. }
  168. // GetPendingTxs return all the pending txs of the L2DB, that have a non NULL AbsoluteFee
  169. func (l2db *L2DB) GetPendingTxs() ([]common.PoolL2Tx, error) {
  170. var txs []*common.PoolL2Tx
  171. err := meddler.QueryAll(
  172. l2db.db, &txs,
  173. selectPoolTxCommon+"WHERE state = $1",
  174. common.PoolL2TxStatePending,
  175. )
  176. return db.SlicePtrsToSlice(txs).([]common.PoolL2Tx), tracerr.Wrap(err)
  177. }
  178. // StartForging updates the state of the transactions that will begin the forging process.
  179. // The state of the txs referenced by txIDs will be changed from Pending -> Forging
  180. func (l2db *L2DB) StartForging(txIDs []common.TxID, batchNum common.BatchNum) error {
  181. if len(txIDs) == 0 {
  182. return nil
  183. }
  184. query, args, err := sqlx.In(
  185. `UPDATE tx_pool
  186. SET state = ?, batch_num = ?
  187. WHERE state = ? AND tx_id IN (?);`,
  188. common.PoolL2TxStateForging,
  189. batchNum,
  190. common.PoolL2TxStatePending,
  191. txIDs,
  192. )
  193. if err != nil {
  194. return tracerr.Wrap(err)
  195. }
  196. query = l2db.db.Rebind(query)
  197. _, err = l2db.db.Exec(query, args...)
  198. return tracerr.Wrap(err)
  199. }
  200. // DoneForging updates the state of the transactions that have been forged
  201. // so the state of the txs referenced by txIDs will be changed from Forging -> Forged
  202. func (l2db *L2DB) DoneForging(txIDs []common.TxID, batchNum common.BatchNum) error {
  203. if len(txIDs) == 0 {
  204. return nil
  205. }
  206. query, args, err := sqlx.In(
  207. `UPDATE tx_pool
  208. SET state = ?, batch_num = ?
  209. WHERE state = ? AND tx_id IN (?);`,
  210. common.PoolL2TxStateForged,
  211. batchNum,
  212. common.PoolL2TxStateForging,
  213. txIDs,
  214. )
  215. if err != nil {
  216. return tracerr.Wrap(err)
  217. }
  218. query = l2db.db.Rebind(query)
  219. _, err = l2db.db.Exec(query, args...)
  220. return tracerr.Wrap(err)
  221. }
  222. // InvalidateTxs updates the state of the transactions that are invalid.
  223. // The state of the txs referenced by txIDs will be changed from * -> Invalid
  224. func (l2db *L2DB) InvalidateTxs(txIDs []common.TxID, batchNum common.BatchNum) error {
  225. if len(txIDs) == 0 {
  226. return nil
  227. }
  228. query, args, err := sqlx.In(
  229. `UPDATE tx_pool
  230. SET state = ?, batch_num = ?
  231. WHERE tx_id IN (?);`,
  232. common.PoolL2TxStateInvalid,
  233. batchNum,
  234. txIDs,
  235. )
  236. if err != nil {
  237. return tracerr.Wrap(err)
  238. }
  239. query = l2db.db.Rebind(query)
  240. _, err = l2db.db.Exec(query, args...)
  241. return tracerr.Wrap(err)
  242. }
  243. // GetPendingUniqueFromIdxs returns from all the pending transactions, the set
  244. // of unique FromIdx
  245. func (l2db *L2DB) GetPendingUniqueFromIdxs() ([]common.Idx, error) {
  246. var idxs []common.Idx
  247. rows, err := l2db.db.Query(`SELECT DISTINCT from_idx FROM tx_pool
  248. WHERE state = $1;`, common.PoolL2TxStatePending)
  249. if err != nil {
  250. return nil, tracerr.Wrap(err)
  251. }
  252. defer db.RowsClose(rows)
  253. var idx common.Idx
  254. for rows.Next() {
  255. err = rows.Scan(&idx)
  256. if err != nil {
  257. return nil, tracerr.Wrap(err)
  258. }
  259. idxs = append(idxs, idx)
  260. }
  261. return idxs, nil
  262. }
  263. var invalidateOldNoncesQuery = fmt.Sprintf(`
  264. UPDATE tx_pool SET
  265. state = '%s',
  266. batch_num = %%d
  267. FROM (VALUES
  268. (NULL::::BIGINT, NULL::::BIGINT),
  269. (:idx, :nonce)
  270. ) as updated_acc (idx, nonce)
  271. WHERE tx_pool.state = '%s' AND
  272. tx_pool.from_idx = updated_acc.idx AND
  273. tx_pool.nonce < updated_acc.nonce;
  274. `, common.PoolL2TxStateInvalid, common.PoolL2TxStatePending)
  275. // InvalidateOldNonces invalidate txs with nonces that are smaller or equal than their
  276. // respective accounts nonces. The state of the affected txs will be changed
  277. // from Pending to Invalid
  278. func (l2db *L2DB) InvalidateOldNonces(updatedAccounts []common.IdxNonce, batchNum common.BatchNum) (err error) {
  279. if len(updatedAccounts) == 0 {
  280. return nil
  281. }
  282. // Fill the batch_num in the query with Sprintf because we are using a
  283. // named query which works with slices, and doens't handle an extra
  284. // individual argument.
  285. query := fmt.Sprintf(invalidateOldNoncesQuery, batchNum)
  286. if _, err := sqlx.NamedExec(l2db.db, query, updatedAccounts); err != nil {
  287. return tracerr.Wrap(err)
  288. }
  289. return nil
  290. }
  291. // Reorg updates the state of txs that were updated in a batch that has been discarted due to a blockchain reorg.
  292. // The state of the affected txs can change form Forged -> Pending or from Invalid -> Pending
  293. func (l2db *L2DB) Reorg(lastValidBatch common.BatchNum) error {
  294. _, err := l2db.db.Exec(
  295. `UPDATE tx_pool SET batch_num = NULL, state = $1
  296. WHERE (state = $2 OR state = $3 OR state = $4) AND batch_num > $5`,
  297. common.PoolL2TxStatePending,
  298. common.PoolL2TxStateForging,
  299. common.PoolL2TxStateForged,
  300. common.PoolL2TxStateInvalid,
  301. lastValidBatch,
  302. )
  303. return tracerr.Wrap(err)
  304. }
  305. // Purge deletes transactions that have been forged or marked as invalid for longer than the safety period
  306. // it also deletes pending txs that have been in the L2DB for longer than the ttl if maxTxs has been exceeded
  307. func (l2db *L2DB) Purge(currentBatchNum common.BatchNum) (err error) {
  308. now := time.Now().UTC().Unix()
  309. _, err = l2db.db.Exec(
  310. `DELETE FROM tx_pool WHERE (
  311. batch_num < $1 AND (state = $2 OR state = $3)
  312. ) OR (
  313. (SELECT count(*) FROM tx_pool WHERE state = $4) > $5
  314. AND timestamp < $6 AND state = $4
  315. );`,
  316. currentBatchNum-l2db.safetyPeriod,
  317. common.PoolL2TxStateForged,
  318. common.PoolL2TxStateInvalid,
  319. common.PoolL2TxStatePending,
  320. l2db.maxTxs,
  321. time.Unix(now-int64(l2db.ttl.Seconds()), 0),
  322. )
  323. return tracerr.Wrap(err)
  324. }
  325. // PurgeByExternalDelete deletes all pending transactions marked with true in
  326. // the `external_delete` column. An external process can set this column to
  327. // true to instruct the coordinator to delete the tx when possible.
  328. func (l2db *L2DB) PurgeByExternalDelete() error {
  329. _, err := l2db.db.Exec(
  330. `DELETE from tx_pool WHERE (external_delete = true AND state = $1);`,
  331. common.PoolL2TxStatePending,
  332. )
  333. return tracerr.Wrap(err)
  334. }