Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

consensus/beacon: check that only the latest pow block is valid ttd block #25187

Merged
merged 5 commits into from Jun 29, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 44 additions & 0 deletions consensus/beacon/consensus.go
Expand Up @@ -112,10 +112,12 @@ func (beacon *Beacon) VerifyHeaders(chain consensus.ChainHeaderReader, headers [
break
}
}

// All the headers have passed the transition point, use new rules.
if len(preHeaders) == 0 {
return beacon.verifyHeaders(chain, headers, nil)
}

// The transition point exists in the middle, separate the headers
// into two batches and apply different verification rules for them.
var (
Expand All @@ -130,6 +132,14 @@ func (beacon *Beacon) VerifyHeaders(chain consensus.ChainHeaderReader, headers [
oldDone, oldResult = beacon.ethone.VerifyHeaders(chain, preHeaders, preSeals)
newDone, newResult = beacon.verifyHeaders(chain, postHeaders, preHeaders[len(preHeaders)-1])
)
// verify that the headers are valid wrt. the terminal block.
index, err := beacon.verifyTerminalPoWBlock(chain, preHeaders)
if err != nil {
// Mark all subsequent headers with the error.
for i := index; i < len(preHeaders); i++ {
errors[i], done[i] = err, true
MariusVanDerWijden marked this conversation as resolved.
Show resolved Hide resolved
}
}
for {
for ; done[out]; out++ {
results <- errors[out]
Expand All @@ -154,6 +164,40 @@ func (beacon *Beacon) VerifyHeaders(chain consensus.ChainHeaderReader, headers [
return abort, results
}

// verifyTerminalPoWBlock verifies that the preHeaders confirm to the specification
// wrt. their total difficulty.
// It expects:
// - preHeaders to be at least 1 element
// - the parent of the header element to be stored in the chain correctly
// - the preHeaders to have a set difficulty
// - the last element to be the terminal block
func (beacon *Beacon) verifyTerminalPoWBlock(chain consensus.ChainHeaderReader, preHeaders []*types.Header) (int, error) {
var (
first = preHeaders[0]
last = preHeaders[len(preHeaders)-1]
)

td := chain.GetTd(first.ParentHash, first.Number.Uint64()-1)
if td == nil {
return 0, consensus.ErrUnknownAncestor
}
if len(preHeaders) != 1 {
for i, head := range preHeaders[:len(preHeaders)-2] {
td.Add(td, head.Difficulty)
// Check if the parent was already the terminal block
if td.Cmp(chain.Config().TerminalTotalDifficulty) >= 0 {
return i, consensus.ErrInvalidTerminalBlock
}
}
}

// Check that the last block is the terminal block
if td.Add(td, last.Difficulty).Cmp(chain.Config().TerminalTotalDifficulty) < 0 {
return len(preHeaders) - 1, consensus.ErrInvalidTerminalBlock
}
return 0, nil
}

// VerifyUncles verifies that the given block's uncles conform to the consensus
// rules of the Ethereum consensus engine.
func (beacon *Beacon) VerifyUncles(chain consensus.ChainReader, block *types.Block) error {
Expand Down
4 changes: 4 additions & 0 deletions consensus/errors.go
Expand Up @@ -34,4 +34,8 @@ var (
// ErrInvalidNumber is returned if a block's number doesn't equal its parent's
// plus one.
ErrInvalidNumber = errors.New("invalid block number")

// ErrInvalidTerminalBlock is returned if a block is invalid wrt. the terminal
// total difficulty.
ErrInvalidTerminalBlock = errors.New("invalid terminal block")
)