Use block number when fetching block objects

This commit is contained in:
Prathamesh Musale 2022-11-02 20:33:16 +05:30
parent e6ac0625d7
commit e01fd374a7
2 changed files with 32 additions and 33 deletions

View File

@ -22,6 +22,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"math/big" "math/big"
"strconv"
"time" "time"
validator "github.com/cerc-io/eth-ipfs-state-validator/v4/pkg" validator "github.com/cerc-io/eth-ipfs-state-validator/v4/pkg"
@ -364,8 +365,10 @@ func (b *Backend) BlockByHash(ctx context.Context, hash common.Hash) (*types.Blo
return nil, err return nil, err
} }
blockNumber := header.Number.Uint64()
// Fetch uncles // Fetch uncles
uncles, err := b.GetUnclesByBlockHash(tx, hash) uncles, err := b.GetUnclesByBlockHashAndNumber(tx, hash, blockNumber)
if err != nil && err != sql.ErrNoRows { if err != nil && err != sql.ErrNoRows {
log.Error("error fetching uncles: ", err) log.Error("error fetching uncles: ", err)
return nil, err return nil, err
@ -389,14 +392,14 @@ func (b *Backend) BlockByHash(ctx context.Context, hash common.Hash) (*types.Blo
} }
// Fetch transactions // Fetch transactions
transactions, err := b.GetTransactionsByBlockHash(tx, hash) transactions, err := b.GetTransactionsByBlockHashAndNumber(tx, hash, blockNumber)
if err != nil && err != sql.ErrNoRows { if err != nil && err != sql.ErrNoRows {
log.Error("error fetching transactions: ", err) log.Error("error fetching transactions: ", err)
return nil, err return nil, err
} }
// Fetch receipts // Fetch receipts
receipts, err := b.GetReceiptsByBlockHash(tx, hash) receipts, err := b.GetReceiptsByBlockHashAndNumber(tx, hash, blockNumber)
if err != nil && err != sql.ErrNoRows { if err != nil && err != sql.ErrNoRows {
log.Error("error fetching receipts: ", err) log.Error("error fetching receipts: ", err)
return nil, err return nil, err
@ -418,8 +421,8 @@ func (b *Backend) GetHeaderByBlockHash(tx *sqlx.Tx, hash common.Hash) (*types.He
} }
// GetUnclesByBlockHash retrieves uncles for a provided block hash // GetUnclesByBlockHash retrieves uncles for a provided block hash
func (b *Backend) GetUnclesByBlockHash(tx *sqlx.Tx, hash common.Hash) ([]*types.Header, error) { func (b *Backend) GetUnclesByBlockHashAndNumber(tx *sqlx.Tx, hash common.Hash, number uint64) ([]*types.Header, error) {
_, uncleBytes, err := b.IPLDRetriever.RetrieveUnclesByBlockHash(tx, hash) _, uncleBytes, err := b.IPLDRetriever.RetrieveUncles(tx, hash, number)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -439,8 +442,8 @@ func (b *Backend) GetUnclesByBlockHash(tx *sqlx.Tx, hash common.Hash) ([]*types.
} }
// GetTransactionsByBlockHash retrieves transactions for a provided block hash // GetTransactionsByBlockHash retrieves transactions for a provided block hash
func (b *Backend) GetTransactionsByBlockHash(tx *sqlx.Tx, hash common.Hash) (types.Transactions, error) { func (b *Backend) GetTransactionsByBlockHashAndNumber(tx *sqlx.Tx, hash common.Hash, number uint64) (types.Transactions, error) {
_, transactionBytes, err := b.IPLDRetriever.RetrieveTransactionsByBlockHash(tx, hash) _, transactionBytes, err := b.IPLDRetriever.RetrieveTransactions(tx, hash, number)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -459,8 +462,8 @@ func (b *Backend) GetTransactionsByBlockHash(tx *sqlx.Tx, hash common.Hash) (typ
} }
// GetReceiptsByBlockHash retrieves receipts for a provided block hash // GetReceiptsByBlockHash retrieves receipts for a provided block hash
func (b *Backend) GetReceiptsByBlockHash(tx *sqlx.Tx, hash common.Hash) (types.Receipts, error) { func (b *Backend) GetReceiptsByBlockHashAndNumber(tx *sqlx.Tx, hash common.Hash, number uint64) (types.Receipts, error) {
_, receiptBytes, txs, err := b.IPLDRetriever.RetrieveReceiptsByBlockHash(tx, hash) _, receiptBytes, txs, err := b.IPLDRetriever.RetrieveReceipts(tx, hash, number)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -523,20 +526,13 @@ func (b *Backend) GetReceipts(ctx context.Context, hash common.Hash) (types.Rece
} }
}() }()
_, receiptBytes, txs, err := b.IPLDRetriever.RetrieveReceiptsByBlockHash(tx, hash) headerCID, err := b.Retriever.RetrieveHeaderCIDByHash(tx, hash)
if err != nil { if err != nil {
return nil, err return nil, err
} }
rcts := make(types.Receipts, len(receiptBytes)) blockNumber, _ := strconv.ParseUint(string(headerCID.BlockNumber), 10, 64)
for i, rctBytes := range receiptBytes {
rct := new(types.Receipt) return b.GetReceiptsByBlockHashAndNumber(tx, hash, blockNumber)
if err := rct.UnmarshalBinary(rctBytes); err != nil {
return nil, err
}
rct.TxHash = txs[i]
rcts[i] = rct
}
return rcts, nil
} }
// GetLogs returns all the logs for the given block hash // GetLogs returns all the logs for the given block hash
@ -557,7 +553,7 @@ func (b *Backend) GetLogs(ctx context.Context, hash common.Hash, number uint64)
} }
}() }()
_, receiptBytes, txs, err := b.IPLDRetriever.RetrieveReceiptsByBlockHash(tx, hash) _, receiptBytes, txs, err := b.IPLDRetriever.RetrieveReceipts(tx, hash, number)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -64,7 +64,7 @@ const (
AND uncle_cids.block_number = blocks.block_number AND uncle_cids.block_number = blocks.block_number
) )
WHERE block_hash = ANY($1::VARCHAR(66)[])` WHERE block_hash = ANY($1::VARCHAR(66)[])`
RetrieveUnclesByBlockHashPgStr = `SELECT uncle_cids.cid, data RetrieveUnclesPgStr = `SELECT uncle_cids.cid, data
FROM eth.uncle_cids FROM eth.uncle_cids
INNER JOIN eth.header_cids ON ( INNER JOIN eth.header_cids ON (
uncle_cids.header_id = header_cids.block_hash uncle_cids.header_id = header_cids.block_hash
@ -75,6 +75,7 @@ const (
AND uncle_cids.block_number = blocks.block_number AND uncle_cids.block_number = blocks.block_number
) )
WHERE header_cids.block_hash = $1 WHERE header_cids.block_hash = $1
AND header_cids.block_number = $2
ORDER BY uncle_cids.parent_hash` ORDER BY uncle_cids.parent_hash`
RetrieveUnclesByBlockNumberPgStr = `SELECT uncle_cids.cid, data RetrieveUnclesByBlockNumberPgStr = `SELECT uncle_cids.cid, data
FROM eth.uncle_cids FROM eth.uncle_cids
@ -101,7 +102,7 @@ const (
AND transaction_cids.block_number = blocks.block_number AND transaction_cids.block_number = blocks.block_number
) )
WHERE tx_hash = ANY($1::VARCHAR(66)[])` WHERE tx_hash = ANY($1::VARCHAR(66)[])`
RetrieveTransactionsByBlockHashPgStr = `SELECT transaction_cids.cid, data RetrieveTransactionsPgStr = `SELECT transaction_cids.cid, data
FROM eth.transaction_cids FROM eth.transaction_cids
INNER JOIN eth.header_cids ON ( INNER JOIN eth.header_cids ON (
transaction_cids.header_id = header_cids.block_hash transaction_cids.header_id = header_cids.block_hash
@ -112,6 +113,7 @@ const (
AND transaction_cids.block_number = blocks.block_number AND transaction_cids.block_number = blocks.block_number
) )
WHERE block_hash = $1 WHERE block_hash = $1
AND header_cids.block_number = $2
ORDER BY eth.transaction_cids.index ASC` ORDER BY eth.transaction_cids.index ASC`
RetrieveTransactionsByBlockNumberPgStr = `SELECT transaction_cids.cid, data RetrieveTransactionsByBlockNumberPgStr = `SELECT transaction_cids.cid, data
FROM eth.transaction_cids FROM eth.transaction_cids
@ -146,7 +148,7 @@ const (
) )
WHERE tx_hash = ANY($1::VARCHAR(66)[]) WHERE tx_hash = ANY($1::VARCHAR(66)[])
AND transaction_cids.header_id = (SELECT canonical_header_hash(transaction_cids.block_number))` AND transaction_cids.header_id = (SELECT canonical_header_hash(transaction_cids.block_number))`
RetrieveReceiptsByBlockHashPgStr = `SELECT receipt_cids.leaf_cid, data, eth.transaction_cids.tx_hash RetrieveReceiptsPgStr = `SELECT receipt_cids.leaf_cid, data, eth.transaction_cids.tx_hash
FROM eth.receipt_cids FROM eth.receipt_cids
INNER JOIN eth.transaction_cids ON ( INNER JOIN eth.transaction_cids ON (
receipt_cids.tx_id = transaction_cids.tx_hash receipt_cids.tx_id = transaction_cids.tx_hash
@ -162,6 +164,7 @@ const (
AND receipt_cids.block_number = blocks.block_number AND receipt_cids.block_number = blocks.block_number
) )
WHERE block_hash = $1 WHERE block_hash = $1
AND header_cids.block_number = $2
ORDER BY eth.transaction_cids.index ASC` ORDER BY eth.transaction_cids.index ASC`
RetrieveReceiptsByBlockNumberPgStr = `SELECT receipt_cids.leaf_cid, data RetrieveReceiptsByBlockNumberPgStr = `SELECT receipt_cids.leaf_cid, data
FROM eth.receipt_cids FROM eth.receipt_cids
@ -338,10 +341,10 @@ func (r *IPLDRetriever) RetrieveUnclesByHashes(hashes []common.Hash) ([]string,
return cids, uncles, nil return cids, uncles, nil
} }
// RetrieveUnclesByBlockHash returns the cids and rlp bytes for the uncles corresponding to the provided block hash (of non-omner root block) // RetrieveUncles returns the cids and rlp bytes for the uncles corresponding to the provided block hash, number (of non-omner root block)
func (r *IPLDRetriever) RetrieveUnclesByBlockHash(tx *sqlx.Tx, hash common.Hash) ([]string, [][]byte, error) { func (r *IPLDRetriever) RetrieveUncles(tx *sqlx.Tx, hash common.Hash, number uint64) ([]string, [][]byte, error) {
uncleResults := make([]ipldResult, 0) uncleResults := make([]ipldResult, 0)
if err := tx.Select(&uncleResults, RetrieveUnclesByBlockHashPgStr, hash.Hex()); err != nil { if err := tx.Select(&uncleResults, RetrieveUnclesPgStr, hash.Hex(), number); err != nil {
return nil, nil, err return nil, nil, err
} }
cids := make([]string, len(uncleResults)) cids := make([]string, len(uncleResults))
@ -393,10 +396,10 @@ func (r *IPLDRetriever) RetrieveTransactionsByHashes(hashes []common.Hash) ([]st
return cids, txs, nil return cids, txs, nil
} }
// RetrieveTransactionsByBlockHash returns the cids and rlp bytes for the transactions corresponding to the provided block hash // RetrieveTransactions returns the cids and rlp bytes for the transactions corresponding to the provided block hash, number
func (r *IPLDRetriever) RetrieveTransactionsByBlockHash(tx *sqlx.Tx, hash common.Hash) ([]string, [][]byte, error) { func (r *IPLDRetriever) RetrieveTransactions(tx *sqlx.Tx, hash common.Hash, number uint64) ([]string, [][]byte, error) {
txResults := make([]ipldResult, 0) txResults := make([]ipldResult, 0)
if err := tx.Select(&txResults, RetrieveTransactionsByBlockHashPgStr, hash.Hex()); err != nil { if err := tx.Select(&txResults, RetrieveTransactionsPgStr, hash.Hex(), number); err != nil {
return nil, nil, err return nil, nil, err
} }
cids := make([]string, len(txResults)) cids := make([]string, len(txResults))
@ -469,11 +472,11 @@ func (r *IPLDRetriever) RetrieveReceiptsByTxHashes(hashes []common.Hash) ([]stri
return cids, rcts, nil return cids, rcts, nil
} }
// RetrieveReceiptsByBlockHash returns the cids and rlp bytes for the receipts corresponding to the provided block hash. // RetrieveReceipts returns the cids and rlp bytes for the receipts corresponding to the provided block hash, number.
// cid returned corresponds to the leaf node data which contains the receipt. // cid returned corresponds to the leaf node data which contains the receipt.
func (r *IPLDRetriever) RetrieveReceiptsByBlockHash(tx *sqlx.Tx, hash common.Hash) ([]string, [][]byte, []common.Hash, error) { func (r *IPLDRetriever) RetrieveReceipts(tx *sqlx.Tx, hash common.Hash, number uint64) ([]string, [][]byte, []common.Hash, error) {
rctResults := make([]rctIpldResult, 0) rctResults := make([]rctIpldResult, 0)
if err := tx.Select(&rctResults, RetrieveReceiptsByBlockHashPgStr, hash.Hex()); err != nil { if err := tx.Select(&rctResults, RetrieveReceiptsPgStr, hash.Hex(), number); err != nil {
return nil, nil, nil, err return nil, nil, nil, err
} }
cids := make([]string, len(rctResults)) cids := make([]string, len(rctResults))