From cbe54fe3f97ed1e721fea19d3a46515ddfd02061 Mon Sep 17 00:00:00 2001 From: terence tsao Date: Wed, 13 Apr 2022 19:47:04 -0700 Subject: [PATCH] Handle nil execution block response when logging TTD (#10502) --- .../powchain/check_transition_config.go | 11 +++++-- .../powchain/check_transition_config_test.go | 32 +++++++++++++++++++ .../engine/v1/json_marshal_unmarshal_test.go | 8 +++++ 3 files changed, 48 insertions(+), 3 deletions(-) diff --git a/beacon-chain/powchain/check_transition_config.go b/beacon-chain/powchain/check_transition_config.go index 959457506..aebc38985 100644 --- a/beacon-chain/powchain/check_transition_config.go +++ b/beacon-chain/powchain/check_transition_config.go @@ -120,11 +120,16 @@ func (s *Service) handleExchangeConfigurationError(err error) { // Logs the terminal total difficulty status. func (s *Service) logTtdStatus(ctx context.Context, ttd *uint256.Int) (bool, error) { latest, err := s.LatestExecutionBlock(ctx) - if err != nil { + switch { + case errors.Is(err, hexutil.ErrEmptyString): + return false, nil + case err != nil: return false, err - } - if latest == nil { + case latest == nil: return false, errors.New("latest block is nil") + case latest.TotalDifficulty == "": + return false, nil + default: } latestTtd, err := hexutil.DecodeBig(latest.TotalDifficulty) if err != nil { diff --git a/beacon-chain/powchain/check_transition_config_test.go b/beacon-chain/powchain/check_transition_config_test.go index 5eac36bfc..659584ff9 100644 --- a/beacon-chain/powchain/check_transition_config_test.go +++ b/beacon-chain/powchain/check_transition_config_test.go @@ -190,6 +190,38 @@ func TestService_logTtdStatus(t *testing.T) { require.Equal(t, false, reached) } +func TestService_logTtdStatus_NotSyncedClient(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + defer func() { + require.NoError(t, r.Body.Close()) + }() + + resp := (*pb.ExecutionBlock)(nil) // Nil response when a client is not synced + respJSON := map[string]interface{}{ + "jsonrpc": "2.0", + "id": 1, + "result": resp, + } + require.NoError(t, json.NewEncoder(w).Encode(respJSON)) + })) + defer srv.Close() + + rpcClient, err := rpc.DialHTTP(srv.URL) + require.NoError(t, err) + defer rpcClient.Close() + + service := &Service{ + cfg: &config{}, + } + service.rpcClient = rpcClient + + ttd := new(uint256.Int) + reached, err := service.logTtdStatus(context.Background(), ttd.SetUint64(24343)) + require.NoError(t, err) + require.Equal(t, false, reached) +} + func emptyPayload() *pb.ExecutionPayload { return &pb.ExecutionPayload{ ParentHash: make([]byte, fieldparams.RootLength), diff --git a/proto/engine/v1/json_marshal_unmarshal_test.go b/proto/engine/v1/json_marshal_unmarshal_test.go index e8a4cd5f3..927e3f170 100644 --- a/proto/engine/v1/json_marshal_unmarshal_test.go +++ b/proto/engine/v1/json_marshal_unmarshal_test.go @@ -5,6 +5,7 @@ import ( "math/big" "testing" + "github.com/ethereum/go-ethereum/common/hexutil" fieldparams "github.com/prysmaticlabs/prysm/config/fieldparams" "github.com/prysmaticlabs/prysm/config/params" "github.com/prysmaticlabs/prysm/encoding/bytesutil" @@ -173,6 +174,13 @@ func TestJsonMarshalUnmarshal(t *testing.T) { require.DeepEqual(t, [][]byte{[]byte("hi")}, payloadPb.Transactions) require.DeepEqual(t, [][]byte{[]byte("bye")}, payloadPb.Uncles) }) + t.Run("nil execution block", func(t *testing.T) { + jsonPayload := (*enginev1.ExecutionBlock)(nil) + enc, err := json.Marshal(jsonPayload) + require.NoError(t, err) + payloadPb := &enginev1.ExecutionBlock{} + require.ErrorIs(t, hexutil.ErrEmptyString, json.Unmarshal(enc, payloadPb)) + }) } func TestPayloadIDBytes_MarshalUnmarshalJSON(t *testing.T) {