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.

1111 lines
31 KiB

Update missing parts, improve til, and more - Node - Updated configuration to initialize the interface to all the smart contracts - Common - Moved BlockData and BatchData types to common so that they can be shared among: historydb, til and synchronizer - Remove hash.go (it was never used) - Remove slot.go (it was never used) - Remove smartcontractparams.go (it was never used, and appropriate structs are defined in `eth/`) - Comment state / status method until requirements of this method are properly defined, and move it to Synchronizer - Synchronizer - Simplify `Sync` routine to only sync one block per call, and return useful information. - Use BlockData and BatchData from common - Check that events belong to the expected block hash - In L1Batch, query L1UserTxs from HistoryDB - Fill ERC20 token information - Test AddTokens with test.Client - HistryDB - Use BlockData and BatchData from common - Add `GetAllTokens` method - Uncomment and update GetL1UserTxs (with corresponding tests) - Til - Rename all instances of RegisterToken to AddToken (to follow the smart contract implementation naming) - Use BlockData and BatchData from common - Move testL1CoordinatorTxs and testL2Txs to a separate struct from BatchData in Context - Start Context with BatchNum = 1 (which the protocol defines to be the first batchNum) - In every Batch, set StateRoot and ExitRoot to a non-nil big.Int (zero). - In all L1Txs, if LoadAmount is not used, set it to 0; if Amount is not used, set it to 0; so that no *big.Int is nil. - In L1UserTx, don't set BatchNum, because when L1UserTxs are created and obtained by the synchronizer, the BatchNum is not known yet (it's a synchronizer job to set it) - In L1UserTxs, set `UserOrigin` and set `ToForgeL1TxsNum`.
4 years ago
Update missing parts, improve til, and more - Node - Updated configuration to initialize the interface to all the smart contracts - Common - Moved BlockData and BatchData types to common so that they can be shared among: historydb, til and synchronizer - Remove hash.go (it was never used) - Remove slot.go (it was never used) - Remove smartcontractparams.go (it was never used, and appropriate structs are defined in `eth/`) - Comment state / status method until requirements of this method are properly defined, and move it to Synchronizer - Synchronizer - Simplify `Sync` routine to only sync one block per call, and return useful information. - Use BlockData and BatchData from common - Check that events belong to the expected block hash - In L1Batch, query L1UserTxs from HistoryDB - Fill ERC20 token information - Test AddTokens with test.Client - HistryDB - Use BlockData and BatchData from common - Add `GetAllTokens` method - Uncomment and update GetL1UserTxs (with corresponding tests) - Til - Rename all instances of RegisterToken to AddToken (to follow the smart contract implementation naming) - Use BlockData and BatchData from common - Move testL1CoordinatorTxs and testL2Txs to a separate struct from BatchData in Context - Start Context with BatchNum = 1 (which the protocol defines to be the first batchNum) - In every Batch, set StateRoot and ExitRoot to a non-nil big.Int (zero). - In all L1Txs, if LoadAmount is not used, set it to 0; if Amount is not used, set it to 0; so that no *big.Int is nil. - In L1UserTx, don't set BatchNum, because when L1UserTxs are created and obtained by the synchronizer, the BatchNum is not known yet (it's a synchronizer job to set it) - In L1UserTxs, set `UserOrigin` and set `ToForgeL1TxsNum`.
4 years ago
Update missing parts, improve til, and more - Node - Updated configuration to initialize the interface to all the smart contracts - Common - Moved BlockData and BatchData types to common so that they can be shared among: historydb, til and synchronizer - Remove hash.go (it was never used) - Remove slot.go (it was never used) - Remove smartcontractparams.go (it was never used, and appropriate structs are defined in `eth/`) - Comment state / status method until requirements of this method are properly defined, and move it to Synchronizer - Synchronizer - Simplify `Sync` routine to only sync one block per call, and return useful information. - Use BlockData and BatchData from common - Check that events belong to the expected block hash - In L1Batch, query L1UserTxs from HistoryDB - Fill ERC20 token information - Test AddTokens with test.Client - HistryDB - Use BlockData and BatchData from common - Add `GetAllTokens` method - Uncomment and update GetL1UserTxs (with corresponding tests) - Til - Rename all instances of RegisterToken to AddToken (to follow the smart contract implementation naming) - Use BlockData and BatchData from common - Move testL1CoordinatorTxs and testL2Txs to a separate struct from BatchData in Context - Start Context with BatchNum = 1 (which the protocol defines to be the first batchNum) - In every Batch, set StateRoot and ExitRoot to a non-nil big.Int (zero). - In all L1Txs, if LoadAmount is not used, set it to 0; if Amount is not used, set it to 0; so that no *big.Int is nil. - In L1UserTx, don't set BatchNum, because when L1UserTxs are created and obtained by the synchronizer, the BatchNum is not known yet (it's a synchronizer job to set it) - In L1UserTxs, set `UserOrigin` and set `ToForgeL1TxsNum`.
4 years ago
Update missing parts, improve til, and more - Node - Updated configuration to initialize the interface to all the smart contracts - Common - Moved BlockData and BatchData types to common so that they can be shared among: historydb, til and synchronizer - Remove hash.go (it was never used) - Remove slot.go (it was never used) - Remove smartcontractparams.go (it was never used, and appropriate structs are defined in `eth/`) - Comment state / status method until requirements of this method are properly defined, and move it to Synchronizer - Synchronizer - Simplify `Sync` routine to only sync one block per call, and return useful information. - Use BlockData and BatchData from common - Check that events belong to the expected block hash - In L1Batch, query L1UserTxs from HistoryDB - Fill ERC20 token information - Test AddTokens with test.Client - HistryDB - Use BlockData and BatchData from common - Add `GetAllTokens` method - Uncomment and update GetL1UserTxs (with corresponding tests) - Til - Rename all instances of RegisterToken to AddToken (to follow the smart contract implementation naming) - Use BlockData and BatchData from common - Move testL1CoordinatorTxs and testL2Txs to a separate struct from BatchData in Context - Start Context with BatchNum = 1 (which the protocol defines to be the first batchNum) - In every Batch, set StateRoot and ExitRoot to a non-nil big.Int (zero). - In all L1Txs, if LoadAmount is not used, set it to 0; if Amount is not used, set it to 0; so that no *big.Int is nil. - In L1UserTx, don't set BatchNum, because when L1UserTxs are created and obtained by the synchronizer, the BatchNum is not known yet (it's a synchronizer job to set it) - In L1UserTxs, set `UserOrigin` and set `ToForgeL1TxsNum`.
4 years ago
Update missing parts, improve til, and more - Node - Updated configuration to initialize the interface to all the smart contracts - Common - Moved BlockData and BatchData types to common so that they can be shared among: historydb, til and synchronizer - Remove hash.go (it was never used) - Remove slot.go (it was never used) - Remove smartcontractparams.go (it was never used, and appropriate structs are defined in `eth/`) - Comment state / status method until requirements of this method are properly defined, and move it to Synchronizer - Synchronizer - Simplify `Sync` routine to only sync one block per call, and return useful information. - Use BlockData and BatchData from common - Check that events belong to the expected block hash - In L1Batch, query L1UserTxs from HistoryDB - Fill ERC20 token information - Test AddTokens with test.Client - HistryDB - Use BlockData and BatchData from common - Add `GetAllTokens` method - Uncomment and update GetL1UserTxs (with corresponding tests) - Til - Rename all instances of RegisterToken to AddToken (to follow the smart contract implementation naming) - Use BlockData and BatchData from common - Move testL1CoordinatorTxs and testL2Txs to a separate struct from BatchData in Context - Start Context with BatchNum = 1 (which the protocol defines to be the first batchNum) - In every Batch, set StateRoot and ExitRoot to a non-nil big.Int (zero). - In all L1Txs, if LoadAmount is not used, set it to 0; if Amount is not used, set it to 0; so that no *big.Int is nil. - In L1UserTx, don't set BatchNum, because when L1UserTxs are created and obtained by the synchronizer, the BatchNum is not known yet (it's a synchronizer job to set it) - In L1UserTxs, set `UserOrigin` and set `ToForgeL1TxsNum`.
4 years ago
Update missing parts, improve til, and more - Node - Updated configuration to initialize the interface to all the smart contracts - Common - Moved BlockData and BatchData types to common so that they can be shared among: historydb, til and synchronizer - Remove hash.go (it was never used) - Remove slot.go (it was never used) - Remove smartcontractparams.go (it was never used, and appropriate structs are defined in `eth/`) - Comment state / status method until requirements of this method are properly defined, and move it to Synchronizer - Synchronizer - Simplify `Sync` routine to only sync one block per call, and return useful information. - Use BlockData and BatchData from common - Check that events belong to the expected block hash - In L1Batch, query L1UserTxs from HistoryDB - Fill ERC20 token information - Test AddTokens with test.Client - HistryDB - Use BlockData and BatchData from common - Add `GetAllTokens` method - Uncomment and update GetL1UserTxs (with corresponding tests) - Til - Rename all instances of RegisterToken to AddToken (to follow the smart contract implementation naming) - Use BlockData and BatchData from common - Move testL1CoordinatorTxs and testL2Txs to a separate struct from BatchData in Context - Start Context with BatchNum = 1 (which the protocol defines to be the first batchNum) - In every Batch, set StateRoot and ExitRoot to a non-nil big.Int (zero). - In all L1Txs, if LoadAmount is not used, set it to 0; if Amount is not used, set it to 0; so that no *big.Int is nil. - In L1UserTx, don't set BatchNum, because when L1UserTxs are created and obtained by the synchronizer, the BatchNum is not known yet (it's a synchronizer job to set it) - In L1UserTxs, set `UserOrigin` and set `ToForgeL1TxsNum`.
4 years ago
Update missing parts, improve til, and more - Node - Updated configuration to initialize the interface to all the smart contracts - Common - Moved BlockData and BatchData types to common so that they can be shared among: historydb, til and synchronizer - Remove hash.go (it was never used) - Remove slot.go (it was never used) - Remove smartcontractparams.go (it was never used, and appropriate structs are defined in `eth/`) - Comment state / status method until requirements of this method are properly defined, and move it to Synchronizer - Synchronizer - Simplify `Sync` routine to only sync one block per call, and return useful information. - Use BlockData and BatchData from common - Check that events belong to the expected block hash - In L1Batch, query L1UserTxs from HistoryDB - Fill ERC20 token information - Test AddTokens with test.Client - HistryDB - Use BlockData and BatchData from common - Add `GetAllTokens` method - Uncomment and update GetL1UserTxs (with corresponding tests) - Til - Rename all instances of RegisterToken to AddToken (to follow the smart contract implementation naming) - Use BlockData and BatchData from common - Move testL1CoordinatorTxs and testL2Txs to a separate struct from BatchData in Context - Start Context with BatchNum = 1 (which the protocol defines to be the first batchNum) - In every Batch, set StateRoot and ExitRoot to a non-nil big.Int (zero). - In all L1Txs, if LoadAmount is not used, set it to 0; if Amount is not used, set it to 0; so that no *big.Int is nil. - In L1UserTx, don't set BatchNum, because when L1UserTxs are created and obtained by the synchronizer, the BatchNum is not known yet (it's a synchronizer job to set it) - In L1UserTxs, set `UserOrigin` and set `ToForgeL1TxsNum`.
4 years ago
Update missing parts, improve til, and more - Node - Updated configuration to initialize the interface to all the smart contracts - Common - Moved BlockData and BatchData types to common so that they can be shared among: historydb, til and synchronizer - Remove hash.go (it was never used) - Remove slot.go (it was never used) - Remove smartcontractparams.go (it was never used, and appropriate structs are defined in `eth/`) - Comment state / status method until requirements of this method are properly defined, and move it to Synchronizer - Synchronizer - Simplify `Sync` routine to only sync one block per call, and return useful information. - Use BlockData and BatchData from common - Check that events belong to the expected block hash - In L1Batch, query L1UserTxs from HistoryDB - Fill ERC20 token information - Test AddTokens with test.Client - HistryDB - Use BlockData and BatchData from common - Add `GetAllTokens` method - Uncomment and update GetL1UserTxs (with corresponding tests) - Til - Rename all instances of RegisterToken to AddToken (to follow the smart contract implementation naming) - Use BlockData and BatchData from common - Move testL1CoordinatorTxs and testL2Txs to a separate struct from BatchData in Context - Start Context with BatchNum = 1 (which the protocol defines to be the first batchNum) - In every Batch, set StateRoot and ExitRoot to a non-nil big.Int (zero). - In all L1Txs, if LoadAmount is not used, set it to 0; if Amount is not used, set it to 0; so that no *big.Int is nil. - In L1UserTx, don't set BatchNum, because when L1UserTxs are created and obtained by the synchronizer, the BatchNum is not known yet (it's a synchronizer job to set it) - In L1UserTxs, set `UserOrigin` and set `ToForgeL1TxsNum`.
4 years ago
Update missing parts, improve til, and more - Node - Updated configuration to initialize the interface to all the smart contracts - Common - Moved BlockData and BatchData types to common so that they can be shared among: historydb, til and synchronizer - Remove hash.go (it was never used) - Remove slot.go (it was never used) - Remove smartcontractparams.go (it was never used, and appropriate structs are defined in `eth/`) - Comment state / status method until requirements of this method are properly defined, and move it to Synchronizer - Synchronizer - Simplify `Sync` routine to only sync one block per call, and return useful information. - Use BlockData and BatchData from common - Check that events belong to the expected block hash - In L1Batch, query L1UserTxs from HistoryDB - Fill ERC20 token information - Test AddTokens with test.Client - HistryDB - Use BlockData and BatchData from common - Add `GetAllTokens` method - Uncomment and update GetL1UserTxs (with corresponding tests) - Til - Rename all instances of RegisterToken to AddToken (to follow the smart contract implementation naming) - Use BlockData and BatchData from common - Move testL1CoordinatorTxs and testL2Txs to a separate struct from BatchData in Context - Start Context with BatchNum = 1 (which the protocol defines to be the first batchNum) - In every Batch, set StateRoot and ExitRoot to a non-nil big.Int (zero). - In all L1Txs, if LoadAmount is not used, set it to 0; if Amount is not used, set it to 0; so that no *big.Int is nil. - In L1UserTx, don't set BatchNum, because when L1UserTxs are created and obtained by the synchronizer, the BatchNum is not known yet (it's a synchronizer job to set it) - In L1UserTxs, set `UserOrigin` and set `ToForgeL1TxsNum`.
4 years ago
Update missing parts, improve til, and more - Node - Updated configuration to initialize the interface to all the smart contracts - Common - Moved BlockData and BatchData types to common so that they can be shared among: historydb, til and synchronizer - Remove hash.go (it was never used) - Remove slot.go (it was never used) - Remove smartcontractparams.go (it was never used, and appropriate structs are defined in `eth/`) - Comment state / status method until requirements of this method are properly defined, and move it to Synchronizer - Synchronizer - Simplify `Sync` routine to only sync one block per call, and return useful information. - Use BlockData and BatchData from common - Check that events belong to the expected block hash - In L1Batch, query L1UserTxs from HistoryDB - Fill ERC20 token information - Test AddTokens with test.Client - HistryDB - Use BlockData and BatchData from common - Add `GetAllTokens` method - Uncomment and update GetL1UserTxs (with corresponding tests) - Til - Rename all instances of RegisterToken to AddToken (to follow the smart contract implementation naming) - Use BlockData and BatchData from common - Move testL1CoordinatorTxs and testL2Txs to a separate struct from BatchData in Context - Start Context with BatchNum = 1 (which the protocol defines to be the first batchNum) - In every Batch, set StateRoot and ExitRoot to a non-nil big.Int (zero). - In all L1Txs, if LoadAmount is not used, set it to 0; if Amount is not used, set it to 0; so that no *big.Int is nil. - In L1UserTx, don't set BatchNum, because when L1UserTxs are created and obtained by the synchronizer, the BatchNum is not known yet (it's a synchronizer job to set it) - In L1UserTxs, set `UserOrigin` and set `ToForgeL1TxsNum`.
4 years ago
Update missing parts, improve til, and more - Node - Updated configuration to initialize the interface to all the smart contracts - Common - Moved BlockData and BatchData types to common so that they can be shared among: historydb, til and synchronizer - Remove hash.go (it was never used) - Remove slot.go (it was never used) - Remove smartcontractparams.go (it was never used, and appropriate structs are defined in `eth/`) - Comment state / status method until requirements of this method are properly defined, and move it to Synchronizer - Synchronizer - Simplify `Sync` routine to only sync one block per call, and return useful information. - Use BlockData and BatchData from common - Check that events belong to the expected block hash - In L1Batch, query L1UserTxs from HistoryDB - Fill ERC20 token information - Test AddTokens with test.Client - HistryDB - Use BlockData and BatchData from common - Add `GetAllTokens` method - Uncomment and update GetL1UserTxs (with corresponding tests) - Til - Rename all instances of RegisterToken to AddToken (to follow the smart contract implementation naming) - Use BlockData and BatchData from common - Move testL1CoordinatorTxs and testL2Txs to a separate struct from BatchData in Context - Start Context with BatchNum = 1 (which the protocol defines to be the first batchNum) - In every Batch, set StateRoot and ExitRoot to a non-nil big.Int (zero). - In all L1Txs, if LoadAmount is not used, set it to 0; if Amount is not used, set it to 0; so that no *big.Int is nil. - In L1UserTx, don't set BatchNum, because when L1UserTxs are created and obtained by the synchronizer, the BatchNum is not known yet (it's a synchronizer job to set it) - In L1UserTxs, set `UserOrigin` and set `ToForgeL1TxsNum`.
4 years ago
  1. package historydb
  2. import (
  3. "database/sql"
  4. "errors"
  5. "fmt"
  6. "math"
  7. "math/big"
  8. ethCommon "github.com/ethereum/go-ethereum/common"
  9. "github.com/hermeznetwork/hermez-node/common"
  10. "github.com/hermeznetwork/hermez-node/db"
  11. "github.com/hermeznetwork/hermez-node/log"
  12. "github.com/iden3/go-iden3-crypto/babyjub"
  13. "github.com/jmoiron/sqlx"
  14. //nolint:errcheck // driver for postgres DB
  15. _ "github.com/lib/pq"
  16. "github.com/russross/meddler"
  17. )
  18. const (
  19. // OrderAsc indicates ascending order when using pagination
  20. OrderAsc = "ASC"
  21. // OrderDesc indicates descending order when using pagination
  22. OrderDesc = "DESC"
  23. )
  24. // TODO(Edu): Document here how HistoryDB is kept consistent
  25. // HistoryDB persist the historic of the rollup
  26. type HistoryDB struct {
  27. db *sqlx.DB
  28. }
  29. // NewHistoryDB initialize the DB
  30. func NewHistoryDB(db *sqlx.DB) *HistoryDB {
  31. return &HistoryDB{db: db}
  32. }
  33. // AddBlock insert a block into the DB
  34. func (hdb *HistoryDB) AddBlock(block *common.Block) error { return hdb.addBlock(hdb.db, block) }
  35. func (hdb *HistoryDB) addBlock(d meddler.DB, block *common.Block) error {
  36. return meddler.Insert(d, "block", block)
  37. }
  38. // AddBlocks inserts blocks into the DB
  39. func (hdb *HistoryDB) AddBlocks(blocks []common.Block) error {
  40. return hdb.addBlocks(hdb.db, blocks)
  41. }
  42. func (hdb *HistoryDB) addBlocks(d meddler.DB, blocks []common.Block) error {
  43. return db.BulkInsert(
  44. d,
  45. `INSERT INTO block (
  46. eth_block_num,
  47. timestamp,
  48. hash
  49. ) VALUES %s;`,
  50. blocks[:],
  51. )
  52. }
  53. // GetBlock retrieve a block from the DB, given a block number
  54. func (hdb *HistoryDB) GetBlock(blockNum int64) (*common.Block, error) {
  55. block := &common.Block{}
  56. err := meddler.QueryRow(
  57. hdb.db, block,
  58. "SELECT * FROM block WHERE eth_block_num = $1;", blockNum,
  59. )
  60. return block, err
  61. }
  62. // GetAllBlocks retrieve all blocks from the DB
  63. func (hdb *HistoryDB) GetAllBlocks() ([]common.Block, error) {
  64. var blocks []*common.Block
  65. err := meddler.QueryAll(
  66. hdb.db, &blocks,
  67. "SELECT * FROM block;",
  68. )
  69. return db.SlicePtrsToSlice(blocks).([]common.Block), err
  70. }
  71. // GetBlocks retrieve blocks from the DB, given a range of block numbers defined by from and to
  72. func (hdb *HistoryDB) GetBlocks(from, to int64) ([]common.Block, error) {
  73. var blocks []*common.Block
  74. err := meddler.QueryAll(
  75. hdb.db, &blocks,
  76. "SELECT * FROM block WHERE $1 <= eth_block_num AND eth_block_num < $2;",
  77. from, to,
  78. )
  79. return db.SlicePtrsToSlice(blocks).([]common.Block), err
  80. }
  81. // GetLastBlock retrieve the block with the highest block number from the DB
  82. func (hdb *HistoryDB) GetLastBlock() (*common.Block, error) {
  83. block := &common.Block{}
  84. err := meddler.QueryRow(
  85. hdb.db, block, "SELECT * FROM block ORDER BY eth_block_num DESC LIMIT 1;",
  86. )
  87. return block, err
  88. }
  89. // AddBatch insert a Batch into the DB
  90. func (hdb *HistoryDB) AddBatch(batch *common.Batch) error { return hdb.addBatch(hdb.db, batch) }
  91. func (hdb *HistoryDB) addBatch(d meddler.DB, batch *common.Batch) error {
  92. // Calculate total collected fees in USD
  93. // Get IDs of collected tokens for fees
  94. tokenIDs := []common.TokenID{}
  95. for id := range batch.CollectedFees {
  96. tokenIDs = append(tokenIDs, id)
  97. }
  98. // Get USD value of the tokens
  99. type tokenPrice struct {
  100. ID common.TokenID `meddler:"token_id"`
  101. USD *float64 `meddler:"usd"`
  102. Decimals int `meddler:"decimals"`
  103. }
  104. var tokenPrices []*tokenPrice
  105. if len(tokenIDs) > 0 {
  106. query, args, err := sqlx.In(
  107. "SELECT token_id, usd, decimals FROM token WHERE token_id IN (?)",
  108. tokenIDs,
  109. )
  110. if err != nil {
  111. return err
  112. }
  113. query = hdb.db.Rebind(query)
  114. if err := meddler.QueryAll(
  115. hdb.db, &tokenPrices, query, args...,
  116. ); err != nil {
  117. return err
  118. }
  119. }
  120. // Calculate total collected
  121. var total float64
  122. for _, tokenPrice := range tokenPrices {
  123. if tokenPrice.USD == nil {
  124. continue
  125. }
  126. f := new(big.Float).SetInt(batch.CollectedFees[tokenPrice.ID])
  127. amount, _ := f.Float64()
  128. total += *tokenPrice.USD * (amount / math.Pow(10, float64(tokenPrice.Decimals))) //nolint decimals have to be ^10
  129. }
  130. batch.TotalFeesUSD = &total
  131. // Insert to DB
  132. return meddler.Insert(d, "batch", batch)
  133. }
  134. // AddBatches insert Bids into the DB
  135. func (hdb *HistoryDB) AddBatches(batches []common.Batch) error {
  136. return hdb.addBatches(hdb.db, batches)
  137. }
  138. func (hdb *HistoryDB) addBatches(d meddler.DB, batches []common.Batch) error {
  139. for i := 0; i < len(batches); i++ {
  140. if err := hdb.addBatch(d, &batches[i]); err != nil {
  141. return err
  142. }
  143. }
  144. return nil
  145. }
  146. // GetBatchAPI return the batch with the given batchNum
  147. func (hdb *HistoryDB) GetBatchAPI(batchNum common.BatchNum) (*BatchAPI, error) {
  148. batch := &BatchAPI{}
  149. return batch, meddler.QueryRow(
  150. hdb.db, batch,
  151. `SELECT batch.*, block.timestamp, block.hash
  152. FROM batch INNER JOIN block ON batch.eth_block_num = block.eth_block_num
  153. WHERE batch_num = $1;`, batchNum,
  154. )
  155. }
  156. // GetBatchesAPI return the batches applying the given filters
  157. func (hdb *HistoryDB) GetBatchesAPI(
  158. minBatchNum, maxBatchNum, slotNum *uint,
  159. forgerAddr *ethCommon.Address,
  160. fromItem, limit *uint, order string,
  161. ) ([]BatchAPI, *db.Pagination, error) {
  162. var query string
  163. var args []interface{}
  164. queryStr := `SELECT batch.*, block.timestamp, block.hash,
  165. count(*) OVER() AS total_items, MIN(batch.item_id) OVER() AS first_item,
  166. MAX(batch.item_id) OVER() AS last_item
  167. FROM batch INNER JOIN block ON batch.eth_block_num = block.eth_block_num `
  168. // Apply filters
  169. nextIsAnd := false
  170. // minBatchNum filter
  171. if minBatchNum != nil {
  172. if nextIsAnd {
  173. queryStr += "AND "
  174. } else {
  175. queryStr += "WHERE "
  176. }
  177. queryStr += "batch.batch_num > ? "
  178. args = append(args, minBatchNum)
  179. nextIsAnd = true
  180. }
  181. // maxBatchNum filter
  182. if maxBatchNum != nil {
  183. if nextIsAnd {
  184. queryStr += "AND "
  185. } else {
  186. queryStr += "WHERE "
  187. }
  188. queryStr += "batch.batch_num < ? "
  189. args = append(args, maxBatchNum)
  190. nextIsAnd = true
  191. }
  192. // slotNum filter
  193. if slotNum != nil {
  194. if nextIsAnd {
  195. queryStr += "AND "
  196. } else {
  197. queryStr += "WHERE "
  198. }
  199. queryStr += "batch.slot_num = ? "
  200. args = append(args, slotNum)
  201. nextIsAnd = true
  202. }
  203. // forgerAddr filter
  204. if forgerAddr != nil {
  205. if nextIsAnd {
  206. queryStr += "AND "
  207. } else {
  208. queryStr += "WHERE "
  209. }
  210. queryStr += "batch.forger_addr = ? "
  211. args = append(args, forgerAddr)
  212. nextIsAnd = true
  213. }
  214. // pagination
  215. if fromItem != nil {
  216. if nextIsAnd {
  217. queryStr += "AND "
  218. } else {
  219. queryStr += "WHERE "
  220. }
  221. if order == OrderAsc {
  222. queryStr += "batch.item_id >= ? "
  223. } else {
  224. queryStr += "batch.item_id <= ? "
  225. }
  226. args = append(args, fromItem)
  227. }
  228. queryStr += "ORDER BY batch.item_id "
  229. if order == OrderAsc {
  230. queryStr += " ASC "
  231. } else {
  232. queryStr += " DESC "
  233. }
  234. queryStr += fmt.Sprintf("LIMIT %d;", *limit)
  235. query = hdb.db.Rebind(queryStr)
  236. log.Debug(query)
  237. batchPtrs := []*BatchAPI{}
  238. if err := meddler.QueryAll(hdb.db, &batchPtrs, query, args...); err != nil {
  239. return nil, nil, err
  240. }
  241. batches := db.SlicePtrsToSlice(batchPtrs).([]BatchAPI)
  242. if len(batches) == 0 {
  243. return nil, nil, sql.ErrNoRows
  244. }
  245. return batches, &db.Pagination{
  246. TotalItems: batches[0].TotalItems,
  247. FirstItem: batches[0].FirstItem,
  248. LastItem: batches[0].LastItem,
  249. }, nil
  250. }
  251. // GetAllBatches retrieve all batches from the DB
  252. func (hdb *HistoryDB) GetAllBatches() ([]common.Batch, error) {
  253. var batches []*common.Batch
  254. err := meddler.QueryAll(
  255. hdb.db, &batches,
  256. "SELECT * FROM batch;",
  257. )
  258. return db.SlicePtrsToSlice(batches).([]common.Batch), err
  259. }
  260. // GetBatches retrieve batches from the DB, given a range of batch numbers defined by from and to
  261. func (hdb *HistoryDB) GetBatches(from, to common.BatchNum) ([]common.Batch, error) {
  262. var batches []*common.Batch
  263. err := meddler.QueryAll(
  264. hdb.db, &batches,
  265. "SELECT * FROM batch WHERE $1 <= batch_num AND batch_num < $2;",
  266. from, to,
  267. )
  268. return db.SlicePtrsToSlice(batches).([]common.Batch), err
  269. }
  270. // GetLastBatchNum returns the BatchNum of the latest forged batch
  271. func (hdb *HistoryDB) GetLastBatchNum() (common.BatchNum, error) {
  272. row := hdb.db.QueryRow("SELECT batch_num FROM batch ORDER BY batch_num DESC LIMIT 1;")
  273. var batchNum common.BatchNum
  274. return batchNum, row.Scan(&batchNum)
  275. }
  276. // GetLastL1TxsNum returns the greatest ForgeL1TxsNum in the DB from forged
  277. // batches. If there's no batch in the DB (nil, nil) is returned.
  278. func (hdb *HistoryDB) GetLastL1TxsNum() (*int64, error) {
  279. row := hdb.db.QueryRow("SELECT MAX(forge_l1_txs_num) FROM batch;")
  280. lastL1TxsNum := new(int64)
  281. return lastL1TxsNum, row.Scan(&lastL1TxsNum)
  282. }
  283. // Reorg deletes all the information that was added into the DB after the
  284. // lastValidBlock. If lastValidBlock is negative, all block information is
  285. // deleted.
  286. func (hdb *HistoryDB) Reorg(lastValidBlock int64) error {
  287. var err error
  288. if lastValidBlock < 0 {
  289. _, err = hdb.db.Exec("DELETE FROM block;")
  290. } else {
  291. _, err = hdb.db.Exec("DELETE FROM block WHERE eth_block_num > $1;", lastValidBlock)
  292. }
  293. return err
  294. }
  295. // SyncPoD stores all the data that can be changed / added on a block in the PoD SC
  296. func (hdb *HistoryDB) SyncPoD(
  297. blockNum uint64,
  298. bids []common.Bid,
  299. coordinators []common.Coordinator,
  300. vars *common.AuctionVars,
  301. ) error {
  302. return nil
  303. }
  304. // AddBids insert Bids into the DB
  305. func (hdb *HistoryDB) AddBids(bids []common.Bid) error { return hdb.addBids(hdb.db, bids) }
  306. func (hdb *HistoryDB) addBids(d meddler.DB, bids []common.Bid) error {
  307. // TODO: check the coordinator info
  308. return db.BulkInsert(
  309. d,
  310. "INSERT INTO bid (slot_num, bid_value, eth_block_num, bidder_addr) VALUES %s;",
  311. bids[:],
  312. )
  313. }
  314. // GetBids return the bids
  315. func (hdb *HistoryDB) GetBids() ([]common.Bid, error) {
  316. var bids []*common.Bid
  317. err := meddler.QueryAll(
  318. hdb.db, &bids,
  319. "SELECT * FROM bid;",
  320. )
  321. return db.SlicePtrsToSlice(bids).([]common.Bid), err
  322. }
  323. // AddCoordinators insert Coordinators into the DB
  324. func (hdb *HistoryDB) AddCoordinators(coordinators []common.Coordinator) error {
  325. return hdb.addCoordinators(hdb.db, coordinators)
  326. }
  327. func (hdb *HistoryDB) addCoordinators(d meddler.DB, coordinators []common.Coordinator) error {
  328. return db.BulkInsert(
  329. d,
  330. "INSERT INTO coordinator (bidder_addr, forger_addr, eth_block_num, url) VALUES %s;",
  331. coordinators[:],
  332. )
  333. }
  334. // AddExitTree insert Exit tree into the DB
  335. func (hdb *HistoryDB) AddExitTree(exitTree []common.ExitInfo) error {
  336. return hdb.addExitTree(hdb.db, exitTree)
  337. }
  338. func (hdb *HistoryDB) addExitTree(d meddler.DB, exitTree []common.ExitInfo) error {
  339. return db.BulkInsert(
  340. d,
  341. "INSERT INTO exit_tree (batch_num, account_idx, merkle_proof, balance, "+
  342. "instant_withdrawn, delayed_withdraw_request, delayed_withdrawn) VALUES %s;",
  343. exitTree[:],
  344. )
  345. }
  346. // AddToken insert a token into the DB
  347. func (hdb *HistoryDB) AddToken(token *common.Token) error {
  348. return meddler.Insert(hdb.db, "token", token)
  349. }
  350. // AddTokens insert tokens into the DB
  351. func (hdb *HistoryDB) AddTokens(tokens []common.Token) error { return hdb.addTokens(hdb.db, tokens) }
  352. func (hdb *HistoryDB) addTokens(d meddler.DB, tokens []common.Token) error {
  353. return db.BulkInsert(
  354. d,
  355. `INSERT INTO token (
  356. token_id,
  357. eth_block_num,
  358. eth_addr,
  359. name,
  360. symbol,
  361. decimals
  362. ) VALUES %s;`,
  363. tokens[:],
  364. )
  365. }
  366. // UpdateTokenValue updates the USD value of a token
  367. func (hdb *HistoryDB) UpdateTokenValue(tokenSymbol string, value float64) error {
  368. _, err := hdb.db.Exec(
  369. "UPDATE token SET usd = $1 WHERE symbol = $2;",
  370. value, tokenSymbol,
  371. )
  372. return err
  373. }
  374. // GetToken returns a token from the DB given a TokenID
  375. func (hdb *HistoryDB) GetToken(tokenID common.TokenID) (*TokenWithUSD, error) {
  376. token := &TokenWithUSD{}
  377. err := meddler.QueryRow(
  378. hdb.db, token, `SELECT * FROM token WHERE token_id = $1;`, tokenID,
  379. )
  380. return token, err
  381. }
  382. // GetAllTokens returns all tokens from the DB
  383. func (hdb *HistoryDB) GetAllTokens() ([]TokenWithUSD, error) {
  384. var tokens []*TokenWithUSD
  385. err := meddler.QueryAll(
  386. hdb.db, &tokens,
  387. "SELECT * FROM token ORDER BY token_id;",
  388. )
  389. return db.SlicePtrsToSlice(tokens).([]TokenWithUSD), err
  390. }
  391. // GetTokens returns a list of tokens from the DB
  392. func (hdb *HistoryDB) GetTokens(ids []common.TokenID, symbols []string, name string, fromItem, limit *uint, order string) ([]TokenWithUSD, *db.Pagination, error) {
  393. var query string
  394. var args []interface{}
  395. 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 `
  396. // Apply filters
  397. nextIsAnd := false
  398. if len(ids) > 0 {
  399. queryStr += "WHERE token_id IN (?) "
  400. nextIsAnd = true
  401. args = append(args, ids)
  402. }
  403. if len(symbols) > 0 {
  404. if nextIsAnd {
  405. queryStr += "AND "
  406. } else {
  407. queryStr += "WHERE "
  408. }
  409. queryStr += "symbol IN (?) "
  410. args = append(args, symbols)
  411. nextIsAnd = true
  412. }
  413. if name != "" {
  414. if nextIsAnd {
  415. queryStr += "AND "
  416. } else {
  417. queryStr += "WHERE "
  418. }
  419. queryStr += "name ~ ? "
  420. args = append(args, name)
  421. nextIsAnd = true
  422. }
  423. if fromItem != nil {
  424. if nextIsAnd {
  425. queryStr += "AND "
  426. } else {
  427. queryStr += "WHERE "
  428. }
  429. if order == OrderAsc {
  430. queryStr += "item_id >= ? "
  431. } else {
  432. queryStr += "item_id <= ? "
  433. }
  434. args = append(args, fromItem)
  435. }
  436. // pagination
  437. queryStr += "ORDER BY item_id "
  438. if order == OrderAsc {
  439. queryStr += "ASC "
  440. } else {
  441. queryStr += "DESC "
  442. }
  443. queryStr += fmt.Sprintf("LIMIT %d;", *limit)
  444. query, argsQ, err := sqlx.In(queryStr, args...)
  445. if err != nil {
  446. return nil, nil, err
  447. }
  448. query = hdb.db.Rebind(query)
  449. tokens := []*TokenWithUSD{}
  450. if err := meddler.QueryAll(hdb.db, &tokens, query, argsQ...); err != nil {
  451. return nil, nil, err
  452. }
  453. if len(tokens) == 0 {
  454. return nil, nil, sql.ErrNoRows
  455. }
  456. return db.SlicePtrsToSlice(tokens).([]TokenWithUSD), &db.Pagination{
  457. TotalItems: tokens[0].TotalItems,
  458. FirstItem: tokens[0].FirstItem,
  459. LastItem: tokens[0].LastItem,
  460. }, nil
  461. }
  462. // GetTokenSymbols returns all the token symbols from the DB
  463. func (hdb *HistoryDB) GetTokenSymbols() ([]string, error) {
  464. var tokenSymbols []string
  465. rows, err := hdb.db.Query("SELECT symbol FROM token;")
  466. if err != nil {
  467. return nil, err
  468. }
  469. sym := new(string)
  470. for rows.Next() {
  471. err = rows.Scan(sym)
  472. if err != nil {
  473. return nil, err
  474. }
  475. tokenSymbols = append(tokenSymbols, *sym)
  476. }
  477. return tokenSymbols, nil
  478. }
  479. // AddAccounts insert accounts into the DB
  480. func (hdb *HistoryDB) AddAccounts(accounts []common.Account) error {
  481. return hdb.addAccounts(hdb.db, accounts)
  482. }
  483. func (hdb *HistoryDB) addAccounts(d meddler.DB, accounts []common.Account) error {
  484. return db.BulkInsert(
  485. d,
  486. `INSERT INTO account (
  487. idx,
  488. token_id,
  489. batch_num,
  490. bjj,
  491. eth_addr
  492. ) VALUES %s;`,
  493. accounts[:],
  494. )
  495. }
  496. // GetAccounts returns a list of accounts from the DB
  497. func (hdb *HistoryDB) GetAccounts() ([]common.Account, error) {
  498. var accs []*common.Account
  499. err := meddler.QueryAll(
  500. hdb.db, &accs,
  501. "SELECT * FROM account ORDER BY idx;",
  502. )
  503. return db.SlicePtrsToSlice(accs).([]common.Account), err
  504. }
  505. // AddL1Txs inserts L1 txs to the DB. USD and LoadAmountUSD will be set automatically before storing the tx.
  506. // If the tx is originated by a coordinator, BatchNum must be provided. If it's originated by a user,
  507. // BatchNum should be null, and the value will be setted by a trigger when a batch forges the tx.
  508. func (hdb *HistoryDB) AddL1Txs(l1txs []common.L1Tx) error { return hdb.addL1Txs(hdb.db, l1txs) }
  509. // addL1Txs inserts L1 txs to the DB. USD and LoadAmountUSD will be set automatically before storing the tx.
  510. // If the tx is originated by a coordinator, BatchNum must be provided. If it's originated by a user,
  511. // BatchNum should be null, and the value will be setted by a trigger when a batch forges the tx.
  512. func (hdb *HistoryDB) addL1Txs(d meddler.DB, l1txs []common.L1Tx) error {
  513. txs := []txWrite{}
  514. for i := 0; i < len(l1txs); i++ {
  515. af := new(big.Float).SetInt(l1txs[i].Amount)
  516. amountFloat, _ := af.Float64()
  517. laf := new(big.Float).SetInt(l1txs[i].LoadAmount)
  518. loadAmountFloat, _ := laf.Float64()
  519. txs = append(txs, txWrite{
  520. // Generic
  521. IsL1: true,
  522. TxID: l1txs[i].TxID,
  523. Type: l1txs[i].Type,
  524. Position: l1txs[i].Position,
  525. FromIdx: &l1txs[i].FromIdx,
  526. ToIdx: l1txs[i].ToIdx,
  527. Amount: l1txs[i].Amount,
  528. AmountFloat: amountFloat,
  529. TokenID: l1txs[i].TokenID,
  530. BatchNum: l1txs[i].BatchNum,
  531. EthBlockNum: l1txs[i].EthBlockNum,
  532. // L1
  533. ToForgeL1TxsNum: l1txs[i].ToForgeL1TxsNum,
  534. UserOrigin: &l1txs[i].UserOrigin,
  535. FromEthAddr: &l1txs[i].FromEthAddr,
  536. FromBJJ: l1txs[i].FromBJJ,
  537. LoadAmount: l1txs[i].LoadAmount,
  538. LoadAmountFloat: &loadAmountFloat,
  539. })
  540. }
  541. return hdb.addTxs(d, txs)
  542. }
  543. // AddL2Txs inserts L2 txs to the DB. TokenID, USD and FeeUSD will be set automatically before storing the tx.
  544. func (hdb *HistoryDB) AddL2Txs(l2txs []common.L2Tx) error { return hdb.addL2Txs(hdb.db, l2txs) }
  545. // addL2Txs inserts L2 txs to the DB. TokenID, USD and FeeUSD will be set automatically before storing the tx.
  546. func (hdb *HistoryDB) addL2Txs(d meddler.DB, l2txs []common.L2Tx) error {
  547. txs := []txWrite{}
  548. for i := 0; i < len(l2txs); i++ {
  549. f := new(big.Float).SetInt(l2txs[i].Amount)
  550. amountFloat, _ := f.Float64()
  551. txs = append(txs, txWrite{
  552. // Generic
  553. IsL1: false,
  554. TxID: l2txs[i].TxID,
  555. Type: l2txs[i].Type,
  556. Position: l2txs[i].Position,
  557. FromIdx: &l2txs[i].FromIdx,
  558. ToIdx: l2txs[i].ToIdx,
  559. Amount: l2txs[i].Amount,
  560. AmountFloat: amountFloat,
  561. BatchNum: &l2txs[i].BatchNum,
  562. EthBlockNum: l2txs[i].EthBlockNum,
  563. // L2
  564. Fee: &l2txs[i].Fee,
  565. Nonce: &l2txs[i].Nonce,
  566. })
  567. }
  568. return hdb.addTxs(d, txs)
  569. }
  570. func (hdb *HistoryDB) addTxs(d meddler.DB, txs []txWrite) error {
  571. return db.BulkInsert(
  572. d,
  573. `INSERT INTO tx (
  574. is_l1,
  575. id,
  576. type,
  577. position,
  578. from_idx,
  579. to_idx,
  580. amount,
  581. amount_f,
  582. token_id,
  583. batch_num,
  584. eth_block_num,
  585. to_forge_l1_txs_num,
  586. user_origin,
  587. from_eth_addr,
  588. from_bjj,
  589. load_amount,
  590. load_amount_f,
  591. fee,
  592. nonce
  593. ) VALUES %s;`,
  594. txs[:],
  595. )
  596. }
  597. // // GetTxs returns a list of txs from the DB
  598. // func (hdb *HistoryDB) GetTxs() ([]common.Tx, error) {
  599. // var txs []*common.Tx
  600. // err := meddler.QueryAll(
  601. // hdb.db, &txs,
  602. // `SELECT * FROM tx
  603. // ORDER BY (batch_num, position) ASC`,
  604. // )
  605. // return db.SlicePtrsToSlice(txs).([]common.Tx), err
  606. // }
  607. // GetHistoryTx returns a tx from the DB given a TxID
  608. func (hdb *HistoryDB) GetHistoryTx(txID common.TxID) (*HistoryTx, error) {
  609. tx := &HistoryTx{}
  610. err := meddler.QueryRow(
  611. hdb.db, tx, `SELECT tx.item_id, tx.is_l1, tx.id, tx.type, tx.position,
  612. tx.from_idx, tx.to_idx, tx.amount, tx.token_id, tx.amount_usd,
  613. tx.batch_num, tx.eth_block_num, tx.to_forge_l1_txs_num, tx.user_origin,
  614. tx.from_eth_addr, tx.from_bjj, tx.load_amount,
  615. tx.load_amount_usd, tx.fee, tx.fee_usd, tx.nonce,
  616. token.token_id, token.eth_block_num AS token_block,
  617. token.eth_addr, token.name, token.symbol, token.decimals, token.usd,
  618. token.usd_update, block.timestamp
  619. FROM tx INNER JOIN token ON tx.token_id = token.token_id
  620. INNER JOIN block ON tx.eth_block_num = block.eth_block_num
  621. WHERE tx.id = $1;`, txID,
  622. )
  623. return tx, err
  624. }
  625. // GetHistoryTxs returns a list of txs from the DB using the HistoryTx struct
  626. // and pagination info
  627. func (hdb *HistoryDB) GetHistoryTxs(
  628. ethAddr *ethCommon.Address, bjj *babyjub.PublicKey,
  629. tokenID *common.TokenID, idx *common.Idx, batchNum *uint, txType *common.TxType,
  630. fromItem, limit *uint, order string,
  631. ) ([]HistoryTx, *db.Pagination, error) {
  632. if ethAddr != nil && bjj != nil {
  633. return nil, nil, errors.New("ethAddr and bjj are incompatible")
  634. }
  635. var query string
  636. var args []interface{}
  637. queryStr := `SELECT tx.item_id, tx.is_l1, tx.id, tx.type, tx.position,
  638. tx.from_idx, tx.to_idx, tx.amount, tx.token_id, tx.amount_usd,
  639. tx.batch_num, tx.eth_block_num, tx.to_forge_l1_txs_num, tx.user_origin,
  640. tx.from_eth_addr, tx.from_bjj, tx.load_amount,
  641. tx.load_amount_usd, tx.fee, tx.fee_usd, tx.nonce,
  642. token.token_id, token.eth_block_num AS token_block,
  643. token.eth_addr, token.name, token.symbol, token.decimals, token.usd,
  644. token.usd_update, block.timestamp, count(*) OVER() AS total_items,
  645. MIN(tx.item_id) OVER() AS first_item, MAX(tx.item_id) OVER() AS last_item
  646. FROM tx INNER JOIN token ON tx.token_id = token.token_id
  647. INNER JOIN block ON tx.eth_block_num = block.eth_block_num `
  648. // Apply filters
  649. nextIsAnd := false
  650. // ethAddr filter
  651. if ethAddr != nil {
  652. queryStr = `WITH acc AS
  653. (select idx from account where eth_addr = ?) ` + queryStr
  654. queryStr += ", acc WHERE (tx.from_idx IN(acc.idx) OR tx.to_idx IN(acc.idx)) "
  655. nextIsAnd = true
  656. args = append(args, ethAddr)
  657. } else if bjj != nil { // bjj filter
  658. queryStr = `WITH acc AS
  659. (select idx from account where bjj = ?) ` + queryStr
  660. queryStr += ", acc WHERE (tx.from_idx IN(acc.idx) OR tx.to_idx IN(acc.idx)) "
  661. nextIsAnd = true
  662. args = append(args, bjj)
  663. }
  664. // tokenID filter
  665. if tokenID != nil {
  666. if nextIsAnd {
  667. queryStr += "AND "
  668. } else {
  669. queryStr += "WHERE "
  670. }
  671. queryStr += "tx.token_id = ? "
  672. args = append(args, tokenID)
  673. nextIsAnd = true
  674. }
  675. // idx filter
  676. if idx != nil {
  677. if nextIsAnd {
  678. queryStr += "AND "
  679. } else {
  680. queryStr += "WHERE "
  681. }
  682. queryStr += "(tx.from_idx = ? OR tx.to_idx = ?) "
  683. args = append(args, idx, idx)
  684. nextIsAnd = true
  685. }
  686. // batchNum filter
  687. if batchNum != nil {
  688. if nextIsAnd {
  689. queryStr += "AND "
  690. } else {
  691. queryStr += "WHERE "
  692. }
  693. queryStr += "tx.batch_num = ? "
  694. args = append(args, batchNum)
  695. nextIsAnd = true
  696. }
  697. // txType filter
  698. if txType != nil {
  699. if nextIsAnd {
  700. queryStr += "AND "
  701. } else {
  702. queryStr += "WHERE "
  703. }
  704. queryStr += "tx.type = ? "
  705. args = append(args, txType)
  706. nextIsAnd = true
  707. }
  708. if fromItem != nil {
  709. if nextIsAnd {
  710. queryStr += "AND "
  711. } else {
  712. queryStr += "WHERE "
  713. }
  714. if order == OrderAsc {
  715. queryStr += "tx.item_id >= ? "
  716. } else {
  717. queryStr += "tx.item_id <= ? "
  718. }
  719. args = append(args, fromItem)
  720. nextIsAnd = true
  721. }
  722. if nextIsAnd {
  723. queryStr += "AND "
  724. } else {
  725. queryStr += "WHERE "
  726. }
  727. queryStr += "tx.batch_num IS NOT NULL "
  728. // pagination
  729. queryStr += "ORDER BY tx.item_id "
  730. if order == OrderAsc {
  731. queryStr += " ASC "
  732. } else {
  733. queryStr += " DESC "
  734. }
  735. queryStr += fmt.Sprintf("LIMIT %d;", *limit)
  736. query = hdb.db.Rebind(queryStr)
  737. log.Debug(query)
  738. txsPtrs := []*HistoryTx{}
  739. if err := meddler.QueryAll(hdb.db, &txsPtrs, query, args...); err != nil {
  740. return nil, nil, err
  741. }
  742. txs := db.SlicePtrsToSlice(txsPtrs).([]HistoryTx)
  743. if len(txs) == 0 {
  744. return nil, nil, sql.ErrNoRows
  745. }
  746. return txs, &db.Pagination{
  747. TotalItems: txs[0].TotalItems,
  748. FirstItem: txs[0].FirstItem,
  749. LastItem: txs[0].LastItem,
  750. }, nil
  751. }
  752. // GetExit returns a exit from the DB
  753. func (hdb *HistoryDB) GetExit(batchNum *uint, idx *common.Idx) (*HistoryExit, error) {
  754. exit := &HistoryExit{}
  755. err := meddler.QueryRow(
  756. hdb.db, exit, `SELECT exit_tree.*, token.token_id, token.eth_block_num AS token_block,
  757. token.eth_addr, token.name, token.symbol, token.decimals, token.usd, token.usd_update
  758. FROM exit_tree INNER JOIN account ON exit_tree.account_idx = account.idx
  759. INNER JOIN token ON account.token_id = token.token_id
  760. WHERE exit_tree.batch_num = $1 AND exit_tree.account_idx = $2;`, batchNum, idx,
  761. )
  762. return exit, err
  763. }
  764. // GetExits returns a list of exits from the DB and pagination info
  765. func (hdb *HistoryDB) GetExits(
  766. ethAddr *ethCommon.Address, bjj *babyjub.PublicKey,
  767. tokenID *common.TokenID, idx *common.Idx, batchNum *uint,
  768. fromItem, limit *uint, order string,
  769. ) ([]HistoryExit, *db.Pagination, error) {
  770. if ethAddr != nil && bjj != nil {
  771. return nil, nil, errors.New("ethAddr and bjj are incompatible")
  772. }
  773. var query string
  774. var args []interface{}
  775. queryStr := `SELECT exit_tree.*, token.token_id, token.eth_block_num AS token_block,
  776. token.eth_addr, token.name, token.symbol, token.decimals, token.usd,
  777. 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
  778. FROM exit_tree INNER JOIN account ON exit_tree.account_idx = account.idx
  779. INNER JOIN token ON account.token_id = token.token_id `
  780. // Apply filters
  781. nextIsAnd := false
  782. // ethAddr filter
  783. if ethAddr != nil {
  784. queryStr += "WHERE account.eth_addr = ? "
  785. nextIsAnd = true
  786. args = append(args, ethAddr)
  787. } else if bjj != nil { // bjj filter
  788. queryStr += "WHERE account.bjj = ? "
  789. nextIsAnd = true
  790. args = append(args, bjj)
  791. }
  792. // tokenID filter
  793. if tokenID != nil {
  794. if nextIsAnd {
  795. queryStr += "AND "
  796. } else {
  797. queryStr += "WHERE "
  798. }
  799. queryStr += "account.token_id = ? "
  800. args = append(args, tokenID)
  801. nextIsAnd = true
  802. }
  803. // idx filter
  804. if idx != nil {
  805. if nextIsAnd {
  806. queryStr += "AND "
  807. } else {
  808. queryStr += "WHERE "
  809. }
  810. queryStr += "exit_tree.account_idx = ? "
  811. args = append(args, idx)
  812. nextIsAnd = true
  813. }
  814. // batchNum filter
  815. if batchNum != nil {
  816. if nextIsAnd {
  817. queryStr += "AND "
  818. } else {
  819. queryStr += "WHERE "
  820. }
  821. queryStr += "exit_tree.batch_num = ? "
  822. args = append(args, batchNum)
  823. nextIsAnd = true
  824. }
  825. if fromItem != nil {
  826. if nextIsAnd {
  827. queryStr += "AND "
  828. } else {
  829. queryStr += "WHERE "
  830. }
  831. if order == OrderAsc {
  832. queryStr += "exit_tree.item_id >= ? "
  833. } else {
  834. queryStr += "exit_tree.item_id <= ? "
  835. }
  836. args = append(args, fromItem)
  837. // nextIsAnd = true
  838. }
  839. // pagination
  840. queryStr += "ORDER BY exit_tree.item_id "
  841. if order == OrderAsc {
  842. queryStr += " ASC "
  843. } else {
  844. queryStr += " DESC "
  845. }
  846. queryStr += fmt.Sprintf("LIMIT %d;", *limit)
  847. query = hdb.db.Rebind(queryStr)
  848. // log.Debug(query)
  849. exits := []*HistoryExit{}
  850. if err := meddler.QueryAll(hdb.db, &exits, query, args...); err != nil {
  851. return nil, nil, err
  852. }
  853. if len(exits) == 0 {
  854. return nil, nil, sql.ErrNoRows
  855. }
  856. return db.SlicePtrsToSlice(exits).([]HistoryExit), &db.Pagination{
  857. TotalItems: exits[0].TotalItems,
  858. FirstItem: exits[0].FirstItem,
  859. LastItem: exits[0].LastItem,
  860. }, nil
  861. }
  862. // // GetTx returns a tx from the DB
  863. // func (hdb *HistoryDB) GetTx(txID common.TxID) (*common.Tx, error) {
  864. // tx := new(common.Tx)
  865. // return tx, meddler.QueryRow(
  866. // hdb.db, tx,
  867. // "SELECT * FROM tx WHERE id = $1;",
  868. // txID,
  869. // )
  870. // }
  871. // GetAllL1UserTxs returns all L1UserTxs from the DB
  872. func (hdb *HistoryDB) GetAllL1UserTxs() ([]common.L1Tx, error) {
  873. var txs []*common.L1Tx
  874. err := meddler.QueryAll(
  875. hdb.db, &txs,
  876. `SELECT tx.id, tx.to_forge_l1_txs_num, tx.position, tx.user_origin,
  877. tx.from_idx, tx.from_eth_addr, tx.from_bjj, tx.to_idx, tx.token_id, tx.amount,
  878. tx.load_amount, tx.eth_block_num, tx.type, tx.batch_num
  879. FROM tx WHERE is_l1 = TRUE AND user_origin = TRUE;`,
  880. )
  881. return db.SlicePtrsToSlice(txs).([]common.L1Tx), err
  882. }
  883. // GetL1UserTxs gets L1 User Txs to be forged in the L1Batch with toForgeL1TxsNum.
  884. func (hdb *HistoryDB) GetL1UserTxs(toForgeL1TxsNum int64) ([]common.L1Tx, error) {
  885. var txs []*common.L1Tx
  886. err := meddler.QueryAll(
  887. hdb.db, &txs,
  888. `SELECT tx.id, tx.to_forge_l1_txs_num, tx.position, tx.user_origin,
  889. tx.from_idx, tx.from_eth_addr, tx.from_bjj, tx.to_idx, tx.token_id, tx.amount,
  890. tx.load_amount, tx.eth_block_num, tx.type, tx.batch_num
  891. FROM tx WHERE to_forge_l1_txs_num = $1 AND is_l1 = TRUE AND user_origin = TRUE;`,
  892. toForgeL1TxsNum,
  893. )
  894. return db.SlicePtrsToSlice(txs).([]common.L1Tx), err
  895. }
  896. // TODO: Think about chaning all the queries that return a last value, to queries that return the next valid value.
  897. // GetLastTxsPosition for a given to_forge_l1_txs_num
  898. func (hdb *HistoryDB) GetLastTxsPosition(toForgeL1TxsNum int64) (int, error) {
  899. row := hdb.db.QueryRow("SELECT MAX(position) FROM tx WHERE to_forge_l1_txs_num = $1;", toForgeL1TxsNum)
  900. var lastL1TxsPosition int
  901. return lastL1TxsPosition, row.Scan(&lastL1TxsPosition)
  902. }
  903. // AddBlockSCData stores all the information of a block retrieved by the
  904. // Synchronizer. Blocks should be inserted in order, leaving no gaps because
  905. // the pagination system of the API/DB depends on this. Within blocks, all
  906. // items should also be in the correct order (Accounts, Tokens, Txs, etc.)
  907. func (hdb *HistoryDB) AddBlockSCData(blockData *common.BlockData) (err error) {
  908. txn, err := hdb.db.Begin()
  909. if err != nil {
  910. return err
  911. }
  912. defer func() {
  913. if err != nil {
  914. errRollback := txn.Rollback()
  915. if errRollback != nil {
  916. log.Errorw("Rollback", "err", errRollback)
  917. }
  918. }
  919. }()
  920. // Add block
  921. err = hdb.addBlock(txn, &blockData.Block)
  922. if err != nil {
  923. return err
  924. }
  925. // Add Coordinators
  926. if len(blockData.Coordinators) > 0 {
  927. err = hdb.addCoordinators(txn, blockData.Coordinators)
  928. if err != nil {
  929. return err
  930. }
  931. }
  932. // Add Bids
  933. if len(blockData.Bids) > 0 {
  934. err = hdb.addBids(txn, blockData.Bids)
  935. if err != nil {
  936. return err
  937. }
  938. }
  939. // Add Tokens
  940. if len(blockData.AddedTokens) > 0 {
  941. err = hdb.addTokens(txn, blockData.AddedTokens)
  942. if err != nil {
  943. return err
  944. }
  945. }
  946. // Add l1 Txs
  947. if len(blockData.L1UserTxs) > 0 {
  948. err = hdb.addL1Txs(txn, blockData.L1UserTxs)
  949. if err != nil {
  950. return err
  951. }
  952. }
  953. // Add Batches
  954. for i := range blockData.Batches {
  955. batch := &blockData.Batches[i]
  956. // Add Batch: this will trigger an update on the DB
  957. // that will set the batch num of forged L1 txs in this batch
  958. err = hdb.addBatch(txn, &batch.Batch)
  959. if err != nil {
  960. return err
  961. }
  962. // Add unforged l1 Txs
  963. if batch.L1Batch {
  964. if len(batch.L1CoordinatorTxs) > 0 {
  965. err = hdb.addL1Txs(txn, batch.L1CoordinatorTxs)
  966. if err != nil {
  967. return err
  968. }
  969. }
  970. }
  971. // Add l2 Txs
  972. if len(batch.L2Txs) > 0 {
  973. err = hdb.addL2Txs(txn, batch.L2Txs)
  974. if err != nil {
  975. return err
  976. }
  977. }
  978. // Add accounts
  979. if len(batch.CreatedAccounts) > 0 {
  980. err = hdb.addAccounts(txn, batch.CreatedAccounts)
  981. if err != nil {
  982. return err
  983. }
  984. }
  985. // Add exit tree
  986. if len(batch.ExitTree) > 0 {
  987. err = hdb.addExitTree(txn, batch.ExitTree)
  988. if err != nil {
  989. return err
  990. }
  991. }
  992. // TODO: INSERT CONTRACTS VARS
  993. }
  994. return txn.Commit()
  995. }
  996. // GetCoordinatorAPI returns a coordinator by its bidderAddr
  997. func (hdb *HistoryDB) GetCoordinatorAPI(bidderAddr ethCommon.Address) (*CoordinatorAPI, error) {
  998. coordinator := &CoordinatorAPI{}
  999. err := meddler.QueryRow(
  1000. hdb.db, coordinator, `SELECT * FROM coordinator WHERE bidder_addr = $1;`, bidderAddr,
  1001. )
  1002. return coordinator, err
  1003. }
  1004. // GetCoordinatorsAPI returns a list of coordinators from the DB and pagination info
  1005. func (hdb *HistoryDB) GetCoordinatorsAPI(fromItem, limit *uint, order string) ([]CoordinatorAPI, *db.Pagination, error) {
  1006. var query string
  1007. var args []interface{}
  1008. queryStr := `SELECT coordinator.*,
  1009. COUNT(*) OVER() AS total_items, MIN(coordinator.item_id) OVER() AS first_item, MAX(coordinator.item_id) OVER() AS last_item
  1010. FROM coordinator `
  1011. // Apply filters
  1012. if fromItem != nil {
  1013. queryStr += "WHERE "
  1014. if order == OrderAsc {
  1015. queryStr += "coordinator.item_id >= ? "
  1016. } else {
  1017. queryStr += "coordinator.item_id <= ? "
  1018. }
  1019. args = append(args, fromItem)
  1020. }
  1021. // pagination
  1022. queryStr += "ORDER BY coordinator.item_id "
  1023. if order == OrderAsc {
  1024. queryStr += " ASC "
  1025. } else {
  1026. queryStr += " DESC "
  1027. }
  1028. queryStr += fmt.Sprintf("LIMIT %d;", *limit)
  1029. query = hdb.db.Rebind(queryStr)
  1030. coordinators := []*CoordinatorAPI{}
  1031. if err := meddler.QueryAll(hdb.db, &coordinators, query, args...); err != nil {
  1032. return nil, nil, err
  1033. }
  1034. if len(coordinators) == 0 {
  1035. return nil, nil, sql.ErrNoRows
  1036. }
  1037. return db.SlicePtrsToSlice(coordinators).([]CoordinatorAPI), &db.Pagination{
  1038. TotalItems: coordinators[0].TotalItems,
  1039. FirstItem: coordinators[0].FirstItem,
  1040. LastItem: coordinators[0].LastItem,
  1041. }, nil
  1042. }