code.vegaprotocol.io/vega@v0.79.0/core/validators/validator_performance_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 validators_test 17 18 import ( 19 "context" 20 "encoding/hex" 21 "testing" 22 23 "code.vegaprotocol.io/vega/core/validators" 24 "code.vegaprotocol.io/vega/libs/num" 25 "code.vegaprotocol.io/vega/logging" 26 27 "github.com/stretchr/testify/require" 28 ) 29 30 var ( 31 tmkey1 = "uBr9FP/M/QyVtOa3j18+hjksXra7qxCa7e25/FVW5c0=" 32 tmkey4 = "g2QGqXnfkNmmVO4QSw/wu5kpk8rbaY+I4qYNzk7QJTc=" 33 34 address1, _ = hex.DecodeString("91484AD0B6343D73690F1D36A80EF92B67622C47") 35 address2, _ = hex.DecodeString("3619F6EC431527F02457875B7355041ADBB54772") 36 address3, _ = hex.DecodeString("13FA0B679D6064772567C7A6050B42CCA1C7C8CD") 37 ) 38 39 func TestValidatorPerformanceNoPerformance(t *testing.T) { 40 vp := validators.NewValidatorPerformance(logging.NewTestLogger()) 41 require.Equal(t, num.DecimalFromFloat(0.05), vp.ValidatorPerformanceScore("some name", 1, 10, num.DecimalOne())) 42 } 43 44 func TestElectedExpectationWithVotingPower(t *testing.T) { 45 vp := validators.NewValidatorPerformance(logging.NewTestLogger()) 46 vp.BeginBlock(context.Background(), hex.EncodeToString(address1)) 47 vp.BeginBlock(context.Background(), hex.EncodeToString(address2)) 48 vp.BeginBlock(context.Background(), hex.EncodeToString(address1)) 49 vp.BeginBlock(context.Background(), hex.EncodeToString(address3)) 50 vp.BeginBlock(context.Background(), hex.EncodeToString(address2)) 51 52 // validator 1 proposed 2 times, out of 5 (i.e. 40%), they only got 20% voting power so they score should be capped at 1 53 require.Equal(t, "1", vp.ValidatorPerformanceScore(tmkey1, 10, 50, num.DecimalZero()).String()) 54 55 // validator 1 proposed 2 times, out of 5 (i.e. 40%), they only got 40% voting power so they score should be 1 56 require.Equal(t, "1", vp.ValidatorPerformanceScore(tmkey1, 20, 50, num.DecimalZero()).String()) 57 58 // validator 1 proposed 2 times, out of 5 (i.e. 40%), they got 60% voting power so they score but with a minimum scaling of 2 blocks they should get a score of 1 59 require.Equal(t, "1", vp.ValidatorPerformanceScore(tmkey1, 30, 50, num.DecimalZero()).String()) 60 61 // validator 4 never proposed but has a 20% of the voting power so with scaling they proposed 2/5 which is greater than their voting power so they get score of 1 62 require.Equal(t, "1", vp.ValidatorPerformanceScore(tmkey4, 10, 50, num.DecimalZero()).String()) 63 } 64 65 func TestPerformanceScoreWithScaling(t *testing.T) { 66 vp := validators.NewValidatorPerformance(logging.NewTestLogger()) 67 for i := 0; i < 25; i++ { 68 vp.BeginBlock(context.Background(), hex.EncodeToString(address1)) 69 } 70 for i := 0; i < 75; i++ { 71 vp.BeginBlock(context.Background(), hex.EncodeToString(address2)) 72 } 73 74 // validator 1 proposed 1/4 of the blocks with 50% of the voting power 75 // with the minimum scaling they get a performance score of 27/50 76 require.Equal(t, "0.54", vp.ValidatorPerformanceScore(tmkey1, 25, 50, num.DecimalZero()).String()) 77 78 // validator 1 proposed 1/4 of the blocks with 50% of the voting power 79 // with the scaling of 15% they get a performance score of 25*1.15/50 80 require.Equal(t, "0.575", vp.ValidatorPerformanceScore(tmkey1, 25, 50, num.DecimalFromFloat(0.15)).String()) 81 }