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.

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