rpc: change BlockNumber constant values to match ethclient (#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>
This commit is contained in:
Martin Holst Swende
2023-05-23 13:18:38 +02:00
committed by GitHub
co-authored by Felix Lange
parent 1a18283e85
commit 9231770811
6 changed files with 60 additions and 53 deletions
+8 -11
View File
@@ -20,6 +20,7 @@ package gethclient
import (
"context"
"encoding/json"
"fmt"
"math/big"
"runtime"
"runtime/debug"
@@ -207,19 +208,15 @@ func toBlockNumArg(number *big.Int) string {
if number == nil {
return "latest"
}
pending := big.NewInt(-1)
if number.Cmp(pending) == 0 {
return "pending"
if number.Sign() >= 0 {
return hexutil.EncodeBig(number)
}
finalized := big.NewInt(int64(rpc.FinalizedBlockNumber))
if number.Cmp(finalized) == 0 {
return "finalized"
// It's negative.
if number.IsInt64() {
return rpc.BlockNumber(number.Int64()).String()
}
safe := big.NewInt(int64(rpc.SafeBlockNumber))
if number.Cmp(safe) == 0 {
return "safe"
}
return hexutil.EncodeBig(number)
// It's negative and large, which is invalid.
return fmt.Sprintf("<invalid %d>", number)
}
func toCallArg(msg ethereum.CallMsg) interface{} {