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.

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