JSON parsing of safe & finalized (#4524)

This commit is contained in:
Andrew Ashikhmin 2022-06-23 19:37:39 +02:00 committed by GitHub
parent 7896fc60b6
commit 8de7c5e41c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 7 deletions

View File

@ -71,15 +71,15 @@ type BlockNumber int64
type Timestamp uint64
const (
FinalizeBlockNumber = BlockNumber(-4)
SafeBlockNumber = BlockNumber(-3)
PendingBlockNumber = BlockNumber(-2)
LatestBlockNumber = BlockNumber(-1)
EarliestBlockNumber = BlockNumber(0)
FinalizedBlockNumber = BlockNumber(-4)
SafeBlockNumber = BlockNumber(-3)
PendingBlockNumber = BlockNumber(-2)
LatestBlockNumber = BlockNumber(-1)
EarliestBlockNumber = BlockNumber(0)
)
// UnmarshalJSON parses the given JSON fragment into a BlockNumber. It supports:
// - "latest", "earliest" or "pending" as string arguments
// - "latest", "earliest", "pending", "safe", or "finalized" as string arguments
// - the block number
// Returned errors:
// - an invalid block number error when the given argument isn't a known strings
@ -100,6 +100,12 @@ func (bn *BlockNumber) UnmarshalJSON(data []byte) error {
case "pending":
*bn = PendingBlockNumber
return nil
case "safe":
*bn = SafeBlockNumber
return nil
case "finalized":
*bn = FinalizedBlockNumber
return nil
case "null":
*bn = LatestBlockNumber
return nil
@ -173,6 +179,14 @@ func (bnh *BlockNumberOrHash) UnmarshalJSON(data []byte) error {
bn := PendingBlockNumber
bnh.BlockNumber = &bn
return nil
case "safe":
bn := SafeBlockNumber
bnh.BlockNumber = &bn
return nil
case "finalized":
bn := FinalizedBlockNumber
bnh.BlockNumber = &bn
return nil
default:
if len(input) == 66 {
hash := common.Hash{}

View File

@ -50,7 +50,7 @@ func _GetBlockNumber(requireCanonical bool, blockNrOrHash rpc.BlockNumberOrHash,
}
case rpc.EarliestBlockNumber:
blockNumber = 0
case rpc.FinalizeBlockNumber:
case rpc.FinalizedBlockNumber:
blockNumber, err = GetFinalizedBlockNumber(tx)
if err != nil {
return 0, common.Hash{}, false, err