code.vegaprotocol.io/vega@v0.79.0/core/integration/steps/blocks.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 steps
    17  
    18  import (
    19  	"context"
    20  	"strconv"
    21  	"time"
    22  
    23  	"code.vegaprotocol.io/vega/core/integration/helpers"
    24  	"code.vegaprotocol.io/vega/core/integration/stubs"
    25  )
    26  
    27  type EpochService interface {
    28  	OnBlockEnd(ctx context.Context)
    29  }
    30  
    31  func TheAverageBlockDurationIs(block *helpers.Block, dur string) error {
    32  	avg, err := strconv.ParseInt(dur, 10, 0)
    33  	if err != nil {
    34  		return err
    35  	}
    36  	block.Duration = avg
    37  	return nil
    38  }
    39  
    40  func TheAverageBlockDurationWithVariance(block *helpers.Block, dur, variance string) error {
    41  	if err := TheAverageBlockDurationIs(block, dur); err != nil {
    42  		return err
    43  	}
    44  	v, err := strconv.ParseFloat(variance, 64)
    45  	if err != nil {
    46  		return err
    47  	}
    48  	block.Variance = v
    49  	return nil
    50  }
    51  
    52  func TheNetworkMovesAheadNBlocks(exec Execution, block *helpers.Block, time *stubs.TimeStub, n string, epochService EpochService) error {
    53  	nr, err := strconv.ParseInt(n, 10, 0)
    54  	if err != nil {
    55  		return err
    56  	}
    57  	now := time.GetTimeNow()
    58  	for i := int64(0); i < nr; i++ {
    59  		epochService.OnBlockEnd(context.Background())
    60  		exec.BlockEnd(context.Background())
    61  		now = now.Add(block.GetDuration())
    62  		// progress time
    63  		time.SetTime(now)
    64  	}
    65  	return nil
    66  }
    67  
    68  func TheNetworkMovesAheadNEpochs(broker *stubs.BrokerStub, block *helpers.Block, exec Execution, epochService EpochService, ts *stubs.TimeStub, epochs string) error {
    69  	nr, err := strconv.ParseInt(epochs, 10, 0)
    70  	if err != nil {
    71  		return err
    72  	}
    73  	for i := int64(0); i < nr; i++ {
    74  		if err := TheNetworkMovesAheadToTheNextEpoch(broker, block, exec, epochService, ts); err != nil {
    75  			return err
    76  		}
    77  	}
    78  	return nil
    79  }
    80  
    81  func TheNetworkMovesAheadToTheNextEpoch(broker *stubs.BrokerStub, block *helpers.Block, exec Execution, epochService EpochService, ts *stubs.TimeStub) error {
    82  	ee := broker.GetCurrentEpoch()
    83  	last := ee.Epoch().GetSeq()
    84  	current := last
    85  	now := ts.GetTimeNow()
    86  	for current == last {
    87  		epochService.OnBlockEnd(context.Background())
    88  		exec.BlockEnd(context.Background())
    89  		now = now.Add(block.GetDuration())
    90  		ts.SetTime(now)
    91  		ee = broker.GetCurrentEpoch()
    92  		current = ee.Epoch().GetSeq()
    93  	}
    94  	return nil
    95  }
    96  
    97  func TheNetworkMovesAheadDurationWithBlocks(exec Execution, block *helpers.Block, ts *stubs.TimeStub, delta, dur string) error {
    98  	td, err := time.ParseDuration(delta)
    99  	if err != nil {
   100  		return err
   101  	}
   102  	bd, err := time.ParseDuration(dur)
   103  	if err != nil {
   104  		return err
   105  	}
   106  	now := ts.GetTimeNow()
   107  	target := now.Add(td)
   108  	for now.Before(target) {
   109  		exec.BlockEnd(context.Background())
   110  		now = now.Add(block.GetStep(bd))
   111  		ts.SetTime(now)
   112  	}
   113  	return nil
   114  }