Skip to content

Commit

Permalink
chg: handle edge cases with string blockNumber
Browse files Browse the repository at this point in the history
  • Loading branch information
marcello33 committed Nov 13, 2024
1 parent ec7ac02 commit 016f79a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 10 deletions.
26 changes: 24 additions & 2 deletions bor/client/grpc/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package grpc

import (
"context"
"fmt"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/rpc"
"math/big"

"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -54,8 +57,10 @@ func (h *BorGRPCClient) GetVoteOnHash(ctx context.Context, startBlock uint64, en

func (h *BorGRPCClient) HeaderByNumber(ctx context.Context, blockID uint64) (*ethTypes.Header, error) {

blockNumberAsString := toBlockNumArg(big.NewInt(int64(blockID)))

req := &proto.GetHeaderByNumberRequest{
Number: blockID,
Number: blockNumberAsString,
}

log.Info("Fetching header by number")
Expand All @@ -78,8 +83,10 @@ func (h *BorGRPCClient) HeaderByNumber(ctx context.Context, blockID uint64) (*et

func (h *BorGRPCClient) BlockByNumber(ctx context.Context, blockID uint64) (*ethTypes.Block, error) {

blockNumberAsString := toBlockNumArg(big.NewInt(int64(blockID)))

req := &proto.GetBlockByNumberRequest{
Number: blockID,
Number: blockNumberAsString,
}

log.Info("Fetching block by number")
Expand Down Expand Up @@ -153,3 +160,18 @@ func receiptResponseToTypesReceipt(receipt *proto.Receipt) *ethTypes.Receipt {
TransactionIndex: uint(receipt.TransactionIndex),
}
}

func toBlockNumArg(number *big.Int) string {
if number == nil {
return "latest"
}
if number.Sign() >= 0 {
return hexutil.EncodeBig(number)
}
// It's negative.
if number.IsInt64() {
return rpc.BlockNumber(number.Int64()).String()
}
// It's negative and large, which is invalid.
return fmt.Sprintf("<invalid %d>", number)
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ require (
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0
github.com/hashicorp/golang-lru v1.0.2
github.com/json-iterator/go v1.1.12
github.com/maticnetwork/polyproto v0.0.3
github.com/maticnetwork/polyproto v0.0.4-0.20241113101917-6744479e4d59
github.com/pborman/uuid v1.2.1
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.19.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2225,8 +2225,8 @@ github.com/maticnetwork/crand v1.0.2 h1:Af0tAivC8zrxXDpGWNWVT/0s1fOz8w0eRbahZgUR
github.com/maticnetwork/crand v1.0.2/go.mod h1:/NRNL3bj2eYdqpWmoIP5puxndTpi0XRxpj5ZKxfHjyg=
github.com/maticnetwork/heimdall v1.0.7/go.mod h1:+ANI5+VV28ahwfdl7oMzrcNwaTEs1Fn6z39BqBGcvaA=
github.com/maticnetwork/polyproto v0.0.3-0.20230216113155-340ea926ca53/go.mod h1:e1mU2EXSwEpn5jM7GfNwu3AupsV6WAGoPFFfswXOF0o=
github.com/maticnetwork/polyproto v0.0.3 h1:a69rIp97fcl3ABY4LlVX9B2t1qhLa0Jhny3HNOzReBU=
github.com/maticnetwork/polyproto v0.0.3/go.mod h1:e1mU2EXSwEpn5jM7GfNwu3AupsV6WAGoPFFfswXOF0o=
github.com/maticnetwork/polyproto v0.0.4-0.20241113101917-6744479e4d59 h1:FAezvZ+jt3b1all6taYql6+mafszYBpwfD5KYQqQdNg=
github.com/maticnetwork/polyproto v0.0.4-0.20241113101917-6744479e4d59/go.mod h1:e1mU2EXSwEpn5jM7GfNwu3AupsV6WAGoPFFfswXOF0o=
github.com/maticnetwork/tendermint v0.33.2 h1:R9M7jgAmON8K/LbzMvtWPDhtPkNcqzkUUHp1ict/h3s=
github.com/maticnetwork/tendermint v0.33.2/go.mod h1:D2fcnxGk6bje+LoPwImuKSSYLiK7/G06IynGNDSEcJk=
github.com/matryer/try v0.0.0-20161228173917-9ac251b645a2/go.mod h1:0KeJpeMD6o+O4hW7qJOT7vyQPKrWmj26uf5wMc/IiIs=
Expand Down
7 changes: 2 additions & 5 deletions helper/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -464,10 +464,7 @@ func (c *ContractCaller) GetMaticChainBlock(blockNum *big.Int) (header *ethTypes

var latestBlock *ethTypes.Header

if c.MaticGrpcFlag && blockNum != nil {
if blockNum.Sign() < 0 {
blockNum = new(big.Int).Abs(blockNum)
}
if c.MaticGrpcFlag {
latestBlock, err = c.MaticGrpcClient.HeaderByNumber(ctx, blockNum.Uint64())
} else {
latestBlock, err = c.MaticChainClient.HeaderByNumber(ctx, blockNum)
Expand Down Expand Up @@ -885,7 +882,7 @@ func (c *ContractCaller) GetBlockByNumber(ctx context.Context, blockNumber uint6
var block *ethTypes.Block
var err error

if c.MaticGrpcFlag && big.NewInt(int64(blockNumber)) != nil {
if c.MaticGrpcFlag {
block, err = c.MaticGrpcClient.BlockByNumber(ctx, blockNumber)
} else {
block, err = c.MaticChainClient.BlockByNumber(ctx, big.NewInt(int64(blockNumber)))
Expand Down

0 comments on commit 016f79a

Please sign in to comment.