Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix klay_getRewards stakers amount to exclude remainder #1771

Merged
merged 2 commits into from Feb 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 5 additions & 3 deletions reward/reward_distributor.go
Expand Up @@ -198,7 +198,7 @@ func GetBlockReward(header *types.Header, rules params.Rules, pset *params.GovPa
// some non-zero fee already has been paid to the proposer.
if !pset.DeferredTxFee() {
blockFee := GetTotalTxFee(header, rules, pset)
aidan-kwon marked this conversation as resolved.
Show resolved Hide resolved
spec.Proposer = spec.Proposer.Add(spec.Proposer, spec.TotalFee)
spec.Proposer = spec.Proposer.Add(spec.Proposer, blockFee)
spec.TotalFee = spec.TotalFee.Add(spec.TotalFee, blockFee)
incrementRewardsMap(spec.Rewards, header.Rewardbase, blockFee)
}
Expand Down Expand Up @@ -237,7 +237,7 @@ func CalcDeferredRewardSimple(header *types.Header, rules params.Rules, pset *pa
spec.TotalFee = big.NewInt(0)
spec.BurntFee = big.NewInt(0)
spec.Proposer = proposer
spec.Rewards[header.Rewardbase] = proposer
incrementRewardsMap(spec.Rewards, header.Rewardbase, proposer)
return spec, nil
}

Expand All @@ -264,7 +264,7 @@ func CalcDeferredRewardSimple(header *types.Header, rules params.Rules, pset *pa
spec.TotalFee = totalFee
spec.BurntFee = burntFee
spec.Proposer = proposer
spec.Rewards[header.Rewardbase] = proposer
incrementRewardsMap(spec.Rewards, header.Rewardbase, proposer)
return spec, nil
}

Expand Down Expand Up @@ -292,7 +292,9 @@ func CalcDeferredReward(header *types.Header, rules params.Rules, pset *params.G
// Remainder from (CN, KGF, KIR) split goes to KGF
kgf = kgf.Add(kgf, splitRem)
// Remainder from staker shares goes to Proposer
// Then, deduct it from stakers so that `minted + totalFee - burntFee = proposer + stakers + kgf + kir`
proposer = proposer.Add(proposer, shareRem)
stakers = stakers.Sub(stakers, shareRem)

// if KGF or KIR is not set, proposer gets the portion
if stakingInfo == nil || common.EmptyAddress(stakingInfo.PoCAddr) {
Expand Down
52 changes: 35 additions & 17 deletions reward/reward_distributor_test.go
Expand Up @@ -17,6 +17,7 @@
package reward

import (
"encoding/json"
"fmt"
"math/big"
"testing"
Expand All @@ -42,6 +43,23 @@ func (governance *testGovernance) setTestGovernance(intMap map[int]interface{})
governance.p = p
}

func assertEqualRewardSpecs(t *testing.T, expected, actual *RewardSpec, msgAndArgs ...interface{}) {
expectedJson, err := json.MarshalIndent(expected, "", " ")
require.Nil(t, err)

actualJson, err := json.MarshalIndent(actual, "", " ")
require.Nil(t, err)

assert.Equal(t, string(expectedJson), string(actualJson), msgAndArgs...)

lhs := new(big.Int).Add(actual.Minted, actual.TotalFee)
lhs = lhs.Sub(lhs, actual.BurntFee)
rhs := new(big.Int).Add(actual.Proposer, actual.Stakers)
rhs = rhs.Add(rhs, actual.Kgf)
rhs = rhs.Add(rhs, actual.Kir)
assert.True(t, lhs.Cmp(rhs) == 0, msgAndArgs...)
}

var (
cnBaseAddr = 500 // Dummy addresses goes like 0x000..5nn
stakeBaseAddr = 600
Expand Down Expand Up @@ -341,7 +359,7 @@ func TestRewardDistributor_GetBlockReward(t *testing.T) {
TotalFee: new(big.Int).SetUint64(1000),
BurntFee: new(big.Int).SetUint64(1000),
Proposer: new(big.Int).SetUint64(0.6528e18 + 1),
Stakers: new(big.Int).SetUint64(2.6112e18),
Stakers: new(big.Int).SetUint64(2.6112e18 - 1),
Kgf: new(big.Int).SetUint64(5.184e18),
Kir: new(big.Int).SetUint64(1.152e18),
Rewards: map[common.Address]*big.Int{
Expand All @@ -360,8 +378,8 @@ func TestRewardDistributor_GetBlockReward(t *testing.T) {
Minted: minted,
TotalFee: new(big.Int).SetUint64(1000),
BurntFee: new(big.Int).SetUint64(0),
Proposer: new(big.Int).SetUint64(0.6528e18 + 1),
Stakers: new(big.Int).SetUint64(2.6112e18),
Proposer: new(big.Int).SetUint64(0.6528e18 + 1000 + 1),
Stakers: new(big.Int).SetUint64(2.6112e18 - 1),
Kgf: new(big.Int).SetUint64(5.184e18),
Kir: new(big.Int).SetUint64(1.152e18),
Rewards: map[common.Address]*big.Int{
Expand Down Expand Up @@ -389,7 +407,7 @@ func TestRewardDistributor_GetBlockReward(t *testing.T) {

spec, err := GetBlockReward(header, rules, pset)
require.Nil(t, err, "testcases[%d] failed", i)
assert.Equal(t, tc.expected, spec, "testcases[%d] failed", i)
assertEqualRewardSpecs(t, tc.expected, spec, "testcases[%d] failed", i)
}
}

Expand Down Expand Up @@ -449,7 +467,7 @@ func TestRewardDistributor_CalcDeferredRewardSimple(t *testing.T) {

spec, err := CalcDeferredRewardSimple(header, rules, pset)
require.Nil(t, err, "testcases[%d] failed", i)
assert.Equal(t, tc.expected, spec, "testcases[%d] failed", i)
assertEqualRewardSpecs(t, tc.expected, spec, "testcases[%d] failed", i)
}
}

Expand Down Expand Up @@ -536,7 +554,7 @@ func TestRewardDistributor_CalcDeferredRewardSimple_nodeferred(t *testing.T) {

spec, err := CalcDeferredRewardSimple(header, rules, pset)
require.Nil(t, err, "testcases[%d] failed", i)
assert.Equal(t, tc.expected, spec, "testcases[%d] failed", i)
assertEqualRewardSpecs(t, tc.expected, spec, "testcases[%d] failed", i)
}
}

Expand Down Expand Up @@ -646,7 +664,7 @@ func TestRewardDistributor_CalcDeferredReward(t *testing.T) {
TotalFee: new(big.Int).SetUint64(1000),
BurntFee: new(big.Int).SetUint64(1000),
Proposer: new(big.Int).SetUint64(0.6528e18 + 1),
Stakers: new(big.Int).SetUint64(2.6112e18),
Stakers: new(big.Int).SetUint64(2.6112e18 - 1),
Kgf: new(big.Int).SetUint64(5.184e18),
Kir: new(big.Int).SetUint64(1.152e18),
Rewards: map[common.Address]*big.Int{
Expand All @@ -668,7 +686,7 @@ func TestRewardDistributor_CalcDeferredReward(t *testing.T) {
TotalFee: new(big.Int).SetUint64(10e18),
BurntFee: new(big.Int).SetUint64(5e18 + 0.6528e18),
Proposer: new(big.Int).SetUint64(5e18 + 1),
Stakers: new(big.Int).SetUint64(2.6112e18),
Stakers: new(big.Int).SetUint64(2.6112e18 - 1),
Kgf: new(big.Int).SetUint64(5.184e18),
Kir: new(big.Int).SetUint64(1.152e18),
Rewards: map[common.Address]*big.Int{
Expand Down Expand Up @@ -706,7 +724,7 @@ func TestRewardDistributor_CalcDeferredReward(t *testing.T) {

spec, err := CalcDeferredReward(header, rules, pset)
require.Nil(t, err, "failed tc: %s", tc.desc)
assert.Equal(t, tc.expected, spec, "failed tc: %s", tc.desc)
assertEqualRewardSpecs(t, tc.expected, spec, "failed tc: %s", tc.desc)
}
}

Expand Down Expand Up @@ -739,7 +757,7 @@ func TestRewardDistributor_CalcDeferredReward_StakingInfos(t *testing.T) {
TotalFee: big.NewInt(1000),
BurntFee: big.NewInt(1000),
Proposer: minted,
Stakers: big.NewInt(2.6112e18),
Stakers: big.NewInt(0),
Kgf: big.NewInt(0),
Kir: big.NewInt(0),
Rewards: map[common.Address]*big.Int{
Expand All @@ -758,7 +776,7 @@ func TestRewardDistributor_CalcDeferredReward_StakingInfos(t *testing.T) {
TotalFee: big.NewInt(1000),
BurntFee: big.NewInt(1000),
Proposer: big.NewInt(8.448e18),
Stakers: big.NewInt(2.6112e18),
Stakers: big.NewInt(0),
Kgf: big.NewInt(0),
Kir: big.NewInt(1.152e18), // minted * 0.12
Rewards: map[common.Address]*big.Int{
Expand All @@ -778,7 +796,7 @@ func TestRewardDistributor_CalcDeferredReward_StakingInfos(t *testing.T) {
TotalFee: big.NewInt(1000),
BurntFee: big.NewInt(1000),
Proposer: big.NewInt(4.416e18),
Stakers: big.NewInt(2.6112e18),
Stakers: big.NewInt(0),
Kgf: big.NewInt(5.184e18), // minted * 0.54
Kir: big.NewInt(0),
Rewards: map[common.Address]*big.Int{
Expand All @@ -798,7 +816,7 @@ func TestRewardDistributor_CalcDeferredReward_StakingInfos(t *testing.T) {
TotalFee: big.NewInt(1000),
BurntFee: big.NewInt(1000),
Proposer: big.NewInt(3.264e18),
Stakers: big.NewInt(2.6112e18),
Stakers: big.NewInt(0),
Kgf: big.NewInt(5.184e18),
Kir: big.NewInt(1.152e18),
Rewards: map[common.Address]*big.Int{
Expand All @@ -817,7 +835,7 @@ func TestRewardDistributor_CalcDeferredReward_StakingInfos(t *testing.T) {
}
spec, err := CalcDeferredReward(header, rules, pset)
require.Nil(t, err, "testcases[%d] failed", i)
assert.Equal(t, tc.expected, spec, "testcases[%d] failed: %s", i, tc.desc)
assertEqualRewardSpecs(t, tc.expected, spec, "testcases[%d] failed: %s", i, tc.desc)
}
}

Expand Down Expand Up @@ -854,7 +872,7 @@ func TestRewardDistributor_CalcDeferredReward_Remainings(t *testing.T) {
TotalFee: big.NewInt(1000),
BurntFee: big.NewInt(522),
Proposer: big.NewInt(501), // proposer=22, rewardFee=478, shareRem=1
Stakers: big.NewInt(90),
Stakers: big.NewInt(89),
Kgf: big.NewInt(182), // splitRem=3
Kir: big.NewInt(39),
Rewards: map[common.Address]*big.Int{
Expand All @@ -874,7 +892,7 @@ func TestRewardDistributor_CalcDeferredReward_Remainings(t *testing.T) {
TotalFee: big.NewInt(1000),
BurntFee: big.NewInt(1000),
Proposer: big.NewInt(0.6528e18 + 1),
Stakers: big.NewInt(2.6112e18),
Stakers: big.NewInt(2.6112e18 - 1),
Kgf: big.NewInt(5.184e18),
Kir: big.NewInt(1.152e18),
Rewards: map[common.Address]*big.Int{
Expand All @@ -897,7 +915,7 @@ func TestRewardDistributor_CalcDeferredReward_Remainings(t *testing.T) {

spec, err := CalcDeferredReward(header, rules, pset)
require.Nil(t, err, "failed tc: %s", tc.desc)
assert.Equal(t, tc.expected, spec, "failed tc: %s", tc.desc)
assertEqualRewardSpecs(t, tc.expected, spec, "failed tc: %s", tc.desc)
}
}

Expand Down