Skip to content

Commit

Permalink
Add log while serving getBlockByNumber (#176)
Browse files Browse the repository at this point in the history
* Add log while serving getBlockByNumber

* Update GQL API to return empty result when header not found for hash
  • Loading branch information
prathamesh0 authored Jul 15, 2022
1 parent 6cb54ca commit b90c289
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
6 changes: 3 additions & 3 deletions pkg/eth/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import (
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/eth/filters"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/rpc"
"github.com/ethereum/go-ethereum/statediff"
Expand Down Expand Up @@ -157,6 +156,7 @@ func (pea *PublicEthAPI) BlockNumber() hexutil.Uint64 {
// * When fullTx is true all transactions in the block are returned, otherwise
// only the transaction hash is returned.
func (pea *PublicEthAPI) GetBlockByNumber(ctx context.Context, number rpc.BlockNumber, fullTx bool) (map[string]interface{}, error) {
logrus.Debug("Received getBlockByNumber request for number ", number.Int64())
block, err := pea.B.BlockByNumber(ctx, number)
if block != nil && err == nil {
return pea.rpcMarshalBlock(block, true, fullTx)
Expand Down Expand Up @@ -1097,13 +1097,13 @@ func (pea *PublicEthAPI) writeStateDiffFor(blockHash common.Hash) {
func (pea *PublicEthAPI) rpcMarshalBlock(b *types.Block, inclTx bool, fullTx bool) (map[string]interface{}, error) {
fields, err := RPCMarshalBlock(b, inclTx, fullTx)
if err != nil {
log.Error("error RPC marshalling block with hash", b.Hash().String(), err)
logrus.Error("error RPC marshalling block with hash", b.Hash().String(), err)
return nil, err
}
if inclTx {
td, err := pea.B.GetTd(b.Hash())
if err != nil {
log.Error("error getting td for block with hash", b.Hash().String(), err)
logrus.Error("error getting td for block with hash and number", b.Hash().String(), b.Number().String(), err)
return nil, err
}
fields["totalDifficulty"] = (*hexutil.Big)(td)
Expand Down
3 changes: 2 additions & 1 deletion pkg/eth/cid_retriever.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ func receiptFilterConditions(id *int, pgStr string, args []interface{}, rctFilte
// RetrieveFilteredGQLLogs retrieves and returns all the log CIDs provided blockHash that conform to the provided
// filter parameters.
func (ecr *CIDRetriever) RetrieveFilteredGQLLogs(tx *sqlx.Tx, rctFilter ReceiptFilter, blockHash *common.Hash) ([]LogResult, error) {
log.Debug("retrieving log cids for receipt ids")
log.Debug("retrieving log cids for receipt ids with block hash", blockHash.String())
args := make([]interface{}, 0, 4)
id := 1
pgStr := `SELECT CAST(eth.log_cids.block_number as Text), eth.log_cids.leaf_cid, eth.log_cids.index, eth.log_cids.rct_id,
Expand Down Expand Up @@ -722,6 +722,7 @@ func (ecr *CIDRetriever) RetrieveHeaderAndTxCIDsByBlockHash(blockHash common.Has
err := ecr.gormDB.Preload("TransactionCIDs", func(tx *gorm.DB) *gorm.DB {
return tx.Select("cid", "tx_hash", "index", "src", "dst", "header_id", "block_number")
}).Joins("IPLD").Find(&headerCIDs, "block_hash = ?", blockHash.String()).Error

if err != nil {
log.Error("header cid retrieval error")
return HeaderCIDRecord{}, err
Expand Down
9 changes: 6 additions & 3 deletions pkg/graphql/graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"database/sql"
"errors"
"fmt"
"strings"
"time"

"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -1268,10 +1269,12 @@ func (r *Resolver) AllEthHeaderCids(ctx context.Context, args struct {
if args.Condition.BlockHash != nil {
headerCID, err := r.backend.Retriever.RetrieveHeaderAndTxCIDsByBlockHash(common.HexToHash(*args.Condition.BlockHash))
if err != nil {
return nil, err
if !strings.Contains(err.Error(), "not found") {
return nil, err
}
} else {
headerCIDs = append(headerCIDs, headerCID)
}

headerCIDs = append(headerCIDs, headerCID)
} else if args.Condition.BlockNumber != nil {
headerCIDs, err = r.backend.Retriever.RetrieveHeaderAndTxCIDsByBlockNumber(args.Condition.BlockNumber.ToInt().Int64())
if err != nil {
Expand Down

0 comments on commit b90c289

Please sign in to comment.