From 0d59fa340801ec414dd5ff802bbd67bb8e66c6ec Mon Sep 17 00:00:00 2001 From: dinhln89 Date: Wed, 6 Jun 2018 15:36:03 +0700 Subject: [PATCH] Add feature calculate reward for signers at checkpoint block. --- cmd/puppeth/wizard_genesis.go | 2 +- consensus/clique/clique.go | 67 ++++++++++++++++++++++++++++------- params/config.go | 1 + 3 files changed, 57 insertions(+), 13 deletions(-) diff --git a/cmd/puppeth/wizard_genesis.go b/cmd/puppeth/wizard_genesis.go index 4c38963ffb..0135e027ea 100644 --- a/cmd/puppeth/wizard_genesis.go +++ b/cmd/puppeth/wizard_genesis.go @@ -105,7 +105,7 @@ func (w *wizard) makeGenesis() { fmt.Println() fmt.Println("How many blocks per checkpoint? (default = 990)") - genesis.Config.Clique.Epoch = uint64(w.readDefaultInt(990)) + genesis.Config.Clique.Checkpoint = uint64(w.readDefaultInt(990)) default: log.Crit("Invalid consensus engine choice", "choice", choice) diff --git a/consensus/clique/clique.go b/consensus/clique/clique.go index 01487b3d37..93c4e3fac6 100644 --- a/consensus/clique/clique.go +++ b/consensus/clique/clique.go @@ -40,6 +40,7 @@ import ( "github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rpc" "github.com/hashicorp/golang-lru" + "encoding/json" ) const ( @@ -605,18 +606,8 @@ func (c *Clique) Prepare(chain consensus.ChainReader, header *types.Header) erro func (c *Clique) Finalize(chain consensus.ChainReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header, receipts []*types.Receipt) (*types.Block, error) { // set block reward // FIXME: unit Ether could be too plump - parentHeader := chain.GetHeaderByHash(header.ParentHash) - if (parentHeader.Number.Uint64() > 0) { - chainReward := new(big.Int).SetUint64(chain.Config().Clique.Reward * params.Ether) - // Not reward for singer of genesis block. - reward := new(big.Int).Set(chainReward) - - parentSigner, err := ecrecover(parentHeader, c.signatures) - if err != nil { - return nil, err - } - - state.AddBalance(parentSigner, reward) + if err := c.accumulateRewards(chain, state, header); err != nil { + return nil, err } // No block rewards in PoA, so the state remains as is and uncles are dropped @@ -732,3 +723,55 @@ func (c *Clique) APIs(chain consensus.ChainReader) []rpc.API { Public: false, }} } + +func (c *Clique) accumulateRewards(chain consensus.ChainReader, state *state.StateDB, header *types.Header) (error) { + type rewardLog struct { + Sign uint64 `json:"sign"` + Reward float64 `json:"reward"` + } + + number := header.Number.Uint64() + checkpoint := chain.Config().Clique.Checkpoint + + if number > 0 && number%checkpoint == 0 { + // Not reward for singer of genesis block and only calculate reward at checkpoint block. + parentHeader := chain.GetHeaderByHash(header.ParentHash) + startBlockNumber := number - checkpoint + 1 + endBlockNumber := parentHeader.Number.Uint64() + signers := make(map[common.Address]*rewardLog) + totalSigner := uint64(0) + + for i := startBlockNumber; i <= endBlockNumber; i++ { + blockHeader := chain.GetHeaderByNumber(i) + if signer, err := ecrecover(blockHeader, c.signatures); err != nil { + return err + } else { + _, exist := signers[signer] + if exist { + signers[signer].Sign++ + } else { + signers[signer] = &rewardLog{1, 0} + } + totalSigner++ + } + } + + chainReward := new(big.Int).SetUint64(chain.Config().Clique.Reward * params.Ether) + // Update balance reward. + calcReward := new(big.Int) + for signer, log := range signers { + calcReward.Mul(chainReward, new(big.Int).SetUint64(log.Sign)) + calcReward.Div(calcReward, new(big.Int).SetUint64(totalSigner)) + log.Reward = float64(calcReward.Int64()) + + state.AddBalance(signer, calcReward) + } + jsonSigners, err := json.Marshal(signers) + if err != nil { + return err + } + log.Info("TOMO - Calculate reward at checkpoint", "startBlock", startBlockNumber, "endBlock", endBlockNumber, "signers", string(jsonSigners), "totalSigner", totalSigner, "totalReward", chainReward) + } + + return nil +} diff --git a/params/config.go b/params/config.go index 868ed1ff84..b47f9bf299 100644 --- a/params/config.go +++ b/params/config.go @@ -136,6 +136,7 @@ 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 Reward uint64 `json:"reward"` // Block reward - unit Ether + Checkpoint uint64 `json:"checkpoint"` // Checkpoint block for calculate rewards. } // String implements the stringer interface, returning the consensus engine details.