From 3ccd6b6dbb462755079dc8394b5c141ef397486c Mon Sep 17 00:00:00 2001 From: Sina Mahmoodi <1591639+s1na@users.noreply.github.com> Date: Wed, 5 Jan 2022 16:22:46 +0100 Subject: [PATCH] graphql: fix block resolving for parent field (#24191) Fixes #24161 --- graphql/graphql.go | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/graphql/graphql.go b/graphql/graphql.go index 3e23f41f7..16e0eb654 100644 --- a/graphql/graphql.go +++ b/graphql/graphql.go @@ -599,21 +599,18 @@ func (b *Block) BaseFeePerGas(ctx context.Context) (*hexutil.Big, error) { } func (b *Block) Parent(ctx context.Context) (*Block, error) { - // If the block header hasn't been fetched, and we'll need it, fetch it. - if b.numberOrHash == nil && b.header == nil { - if _, err := b.resolveHeader(ctx); err != nil { - return nil, err - } + if _, err := b.resolveHeader(ctx); err != nil { + return nil, err } - if b.header != nil && b.header.Number.Uint64() > 0 { - num := rpc.BlockNumberOrHashWithNumber(rpc.BlockNumber(b.header.Number.Uint64() - 1)) - return &Block{ - backend: b.backend, - numberOrHash: &num, - hash: b.header.ParentHash, - }, nil + if b.header == nil || b.header.Number.Uint64() < 1 { + return nil, nil } - return nil, nil + num := rpc.BlockNumberOrHashWithNumber(rpc.BlockNumber(b.header.Number.Uint64() - 1)) + return &Block{ + backend: b.backend, + numberOrHash: &num, + hash: b.header.ParentHash, + }, nil } func (b *Block) Difficulty(ctx context.Context) (hexutil.Big, error) {