From 80413211c2a9d0d5ff34c44dcca6d5a817c9b488 Mon Sep 17 00:00:00 2001 From: nabarun Date: Mon, 30 May 2022 19:41:03 +0530 Subject: [PATCH] Remove block_number from queries for v3 schema --- pkg/eth/cid_retriever.go | 36 +++++++++++++++++++++++++++--------- pkg/eth/ipld_fetcher.go | 5 +---- pkg/shared/functions.go | 6 +++--- 3 files changed, 31 insertions(+), 16 deletions(-) diff --git a/pkg/eth/cid_retriever.go b/pkg/eth/cid_retriever.go index 206ca791..789ad459 100644 --- a/pkg/eth/cid_retriever.go +++ b/pkg/eth/cid_retriever.go @@ -577,7 +577,8 @@ func (ecr *CIDRetriever) RetrieveBlockByNumber(blockNumber int64) (models.Header // RetrieveHeaderCIDByHash returns the header for the given block hash func (ecr *CIDRetriever) RetrieveHeaderCIDByHash(tx *sqlx.Tx, blockHash common.Hash) (models.HeaderModel, error) { 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` var headerCID models.HeaderModel 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) } -// RetrieveTxCIDsByBlockNumber retrieves all tx CIDs for the given blockNumber -func (ecr *CIDRetriever) RetrieveTxCIDsByBlockNumber(tx *sqlx.Tx, blockNumber int64) ([]models.TxModel, error) { - log.Debug("retrieving tx cids for block number ", blockNumber) - pgStr := `SELECT CAST(block_number as Text), header_id, index, tx_hash, cid, mh_key, +// RetrieveTxCIDsByHeaderIDs retrieves all tx CIDs for the given headerIDs +func (ecr *CIDRetriever) RetrieveTxCIDsByHeaderIDs(tx *sqlx.Tx, headerIDs []string) ([]models.TxModel, error) { + log.Debug("retrieving tx cids for headerIDs ", headerIDs) + pgStr := `SELECT header_id, index, tx_hash, cid, mh_key, dst, src, tx_data, tx_type, value FROM eth.transaction_cids - WHERE block_number = $1 + WHERE header_id in (?) ORDER BY index` + 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 @@ -646,7 +658,13 @@ func (ecr *CIDRetriever) RetrieveHeaderAndTxCIDsByBlockNumber(blockNumber int64) } 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 { log.Error("tx cid retrieval error") 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 FROM eth.transaction_cids WHERE tx_hash = $1 diff --git a/pkg/eth/ipld_fetcher.go b/pkg/eth/ipld_fetcher.go index 965028c1..fa0d4c57 100644 --- a/pkg/eth/ipld_fetcher.go +++ b/pkg/eth/ipld_fetcher.go @@ -20,7 +20,6 @@ import ( "errors" "fmt" "math/big" - "strconv" "github.com/ethereum/go-ethereum/common" "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 } - blockNumbers := make([]uint64, len(cids)) mhKeys := make([]string, len(cids)) for i, c := range cids { var err error mhKeys[i] = c.MhKey - blockNumbers[i], err = strconv.ParseUint(c.BlockNumber, 10, 64) if err != nil { return nil, err } } - fetchedIPLDs, err := shared.FetchIPLDsByMhKeysAndBlockNumbers(tx, mhKeys, blockNumbers) + fetchedIPLDs, err := shared.FetchIPLDsByMhKeys(tx, mhKeys) if err != nil { return nil, err } diff --git a/pkg/shared/functions.go b/pkg/shared/functions.go index c17d1b01..eede20bc 100644 --- a/pkg/shared/functions.go +++ b/pkg/shared/functions.go @@ -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 -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 - pgStr := `SELECT key, data, block_number FROM public.blocks WHERE key IN (?) AND block_number IN (?)` - query, args, err := sqlx.In(pgStr, mhKeys, blockNumbers) + pgStr := `SELECT key, data FROM public.blocks WHERE key IN (?)` + query, args, err := sqlx.In(pgStr, mhKeys) if err != nil { return blocks, err }