github.com/prysmaticlabs/prysm@v1.4.4/shared/testutil/state.go (about)

     1  package testutil
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/ethereum/go-ethereum/common/hexutil"
     7  	types "github.com/prysmaticlabs/eth2-types"
     8  	"github.com/prysmaticlabs/go-bitfield"
     9  	"github.com/prysmaticlabs/prysm/beacon-chain/state/v1"
    10  	pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
    11  	ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
    12  	"github.com/prysmaticlabs/prysm/shared/params"
    13  )
    14  
    15  // FillRootsNaturalOpt is meant to be used as an option when calling NewBeaconState.
    16  // It fills state and block roots with hex representations of natural numbers starting with 0.
    17  // Example: 16 becomes 0x00...0f.
    18  func FillRootsNaturalOpt(state *pb.BeaconState) error {
    19  	rootsLen := params.MainnetConfig().SlotsPerHistoricalRoot
    20  	roots := make([][]byte, rootsLen)
    21  	for i := types.Slot(0); i < rootsLen; i++ {
    22  		roots[i] = make([]byte, 32)
    23  	}
    24  	for j := 0; j < len(roots); j++ {
    25  		// Remove '0x' prefix and left-pad '0' to have 64 chars in total.
    26  		s := fmt.Sprintf("%064s", hexutil.EncodeUint64(uint64(j))[2:])
    27  		h, err := hexutil.Decode("0x" + s)
    28  		if err != nil {
    29  			return err
    30  		}
    31  		roots[j] = h
    32  	}
    33  	state.StateRoots = roots
    34  	state.BlockRoots = roots
    35  	return nil
    36  }
    37  
    38  // NewBeaconState creates a beacon state with minimum marshalable fields.
    39  func NewBeaconState(options ...func(state *pb.BeaconState) error) (*v1.BeaconState, error) {
    40  	seed := &pb.BeaconState{
    41  		BlockRoots:                 filledByteSlice2D(uint64(params.MainnetConfig().SlotsPerHistoricalRoot), 32),
    42  		StateRoots:                 filledByteSlice2D(uint64(params.MainnetConfig().SlotsPerHistoricalRoot), 32),
    43  		Slashings:                  make([]uint64, params.MainnetConfig().EpochsPerSlashingsVector),
    44  		RandaoMixes:                filledByteSlice2D(uint64(params.MainnetConfig().EpochsPerHistoricalVector), 32),
    45  		Validators:                 make([]*ethpb.Validator, 0),
    46  		CurrentJustifiedCheckpoint: &ethpb.Checkpoint{Root: make([]byte, 32)},
    47  		Eth1Data: &ethpb.Eth1Data{
    48  			DepositRoot: make([]byte, 32),
    49  			BlockHash:   make([]byte, 32),
    50  		},
    51  		Fork: &pb.Fork{
    52  			PreviousVersion: make([]byte, 4),
    53  			CurrentVersion:  make([]byte, 4),
    54  		},
    55  		Eth1DataVotes:               make([]*ethpb.Eth1Data, 0),
    56  		HistoricalRoots:             make([][]byte, 0),
    57  		JustificationBits:           bitfield.Bitvector4{0x0},
    58  		FinalizedCheckpoint:         &ethpb.Checkpoint{Root: make([]byte, 32)},
    59  		LatestBlockHeader:           HydrateBeaconHeader(&ethpb.BeaconBlockHeader{}),
    60  		PreviousEpochAttestations:   make([]*pb.PendingAttestation, 0),
    61  		CurrentEpochAttestations:    make([]*pb.PendingAttestation, 0),
    62  		PreviousJustifiedCheckpoint: &ethpb.Checkpoint{Root: make([]byte, 32)},
    63  	}
    64  
    65  	for _, opt := range options {
    66  		err := opt(seed)
    67  		if err != nil {
    68  			return nil, err
    69  		}
    70  	}
    71  
    72  	var st, err = v1.InitializeFromProtoUnsafe(seed)
    73  	if err != nil {
    74  		return nil, err
    75  	}
    76  
    77  	return st.Copy().(*v1.BeaconState), nil
    78  }
    79  
    80  // SSZ will fill 2D byte slices with their respective values, so we must fill these in too for round
    81  // trip testing.
    82  func filledByteSlice2D(length, innerLen uint64) [][]byte {
    83  	b := make([][]byte, length)
    84  	for i := uint64(0); i < length; i++ {
    85  		b[i] = make([]byte, innerLen)
    86  	}
    87  	return b
    88  }