github.com/prysmaticlabs/prysm@v1.4.4/shared/benchutil/pregen.go (about)

     1  // Package benchutil contains useful helpers
     2  // for pregenerating filled data structures such as blocks/states for benchmarks.
     3  package benchutil
     4  
     5  import (
     6  	"fmt"
     7  	"io/ioutil"
     8  
     9  	"github.com/bazelbuild/rules_go/go/tools/bazel"
    10  	iface "github.com/prysmaticlabs/prysm/beacon-chain/state/interface"
    11  	"github.com/prysmaticlabs/prysm/beacon-chain/state/v1"
    12  	pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
    13  	ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
    14  	"github.com/prysmaticlabs/prysm/shared/params"
    15  )
    16  
    17  // ValidatorCount is for declaring how many validators the benchmarks will be
    18  // performed with. Default is 16384 or 524K ETH staked.
    19  var ValidatorCount = uint64(16384)
    20  
    21  // AttestationsPerEpoch represents the requested amount attestations in an epoch.
    22  // This affects the amount of attestations in a fully attested for block and the amount
    23  // of attestations in the state per epoch, so a full 2 epochs should result in twice
    24  // this amount of attestations in the state. Default is 128.
    25  var AttestationsPerEpoch = uint64(128)
    26  
    27  // GenesisFileName is the generated genesis beacon state file name.
    28  var GenesisFileName = fmt.Sprintf("bStateGenesis-%dAtts-%dVals.ssz", AttestationsPerEpoch, ValidatorCount)
    29  
    30  // BState1EpochFileName is the generated beacon state after 1 skipped epoch file name.
    31  var BState1EpochFileName = fmt.Sprintf("bState1Epoch-%dAtts-%dVals.ssz", AttestationsPerEpoch, ValidatorCount)
    32  
    33  // BState2EpochFileName is the generated beacon state after 2 full epochs file name.
    34  var BState2EpochFileName = fmt.Sprintf("bState2Epochs-%dAtts-%dVals.ssz", AttestationsPerEpoch, ValidatorCount)
    35  
    36  // FullBlockFileName is the generated full block file name.
    37  var FullBlockFileName = fmt.Sprintf("fullBlock-%dAtts-%dVals.ssz", AttestationsPerEpoch, ValidatorCount)
    38  
    39  func filePath(path string) string {
    40  	return fmt.Sprintf("shared/benchutil/benchmark_files/%s", path)
    41  }
    42  
    43  // PreGenState1Epoch unmarshals the pre-generated beacon state after 1 epoch of block processing and returns it.
    44  func PreGenState1Epoch() (iface.BeaconState, error) {
    45  	path, err := bazel.Runfile(filePath(BState1EpochFileName))
    46  	if err != nil {
    47  		return nil, err
    48  	}
    49  	beaconBytes, err := ioutil.ReadFile(path)
    50  	if err != nil {
    51  		return nil, err
    52  	}
    53  	beaconState := &pb.BeaconState{}
    54  	if err := beaconState.UnmarshalSSZ(beaconBytes); err != nil {
    55  		return nil, err
    56  	}
    57  	return v1.InitializeFromProto(beaconState)
    58  }
    59  
    60  // PreGenState2FullEpochs unmarshals the pre-generated beacon state after 2 epoch of full block processing and returns it.
    61  func PreGenState2FullEpochs() (iface.BeaconState, error) {
    62  	path, err := bazel.Runfile(filePath(BState2EpochFileName))
    63  	if err != nil {
    64  		return nil, err
    65  	}
    66  	beaconBytes, err := ioutil.ReadFile(path)
    67  	if err != nil {
    68  		return nil, err
    69  	}
    70  	beaconState := &pb.BeaconState{}
    71  	if err := beaconState.UnmarshalSSZ(beaconBytes); err != nil {
    72  		return nil, err
    73  	}
    74  	return v1.InitializeFromProto(beaconState)
    75  }
    76  
    77  // PreGenFullBlock unmarshals the pre-generated signed beacon block containing an epochs worth of attestations and returns it.
    78  func PreGenFullBlock() (*ethpb.SignedBeaconBlock, error) {
    79  	path, err := bazel.Runfile(filePath(FullBlockFileName))
    80  	if err != nil {
    81  		return nil, err
    82  	}
    83  	blockBytes, err := ioutil.ReadFile(path)
    84  	if err != nil {
    85  		return nil, err
    86  	}
    87  	beaconBlock := &ethpb.SignedBeaconBlock{}
    88  	if err := beaconBlock.UnmarshalSSZ(blockBytes); err != nil {
    89  		return nil, err
    90  	}
    91  	return beaconBlock, nil
    92  }
    93  
    94  // SetBenchmarkConfig changes the beacon config to match the requested amount of
    95  // attestations set to AttestationsPerEpoch.
    96  func SetBenchmarkConfig() {
    97  	maxAtts := AttestationsPerEpoch
    98  	slotsPerEpoch := uint64(params.BeaconConfig().SlotsPerEpoch)
    99  	committeeSize := (ValidatorCount / slotsPerEpoch) / (maxAtts / slotsPerEpoch)
   100  	c := params.BeaconConfig()
   101  	c.ShardCommitteePeriod = 0
   102  	c.MinValidatorWithdrawabilityDelay = 0
   103  	c.TargetCommitteeSize = committeeSize
   104  	c.MaxAttestations = maxAtts
   105  	params.OverrideBeaconConfig(c)
   106  }