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
This commit is contained in:
terence tsao 2022-03-30 20:05:23 -07:00 committed by GitHub
parent 0df8d7f0c0
commit 4b3364ac6b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 55 additions and 1 deletions

View File

@ -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",

View File

@ -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
}

View File

@ -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),