Fixed reward inflation and add unit test.

This commit is contained in:
AnilChinchawale 2018-11-12 12:03:22 +05:30
parent de7c60ed49
commit 4211d56ad0
4 changed files with 40 additions and 0 deletions

View file

@ -12,4 +12,5 @@ const (
EpocBlockRandomize = 900
MaxMasternodes = 150
LimitPenaltyEpoch = 4
BlocksPerYear = uint64(15768000)
)

View file

@ -43,6 +43,7 @@ type rewardLog struct {
}
var TxSignMu sync.RWMutex
// Send tx sign for block number to smart contract blockSigner.
func CreateTransactionSign(chainConfig *params.ChainConfig, pool *core.TxPool, manager *accounts.Manager, block *types.Block, chainDb ethdb.Database) error {
TxSignMu.Lock()

View file

@ -291,7 +291,10 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
if number > 0 && number-rCheckpoint > 0 && foudationWalletAddr != (common.Address{}) {
// Get signers in blockSigner smartcontract.
addr := common.HexToAddress(common.BlockSigners)
// Get reward inflation.
chainReward := new(big.Int).Mul(new(big.Int).SetUint64(chain.Config().XDPoS.Reward), new(big.Int).SetUint64(params.Ether))
chainReward = rewardInflation(chainReward, number, common.BlocksPerYear)
totalSigner := new(uint64)
signers, err := contracts.GetRewardForCheckpoint(chain, addr, number, rCheckpoint, client, totalSigner)
if err != nil {
@ -645,4 +648,15 @@ func GetValidators(bc *core.BlockChain, masternodes []common.Address) ([]byte, e
return contracts.BuildValidatorFromM2(m2), nil
}
return nil, core.ErrNotFoundM1
}
func rewardInflation(chainReward *big.Int, number uint64, blockPerYear uint64) *big.Int {
if blockPerYear*2 <= number && number < blockPerYear*6 {
chainReward.Div(chainReward, new(big.Int).SetUint64(2))
}
if blockPerYear*6 <= number {
chainReward.Div(chainReward, new(big.Int).SetUint64(4))
}
return chainReward
}

24
eth/backend_test.go Normal file
View file

@ -0,0 +1,24 @@
package eth
import (
"github.com/ethereum/go-ethereum/params"
"math/big"
"testing"
)
func TestRewardInflation(t *testing.T) {
for i := 0; i < 100; i++ {
chainReward := new(big.Int).Mul(new(big.Int).SetUint64(250), new(big.Int).SetUint64(params.Ether))
chainReward = rewardInflation(chainReward, uint64(i), 10)
halfReward := new(big.Int).Mul(new(big.Int).SetUint64(125), new(big.Int).SetUint64(params.Ether))
if 20 <= i && i < 60 && chainReward.Cmp(halfReward) != 0 {
t.Error("Fail tor calculate reward inflation for 2 -> 5 years", "chainReward", chainReward)
}
quarterReward := new(big.Int).Mul(new(big.Int).SetUint64(62.5*1000), new(big.Int).SetUint64(params.Finney))
if 60 <= i && chainReward.Cmp(quarterReward) != 0 {
t.Error("Fail tor calculate reward inflation above 6 years", "chainReward", chainReward)
}
}
}