From 8aed80d428b93bd13143b777ee4227c47dfc95ac Mon Sep 17 00:00:00 2001 From: DinhLN Date: Fri, 9 Nov 2018 14:34:53 +0700 Subject: [PATCH] Fixed reward inflation and add unit test. --- common/constants.go | 1 + contracts/utils.go | 1 + eth/backend.go | 14 ++++++++++++++ eth/backend_test.go | 24 ++++++++++++++++++++++++ 4 files changed, 40 insertions(+) create mode 100644 eth/backend_test.go diff --git a/common/constants.go b/common/constants.go index 526325a1ae..2d3bcda972 100644 --- a/common/constants.go +++ b/common/constants.go @@ -12,4 +12,5 @@ const ( EpocBlockRandomize = 900 MaxMasternodes = 150 LimitPenaltyEpoch = 4 + BlocksPerYear = uint64(15768000) ) diff --git a/contracts/utils.go b/contracts/utils.go index b80a505486..b86f7790a8 100644 --- a/contracts/utils.go +++ b/contracts/utils.go @@ -58,6 +58,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() diff --git a/eth/backend.go b/eth/backend.go index b31015f043..26915e331c 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -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().Posv.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 { @@ -646,3 +649,14 @@ func GetValidators(bc *core.BlockChain, masternodes []common.Address) ([]byte, e } 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 +} diff --git a/eth/backend_test.go b/eth/backend_test.go new file mode 100644 index 0000000000..d0c8ea4d3b --- /dev/null +++ b/eth/backend_test.go @@ -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) + } + } +}