Fixed minor warning of go lint and rename checkpoint config parameter.

This commit is contained in:
AnilChinchawale 2018-07-15 16:47:41 +05:30
parent 1e59069d3a
commit 80d66cb264
3 changed files with 74 additions and 12 deletions

View file

@ -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")
}
}
}

View file

@ -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
}

View file

@ -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)}
}
}