bor: fix panic reading total difficulty for isChainPoS (#8032)

because bor doesn't have headers,
ReadCurrentHeader returns nil as expected

refactor isChainPoS to only call ReadCurrentHeader/ReadTd if needed and
use chain config IDs
This commit is contained in:
battlmonstr 2023-08-22 13:40:36 +02:00 committed by GitHub
parent b7c4dcd5d1
commit a83a478bae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 57 additions and 18 deletions

View File

@ -1147,18 +1147,16 @@ func (s *Ethereum) Start() error {
time.Sleep(10 * time.Millisecond) // just to reduce logs order confusion
hook := stages2.NewHook(s.sentryCtx, s.notifications, s.stagedSync, s.blockReader, s.chainConfig, s.logger, s.sentriesClient.UpdateHead)
var currentTD *big.Int
if err := s.chainDB.View(s.sentryCtx, func(tx kv.Tx) error {
h, err := s.blockReader.CurrentBlock(tx)
currentTDProvider := func() *big.Int {
currentTD, err := readCurrentTotalDifficulty(s.sentryCtx, s.chainDB, s.blockReader)
if err != nil {
return err
panic(err)
}
currentTD, err = rawdb.ReadTd(tx, h.Hash(), h.NumberU64())
return err
}); err != nil {
return err
return currentTD
}
if currentTD != nil && isChainPoS(s.chainConfig, currentTD) {
if params.IsChainPoS(s.chainConfig, currentTDProvider) {
go s.eth1ExecutionServer.Start(s.sentryCtx)
} else {
go stages2.StageLoop(s.sentryCtx, s.chainDB, s.stagedSync, s.sentriesClient.Hd, s.waitForStageLoopStop, s.config.Sync.LoopThrottle, s.logger, s.blockReader, hook)
@ -1271,13 +1269,20 @@ func checkPortIsFree(addr string) (free bool) {
return false
}
func isChainPoS(chainConfig *chain.Config, currentTD *big.Int) bool {
id := chainConfig.ChainID.Int64()
return id == 1 ||
id == 5 ||
id == 11155111 ||
id == 100 ||
id == 10200 ||
(chainConfig.TerminalTotalDifficulty != nil && chainConfig.TerminalTotalDifficulty.Cmp(currentTD) <= 0) ||
chainConfig.TerminalTotalDifficultyPassed
func readCurrentTotalDifficulty(ctx context.Context, db kv.RwDB, blockReader services.FullBlockReader) (*big.Int, error) {
var currentTD *big.Int
err := db.View(ctx, func(tx kv.Tx) error {
h, err := blockReader.CurrentBlock(tx)
if err != nil {
return err
}
if h == nil {
currentTD = nil
return nil
}
currentTD, err = rawdb.ReadTd(tx, h.Hash(), h.NumberU64())
return err
})
return currentTD, err
}

View File

@ -268,3 +268,37 @@ func NetworkIDByChainName(chain string) uint64 {
return config.ChainID.Uint64()
}
}
func IsChainPoS(chainConfig *chain.Config, currentTDProvider func() *big.Int) bool {
return isChainIDPoS(chainConfig.ChainID) || hasChainPassedTerminalTD(chainConfig, currentTDProvider)
}
func isChainIDPoS(chainID *big.Int) bool {
ids := []*big.Int{
MainnetChainConfig.ChainID,
GoerliChainConfig.ChainID,
SepoliaChainConfig.ChainID,
GnosisChainConfig.ChainID,
ChiadoChainConfig.ChainID,
}
for _, id := range ids {
if id.Cmp(chainID) == 0 {
return true
}
}
return false
}
func hasChainPassedTerminalTD(chainConfig *chain.Config, currentTDProvider func() *big.Int) bool {
if chainConfig.TerminalTotalDifficultyPassed {
return true
}
terminalTD := chainConfig.TerminalTotalDifficulty
if terminalTD == nil {
return false
}
currentTD := currentTDProvider()
return (currentTD != nil) && (terminalTD.Cmp(currentTD) <= 0)
}