diff --git a/common/constants.go b/common/constants.go index 9dd97f3a2c..89ba5c315f 100644 --- a/common/constants.go +++ b/common/constants.go @@ -12,4 +12,5 @@ const ( EpocBlockRandomize = 900 MaxMasternodes = 150 LimitPenaltyEpoch = 4 + BlocksPerYear = uint64(15768000) ) \ No newline at end of file diff --git a/contracts/utils.go b/contracts/utils.go index b2d8b42cc8..42144ec96b 100644 --- a/contracts/utils.go +++ b/contracts/utils.go @@ -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() diff --git a/eth/backend.go b/eth/backend.go index d50fe1fe4a..2e9168ce35 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().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 } \ No newline at end of file diff --git a/eth/backend_test.go b/eth/backend_test.go new file mode 100644 index 0000000000..49d5228481 --- /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) + } + } +} \ No newline at end of file