code.vegaprotocol.io/vega@v0.79.0/core/integration/helpers/block.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 helpers 17 18 import ( 19 "math/rand" 20 "time" 21 ) 22 23 type Block struct { 24 Duration int64 25 Variance float64 26 } 27 28 func NewBlock() *Block { 29 return &Block{ 30 Duration: 1, 31 } 32 } 33 34 func (b Block) GetDuration() time.Duration { 35 if b.Variance == 0 { 36 return time.Duration(b.Duration) * time.Second 37 } 38 base := time.Duration(b.Duration) * time.Second 39 factor := int64(b.Variance * float64(time.Second)) 40 // factor of 3, random 0-6 yields random number between -3 and +3 41 offset := factor - rand.Int63n(2*factor) 42 return base + time.Duration(offset) 43 } 44 45 func (b Block) GetStep(d time.Duration) time.Duration { 46 if b.Variance == 0 { 47 return d 48 } 49 factor := int64(b.Variance * float64(d)) 50 offset := factor - rand.Int63n(factor*2) 51 return d + time.Duration(offset) 52 }