Remove block_number from queries for v3 schema

This commit is contained in:
nabarun 2022-05-30 19:41:03 +05:30
parent 7f15befdee
commit 80413211c2
3 changed files with 31 additions and 16 deletions

View File

@ -577,7 +577,8 @@ func (ecr *CIDRetriever) RetrieveBlockByNumber(blockNumber int64) (models.Header
// RetrieveHeaderCIDByHash returns the header for the given block hash // RetrieveHeaderCIDByHash returns the header for the given block hash
func (ecr *CIDRetriever) RetrieveHeaderCIDByHash(tx *sqlx.Tx, blockHash common.Hash) (models.HeaderModel, error) { func (ecr *CIDRetriever) RetrieveHeaderCIDByHash(tx *sqlx.Tx, blockHash common.Hash) (models.HeaderModel, error) {
log.Debug("retrieving header cids for block hash ", blockHash.String()) log.Debug("retrieving header cids for block hash ", blockHash.String())
pgStr := `SELECT block_hash, CAST(block_number as Text), parent_hash, cid, mh_key, timestamp FROM eth.header_cids pgStr := `SELECT block_hash, CAST(block_number as Text), parent_hash, cid, mh_key, CAST(td as Text),
state_root,uncle_root,tx_root,receipt_root,bloom,timestamp FROM eth.header_cids
WHERE block_hash = $1` WHERE block_hash = $1`
var headerCID models.HeaderModel var headerCID models.HeaderModel
return headerCID, tx.Get(&headerCID, pgStr, blockHash.String()) return headerCID, tx.Get(&headerCID, pgStr, blockHash.String())
@ -593,16 +594,27 @@ func (ecr *CIDRetriever) RetrieveTxCIDsByHeaderID(tx *sqlx.Tx, headerID string)
return txCIDs, tx.Select(&txCIDs, pgStr, headerID) return txCIDs, tx.Select(&txCIDs, pgStr, headerID)
} }
// RetrieveTxCIDsByBlockNumber retrieves all tx CIDs for the given blockNumber // RetrieveTxCIDsByHeaderIDs retrieves all tx CIDs for the given headerIDs
func (ecr *CIDRetriever) RetrieveTxCIDsByBlockNumber(tx *sqlx.Tx, blockNumber int64) ([]models.TxModel, error) { func (ecr *CIDRetriever) RetrieveTxCIDsByHeaderIDs(tx *sqlx.Tx, headerIDs []string) ([]models.TxModel, error) {
log.Debug("retrieving tx cids for block number ", blockNumber) log.Debug("retrieving tx cids for headerIDs ", headerIDs)
pgStr := `SELECT CAST(block_number as Text), header_id, index, tx_hash, cid, mh_key, pgStr := `SELECT header_id, index, tx_hash, cid, mh_key,
dst, src, tx_data, tx_type, value dst, src, tx_data, tx_type, value
FROM eth.transaction_cids FROM eth.transaction_cids
WHERE block_number = $1 WHERE header_id in (?)
ORDER BY index` ORDER BY index`
var txCIDs []models.TxModel var txCIDs []models.TxModel
return txCIDs, tx.Select(&txCIDs, pgStr, blockNumber) if len(headerIDs) < 1 {
return txCIDs, nil
}
query, args, err := sqlx.In(pgStr, headerIDs)
if err != nil {
return txCIDs, err
}
query = tx.Rebind(query)
return txCIDs, tx.Select(&txCIDs, query, args...)
} }
// RetrieveReceiptCIDsByTxIDs retrieves receipt CIDs by their associated tx IDs // RetrieveReceiptCIDsByTxIDs retrieves receipt CIDs by their associated tx IDs
@ -646,7 +658,13 @@ func (ecr *CIDRetriever) RetrieveHeaderAndTxCIDsByBlockNumber(blockNumber int64)
} }
var allTxCIDs [][]models.TxModel var allTxCIDs [][]models.TxModel
txCIDs, err := ecr.RetrieveTxCIDsByBlockNumber(tx, blockNumber) txHeaderIDs := funk.Map(
headerCIDs,
func(headerCID models.HeaderModel) string {
return headerCID.BlockHash
},
)
txCIDs, err := ecr.RetrieveTxCIDsByHeaderIDs(tx, txHeaderIDs.([]string))
if err != nil { if err != nil {
log.Error("tx cid retrieval error") log.Error("tx cid retrieval error")
return nil, nil, err return nil, nil, err
@ -736,7 +754,7 @@ func (ecr *CIDRetriever) RetrieveTxCIDByHash(txHash string) (models.TxModel, err
} }
}() }()
pgStr := `SELECT CAST(block_number as Text), header_id, index, tx_hash, cid, mh_key, pgStr := `SELECT header_id, index, tx_hash, cid, mh_key,
dst, src, tx_data, tx_type, value dst, src, tx_data, tx_type, value
FROM eth.transaction_cids FROM eth.transaction_cids
WHERE tx_hash = $1 WHERE tx_hash = $1

View File

@ -20,7 +20,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"math/big" "math/big"
"strconv"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/statediff/indexer/models" "github.com/ethereum/go-ethereum/statediff/indexer/models"
@ -123,18 +122,16 @@ func (f *IPLDFetcher) FetchHeaders(tx *sqlx.Tx, cids []models.HeaderModel) ([]mo
return headerIPLDs, nil return headerIPLDs, nil
} }
blockNumbers := make([]uint64, len(cids))
mhKeys := make([]string, len(cids)) mhKeys := make([]string, len(cids))
for i, c := range cids { for i, c := range cids {
var err error var err error
mhKeys[i] = c.MhKey mhKeys[i] = c.MhKey
blockNumbers[i], err = strconv.ParseUint(c.BlockNumber, 10, 64)
if err != nil { if err != nil {
return nil, err return nil, err
} }
} }
fetchedIPLDs, err := shared.FetchIPLDsByMhKeysAndBlockNumbers(tx, mhKeys, blockNumbers) fetchedIPLDs, err := shared.FetchIPLDsByMhKeys(tx, mhKeys)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -79,10 +79,10 @@ func FetchIPLDByMhKey(tx *sqlx.Tx, mhKey string) ([]byte, error) {
} }
// FetchIPLDByMhKeysAndBlockNumbers is used to retrieve iplds from Postgres blockstore with the provided tx, mhkey strings and blockNumbers // FetchIPLDByMhKeysAndBlockNumbers is used to retrieve iplds from Postgres blockstore with the provided tx, mhkey strings and blockNumbers
func FetchIPLDsByMhKeysAndBlockNumbers(tx *sqlx.Tx, mhKeys []string, blockNumbers []uint64) ([]models.IPLDModel, error) { func FetchIPLDsByMhKeys(tx *sqlx.Tx, mhKeys []string) ([]models.IPLDModel, error) {
var blocks []models.IPLDModel var blocks []models.IPLDModel
pgStr := `SELECT key, data, block_number FROM public.blocks WHERE key IN (?) AND block_number IN (?)` pgStr := `SELECT key, data FROM public.blocks WHERE key IN (?)`
query, args, err := sqlx.In(pgStr, mhKeys, blockNumbers) query, args, err := sqlx.In(pgStr, mhKeys)
if err != nil { if err != nil {
return blocks, err return blocks, err
} }