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"
|
|
|
|
|
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-01-31 18:03:37 +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-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-26 19:55:26 +00:00
|
|
|
for _, headerCID := range cw.Headers {
|
|
|
|
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 nil, true, err
|
2019-08-28 18:41:49 +00:00
|
|
|
}
|
2020-01-26 19:55:26 +00:00
|
|
|
cw.Uncles = append(cw.Uncles, uncleCIDs...)
|
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 {
|
2020-01-26 19:55:26 +00:00
|
|
|
cw.Transactions, err = ecr.RetrieveTxCIDs(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)
|
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-03 18:22:29 +00:00
|
|
|
func (ecr *CIDRetriever) RetrieveTxCIDs(tx *sqlx.Tx, txFilter 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)
|
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-09 21:28:30 +00:00
|
|
|
WHERE header_cids.block_number = $%d`, id)
|
2019-05-21 19:27:24 +00:00
|
|
|
args = append(args, blockNumber)
|
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-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-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-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-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`
|
|
|
|
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
|
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-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-02-03 18:22:29 +00:00
|
|
|
func (ecr *CIDRetriever) RetrieveStateCIDs(tx *sqlx.Tx, stateFilter 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
|
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)
|
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-02-03 18:22:29 +00:00
|
|
|
func (ecr *CIDRetriever) RetrieveStorageCIDs(tx *sqlx.Tx, storageFilter 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-02-09 21:28:30 +00:00
|
|
|
id := 1
|
|
|
|
pgStr := fmt.Sprintf(`SELECT storage_cids.id, storage_cids.state_id, storage_cids.storage_key,
|
2020-01-31 19:25:15 +00:00
|
|
|
storage_cids.leaf, storage_cids.cid, state_cids.state_key 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-09 21:28:30 +00:00
|
|
|
AND header_cids.block_number = $%d`, id)
|
2019-05-21 19:27:24 +00:00
|
|
|
args = append(args, blockNumber)
|
2020-02-09 21:28:30 +00:00
|
|
|
id++
|
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-02-09 21:28:30 +00:00
|
|
|
pgStr += fmt.Sprintf(` AND state_cids.state_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 {
|
|
|
|
pgStr += fmt.Sprintf(` AND storage_cids.storage_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 {
|
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) {
|
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)
|
|
|
|
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-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-02-20 22:13:19 +00:00
|
|
|
pgStr := `SELECT receipt_cids.id, receipt_cids.tx_id, receipt_cids.cid,
|
|
|
|
receipt_cids.contract, receipt_cids.topic0s, receipt_cids.topic1s,
|
|
|
|
receipt_cids.topic2s, receipt_cids.topic3s
|
|
|
|
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
|
|
|
}
|