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

rpc: change BlockNumber constant values to match ethclient #27219

Merged
merged 8 commits into from
May 23, 2023

Conversation

holiman
Copy link
Contributor

@holiman holiman commented May 4, 2023

ethclient/ethclient.go Outdated Show resolved Hide resolved
@panicalways
Copy link
Contributor

Isn't that a big breaking change?

// Get pending balance
ethclient.BalanceAt(..., big.NewInt(-1))

Anyone who was using it as above will be broken.

@holiman
Copy link
Contributor Author

holiman commented May 4, 2023 via email

Copy link
Contributor

@s1na s1na left a comment

Choose a reason for hiding this comment

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

LGTM. We should add a backwards incompatibility note in the release

@karalabe
Copy link
Member

karalabe commented May 4, 2023

Using negative numbers for this is so so so bad. Still, shouldn't the negative numbers only be some internal implementation detail? They should not be exposed to the outside world afaik. cc @fjl?

@rjl493456442
Copy link
Member

I agree we shouldn't expose internal implementation specific detail to users. We need to change the block parameter type to a compound one which can be either a valid block number or a default block params(finalized, pending, etc).

But it requires a breaking change.

@fjl
Copy link
Contributor

fjl commented May 5, 2023

I hate that we have support for these special negative numbers in the first place. The only reason we have them is because I gave in on some PR years ago.

In the original ethclient API, we had support for "latest" by passing a nil block number as the only special case. This was at the time when block numbers were *big.Int everywhere, so the choice of nil seemed quite reasonable. We didn't have explicit support for "pending". It's not often that people need to get the 'pending header'.

ethclient does have an explicit way of accessing pending state, with special methods.

We added support for -1 being "pending" in #21177. Initially, it was implemented such that all negative numbers would mean "pending". The use case this was added for, is specifying the block ranges for log filtering. Note that we didn't think about rpc.BlockNumber compatibility when this feature was added. We just needed a way to add support for this special value and nil was already taken.

Later, we added support for "safe" and "finalized" in the same way, in #25580. These do make sense for BlockByNumber, etc. It was obvious to add them using the same numbering system used by rpc.BlockNumber because the new tags had just been added there as well, and we had to find some way of putting them in.

@fjl
Copy link
Contributor

fjl commented May 5, 2023

The problem is, -1 is part of the ethclient API, and has been for quite a while.

If we have to make a change at all, I would suggest we change the definition of rpc.PendingBlockNumber to be -1 and define rpc.LatestBlockNumber as -2. The specific values of these constants are not important, and they are not otherwise exposed in public APIs.

@holiman
Copy link
Contributor Author

holiman commented May 9, 2023

If we have to make a change at all, I would suggest we change the definition of rpc.PendingBlockNumber to be -1 and define rpc.LatestBlockNumber as -2. The specific values of these constants are not important, and they are not otherwise exposed in public APIs.

I don't see the point of that. If we do like in this PR, then:

  • How we break backwards-compatilibity: caller previously getting pending (via -1) is now getting latest instead.

With your proposed change,

  • How we break backwards-compatilibity: caller previously getting latest (via -1) is now getting pending instead.

I think we should move towards minimizing the use of pending, since the concept is so vague.

and they are not otherwise exposed in public APIs.

Are you sure?
Anyway, maybe I misunderstand something here. Perhaps better to discuss

@fjl
Copy link
Contributor

fjl commented May 9, 2023

I meant, we could swap the values of the constants instead of applying changes in this PR. Then ethclient's definition of -1 == "pending" would be the same as rpc.PendingBlockNumber.

There is no other code in go-ethereum that relies on the numeric value of these constants. rpc.BlockNumber is really only used as a parameter type in RPC method handlers, and so will always be decoded from JSON. Passing a negative block number is not allowed in JSON, the decoder will error (see https://github.com/ethereum/go-ethereum/blob/master/rpc/types.go#L79). You can only get these negative values by passing a block specifier string such as "pending".

@holiman holiman requested a review from fjl as a code owner May 10, 2023 14:25
@holiman
Copy link
Contributor Author

holiman commented May 10, 2023

@fjl PTAL

@fjl
Copy link
Contributor

fjl commented May 10, 2023

Interesting, a filters-related test failed. Need to investigate this.

@holiman holiman requested a review from karalabe as a code owner May 11, 2023 09:01
fjl added 3 commits May 16, 2023 16:37
- avoid creating big.Int just to compare with BlockNumber
- use String method of BlockNumber
@fjl fjl changed the title ethclient: fix discrepancy between latest/pending blocknumbers rpc: change BlockNumber constant values to match ethclient May 23, 2023
@@ -56,7 +56,8 @@ func TestUnmarshalJSONNewFilterArgs(t *testing.T) {

// from, to block number
var test1 FilterCriteria
vector := fmt.Sprintf(`{"fromBlock":"%#x","toBlock":"%#x"}`, fromBlock, toBlock)
vector := fmt.Sprintf(`{"fromBlock":"%v","toBlock":"%v"}`, fromBlock, toBlock)
t.Logf("vector: %s", vector)
Copy link
Contributor

Choose a reason for hiding this comment

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

Seems like debugging debris

@fjl fjl removed the status:triage label May 23, 2023
@fjl fjl merged commit 9231770 into ethereum:master May 23, 2023
@fjl fjl added this to the 1.11.7 milestone May 23, 2023
@holiman holiman deleted the fix_ethclient branch October 11, 2023 07:27
devopsbo3 pushed a commit to HorizenOfficial/go-ethereum that referenced this pull request Nov 10, 2023
…27219)

ethclient accepts certain negative block number values as specifiers for the "pending",
"safe" and "finalized" block. In case of "pending", the value accepted by ethclient (-1)
did not match rpc.PendingBlockNumber (-2).

This wasn't really a problem, but other values accepted by ethclient did match the
definitions in package rpc, and it's weird to have this one special case where they don't.

To fix it, we decided to change the values of the constants rather than changing ethclient.
The constant values are not otherwise significant. This is a breaking API change, but we
believe not a dangerous one.

---------

Co-authored-by: Felix Lange <fjl@twurst.com>
devopsbo3 added a commit to HorizenOfficial/go-ethereum that referenced this pull request Nov 10, 2023
devopsbo3 added a commit to HorizenOfficial/go-ethereum that referenced this pull request Nov 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants