diff --git a/cmd/puppeth/wizard_genesis.go b/cmd/puppeth/wizard_genesis.go index 799384985aa0..89423e8b55d4 100644 --- a/cmd/puppeth/wizard_genesis.go +++ b/cmd/puppeth/wizard_genesis.go @@ -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) } diff --git a/consensus/clique/clique.go b/consensus/clique/clique.go index 51acff97093c..f5b4ef19722f 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)) @@ -718,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() + 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 +} diff --git a/params/config.go b/params/config.go index 868ed1ff8431..e6ce0828d54d 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.