code.vegaprotocol.io/vega@v0.79.0/core/statevar/add_noise_qa.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 //go:build qa 17 // +build qa 18 19 package statevar 20 21 import ( 22 "math/rand" 23 24 "code.vegaprotocol.io/vega/libs/num" 25 "code.vegaprotocol.io/vega/logging" 26 "code.vegaprotocol.io/vega/protos/vega" 27 vegapb "code.vegaprotocol.io/vega/protos/vega" 28 ) 29 30 // AddNoise is a function used in qa build to add noise to the state variables within their tolerance to instrument consensus seeking. 31 func (sv *StateVariable) AddNoise(kvb []*vegapb.KeyValueBundle) []*vegapb.KeyValueBundle { 32 for _, kvt := range kvb { 33 tol, _ := num.DecimalFromString(kvt.Tolerance) 34 switch v := kvt.Value.Value.(type) { 35 case *vega.StateVarValue_ScalarVal: 36 random := rand.Float64() * tol.InexactFloat64() / 2.0 37 if sv.log.GetLevel() <= logging.DebugLevel { 38 sv.log.Debug("adding random noise", logging.String("key-name", kvt.Key), logging.Float64("randomness", random)) 39 } 40 val, _ := num.DecimalFromString(v.ScalarVal.Value) 41 val = val.Add(num.DecimalFromFloat(random)) 42 kvt.Value.Value = &vegapb.StateVarValue_ScalarVal{ 43 ScalarVal: &vegapb.ScalarValue{ 44 Value: val.String(), 45 }, 46 } 47 48 case *vega.StateVarValue_VectorVal: 49 vec := make([]num.Decimal, 0, len(v.VectorVal.Value)) 50 for i, entry := range v.VectorVal.Value { 51 random := rand.Float64() * tol.InexactFloat64() / 2.0 52 if sv.log.GetLevel() <= logging.DebugLevel { 53 sv.log.Debug("adding random noise", logging.String("key-name", kvt.Key), logging.Int("index", i), logging.Float64("randomness", random)) 54 } 55 value, _ := num.DecimalFromString(entry) 56 vec = append(vec, value.Add(num.DecimalFromFloat(random))) 57 } 58 vecAsString := make([]string, 0, len(vec)) 59 for _, v := range vec { 60 vecAsString = append(vecAsString, v.String()) 61 } 62 kvt.Value.Value = &vegapb.StateVarValue_VectorVal{ 63 VectorVal: &vegapb.VectorValue{ 64 Value: vecAsString, 65 }, 66 } 67 } 68 } 69 return kvb 70 }