EBK1000As mentioned above stake works right for small amount of coins.
It's seems to be cause uint64 overflow.
I enable debug, printcreation and printcoinage.
And check debug.log:
GetProofOfStakeReward(): create=1.08395022 nCoinAge=263761221736
GetProofOfStakeReward(): create=0.53003278 nCoinAge=11197021089386
Why does we have the bigger reward (1.08) for less coinAge (263761221736) and vice versa?
The code for reward calculation:
int64_t nSubsidy = nCoinAge * nRewardCoinYear / 365 / COIN;
First:
263761221736 * 15 * 1000000 / 365 / 10000000 = 108395022, 108395022 / 100000000 = 1,08395022 (we have 1.08395022 in the log)
It's right.
Second:
11197021089386 * 15 * 1000000 / 365 / 10000000 = 4601515516, 4601515516 / 100000000 = 46,01515516 (but we have only 0.53003278 in the log)
It's not correct.
We must receive ~46, but we receive only ~0.5 due uint64 overflow.Max value of uint64 is: 18,446,744,073,709,551,615
In example above we have: 11197021089386 * 15 * 1000000 = 167955316340790000000
167,955,316,340,790,000,000
vs
18,446,744,073,709,551,615
Overflow happened.
I suggest change the code:
int64_t nSubsidy = nCoinAge * nRewardCoinYear / 365 / COIN;
To:
int64_t nSubsidy = (nCoinAge / COIN) * nRewardCoinYear / 365;
Possible brackets are not needed - it is necessary to test.
Or:
int64_t nRewardCoinYear = 1;
if (nHeight < 512000)
nRewardCoinYear = 15;
if (nHeight >= 512000 && nHeight < 1025000)
nRewardCoinYear = 10;
if (nHeight >= 1025000 && nHeight < 1537000)
nRewardCoinYear = 5;
int64_t nSubsidy = nCoinAge * nRewardCoinYear / 365 / 100;
If you not plan to change the CENT and COIN value in main.h in the future.