From 80d66cb2645efff0c92846dc52a8e97f1ea7780f Mon Sep 17 00:00:00 2001 From: AnilChinchawale Date: Sun, 15 Jul 2018 16:47:41 +0530 Subject: [PATCH] Fixed minor warning of go lint and rename checkpoint config parameter. --- cmd/puppeth/wizard_genesis.go | 8 +++- consensus/clique/clique.go | 69 ++++++++++++++++++++++++++++++++--- params/config.go | 9 +++-- 3 files changed, 74 insertions(+), 12 deletions(-) diff --git a/cmd/puppeth/wizard_genesis.go b/cmd/puppeth/wizard_genesis.go index e409c9ef9b..324224094f 100644 --- a/cmd/puppeth/wizard_genesis.go +++ b/cmd/puppeth/wizard_genesis.go @@ -75,7 +75,7 @@ func (w *wizard) makeGenesis() { fmt.Println() fmt.Println("How many Ethers should be rewarded to signer? (default = 0)") genesis.Config.Clique.Reward = uint64(w.readDefaultInt(0)) - + // We also need the initial list of signers fmt.Println() fmt.Println("Which accounts are allowed to seal? (mandatory at least one)") @@ -103,6 +103,10 @@ func (w *wizard) makeGenesis() { copy(genesis.ExtraData[32+i*common.AddressLength:], signer[:]) } + fmt.Println() + fmt.Println("How many blocks per checkpoint? (default = 990)") + genesis.Config.Clique.RewardCheckpoint = uint64(w.readDefaultInt(990)) + default: log.Crit("Invalid consensus engine choice", "choice", choice) } @@ -195,4 +199,4 @@ func (w *wizard) manageGenesis() { default: log.Error("That's not something I can do") } -} +} \ No newline at end of file diff --git a/consensus/clique/clique.go b/consensus/clique/clique.go index 67800c4f9a..6ad1d6c4bb 100644 --- a/consensus/clique/clique.go +++ b/consensus/clique/clique.go @@ -25,6 +25,7 @@ import ( "sync" "time" + "encoding/json" "github.com/ethereum/go-ethereum/accounts" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" @@ -39,7 +40,7 @@ import ( "github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rpc" - lru "github.com/hashicorp/golang-lru" + "github.com/hashicorp/golang-lru" ) const ( @@ -385,6 +386,11 @@ func position(list []common.Address, x common.Address) int { } func YourTurn(snap *Snapshot, header *types.Header, cur common.Address) (bool, error) { + if header.Number.Uint64() == 0 { + // Not check signer for genesis block. + return true, nil + } + pre, err := ecrecover(header, snap.sigcache) if err != nil { return false, err @@ -392,7 +398,7 @@ func YourTurn(snap *Snapshot, header *types.Header, cur common.Address) (bool, e preIndex := position(snap.signers(), pre) curIndex := position(snap.signers(), cur) log.Info("Debugging info", "number of masternodes", len(snap.signers()), "previous", pre, "position", preIndex, "current", cur, "position", curIndex) - return (preIndex+1)%len(snap.signers()) == curIndex || pre.String() == genesisCoinBase, nil + return (preIndex+1)%len(snap.signers()) == curIndex, nil } // snapshot retrieves the authorization snapshot at a given point in time. @@ -600,10 +606,9 @@ 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 - chainReward := new(big.Int).SetUint64(chain.Config().Clique.Reward * params.Ether) - - reward := new(big.Int).Set(chainReward) - state.AddBalance(header.Coinbase, 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 header.Root = state.IntermediateRoot(chain.Config().IsEIP158(header.Number)) @@ -717,4 +722,56 @@ func (c *Clique) APIs(chain consensus.ChainReader) []rpc.API { Service: &API{chain: chain, clique: c}, 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() + rCheckpoint := chain.Config().Clique.RewardCheckpoint + + if number > 0 && rCheckpoint > 0 && number%rCheckpoint == 0 { + // Not reward for singer of genesis block and only calculate reward at checkpoint block. + parentHeader := chain.GetHeaderByHash(header.ParentHash) + startBlockNumber := number - rCheckpoint + 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, rLog := range signers { + calcReward.Mul(chainReward, new(big.Int).SetUint64(rLog.Sign)) + calcReward.Div(calcReward, new(big.Int).SetUint64(totalSigner)) + rLog.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 } \ No newline at end of file diff --git a/params/config.go b/params/config.go index 868ed1ff84..998282b899 100644 --- a/params/config.go +++ b/params/config.go @@ -133,9 +133,10 @@ func (c *EthashConfig) String() string { // CliqueConfig is the consensus engine configs for proof-of-authority based sealing. 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 + 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 + RewardCheckpoint uint64 `json:"rewardCheckpoint"` // Checkpoint block for calculate rewards. } // String implements the stringer interface, returning the consensus engine details. @@ -336,4 +337,4 @@ func (c *ChainConfig) Rules(num *big.Int) Rules { chainId = new(big.Int) } return Rules{ChainId: new(big.Int).Set(chainId), IsHomestead: c.IsHomestead(num), IsEIP150: c.IsEIP150(num), IsEIP155: c.IsEIP155(num), IsEIP158: c.IsEIP158(num), IsByzantium: c.IsByzantium(num)} -} +} \ No newline at end of file