From 9cb6fcac0b7e6b7478950eb86327a75778585e2a Mon Sep 17 00:00:00 2001 From: marcello33 Date: Wed, 13 Nov 2024 11:33:02 +0100 Subject: [PATCH 1/7] chg: handle edge cases with string blockNumber --- eth/filters/IBackend.go | 4 ++-- go.mod | 2 +- go.sum | 4 ++-- internal/cli/server/api_service.go | 36 ++++++++++++++++++++++++++++-- 4 files changed, 39 insertions(+), 7 deletions(-) diff --git a/eth/filters/IBackend.go b/eth/filters/IBackend.go index 7dfd509f83..3a69c90d2e 100644 --- a/eth/filters/IBackend.go +++ b/eth/filters/IBackend.go @@ -132,7 +132,7 @@ func (mr *MockBackendMockRecorder) GetBorBlockLogs(arg0, arg1 interface{}) *gomo // GetBorBlockReceipt mocks base method. func (m *MockBackend) GetBorBlockReceipt(arg0 context.Context, arg1 common.Hash) (*types.Receipt, error) { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "BorBlockReceipt", arg0, arg1) + ret := m.ctrl.Call(m, "GetBorBlockReceipt", arg0, arg1) ret0, _ := ret[0].(*types.Receipt) ret1, _ := ret[1].(error) return ret0, ret1 @@ -141,7 +141,7 @@ func (m *MockBackend) GetBorBlockReceipt(arg0 context.Context, arg1 common.Hash) // GetBorBlockReceipt indicates an expected call of GetBorBlockReceipt. func (mr *MockBackendMockRecorder) GetBorBlockReceipt(arg0, arg1 interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "BorBlockReceipt", reflect.TypeOf((*MockBackend)(nil).GetBorBlockReceipt), arg0, arg1) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetBorBlockReceipt", reflect.TypeOf((*MockBackend)(nil).GetBorBlockReceipt), arg0, arg1) } // GetLogs mocks base method. diff --git a/go.mod b/go.mod index a976c95e21..f8258fb3d8 100644 --- a/go.mod +++ b/go.mod @@ -65,7 +65,7 @@ require ( github.com/kylelemons/godebug v1.1.0 github.com/maticnetwork/crand v1.0.2 github.com/maticnetwork/heimdall v1.0.7 - github.com/maticnetwork/polyproto v0.0.3 + github.com/maticnetwork/polyproto v0.0.4-0.20241113101917-6744479e4d59 github.com/mattn/go-colorable v0.1.13 github.com/mattn/go-isatty v0.0.20 github.com/mitchellh/cli v1.1.5 diff --git a/go.sum b/go.sum index 4ed86b6fc1..b60ff0b105 100644 --- a/go.sum +++ b/go.sum @@ -1707,8 +1707,8 @@ github.com/maticnetwork/heimdall v1.0.4/go.mod h1:Xh7KFvtbs/SVNjOI8IgYmk6JdzYx89 github.com/maticnetwork/heimdall v1.0.7 h1:QStn+hbZKxfE+PqecaorA/uATDPuQoi+U9Z7IIonb60= 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.0 h1:f+vORM02BoUOlCvnu3Zjw5rv6l6JSNVchWjH03rUuR8= github.com/maticnetwork/tendermint v0.33.0/go.mod h1:D2fcnxGk6bje+LoPwImuKSSYLiK7/G06IynGNDSEcJk= github.com/matryer/try v0.0.0-20161228173917-9ac251b645a2/go.mod h1:0KeJpeMD6o+O4hW7qJOT7vyQPKrWmj26uf5wMc/IiIs= diff --git a/internal/cli/server/api_service.go b/internal/cli/server/api_service.go index 67e7c0924c..670cb65dbe 100644 --- a/internal/cli/server/api_service.go +++ b/internal/cli/server/api_service.go @@ -38,7 +38,11 @@ func headerToProtoborHeader(h *types.Header) *protobor.Header { } func (s *Server) HeaderByNumber(ctx context.Context, req *protobor.GetHeaderByNumberRequest) (*protobor.GetHeaderByNumberResponse, error) { - header, err := s.backend.APIBackend.HeaderByNumber(ctx, rpc.BlockNumber(req.Number)) + bN, err := getRpcBlockNumberFromString(req.Number) + if err != nil { + return nil, err + } + header, err := s.backend.APIBackend.HeaderByNumber(ctx, bN) if err != nil { return nil, err } @@ -47,7 +51,11 @@ func (s *Server) HeaderByNumber(ctx context.Context, req *protobor.GetHeaderByNu } func (s *Server) BlockByNumber(ctx context.Context, req *protobor.GetBlockByNumberRequest) (*protobor.GetBlockByNumberResponse, error) { - block, err := s.backend.APIBackend.BlockByNumber(ctx, rpc.BlockNumber(req.Number)) + bN, err := getRpcBlockNumberFromString(req.Number) + if err != nil { + return nil, err + } + block, err := s.backend.APIBackend.BlockByNumber(ctx, bN) if err != nil { return nil, err } @@ -91,3 +99,27 @@ func (s *Server) BorBlockReceipt(ctx context.Context, req *protobor.ReceiptReque return &protobor.ReceiptResponse{Receipt: ConvertReceiptToProtoReceipt(receipt)}, nil } + +func getRpcBlockNumberFromString(blockNumber string) (rpc.BlockNumber, error) { + if blockNumber == "latest" { + return rpc.LatestBlockNumber, nil + } + + if blockNumber == "earliest" { + return rpc.EarliestBlockNumber, nil + } + + if blockNumber == "pending" { + return rpc.PendingBlockNumber, nil + } + + if blockNumber == "finalized" { + return rpc.FinalizedBlockNumber, nil + } + + if blockNumber == "safe" { + return rpc.SafeBlockNumber, nil + } + + return rpc.BlockNumber(0), errors.New("invalid block number") +} From 927db07fc4d84158a35350eb455b7c4dd8dd024d Mon Sep 17 00:00:00 2001 From: marcello33 Date: Wed, 13 Nov 2024 14:04:19 +0100 Subject: [PATCH 2/7] chg: switch blockNumber --- internal/cli/server/api_service.go | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/internal/cli/server/api_service.go b/internal/cli/server/api_service.go index 670cb65dbe..a9dba46a1f 100644 --- a/internal/cli/server/api_service.go +++ b/internal/cli/server/api_service.go @@ -101,25 +101,18 @@ func (s *Server) BorBlockReceipt(ctx context.Context, req *protobor.ReceiptReque } func getRpcBlockNumberFromString(blockNumber string) (rpc.BlockNumber, error) { - if blockNumber == "latest" { + switch blockNumber { + case "latest": return rpc.LatestBlockNumber, nil - } - - if blockNumber == "earliest" { + case "earliest": return rpc.EarliestBlockNumber, nil - } - - if blockNumber == "pending" { + case "pending": return rpc.PendingBlockNumber, nil - } - - if blockNumber == "finalized" { + case "finalized": return rpc.FinalizedBlockNumber, nil - } - - if blockNumber == "safe" { + case "safe": return rpc.SafeBlockNumber, nil + default: + return rpc.BlockNumber(0), errors.New("invalid block number") } - - return rpc.BlockNumber(0), errors.New("invalid block number") } From baa8a2bb6354f2d86c9c4f5c3b2250cdab823564 Mon Sep 17 00:00:00 2001 From: marcello33 Date: Wed, 13 Nov 2024 14:45:41 +0100 Subject: [PATCH 3/7] cgh: fix rpc blockNumber and bash warnings --- integration-tests/bor_health.sh | 4 ++-- integration-tests/smoke_test.sh | 14 +++++++------- internal/cli/server/api_service.go | 7 ++++++- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/integration-tests/bor_health.sh b/integration-tests/bor_health.sh index 0791d415fd..469992098c 100644 --- a/integration-tests/bor_health.sh +++ b/integration-tests/bor_health.sh @@ -11,5 +11,5 @@ do fi done -echo $peers -echo $block +echo "$peers" +echo "$block" diff --git a/integration-tests/smoke_test.sh b/integration-tests/smoke_test.sh index 4541ec5d4d..f5354ffde7 100644 --- a/integration-tests/smoke_test.sh +++ b/integration-tests/smoke_test.sh @@ -18,8 +18,8 @@ do exit 1 fi - if (( $balance > $balanceInit )); then - if [ $stateSyncFound != "true" ]; then + if (( balance > balanceInit )); then + if [ "$stateSyncFound" != "true" ]; then stateSyncTime=$(( SECONDS - start_time )) stateSyncFound="true" fi @@ -27,18 +27,18 @@ do checkpointID=$(curl -sL http://localhost:1317/checkpoints/latest | jq .result.id) - if [ $checkpointID != "null" ]; then - if [ $checkpointFound != "true" ]; then + if [ "$checkpointID" != "null" ]; then + if [ "$checkpointFound" != "true" ]; then checkpointTime=$(( SECONDS - start_time )) checkpointFound="true" fi fi - if [ $stateSyncFound == "true" ] && [ $checkpointFound == "true" ]; then + if [ "$stateSyncFound" == "true" ] && [ "$checkpointFound" == "true" ]; then break fi done echo "Both state sync and checkpoint went through. All tests have passed!" -echo "Time taken for state sync: $(printf '%02dm:%02ds\n' $(($stateSyncTime%3600/60)) $(($stateSyncTime%60)))" -echo "Time taken for checkpoint: $(printf '%02dm:%02ds\n' $(($checkpointTime%3600/60)) $(($checkpointTime%60)))" +echo "Time taken for state sync: $(printf '%02dm:%02ds\n' $((stateSyncTime%3600/60)) $((stateSyncTime%60)))" +echo "Time taken for checkpoint: $(printf '%02dm:%02ds\n' $((checkpointTime%3600/60)) $((checkpointTime%60)))" diff --git a/internal/cli/server/api_service.go b/internal/cli/server/api_service.go index a9dba46a1f..78ddade84d 100644 --- a/internal/cli/server/api_service.go +++ b/internal/cli/server/api_service.go @@ -3,6 +3,7 @@ package server import ( "context" "errors" + "strconv" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/rpc" @@ -113,6 +114,10 @@ func getRpcBlockNumberFromString(blockNumber string) (rpc.BlockNumber, error) { case "safe": return rpc.SafeBlockNumber, nil default: - return rpc.BlockNumber(0), errors.New("invalid block number") + blckNum, err := strconv.ParseUint(blockNumber, 10, 64) + if err != nil { + return rpc.BlockNumber(0), errors.New("invalid block number") + } + return rpc.BlockNumber(blckNum), nil } } From 32138d70ad37ceff1cf7f4ed10f2ddaca35c24a2 Mon Sep 17 00:00:00 2001 From: marcello33 Date: Wed, 13 Nov 2024 14:58:18 +0100 Subject: [PATCH 4/7] cgh: fix possible out of range on blockNumber --- internal/cli/server/api_service.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/internal/cli/server/api_service.go b/internal/cli/server/api_service.go index 78ddade84d..2861cf3a9d 100644 --- a/internal/cli/server/api_service.go +++ b/internal/cli/server/api_service.go @@ -118,6 +118,9 @@ func getRpcBlockNumberFromString(blockNumber string) (rpc.BlockNumber, error) { if err != nil { return rpc.BlockNumber(0), errors.New("invalid block number") } + if blckNum > uint64(^uint32(0)) { + return rpc.BlockNumber(0), errors.New("block number out of range") + } return rpc.BlockNumber(blckNum), nil } } From 0634d11d8652cbd57571e47a63b3de15157e5090 Mon Sep 17 00:00:00 2001 From: marcello33 Date: Wed, 13 Nov 2024 15:01:55 +0100 Subject: [PATCH 5/7] cgh: use geth for maxInt --- internal/cli/server/api_service.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/internal/cli/server/api_service.go b/internal/cli/server/api_service.go index 2861cf3a9d..b5469c3752 100644 --- a/internal/cli/server/api_service.go +++ b/internal/cli/server/api_service.go @@ -5,6 +5,7 @@ import ( "errors" "strconv" + "github.com/ethereum/go-ethereum/common/math" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/rpc" @@ -118,7 +119,7 @@ func getRpcBlockNumberFromString(blockNumber string) (rpc.BlockNumber, error) { if err != nil { return rpc.BlockNumber(0), errors.New("invalid block number") } - if blckNum > uint64(^uint32(0)) { + if blckNum > math.MaxInt64 { return rpc.BlockNumber(0), errors.New("block number out of range") } return rpc.BlockNumber(blckNum), nil From 10d9594b52ee02dcdebc1a0284993b6a89046c72 Mon Sep 17 00:00:00 2001 From: marcello33 Date: Thu, 14 Nov 2024 07:19:13 +0100 Subject: [PATCH 6/7] chg: try blocks hex ecnoding --- internal/cli/server/api_service.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/cli/server/api_service.go b/internal/cli/server/api_service.go index b5469c3752..3781b6b45d 100644 --- a/internal/cli/server/api_service.go +++ b/internal/cli/server/api_service.go @@ -3,8 +3,8 @@ package server import ( "context" "errors" - "strconv" + "github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/common/math" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/rpc" @@ -115,7 +115,7 @@ func getRpcBlockNumberFromString(blockNumber string) (rpc.BlockNumber, error) { case "safe": return rpc.SafeBlockNumber, nil default: - blckNum, err := strconv.ParseUint(blockNumber, 10, 64) + blckNum, err := hexutil.DecodeUint64(blockNumber) if err != nil { return rpc.BlockNumber(0), errors.New("invalid block number") } From b6efb3f10213157725d682a3a85358a64011e667 Mon Sep 17 00:00:00 2001 From: marcello33 Date: Thu, 14 Nov 2024 11:42:54 +0100 Subject: [PATCH 7/7] chg: bump polyproto --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index f8258fb3d8..967a73dbef 100644 --- a/go.mod +++ b/go.mod @@ -65,7 +65,7 @@ require ( github.com/kylelemons/godebug v1.1.0 github.com/maticnetwork/crand v1.0.2 github.com/maticnetwork/heimdall v1.0.7 - github.com/maticnetwork/polyproto v0.0.4-0.20241113101917-6744479e4d59 + github.com/maticnetwork/polyproto v0.0.4 github.com/mattn/go-colorable v0.1.13 github.com/mattn/go-isatty v0.0.20 github.com/mitchellh/cli v1.1.5 diff --git a/go.sum b/go.sum index b60ff0b105..491f81cc7a 100644 --- a/go.sum +++ b/go.sum @@ -1707,8 +1707,8 @@ github.com/maticnetwork/heimdall v1.0.4/go.mod h1:Xh7KFvtbs/SVNjOI8IgYmk6JdzYx89 github.com/maticnetwork/heimdall v1.0.7 h1:QStn+hbZKxfE+PqecaorA/uATDPuQoi+U9Z7IIonb60= 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.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/polyproto v0.0.4 h1:qQ/qwcO6UNGS4mJlzlLJn1AUMfJK9Rqmf1v+KJgnPsk= +github.com/maticnetwork/polyproto v0.0.4/go.mod h1:e1mU2EXSwEpn5jM7GfNwu3AupsV6WAGoPFFfswXOF0o= github.com/maticnetwork/tendermint v0.33.0 h1:f+vORM02BoUOlCvnu3Zjw5rv6l6JSNVchWjH03rUuR8= github.com/maticnetwork/tendermint v0.33.0/go.mod h1:D2fcnxGk6bje+LoPwImuKSSYLiK7/G06IynGNDSEcJk= github.com/matryer/try v0.0.0-20161228173917-9ac251b645a2/go.mod h1:0KeJpeMD6o+O4hW7qJOT7vyQPKrWmj26uf5wMc/IiIs=