github.com/prysmaticlabs/prysm@v1.4.4/shared/params/loader_test.go (about)

     1  package params
     2  
     3  import (
     4  	"io/ioutil"
     5  	"path"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/bazelbuild/rules_go/go/tools/bazel"
    10  	"github.com/prysmaticlabs/prysm/shared/testutil/assert"
    11  	"github.com/prysmaticlabs/prysm/shared/testutil/require"
    12  )
    13  
    14  func TestLoadConfigFileMainnet(t *testing.T) {
    15  	// See https://media.githubusercontent.com/media/ethereum/eth2.0-spec-tests/master/tests/minimal/config/phase0.yaml
    16  	assertVals := func(name string, c1, c2 *BeaconChainConfig) {
    17  		//  Misc params.
    18  		assert.Equal(t, c1.MaxCommitteesPerSlot, c2.MaxCommitteesPerSlot, "%s: MaxCommitteesPerSlot", name)
    19  		assert.Equal(t, c1.TargetCommitteeSize, c2.TargetCommitteeSize, "%s: TargetCommitteeSize", name)
    20  		assert.Equal(t, c1.MaxValidatorsPerCommittee, c2.MaxValidatorsPerCommittee, "%s: MaxValidatorsPerCommittee", name)
    21  		assert.Equal(t, c1.MinPerEpochChurnLimit, c2.MinPerEpochChurnLimit, "%s: MinPerEpochChurnLimit", name)
    22  		assert.Equal(t, c1.ChurnLimitQuotient, c2.ChurnLimitQuotient, "%s: ChurnLimitQuotient", name)
    23  		assert.Equal(t, c1.ShuffleRoundCount, c2.ShuffleRoundCount, "%s: ShuffleRoundCount", name)
    24  		assert.Equal(t, c1.MinGenesisActiveValidatorCount, c2.MinGenesisActiveValidatorCount, "%s: MinGenesisActiveValidatorCount", name)
    25  		assert.Equal(t, c1.MinGenesisTime, c2.MinGenesisTime, "%s: MinGenesisTime", name)
    26  		assert.Equal(t, c1.HysteresisQuotient, c2.HysteresisQuotient, "%s: HysteresisQuotient", name)
    27  		assert.Equal(t, c1.HysteresisDownwardMultiplier, c2.HysteresisDownwardMultiplier, "%s: HysteresisDownwardMultiplier", name)
    28  		assert.Equal(t, c1.HysteresisUpwardMultiplier, c2.HysteresisUpwardMultiplier, "%s: HysteresisUpwardMultiplier", name)
    29  
    30  		// Fork Choice params.
    31  		assert.Equal(t, c1.SafeSlotsToUpdateJustified, c2.SafeSlotsToUpdateJustified, "%s: SafeSlotsToUpdateJustified", name)
    32  
    33  		// Validator params.
    34  		assert.Equal(t, c1.Eth1FollowDistance, c2.Eth1FollowDistance, "%s: Eth1FollowDistance", name)
    35  		assert.Equal(t, c1.TargetAggregatorsPerCommittee, c2.TargetAggregatorsPerCommittee, "%s: TargetAggregatorsPerCommittee", name)
    36  		assert.Equal(t, c1.RandomSubnetsPerValidator, c2.RandomSubnetsPerValidator, "%s: RandomSubnetsPerValidator", name)
    37  		assert.Equal(t, c1.EpochsPerRandomSubnetSubscription, c2.EpochsPerRandomSubnetSubscription, "%s: EpochsPerRandomSubnetSubscription", name)
    38  		assert.Equal(t, c1.SecondsPerETH1Block, c2.SecondsPerETH1Block, "%s: SecondsPerETH1Block", name)
    39  
    40  		// Deposit contract.
    41  		assert.Equal(t, c1.DepositChainID, c2.DepositChainID, "%s: DepositChainID", name)
    42  		assert.Equal(t, c1.DepositNetworkID, c2.DepositNetworkID, "%s: DepositNetworkID", name)
    43  		assert.Equal(t, c1.DepositContractAddress, c2.DepositContractAddress, "%s: DepositContractAddress", name)
    44  
    45  		// Gwei values.
    46  		assert.Equal(t, c1.MinDepositAmount, c2.MinDepositAmount, "%s: MinDepositAmount", name)
    47  		assert.Equal(t, c1.MaxEffectiveBalance, c2.MaxEffectiveBalance, "%s: MaxEffectiveBalance", name)
    48  		assert.Equal(t, c1.EjectionBalance, c2.EjectionBalance, "%s: EjectionBalance", name)
    49  		assert.Equal(t, c1.EffectiveBalanceIncrement, c2.EffectiveBalanceIncrement, "%s: EffectiveBalanceIncrement", name)
    50  
    51  		// Initial values.
    52  		assert.DeepEqual(t, c1.GenesisForkVersion, c2.GenesisForkVersion, "%s: GenesisForkVersion", name)
    53  		assert.DeepEqual(t, c1.BLSWithdrawalPrefixByte, c2.BLSWithdrawalPrefixByte, "%s: BLSWithdrawalPrefixByte", name)
    54  
    55  		// Time parameters.
    56  		assert.Equal(t, c1.GenesisDelay, c2.GenesisDelay, "%s: GenesisDelay", name)
    57  		assert.Equal(t, c1.SecondsPerSlot, c2.SecondsPerSlot, "%s: SecondsPerSlot", name)
    58  		assert.Equal(t, c1.MinAttestationInclusionDelay, c2.MinAttestationInclusionDelay, "%s: MinAttestationInclusionDelay", name)
    59  		assert.Equal(t, c1.SlotsPerEpoch, c2.SlotsPerEpoch, "%s: SlotsPerEpoch", name)
    60  		assert.Equal(t, c1.MinSeedLookahead, c2.MinSeedLookahead, "%s: MinSeedLookahead", name)
    61  		assert.Equal(t, c1.MaxSeedLookahead, c2.MaxSeedLookahead, "%s: MaxSeedLookahead", name)
    62  		assert.Equal(t, c1.EpochsPerEth1VotingPeriod, c2.EpochsPerEth1VotingPeriod, "%s: EpochsPerEth1VotingPeriod", name)
    63  		assert.Equal(t, c1.SlotsPerHistoricalRoot, c2.SlotsPerHistoricalRoot, "%s: SlotsPerHistoricalRoot", name)
    64  		assert.Equal(t, c1.MinValidatorWithdrawabilityDelay, c2.MinValidatorWithdrawabilityDelay, "%s: MinValidatorWithdrawabilityDelay", name)
    65  		assert.Equal(t, c1.ShardCommitteePeriod, c2.ShardCommitteePeriod, "%s: ShardCommitteePeriod", name)
    66  		assert.Equal(t, c1.MinEpochsToInactivityPenalty, c2.MinEpochsToInactivityPenalty, "%s: MinEpochsToInactivityPenalty", name)
    67  
    68  		// State vector lengths.
    69  		assert.Equal(t, c1.EpochsPerHistoricalVector, c2.EpochsPerHistoricalVector, "%s: EpochsPerHistoricalVector", name)
    70  		assert.Equal(t, c1.EpochsPerSlashingsVector, c2.EpochsPerSlashingsVector, "%s: EpochsPerSlashingsVector", name)
    71  		assert.Equal(t, c1.HistoricalRootsLimit, c2.HistoricalRootsLimit, "%s: HistoricalRootsLimit", name)
    72  		assert.Equal(t, c1.ValidatorRegistryLimit, c2.ValidatorRegistryLimit, "%s: ValidatorRegistryLimit", name)
    73  
    74  		// Reward and penalty quotients.
    75  		assert.Equal(t, c1.BaseRewardFactor, c2.BaseRewardFactor, "%s: BaseRewardFactor", name)
    76  		assert.Equal(t, c1.WhistleBlowerRewardQuotient, c2.WhistleBlowerRewardQuotient, "%s: WhistleBlowerRewardQuotient", name)
    77  		assert.Equal(t, c1.ProposerRewardQuotient, c2.ProposerRewardQuotient, "%s: ProposerRewardQuotient", name)
    78  		assert.Equal(t, c1.InactivityPenaltyQuotient, c2.InactivityPenaltyQuotient, "%s: InactivityPenaltyQuotient", name)
    79  		assert.Equal(t, c1.MinSlashingPenaltyQuotient, c2.MinSlashingPenaltyQuotient, "%s: MinSlashingPenaltyQuotient", name)
    80  		assert.Equal(t, c1.ProportionalSlashingMultiplier, c2.ProportionalSlashingMultiplier, "%s: ProportionalSlashingMultiplier", name)
    81  
    82  		// Max operations per block.
    83  		assert.Equal(t, c1.MaxProposerSlashings, c2.MaxProposerSlashings, "%s: MaxProposerSlashings", name)
    84  		assert.Equal(t, c1.MaxAttesterSlashings, c2.MaxAttesterSlashings, "%s: MaxAttesterSlashings", name)
    85  		assert.Equal(t, c1.MaxAttestations, c2.MaxAttestations, "%s: MaxAttestations", name)
    86  		assert.Equal(t, c1.MaxDeposits, c2.MaxDeposits, "%s: MaxDeposits", name)
    87  		assert.Equal(t, c1.MaxVoluntaryExits, c2.MaxVoluntaryExits, "%s: MaxVoluntaryExits", name)
    88  
    89  		// Signature domains.
    90  		assert.Equal(t, c1.DomainBeaconProposer, c2.DomainBeaconProposer, "%s: DomainBeaconProposer", name)
    91  		assert.Equal(t, c1.DomainBeaconAttester, c2.DomainBeaconAttester, "%s: DomainBeaconAttester", name)
    92  		assert.Equal(t, c1.DomainRandao, c2.DomainRandao, "%s: DomainRandao", name)
    93  		assert.Equal(t, c1.DomainDeposit, c2.DomainDeposit, "%s: DomainDeposit", name)
    94  		assert.Equal(t, c1.DomainVoluntaryExit, c2.DomainVoluntaryExit, "%s: DomainVoluntaryExit", name)
    95  		assert.Equal(t, c1.DomainSelectionProof, c2.DomainSelectionProof, "%s: DomainSelectionProof", name)
    96  		assert.Equal(t, c1.DomainAggregateAndProof, c2.DomainAggregateAndProof, "%s: DomainAggregateAndProof", name)
    97  	}
    98  
    99  	t.Run("mainnet", func(t *testing.T) {
   100  		mainnetConfigFile := ConfigFilePath(t, "mainnet")
   101  		LoadChainConfigFile(mainnetConfigFile)
   102  		assertVals("mainnet", MainnetConfig(), BeaconConfig())
   103  	})
   104  
   105  	t.Run("minimal", func(t *testing.T) {
   106  		minimalConfigFile := ConfigFilePath(t, "minimal")
   107  		LoadChainConfigFile(minimalConfigFile)
   108  		assertVals("minimal", MinimalSpecConfig(), BeaconConfig())
   109  	})
   110  }
   111  
   112  func TestLoadConfigFile_OverwriteCorrectly(t *testing.T) {
   113  	file, err := ioutil.TempFile("", "")
   114  	require.NoError(t, err)
   115  	// Set current config to minimal config
   116  	OverrideBeaconConfig(MinimalSpecConfig())
   117  
   118  	// load empty config file, so that it defaults to mainnet values
   119  	LoadChainConfigFile(file.Name())
   120  	if BeaconConfig().MinGenesisTime != MainnetConfig().MinGenesisTime {
   121  		t.Errorf("Expected MinGenesisTime to be set to mainnet value: %d found: %d",
   122  			MainnetConfig().MinGenesisTime,
   123  			BeaconConfig().MinGenesisTime)
   124  	}
   125  	if BeaconConfig().SlotsPerEpoch != MainnetConfig().SlotsPerEpoch {
   126  		t.Errorf("Expected SlotsPerEpoch to be set to mainnet value: %d found: %d",
   127  			MainnetConfig().SlotsPerEpoch,
   128  			BeaconConfig().SlotsPerEpoch)
   129  	}
   130  }
   131  
   132  func Test_replaceHexStringWithYAMLFormat(t *testing.T) {
   133  
   134  	testLines := []struct {
   135  		line   string
   136  		wanted string
   137  	}{
   138  		{
   139  			line:   "ONE_BYTE: 0x41",
   140  			wanted: "ONE_BYTE: 65\n",
   141  		},
   142  		{
   143  			line:   "FOUR_BYTES: 0x41414141",
   144  			wanted: "FOUR_BYTES: \n- 65\n- 65\n- 65\n- 65\n",
   145  		},
   146  		{
   147  			line:   "THREE_BYTES: 0x414141",
   148  			wanted: "THREE_BYTES: \n- 65\n- 65\n- 65\n- 0\n",
   149  		},
   150  		{
   151  			line:   "EIGHT_BYTES: 0x4141414141414141",
   152  			wanted: "EIGHT_BYTES: \n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n",
   153  		},
   154  		{
   155  			line: "SIXTEEN_BYTES: 0x41414141414141414141414141414141",
   156  			wanted: "SIXTEEN_BYTES: \n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n" +
   157  				"- 65\n- 65\n- 65\n- 65\n",
   158  		},
   159  		{
   160  			line: "TWENTY_BYTES: 0x4141414141414141414141414141414141414141",
   161  			wanted: "TWENTY_BYTES: \n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n" +
   162  				"- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n",
   163  		},
   164  		{
   165  			line: "THIRTY_TWO_BYTES: 0x4141414141414141414141414141414141414141414141414141414141414141",
   166  			wanted: "THIRTY_TWO_BYTES: \n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n" +
   167  				"- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n" +
   168  				"- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n",
   169  		},
   170  		{
   171  			line: "FORTY_EIGHT_BYTES: 0x41414141414141414141414141414141414141414141414141414141414141414141" +
   172  				"4141414141414141414141414141",
   173  			wanted: "FORTY_EIGHT_BYTES: \n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n" +
   174  				"- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n" +
   175  				"- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n" +
   176  				"- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n",
   177  		},
   178  		{
   179  			line: "NINETY_SIX_BYTES: 0x414141414141414141414141414141414141414141414141414141414141414141414141" +
   180  				"4141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141" +
   181  				"41414141414141414141414141",
   182  			wanted: "NINETY_SIX_BYTES: \n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n" +
   183  				"- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n" +
   184  				"- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n" +
   185  				"- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n" +
   186  				"- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n" +
   187  				"- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n" +
   188  				"- 65\n- 65\n- 65\n- 65\n- 65\n- 65\n",
   189  		},
   190  	}
   191  	for _, line := range testLines {
   192  		parts := replaceHexStringWithYAMLFormat(line.line)
   193  		res := strings.Join(parts, "\n")
   194  
   195  		if res != line.wanted {
   196  			t.Errorf("expected conversion to be: %v got: %v", line.wanted, res)
   197  		}
   198  	}
   199  }
   200  
   201  // ConfigFilePath sets the proper config and returns the relevant
   202  // config file path from eth2-spec-tests directory.
   203  func ConfigFilePath(t *testing.T, config string) string {
   204  	configFolderPath := path.Join("tests", config)
   205  	filepath, err := bazel.Runfile(configFolderPath)
   206  	require.NoError(t, err)
   207  	configFilePath := path.Join(filepath, "config", "phase0.yaml")
   208  	return configFilePath
   209  }