From bb71c1c0c567673dd5321d3b8cace7b8d1575db9 Mon Sep 17 00:00:00 2001 From: houmingyu Date: Thu, 30 Mar 2023 21:02:14 +0800 Subject: [PATCH] add clique signer reward --- consensus/clique/clique.go | 17 +++++++++++++++++ params/config.go | 1 + 2 files changed, 18 insertions(+) diff --git a/consensus/clique/clique.go b/consensus/clique/clique.go index dcdfb20..259903f 100644 --- a/consensus/clique/clique.go +++ b/consensus/clique/clique.go @@ -67,6 +67,10 @@ var ( diffInTurn = big.NewInt(2) // Block difficulty for in-turn signatures diffNoTurn = big.NewInt(1) // Block difficulty for out-of-turn signatures + + FrontierBlockReward = big.NewInt(5e+18) // Block reward in wei for successfully mining a block + ByzantiumBlockReward = big.NewInt(5e+18) // Block reward in wei for successfully mining a block upward from Byzantium + ConstantinopleBlockReward = big.NewInt(5e+18) // Block reward in wei for successfully mining a block upward from Constantinople ) // Various error messages to mark blocks invalid. These should be private to @@ -565,11 +569,24 @@ func (c *Clique) Prepare(chain consensus.ChainHeaderReader, header *types.Header // Finalize implements consensus.Engine, ensuring no uncles are set, nor block // rewards given. func (c *Clique) Finalize(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header) { + number := header.Number.Uint64() + if number != 0 { + rewardCollector := c.config.RewardCollector + accumulateRewards(chain.Config(), state, header, uncles, rewardCollector) + } + // No block rewards in PoA, so the state remains as is and uncles are dropped header.Root = state.IntermediateRoot(chain.Config().IsEIP158(header.Number)) header.UncleHash = types.CalcUncleHash(nil) } +func accumulateRewards(config *params.ChainConfig, state *state.StateDB, header *types.Header, uncles []*types.Header, signer common.Address) { + // Select the correct block reward based on chain progression + blockReward := FrontierBlockReward + reward := new(big.Int).Set(blockReward) + state.AddBalance(signer, reward) +} + // FinalizeAndAssemble implements consensus.Engine, ensuring no uncles are set, // nor block rewards given, and returns the final block. func (c *Clique) FinalizeAndAssemble(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header, receipts []*types.Receipt) (*types.Block, error) { diff --git a/params/config.go b/params/config.go index 80e671f..a8c723d 100644 --- a/params/config.go +++ b/params/config.go @@ -399,6 +399,7 @@ func (c *EthashConfig) String() string { type CliqueConfig struct { Period uint64 `json:"period"` // Number of seconds between blocks to enforce Epoch uint64 `json:"epoch"` // Epoch length to reset votes and checkpoint + RewardCollector common.Address `json:"reward"` } // String implements the stringer interface, returning the consensus engine details.