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.

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