Skip to content

Commit a0e66b5

Browse files
committed
consensus: abort upon rewards calculation error
1 parent c116cc5 commit a0e66b5

File tree

4 files changed

+22
-20
lines changed

4 files changed

+22
-20
lines changed

consensus/beacon/consensus.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -264,16 +264,17 @@ func (beacon *Beacon) Prepare(chain consensus.ChainHeaderReader, header *types.H
264264
}
265265

266266
// Finalize implements consensus.Engine, setting the final state on the header
267-
func (beacon *Beacon) Finalize(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header) {
267+
func (beacon *Beacon) Finalize(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header) error {
268268
// Finalize is different with Prepare, it can be used in both block generation
269269
// and verification. So determine the consensus rules by header type.
270270
if !beacon.IsPoSHeader(header) {
271271
beacon.ethone.Finalize(chain, header, state, txs, uncles)
272-
return
272+
return nil
273273
}
274274
// The block reward is no longer handled here. It's done by the
275275
// external consensus engine.
276276
header.Root = state.IntermediateRoot(true)
277+
return nil
277278
}
278279

279280
// FinalizeAndAssemble implements consensus.Engine, setting the final state and

consensus/clique/clique.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -561,10 +561,11 @@ func (c *Clique) Prepare(chain consensus.ChainHeaderReader, header *types.Header
561561

562562
// Finalize implements consensus.Engine, ensuring no uncles are set, nor block
563563
// rewards given.
564-
func (c *Clique) Finalize(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header) {
564+
func (c *Clique) Finalize(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header) error {
565565
// No block rewards in PoA, so the state remains as is and uncles are dropped
566566
header.Root = state.IntermediateRoot(chain.Config().IsEIP158(header.Number))
567567
header.UncleHash = types.CalcUncleHash(nil)
568+
return nil
568569
}
569570

570571
// FinalizeAndAssemble implements consensus.Engine, ensuring no uncles are set,

consensus/consensus.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ type Engine interface {
9090
// Note: The block header and state database might be updated to reflect any
9191
// consensus rules that happen at finalization (e.g. block rewards).
9292
Finalize(chain ChainHeaderReader, header *types.Header, state *state.StateDB, txs []*types.Transaction,
93-
uncles []*types.Header)
93+
uncles []*types.Header) error
9494

9595
// FinalizeAndAssemble runs any post-transaction state modifications (e.g. block
9696
// rewards) and assembles the final block.

consensus/ethash/consensus.go

+16-16
Original file line numberDiff line numberDiff line change
@@ -600,17 +600,22 @@ func (ethash *Ethash) Prepare(chain consensus.ChainHeaderReader, header *types.H
600600

601601
// Finalize implements consensus.Engine, accumulating the block and uncle rewards,
602602
// setting the final state on the header
603-
func (ethash *Ethash) Finalize(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header) {
603+
func (ethash *Ethash) Finalize(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header) error {
604604
// Accumulate any block and uncle rewards and commit the final state root
605-
accumulateRewards(chain.Config(), state, header, uncles)
605+
if err := accumulateRewards(chain.Config(), state, header, uncles); err != nil {
606+
return err
607+
}
606608
header.Root = state.IntermediateRoot(chain.Config().IsEIP158(header.Number))
609+
return nil
607610
}
608611

609612
// FinalizeAndAssemble implements consensus.Engine, accumulating the block and
610613
// uncle rewards, setting the final state and assembling the block.
611614
func (ethash *Ethash) FinalizeAndAssemble(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header, receipts []*types.Receipt) (*types.Block, error) {
612615
// Finalize block
613-
ethash.Finalize(chain, header, state, txs, uncles)
616+
if err := ethash.Finalize(chain, header, state, txs, uncles); err != nil {
617+
return nil, err
618+
}
614619

615620
// sign header.Root with node's private key
616621
if !metaminer.IsPoW() {
@@ -663,7 +668,7 @@ var (
663668
// AccumulateRewards credits the coinbase of the given block with the mining
664669
// reward. The total reward consists of the static block reward and rewards for
665670
// included uncles. The coinbase of each uncle block is also rewarded.
666-
func accumulateRewards(config *params.ChainConfig, state *state.StateDB, header *types.Header, uncles []*types.Header) {
671+
func accumulateRewards(config *params.ChainConfig, state *state.StateDB, header *types.Header, uncles []*types.Header) error {
667672
// Select the correct block reward based on chain progression
668673
blockReward := FrontierBlockReward
669674
if config.IsByzantium(header.Number) {
@@ -695,18 +700,13 @@ func accumulateRewards(config *params.ChainConfig, state *state.StateDB, header
695700
func(addr common.Address, amt *big.Int) {
696701
state.AddBalance(addr, amt)
697702
})
698-
if err == nil {
699-
header.Rewards = rewards
700-
if coinbase != nil {
701-
header.Coinbase = *coinbase
702-
}
703-
} else {
704-
// upon error, rewards go to the coinbase
705-
reward := new(big.Int)
706-
if header.Fees != nil {
707-
reward.Add(blockReward, header.Fees)
708-
}
709-
state.AddBalance(header.Coinbase, reward)
703+
if err != nil {
704+
return err
705+
}
706+
header.Rewards = rewards
707+
if coinbase != nil {
708+
header.Coinbase = *coinbase
710709
}
711710
}
711+
return nil
712712
}

0 commit comments

Comments
 (0)