Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ethclient/ethclient.go add_get_block_by_tag_function #30984

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions ethclient/ethclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ func (ec *Client) BlockByNumber(ctx context.Context, number *big.Int) (*types.Bl
return ec.getBlock(ctx, "eth_getBlockByNumber", toBlockNumArg(number), true)
}

// BlockByNumber returns a block from the current canonical chain. If tag is nil, the
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can retrieve the block by using tag with BlockByNumber endpoint.

e.g.

  • BlockByNumber(context.Background(), nil) means latest,
  • BlockByNumber(context.Background(), big.NewInt(-1)) means pending,
  • BlockByNumber(context.Background(), big.NewInt(-2)) means latest,
  • BlockByNumber(context.Background(), big.NewInt(-3)) means finalized,
  • BlockByNumber(context.Background(), big.NewInt(-4)) means safe,

However, I do see the point for having an endpoint to explicitly request blocks with tag.
While these special negative numbers can be translated by ethclient to tags, they are Geth-related, not publicly defined in the spec. Probably it's better to extend the BlockByNumber endpoint a bit, allowing specify tag explicitly?

cc @fjl

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your reply.,Yes, I can know which numbers represent which labels after digging into the code, but it is very inconvenient, so do I need to modify the BlockByNumber method and submit it again?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree the numbers are not very visible right now. After some discussion in team, we don't want to add another method, but would be open to improving documentation. We could also add redefinitions of these constants into package ethclient for convenience.

Copy link
Author

@EdisonSR EdisonSR Jan 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your reply.In response to your request, I have removed the method of querying according to tag , and in the BlockByNumber method, I have detailed instructions on how to obtain the corresponding blocknumber through tags. Please have a look, I hope this is a good PR , you can see the new commit.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// latest known block is returned.
// you can use the special tag earliest, latest, finalized, safe, or finalized.
func (ec *Client) BlockBySpecialTag(ctx context.Context, tag string) (*types.Block, error) {
return ec.getBlock(ctx, "eth_getBlockByNumber", tag, true)
}

// BlockNumber returns the most recent block number
func (ec *Client) BlockNumber(ctx context.Context) (uint64, error) {
var result hexutil.Uint64
Expand Down