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"
|
2023-03-02 23:59:13 +00:00
|
|
|
"strconv"
|
2019-06-18 17:28:57 +00:00
|
|
|
|
2023-01-21 01:39:26 +00:00
|
|
|
"github.com/cerc-io/ipld-eth-server/v4/pkg/log"
|
2020-01-21 19:12:35 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2021-08-12 06:23:41 +00:00
|
|
|
"github.com/ethereum/go-ethereum/statediff/indexer/models"
|
2019-05-21 19:27:24 +00:00
|
|
|
"github.com/jmoiron/sqlx"
|
|
|
|
"github.com/lib/pq"
|
2022-06-03 09:00:20 +00:00
|
|
|
"gorm.io/driver/postgres"
|
|
|
|
"gorm.io/gorm"
|
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 {
|
2022-06-03 09:00:20 +00:00
|
|
|
db *sqlx.DB
|
|
|
|
gormDB *gorm.DB
|
|
|
|
}
|
|
|
|
|
2022-06-06 10:06:29 +00:00
|
|
|
type IPLDModelRecord struct {
|
2022-06-03 09:00:20 +00:00
|
|
|
models.IPLDModel
|
|
|
|
}
|
|
|
|
|
|
|
|
// TableName overrides the table name used by IPLD
|
2022-06-06 10:06:29 +00:00
|
|
|
func (IPLDModelRecord) TableName() string {
|
2023-03-02 23:41:44 +00:00
|
|
|
return "ipld.blocks"
|
2022-06-03 09:00:20 +00:00
|
|
|
}
|
|
|
|
|
2022-06-06 11:16:53 +00:00
|
|
|
type HeaderCIDRecord struct {
|
2022-06-03 09:00:20 +00:00
|
|
|
CID string `gorm:"column:cid"`
|
|
|
|
BlockHash string `gorm:"primaryKey"`
|
2022-06-07 12:07:01 +00:00
|
|
|
BlockNumber string `gorm:"primaryKey"`
|
2022-06-03 09:00:20 +00:00
|
|
|
ParentHash string
|
|
|
|
Timestamp uint64
|
|
|
|
StateRoot string
|
|
|
|
TotalDifficulty string `gorm:"column:td"`
|
|
|
|
TxRoot string
|
|
|
|
RctRoot string `gorm:"column:receipt_root"`
|
|
|
|
UncleRoot string
|
|
|
|
Bloom []byte
|
|
|
|
MhKey string
|
2022-06-06 10:06:29 +00:00
|
|
|
|
|
|
|
// gorm doesn't check if foreign key exists in database.
|
|
|
|
// It is required to eager load relations using preload.
|
2022-06-07 12:07:01 +00:00
|
|
|
TransactionCIDs []TransactionCIDRecord `gorm:"foreignKey:HeaderID,BlockNumber;references:BlockHash,BlockNumber"`
|
|
|
|
IPLD IPLDModelRecord `gorm:"foreignKey:MhKey,BlockNumber;references:Key,BlockNumber"`
|
2022-06-03 09:00:20 +00:00
|
|
|
}
|
|
|
|
|
2022-06-06 11:16:53 +00:00
|
|
|
// TableName overrides the table name used by HeaderCIDRecord
|
|
|
|
func (HeaderCIDRecord) TableName() string {
|
2022-06-03 09:00:20 +00:00
|
|
|
return "eth.header_cids"
|
|
|
|
}
|
|
|
|
|
2022-06-06 11:16:53 +00:00
|
|
|
type TransactionCIDRecord struct {
|
2022-06-07 12:07:01 +00:00
|
|
|
CID string `gorm:"column:cid"`
|
|
|
|
TxHash string `gorm:"primaryKey"`
|
|
|
|
BlockNumber string `gorm:"primaryKey"`
|
|
|
|
HeaderID string `gorm:"column:header_id"`
|
|
|
|
Index int64
|
|
|
|
Src string
|
|
|
|
Dst string
|
|
|
|
MhKey string
|
|
|
|
IPLD IPLDModelRecord `gorm:"foreignKey:MhKey,BlockNumber;references:Key,BlockNumber"`
|
2022-06-03 09:00:20 +00:00
|
|
|
}
|
|
|
|
|
2022-06-06 11:16:53 +00:00
|
|
|
// TableName overrides the table name used by TransactionCIDRecord
|
|
|
|
func (TransactionCIDRecord) TableName() string {
|
2022-06-03 09:00:20 +00:00
|
|
|
return "eth.transaction_cids"
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
|
|
|
|
2020-01-17 23:16:01 +00:00
|
|
|
// NewCIDRetriever returns a pointer to a new CIDRetriever which supports the CIDRetriever interface
|
2022-03-10 09:53:03 +00:00
|
|
|
func NewCIDRetriever(db *sqlx.DB) *CIDRetriever {
|
2022-06-03 09:00:20 +00:00
|
|
|
gormDB, err := gorm.Open(postgres.New(postgres.Config{
|
|
|
|
Conn: db,
|
|
|
|
}), &gorm.Config{})
|
2022-06-06 10:06:29 +00:00
|
|
|
|
2022-06-03 09:00:20 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-17 23:16:01 +00:00
|
|
|
return &CIDRetriever{
|
2022-06-03 09:00:20 +00:00
|
|
|
db: db,
|
|
|
|
gormDB: gormDB,
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
2022-04-26 14:10:06 +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
|
|
|
|
2021-08-31 11:32:01 +00:00
|
|
|
func topicFilterCondition(id *int, topics [][]string, args []interface{}, pgStr string, first bool) (string, []interface{}) {
|
2021-08-14 13:50:22 +00:00
|
|
|
for i, topicSet := range topics {
|
|
|
|
if len(topicSet) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if !first {
|
|
|
|
pgStr += " AND"
|
|
|
|
} else {
|
|
|
|
first = false
|
|
|
|
}
|
2021-08-31 11:32:01 +00:00
|
|
|
pgStr += fmt.Sprintf(` eth.log_cids.topic%d = ANY ($%d)`, i, *id)
|
2021-08-14 13:50:22 +00:00
|
|
|
args = append(args, pq.Array(topicSet))
|
2021-08-31 11:32:01 +00:00
|
|
|
*id++
|
2021-08-14 13:50:22 +00:00
|
|
|
}
|
2021-08-31 11:32:01 +00:00
|
|
|
return pgStr, args
|
2021-08-14 13:50:22 +00:00
|
|
|
}
|
|
|
|
|
2021-08-31 11:32:01 +00:00
|
|
|
func logFilterCondition(id *int, pgStr string, args []interface{}, rctFilter ReceiptFilter) (string, []interface{}) {
|
2021-08-20 07:37:11 +00:00
|
|
|
if len(rctFilter.LogAddresses) > 0 {
|
2021-08-31 11:32:01 +00:00
|
|
|
pgStr += fmt.Sprintf(` AND eth.log_cids.address = ANY ($%d)`, *id)
|
2021-08-20 07:37:11 +00:00
|
|
|
args = append(args, pq.Array(rctFilter.LogAddresses))
|
2021-08-31 11:32:01 +00:00
|
|
|
*id++
|
|
|
|
}
|
2021-08-20 07:37:11 +00:00
|
|
|
|
2021-08-31 11:32:01 +00:00
|
|
|
// Filter on topics if there are any
|
|
|
|
if hasTopics(rctFilter.Topics) {
|
|
|
|
pgStr, args = topicFilterCondition(id, rctFilter.Topics, args, pgStr, false)
|
2021-08-20 07:37:11 +00:00
|
|
|
}
|
|
|
|
|
2021-08-31 11:32:01 +00:00
|
|
|
return pgStr, args
|
2021-08-20 07:37:11 +00:00
|
|
|
}
|
|
|
|
|
2022-03-10 12:35:19 +00:00
|
|
|
func receiptFilterConditions(id *int, pgStr string, args []interface{}, rctFilter ReceiptFilter, txHashes []string) (string, []interface{}) {
|
|
|
|
rctCond := " AND (receipt_cids.tx_id = ANY ( "
|
|
|
|
logQuery := "SELECT rct_id FROM eth.log_cids WHERE"
|
2020-04-02 03:34:06 +00:00
|
|
|
if len(rctFilter.LogAddresses) > 0 {
|
|
|
|
// Filter on log contract addresses if there are any
|
2021-08-31 11:32:01 +00:00
|
|
|
pgStr += fmt.Sprintf(`%s %s eth.log_cids.address = ANY ($%d)`, rctCond, logQuery, *id)
|
2020-04-02 03:34:06 +00:00
|
|
|
args = append(args, pq.Array(rctFilter.LogAddresses))
|
2021-08-31 11:32:01 +00:00
|
|
|
*id++
|
2021-08-14 13:50:22 +00:00
|
|
|
|
2020-02-23 23:14:29 +00:00
|
|
|
// Filter on topics if there are any
|
|
|
|
if hasTopics(rctFilter.Topics) {
|
2021-08-31 11:32:01 +00:00
|
|
|
pgStr, args = topicFilterCondition(id, rctFilter.Topics, args, pgStr, false)
|
2020-02-23 23:14:29 +00:00
|
|
|
}
|
2021-08-14 13:50:22 +00:00
|
|
|
|
2020-02-23 23:14:29 +00:00
|
|
|
pgStr += ")"
|
2021-08-14 13:50:22 +00:00
|
|
|
|
2022-03-10 12:35:19 +00:00
|
|
|
// Filter on txHashes if there are any, and we are matching txs
|
|
|
|
if rctFilter.MatchTxs && len(txHashes) > 0 {
|
|
|
|
pgStr += fmt.Sprintf(` OR receipt_cids.tx_id = ANY($%d)`, *id)
|
|
|
|
args = append(args, pq.Array(txHashes))
|
2020-02-23 23:14:29 +00:00
|
|
|
}
|
|
|
|
pgStr += ")"
|
|
|
|
} else { // If there are no contract addresses to filter on
|
|
|
|
// Filter on topics if there are any
|
|
|
|
if hasTopics(rctFilter.Topics) {
|
2021-08-14 13:50:22 +00:00
|
|
|
pgStr += rctCond + logQuery
|
2021-08-31 11:32:01 +00:00
|
|
|
pgStr, args = topicFilterCondition(id, rctFilter.Topics, args, pgStr, true)
|
2020-02-23 23:14:29 +00:00
|
|
|
pgStr += ")"
|
2022-03-10 12:35:19 +00:00
|
|
|
// Filter on txHashes if there are any, and we are matching txs
|
|
|
|
if rctFilter.MatchTxs && len(txHashes) > 0 {
|
|
|
|
pgStr += fmt.Sprintf(` OR receipt_cids.tx_id = ANY($%d)`, *id)
|
|
|
|
args = append(args, pq.Array(txHashes))
|
2020-02-23 23:14:29 +00:00
|
|
|
}
|
|
|
|
pgStr += ")"
|
2022-03-10 12:35:19 +00:00
|
|
|
} else if rctFilter.MatchTxs && len(txHashes) > 0 {
|
2020-02-23 23:14:29 +00:00
|
|
|
// If there are no contract addresses or topics to filter on,
|
2022-03-10 12:35:19 +00:00
|
|
|
// Filter on txHashes if there are any, and we are matching txs
|
|
|
|
pgStr += fmt.Sprintf(` AND receipt_cids.tx_id = ANY($%d)`, *id)
|
|
|
|
args = append(args, pq.Array(txHashes))
|
2020-02-23 23:14:29 +00:00
|
|
|
}
|
|
|
|
}
|
2021-08-14 13:50:22 +00:00
|
|
|
|
2021-08-31 11:32:01 +00:00
|
|
|
return pgStr, args
|
2021-08-14 13:50:22 +00:00
|
|
|
}
|
|
|
|
|
2022-04-26 14:10:06 +00:00
|
|
|
// RetrieveFilteredGQLLogs retrieves and returns all the log CIDs provided blockHash that conform to the provided
|
2021-08-20 07:37:11 +00:00
|
|
|
// filter parameters.
|
2022-11-04 04:32:09 +00:00
|
|
|
func (ecr *CIDRetriever) RetrieveFilteredGQLLogs(tx *sqlx.Tx, rctFilter ReceiptFilter, blockHash *common.Hash, blockNumber *big.Int) ([]LogResult, error) {
|
2022-07-15 10:49:31 +00:00
|
|
|
log.Debug("retrieving log cids for receipt ids with block hash", blockHash.String())
|
2021-08-20 07:37:11 +00:00
|
|
|
args := make([]interface{}, 0, 4)
|
|
|
|
id := 1
|
2022-10-05 09:44:17 +00:00
|
|
|
pgStr := `SELECT CAST(eth.log_cids.block_number as Text), eth.log_cids.header_id as block_hash,
|
2023-03-02 23:59:13 +00:00
|
|
|
eth.log_cids.cid, eth.log_cids.index, eth.log_cids.rct_id, eth.log_cids.address,
|
2022-10-05 09:44:17 +00:00
|
|
|
eth.log_cids.topic0, eth.log_cids.topic1, eth.log_cids.topic2, eth.log_cids.topic3, eth.log_cids.log_data,
|
2023-03-02 23:59:13 +00:00
|
|
|
data, eth.receipt_cids.cid, eth.receipt_cids.post_status, eth.receipt_cids.tx_id AS tx_hash
|
2023-03-02 23:41:44 +00:00
|
|
|
FROM eth.log_cids, eth.receipt_cids, ipld.blocks
|
2022-03-10 12:35:19 +00:00
|
|
|
WHERE eth.log_cids.rct_id = receipt_cids.tx_id
|
2022-10-05 09:44:17 +00:00
|
|
|
AND eth.log_cids.header_id = receipt_cids.header_id
|
|
|
|
AND eth.log_cids.block_number = receipt_cids.block_number
|
2023-03-02 23:59:13 +00:00
|
|
|
AND log_cids.cid = blocks.key
|
2022-04-26 14:10:06 +00:00
|
|
|
AND log_cids.block_number = blocks.block_number
|
2022-10-05 09:44:17 +00:00
|
|
|
AND receipt_cids.header_id = $1`
|
2021-08-20 07:37:11 +00:00
|
|
|
|
|
|
|
args = append(args, blockHash.String())
|
|
|
|
id++
|
|
|
|
|
2022-11-04 04:32:09 +00:00
|
|
|
if blockNumber != nil {
|
|
|
|
pgStr += ` AND receipt_cids.block_number = $2`
|
|
|
|
id++
|
|
|
|
args = append(args, blockNumber.Int64())
|
|
|
|
}
|
|
|
|
|
2021-08-31 11:32:01 +00:00
|
|
|
pgStr, args = logFilterCondition(&id, pgStr, args, rctFilter)
|
2021-08-20 07:37:11 +00:00
|
|
|
pgStr += ` ORDER BY log_cids.index`
|
|
|
|
|
2021-09-01 07:02:28 +00:00
|
|
|
logCIDs := make([]LogResult, 0)
|
2021-08-20 07:37:11 +00:00
|
|
|
err := tx.Select(&logCIDs, pgStr, args...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return logCIDs, nil
|
|
|
|
}
|
|
|
|
|
2022-07-12 08:10:45 +00:00
|
|
|
// RetrieveFilteredLog retrieves and returns all the log CIDs provided blockHeight or blockHash that conform to the provided
|
2021-08-20 07:37:11 +00:00
|
|
|
// filter parameters.
|
2021-09-01 07:02:28 +00:00
|
|
|
func (ecr *CIDRetriever) RetrieveFilteredLog(tx *sqlx.Tx, rctFilter ReceiptFilter, blockNumber int64, blockHash *common.Hash) ([]LogResult, error) {
|
2021-08-20 07:37:11 +00:00
|
|
|
log.Debug("retrieving log cids for receipt ids")
|
|
|
|
args := make([]interface{}, 0, 4)
|
2023-03-02 23:59:13 +00:00
|
|
|
pgStr := `SELECT CAST(eth.log_cids.block_number as Text), eth.log_cids.cid, eth.log_cids.index, eth.log_cids.rct_id,
|
2022-07-12 08:10:45 +00:00
|
|
|
eth.log_cids.address, eth.log_cids.topic0, eth.log_cids.topic1, eth.log_cids.topic2, eth.log_cids.topic3,
|
|
|
|
eth.log_cids.log_data, eth.transaction_cids.tx_hash, eth.transaction_cids.index as txn_index,
|
2023-03-02 23:59:13 +00:00
|
|
|
eth.receipt_cids.cid as cid, eth.receipt_cids.post_status, header_cids.block_hash
|
2022-03-10 12:35:19 +00:00
|
|
|
FROM eth.log_cids, eth.receipt_cids, eth.transaction_cids, eth.header_cids
|
|
|
|
WHERE eth.log_cids.rct_id = receipt_cids.tx_id
|
2022-07-12 08:10:45 +00:00
|
|
|
AND eth.log_cids.header_id = eth.receipt_cids.header_id
|
2022-04-26 14:10:06 +00:00
|
|
|
AND eth.log_cids.block_number = eth.receipt_cids.block_number
|
2022-03-10 12:35:19 +00:00
|
|
|
AND receipt_cids.tx_id = transaction_cids.tx_hash
|
2022-07-12 08:10:45 +00:00
|
|
|
AND receipt_cids.header_id = transaction_cids.header_id
|
2022-04-26 14:10:06 +00:00
|
|
|
AND receipt_cids.block_number = transaction_cids.block_number
|
|
|
|
AND transaction_cids.header_id = header_cids.block_hash
|
|
|
|
AND transaction_cids.block_number = header_cids.block_number`
|
2021-08-20 07:37:11 +00:00
|
|
|
id := 1
|
|
|
|
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++
|
|
|
|
}
|
|
|
|
|
2021-08-31 11:32:01 +00:00
|
|
|
pgStr, args = logFilterCondition(&id, pgStr, args, rctFilter)
|
2021-08-20 07:37:11 +00:00
|
|
|
pgStr += ` ORDER BY log_cids.index`
|
|
|
|
|
2021-09-01 07:02:28 +00:00
|
|
|
logCIDs := make([]LogResult, 0)
|
2021-08-20 07:37:11 +00:00
|
|
|
err := tx.Select(&logCIDs, pgStr, args...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return logCIDs, nil
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-03-02 23:59:13 +00:00
|
|
|
// RetrieveBlockNumberByHash returns the block number for the given block hash
|
|
|
|
func (ecr *CIDRetriever) RetrieveBlockNumberByHash(tx *sqlx.Tx, blockHash common.Hash) (uint64, error) {
|
|
|
|
log.Debug("retrieving block number for block hash ", blockHash.String())
|
|
|
|
pgStr := `SELECT CAST(block_number as TEXT) FROM eth.header_cids WHERE block_hash = $1`
|
|
|
|
var blockNumberStr string
|
|
|
|
if err := tx.Get(&blockNumberStr, pgStr, blockHash.String()); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return strconv.ParseUint(blockNumberStr, 10, 64)
|
2020-01-26 19:55:26 +00:00
|
|
|
}
|
|
|
|
|
2022-05-30 06:31:27 +00:00
|
|
|
// RetrieveHeaderAndTxCIDsByBlockNumber retrieves header CIDs and their associated tx CIDs by block number
|
2022-06-06 11:16:53 +00:00
|
|
|
func (ecr *CIDRetriever) RetrieveHeaderAndTxCIDsByBlockNumber(blockNumber int64) ([]HeaderCIDRecord, error) {
|
2022-05-25 13:13:53 +00:00
|
|
|
log.Debug("retrieving header cids and tx cids for block number ", blockNumber)
|
|
|
|
|
2022-06-06 11:16:53 +00:00
|
|
|
var headerCIDs []HeaderCIDRecord
|
2022-05-25 13:13:53 +00:00
|
|
|
|
2022-06-03 09:00:20 +00:00
|
|
|
// https://github.com/go-gorm/gorm/issues/4083#issuecomment-778883283
|
2022-06-06 11:16:53 +00:00
|
|
|
// Will use join for TransactionCIDs once preload for 1:N is supported.
|
|
|
|
err := ecr.gormDB.Preload("TransactionCIDs", func(tx *gorm.DB) *gorm.DB {
|
2022-06-07 12:07:01 +00:00
|
|
|
return tx.Select("cid", "tx_hash", "index", "src", "dst", "header_id", "block_number")
|
|
|
|
}).Joins("IPLD").Find(&headerCIDs, "header_cids.block_number = ?", blockNumber).Error
|
2022-06-06 10:06:29 +00:00
|
|
|
|
2022-05-25 13:13:53 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("header cid retrieval error")
|
2022-06-03 09:00:20 +00:00
|
|
|
return nil, err
|
2022-05-25 13:13:53 +00:00
|
|
|
}
|
|
|
|
|
2022-06-03 09:00:20 +00:00
|
|
|
return headerCIDs, nil
|
2022-05-25 13:13:53 +00:00
|
|
|
}
|
|
|
|
|
2022-11-04 04:32:09 +00:00
|
|
|
// RetrieveHeaderAndTxCIDsByBlockHash retrieves header CID and their associated tx CIDs by block hash (and optionally block number)
|
|
|
|
func (ecr *CIDRetriever) RetrieveHeaderAndTxCIDsByBlockHash(blockHash common.Hash, blockNumber *big.Int) (HeaderCIDRecord, error) {
|
2022-05-25 13:13:53 +00:00
|
|
|
log.Debug("retrieving header cid and tx cids for block hash ", blockHash.String())
|
|
|
|
|
2022-07-12 08:10:45 +00:00
|
|
|
var headerCIDs []HeaderCIDRecord
|
2022-05-25 13:13:53 +00:00
|
|
|
|
2022-11-04 04:32:09 +00:00
|
|
|
conditions := map[string]interface{}{"block_hash": blockHash.String()}
|
|
|
|
if blockNumber != nil {
|
|
|
|
conditions["header_cids.block_number"] = blockNumber.Int64()
|
|
|
|
}
|
|
|
|
|
2022-06-03 09:00:20 +00:00
|
|
|
// https://github.com/go-gorm/gorm/issues/4083#issuecomment-778883283
|
2022-06-06 11:16:53 +00:00
|
|
|
// Will use join for TransactionCIDs once preload for 1:N is supported.
|
|
|
|
err := ecr.gormDB.Preload("TransactionCIDs", func(tx *gorm.DB) *gorm.DB {
|
2022-06-07 12:07:01 +00:00
|
|
|
return tx.Select("cid", "tx_hash", "index", "src", "dst", "header_id", "block_number")
|
2022-11-04 04:32:09 +00:00
|
|
|
}).Joins("IPLD").Find(&headerCIDs, conditions).Error
|
2022-07-15 10:49:31 +00:00
|
|
|
|
2022-05-25 13:13:53 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("header cid retrieval error")
|
2022-07-12 08:10:45 +00:00
|
|
|
return HeaderCIDRecord{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(headerCIDs) == 0 {
|
|
|
|
return HeaderCIDRecord{}, errHeaderHashNotFound
|
|
|
|
} else if len(headerCIDs) > 1 {
|
|
|
|
return HeaderCIDRecord{}, errMultipleHeadersForHash
|
2022-05-25 13:13:53 +00:00
|
|
|
}
|
|
|
|
|
2022-07-12 08:10:45 +00:00
|
|
|
return headerCIDs[0], nil
|
2022-05-25 13:13:53 +00:00
|
|
|
}
|
2022-05-30 06:31:27 +00:00
|
|
|
|
2022-11-04 04:32:09 +00:00
|
|
|
// RetrieveTxCIDByHash returns the tx for the given tx hash (and optionally block number)
|
|
|
|
func (ecr *CIDRetriever) RetrieveTxCIDByHash(txHash string, blockNumber *big.Int) (TransactionCIDRecord, error) {
|
2022-05-30 06:31:27 +00:00
|
|
|
log.Debug("retrieving tx cid for tx hash ", txHash)
|
|
|
|
|
2022-07-12 08:10:45 +00:00
|
|
|
var txCIDs []TransactionCIDRecord
|
2022-06-03 09:00:20 +00:00
|
|
|
|
2022-11-04 04:32:09 +00:00
|
|
|
var err error
|
|
|
|
if blockNumber != nil {
|
|
|
|
err = ecr.gormDB.Joins("IPLD").Find(&txCIDs, "tx_hash = ? AND transaction_cids.header_id = (SELECT canonical_header_hash(transaction_cids.block_number)) AND transaction_cids.block_number = ?", txHash, blockNumber.Int64()).Error
|
|
|
|
} else {
|
|
|
|
err = ecr.gormDB.Joins("IPLD").Find(&txCIDs, "tx_hash = ? AND transaction_cids.header_id = (SELECT canonical_header_hash(transaction_cids.block_number))", txHash).Error
|
|
|
|
}
|
2022-05-30 06:31:27 +00:00
|
|
|
if err != nil {
|
2022-11-04 04:32:09 +00:00
|
|
|
log.Error("tx retrieval error")
|
2022-07-12 08:10:45 +00:00
|
|
|
return TransactionCIDRecord{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(txCIDs) == 0 {
|
|
|
|
return TransactionCIDRecord{}, errTxHashNotFound
|
|
|
|
} else if len(txCIDs) > 1 {
|
|
|
|
// a transaction can be part of a only one canonical block
|
|
|
|
return TransactionCIDRecord{}, errTxHashInMultipleBlocks
|
2022-05-30 06:31:27 +00:00
|
|
|
}
|
|
|
|
|
2022-07-12 08:10:45 +00:00
|
|
|
return txCIDs[0], nil
|
2022-05-30 06:31:27 +00:00
|
|
|
}
|