2022-02-03 05:28:23 +00:00
|
|
|
package blockchain
|
|
|
|
|
|
|
|
import (
|
2022-02-09 18:15:29 +00:00
|
|
|
"math/big"
|
|
|
|
|
2022-02-03 05:28:23 +00:00
|
|
|
"github.com/holiman/uint256"
|
2022-02-09 18:15:29 +00:00
|
|
|
"github.com/pkg/errors"
|
2022-02-03 05:28:23 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/config/params"
|
|
|
|
)
|
|
|
|
|
|
|
|
// validates terminal pow block by comparing own total difficulty with parent's total difficulty.
|
|
|
|
//
|
|
|
|
// def is_valid_terminal_pow_block(block: PowBlock, parent: PowBlock) -> bool:
|
|
|
|
// is_total_difficulty_reached = block.total_difficulty >= TERMINAL_TOTAL_DIFFICULTY
|
|
|
|
// is_parent_total_difficulty_valid = parent.total_difficulty < TERMINAL_TOTAL_DIFFICULTY
|
|
|
|
// return is_total_difficulty_reached and is_parent_total_difficulty_valid
|
2022-02-09 18:15:29 +00:00
|
|
|
func validTerminalPowBlock(currentDifficulty *uint256.Int, parentDifficulty *uint256.Int) (bool, error) {
|
|
|
|
b, ok := new(big.Int).SetString(params.BeaconConfig().TerminalTotalDifficulty, 10)
|
|
|
|
if !ok {
|
|
|
|
return false, errors.New("failed to parse terminal total difficulty")
|
|
|
|
}
|
|
|
|
ttd, of := uint256.FromBig(b)
|
|
|
|
if of {
|
|
|
|
return false, errors.New("overflow terminal total difficulty")
|
|
|
|
}
|
2022-02-03 05:28:23 +00:00
|
|
|
totalDifficultyReached := currentDifficulty.Cmp(ttd) >= 0
|
|
|
|
parentTotalDifficultyValid := ttd.Cmp(parentDifficulty) > 0
|
2022-02-09 18:15:29 +00:00
|
|
|
return totalDifficultyReached && parentTotalDifficultyValid, nil
|
2022-02-03 05:28:23 +00:00
|
|
|
}
|