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
+26 -18
View File
@@ -65,8 +65,8 @@ type BlockNumber int64
const (
SafeBlockNumber = BlockNumber(-4)
FinalizedBlockNumber = BlockNumber(-3)
PendingBlockNumber = BlockNumber(-2)
LatestBlockNumber = BlockNumber(-1)
LatestBlockNumber = BlockNumber(-2)
PendingBlockNumber = BlockNumber(-1)
EarliestBlockNumber = BlockNumber(0)
)
@@ -111,28 +111,36 @@ func (bn *BlockNumber) UnmarshalJSON(data []byte) error {
return nil
}
// Int64 returns the block number as int64.
func (bn BlockNumber) Int64() int64 {
return (int64)(bn)
}
// MarshalText implements encoding.TextMarshaler. It marshals:
// - "safe", "finalized", "latest", "earliest" or "pending" as strings
// - other numbers as hex
func (bn BlockNumber) MarshalText() ([]byte, error) {
switch bn {
case EarliestBlockNumber:
return []byte("earliest"), nil
case LatestBlockNumber:
return []byte("latest"), nil
case PendingBlockNumber:
return []byte("pending"), nil
case FinalizedBlockNumber:
return []byte("finalized"), nil
case SafeBlockNumber:
return []byte("safe"), nil
default:
return hexutil.Uint64(bn).MarshalText()
}
return []byte(bn.String()), nil
}
func (bn BlockNumber) Int64() int64 {
return (int64)(bn)
func (bn BlockNumber) String() string {
switch bn {
case EarliestBlockNumber:
return "earliest"
case LatestBlockNumber:
return "latest"
case PendingBlockNumber:
return "pending"
case FinalizedBlockNumber:
return "finalized"
case SafeBlockNumber:
return "safe"
default:
if bn < 0 {
return fmt.Sprintf("<invalid %d>", bn)
}
return hexutil.Uint64(bn).String()
}
}
type BlockNumberOrHash struct {