code.vegaprotocol.io/vega@v0.79.0/core/rewards/validator_ranking_reward_calculator_test.go (about) 1 // Copyright (C) 2023 Gobalsky Labs Limited 2 // 3 // This program is free software: you can redistribute it and/or modify 4 // it under the terms of the GNU Affero General Public License as 5 // published by the Free Software Foundation, either version 3 of the 6 // License, or (at your option) any later version. 7 // 8 // This program is distributed in the hope that it will be useful, 9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 // GNU Affero General Public License for more details. 12 // 13 // You should have received a copy of the GNU Affero General Public License 14 // along with this program. If not, see <http://www.gnu.org/licenses/>. 15 16 package rewards 17 18 import ( 19 "testing" 20 "time" 21 22 "code.vegaprotocol.io/vega/core/types" 23 "code.vegaprotocol.io/vega/libs/num" 24 25 "github.com/stretchr/testify/require" 26 ) 27 28 func TestCalculateRewardsForValidators(t *testing.T) { 29 tm := time.Now() 30 // no account balance 31 require.Nil(t, calculateRewardsForValidators("1", "zohar", "123", num.UintZero(), tm, []*types.PartyContributionScore{{Party: "z1", Score: num.DecimalFromFloat(0.1)}}, 0)) 32 // no contributions 33 require.Nil(t, calculateRewardsForValidators("1", "zohar", "123", num.NewUint(100), tm, []*types.PartyContributionScore{}, 0)) 34 // one contributed 35 po := calculateRewardsForValidators("1", "zohar-asset", "123", num.NewUint(100), tm, []*types.PartyContributionScore{{Party: "z1", Score: num.DecimalFromFloat(0.1)}}, 1) 36 require.NotNil(t, po) 37 require.Equal(t, "100", po.totalReward.String()) 38 require.Equal(t, uint64(1), po.lockedForEpochs) 39 require.Equal(t, 1, len(po.partyToAmount)) 40 require.Equal(t, "100", po.partyToAmount["z1"].String()) 41 require.Equal(t, "zohar-asset", po.asset) 42 require.Equal(t, "123", po.fromAccount) 43 require.Equal(t, "1", po.epochSeq) 44 require.Equal(t, tm.Unix(), po.timestamp) 45 46 // 3 contributions 47 // z1 - 0.1/0.8 = 0.125 => 125 48 // z2 - 0.5/0.8 = 0.625 => 625 49 // z3 - 0.2/0.8 = 0.250 => 250 50 po = calculateRewardsForValidators("1", "zohar-asset", "123", num.NewUint(1000), tm, []*types.PartyContributionScore{{Party: "z1", Score: num.DecimalFromFloat(0.1)}, {Party: "z2", Score: num.DecimalFromFloat(0.5)}, {Party: "z3", Score: num.DecimalFromFloat(0.2)}}, 1) 51 require.NotNil(t, po) 52 require.Equal(t, "1000", po.totalReward.String()) 53 require.Equal(t, uint64(1), po.lockedForEpochs) 54 require.Equal(t, 3, len(po.partyToAmount)) 55 require.Equal(t, "125", po.partyToAmount["z1"].String()) 56 require.Equal(t, "625", po.partyToAmount["z2"].String()) 57 require.Equal(t, "250", po.partyToAmount["z3"].String()) 58 require.Equal(t, "zohar-asset", po.asset) 59 require.Equal(t, "123", po.fromAccount) 60 require.Equal(t, "1", po.epochSeq) 61 require.Equal(t, tm.Unix(), po.timestamp) 62 }