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.

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