From 016f79a7a18fa6d01c9f745df5b4f2dbc6178c26 Mon Sep 17 00:00:00 2001 From: marcello33 Date: Wed, 13 Nov 2024 11:33:19 +0100 Subject: [PATCH] chg: handle edge cases with string blockNumber --- bor/client/grpc/query.go | 26 ++++++++++++++++++++++++-- go.mod | 2 +- go.sum | 4 ++-- helper/call.go | 7 ++----- 4 files changed, 29 insertions(+), 10 deletions(-) diff --git a/bor/client/grpc/query.go b/bor/client/grpc/query.go index 36e07663c..f363adbb9 100644 --- a/bor/client/grpc/query.go +++ b/bor/client/grpc/query.go @@ -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" @@ -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") @@ -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") @@ -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("", number) +} diff --git a/go.mod b/go.mod index c976c52f6..c8b1ce5d3 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 5d48562bb..dae90f54c 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/helper/call.go b/helper/call.go index 556291d41..4f1a84a66 100644 --- a/helper/call.go +++ b/helper/call.go @@ -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) @@ -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)))