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.

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