From b90c28906ac2c259b033e1831210c7e4593a616b Mon Sep 17 00:00:00 2001 From: prathamesh0 <42446521+prathamesh0@users.noreply.github.com> Date: Fri, 15 Jul 2022 16:19:31 +0530 Subject: [PATCH] Add log while serving getBlockByNumber (#176) * Add log while serving getBlockByNumber * Update GQL API to return empty result when header not found for hash --- pkg/eth/api.go | 6 +++--- pkg/eth/cid_retriever.go | 3 ++- pkg/graphql/graphql.go | 9 ++++++--- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/pkg/eth/api.go b/pkg/eth/api.go index 43d1811b..f98d1afd 100644 --- a/pkg/eth/api.go +++ b/pkg/eth/api.go @@ -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" @@ -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) @@ -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) diff --git a/pkg/eth/cid_retriever.go b/pkg/eth/cid_retriever.go index 1c970d61..bab05da3 100644 --- a/pkg/eth/cid_retriever.go +++ b/pkg/eth/cid_retriever.go @@ -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, @@ -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 diff --git a/pkg/graphql/graphql.go b/pkg/graphql/graphql.go index c414e9a9..9456fc82 100644 --- a/pkg/graphql/graphql.go +++ b/pkg/graphql/graphql.go @@ -23,6 +23,7 @@ import ( "database/sql" "errors" "fmt" + "strings" "time" "github.com/ethereum/go-ethereum/common" @@ -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 {