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/>.
|
|
|
|
|
2019-10-02 14:10:37 +00:00
|
|
|
package super_node
|
2019-05-21 19:27:24 +00:00
|
|
|
|
|
|
|
import (
|
2019-06-18 17:28:57 +00:00
|
|
|
"math/big"
|
|
|
|
|
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-06-06 17:55:59 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/config"
|
2019-05-21 19:27:24 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
|
2019-08-26 02:13:40 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/ipfs"
|
2019-05-21 19:27:24 +00:00
|
|
|
)
|
|
|
|
|
2019-06-06 03:50:12 +00:00
|
|
|
// CIDRetriever is the interface for retrieving CIDs from the Postgres cache
|
2019-05-21 19:27:24 +00:00
|
|
|
type CIDRetriever interface {
|
2019-08-05 17:56:15 +00:00
|
|
|
RetrieveCIDs(streamFilters config.Subscription, blockNumber int64) (*ipfs.CIDWrapper, error)
|
2019-07-02 17:38:12 +00:00
|
|
|
RetrieveLastBlockNumber() (int64, error)
|
|
|
|
RetrieveFirstBlockNumber() (int64, error)
|
2019-11-01 19:03:28 +00:00
|
|
|
RetrieveGapsInData() ([][2]uint64, error)
|
2020-01-16 20:48:38 +00:00
|
|
|
RetrieveHeaderCIDs(tx *sqlx.Tx, blockNumber int64) ([]string, error)
|
|
|
|
RetrieveUncleCIDs(tx *sqlx.Tx, blockNumber int64) ([]string, error)
|
|
|
|
RetrieveTrxCIDs(tx *sqlx.Tx, txFilter config.TrxFilter, blockNumber int64) ([]string, []int64, error)
|
|
|
|
RetrieveRctCIDs(tx *sqlx.Tx, rctFilter config.ReceiptFilter, blockNumber int64, trxIds []int64) ([]string, error)
|
|
|
|
RetrieveStateCIDs(tx *sqlx.Tx, stateFilter config.StateFilter, blockNumber int64) ([]ipfs.StateNodeCID, error)
|
|
|
|
Database() *postgres.DB
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
|
|
|
|
2019-06-06 03:50:12 +00:00
|
|
|
// EthCIDRetriever is the underlying struct supporting the CIDRetriever interface
|
2019-05-21 19:27:24 +00:00
|
|
|
type EthCIDRetriever struct {
|
|
|
|
db *postgres.DB
|
|
|
|
}
|
|
|
|
|
2019-06-06 03:50:12 +00:00
|
|
|
// NewCIDRetriever returns a pointer to a new EthCIDRetriever which supports the CIDRetriever interface
|
2019-05-21 19:27:24 +00:00
|
|
|
func NewCIDRetriever(db *postgres.DB) *EthCIDRetriever {
|
|
|
|
return &EthCIDRetriever{
|
|
|
|
db: db,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-08 19:51:38 +00:00
|
|
|
// RetrieveFirstBlockNumber is used to retrieve the first block number in the db
|
2019-07-02 17:38:12 +00:00
|
|
|
func (ecr *EthCIDRetriever) RetrieveFirstBlockNumber() (int64, error) {
|
|
|
|
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
|
2019-07-02 17:38:12 +00:00
|
|
|
func (ecr *EthCIDRetriever) 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
|
|
|
|
|
|
|
// RetrieveCIDs is used to retrieve all of the CIDs which conform to the passed StreamFilters
|
2019-08-05 17:56:15 +00:00
|
|
|
func (ecr *EthCIDRetriever) RetrieveCIDs(streamFilters config.Subscription, blockNumber int64) (*ipfs.CIDWrapper, error) {
|
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 {
|
|
|
|
return nil, err
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
2020-01-16 20:48:38 +00:00
|
|
|
|
2019-08-05 17:56:15 +00:00
|
|
|
cw := new(ipfs.CIDWrapper)
|
2019-07-02 17:38:12 +00:00
|
|
|
cw.BlockNumber = big.NewInt(blockNumber)
|
2019-06-18 17:28:57 +00:00
|
|
|
|
2019-07-02 17:38:12 +00:00
|
|
|
// Retrieve cached header CIDs
|
|
|
|
if !streamFilters.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-16 20:48:38 +00:00
|
|
|
return nil, err
|
2019-07-02 17:38:12 +00:00
|
|
|
}
|
2019-10-08 20:31:07 +00:00
|
|
|
if streamFilters.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-16 20:48:38 +00:00
|
|
|
return nil, err
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
|
|
|
}
|
2019-07-02 17:38:12 +00:00
|
|
|
}
|
2019-06-18 17:28:57 +00:00
|
|
|
|
2019-07-02 17:38:12 +00:00
|
|
|
// Retrieve cached trx CIDs
|
|
|
|
var trxIds []int64
|
|
|
|
if !streamFilters.TrxFilter.Off {
|
2020-01-16 20:48:38 +00:00
|
|
|
cw.Transactions, trxIds, err = ecr.RetrieveTrxCIDs(tx, streamFilters.TrxFilter, blockNumber)
|
|
|
|
if err != nil {
|
|
|
|
err := tx.Rollback()
|
|
|
|
if err != nil {
|
|
|
|
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-16 20:48:38 +00:00
|
|
|
return nil, err
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
2019-07-02 17:38:12 +00:00
|
|
|
}
|
2019-06-18 17:28:57 +00:00
|
|
|
|
2019-07-02 17:38:12 +00:00
|
|
|
// Retrieve cached receipt CIDs
|
|
|
|
if !streamFilters.ReceiptFilter.Off {
|
2020-01-16 20:48:38 +00:00
|
|
|
cw.Receipts, err = ecr.RetrieveRctCIDs(tx, streamFilters.ReceiptFilter, blockNumber, trxIds)
|
|
|
|
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-16 20:48:38 +00:00
|
|
|
return nil, err
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
2019-07-02 17:38:12 +00:00
|
|
|
}
|
2019-06-18 17:28:57 +00:00
|
|
|
|
2019-07-02 17:38:12 +00:00
|
|
|
// Retrieve cached state CIDs
|
|
|
|
if !streamFilters.StateFilter.Off {
|
2020-01-16 20:48:38 +00:00
|
|
|
cw.StateNodes, err = ecr.RetrieveStateCIDs(tx, streamFilters.StateFilter, 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("state cid retrieval error")
|
2020-01-16 20:48:38 +00:00
|
|
|
return nil, err
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
2019-07-02 17:38:12 +00:00
|
|
|
}
|
2019-06-18 17:28:57 +00:00
|
|
|
|
2019-07-02 17:38:12 +00:00
|
|
|
// Retrieve cached storage CIDs
|
|
|
|
if !streamFilters.StorageFilter.Off {
|
2020-01-16 20:48:38 +00:00
|
|
|
cw.StorageNodes, err = ecr.RetrieveStorageCIDs(tx, streamFilters.StorageFilter, 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("storage cid retrieval error")
|
2020-01-16 20:48:38 +00:00
|
|
|
return nil, err
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-02 17:38:12 +00:00
|
|
|
return cw, tx.Commit()
|
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
|
|
|
|
func (ecr *EthCIDRetriever) RetrieveHeaderCIDs(tx *sqlx.Tx, blockNumber int64) ([]string, error) {
|
2019-06-07 16:01:29 +00:00
|
|
|
log.Debug("retrieving header cids for block ", blockNumber)
|
2019-06-07 13:42:10 +00:00
|
|
|
headers := make([]string, 0)
|
|
|
|
pgStr := `SELECT cid 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
|
|
|
|
func (ecr *EthCIDRetriever) RetrieveUncleCIDs(tx *sqlx.Tx, blockNumber int64) ([]string, error) {
|
2019-06-18 17:28:57 +00:00
|
|
|
log.Debug("retrieving header cids for block ", blockNumber)
|
|
|
|
headers := make([]string, 0)
|
|
|
|
pgStr := `SELECT cid 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
|
|
|
|
func (ecr *EthCIDRetriever) RetrieveTrxCIDs(tx *sqlx.Tx, txFilter config.TrxFilter, blockNumber int64) ([]string, []int64, 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)
|
|
|
|
type result struct {
|
2019-06-06 03:50:12 +00:00
|
|
|
ID int64 `db:"id"`
|
2019-05-21 19:27:24 +00:00
|
|
|
Cid string `db:"cid"`
|
|
|
|
}
|
|
|
|
results := make([]result, 0)
|
|
|
|
pgStr := `SELECT transaction_cids.id, transaction_cids.cid FROM transaction_cids INNER JOIN header_cids ON (transaction_cids.header_id = header_cids.id)
|
|
|
|
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
|
|
|
}
|
2019-06-07 13:42:10 +00:00
|
|
|
err := tx.Select(&results, pgStr, args...)
|
2019-05-21 19:27:24 +00:00
|
|
|
if err != nil {
|
2019-06-07 13:42:10 +00:00
|
|
|
return nil, nil, err
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
2019-06-07 13:42:10 +00:00
|
|
|
ids := make([]int64, 0, len(results))
|
|
|
|
cids := make([]string, 0, len(results))
|
2019-05-21 19:27:24 +00:00
|
|
|
for _, res := range results {
|
2019-06-07 13:42:10 +00:00
|
|
|
cids = append(cids, res.Cid)
|
2019-06-06 03:50:12 +00:00
|
|
|
ids = append(ids, res.ID)
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
2019-06-07 13:42:10 +00:00
|
|
|
return cids, ids, nil
|
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
|
|
|
|
func (ecr *EthCIDRetriever) RetrieveRctCIDs(tx *sqlx.Tx, rctFilter config.ReceiptFilter, blockNumber int64, trxIds []int64) ([]string, error) {
|
2019-06-07 16:01:29 +00:00
|
|
|
log.Debug("retrieving receipt cids for block ", blockNumber)
|
2019-08-28 18:41:49 +00:00
|
|
|
args := make([]interface{}, 0, 4)
|
2019-05-21 19:27:24 +00:00
|
|
|
pgStr := `SELECT receipt_cids.cid FROM receipt_cids, transaction_cids, header_cids
|
|
|
|
WHERE receipt_cids.tx_id = transaction_cids.id
|
|
|
|
AND transaction_cids.header_id = header_cids.id
|
|
|
|
AND header_cids.block_number = $1`
|
|
|
|
args = append(args, blockNumber)
|
2020-01-16 20:48:38 +00:00
|
|
|
if len(rctFilter.Topic0s) > 0 {
|
2019-06-20 15:59:10 +00:00
|
|
|
pgStr += ` AND ((receipt_cids.topic0s && $2::VARCHAR(66)[]`
|
2020-01-16 20:48:38 +00:00
|
|
|
args = append(args, pq.Array(rctFilter.Topic0s))
|
|
|
|
if len(rctFilter.Contracts) > 0 {
|
2019-08-28 18:41:49 +00:00
|
|
|
pgStr += ` AND receipt_cids.contract = ANY($3::VARCHAR(66)[]))`
|
2020-01-16 20:48:38 +00:00
|
|
|
args = append(args, pq.Array(rctFilter.Contracts))
|
|
|
|
if rctFilter.MatchTxs && len(trxIds) > 0 {
|
2019-08-28 18:41:49 +00:00
|
|
|
pgStr += ` OR receipt_cids.tx_id = ANY($4::INTEGER[]))`
|
|
|
|
args = append(args, pq.Array(trxIds))
|
|
|
|
} else {
|
|
|
|
pgStr += `)`
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
pgStr += `)`
|
2020-01-16 20:48:38 +00:00
|
|
|
if rctFilter.MatchTxs && len(trxIds) > 0 {
|
2019-08-28 18:41:49 +00:00
|
|
|
pgStr += ` OR receipt_cids.tx_id = ANY($3::INTEGER[]))`
|
|
|
|
args = append(args, pq.Array(trxIds))
|
|
|
|
} else {
|
|
|
|
pgStr += `)`
|
|
|
|
}
|
|
|
|
}
|
2019-05-21 19:27:24 +00:00
|
|
|
} else {
|
2020-01-16 20:48:38 +00:00
|
|
|
if len(rctFilter.Contracts) > 0 {
|
2019-08-28 18:41:49 +00:00
|
|
|
pgStr += ` AND (receipt_cids.contract = ANY($2::VARCHAR(66)[])`
|
2020-01-16 20:48:38 +00:00
|
|
|
args = append(args, pq.Array(rctFilter.Contracts))
|
|
|
|
if rctFilter.MatchTxs && len(trxIds) > 0 {
|
2019-08-28 18:41:49 +00:00
|
|
|
pgStr += ` OR receipt_cids.tx_id = ANY($3::INTEGER[]))`
|
|
|
|
args = append(args, pq.Array(trxIds))
|
|
|
|
} else {
|
|
|
|
pgStr += `)`
|
|
|
|
}
|
2020-01-16 20:48:38 +00:00
|
|
|
} else if rctFilter.MatchTxs && len(trxIds) > 0 {
|
2019-08-28 18:41:49 +00:00
|
|
|
pgStr += ` AND receipt_cids.tx_id = ANY($2::INTEGER[])`
|
|
|
|
args = append(args, pq.Array(trxIds))
|
|
|
|
}
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
2019-06-07 13:42:10 +00:00
|
|
|
receiptCids := make([]string, 0)
|
|
|
|
err := tx.Select(&receiptCids, pgStr, args...)
|
|
|
|
return receiptCids, err
|
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
|
|
|
|
func (ecr *EthCIDRetriever) RetrieveStateCIDs(tx *sqlx.Tx, stateFilter config.StateFilter, blockNumber int64) ([]ipfs.StateNodeCID, 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)
|
2019-08-27 19:22:09 +00:00
|
|
|
pgStr := `SELECT state_cids.cid, state_cids.state_key, state_cids.leaf 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 {
|
|
|
|
keys := make([]string, 0, addrLen)
|
2020-01-16 20:48:38 +00:00
|
|
|
for _, addr := range stateFilter.Addresses {
|
2019-08-05 17:56:15 +00:00
|
|
|
keys = append(keys, ipfs.HexToKey(addr).Hex())
|
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`
|
|
|
|
}
|
2019-08-05 17:56:15 +00:00
|
|
|
stateNodeCIDs := make([]ipfs.StateNodeCID, 0)
|
2019-06-07 13:42:10 +00:00
|
|
|
err := tx.Select(&stateNodeCIDs, pgStr, args...)
|
|
|
|
return stateNodeCIDs, err
|
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
|
|
|
|
func (ecr *EthCIDRetriever) RetrieveStorageCIDs(tx *sqlx.Tx, storageFilter config.StorageFilter, blockNumber int64) ([]ipfs.StorageNodeCID, 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)
|
2019-08-27 19:22:09 +00:00
|
|
|
pgStr := `SELECT storage_cids.cid, state_cids.state_key, storage_cids.storage_key, storage_cids.leaf 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 {
|
|
|
|
keys := make([]string, 0, addrLen)
|
2020-01-16 20:48:38 +00:00
|
|
|
for _, addr := range storageFilter.Addresses {
|
2019-08-05 17:56:15 +00:00
|
|
|
keys = append(keys, ipfs.HexToKey(addr).Hex())
|
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`
|
|
|
|
}
|
2019-08-05 17:56:15 +00:00
|
|
|
storageNodeCIDs := make([]ipfs.StorageNodeCID, 0)
|
2019-06-07 13:42:10 +00:00
|
|
|
err := tx.Select(&storageNodeCIDs, pgStr, args...)
|
|
|
|
return storageNodeCIDs, err
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
2019-10-02 14:10:37 +00:00
|
|
|
|
|
|
|
type gap struct {
|
2019-11-01 19:03:28 +00:00
|
|
|
Start uint64 `db:"start"`
|
|
|
|
Stop uint64 `db:"stop"`
|
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
|
2019-11-01 19:03:28 +00:00
|
|
|
func (ecr *EthCIDRetriever) RetrieveGapsInData() ([][2]uint64, 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`
|
|
|
|
gaps := make([]gap, 0)
|
|
|
|
err := ecr.db.Select(&gaps, pgStr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-11-01 19:03:28 +00:00
|
|
|
gapRanges := make([][2]uint64, 0)
|
2019-10-02 14:10:37 +00:00
|
|
|
for _, gap := range gaps {
|
2019-11-01 19:03:28 +00:00
|
|
|
gapRanges = append(gapRanges, [2]uint64{gap.Start, gap.Stop})
|
2019-10-02 14:10:37 +00:00
|
|
|
}
|
|
|
|
return gapRanges, nil
|
|
|
|
}
|
2020-01-16 20:48:38 +00:00
|
|
|
|
|
|
|
func (ecr *EthCIDRetriever) Database() *postgres.DB {
|
|
|
|
return ecr.db
|
|
|
|
}
|