Skip to content

Commit

Permalink
rpc, ethclient: cater 'finalized' and 'safe' blocks in ethclient (#517)…
Browse files Browse the repository at this point in the history
… (#523)

* rpc: add finalized and safe block types

* ethclient: honour safe and finalized block requests

* fix: remove BlockByNumberWithoutTx function
  • Loading branch information
manav2401 authored Sep 22, 2022
1 parent a11952f commit 813b74f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
8 changes: 8 additions & 0 deletions ethclient/ethclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,14 @@ func toBlockNumArg(number *big.Int) string {
if number.Cmp(pending) == 0 {
return "pending"
}
finalized := big.NewInt(int64(rpc.FinalizedBlockNumber))
if number.Cmp(finalized) == 0 {
return "finalized"
}
safe := big.NewInt(int64(rpc.SafeBlockNumber))
if number.Cmp(safe) == 0 {
return "safe"
}
return hexutil.EncodeBig(number)
}

Expand Down
8 changes: 8 additions & 0 deletions ethclient/gethclient/gethclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,14 @@ func toBlockNumArg(number *big.Int) string {
if number.Cmp(pending) == 0 {
return "pending"
}
finalized := big.NewInt(int64(rpc.FinalizedBlockNumber))
if number.Cmp(finalized) == 0 {
return "finalized"
}
safe := big.NewInt(int64(rpc.SafeBlockNumber))
if number.Cmp(safe) == 0 {
return "safe"
}
return hexutil.EncodeBig(number)
}

Expand Down
8 changes: 5 additions & 3 deletions rpc/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,11 @@ type jsonWriter interface {
type BlockNumber int64

const (
PendingBlockNumber = BlockNumber(-2)
LatestBlockNumber = BlockNumber(-1)
EarliestBlockNumber = BlockNumber(0)
SafeBlockNumber = BlockNumber(-4)
FinalizedBlockNumber = BlockNumber(-3)
PendingBlockNumber = BlockNumber(-2)
LatestBlockNumber = BlockNumber(-1)
EarliestBlockNumber = BlockNumber(0)
)

// UnmarshalJSON parses the given JSON fragment into a BlockNumber. It supports:
Expand Down

0 comments on commit 813b74f

Please sign in to comment.