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.

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