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-04-20 13:14:02 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/libraries/shared/storage/utils"
|
|
|
|
|
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"
|
|
|
|
|
2020-02-10 15:00:55 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/postgres"
|
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
|
2020-01-31 19:25:15 +00:00
|
|
|
err := ecr.db.Get(&blockNumber, "SELECT block_number FROM eth.header_cids ORDER BY block_number ASC LIMIT 1")
|
2019-07-02 17:38:12 +00:00
|
|
|
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
|
2020-01-31 19:25:15 +00:00
|
|
|
err := ecr.db.Get(&blockNumber, "SELECT block_number FROM eth.header_cids ORDER BY block_number DESC LIMIT 1 ")
|
2019-05-21 19:27:24 +00:00
|
|
|
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
|
2020-02-23 23:14:29 +00:00
|
|
|
func (ecr *CIDRetriever) Retrieve(filter shared.SubscriptionSettings, blockNumber int64) ([]shared.CIDsForFetching, bool, error) {
|
2020-02-03 18:22:29 +00:00
|
|
|
streamFilter, ok := filter.(*SubscriptionSettings)
|
2020-01-17 23:16:01 +00:00
|
|
|
if !ok {
|
2020-02-03 18:22:29 +00:00
|
|
|
return nil, true, fmt.Errorf("eth retriever expected filter type %T got %T", &SubscriptionSettings{}, filter)
|
2020-01-17 23:16:01 +00:00
|
|
|
}
|
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-02-23 23:14:29 +00:00
|
|
|
// Retrieve cached header CIDs at this block height
|
|
|
|
headers, err := ecr.RetrieveHeaderCIDs(tx, blockNumber)
|
|
|
|
if err != nil {
|
|
|
|
if err := tx.Rollback(); err != nil {
|
|
|
|
log.Error(err)
|
2019-07-02 17:38:12 +00:00
|
|
|
}
|
2020-02-23 23:14:29 +00:00
|
|
|
log.Error("header cid retrieval error")
|
|
|
|
return nil, true, err
|
|
|
|
}
|
|
|
|
cws := make([]shared.CIDsForFetching, len(headers))
|
|
|
|
empty := true
|
|
|
|
for i, header := range headers {
|
|
|
|
cw := new(CIDWrapper)
|
|
|
|
cw.BlockNumber = big.NewInt(blockNumber)
|
|
|
|
if !streamFilter.HeaderFilter.Off {
|
|
|
|
cw.Header = header
|
|
|
|
empty = false
|
|
|
|
if streamFilter.HeaderFilter.Uncles {
|
|
|
|
// Retrieve uncle cids for this header id
|
|
|
|
uncleCIDs, err := ecr.RetrieveUncleCIDsByHeaderID(tx, header.ID)
|
2020-01-26 19:55:26 +00:00
|
|
|
if err != nil {
|
|
|
|
if err := tx.Rollback(); err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
log.Error("uncle cid retrieval error")
|
|
|
|
return nil, true, err
|
2019-08-28 18:41:49 +00:00
|
|
|
}
|
2020-02-23 23:14:29 +00:00
|
|
|
cw.Uncles = uncleCIDs
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
|
|
|
}
|
2020-02-23 23:14:29 +00:00
|
|
|
// Retrieve cached trx CIDs
|
|
|
|
if !streamFilter.TxFilter.Off {
|
|
|
|
cw.Transactions, err = ecr.RetrieveTxCIDs(tx, streamFilter.TxFilter, header.ID)
|
|
|
|
if err != nil {
|
|
|
|
if err := tx.Rollback(); err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
log.Error("transaction cid retrieval error")
|
|
|
|
return nil, true, err
|
|
|
|
}
|
|
|
|
if len(cw.Transactions) > 0 {
|
|
|
|
empty = false
|
2019-08-28 18:41:49 +00:00
|
|
|
}
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
2020-02-23 23:14:29 +00:00
|
|
|
trxIds := make([]int64, len(cw.Transactions))
|
|
|
|
for j, tx := range cw.Transactions {
|
|
|
|
trxIds[j] = tx.ID
|
|
|
|
}
|
|
|
|
// Retrieve cached receipt CIDs
|
|
|
|
if !streamFilter.ReceiptFilter.Off {
|
|
|
|
cw.Receipts, err = ecr.RetrieveRctCIDsByHeaderID(tx, streamFilter.ReceiptFilter, header.ID, trxIds)
|
|
|
|
if err != nil {
|
|
|
|
if err := tx.Rollback(); err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
log.Error("receipt cid retrieval error")
|
|
|
|
return nil, true, err
|
|
|
|
}
|
|
|
|
if len(cw.Receipts) > 0 {
|
|
|
|
empty = false
|
2019-08-28 18:41:49 +00:00
|
|
|
}
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
2020-02-23 23:14:29 +00:00
|
|
|
// Retrieve cached state CIDs
|
|
|
|
if !streamFilter.StateFilter.Off {
|
|
|
|
cw.StateNodes, err = ecr.RetrieveStateCIDs(tx, streamFilter.StateFilter, header.ID)
|
|
|
|
if err != nil {
|
|
|
|
if err := tx.Rollback(); err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
log.Error("state cid retrieval error")
|
|
|
|
return nil, true, err
|
|
|
|
}
|
|
|
|
if len(cw.StateNodes) > 0 {
|
|
|
|
empty = false
|
2019-08-28 18:41:49 +00:00
|
|
|
}
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
2020-02-23 23:14:29 +00:00
|
|
|
// Retrieve cached storage CIDs
|
|
|
|
if !streamFilter.StorageFilter.Off {
|
|
|
|
cw.StorageNodes, err = ecr.RetrieveStorageCIDs(tx, streamFilter.StorageFilter, header.ID)
|
|
|
|
if err != nil {
|
|
|
|
if err := tx.Rollback(); err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
log.Error("storage cid retrieval error")
|
|
|
|
return nil, true, err
|
|
|
|
}
|
|
|
|
if len(cw.StorageNodes) > 0 {
|
|
|
|
empty = false
|
2019-08-28 18:41:49 +00:00
|
|
|
}
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
2020-02-23 23:14:29 +00:00
|
|
|
cws[i] = cw
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
|
|
|
|
2020-02-23 23:14:29 +00:00
|
|
|
return cws, empty, 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
|
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)
|
2020-01-31 19:25:15 +00:00
|
|
|
pgStr := `SELECT * FROM eth.header_cids
|
2020-01-26 19:55:26 +00:00
|
|
|
WHERE block_number = $1`
|
|
|
|
return headers, tx.Select(&headers, pgStr, blockNumber)
|
2019-06-18 17:28:57 +00:00
|
|
|
}
|
|
|
|
|
2020-01-26 19:55:26 +00:00
|
|
|
// RetrieveUncleCIDsByHeaderID retrieves and returns all of the uncle cids for the provided header
|
|
|
|
func (ecr *CIDRetriever) RetrieveUncleCIDsByHeaderID(tx *sqlx.Tx, headerID int64) ([]UncleModel, error) {
|
|
|
|
log.Debug("retrieving uncle cids for block id ", headerID)
|
|
|
|
headers := make([]UncleModel, 0)
|
2020-01-31 19:25:15 +00:00
|
|
|
pgStr := `SELECT * FROM eth.uncle_cids
|
2020-01-26 19:55:26 +00:00
|
|
|
WHERE header_id = $1`
|
|
|
|
return headers, tx.Select(&headers, pgStr, headerID)
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
|
|
|
|
2020-01-26 19:55:26 +00:00
|
|
|
// RetrieveTxCIDs retrieves and returns all of the trx cids at the provided blockheight that conform to the provided filter parameters
|
2020-01-16 20:48:38 +00:00
|
|
|
// also returns the ids for the returned transaction cids
|
2020-02-23 23:14:29 +00:00
|
|
|
func (ecr *CIDRetriever) RetrieveTxCIDs(tx *sqlx.Tx, txFilter TxFilter, headerID int64) ([]TxModel, error) {
|
|
|
|
log.Debug("retrieving transaction cids for header id ", headerID)
|
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)
|
2020-02-09 21:28:30 +00:00
|
|
|
id := 1
|
|
|
|
pgStr := fmt.Sprintf(`SELECT transaction_cids.id, transaction_cids.header_id,
|
2020-01-17 23:16:01 +00:00
|
|
|
transaction_cids.tx_hash, transaction_cids.cid,
|
2020-01-26 19:55:26 +00:00
|
|
|
transaction_cids.dst, transaction_cids.src, transaction_cids.index
|
2020-01-31 19:25:15 +00:00
|
|
|
FROM eth.transaction_cids INNER JOIN eth.header_cids ON (transaction_cids.header_id = header_cids.id)
|
2020-02-23 23:14:29 +00:00
|
|
|
WHERE header_cids.id = $%d`, id)
|
|
|
|
args = append(args, headerID)
|
2020-02-09 21:28:30 +00:00
|
|
|
id++
|
2020-01-16 20:48:38 +00:00
|
|
|
if len(txFilter.Dst) > 0 {
|
2020-02-09 21:28:30 +00:00
|
|
|
pgStr += fmt.Sprintf(` AND transaction_cids.dst = ANY($%d::VARCHAR(66)[])`, id)
|
2020-01-16 20:48:38 +00:00
|
|
|
args = append(args, pq.Array(txFilter.Dst))
|
2020-02-09 21:28:30 +00:00
|
|
|
id++
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
2020-01-16 20:48:38 +00:00
|
|
|
if len(txFilter.Src) > 0 {
|
2020-02-09 21:28:30 +00:00
|
|
|
pgStr += fmt.Sprintf(` AND transaction_cids.src = ANY($%d::VARCHAR(66)[])`, id)
|
2020-01-16 20:48:38 +00:00
|
|
|
args = append(args, pq.Array(txFilter.Src))
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
2020-02-20 22:13:19 +00:00
|
|
|
pgStr += ` ORDER BY transaction_cids.index`
|
2020-01-21 19:12:35 +00:00
|
|
|
return results, tx.Select(&results, pgStr, args...)
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
|
|
|
|
2020-02-23 23:14:29 +00:00
|
|
|
// RetrieveRctCIDsByHeaderID retrieves and returns all of the rct cids at the provided header ID that conform to the provided
|
|
|
|
// filter parameters and correspond to the provided tx ids
|
|
|
|
func (ecr *CIDRetriever) RetrieveRctCIDsByHeaderID(tx *sqlx.Tx, rctFilter ReceiptFilter, headerID int64, trxIds []int64) ([]ReceiptModel, error) {
|
|
|
|
log.Debug("retrieving receipt cids for header id ", headerID)
|
|
|
|
args := make([]interface{}, 0, 4)
|
2020-04-04 19:56:11 +00:00
|
|
|
pgStr := `SELECT receipt_cids.id, receipt_cids.tx_id, receipt_cids.cid, receipt_cids.contract,
|
2020-04-03 20:15:06 +00:00
|
|
|
receipt_cids.contract_hash, receipt_cids.topic0s, receipt_cids.topic1s,
|
2020-04-02 03:34:06 +00:00
|
|
|
receipt_cids.topic2s, receipt_cids.topic3s, receipt_cids.log_contracts
|
2020-02-23 23:14:29 +00:00
|
|
|
FROM eth.receipt_cids, eth.transaction_cids, eth.header_cids
|
|
|
|
WHERE receipt_cids.tx_id = transaction_cids.id
|
|
|
|
AND transaction_cids.header_id = header_cids.id
|
|
|
|
AND header_cids.id = $1`
|
|
|
|
id := 2
|
|
|
|
args = append(args, headerID)
|
2020-04-02 03:34:06 +00:00
|
|
|
if len(rctFilter.LogAddresses) > 0 {
|
|
|
|
// Filter on log contract addresses if there are any
|
|
|
|
pgStr += fmt.Sprintf(` AND ((receipt_cids.log_contracts && $%d::VARCHAR(66)[]`, id)
|
|
|
|
args = append(args, pq.Array(rctFilter.LogAddresses))
|
2020-02-23 23:14:29 +00:00
|
|
|
id++
|
|
|
|
// Filter on topics if there are any
|
|
|
|
if hasTopics(rctFilter.Topics) {
|
|
|
|
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 += ")"
|
|
|
|
}
|
|
|
|
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 hasTopics(rctFilter.Topics) {
|
|
|
|
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
|
|
|
|
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 rctFilter.MatchTxs && len(trxIds) > 0 {
|
|
|
|
// 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)
|
|
|
|
args = append(args, pq.Array(trxIds))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pgStr += ` ORDER BY transaction_cids.index`
|
|
|
|
receiptCids := make([]ReceiptModel, 0)
|
|
|
|
return receiptCids, tx.Select(&receiptCids, pgStr, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RetrieveRctCIDs retrieves and returns all of the rct cids at the provided blockheight or block hash that conform to the provided
|
2020-01-16 20:48:38 +00:00
|
|
|
// filter parameters and correspond to the provided tx ids
|
2020-02-03 18:22:29 +00:00
|
|
|
func (ecr *CIDRetriever) RetrieveRctCIDs(tx *sqlx.Tx, rctFilter 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-02-23 23:14:29 +00:00
|
|
|
args := make([]interface{}, 0, 5)
|
2020-04-04 19:56:11 +00:00
|
|
|
pgStr := `SELECT receipt_cids.id, receipt_cids.tx_id, receipt_cids.cid, receipt_cids.contract,
|
2020-04-03 20:15:06 +00:00
|
|
|
receipt_cids.contract_hash, receipt_cids.topic0s, receipt_cids.topic1s,
|
2020-04-02 03:34:06 +00:00
|
|
|
receipt_cids.topic2s, receipt_cids.topic3s, receipt_cids.log_contracts
|
2020-01-31 19:25:15 +00:00
|
|
|
FROM eth.receipt_cids, eth.transaction_cids, eth.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`
|
2020-02-23 23:14:29 +00:00
|
|
|
id := 1
|
2020-01-21 19:12:35 +00:00
|
|
|
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++
|
|
|
|
}
|
2020-04-02 03:34:06 +00:00
|
|
|
if len(rctFilter.LogAddresses) > 0 {
|
|
|
|
// Filter on log contract addresses if there are any
|
|
|
|
pgStr += fmt.Sprintf(` AND ((receipt_cids.log_contracts && $%d::VARCHAR(66)[]`, id)
|
|
|
|
args = append(args, pq.Array(rctFilter.LogAddresses))
|
2020-01-21 19:12:35 +00:00
|
|
|
id++
|
|
|
|
// Filter on topics if there are any
|
2020-02-20 22:13:19 +00:00
|
|
|
if hasTopics(rctFilter.Topics) {
|
2020-01-21 19:12:35 +00:00
|
|
|
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
|
2020-02-20 22:13:19 +00:00
|
|
|
if hasTopics(rctFilter.Topics) {
|
2020-01-21 19:12:35 +00:00
|
|
|
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-02-20 22:13:19 +00:00
|
|
|
pgStr += ` ORDER BY transaction_cids.index`
|
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-02-20 22:13:19 +00:00
|
|
|
func hasTopics(topics [][]string) bool {
|
|
|
|
for _, topicSet := range topics {
|
|
|
|
if len(topicSet) > 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-02-23 23:14:29 +00:00
|
|
|
// RetrieveStateCIDs retrieves and returns all of the state node cids at the provided header ID that conform to the provided filter parameters
|
|
|
|
func (ecr *CIDRetriever) RetrieveStateCIDs(tx *sqlx.Tx, stateFilter StateFilter, headerID int64) ([]StateNodeModel, error) {
|
|
|
|
log.Debug("retrieving state cids for header id ", headerID)
|
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,
|
2020-03-26 00:16:12 +00:00
|
|
|
state_cids.state_leaf_key, state_cids.node_type, state_cids.cid, state_cids.state_path
|
2020-01-31 19:25:15 +00:00
|
|
|
FROM eth.state_cids INNER JOIN eth.header_cids ON (state_cids.header_id = header_cids.id)
|
2020-02-23 23:14:29 +00:00
|
|
|
WHERE header_cids.id = $1`
|
|
|
|
args = append(args, headerID)
|
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
|
|
|
}
|
2020-03-26 00:16:12 +00:00
|
|
|
pgStr += ` AND state_cids.state_leaf_key = ANY($2::VARCHAR(66)[])`
|
2019-05-21 19:27:24 +00:00
|
|
|
args = append(args, pq.Array(keys))
|
|
|
|
}
|
2020-01-16 20:48:38 +00:00
|
|
|
if !stateFilter.IntermediateNodes {
|
2020-03-11 18:41:59 +00:00
|
|
|
pgStr += ` AND state_cids.node_type = 2`
|
2019-06-07 13:42:10 +00:00
|
|
|
}
|
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-02-23 23:14:29 +00:00
|
|
|
// RetrieveStorageCIDs retrieves and returns all of the storage node cids at the provided header id that conform to the provided filter parameters
|
|
|
|
func (ecr *CIDRetriever) RetrieveStorageCIDs(tx *sqlx.Tx, storageFilter StorageFilter, headerID int64) ([]StorageNodeWithStateKeyModel, error) {
|
|
|
|
log.Debug("retrieving storage cids for header id ", headerID)
|
2019-05-21 19:27:24 +00:00
|
|
|
args := make([]interface{}, 0, 3)
|
2020-03-26 00:16:12 +00:00
|
|
|
pgStr := `SELECT storage_cids.id, storage_cids.state_id, storage_cids.storage_leaf_key,
|
|
|
|
storage_cids.node_type, storage_cids.cid, storage_cids.storage_path, state_cids.state_leaf_key
|
2020-03-11 18:41:59 +00:00
|
|
|
FROM eth.storage_cids, eth.state_cids, eth.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
|
2020-02-23 23:14:29 +00:00
|
|
|
AND header_cids.id = $1`
|
|
|
|
args = append(args, headerID)
|
|
|
|
id := 2
|
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
|
|
|
}
|
2020-03-26 00:16:12 +00:00
|
|
|
pgStr += fmt.Sprintf(` AND state_cids.state_leaf_key = ANY($%d::VARCHAR(66)[])`, id)
|
2019-05-21 19:27:24 +00:00
|
|
|
args = append(args, pq.Array(keys))
|
2020-02-09 21:28:30 +00:00
|
|
|
id++
|
|
|
|
}
|
|
|
|
if len(storageFilter.StorageKeys) > 0 {
|
2020-03-26 00:16:12 +00:00
|
|
|
pgStr += fmt.Sprintf(` AND storage_cids.storage_leaf_key = ANY($%d::VARCHAR(66)[])`, id)
|
2020-01-16 20:48:38 +00:00
|
|
|
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 {
|
2020-03-11 18:41:59 +00:00
|
|
|
pgStr += ` AND storage_cids.node_type = 2`
|
2019-06-07 13:42:10 +00:00
|
|
|
}
|
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-04-04 01:36:37 +00:00
|
|
|
// it finds the union of heights where no data exists and where the times_validated is lower than the validation level
|
|
|
|
func (ecr *CIDRetriever) RetrieveGapsInData(validationLevel int) ([]shared.Gap, error) {
|
2020-01-31 19:25:15 +00:00
|
|
|
pgStr := `SELECT header_cids.block_number + 1 AS start, min(fr.block_number) - 1 AS stop FROM eth.header_cids
|
|
|
|
LEFT JOIN eth.header_cids r on eth.header_cids.block_number = r.block_number - 1
|
|
|
|
LEFT JOIN eth.header_cids fr on eth.header_cids.block_number < fr.block_number
|
2019-10-02 14:10:37 +00:00
|
|
|
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)
|
2020-04-04 01:36:37 +00:00
|
|
|
if err := ecr.db.Select(&results, pgStr); err != nil {
|
2019-10-02 14:10:37 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-04-04 01:36:37 +00:00
|
|
|
emptyGaps := make([]shared.Gap, len(results))
|
2020-01-17 23:16:01 +00:00
|
|
|
for i, res := range results {
|
2020-04-04 01:36:37 +00:00
|
|
|
emptyGaps[i] = shared.Gap{
|
2020-01-17 23:16:01 +00:00
|
|
|
Start: res.Start,
|
|
|
|
Stop: res.Stop,
|
|
|
|
}
|
2019-10-02 14:10:37 +00:00
|
|
|
}
|
2020-04-04 01:36:37 +00:00
|
|
|
|
|
|
|
// Find sections of blocks where we are below the validation level
|
|
|
|
// There will be no overlap between these "gaps" and the ones above
|
|
|
|
pgStr = `SELECT block_number FROM eth.header_cids
|
|
|
|
WHERE times_validated < $1
|
|
|
|
ORDER BY block_number`
|
|
|
|
var heights []uint64
|
|
|
|
if err := ecr.db.Select(&heights, pgStr, validationLevel); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(heights) == 0 {
|
|
|
|
return emptyGaps, nil
|
|
|
|
}
|
2020-04-20 13:14:02 +00:00
|
|
|
return append(emptyGaps, utils.MissingHeightsToGaps(heights)...), nil
|
2019-10-02 14:10:37 +00:00
|
|
|
}
|
2020-01-16 20:48:38 +00:00
|
|
|
|
2020-01-26 19:55:26 +00:00
|
|
|
// RetrieveBlockByHash returns all of the CIDs needed to compose an entire block, for a given block hash
|
|
|
|
func (ecr *CIDRetriever) RetrieveBlockByHash(blockHash common.Hash) (HeaderModel, []UncleModel, []TxModel, []ReceiptModel, error) {
|
|
|
|
log.Debug("retrieving block cids for block hash ", blockHash.String())
|
|
|
|
tx, err := ecr.db.Beginx()
|
|
|
|
if err != nil {
|
|
|
|
return HeaderModel{}, nil, nil, nil, err
|
|
|
|
}
|
|
|
|
headerCID, err := ecr.RetrieveHeaderCIDByHash(tx, blockHash)
|
|
|
|
if err != nil {
|
|
|
|
if err := tx.Rollback(); err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
log.Error("header cid retrieval error")
|
|
|
|
return HeaderModel{}, nil, nil, nil, err
|
|
|
|
}
|
|
|
|
uncleCIDs, err := ecr.RetrieveUncleCIDsByHeaderID(tx, headerCID.ID)
|
|
|
|
if err != nil {
|
|
|
|
if err := tx.Rollback(); err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
log.Error("uncle cid retrieval error")
|
|
|
|
return HeaderModel{}, nil, nil, nil, err
|
|
|
|
}
|
|
|
|
txCIDs, err := ecr.RetrieveTxCIDsByHeaderID(tx, headerCID.ID)
|
|
|
|
if err != nil {
|
|
|
|
if err := tx.Rollback(); err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
log.Error("tx cid retrieval error")
|
|
|
|
return HeaderModel{}, nil, nil, nil, err
|
|
|
|
}
|
|
|
|
txIDs := make([]int64, len(txCIDs))
|
|
|
|
for i, txCID := range txCIDs {
|
|
|
|
txIDs[i] = txCID.ID
|
|
|
|
}
|
|
|
|
rctCIDs, err := ecr.RetrieveReceiptCIDsByTxIDs(tx, txIDs)
|
|
|
|
if err != nil {
|
|
|
|
if err := tx.Rollback(); err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
log.Error("rct cid retrieval error")
|
|
|
|
return HeaderModel{}, nil, nil, nil, err
|
|
|
|
}
|
|
|
|
return headerCID, uncleCIDs, txCIDs, rctCIDs, tx.Commit()
|
|
|
|
}
|
|
|
|
|
|
|
|
// RetrieveBlockByNumber returns all of the CIDs needed to compose an entire block, for a given block number
|
|
|
|
func (ecr *CIDRetriever) RetrieveBlockByNumber(blockNumber int64) (HeaderModel, []UncleModel, []TxModel, []ReceiptModel, error) {
|
|
|
|
log.Debug("retrieving block cids for block number ", blockNumber)
|
|
|
|
tx, err := ecr.db.Beginx()
|
|
|
|
if err != nil {
|
|
|
|
return HeaderModel{}, nil, nil, nil, err
|
|
|
|
}
|
|
|
|
headerCID, err := ecr.RetrieveHeaderCIDs(tx, blockNumber)
|
|
|
|
if err != nil {
|
|
|
|
if err := tx.Rollback(); err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
log.Error("header cid retrieval error")
|
|
|
|
return HeaderModel{}, nil, nil, nil, err
|
|
|
|
}
|
|
|
|
if len(headerCID) < 1 {
|
|
|
|
return HeaderModel{}, nil, nil, nil, fmt.Errorf("header cid retrieval error, no header CIDs found at block %d", blockNumber)
|
|
|
|
}
|
|
|
|
uncleCIDs, err := ecr.RetrieveUncleCIDsByHeaderID(tx, headerCID[0].ID)
|
|
|
|
if err != nil {
|
|
|
|
if err := tx.Rollback(); err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
log.Error("uncle cid retrieval error")
|
|
|
|
return HeaderModel{}, nil, nil, nil, err
|
|
|
|
}
|
|
|
|
txCIDs, err := ecr.RetrieveTxCIDsByHeaderID(tx, headerCID[0].ID)
|
|
|
|
if err != nil {
|
|
|
|
if err := tx.Rollback(); err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
log.Error("tx cid retrieval error")
|
|
|
|
return HeaderModel{}, nil, nil, nil, err
|
|
|
|
}
|
|
|
|
txIDs := make([]int64, len(txCIDs))
|
|
|
|
for i, txCID := range txCIDs {
|
|
|
|
txIDs[i] = txCID.ID
|
|
|
|
}
|
|
|
|
rctCIDs, err := ecr.RetrieveReceiptCIDsByTxIDs(tx, txIDs)
|
|
|
|
if err != nil {
|
|
|
|
if err := tx.Rollback(); err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
log.Error("rct cid retrieval error")
|
|
|
|
return HeaderModel{}, nil, nil, nil, err
|
|
|
|
}
|
|
|
|
return headerCID[0], uncleCIDs, txCIDs, rctCIDs, tx.Commit()
|
|
|
|
}
|
|
|
|
|
|
|
|
// RetrieveHeaderCIDByHash returns the header for the given block hash
|
|
|
|
func (ecr *CIDRetriever) RetrieveHeaderCIDByHash(tx *sqlx.Tx, blockHash common.Hash) (HeaderModel, error) {
|
|
|
|
log.Debug("retrieving header cids for block hash ", blockHash.String())
|
2020-01-31 19:25:15 +00:00
|
|
|
pgStr := `SELECT * FROM eth.header_cids
|
2020-01-26 19:55:26 +00:00
|
|
|
WHERE block_hash = $1`
|
|
|
|
var headerCID HeaderModel
|
|
|
|
return headerCID, tx.Get(&headerCID, pgStr, blockHash.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// RetrieveTxCIDsByHeaderID retrieves all tx CIDs for the given header id
|
|
|
|
func (ecr *CIDRetriever) RetrieveTxCIDsByHeaderID(tx *sqlx.Tx, headerID int64) ([]TxModel, error) {
|
|
|
|
log.Debug("retrieving tx cids for block id ", headerID)
|
2020-01-31 19:25:15 +00:00
|
|
|
pgStr := `SELECT * FROM eth.transaction_cids
|
2020-02-20 22:13:19 +00:00
|
|
|
WHERE header_id = $1
|
|
|
|
ORDER BY index`
|
2020-01-26 19:55:26 +00:00
|
|
|
var txCIDs []TxModel
|
|
|
|
return txCIDs, tx.Select(&txCIDs, pgStr, headerID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RetrieveReceiptCIDsByTxIDs retrieves receipt CIDs by their associated tx IDs
|
|
|
|
func (ecr *CIDRetriever) RetrieveReceiptCIDsByTxIDs(tx *sqlx.Tx, txIDs []int64) ([]ReceiptModel, error) {
|
|
|
|
log.Debugf("retrieving receipt cids for tx ids %v", txIDs)
|
2020-04-04 19:56:11 +00:00
|
|
|
pgStr := `SELECT receipt_cids.id, receipt_cids.tx_id, receipt_cids.cid, receipt_cids.contract,
|
2020-04-03 20:15:06 +00:00
|
|
|
receipt_cids.contract_hash, receipt_cids.topic0s, receipt_cids.topic1s,
|
2020-04-02 03:34:06 +00:00
|
|
|
receipt_cids.topic2s, receipt_cids.topic3s, receipt_cids.log_contracts
|
2020-02-20 22:13:19 +00:00
|
|
|
FROM eth.receipt_cids, eth.transaction_cids
|
|
|
|
WHERE tx_id = ANY($1::INTEGER[])
|
|
|
|
AND receipt_cids.tx_id = transaction_cids.id
|
|
|
|
ORDER BY transaction_cids.index`
|
2020-01-26 19:55:26 +00:00
|
|
|
var rctCIDs []ReceiptModel
|
|
|
|
return rctCIDs, tx.Select(&rctCIDs, pgStr, pq.Array(txIDs))
|
2020-01-16 20:48:38 +00:00
|
|
|
}
|