Skip to content

Commit

Permalink
move check to finalize
Browse files Browse the repository at this point in the history
  • Loading branch information
manav2401 committed Feb 6, 2025
1 parent 22fdfb3 commit 7c8937f
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 41 deletions.
6 changes: 3 additions & 3 deletions consensus/beacon/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,10 +349,9 @@ func (beacon *Beacon) Prepare(chain consensus.ChainHeaderReader, header *types.H
}

// Finalize implements consensus.Engine and processes withdrawals on top.
func (beacon *Beacon) Finalize(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, body *types.Body) {
func (beacon *Beacon) Finalize(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, body *types.Body) error {
if !beacon.IsPoSHeader(header) {
beacon.ethone.Finalize(chain, header, state, body)
return
return beacon.ethone.Finalize(chain, header, state, body)
}
// Withdrawals processing.
for _, w := range body.Withdrawals {
Expand All @@ -362,6 +361,7 @@ func (beacon *Beacon) Finalize(chain consensus.ChainHeaderReader, header *types.
state.AddBalance(w.Address, amount, tracing.BalanceIncreaseWithdrawal)
}
// No block reward which is issued by consensus layer instead.
return nil
}

// FinalizeAndAssemble implements consensus.Engine, setting the final state and
Expand Down
73 changes: 38 additions & 35 deletions consensus/bor/bor.go
Original file line number Diff line number Diff line change
Expand Up @@ -464,36 +464,6 @@ func (c *Bor) verifyCascadingFields(chain consensus.ChainHeaderReader, header *t
return err
}

// Verify the validator list match the local contract
if IsSprintStart(number+1, c.config.CalculateSprint(number)) {
// Use parent block's hash to make the eth_call to fetch validators so that the state being
// used to make the call is of the same fork.
newValidators, err := c.spanner.GetCurrentValidatorsByBlockNrOrHash(context.Background(), rpc.BlockNumberOrHashWithHash(header.ParentHash, false), number+1)
if err != nil {
log.Error("[debug] err in fetching vals in bor ", "err", err)
return err
}

sort.Sort(valset.ValidatorsByAddress(newValidators))

headerVals, err := valset.ParseValidators(header.GetValidatorBytes(c.chainConfig))
if err != nil {
return err
}

if len(newValidators) != len(headerVals) {
log.Warn("Invalid validator set", "block number", number, "newValidators", newValidators, "headerVals", headerVals)
return errInvalidSpanValidators
}

for i, val := range newValidators {
if !bytes.Equal(val.HeaderBytes(), headerVals[i].HeaderBytes()) {
log.Warn("Invalid validator set", "block number", number, "index", i, "local validator", val, "header validator", headerVals[i])
return errInvalidSpanValidators
}
}
}

// verify the validator list in the last sprint block
if IsSprintStart(number, c.config.CalculateSprint(number)) {
parentValidatorBytes := parent.GetValidatorBytes(c.chainConfig)
Expand Down Expand Up @@ -819,32 +789,63 @@ func (c *Bor) Prepare(chain consensus.ChainHeaderReader, header *types.Header) e

// Finalize implements consensus.Engine, ensuring no uncles are set, nor block
// rewards given.
func (c *Bor) Finalize(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, body *types.Body) {
func (c *Bor) Finalize(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, body *types.Body) error {
headerNumber := header.Number.Uint64()
if body.Withdrawals != nil || header.WithdrawalsHash != nil {
return
return consensus.ErrUnexpectedWithdrawals
}

var (
stateSyncData []*types.StateSyncData
err error
)

number := header.Number.Uint64()
// Verify the validator list match the local contract
if IsSprintStart(number+1, c.config.CalculateSprint(number)) {
// Use parent block's hash to make the eth_call to fetch validators so that the state being
// used to make the call is of the same fork.
newValidators, err := c.spanner.GetCurrentValidatorsByBlockNrOrHash(context.Background(), rpc.BlockNumberOrHashWithHash(header.ParentHash, false), number+1)
if err != nil {
log.Error("[debug] err in fetching vals in bor ", "err", err)
return err
}

sort.Sort(valset.ValidatorsByAddress(newValidators))

headerVals, err := valset.ParseValidators(header.GetValidatorBytes(c.chainConfig))
if err != nil {
return err
}

if len(newValidators) != len(headerVals) {
log.Warn("Invalid validator set", "block number", number, "newValidators", newValidators, "headerVals", headerVals)
return errInvalidSpanValidators
}

for i, val := range newValidators {
if !bytes.Equal(val.HeaderBytes(), headerVals[i].HeaderBytes()) {
log.Warn("Invalid validator set", "block number", number, "index", i, "local validator", val, "header validator", headerVals[i])
return errInvalidSpanValidators
}
}
}

if IsSprintStart(headerNumber, c.config.CalculateSprint(headerNumber)) {
start := time.Now()
cx := statefull.ChainContext{Chain: chain, Bor: c}
// check and commit span
if err := c.checkAndCommitSpan(state, header, cx); err != nil {
log.Error("Error while committing span", "error", err)
return
return err
}

if c.HeimdallClient != nil {
// commit states
stateSyncData, err = c.CommitStates(state, header, cx)
if err != nil {
log.Error("Error while committing states", "error", err)
return
return err
}
}

Expand All @@ -853,7 +854,7 @@ func (c *Bor) Finalize(chain consensus.ChainHeaderReader, header *types.Header,

if err = c.changeContractCodeIfNeeded(headerNumber, state); err != nil {
log.Error("Error changing contract code", "error", err)
return
return err
}

// No block rewards in PoA, so the state remains as is and uncles are dropped
Expand All @@ -863,6 +864,8 @@ func (c *Bor) Finalize(chain consensus.ChainHeaderReader, header *types.Header,
// Set state sync data to blockchain
bc := chain.(*core.BlockChain)
bc.SetStateSync(stateSyncData)

return nil
}

func decodeGenesisAlloc(i interface{}) (types.GenesisAlloc, error) {
Expand Down
3 changes: 2 additions & 1 deletion consensus/clique/clique.go
Original file line number Diff line number Diff line change
Expand Up @@ -619,8 +619,9 @@ func (c *Clique) Prepare(chain consensus.ChainHeaderReader, header *types.Header

// Finalize implements consensus.Engine. There is no post-transaction
// consensus rules in clique, do nothing here.
func (c *Clique) Finalize(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, body *types.Body) {
func (c *Clique) Finalize(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, body *types.Body) error {
// No block rewards in PoA, so the state remains as is
return nil
}

// FinalizeAndAssemble implements consensus.Engine, ensuring no uncles are set,
Expand Down
2 changes: 1 addition & 1 deletion consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ type Engine interface {
//
// Note: The state database might be updated to reflect any consensus rules
// that happen at finalization (e.g. block rewards).
Finalize(chain ChainHeaderReader, header *types.Header, state *state.StateDB, body *types.Body)
Finalize(chain ChainHeaderReader, header *types.Header, state *state.StateDB, body *types.Body) error

// FinalizeAndAssemble runs any post-transaction state modifications (e.g. block
// rewards or process withdrawals) and assembles the final block.
Expand Down
4 changes: 3 additions & 1 deletion consensus/ethash/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -503,9 +503,11 @@ func (ethash *Ethash) Prepare(chain consensus.ChainHeaderReader, header *types.H
}

// Finalize implements consensus.Engine, accumulating the block and uncle rewards.
func (ethash *Ethash) Finalize(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, body *types.Body) {
func (ethash *Ethash) Finalize(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, body *types.Body) error {
// Accumulate any block and uncle rewards
accumulateRewards(chain.Config(), state, header, body.Uncles)

return nil
}

// FinalizeAndAssemble implements consensus.Engine, accumulating the block and
Expand Down

0 comments on commit 7c8937f

Please sign in to comment.