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.

917 lines
25 KiB

  1. package historydb
  2. import (
  3. "database/sql"
  4. "errors"
  5. "fmt"
  6. "math/big"
  7. ethCommon "github.com/ethereum/go-ethereum/common"
  8. "github.com/hermeznetwork/hermez-node/common"
  9. "github.com/hermeznetwork/hermez-node/db"
  10. "github.com/hermeznetwork/hermez-node/log"
  11. "github.com/iden3/go-iden3-crypto/babyjub"
  12. "github.com/jmoiron/sqlx"
  13. //nolint:errcheck // driver for postgres DB
  14. _ "github.com/lib/pq"
  15. "github.com/russross/meddler"
  16. )
  17. const (
  18. // OrderAsc indicates ascending order when using pagination
  19. OrderAsc = "ASC"
  20. // OrderDesc indicates descending order when using pagination
  21. OrderDesc = "DESC"
  22. )
  23. // TODO(Edu): Document here how HistoryDB is kept consistent
  24. // HistoryDB persist the historic of the rollup
  25. type HistoryDB struct {
  26. db *sqlx.DB
  27. }
  28. // BlockData contains the information of a Block
  29. type BlockData struct {
  30. Block *common.Block
  31. // Rollup
  32. // L1UserTxs that were submitted in the block
  33. L1UserTxs []common.L1Tx
  34. Batches []BatchData
  35. RegisteredTokens []common.Token
  36. RollupVars *common.RollupVars
  37. // Auction
  38. Bids []common.Bid
  39. Coordinators []common.Coordinator
  40. AuctionVars *common.AuctionVars
  41. WithdrawDelayerVars *common.WithdrawDelayerVars
  42. // TODO: enable when common.WithdrawalDelayerVars is Merged from Synchronizer PR
  43. // WithdrawalDelayerVars *common.WithdrawalDelayerVars
  44. }
  45. // BatchData contains the information of a Batch
  46. type BatchData struct {
  47. // L1UserTxs that were forged in the batch
  48. L1Batch bool // TODO: Remove once Batch.ForgeL1TxsNum is a pointer
  49. L1UserTxs []common.L1Tx
  50. L1CoordinatorTxs []common.L1Tx
  51. L2Txs []common.L2Tx
  52. CreatedAccounts []common.Account
  53. ExitTree []common.ExitInfo
  54. Batch *common.Batch
  55. }
  56. // NewBatchData creates an empty BatchData with the slices initialized.
  57. func NewBatchData() *BatchData {
  58. return &BatchData{
  59. L1Batch: false,
  60. L1UserTxs: make([]common.L1Tx, 0),
  61. L1CoordinatorTxs: make([]common.L1Tx, 0),
  62. L2Txs: make([]common.L2Tx, 0),
  63. CreatedAccounts: make([]common.Account, 0),
  64. ExitTree: make([]common.ExitInfo, 0),
  65. Batch: &common.Batch{},
  66. }
  67. }
  68. // NewHistoryDB initialize the DB
  69. func NewHistoryDB(db *sqlx.DB) *HistoryDB {
  70. return &HistoryDB{db: db}
  71. }
  72. // AddBlock insert a block into the DB
  73. func (hdb *HistoryDB) AddBlock(block *common.Block) error { return hdb.addBlock(hdb.db, block) }
  74. func (hdb *HistoryDB) addBlock(d meddler.DB, block *common.Block) error {
  75. return meddler.Insert(d, "block", block)
  76. }
  77. // AddBlocks inserts blocks into the DB
  78. func (hdb *HistoryDB) AddBlocks(blocks []common.Block) error {
  79. return hdb.addBlocks(hdb.db, blocks)
  80. }
  81. func (hdb *HistoryDB) addBlocks(d meddler.DB, blocks []common.Block) error {
  82. return db.BulkInsert(
  83. d,
  84. `INSERT INTO block (
  85. eth_block_num,
  86. timestamp,
  87. hash
  88. ) VALUES %s;`,
  89. blocks[:],
  90. )
  91. }
  92. // GetBlock retrieve a block from the DB, given a block number
  93. func (hdb *HistoryDB) GetBlock(blockNum int64) (*common.Block, error) {
  94. block := &common.Block{}
  95. err := meddler.QueryRow(
  96. hdb.db, block,
  97. "SELECT * FROM block WHERE eth_block_num = $1;", blockNum,
  98. )
  99. return block, err
  100. }
  101. // GetBlocks retrieve blocks from the DB, given a range of block numbers defined by from and to
  102. func (hdb *HistoryDB) GetBlocks(from, to int64) ([]common.Block, error) {
  103. var blocks []*common.Block
  104. err := meddler.QueryAll(
  105. hdb.db, &blocks,
  106. "SELECT * FROM block WHERE $1 <= eth_block_num AND eth_block_num < $2;",
  107. from, to,
  108. )
  109. return db.SlicePtrsToSlice(blocks).([]common.Block), err
  110. }
  111. // GetLastBlock retrieve the block with the highest block number from the DB
  112. func (hdb *HistoryDB) GetLastBlock() (*common.Block, error) {
  113. block := &common.Block{}
  114. err := meddler.QueryRow(
  115. hdb.db, block, "SELECT * FROM block ORDER BY eth_block_num DESC LIMIT 1;",
  116. )
  117. return block, err
  118. }
  119. // AddBatch insert a Batch into the DB
  120. func (hdb *HistoryDB) AddBatch(batch *common.Batch) error { return hdb.addBatch(hdb.db, batch) }
  121. func (hdb *HistoryDB) addBatch(d meddler.DB, batch *common.Batch) error {
  122. return meddler.Insert(d, "batch", batch)
  123. }
  124. // AddBatches insert Bids into the DB
  125. func (hdb *HistoryDB) AddBatches(batches []common.Batch) error {
  126. return hdb.addBatches(hdb.db, batches)
  127. }
  128. func (hdb *HistoryDB) addBatches(d meddler.DB, batches []common.Batch) error {
  129. // TODO: Calculate and insert total_fees_usd
  130. return db.BulkInsert(
  131. d,
  132. `INSERT INTO batch (
  133. batch_num,
  134. eth_block_num,
  135. forger_addr,
  136. fees_collected,
  137. state_root,
  138. num_accounts,
  139. exit_root,
  140. forge_l1_txs_num,
  141. slot_num
  142. ) VALUES %s;`,
  143. batches[:],
  144. )
  145. }
  146. // GetBatches retrieve batches from the DB, given a range of batch numbers defined by from and to
  147. func (hdb *HistoryDB) GetBatches(from, to common.BatchNum) ([]common.Batch, error) {
  148. var batches []*common.Batch
  149. err := meddler.QueryAll(
  150. hdb.db, &batches,
  151. "SELECT * FROM batch WHERE $1 <= batch_num AND batch_num < $2;",
  152. from, to,
  153. )
  154. return db.SlicePtrsToSlice(batches).([]common.Batch), err
  155. }
  156. // GetLastBatchNum returns the BatchNum of the latest forged batch
  157. func (hdb *HistoryDB) GetLastBatchNum() (common.BatchNum, error) {
  158. row := hdb.db.QueryRow("SELECT batch_num FROM batch ORDER BY batch_num DESC LIMIT 1;")
  159. var batchNum common.BatchNum
  160. return batchNum, row.Scan(&batchNum)
  161. }
  162. // GetLastL1TxsNum returns the greatest ForgeL1TxsNum in the DB. If there's no
  163. // batch in the DB (nil, nil) is returned.
  164. func (hdb *HistoryDB) GetLastL1TxsNum() (*int64, error) {
  165. row := hdb.db.QueryRow("SELECT MAX(forge_l1_txs_num) FROM batch;")
  166. lastL1TxsNum := new(int64)
  167. return lastL1TxsNum, row.Scan(&lastL1TxsNum)
  168. }
  169. // Reorg deletes all the information that was added into the DB after the
  170. // lastValidBlock. If lastValidBlock is negative, all block information is
  171. // deleted.
  172. func (hdb *HistoryDB) Reorg(lastValidBlock int64) error {
  173. var err error
  174. if lastValidBlock < 0 {
  175. _, err = hdb.db.Exec("DELETE FROM block;")
  176. } else {
  177. _, err = hdb.db.Exec("DELETE FROM block WHERE eth_block_num > $1;", lastValidBlock)
  178. }
  179. return err
  180. }
  181. // SyncPoD stores all the data that can be changed / added on a block in the PoD SC
  182. func (hdb *HistoryDB) SyncPoD(
  183. blockNum uint64,
  184. bids []common.Bid,
  185. coordinators []common.Coordinator,
  186. vars *common.AuctionVars,
  187. ) error {
  188. return nil
  189. }
  190. // AddBids insert Bids into the DB
  191. func (hdb *HistoryDB) AddBids(bids []common.Bid) error { return hdb.addBids(hdb.db, bids) }
  192. func (hdb *HistoryDB) addBids(d meddler.DB, bids []common.Bid) error {
  193. // TODO: check the coordinator info
  194. return db.BulkInsert(
  195. d,
  196. "INSERT INTO bid (slot_num, bid_value, eth_block_num, bidder_addr) VALUES %s;",
  197. bids[:],
  198. )
  199. }
  200. // GetBids return the bids
  201. func (hdb *HistoryDB) GetBids() ([]common.Bid, error) {
  202. var bids []*common.Bid
  203. err := meddler.QueryAll(
  204. hdb.db, &bids,
  205. "SELECT * FROM bid;",
  206. )
  207. return db.SlicePtrsToSlice(bids).([]common.Bid), err
  208. }
  209. // AddCoordinators insert Coordinators into the DB
  210. func (hdb *HistoryDB) AddCoordinators(coordinators []common.Coordinator) error {
  211. return hdb.addCoordinators(hdb.db, coordinators)
  212. }
  213. func (hdb *HistoryDB) addCoordinators(d meddler.DB, coordinators []common.Coordinator) error {
  214. return db.BulkInsert(
  215. d,
  216. "INSERT INTO coordinator (bidder_addr, forger_addr, eth_block_num, url) VALUES %s;",
  217. coordinators[:],
  218. )
  219. }
  220. // AddExitTree insert Exit tree into the DB
  221. func (hdb *HistoryDB) AddExitTree(exitTree []common.ExitInfo) error {
  222. return hdb.addExitTree(hdb.db, exitTree)
  223. }
  224. func (hdb *HistoryDB) addExitTree(d meddler.DB, exitTree []common.ExitInfo) error {
  225. return db.BulkInsert(
  226. d,
  227. "INSERT INTO exit_tree (batch_num, account_idx, merkle_proof, balance, "+
  228. "instant_withdrawn, delayed_withdraw_request, delayed_withdrawn) VALUES %s;",
  229. exitTree[:],
  230. )
  231. }
  232. // AddToken insert a token into the DB
  233. func (hdb *HistoryDB) AddToken(token *common.Token) error {
  234. return meddler.Insert(hdb.db, "token", token)
  235. }
  236. // AddTokens insert tokens into the DB
  237. func (hdb *HistoryDB) AddTokens(tokens []common.Token) error { return hdb.addTokens(hdb.db, tokens) }
  238. func (hdb *HistoryDB) addTokens(d meddler.DB, tokens []common.Token) error {
  239. return db.BulkInsert(
  240. d,
  241. `INSERT INTO token (
  242. token_id,
  243. eth_block_num,
  244. eth_addr,
  245. name,
  246. symbol,
  247. decimals
  248. ) VALUES %s;`,
  249. tokens[:],
  250. )
  251. }
  252. // UpdateTokenValue updates the USD value of a token
  253. func (hdb *HistoryDB) UpdateTokenValue(tokenSymbol string, value float64) error {
  254. _, err := hdb.db.Exec(
  255. "UPDATE token SET usd = $1 WHERE symbol = $2;",
  256. value, tokenSymbol,
  257. )
  258. return err
  259. }
  260. // GetToken returns a token from the DB given a TokenID
  261. func (hdb *HistoryDB) GetToken(tokenID common.TokenID) (*TokenRead, error) {
  262. token := &TokenRead{}
  263. err := meddler.QueryRow(
  264. hdb.db, token, `SELECT * FROM token WHERE token_id = $1;`, tokenID,
  265. )
  266. return token, err
  267. }
  268. // GetTokens returns a list of tokens from the DB
  269. func (hdb *HistoryDB) GetTokens(ids []common.TokenID, symbols []string, name string, fromItem, limit *uint, order string) ([]TokenRead, *db.Pagination, error) {
  270. var query string
  271. var args []interface{}
  272. queryStr := `SELECT * , COUNT(*) OVER() AS total_items, MIN(token.item_id) OVER() AS first_item, MAX(token.item_id) OVER() AS last_item FROM token `
  273. // Apply filters
  274. nextIsAnd := false
  275. if len(ids) > 0 {
  276. queryStr += "WHERE token_id IN (?) "
  277. nextIsAnd = true
  278. args = append(args, ids)
  279. }
  280. if len(symbols) > 0 {
  281. if nextIsAnd {
  282. queryStr += "AND "
  283. } else {
  284. queryStr += "WHERE "
  285. }
  286. queryStr += "symbol IN (?) "
  287. args = append(args, symbols)
  288. nextIsAnd = true
  289. }
  290. if name != "" {
  291. if nextIsAnd {
  292. queryStr += "AND "
  293. } else {
  294. queryStr += "WHERE "
  295. }
  296. queryStr += "name ~ ? "
  297. args = append(args, name)
  298. nextIsAnd = true
  299. }
  300. if fromItem != nil {
  301. if nextIsAnd {
  302. queryStr += "AND "
  303. } else {
  304. queryStr += "WHERE "
  305. }
  306. if order == OrderAsc {
  307. queryStr += "item_id >= ? "
  308. } else {
  309. queryStr += "item_id <= ? "
  310. }
  311. args = append(args, fromItem)
  312. }
  313. // pagination
  314. queryStr += "ORDER BY item_id "
  315. if order == OrderAsc {
  316. queryStr += "ASC "
  317. } else {
  318. queryStr += "DESC "
  319. }
  320. queryStr += fmt.Sprintf("LIMIT %d;", *limit)
  321. query, argsQ, err := sqlx.In(queryStr, args...)
  322. if err != nil {
  323. return nil, nil, err
  324. }
  325. query = hdb.db.Rebind(query)
  326. tokens := []*TokenRead{}
  327. if err := meddler.QueryAll(hdb.db, &tokens, query, argsQ...); err != nil {
  328. return nil, nil, err
  329. }
  330. if len(tokens) == 0 {
  331. return nil, nil, sql.ErrNoRows
  332. }
  333. return db.SlicePtrsToSlice(tokens).([]TokenRead), &db.Pagination{
  334. TotalItems: tokens[0].TotalItems,
  335. FirstItem: tokens[0].FirstItem,
  336. LastItem: tokens[0].LastItem,
  337. }, nil
  338. }
  339. // GetTokenSymbols returns all the token symbols from the DB
  340. func (hdb *HistoryDB) GetTokenSymbols() ([]string, error) {
  341. var tokenSymbols []string
  342. rows, err := hdb.db.Query("SELECT symbol FROM token;")
  343. if err != nil {
  344. return nil, err
  345. }
  346. sym := new(string)
  347. for rows.Next() {
  348. err = rows.Scan(sym)
  349. if err != nil {
  350. return nil, err
  351. }
  352. tokenSymbols = append(tokenSymbols, *sym)
  353. }
  354. return tokenSymbols, nil
  355. }
  356. // AddAccounts insert accounts into the DB
  357. func (hdb *HistoryDB) AddAccounts(accounts []common.Account) error {
  358. return hdb.addAccounts(hdb.db, accounts)
  359. }
  360. func (hdb *HistoryDB) addAccounts(d meddler.DB, accounts []common.Account) error {
  361. return db.BulkInsert(
  362. d,
  363. `INSERT INTO account (
  364. idx,
  365. token_id,
  366. batch_num,
  367. bjj,
  368. eth_addr
  369. ) VALUES %s;`,
  370. accounts[:],
  371. )
  372. }
  373. // GetAccounts returns a list of accounts from the DB
  374. func (hdb *HistoryDB) GetAccounts() ([]common.Account, error) {
  375. var accs []*common.Account
  376. err := meddler.QueryAll(
  377. hdb.db, &accs,
  378. "SELECT * FROM account ORDER BY idx;",
  379. )
  380. return db.SlicePtrsToSlice(accs).([]common.Account), err
  381. }
  382. // AddL1Txs inserts L1 txs to the DB. USD and LoadAmountUSD will be set automatically before storing the tx.
  383. // If the tx is originated by a coordinator, BatchNum must be provided. If it's originated by a user,
  384. // BatchNum should be null, and the value will be setted by a trigger when a batch forges the tx.
  385. func (hdb *HistoryDB) AddL1Txs(l1txs []common.L1Tx) error { return hdb.addL1Txs(hdb.db, l1txs) }
  386. // addL1Txs inserts L1 txs to the DB. USD and LoadAmountUSD will be set automatically before storing the tx.
  387. // If the tx is originated by a coordinator, BatchNum must be provided. If it's originated by a user,
  388. // BatchNum should be null, and the value will be setted by a trigger when a batch forges the tx.
  389. func (hdb *HistoryDB) addL1Txs(d meddler.DB, l1txs []common.L1Tx) error {
  390. txs := []txWrite{}
  391. for i := 0; i < len(l1txs); i++ {
  392. af := new(big.Float).SetInt(l1txs[i].Amount)
  393. amountFloat, _ := af.Float64()
  394. laf := new(big.Float).SetInt(l1txs[i].LoadAmount)
  395. loadAmountFloat, _ := laf.Float64()
  396. txs = append(txs, txWrite{
  397. // Generic
  398. IsL1: true,
  399. TxID: l1txs[i].TxID,
  400. Type: l1txs[i].Type,
  401. Position: l1txs[i].Position,
  402. FromIdx: &l1txs[i].FromIdx,
  403. ToIdx: l1txs[i].ToIdx,
  404. Amount: l1txs[i].Amount,
  405. AmountFloat: amountFloat,
  406. TokenID: l1txs[i].TokenID,
  407. BatchNum: l1txs[i].BatchNum,
  408. EthBlockNum: l1txs[i].EthBlockNum,
  409. // L1
  410. ToForgeL1TxsNum: l1txs[i].ToForgeL1TxsNum,
  411. UserOrigin: &l1txs[i].UserOrigin,
  412. FromEthAddr: &l1txs[i].FromEthAddr,
  413. FromBJJ: l1txs[i].FromBJJ,
  414. LoadAmount: l1txs[i].LoadAmount,
  415. LoadAmountFloat: &loadAmountFloat,
  416. })
  417. }
  418. return hdb.addTxs(d, txs)
  419. }
  420. // AddL2Txs inserts L2 txs to the DB. TokenID, USD and FeeUSD will be set automatically before storing the tx.
  421. func (hdb *HistoryDB) AddL2Txs(l2txs []common.L2Tx) error { return hdb.addL2Txs(hdb.db, l2txs) }
  422. // addL2Txs inserts L2 txs to the DB. TokenID, USD and FeeUSD will be set automatically before storing the tx.
  423. func (hdb *HistoryDB) addL2Txs(d meddler.DB, l2txs []common.L2Tx) error {
  424. txs := []txWrite{}
  425. for i := 0; i < len(l2txs); i++ {
  426. f := new(big.Float).SetInt(l2txs[i].Amount)
  427. amountFloat, _ := f.Float64()
  428. txs = append(txs, txWrite{
  429. // Generic
  430. IsL1: false,
  431. TxID: l2txs[i].TxID,
  432. Type: l2txs[i].Type,
  433. Position: l2txs[i].Position,
  434. FromIdx: &l2txs[i].FromIdx,
  435. ToIdx: l2txs[i].ToIdx,
  436. Amount: l2txs[i].Amount,
  437. AmountFloat: amountFloat,
  438. BatchNum: &l2txs[i].BatchNum,
  439. EthBlockNum: l2txs[i].EthBlockNum,
  440. // L2
  441. Fee: &l2txs[i].Fee,
  442. Nonce: &l2txs[i].Nonce,
  443. })
  444. }
  445. return hdb.addTxs(d, txs)
  446. }
  447. func (hdb *HistoryDB) addTxs(d meddler.DB, txs []txWrite) error {
  448. return db.BulkInsert(
  449. d,
  450. `INSERT INTO tx (
  451. is_l1,
  452. id,
  453. type,
  454. position,
  455. from_idx,
  456. to_idx,
  457. amount,
  458. amount_f,
  459. token_id,
  460. batch_num,
  461. eth_block_num,
  462. to_forge_l1_txs_num,
  463. user_origin,
  464. from_eth_addr,
  465. from_bjj,
  466. load_amount,
  467. load_amount_f,
  468. fee,
  469. nonce
  470. ) VALUES %s;`,
  471. txs[:],
  472. )
  473. }
  474. // // GetTxs returns a list of txs from the DB
  475. // func (hdb *HistoryDB) GetTxs() ([]common.Tx, error) {
  476. // var txs []*common.Tx
  477. // err := meddler.QueryAll(
  478. // hdb.db, &txs,
  479. // `SELECT * FROM tx
  480. // ORDER BY (batch_num, position) ASC`,
  481. // )
  482. // return db.SlicePtrsToSlice(txs).([]common.Tx), err
  483. // }
  484. // GetHistoryTx returns a tx from the DB given a TxID
  485. func (hdb *HistoryDB) GetHistoryTx(txID common.TxID) (*HistoryTx, error) {
  486. tx := &HistoryTx{}
  487. err := meddler.QueryRow(
  488. hdb.db, tx, `SELECT tx.item_id, tx.is_l1, tx.id, tx.type, tx.position,
  489. tx.from_idx, tx.to_idx, tx.amount, tx.token_id, tx.amount_usd,
  490. tx.batch_num, tx.eth_block_num, tx.to_forge_l1_txs_num, tx.user_origin,
  491. tx.from_eth_addr, tx.from_bjj, tx.load_amount,
  492. tx.load_amount_usd, tx.fee, tx.fee_usd, tx.nonce,
  493. token.token_id, token.eth_block_num AS token_block,
  494. token.eth_addr, token.name, token.symbol, token.decimals, token.usd,
  495. token.usd_update, block.timestamp
  496. FROM tx INNER JOIN token ON tx.token_id = token.token_id
  497. INNER JOIN block ON tx.eth_block_num = block.eth_block_num
  498. WHERE tx.id = $1;`, txID,
  499. )
  500. return tx, err
  501. }
  502. // GetHistoryTxs returns a list of txs from the DB using the HistoryTx struct
  503. // and pagination info
  504. func (hdb *HistoryDB) GetHistoryTxs(
  505. ethAddr *ethCommon.Address, bjj *babyjub.PublicKey,
  506. tokenID *common.TokenID, idx *common.Idx, batchNum *uint, txType *common.TxType,
  507. fromItem, limit *uint, order string,
  508. ) ([]HistoryTx, *db.Pagination, error) {
  509. if ethAddr != nil && bjj != nil {
  510. return nil, nil, errors.New("ethAddr and bjj are incompatible")
  511. }
  512. var query string
  513. var args []interface{}
  514. queryStr := `SELECT tx.item_id, tx.is_l1, tx.id, tx.type, tx.position,
  515. tx.from_idx, tx.to_idx, tx.amount, tx.token_id, tx.amount_usd,
  516. tx.batch_num, tx.eth_block_num, tx.to_forge_l1_txs_num, tx.user_origin,
  517. tx.from_eth_addr, tx.from_bjj, tx.load_amount,
  518. tx.load_amount_usd, tx.fee, tx.fee_usd, tx.nonce,
  519. token.token_id, token.eth_block_num AS token_block,
  520. token.eth_addr, token.name, token.symbol, token.decimals, token.usd,
  521. token.usd_update, block.timestamp, count(*) OVER() AS total_items,
  522. MIN(tx.item_id) OVER() AS first_item, MAX(tx.item_id) OVER() AS last_item
  523. FROM tx INNER JOIN token ON tx.token_id = token.token_id
  524. INNER JOIN block ON tx.eth_block_num = block.eth_block_num `
  525. // Apply filters
  526. nextIsAnd := false
  527. // ethAddr filter
  528. if ethAddr != nil {
  529. queryStr = `WITH acc AS
  530. (select idx from account where eth_addr = ?) ` + queryStr
  531. queryStr += ", acc WHERE (tx.from_idx IN(acc.idx) OR tx.to_idx IN(acc.idx)) "
  532. nextIsAnd = true
  533. args = append(args, ethAddr)
  534. } else if bjj != nil { // bjj filter
  535. queryStr = `WITH acc AS
  536. (select idx from account where bjj = ?) ` + queryStr
  537. queryStr += ", acc WHERE (tx.from_idx IN(acc.idx) OR tx.to_idx IN(acc.idx)) "
  538. nextIsAnd = true
  539. args = append(args, bjj)
  540. }
  541. // tokenID filter
  542. if tokenID != nil {
  543. if nextIsAnd {
  544. queryStr += "AND "
  545. } else {
  546. queryStr += "WHERE "
  547. }
  548. queryStr += "tx.token_id = ? "
  549. args = append(args, tokenID)
  550. nextIsAnd = true
  551. }
  552. // idx filter
  553. if idx != nil {
  554. if nextIsAnd {
  555. queryStr += "AND "
  556. } else {
  557. queryStr += "WHERE "
  558. }
  559. queryStr += "(tx.from_idx = ? OR tx.to_idx = ?) "
  560. args = append(args, idx, idx)
  561. nextIsAnd = true
  562. }
  563. // batchNum filter
  564. if batchNum != nil {
  565. if nextIsAnd {
  566. queryStr += "AND "
  567. } else {
  568. queryStr += "WHERE "
  569. }
  570. queryStr += "tx.batch_num = ? "
  571. args = append(args, batchNum)
  572. nextIsAnd = true
  573. }
  574. // txType filter
  575. if txType != nil {
  576. if nextIsAnd {
  577. queryStr += "AND "
  578. } else {
  579. queryStr += "WHERE "
  580. }
  581. queryStr += "tx.type = ? "
  582. args = append(args, txType)
  583. nextIsAnd = true
  584. }
  585. if fromItem != nil {
  586. if nextIsAnd {
  587. queryStr += "AND "
  588. } else {
  589. queryStr += "WHERE "
  590. }
  591. if order == OrderAsc {
  592. queryStr += "tx.item_id >= ? "
  593. } else {
  594. queryStr += "tx.item_id <= ? "
  595. }
  596. args = append(args, fromItem)
  597. nextIsAnd = true
  598. }
  599. if nextIsAnd {
  600. queryStr += "AND "
  601. } else {
  602. queryStr += "WHERE "
  603. }
  604. queryStr += "tx.batch_num IS NOT NULL "
  605. // pagination
  606. queryStr += "ORDER BY tx.item_id "
  607. if order == OrderAsc {
  608. queryStr += " ASC "
  609. } else {
  610. queryStr += " DESC "
  611. }
  612. queryStr += fmt.Sprintf("LIMIT %d;", *limit)
  613. query = hdb.db.Rebind(queryStr)
  614. log.Debug(query)
  615. txsPtrs := []*HistoryTx{}
  616. if err := meddler.QueryAll(hdb.db, &txsPtrs, query, args...); err != nil {
  617. return nil, nil, err
  618. }
  619. txs := db.SlicePtrsToSlice(txsPtrs).([]HistoryTx)
  620. if len(txs) == 0 {
  621. return nil, nil, sql.ErrNoRows
  622. }
  623. return txs, &db.Pagination{
  624. TotalItems: txs[0].TotalItems,
  625. FirstItem: txs[0].FirstItem,
  626. LastItem: txs[0].LastItem,
  627. }, nil
  628. }
  629. // GetExit returns a exit from the DB
  630. func (hdb *HistoryDB) GetExit(batchNum *uint, idx *common.Idx) (*HistoryExit, error) {
  631. exit := &HistoryExit{}
  632. err := meddler.QueryRow(
  633. hdb.db, exit, `SELECT exit_tree.*, token.token_id, token.eth_block_num AS token_block,
  634. token.eth_addr, token.name, token.symbol, token.decimals, token.usd, token.usd_update
  635. FROM exit_tree INNER JOIN account ON exit_tree.account_idx = account.idx
  636. INNER JOIN token ON account.token_id = token.token_id
  637. WHERE exit_tree.batch_num = $1 AND exit_tree.account_idx = $2;`, batchNum, idx,
  638. )
  639. return exit, err
  640. }
  641. // GetExits returns a list of exits from the DB and pagination info
  642. func (hdb *HistoryDB) GetExits(
  643. ethAddr *ethCommon.Address, bjj *babyjub.PublicKey,
  644. tokenID *common.TokenID, idx *common.Idx, batchNum *uint,
  645. fromItem, limit *uint, order string,
  646. ) ([]HistoryExit, *db.Pagination, error) {
  647. if ethAddr != nil && bjj != nil {
  648. return nil, nil, errors.New("ethAddr and bjj are incompatible")
  649. }
  650. var query string
  651. var args []interface{}
  652. queryStr := `SELECT exit_tree.*, token.token_id, token.eth_block_num AS token_block,
  653. token.eth_addr, token.name, token.symbol, token.decimals, token.usd,
  654. token.usd_update, COUNT(*) OVER() AS total_items, MIN(exit_tree.item_id) OVER() AS first_item, MAX(exit_tree.item_id) OVER() AS last_item
  655. FROM exit_tree INNER JOIN account ON exit_tree.account_idx = account.idx
  656. INNER JOIN token ON account.token_id = token.token_id `
  657. // Apply filters
  658. nextIsAnd := false
  659. // ethAddr filter
  660. if ethAddr != nil {
  661. queryStr += "WHERE account.eth_addr = ? "
  662. nextIsAnd = true
  663. args = append(args, ethAddr)
  664. } else if bjj != nil { // bjj filter
  665. queryStr += "WHERE account.bjj = ? "
  666. nextIsAnd = true
  667. args = append(args, bjj)
  668. }
  669. // tokenID filter
  670. if tokenID != nil {
  671. if nextIsAnd {
  672. queryStr += "AND "
  673. } else {
  674. queryStr += "WHERE "
  675. }
  676. queryStr += "account.token_id = ? "
  677. args = append(args, tokenID)
  678. nextIsAnd = true
  679. }
  680. // idx filter
  681. if idx != nil {
  682. if nextIsAnd {
  683. queryStr += "AND "
  684. } else {
  685. queryStr += "WHERE "
  686. }
  687. queryStr += "exit_tree.account_idx = ? "
  688. args = append(args, idx)
  689. nextIsAnd = true
  690. }
  691. // batchNum filter
  692. if batchNum != nil {
  693. if nextIsAnd {
  694. queryStr += "AND "
  695. } else {
  696. queryStr += "WHERE "
  697. }
  698. queryStr += "exit_tree.batch_num = ? "
  699. args = append(args, batchNum)
  700. nextIsAnd = true
  701. }
  702. if fromItem != nil {
  703. if nextIsAnd {
  704. queryStr += "AND "
  705. } else {
  706. queryStr += "WHERE "
  707. }
  708. if order == OrderAsc {
  709. queryStr += "exit_tree.item_id >= ? "
  710. } else {
  711. queryStr += "exit_tree.item_id <= ? "
  712. }
  713. args = append(args, fromItem)
  714. // nextIsAnd = true
  715. }
  716. // pagination
  717. queryStr += "ORDER BY exit_tree.item_id "
  718. if order == OrderAsc {
  719. queryStr += " ASC "
  720. } else {
  721. queryStr += " DESC "
  722. }
  723. queryStr += fmt.Sprintf("LIMIT %d;", *limit)
  724. query = hdb.db.Rebind(queryStr)
  725. // log.Debug(query)
  726. exits := []*HistoryExit{}
  727. if err := meddler.QueryAll(hdb.db, &exits, query, args...); err != nil {
  728. return nil, nil, err
  729. }
  730. if len(exits) == 0 {
  731. return nil, nil, sql.ErrNoRows
  732. }
  733. return db.SlicePtrsToSlice(exits).([]HistoryExit), &db.Pagination{
  734. TotalItems: exits[0].TotalItems,
  735. FirstItem: exits[0].FirstItem,
  736. LastItem: exits[0].LastItem,
  737. }, nil
  738. }
  739. // // GetTx returns a tx from the DB
  740. // func (hdb *HistoryDB) GetTx(txID common.TxID) (*common.Tx, error) {
  741. // tx := new(common.Tx)
  742. // return tx, meddler.QueryRow(
  743. // hdb.db, tx,
  744. // "SELECT * FROM tx WHERE id = $1;",
  745. // txID,
  746. // )
  747. // }
  748. // // GetL1UserTxs gets L1 User Txs to be forged in a batch that will create an account
  749. // // TODO: This is currently not used. Figure out if it should be used somewhere or removed.
  750. // func (hdb *HistoryDB) GetL1UserTxs(toForgeL1TxsNum int64) ([]*common.Tx, error) {
  751. // var txs []*common.Tx
  752. // err := meddler.QueryAll(
  753. // hdb.db, &txs,
  754. // "SELECT * FROM tx WHERE to_forge_l1_txs_num = $1 AND is_l1 = TRUE AND user_origin = TRUE;",
  755. // toForgeL1TxsNum,
  756. // )
  757. // return txs, err
  758. // }
  759. // TODO: Think about chaning all the queries that return a last value, to queries that return the next valid value.
  760. // GetLastTxsPosition for a given to_forge_l1_txs_num
  761. func (hdb *HistoryDB) GetLastTxsPosition(toForgeL1TxsNum int64) (int, error) {
  762. row := hdb.db.QueryRow("SELECT MAX(position) FROM tx WHERE to_forge_l1_txs_num = $1;", toForgeL1TxsNum)
  763. var lastL1TxsPosition int
  764. return lastL1TxsPosition, row.Scan(&lastL1TxsPosition)
  765. }
  766. // AddBlockSCData stores all the information of a block retrieved by the Synchronizer
  767. func (hdb *HistoryDB) AddBlockSCData(blockData *BlockData) (err error) {
  768. txn, err := hdb.db.Begin()
  769. if err != nil {
  770. return err
  771. }
  772. defer func() {
  773. if err != nil {
  774. errRollback := txn.Rollback()
  775. if errRollback != nil {
  776. log.Errorw("Rollback", "err", errRollback)
  777. }
  778. }
  779. }()
  780. // Add block
  781. err = hdb.addBlock(txn, blockData.Block)
  782. if err != nil {
  783. return err
  784. }
  785. // Add Coordinators
  786. if len(blockData.Coordinators) > 0 {
  787. err = hdb.addCoordinators(txn, blockData.Coordinators)
  788. if err != nil {
  789. return err
  790. }
  791. }
  792. // Add Bids
  793. if len(blockData.Bids) > 0 {
  794. err = hdb.addBids(txn, blockData.Bids)
  795. if err != nil {
  796. return err
  797. }
  798. }
  799. // Add Tokens
  800. if len(blockData.RegisteredTokens) > 0 {
  801. err = hdb.addTokens(txn, blockData.RegisteredTokens)
  802. if err != nil {
  803. return err
  804. }
  805. }
  806. // Add l1 Txs
  807. if len(blockData.L1UserTxs) > 0 {
  808. err = hdb.addL1Txs(txn, blockData.L1UserTxs)
  809. if err != nil {
  810. return err
  811. }
  812. }
  813. // Add Batches
  814. for _, batch := range blockData.Batches {
  815. // Add Batch: this will trigger an update on the DB
  816. // that will set the batch num of forged L1 txs in this batch
  817. err = hdb.addBatch(txn, batch.Batch)
  818. if err != nil {
  819. return err
  820. }
  821. // Add unforged l1 Txs
  822. if batch.L1Batch {
  823. if len(batch.L1CoordinatorTxs) > 0 {
  824. err = hdb.addL1Txs(txn, batch.L1CoordinatorTxs)
  825. if err != nil {
  826. return err
  827. }
  828. }
  829. }
  830. // Add l2 Txs
  831. if len(batch.L2Txs) > 0 {
  832. err = hdb.addL2Txs(txn, batch.L2Txs)
  833. if err != nil {
  834. return err
  835. }
  836. }
  837. // Add accounts
  838. if len(batch.CreatedAccounts) > 0 {
  839. err = hdb.addAccounts(txn, batch.CreatedAccounts)
  840. if err != nil {
  841. return err
  842. }
  843. }
  844. // Add exit tree
  845. if len(batch.ExitTree) > 0 {
  846. err = hdb.addExitTree(txn, batch.ExitTree)
  847. if err != nil {
  848. return err
  849. }
  850. }
  851. // TODO: INSERT CONTRACTS VARS
  852. }
  853. return txn.Commit()
  854. }