2019-04-18 12:39:37 +00:00
|
|
|
// VulcanizeDB
|
|
|
|
// Copyright © 2019 Vulcanize
|
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2020-01-17 23:16:01 +00:00
|
|
|
package eth
|
2019-05-21 19:27:24 +00:00
|
|
|
|
|
|
|
import (
|
2020-01-17 23:16:01 +00:00
|
|
|
"fmt"
|
2019-06-18 17:28:57 +00:00
|
|
|
"math/big"
|
|
|
|
|
2020-01-21 19:12:35 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2019-05-21 19:27:24 +00:00
|
|
|
"github.com/jmoiron/sqlx"
|
|
|
|
"github.com/lib/pq"
|
2019-06-07 13:42:10 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
2019-05-21 19:27:24 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
|
2020-01-17 23:16:01 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/super_node/config"
|
2020-01-21 19:12:35 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/super_node/shared"
|
2019-05-21 19:27:24 +00:00
|
|
|
)
|
|
|
|
|
2020-01-17 23:16:01 +00:00
|
|
|
// CIDRetriever satisfies the CIDRetriever interface for ethereum
|
|
|
|
type CIDRetriever struct {
|
2019-05-21 19:27:24 +00:00
|
|
|
db *postgres.DB
|
|
|
|
}
|
|
|
|
|
2020-01-17 23:16:01 +00:00
|
|
|
// NewCIDRetriever returns a pointer to a new CIDRetriever which supports the CIDRetriever interface
|
|
|
|
func NewCIDRetriever(db *postgres.DB) *CIDRetriever {
|
|
|
|
return &CIDRetriever{
|
2019-05-21 19:27:24 +00:00
|
|
|
db: db,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-08 19:51:38 +00:00
|
|
|
// RetrieveFirstBlockNumber is used to retrieve the first block number in the db
|
2020-01-17 23:16:01 +00:00
|
|
|
func (ecr *CIDRetriever) RetrieveFirstBlockNumber() (int64, error) {
|
2019-07-02 17:38:12 +00:00
|
|
|
var blockNumber int64
|
|
|
|
err := ecr.db.Get(&blockNumber, "SELECT block_number FROM header_cids ORDER BY block_number ASC LIMIT 1")
|
|
|
|
return blockNumber, err
|
|
|
|
}
|
|
|
|
|
2019-10-08 19:51:38 +00:00
|
|
|
// RetrieveLastBlockNumber is used to retrieve the latest block number in the db
|
2020-01-17 23:16:01 +00:00
|
|
|
func (ecr *CIDRetriever) RetrieveLastBlockNumber() (int64, error) {
|
2019-05-21 19:27:24 +00:00
|
|
|
var blockNumber int64
|
|
|
|
err := ecr.db.Get(&blockNumber, "SELECT block_number FROM header_cids ORDER BY block_number DESC LIMIT 1 ")
|
|
|
|
return blockNumber, err
|
|
|
|
}
|
2019-06-06 03:50:12 +00:00
|
|
|
|
2020-01-17 23:16:01 +00:00
|
|
|
// Retrieve is used to retrieve all of the CIDs which conform to the passed StreamFilters
|
|
|
|
func (ecr *CIDRetriever) Retrieve(filter interface{}, blockNumber int64) (interface{}, bool, error) {
|
|
|
|
streamFilter, ok := filter.(*config.EthSubscription)
|
|
|
|
if !ok {
|
|
|
|
return nil, true, fmt.Errorf("eth retriever expected filter type %T got %T", &config.EthSubscription{}, filter)
|
|
|
|
}
|
2019-06-07 13:42:10 +00:00
|
|
|
log.Debug("retrieving cids")
|
2020-01-16 20:48:38 +00:00
|
|
|
tx, err := ecr.db.Beginx()
|
|
|
|
if err != nil {
|
2020-01-17 23:16:01 +00:00
|
|
|
return nil, true, err
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
2020-01-16 20:48:38 +00:00
|
|
|
|
2020-01-17 23:16:01 +00:00
|
|
|
cw := new(CIDWrapper)
|
2019-07-02 17:38:12 +00:00
|
|
|
cw.BlockNumber = big.NewInt(blockNumber)
|
|
|
|
// Retrieve cached header CIDs
|
2020-01-17 23:16:01 +00:00
|
|
|
if !streamFilter.HeaderFilter.Off {
|
2020-01-16 20:48:38 +00:00
|
|
|
cw.Headers, err = ecr.RetrieveHeaderCIDs(tx, blockNumber)
|
|
|
|
if err != nil {
|
|
|
|
if err := tx.Rollback(); err != nil {
|
|
|
|
log.Error(err)
|
2019-08-28 18:41:49 +00:00
|
|
|
}
|
2019-07-02 17:38:12 +00:00
|
|
|
log.Error("header cid retrieval error")
|
2020-01-17 23:16:01 +00:00
|
|
|
return nil, true, err
|
2019-07-02 17:38:12 +00:00
|
|
|
}
|
2020-01-17 23:16:01 +00:00
|
|
|
if streamFilter.HeaderFilter.Uncles {
|
2020-01-16 20:48:38 +00:00
|
|
|
cw.Uncles, err = ecr.RetrieveUncleCIDs(tx, blockNumber)
|
|
|
|
if err != nil {
|
|
|
|
if err := tx.Rollback(); err != nil {
|
|
|
|
log.Error(err)
|
2019-08-28 18:41:49 +00:00
|
|
|
}
|
|
|
|
log.Error("uncle cid retrieval error")
|
2020-01-17 23:16:01 +00:00
|
|
|
return nil, true, err
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
|
|
|
}
|
2019-07-02 17:38:12 +00:00
|
|
|
}
|
|
|
|
// Retrieve cached trx CIDs
|
2020-01-17 23:16:01 +00:00
|
|
|
if !streamFilter.TxFilter.Off {
|
|
|
|
cw.Transactions, err = ecr.RetrieveTrxCIDs(tx, streamFilter.TxFilter, blockNumber)
|
2020-01-16 20:48:38 +00:00
|
|
|
if err != nil {
|
2020-01-17 23:16:01 +00:00
|
|
|
if err := tx.Rollback(); err != nil {
|
2020-01-16 20:48:38 +00:00
|
|
|
log.Error(err)
|
2019-08-28 18:41:49 +00:00
|
|
|
}
|
2019-07-02 17:38:12 +00:00
|
|
|
log.Error("transaction cid retrieval error")
|
2020-01-17 23:16:01 +00:00
|
|
|
return nil, true, err
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
2019-07-02 17:38:12 +00:00
|
|
|
}
|
2020-01-17 23:16:01 +00:00
|
|
|
trxIds := make([]int64, 0, len(cw.Transactions))
|
|
|
|
for _, tx := range cw.Transactions {
|
|
|
|
trxIds = append(trxIds, tx.ID)
|
|
|
|
}
|
2019-07-02 17:38:12 +00:00
|
|
|
// Retrieve cached receipt CIDs
|
2020-01-17 23:16:01 +00:00
|
|
|
if !streamFilter.ReceiptFilter.Off {
|
2020-01-21 19:12:35 +00:00
|
|
|
cw.Receipts, err = ecr.RetrieveRctCIDs(tx, streamFilter.ReceiptFilter, blockNumber, nil, trxIds)
|
2020-01-16 20:48:38 +00:00
|
|
|
if err != nil {
|
|
|
|
if err := tx.Rollback(); err != nil {
|
|
|
|
log.Error(err)
|
2019-08-28 18:41:49 +00:00
|
|
|
}
|
2019-07-02 17:38:12 +00:00
|
|
|
log.Error("receipt cid retrieval error")
|
2020-01-17 23:16:01 +00:00
|
|
|
return nil, true, err
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
2019-07-02 17:38:12 +00:00
|
|
|
}
|
|
|
|
// Retrieve cached state CIDs
|
2020-01-17 23:16:01 +00:00
|
|
|
if !streamFilter.StateFilter.Off {
|
|
|
|
cw.StateNodes, err = ecr.RetrieveStateCIDs(tx, streamFilter.StateFilter, blockNumber)
|
2020-01-16 20:48:38 +00:00
|
|
|
if err != nil {
|
|
|
|
if err := tx.Rollback(); err != nil {
|
|
|
|
log.Error(err)
|
2019-08-28 18:41:49 +00:00
|
|
|
}
|
2019-07-02 17:38:12 +00:00
|
|
|
log.Error("state cid retrieval error")
|
2020-01-17 23:16:01 +00:00
|
|
|
return nil, true, err
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
2019-07-02 17:38:12 +00:00
|
|
|
}
|
|
|
|
// Retrieve cached storage CIDs
|
2020-01-17 23:16:01 +00:00
|
|
|
if !streamFilter.StorageFilter.Off {
|
|
|
|
cw.StorageNodes, err = ecr.RetrieveStorageCIDs(tx, streamFilter.StorageFilter, blockNumber)
|
2020-01-16 20:48:38 +00:00
|
|
|
if err != nil {
|
|
|
|
if err := tx.Rollback(); err != nil {
|
|
|
|
log.Error(err)
|
2019-08-28 18:41:49 +00:00
|
|
|
}
|
2019-07-02 17:38:12 +00:00
|
|
|
log.Error("storage cid retrieval error")
|
2020-01-17 23:16:01 +00:00
|
|
|
return nil, true, err
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-17 23:16:01 +00:00
|
|
|
return cw, empty(cw), tx.Commit()
|
|
|
|
}
|
2019-05-21 19:27:24 +00:00
|
|
|
|
2020-01-17 23:16:01 +00:00
|
|
|
func empty(cidWrapper *CIDWrapper) bool {
|
|
|
|
if len(cidWrapper.Transactions) > 0 || len(cidWrapper.Headers) > 0 || len(cidWrapper.Uncles) > 0 || len(cidWrapper.Receipts) > 0 || len(cidWrapper.StateNodes) > 0 || len(cidWrapper.StorageNodes) > 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
|
|
|
|
2020-01-16 20:48:38 +00:00
|
|
|
// RetrieveHeaderCIDs retrieves and returns all of the header cids at the provided blockheight
|
2020-01-17 23:16:01 +00:00
|
|
|
func (ecr *CIDRetriever) RetrieveHeaderCIDs(tx *sqlx.Tx, blockNumber int64) ([]HeaderModel, error) {
|
2019-06-07 16:01:29 +00:00
|
|
|
log.Debug("retrieving header cids for block ", blockNumber)
|
2020-01-17 23:16:01 +00:00
|
|
|
headers := make([]HeaderModel, 0)
|
|
|
|
pgStr := `SELECT * FROM header_cids
|
2019-10-08 20:31:07 +00:00
|
|
|
WHERE block_number = $1 AND uncle IS FALSE`
|
2019-06-18 17:28:57 +00:00
|
|
|
err := tx.Select(&headers, pgStr, blockNumber)
|
|
|
|
return headers, err
|
|
|
|
}
|
|
|
|
|
2020-01-16 20:48:38 +00:00
|
|
|
// RetrieveUncleCIDs retrieves and returns all of the uncle cids at the provided blockheight
|
2020-01-17 23:16:01 +00:00
|
|
|
func (ecr *CIDRetriever) RetrieveUncleCIDs(tx *sqlx.Tx, blockNumber int64) ([]HeaderModel, error) {
|
2019-06-18 17:28:57 +00:00
|
|
|
log.Debug("retrieving header cids for block ", blockNumber)
|
2020-01-17 23:16:01 +00:00
|
|
|
headers := make([]HeaderModel, 0)
|
|
|
|
pgStr := `SELECT * FROM header_cids
|
2019-10-08 20:31:07 +00:00
|
|
|
WHERE block_number = $1 AND uncle IS TRUE`
|
2019-06-07 13:42:10 +00:00
|
|
|
err := tx.Select(&headers, pgStr, blockNumber)
|
|
|
|
return headers, err
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
|
|
|
|
2020-01-16 20:48:38 +00:00
|
|
|
// RetrieveTrxCIDs retrieves and returns all of the trx cids at the provided blockheight that conform to the provided filter parameters
|
|
|
|
// also returns the ids for the returned transaction cids
|
2020-01-17 23:16:01 +00:00
|
|
|
func (ecr *CIDRetriever) RetrieveTrxCIDs(tx *sqlx.Tx, txFilter config.TxFilter, blockNumber int64) ([]TxModel, error) {
|
2019-06-07 16:01:29 +00:00
|
|
|
log.Debug("retrieving transaction cids for block ", blockNumber)
|
2019-05-21 19:27:24 +00:00
|
|
|
args := make([]interface{}, 0, 3)
|
2020-01-17 23:16:01 +00:00
|
|
|
results := make([]TxModel, 0)
|
|
|
|
pgStr := `SELECT transaction_cids.id, transaction_cids.header_id,
|
|
|
|
transaction_cids.tx_hash, transaction_cids.cid,
|
|
|
|
transaction_cids.dst, transaction_cids.src
|
|
|
|
FROM transaction_cids INNER JOIN header_cids ON (transaction_cids.header_id = header_cids.id)
|
2019-05-21 19:27:24 +00:00
|
|
|
WHERE header_cids.block_number = $1`
|
|
|
|
args = append(args, blockNumber)
|
2020-01-16 20:48:38 +00:00
|
|
|
if len(txFilter.Dst) > 0 {
|
2019-05-21 19:27:24 +00:00
|
|
|
pgStr += ` AND transaction_cids.dst = ANY($2::VARCHAR(66)[])`
|
2020-01-16 20:48:38 +00:00
|
|
|
args = append(args, pq.Array(txFilter.Dst))
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
2020-01-16 20:48:38 +00:00
|
|
|
if len(txFilter.Src) > 0 {
|
2019-05-21 19:27:24 +00:00
|
|
|
pgStr += ` AND transaction_cids.src = ANY($3::VARCHAR(66)[])`
|
2020-01-16 20:48:38 +00:00
|
|
|
args = append(args, pq.Array(txFilter.Src))
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
2020-01-21 19:12:35 +00:00
|
|
|
return results, tx.Select(&results, pgStr, args...)
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
|
|
|
|
2020-01-16 20:48:38 +00:00
|
|
|
// RetrieveRctCIDs retrieves and returns all of the rct cids at the provided blockheight that conform to the provided
|
|
|
|
// filter parameters and correspond to the provided tx ids
|
2020-01-21 19:12:35 +00:00
|
|
|
func (ecr *CIDRetriever) RetrieveRctCIDs(tx *sqlx.Tx, rctFilter config.ReceiptFilter, blockNumber int64, blockHash *common.Hash, trxIds []int64) ([]ReceiptModel, error) {
|
2019-06-07 16:01:29 +00:00
|
|
|
log.Debug("retrieving receipt cids for block ", blockNumber)
|
2020-01-21 19:12:35 +00:00
|
|
|
id := 1
|
2019-08-28 18:41:49 +00:00
|
|
|
args := make([]interface{}, 0, 4)
|
2020-01-17 23:16:01 +00:00
|
|
|
pgStr := `SELECT receipt_cids.id, receipt_cids.tx_id, receipt_cids.cid,
|
2020-01-21 19:12:35 +00:00
|
|
|
receipt_cids.contract, receipt_cids.topic0s, receipt_cids.topic1s,
|
|
|
|
receipt_cids.topic2s, receipt_cids.topic3s
|
2020-01-17 23:16:01 +00:00
|
|
|
FROM receipt_cids, transaction_cids, header_cids
|
2019-05-21 19:27:24 +00:00
|
|
|
WHERE receipt_cids.tx_id = transaction_cids.id
|
2020-01-21 19:12:35 +00:00
|
|
|
AND transaction_cids.header_id = header_cids.id`
|
|
|
|
if blockNumber > 0 {
|
|
|
|
pgStr += fmt.Sprintf(` AND header_cids.block_number = $%d`, id)
|
|
|
|
args = append(args, blockNumber)
|
|
|
|
id++
|
|
|
|
}
|
|
|
|
if blockHash != nil {
|
|
|
|
pgStr += fmt.Sprintf(` AND header_cids.block_hash = $%d`, id)
|
|
|
|
args = append(args, blockHash.String())
|
|
|
|
id++
|
|
|
|
}
|
|
|
|
if len(rctFilter.Contracts) > 0 {
|
|
|
|
// Filter on contract addresses if there are any
|
|
|
|
pgStr += fmt.Sprintf(` AND ((receipt_cids.contract = ANY($%d::VARCHAR(66)[])`, id)
|
|
|
|
args = append(args, pq.Array(rctFilter.Contracts))
|
|
|
|
id++
|
|
|
|
// Filter on topics if there are any
|
|
|
|
if len(rctFilter.Topics) > 0 {
|
|
|
|
pgStr += " AND ("
|
|
|
|
first := true
|
|
|
|
for i, topicSet := range rctFilter.Topics {
|
|
|
|
if i < 4 && len(topicSet) > 0 {
|
|
|
|
if first {
|
|
|
|
pgStr += fmt.Sprintf(`receipt_cids.topic%ds && $%d::VARCHAR(66)[]`, i, id)
|
|
|
|
first = false
|
|
|
|
} else {
|
|
|
|
pgStr += fmt.Sprintf(` AND receipt_cids.topic%ds && $%d::VARCHAR(66)[]`, i, id)
|
|
|
|
}
|
|
|
|
args = append(args, pq.Array(topicSet))
|
|
|
|
id++
|
|
|
|
}
|
2019-08-28 18:41:49 +00:00
|
|
|
}
|
2020-01-21 19:12:35 +00:00
|
|
|
pgStr += ")"
|
2019-08-28 18:41:49 +00:00
|
|
|
}
|
2020-01-21 19:12:35 +00:00
|
|
|
pgStr += ")"
|
|
|
|
// Filter on txIDs if there are any and we are matching txs
|
|
|
|
if rctFilter.MatchTxs && len(trxIds) > 0 {
|
|
|
|
pgStr += fmt.Sprintf(` OR receipt_cids.tx_id = ANY($%d::INTEGER[])`, id)
|
|
|
|
args = append(args, pq.Array(trxIds))
|
|
|
|
}
|
|
|
|
pgStr += ")"
|
|
|
|
} else { // If there are no contract addresses to filter on
|
|
|
|
// Filter on topics if there are any
|
|
|
|
if len(rctFilter.Topics) > 0 {
|
|
|
|
pgStr += " AND (("
|
|
|
|
first := true
|
|
|
|
for i, topicSet := range rctFilter.Topics {
|
|
|
|
if i < 4 && len(topicSet) > 0 {
|
|
|
|
if first {
|
|
|
|
pgStr += fmt.Sprintf(`receipt_cids.topic%ds && $%d::VARCHAR(66)[]`, i, id)
|
|
|
|
first = false
|
|
|
|
} else {
|
|
|
|
pgStr += fmt.Sprintf(` AND receipt_cids.topic%ds && $%d::VARCHAR(66)[]`, i, id)
|
|
|
|
}
|
|
|
|
args = append(args, pq.Array(topicSet))
|
|
|
|
id++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pgStr += ")"
|
|
|
|
// Filter on txIDs if there are any and we are matching txs
|
2020-01-16 20:48:38 +00:00
|
|
|
if rctFilter.MatchTxs && len(trxIds) > 0 {
|
2020-01-21 19:12:35 +00:00
|
|
|
pgStr += fmt.Sprintf(` OR receipt_cids.tx_id = ANY($%d::INTEGER[])`, id)
|
2019-08-28 18:41:49 +00:00
|
|
|
args = append(args, pq.Array(trxIds))
|
|
|
|
}
|
2020-01-21 19:12:35 +00:00
|
|
|
pgStr += ")"
|
2020-01-16 20:48:38 +00:00
|
|
|
} else if rctFilter.MatchTxs && len(trxIds) > 0 {
|
2020-01-21 19:12:35 +00:00
|
|
|
// If there are no contract addresses or topics to filter on,
|
|
|
|
// Filter on txIDs if there are any and we are matching txs
|
|
|
|
pgStr += fmt.Sprintf(` AND receipt_cids.tx_id = ANY($%d::INTEGER[])`, id)
|
2019-08-28 18:41:49 +00:00
|
|
|
args = append(args, pq.Array(trxIds))
|
|
|
|
}
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
2020-01-17 23:16:01 +00:00
|
|
|
receiptCids := make([]ReceiptModel, 0)
|
2020-01-21 19:12:35 +00:00
|
|
|
return receiptCids, tx.Select(&receiptCids, pgStr, args...)
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
|
|
|
|
2020-01-16 20:48:38 +00:00
|
|
|
// RetrieveStateCIDs retrieves and returns all of the state node cids at the provided blockheight that conform to the provided filter parameters
|
2020-01-17 23:16:01 +00:00
|
|
|
func (ecr *CIDRetriever) RetrieveStateCIDs(tx *sqlx.Tx, stateFilter config.StateFilter, blockNumber int64) ([]StateNodeModel, error) {
|
2019-06-07 16:01:29 +00:00
|
|
|
log.Debug("retrieving state cids for block ", blockNumber)
|
2019-05-21 19:27:24 +00:00
|
|
|
args := make([]interface{}, 0, 2)
|
2020-01-17 23:16:01 +00:00
|
|
|
pgStr := `SELECT state_cids.id, state_cids.header_id,
|
|
|
|
state_cids.state_key, state_cids.leaf, state_cids.cid
|
|
|
|
FROM state_cids INNER JOIN header_cids ON (state_cids.header_id = header_cids.id)
|
2019-05-21 19:27:24 +00:00
|
|
|
WHERE header_cids.block_number = $1`
|
|
|
|
args = append(args, blockNumber)
|
2020-01-16 20:48:38 +00:00
|
|
|
addrLen := len(stateFilter.Addresses)
|
2019-05-21 19:27:24 +00:00
|
|
|
if addrLen > 0 {
|
2020-01-21 19:12:35 +00:00
|
|
|
keys := make([]string, addrLen)
|
|
|
|
for i, addr := range stateFilter.Addresses {
|
|
|
|
keys[i] = crypto.Keccak256Hash(common.HexToAddress(addr).Bytes()).String()
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
|
|
|
pgStr += ` AND state_cids.state_key = ANY($2::VARCHAR(66)[])`
|
|
|
|
args = append(args, pq.Array(keys))
|
|
|
|
}
|
2020-01-16 20:48:38 +00:00
|
|
|
if !stateFilter.IntermediateNodes {
|
2019-06-07 13:42:10 +00:00
|
|
|
pgStr += ` AND state_cids.leaf = TRUE`
|
|
|
|
}
|
2020-01-17 23:16:01 +00:00
|
|
|
stateNodeCIDs := make([]StateNodeModel, 0)
|
2020-01-21 19:12:35 +00:00
|
|
|
return stateNodeCIDs, tx.Select(&stateNodeCIDs, pgStr, args...)
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
|
|
|
|
2020-01-16 20:48:38 +00:00
|
|
|
// RetrieveStorageCIDs retrieves and returns all of the storage node cids at the provided blockheight that conform to the provided filter parameters
|
2020-01-17 23:16:01 +00:00
|
|
|
func (ecr *CIDRetriever) RetrieveStorageCIDs(tx *sqlx.Tx, storageFilter config.StorageFilter, blockNumber int64) ([]StorageNodeWithStateKeyModel, error) {
|
2019-06-07 16:01:29 +00:00
|
|
|
log.Debug("retrieving storage cids for block ", blockNumber)
|
2019-05-21 19:27:24 +00:00
|
|
|
args := make([]interface{}, 0, 3)
|
2020-01-17 23:16:01 +00:00
|
|
|
pgStr := `SELECT storage_cids.id, storage_cids.state_id, storage_cids.storage_key,
|
|
|
|
storage_cids.leaf, storage_cids.cid, state_cids.state_key FROM storage_cids, state_cids, header_cids
|
2019-05-21 19:27:24 +00:00
|
|
|
WHERE storage_cids.state_id = state_cids.id
|
|
|
|
AND state_cids.header_id = header_cids.id
|
|
|
|
AND header_cids.block_number = $1`
|
|
|
|
args = append(args, blockNumber)
|
2020-01-16 20:48:38 +00:00
|
|
|
addrLen := len(storageFilter.Addresses)
|
2019-05-21 19:27:24 +00:00
|
|
|
if addrLen > 0 {
|
2020-01-21 19:12:35 +00:00
|
|
|
keys := make([]string, addrLen)
|
|
|
|
for i, addr := range storageFilter.Addresses {
|
|
|
|
keys[i] = crypto.Keccak256Hash(common.HexToAddress(addr).Bytes()).String()
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
|
|
|
pgStr += ` AND state_cids.state_key = ANY($2::VARCHAR(66)[])`
|
|
|
|
args = append(args, pq.Array(keys))
|
2020-01-16 20:48:38 +00:00
|
|
|
if len(storageFilter.StorageKeys) > 0 {
|
|
|
|
pgStr += ` AND storage_cids.storage_key = ANY($3::VARCHAR(66)[])`
|
|
|
|
args = append(args, pq.Array(storageFilter.StorageKeys))
|
|
|
|
}
|
|
|
|
} else if len(storageFilter.StorageKeys) > 0 {
|
|
|
|
pgStr += ` AND storage_cids.storage_key = ANY($2::VARCHAR(66)[])`
|
|
|
|
args = append(args, pq.Array(storageFilter.StorageKeys))
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
2020-01-16 20:48:38 +00:00
|
|
|
if !storageFilter.IntermediateNodes {
|
2019-06-07 13:42:10 +00:00
|
|
|
pgStr += ` AND storage_cids.leaf = TRUE`
|
|
|
|
}
|
2020-01-17 23:16:01 +00:00
|
|
|
storageNodeCIDs := make([]StorageNodeWithStateKeyModel, 0)
|
2020-01-21 19:12:35 +00:00
|
|
|
return storageNodeCIDs, tx.Select(&storageNodeCIDs, pgStr, args...)
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
2019-10-02 14:10:37 +00:00
|
|
|
|
2019-10-08 19:51:38 +00:00
|
|
|
// RetrieveGapsInData is used to find the the block numbers at which we are missing data in the db
|
2020-01-17 23:16:01 +00:00
|
|
|
func (ecr *CIDRetriever) RetrieveGapsInData() ([]shared.Gap, error) {
|
2019-10-02 14:10:37 +00:00
|
|
|
pgStr := `SELECT header_cids.block_number + 1 AS start, min(fr.block_number) - 1 AS stop FROM header_cids
|
|
|
|
LEFT JOIN header_cids r on header_cids.block_number = r.block_number - 1
|
|
|
|
LEFT JOIN header_cids fr on header_cids.block_number < fr.block_number
|
|
|
|
WHERE r.block_number is NULL and fr.block_number IS NOT NULL
|
|
|
|
GROUP BY header_cids.block_number, r.block_number`
|
2020-01-17 23:16:01 +00:00
|
|
|
results := make([]struct {
|
|
|
|
Start uint64 `db:"start"`
|
|
|
|
Stop uint64 `db:"stop"`
|
|
|
|
}, 0)
|
|
|
|
err := ecr.db.Select(&results, pgStr)
|
2019-10-02 14:10:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-17 23:16:01 +00:00
|
|
|
gaps := make([]shared.Gap, len(results))
|
|
|
|
for i, res := range results {
|
|
|
|
gaps[i] = shared.Gap{
|
|
|
|
Start: res.Start,
|
|
|
|
Stop: res.Stop,
|
|
|
|
}
|
2019-10-02 14:10:37 +00:00
|
|
|
}
|
2020-01-17 23:16:01 +00:00
|
|
|
return gaps, nil
|
2019-10-02 14:10:37 +00:00
|
|
|
}
|
2020-01-16 20:48:38 +00:00
|
|
|
|
2020-01-17 23:16:01 +00:00
|
|
|
func (ecr *CIDRetriever) Database() *postgres.DB {
|
2020-01-16 20:48:38 +00:00
|
|
|
return ecr.db
|
|
|
|
}
|