From 4b3364ac6bc5e2c13a73f727dca53231d859a776 Mon Sep 17 00:00:00 2001 From: terence tsao Date: Wed, 30 Mar 2022 20:05:23 -0700 Subject: [PATCH] Log terminal total difficulty status (#10457) * Log terminal total diff status * Update check_transition_config.go * Update BUILD.bazel * Update check_transition_config.go * Update check_transition_config.go * Update check_transition_config.go --- beacon-chain/powchain/BUILD.bazel | 1 + .../powchain/check_transition_config.go | 36 ++++++++++++++++++- .../powchain/check_transition_config_test.go | 19 ++++++++++ 3 files changed, 55 insertions(+), 1 deletion(-) diff --git a/beacon-chain/powchain/BUILD.bazel b/beacon-chain/powchain/BUILD.bazel index 6d29841ac..93b870939 100644 --- a/beacon-chain/powchain/BUILD.bazel +++ b/beacon-chain/powchain/BUILD.bazel @@ -120,6 +120,7 @@ go_test( "@com_github_ethereum_go_ethereum//common/hexutil:go_default_library", "@com_github_ethereum_go_ethereum//core/types:go_default_library", "@com_github_ethereum_go_ethereum//trie:go_default_library", + "@com_github_holiman_uint256//:go_default_library", "@com_github_prometheus_client_golang//prometheus:go_default_library", "@com_github_prysmaticlabs_eth2_types//:go_default_library", "@com_github_sirupsen_logrus//:go_default_library", diff --git a/beacon-chain/powchain/check_transition_config.go b/beacon-chain/powchain/check_transition_config.go index cf82b5bb9..cd0e1bcb4 100644 --- a/beacon-chain/powchain/check_transition_config.go +++ b/beacon-chain/powchain/check_transition_config.go @@ -7,12 +7,14 @@ import ( "math/big" "time" + "github.com/ethereum/go-ethereum/common/hexutil" "github.com/holiman/uint256" "github.com/prysmaticlabs/prysm/beacon-chain/core/blocks" statefeed "github.com/prysmaticlabs/prysm/beacon-chain/core/feed/state" v1 "github.com/prysmaticlabs/prysm/beacon-chain/powchain/engine-api-client/v1" "github.com/prysmaticlabs/prysm/config/params" pb "github.com/prysmaticlabs/prysm/proto/engine/v1" + "github.com/sirupsen/logrus" ) var ( @@ -58,6 +60,7 @@ func (s *Service) checkTransitionConfiguration( // This serves as a heartbeat to ensure the execution client and Prysm are ready for the // Bellatrix hard-fork transition. ticker := time.NewTicker(checkTransitionPollingInterval) + hasTtdReached := false defer ticker.Stop() sub := s.cfg.stateNotifier.StateFeed().Subscribe(blockNotifications) defer sub.Unsubscribe() @@ -77,9 +80,17 @@ func (s *Service) checkTransitionConfiguration( log.Debug("PoS transition is complete, no longer checking for configuration changes") return } - case <-ticker.C: + case tm := <-ticker.C: + ctx, cancel := context.WithDeadline(ctx, tm.Add(v1.DefaultTimeout)) err = s.engineAPIClient.ExchangeTransitionConfiguration(ctx, cfg) s.handleExchangeConfigurationError(err) + if !hasTtdReached { + hasTtdReached, err = s.logTtdStatus(ctx, ttd) + if err != nil { + log.WithError(err).Error("Could not log ttd status") + } + } + cancel() } } } @@ -104,3 +115,26 @@ func (s *Service) handleExchangeConfigurationError(err error) { } log.WithError(err).Error("Could not check configuration values between execution and consensus client") } + +// Logs the terminal total difficulty status. +func (s *Service) logTtdStatus(ctx context.Context, ttd *uint256.Int) (bool, error) { + latest, err := s.engineAPIClient.LatestExecutionBlock(ctx) + if err != nil { + return false, err + } + if latest == nil { + return false, errors.New("latest block is nil") + } + latestTtd, err := hexutil.DecodeBig(latest.TotalDifficulty) + if err != nil { + return false, err + } + if latestTtd.Cmp(ttd.ToBig()) >= 0 { + return true, nil + } + log.WithFields(logrus.Fields{ + "latestDifficulty": latestTtd.String(), + "terminalDifficulty": ttd.ToBig().String(), + }).Info("terminal difficulty has not been reached yet") + return false, nil +} diff --git a/beacon-chain/powchain/check_transition_config_test.go b/beacon-chain/powchain/check_transition_config_test.go index d8468dc7c..cf3c4c9b0 100644 --- a/beacon-chain/powchain/check_transition_config_test.go +++ b/beacon-chain/powchain/check_transition_config_test.go @@ -6,6 +6,7 @@ import ( "testing" "time" + "github.com/holiman/uint256" mockChain2 "github.com/prysmaticlabs/prysm/beacon-chain/blockchain/testing" statefeed "github.com/prysmaticlabs/prysm/beacon-chain/core/feed/state" v1 "github.com/prysmaticlabs/prysm/beacon-chain/powchain/engine-api-client/v1" @@ -109,6 +110,24 @@ func TestService_handleExchangeConfigurationError(t *testing.T) { require.LogsContain(t, hook, "Could not check configuration values") }) } + +func TestService_logTtdStatus(t *testing.T) { + srv := &Service{} + srv.engineAPIClient = &mocks.EngineClient{ExecutionBlock: &enginev1.ExecutionBlock{TotalDifficulty: "0x12345678"}} + ttd := new(uint256.Int) + reached, err := srv.logTtdStatus(context.Background(), ttd.SetUint64(24343)) + require.NoError(t, err) + require.Equal(t, true, reached) + + reached, err = srv.logTtdStatus(context.Background(), ttd.SetUint64(323423484)) + require.NoError(t, err) + require.Equal(t, false, reached) + + srv.engineAPIClient = &mocks.EngineClient{ErrLatestExecBlock: errors.New("something went wrong")} + _, err = srv.logTtdStatus(context.Background(), ttd.SetUint64(24343)) + require.ErrorContains(t, "something went wrong", err) +} + func emptyPayload() *enginev1.ExecutionPayload { return &enginev1.ExecutionPayload{ ParentHash: make([]byte, fieldparams.RootLength),