Add unit test for calculate reward for signers at reward checkpoint.

This commit is contained in:
parmarrushabh 2018-09-18 10:01:00 +05:30
parent 3584bcfa5f
commit 530f8e3656
3 changed files with 95 additions and 58 deletions

View file

@ -1,6 +1,7 @@
package contracts
import (
"encoding/json"
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
@ -68,9 +69,8 @@ func CreateTxSign(blockNumber *big.Int, nonce uint64, blockSigner common.Address
}
// Get signers signed for blockNumber from blockSigner contract.
func GetSignersFromContract(client bind.ContractBackend, blockNumber uint64) ([]common.Address, error) {
addr := common.HexToAddress(common.BlockSigners)
blockSigner, err := contract.NewBlockSigner(addr, client)
func GetSignersFromContract(addrBlockSigner common.Address, client bind.ContractBackend, blockNumber uint64) ([]common.Address, error) {
blockSigner, err := contract.NewBlockSigner(addrBlockSigner, client)
if err != nil {
log.Error("Fail get instance of blockSigner", "error", err)
return nil, err
@ -83,4 +83,66 @@ func GetSignersFromContract(client bind.ContractBackend, blockNumber uint64) ([]
}
return addrs, nil
}
// Calculate reward for reward checkpoint.
func GetRewardForCheckpoint(chainReward *big.Int, blockSignerAddr common.Address, number uint64, rCheckpoint uint64, client bind.ContractBackend) (map[common.Address]*big.Int, error) {
type rewardLog struct {
Sign uint64 `json:"sign"`
Reward *big.Int `json:"reward"`
}
// Not reward for singer of genesis block and only calculate reward at checkpoint block.
startBlockNumber := number - (rCheckpoint * 2) + 1
endBlockNumber := startBlockNumber + rCheckpoint - 1
signers := make(map[common.Address]*rewardLog)
totalSigner := uint64(0)
for i := startBlockNumber; i <= endBlockNumber; i++ {
addrs, err := GetSignersFromContract(blockSignerAddr, client, i)
if err != nil {
log.Error("Fail to get signers from smartcontract.", "error", err, "blockNumber", i)
return nil, err
}
// Filter duplicate address.
if len(addrs) > 0 {
addrSigners := make(map[common.Address]bool)
for _, addr := range addrs {
if _, ok := addrSigners[addr]; !ok {
addrSigners[addr] = true
}
}
for addr := range addrSigners {
_, exist := signers[addr]
if exist {
signers[addr].Sign++
} else {
signers[addr] = &rewardLog{1, new(big.Int)}
}
totalSigner++
}
}
}
resultSigners := make(map[common.Address]*big.Int)
// Add reward for signer.
calcReward := new(big.Int)
// Add reward for signers.
for signer, rLog := range signers {
calcReward.Mul(chainReward, new(big.Int).SetUint64(rLog.Sign))
calcReward.Div(calcReward, new(big.Int).SetUint64(totalSigner))
rLog.Reward = calcReward
resultSigners[signer] = calcReward
}
jsonSigners, err := json.Marshal(signers)
if err != nil {
log.Error("Fail to parse json signers", "error", err)
return nil, err
}
log.Info("Calculate reward at checkpoint", "startBlock", startBlockNumber, "endBlock", endBlockNumber, "signers", string(jsonSigners), "totalSigner", totalSigner, "totalReward", chainReward)
return resultSigners, nil
}

View file

@ -10,6 +10,7 @@ import (
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/params"
"math/big"
"math/rand"
"testing"
@ -77,4 +78,23 @@ func TestSendTxSign(t *testing.T) {
t.Error("Tx sign for block validators not match")
}
}
// Unit test for reward checkpoint.
rCheckpoint := uint64(10)
chainReward := new(big.Int).SetUint64(15 * params.Ether)
for i := uint64(0); i < 100; i++ {
if i > 0 && i%rCheckpoint == 0 && i-rCheckpoint > 0 {
signers, err := GetRewardForCheckpoint(chainReward, blockSignerAddr, i, rCheckpoint, backend)
if err != nil {
t.Errorf("Fail to get signers for reward checkpoint: %v", err)
}
rewards := new(big.Int)
for _, reward := range signers {
rewards.Add(rewards, reward)
}
if rewards.Cmp(chainReward) != 0 {
t.Errorf("Total reward not same reward checkpoint: %v - %v", chainReward, rewards)
}
}
}
}

View file

@ -25,7 +25,6 @@ import (
"sync"
"sync/atomic"
"encoding/json"
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
@ -190,73 +189,29 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
c := eth.engine.(*clique.Clique)
// Hook reward for clique validator.
c.HookReward = func(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
prevCheckpoint := number - rCheckpoint
if number > 0 && prevCheckpoint > 0 {
// Not reward for singer of genesis block and only calculate reward at checkpoint block.
startBlockNumber := number - (rCheckpoint * 2) + 1
endBlockNumber := startBlockNumber + rCheckpoint - 1
signers := make(map[common.Address]*rewardLog)
totalSigner := uint64(0)
if number > 0 && number-rCheckpoint > 0 {
// Get signers in blockSigner smartcontract.
client, err := contracts.GetEthClient(ctx)
if err != nil {
log.Error("Fail to connect IPC from blockSigner", "error", err)
return err
}
for i := startBlockNumber; i <= endBlockNumber; i++ {
addrs, err := contracts.GetSignersFromContract(client, i)
if err != nil {
log.Error("Fail to get signers from smartcontract.", "error", err, "blockNumber", i)
return err
}
// Filter duplicate address.
if len(addrs) > 0 {
addrSigners := make(map[common.Address]bool)
for _, addr := range addrs {
if _, ok := addrSigners[addr]; ok {
} else {
addrSigners[addr] = true
}
}
for addr := range addrSigners {
_, exist := signers[addr]
if exist {
signers[addr].Sign++
} else {
signers[addr] = &rewardLog{1, 0}
}
totalSigner++
}
}
}
addr := common.HexToAddress(common.BlockSigners)
chainReward := new(big.Int).SetUint64(chain.Config().Clique.Reward * params.Ether)
// Add reward for signer.
calcReward := new(big.Int)
// Add reward for signers.
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)
signers, err := contracts.GetRewardForCheckpoint(chainReward, addr, number, rCheckpoint, client)
if err != nil {
log.Error("Fail to parse json signers", "error", err)
return err
log.Error("Fail to get signers for reward checkpoint", "error", err)
}
log.Info("Calculate reward at checkpoint", "startBlock", startBlockNumber, "endBlock", endBlockNumber, "signers", string(jsonSigners), "totalSigner", totalSigner, "totalReward", chainReward)
// Add reward for signers.
if len(signers) > 0 {
for signer, calcReward := range signers {
state.AddBalance(signer, calcReward)
}
}
}
return nil