public void MaximumPenalties() { // Arrange IServiceProvider testServiceProvider = TestSystem.BuildTestServiceProvider(); BeaconState state = TestState.PrepareTestState(testServiceProvider); StateListLengths stateListLengths = testServiceProvider.GetService <IOptions <StateListLengths> >().Value; BeaconStateAccessor beaconStateAccessor = testServiceProvider.GetService <BeaconStateAccessor>(); int slashedCount = (state.Validators.Count / 3) + 1; Epoch currentEpoch = beaconStateAccessor.GetCurrentEpoch(state); Epoch outEpoch = currentEpoch + new Epoch((ulong)stateListLengths.EpochsPerSlashingsVector / 2); var slashedIndices = Enumerable.Range(0, slashedCount).ToList(); SlashValidators(testServiceProvider, state, slashedIndices, Enumerable.Repeat(outEpoch, slashedCount)); Gwei totalBalance = beaconStateAccessor.GetTotalActiveBalance(state); Gwei totalPenalties = state.Slashings.Aggregate(Gwei.Zero, (accumulator, x) => accumulator + x); (totalBalance / 3).ShouldBeLessThanOrEqualTo(totalPenalties); // Act RunProcessSlashings(testServiceProvider, state); // Assert foreach (int index in slashedIndices) { state.Balances[index].ShouldBe(Gwei.Zero, $"Incorrect balance {index}"); } }
// Run ``process_voluntary_exit``, yielding: // - pre-state('pre') // - voluntary_exit('voluntary_exit') // - post-state('post'). //If ``valid == False``, run expecting ``AssertionError`` private void RunVoluntaryExitProcessing(IServiceProvider testServiceProvider, BeaconState state, VoluntaryExit voluntaryExit, bool expectValid) { TimeParameters timeParameters = testServiceProvider.GetService <IOptions <TimeParameters> >().Value; MaxOperationsPerBlock maxOperationsPerBlock = testServiceProvider.GetService <IOptions <MaxOperationsPerBlock> >().Value; ChainConstants chainConstants = testServiceProvider.GetService <ChainConstants>(); BeaconStateAccessor beaconStateAccessor = testServiceProvider.GetService <BeaconStateAccessor>(); BeaconStateTransition beaconStateTransition = testServiceProvider.GetService <BeaconStateTransition>(); // Act if (!expectValid) { Should.Throw <Exception>(() => { beaconStateTransition.ProcessVoluntaryExit(state, voluntaryExit); }); return; } ValidatorIndex validatorIndex = voluntaryExit.ValidatorIndex; Epoch preExitEpoch = state.Validators[(int)(ulong)validatorIndex].ExitEpoch; beaconStateTransition.ProcessVoluntaryExit(state, voluntaryExit); // Assert preExitEpoch.ShouldBe(chainConstants.FarFutureEpoch); Epoch postExitEpoch = state.Validators[(int)(ulong)validatorIndex].ExitEpoch; postExitEpoch.ShouldBeLessThan(chainConstants.FarFutureEpoch); }
public void BasicActivation() { // Arrange IServiceProvider testServiceProvider = TestSystem.BuildTestServiceProvider(); BeaconState state = TestState.PrepareTestState(testServiceProvider); ChainConstants chainConstants = testServiceProvider.GetService <ChainConstants>(); TimeParameters timeParameters = testServiceProvider.GetService <IOptions <TimeParameters> >().Value; BeaconChainUtility beaconChainUtility = testServiceProvider.GetService <BeaconChainUtility>(); BeaconStateAccessor beaconStateAccessor = testServiceProvider.GetService <BeaconStateAccessor>(); int index = 0; MockDeposit(testServiceProvider, state, index); for (ulong count = (ulong)0; count < (ulong)timeParameters.MaximumSeedLookahead + 1; count++) { TestState.NextEpoch(testServiceProvider, state); } // Act RunProcessRegistryUpdates(testServiceProvider, state); // Assert Validator validator = state.Validators[index]; validator.ActivationEligibilityEpoch.ShouldNotBe(chainConstants.FarFutureEpoch); validator.ActivationEpoch.ShouldNotBe(chainConstants.FarFutureEpoch); Epoch currentEpoch = beaconStateAccessor.GetCurrentEpoch(state); bool isActive = beaconChainUtility.IsActiveValidator(validator, currentEpoch); isActive.ShouldBeTrue(); }
/// <summary> /// Prepare the state for the deposit, and create a deposit for the given validator, depositing the given amount. /// </summary> public static Deposit PrepareStateAndDeposit(IServiceProvider testServiceProvider, BeaconState state, ValidatorIndex validatorIndex, Gwei amount, Bytes32 withdrawalCredentials, bool signed) { ChainConstants chainConstants = testServiceProvider.GetService <ChainConstants>(); InitialValues initialValues = testServiceProvider.GetService <IOptions <InitialValues> >().Value; TimeParameters timeParameters = testServiceProvider.GetService <IOptions <TimeParameters> >().Value; BeaconChainUtility beaconChainUtility = testServiceProvider.GetService <BeaconChainUtility>(); BeaconStateAccessor beaconStateAccessor = testServiceProvider.GetService <BeaconStateAccessor>(); byte[][] privateKeys = TestKeys.PrivateKeys(timeParameters).ToArray(); BlsPublicKey[] publicKeys = TestKeys.PublicKeys(timeParameters).ToArray(); byte[] privateKey = privateKeys[(int)(ulong)validatorIndex]; BlsPublicKey publicKey = publicKeys[(int)(ulong)validatorIndex]; if (withdrawalCredentials == Bytes32.Zero) { // insecurely use pubkey as withdrawal key if no credentials provided byte[] withdrawalCredentialBytes = TestSecurity.Hash(publicKey.AsSpan()); withdrawalCredentialBytes[0] = initialValues.BlsWithdrawalPrefix; withdrawalCredentials = new Bytes32(withdrawalCredentialBytes); } List <DepositData> depositDataList = new List <DepositData>(); (Deposit deposit, Root depositRoot) = BuildDeposit(testServiceProvider, state, depositDataList, publicKey, privateKey, amount, withdrawalCredentials, signed); state.SetEth1DepositIndex(0); state.Eth1Data.SetDepositRoot(depositRoot); state.Eth1Data.SetDepositCount((ulong)depositDataList.Count); return(deposit); }
// def get_valid_attestation(spec, state, slot=None, index=None, signed=False): public static Attestation GetValidAttestation(IServiceProvider testServiceProvider, BeaconState state, Slot slot, CommitteeIndex index, bool signed) { BeaconStateAccessor beaconStateAccessor = testServiceProvider.GetService <BeaconStateAccessor>(); if (slot == Slot.None) { slot = state.Slot; } if (index == CommitteeIndex.None) { index = new CommitteeIndex(0); } AttestationData attestationData = BuildAttestationData(testServiceProvider, state, slot, index); IReadOnlyList <ValidatorIndex> beaconCommittee = beaconStateAccessor.GetBeaconCommittee(state, attestationData.Slot, attestationData.Index); int committeeSize = beaconCommittee.Count; BitArray aggregationBits = new BitArray(committeeSize); Attestation attestation = new Attestation(aggregationBits, attestationData, BlsSignature.Zero); FillAggregateAttestation(state, attestation, beaconStateAccessor); if (signed) { SignAttestation(testServiceProvider, state, attestation); } return(attestation); }
public static AttesterSlashing GetValidAttesterSlashing(IServiceProvider testServiceProvider, BeaconState state, bool signed1, bool signed2) { TimeParameters timeParameters = testServiceProvider.GetService <IOptions <TimeParameters> >().Value; BeaconStateAccessor beaconStateAccessor = testServiceProvider.GetService <BeaconStateAccessor>(); Attestation attestation1 = TestAttestation.GetValidAttestation(testServiceProvider, state, Slot.None, CommitteeIndex.None, signed1); Hash32 targetRoot2 = new Hash32(Enumerable.Repeat((byte)0x01, 32).ToArray()); Attestation attestation2 = new Attestation( attestation1.AggregationBits, new AttestationData(attestation1.Data.Slot, attestation1.Data.Index, attestation1.Data.BeaconBlockRoot, attestation1.Data.Source, new Checkpoint( attestation1.Data.Target.Epoch, targetRoot2 )), BlsSignature.Empty ); if (signed2) { TestAttestation.SignAttestation(testServiceProvider, state, attestation2); } IndexedAttestation indexedAttestation1 = beaconStateAccessor.GetIndexedAttestation(state, attestation1); IndexedAttestation indexedAttestation2 = beaconStateAccessor.GetIndexedAttestation(state, attestation2); AttesterSlashing attesterSlashing = new AttesterSlashing(indexedAttestation1, indexedAttestation2); return(attesterSlashing); }
private async Task RunOnAttestation(IServiceProvider testServiceProvider, BeaconState state, IStore store, Attestation attestation, bool expectValid) { MiscellaneousParameters miscellaneousParameters = testServiceProvider.GetService <IOptions <MiscellaneousParameters> >().Value; MaxOperationsPerBlock maxOperationsPerBlock = testServiceProvider.GetService <IOptions <MaxOperationsPerBlock> >().Value; IForkChoice forkChoice = testServiceProvider.GetService <IForkChoice>(); if (!expectValid) { Should.Throw <Exception>(async() => { await forkChoice.OnAttestationAsync(store, attestation); }); return; } BeaconStateAccessor beaconStateAccessor = testServiceProvider.GetService <BeaconStateAccessor>(); IndexedAttestation indexedAttestation = beaconStateAccessor.GetIndexedAttestation(state, attestation); await forkChoice.OnAttestationAsync(store, attestation); IEnumerable <ValidatorIndex> attestingIndices = beaconStateAccessor.GetAttestingIndices(state, attestation.Data, attestation.AggregationBits); ValidatorIndex firstAttestingIndex = attestingIndices.First(); LatestMessage latestMessage = (await store.GetLatestMessageAsync(firstAttestingIndex, true)) !; latestMessage.Epoch.ShouldBe(attestation.Data.Target.Epoch); latestMessage.Root.ShouldBe(attestation.Data.BeaconBlockRoot); }
public void InvalidSignature() { // Arrange IServiceProvider testServiceProvider = TestSystem.BuildTestServiceProvider(); BeaconState state = TestState.PrepareTestState(testServiceProvider); TimeParameters timeParameters = testServiceProvider.GetService <IOptions <TimeParameters> >().Value; BeaconStateAccessor beaconStateAccessor = testServiceProvider.GetService <BeaconStateAccessor>(); // move state forward PERSISTENT_COMMITTEE_PERIOD epochs to allow for exit ulong move = timeParameters.SlotsPerEpoch * (ulong)timeParameters.PersistentCommitteePeriod; ulong newSlot = state.Slot + move; state.SetSlot((Slot)newSlot); Epoch currentEpoch = beaconStateAccessor.GetCurrentEpoch(state); ValidatorIndex validatorIndex = beaconStateAccessor.GetActiveValidatorIndices(state, currentEpoch)[0]; Validator validator = state.Validators[(int)(ulong)validatorIndex]; byte[] privateKey = TestKeys.PublicKeyToPrivateKey(validator.PublicKey, timeParameters); VoluntaryExit voluntaryExit = TestVoluntaryExit.BuildVoluntaryExit(testServiceProvider, currentEpoch, validatorIndex); SignedVoluntaryExit signedVoluntaryExit = new SignedVoluntaryExit(voluntaryExit, BlsSignature.Zero); RunVoluntaryExitProcessing(testServiceProvider, state, signedVoluntaryExit, expectValid: false); }
public static void SignDepositData(IServiceProvider testServiceProvider, DepositData depositData, byte[] privateKey, BeaconState?state) { SignatureDomains signatureDomains = testServiceProvider.GetService <IOptions <SignatureDomains> >().Value; BeaconChainUtility beaconChainUtility = testServiceProvider.GetService <BeaconChainUtility>(); ICryptographyService cryptographyService = testServiceProvider.GetService <ICryptographyService>(); Domain domain; if (state == null) { // Genesis domain = beaconChainUtility.ComputeDomain(signatureDomains.Deposit); } else { BeaconStateAccessor beaconStateAccessor = testServiceProvider.GetService <BeaconStateAccessor>(); domain = beaconStateAccessor.GetDomain(state, signatureDomains.Deposit, Epoch.None); } DepositMessage depositMessage = new DepositMessage(depositData.PublicKey, depositData.WithdrawalCredentials, depositData.Amount); Root depositMessageRoot = cryptographyService.HashTreeRoot(depositMessage); Root signingRoot = beaconChainUtility.ComputeSigningRoot(depositMessageRoot, domain); BlsSignature signature = TestSecurity.BlsSign(signingRoot, privateKey); depositData.SetSignature(signature); }
public void ProposerIndexForFirstTwoEpochsMustBeValid() { // Arrange IServiceCollection testServiceCollection = TestSystem.BuildTestServiceCollection(useStore: true); testServiceCollection.AddSingleton <IHostEnvironment>(Substitute.For <IHostEnvironment>()); ServiceProvider testServiceProvider = testServiceCollection.BuildServiceProvider(); BeaconState state = TestState.PrepareTestState(testServiceProvider); ForkChoice forkChoice = testServiceProvider.GetService <ForkChoice>(); // Get genesis store initialise MemoryStoreProvider with the state IStore store = forkChoice.GetGenesisStore(state); // Move forward time BeaconStateAccessor beaconStateAccessor = testServiceProvider.GetService <BeaconStateAccessor>(); TimeParameters timeParameters = testServiceProvider.GetService <IOptions <TimeParameters> >().Value; ulong time = state.GenesisTime + 1; ulong nextSlotTime = state.GenesisTime; ValidatorIndex maximumValidatorIndex = new ValidatorIndex((ulong)state.Validators.Count - 1); ValidatorIndex validatorIndex0 = beaconStateAccessor.GetBeaconProposerIndex(state); Console.WriteLine("Slot {0}, time {1} = proposer index {2}", state.Slot, time, validatorIndex0); validatorIndex0.ShouldBeLessThanOrEqualTo(maximumValidatorIndex); List <ValidatorIndex> proposerIndexes = new List <ValidatorIndex>(); proposerIndexes.Add(validatorIndex0); for (int slotIndex = 1; slotIndex <= 16; slotIndex++) { // Slot 1 nextSlotTime = nextSlotTime + timeParameters.SecondsPerSlot; while (time < nextSlotTime) { forkChoice.OnTick(store, time); time++; } forkChoice.OnTick(store, time); time++; BeaconBlock block = TestBlock.BuildEmptyBlockForNextSlot(testServiceProvider, state, signed: true); TestState.StateTransitionAndSignBlock(testServiceProvider, state, block); forkChoice.OnBlock(store, block); forkChoice.OnTick(store, time); time++; ValidatorIndex validatorIndex = beaconStateAccessor.GetBeaconProposerIndex(state); Console.WriteLine("Slot {0}, time {1} = proposer index {2}", state.Slot, time, validatorIndex); validatorIndex.ShouldBeLessThanOrEqualTo(maximumValidatorIndex); proposerIndexes.Add(validatorIndex); } for (var slotIndex = 0; slotIndex < proposerIndexes.Count; slotIndex++) { ValidatorIndex proposerIndex = proposerIndexes[slotIndex]; Validator proposer = state.Validators[(int)(ulong)proposerIndex]; Console.WriteLine("Slot {0} = proposer index {1}, public key {2}", slotIndex, proposerIndex, proposer.PublicKey); } }
public static void SignAttestation(IServiceProvider testServiceProvider, BeaconState state, Attestation attestation) { BeaconStateAccessor beaconStateAccessor = testServiceProvider.GetService <BeaconStateAccessor>(); IReadOnlyList <ValidatorIndex> participants = beaconStateAccessor.GetAttestingIndices(state, attestation.Data, attestation.AggregationBits); BlsSignature signature = SignAggregateAttestation(testServiceProvider, state, attestation.Data, participants); attestation.SetSignature(signature); }
private static void FillAggregateAttestation(BeaconState state, Attestation attestation, BeaconStateAccessor beaconStateAccessor) { var beaconCommittee = beaconStateAccessor.GetBeaconCommittee(state, attestation.Data.Slot, attestation.Data.Index); for (var i = 0; i < beaconCommittee.Count; i++) { attestation.AggregationBits[i] = true; } }
private static void FillAggregateAttestation(BeaconState state, Attestation attestation, BeaconStateAccessor beaconStateAccessor) { IReadOnlyList <ValidatorIndex> beaconCommittee = beaconStateAccessor.GetBeaconCommittee(state, attestation.Data.Slot, attestation.Data.Index); for (int i = 0; i < beaconCommittee.Count; i++) { attestation.AggregationBits[i] = true; } }
public async Task GenesisWithEmptyParametersTimeShouldReject() { // Arrange IServiceProvider testServiceProvider = TestSystem.BuildTestServiceProvider(); ChainConstants chainConstants = testServiceProvider.GetService <ChainConstants>(); IOptionsMonitor <MiscellaneousParameters> miscellaneousParameterOptions = testServiceProvider.GetService <IOptionsMonitor <MiscellaneousParameters> >(); IOptionsMonitor <GweiValues> gweiValueOptions = testServiceProvider.GetService <IOptionsMonitor <GweiValues> >(); IOptionsMonitor <InitialValues> initialValueOptions = testServiceProvider.GetService <IOptionsMonitor <InitialValues> >(); IOptionsMonitor <TimeParameters> timeParameterOptions = testServiceProvider.GetService <IOptionsMonitor <TimeParameters> >(); IOptionsMonitor <StateListLengths> stateListLengthOptions = testServiceProvider.GetService <IOptionsMonitor <StateListLengths> >(); IOptionsMonitor <RewardsAndPenalties> rewardsAndPenaltiesOptions = testServiceProvider.GetService <IOptionsMonitor <RewardsAndPenalties> >(); IOptionsMonitor <MaxOperationsPerBlock> maxOperationsPerBlockOptions = testServiceProvider.GetService <IOptionsMonitor <MaxOperationsPerBlock> >(); IOptionsMonitor <ForkChoiceConfiguration> forkChoiceConfigurationOptions = testServiceProvider.GetService <IOptionsMonitor <ForkChoiceConfiguration> >(); IOptionsMonitor <SignatureDomains> signatureDomainOptions = testServiceProvider.GetService <IOptionsMonitor <SignatureDomains> >(); miscellaneousParameterOptions.CurrentValue.MinimumGenesisActiveValidatorCount = 2; LoggerFactory loggerFactory = new LoggerFactory(new[] { new ConsoleLoggerProvider(TestOptionsMonitor.Create(new ConsoleLoggerOptions())) }); ICryptographyService cryptographyService = testServiceProvider.GetService <ICryptographyService>(); BeaconChainUtility beaconChainUtility = new BeaconChainUtility(loggerFactory.CreateLogger <BeaconChainUtility>(), miscellaneousParameterOptions, gweiValueOptions, timeParameterOptions, cryptographyService); BeaconStateAccessor beaconStateAccessor = new BeaconStateAccessor(miscellaneousParameterOptions, initialValueOptions, timeParameterOptions, stateListLengthOptions, signatureDomainOptions, cryptographyService, beaconChainUtility); BeaconStateMutator beaconStateMutator = new BeaconStateMutator(chainConstants, timeParameterOptions, stateListLengthOptions, rewardsAndPenaltiesOptions, beaconChainUtility, beaconStateAccessor); BeaconStateTransition beaconStateTransition = new BeaconStateTransition(loggerFactory.CreateLogger <BeaconStateTransition>(), chainConstants, miscellaneousParameterOptions, gweiValueOptions, initialValueOptions, timeParameterOptions, stateListLengthOptions, rewardsAndPenaltiesOptions, maxOperationsPerBlockOptions, signatureDomainOptions, cryptographyService, beaconChainUtility, beaconStateAccessor, beaconStateMutator); BeaconNode.Genesis beaconChain = new BeaconNode.Genesis(loggerFactory.CreateLogger <BeaconNode.Genesis>(), chainConstants, miscellaneousParameterOptions, gweiValueOptions, initialValueOptions, timeParameterOptions, stateListLengthOptions, maxOperationsPerBlockOptions, cryptographyService, beaconStateAccessor, beaconStateTransition); MemoryStoreProvider storeProvider = new MemoryStoreProvider(loggerFactory, timeParameterOptions); ForkChoice forkChoice = new ForkChoice(loggerFactory.CreateLogger <ForkChoice>(), miscellaneousParameterOptions, initialValueOptions, timeParameterOptions, stateListLengthOptions, maxOperationsPerBlockOptions, forkChoiceConfigurationOptions, signatureDomainOptions, cryptographyService, beaconChainUtility, beaconStateAccessor, beaconStateTransition, storeProvider); ChainStart chainStart = new ChainStart(loggerFactory.CreateLogger <ChainStart>(), beaconChain, forkChoice); // Act Hash32 eth1BlockHash = Hash32.Zero; ulong eth1Timestamp = 106185600uL; // 1973-05-14 Deposit[] deposits = Array.Empty <Deposit>(); bool success = await chainStart.TryGenesisAsync(eth1BlockHash, eth1Timestamp, deposits); // Assert success.ShouldBeFalse(); }
public static void SignBlock(IServiceProvider testServiceProvider, BeaconState state, BeaconBlock block, ValidatorIndex proposerIndex) { MiscellaneousParameters miscellaneousParameters = testServiceProvider.GetService <IOptions <MiscellaneousParameters> >().Value; TimeParameters timeParameters = testServiceProvider.GetService <IOptions <TimeParameters> >().Value; MaxOperationsPerBlock maxOperationsPerBlock = testServiceProvider.GetService <IOptions <MaxOperationsPerBlock> >().Value; SignatureDomains signatureDomains = testServiceProvider.GetService <IOptions <SignatureDomains> >().Value; ICryptographyService cryptographyService = testServiceProvider.GetService <ICryptographyService>(); BeaconChainUtility beaconChainUtility = testServiceProvider.GetService <BeaconChainUtility>(); BeaconStateAccessor beaconStateAccessor = testServiceProvider.GetService <BeaconStateAccessor>(); BeaconStateTransition beaconStateTransition = testServiceProvider.GetService <BeaconStateTransition>(); if (state.Slot > block.Slot) { throw new ArgumentOutOfRangeException("block.Slot", block.Slot, $"Slot of block must be equal or less that state slot {state.Slot}"); } Epoch blockEpoch = beaconChainUtility.ComputeEpochAtSlot(block.Slot); if (proposerIndex == ValidatorIndex.None) { if (block.Slot == state.Slot) { proposerIndex = beaconStateAccessor.GetBeaconProposerIndex(state); } else { Epoch stateEpoch = beaconChainUtility.ComputeEpochAtSlot(state.Slot); if (stateEpoch + 1 > blockEpoch) { Console.WriteLine("WARNING: Block slot far away, and no proposer index manually given." + " Signing block is slow due to transition for proposer index calculation."); } // use stub state to get proposer index of future slot BeaconState stubState = BeaconState.Clone(state); beaconStateTransition.ProcessSlots(stubState, block.Slot); proposerIndex = beaconStateAccessor.GetBeaconProposerIndex(stubState); } } byte[][] privateKeys = TestKeys.PrivateKeys(timeParameters).ToArray(); byte[] privateKey = privateKeys[(int)(ulong)proposerIndex]; Domain randaoDomain = beaconStateAccessor.GetDomain(state, signatureDomains.Randao, blockEpoch); Hash32 randaoRevealHash = blockEpoch.HashTreeRoot(); BlsSignature randaoReveal = TestSecurity.BlsSign(randaoRevealHash, privateKey, randaoDomain); block.Body.SetRandaoReveal(randaoReveal); Domain signatureDomain = beaconStateAccessor.GetDomain(state, signatureDomains.BeaconProposer, blockEpoch); Hash32 signingRoot = cryptographyService.SigningRoot(block); BlsSignature signature = TestSecurity.BlsSign(signingRoot, privateKey, signatureDomain); block.SetSignature(signature); }
public SimpleLatestMessageDrivenGreedyHeaviestObservedSubtree( ILogger <SimpleLatestMessageDrivenGreedyHeaviestObservedSubtree> logger, ChainConstants chainConstants, BeaconChainUtility beaconChainUtility, BeaconStateAccessor beaconStateAccessor) { _logger = logger; _chainConstants = chainConstants; _beaconChainUtility = beaconChainUtility; _beaconStateAccessor = beaconStateAccessor; }
public QuickStartMockEth1DataProvider(IOptionsMonitor <TimeParameters> timeParameterOptions, ICryptographyService cryptographyService, BeaconStateAccessor beaconStateAccessor, IForkChoice forkChoice, IStore store) { _timeParameterOptions = timeParameterOptions; _cryptographyService = cryptographyService; _beaconStateAccessor = beaconStateAccessor; _forkChoice = forkChoice; _store = store; }
public static SignedBeaconBlock SignBlock(IServiceProvider testServiceProvider, BeaconState state, BeaconBlock block, ValidatorIndex?optionalProposerIndex) { TimeParameters timeParameters = testServiceProvider.GetService <IOptions <TimeParameters> >().Value; SignatureDomains signatureDomains = testServiceProvider.GetService <IOptions <SignatureDomains> >().Value; ICryptographyService cryptographyService = testServiceProvider.GetService <ICryptographyService>(); BeaconChainUtility beaconChainUtility = testServiceProvider.GetService <BeaconChainUtility>(); BeaconStateAccessor beaconStateAccessor = testServiceProvider.GetService <BeaconStateAccessor>(); BeaconStateTransition beaconStateTransition = testServiceProvider.GetService <BeaconStateTransition>(); if (state.Slot > block.Slot) { throw new ArgumentOutOfRangeException("block.Slot", block.Slot, $"Slot of block must be equal or less that state slot {state.Slot}"); } Epoch blockEpoch = beaconChainUtility.ComputeEpochAtSlot(block.Slot); ValidatorIndex proposerIndex; if (optionalProposerIndex.HasValue) { proposerIndex = optionalProposerIndex.Value; } else { if (block.Slot == state.Slot) { proposerIndex = beaconStateAccessor.GetBeaconProposerIndex(state); } else { Epoch stateEpoch = beaconChainUtility.ComputeEpochAtSlot(state.Slot); if (stateEpoch + 1 > blockEpoch) { Console.WriteLine("WARNING: Block slot far away, and no proposer index manually given." + " Signing block is slow due to transition for proposer index calculation."); } // use stub state to get proposer index of future slot BeaconState stubState = BeaconState.Clone(state); beaconStateTransition.ProcessSlots(stubState, block.Slot); proposerIndex = beaconStateAccessor.GetBeaconProposerIndex(stubState); } } byte[][] privateKeys = TestKeys.PrivateKeys(timeParameters).ToArray(); byte[] privateKey = privateKeys[(int)(ulong)proposerIndex]; Root blockHashTreeRoot = cryptographyService.HashTreeRoot(block); Domain proposerDomain = beaconStateAccessor.GetDomain(state, signatureDomains.BeaconProposer, blockEpoch); Root signingRoot = beaconChainUtility.ComputeSigningRoot(blockHashTreeRoot, proposerDomain); BlsSignature signature = TestSecurity.BlsSign(signingRoot, privateKey); return(new SignedBeaconBlock(block, signature)); }
public static BlsSignature GetAttestationSignature(IServiceProvider testServiceProvider, BeaconState state, AttestationData attestationData, byte[] privateKey) { SignatureDomains signatureDomains = testServiceProvider.GetService <IOptions <SignatureDomains> >().Value; BeaconChainUtility beaconChainUtility = testServiceProvider.GetService <BeaconChainUtility>(); BeaconStateAccessor beaconStateAccessor = testServiceProvider.GetService <BeaconStateAccessor>(); Root attestationDataRoot = attestationData.HashTreeRoot(); Domain domain = beaconStateAccessor.GetDomain(state, signatureDomains.BeaconAttester, attestationData.Target.Epoch); Root signingRoot = beaconChainUtility.ComputeSigningRoot(attestationDataRoot, domain); BlsSignature signature = TestSecurity.BlsSign(signingRoot, privateKey); return(signature); }
public MockedEth1DataProvider(IOptionsMonitor <TimeParameters> timeParameterOptions, IOptionsMonitor <HonestValidatorConstants> honestValidatorConstantOptions, ICryptographyService cryptographyService, BeaconStateAccessor beaconStateAccessor, ForkChoice forkChoice, IStoreProvider storeProvider) { _timeParameterOptions = timeParameterOptions; _honestValidatorConstantOptions = honestValidatorConstantOptions; _cryptographyService = cryptographyService; _beaconStateAccessor = beaconStateAccessor; _forkChoice = forkChoice; _storeProvider = storeProvider; }
public static SignedBeaconBlockHeader SignBlockHeader(IServiceProvider testServiceProvider, BeaconState state, BeaconBlockHeader header, byte[] privateKey) { SignatureDomains signatureDomains = testServiceProvider.GetService <IOptions <SignatureDomains> >().Value; IBeaconChainUtility beaconChainUtility = testServiceProvider.GetService <IBeaconChainUtility>(); BeaconStateAccessor beaconStateAccessor = testServiceProvider.GetService <BeaconStateAccessor>(); ICryptographyService cryptographyService = testServiceProvider.GetService <ICryptographyService>(); Root headerRoot = cryptographyService.HashTreeRoot(header); Domain domain = beaconStateAccessor.GetDomain(state, signatureDomains.BeaconProposer, Epoch.None); Root signingRoot = beaconChainUtility.ComputeSigningRoot(headerRoot, domain); BlsSignature signature = TestSecurity.BlsSign(signingRoot, privateKey); return(new SignedBeaconBlockHeader(header, signature)); }
public async Task GenesisWithEmptyParametersTimeShouldReject() { // Arrange var testServiceProvider = TestSystem.BuildTestServiceProvider(); var chainConstants = testServiceProvider.GetService <ChainConstants>(); var miscellaneousParameterOptions = testServiceProvider.GetService <IOptionsMonitor <MiscellaneousParameters> >(); var gweiValueOptions = testServiceProvider.GetService <IOptionsMonitor <GweiValues> >(); var initialValueOptions = testServiceProvider.GetService <IOptionsMonitor <InitialValues> >(); var timeParameterOptions = testServiceProvider.GetService <IOptionsMonitor <TimeParameters> >(); var stateListLengthOptions = testServiceProvider.GetService <IOptionsMonitor <StateListLengths> >(); var rewardsAndPenaltiesOptions = testServiceProvider.GetService <IOptionsMonitor <RewardsAndPenalties> >(); var maxOperationsPerBlockOptions = testServiceProvider.GetService <IOptionsMonitor <MaxOperationsPerBlock> >(); var signatureDomainOptions = testServiceProvider.GetService <IOptionsMonitor <SignatureDomains> >(); miscellaneousParameterOptions.CurrentValue.MinimumGenesisActiveValidatorCount = 2; var loggerFactory = new LoggerFactory(new[] { new ConsoleLoggerProvider(TestOptionsMonitor.Create(new ConsoleLoggerOptions())) }); var cryptographyService = new CryptographyService(); var beaconChainUtility = new BeaconChainUtility(loggerFactory.CreateLogger <BeaconChainUtility>(), miscellaneousParameterOptions, gweiValueOptions, timeParameterOptions, cryptographyService); var beaconStateAccessor = new BeaconStateAccessor(miscellaneousParameterOptions, initialValueOptions, timeParameterOptions, stateListLengthOptions, signatureDomainOptions, cryptographyService, beaconChainUtility); var beaconStateMutator = new BeaconStateMutator(chainConstants, timeParameterOptions, stateListLengthOptions, rewardsAndPenaltiesOptions, beaconChainUtility, beaconStateAccessor); var beaconStateTransition = new BeaconStateTransition(loggerFactory.CreateLogger <BeaconStateTransition>(), chainConstants, miscellaneousParameterOptions, gweiValueOptions, initialValueOptions, timeParameterOptions, stateListLengthOptions, rewardsAndPenaltiesOptions, maxOperationsPerBlockOptions, signatureDomainOptions, cryptographyService, beaconChainUtility, beaconStateAccessor, beaconStateMutator); var beaconChain = new BeaconChain(loggerFactory.CreateLogger <BeaconChain>(), chainConstants, miscellaneousParameterOptions, gweiValueOptions, initialValueOptions, timeParameterOptions, stateListLengthOptions, maxOperationsPerBlockOptions, cryptographyService, beaconChainUtility, beaconStateAccessor, beaconStateMutator, beaconStateTransition); // Act var eth1BlockHash = Hash32.Zero; var eth1Timestamp = (ulong)106185600; // 1973-05-14 var deposits = Array.Empty <Deposit>(); var success = await beaconChain.TryGenesisAsync(eth1BlockHash, eth1Timestamp, deposits); // Assert success.ShouldBeFalse(); beaconChain.State.ShouldBeNull(); }
public void ActivationQueueSorting() { // Arrange IServiceProvider testServiceProvider = TestSystem.BuildTestServiceProvider(); BeaconState state = TestState.PrepareTestState(testServiceProvider); ChainConstants chainConstants = testServiceProvider.GetService <ChainConstants>(); BeaconStateAccessor beaconStateAccessor = testServiceProvider.GetService <BeaconStateAccessor>(); int mockActivations = 10; Epoch currentEpoch = beaconStateAccessor.GetCurrentEpoch(state); for (int index = 0; index < mockActivations; index++) { MockDeposit(testServiceProvider, state, index); state.Validators[index].SetEligible(currentEpoch + Epoch.One); } // eligibility must be finalized state.SetFinalizedCheckpoint(new Checkpoint(currentEpoch + new Epoch(2), Root.Zero)); // give the last priority over the others state.Validators[mockActivations - 1].SetEligible(currentEpoch); // make sure we are hitting the churn int churnLimit = (int)beaconStateAccessor.GetValidatorChurnLimit(state); mockActivations.ShouldBeGreaterThan(churnLimit); // Act RunProcessRegistryUpdates(testServiceProvider, state); // Assert //# the first got in as second state.Validators[0].ActivationEpoch.ShouldNotBe(chainConstants.FarFutureEpoch); //# the prioritized got in as first state.Validators[mockActivations - 1].ActivationEpoch.ShouldNotBe(chainConstants.FarFutureEpoch); //# the second last is at the end of the queue, and did not make the churn, //# hence is not assigned an activation_epoch yet. state.Validators[mockActivations - 2].ActivationEpoch.ShouldBe(chainConstants.FarFutureEpoch); //# the one at churn_limit - 1 did not make it, it was out-prioritized state.Validators[churnLimit - 1].ActivationEpoch.ShouldBe(chainConstants.FarFutureEpoch); //# but the the one in front of the above did state.Validators[churnLimit - 2].ActivationEpoch.ShouldNotBe(chainConstants.FarFutureEpoch); }
public void Ejection() { // Arrange IServiceProvider testServiceProvider = TestSystem.BuildTestServiceProvider(); BeaconState state = TestState.PrepareTestState(testServiceProvider); ChainConstants chainConstants = testServiceProvider.GetService <ChainConstants>(); GweiValues gweiValues = testServiceProvider.GetService <IOptions <GweiValues> >().Value; TimeParameters timeParameters = testServiceProvider.GetService <IOptions <TimeParameters> >().Value; BeaconChainUtility beaconChainUtility = testServiceProvider.GetService <BeaconChainUtility>(); BeaconStateAccessor beaconStateAccessor = testServiceProvider.GetService <BeaconStateAccessor>(); int index = 0; Epoch currentEpoch = beaconStateAccessor.GetCurrentEpoch(state); Validator validator = state.Validators[index]; bool isActive = beaconChainUtility.IsActiveValidator(validator, currentEpoch); isActive.ShouldBeTrue(); validator.ExitEpoch.ShouldBe(chainConstants.FarFutureEpoch); // Mock an ejection state.Validators[index].SetEffectiveBalance(gweiValues.EjectionBalance); for (ulong count = (ulong)0; count < (ulong)timeParameters.MaximumSeedLookahead + 1; count++) { TestState.NextEpoch(testServiceProvider, state); } // Act RunProcessRegistryUpdates(testServiceProvider, state); // Assert Epoch epochAfter = beaconStateAccessor.GetCurrentEpoch(state); Validator validatorAfter = state.Validators[index]; bool isActiveAfter = beaconChainUtility.IsActiveValidator(validatorAfter, epochAfter); isActiveAfter.ShouldBeFalse(); validatorAfter.ExitEpoch.ShouldNotBe(chainConstants.FarFutureEpoch); }
public static (IList <Deposit>, Root) PrepareGenesisDeposits(IServiceProvider testServiceProvider, int genesisValidatorCount, Gwei amount, bool signed) { ChainConstants chainConstants = testServiceProvider.GetService <ChainConstants>(); MiscellaneousParameters miscellaneousParameters = testServiceProvider.GetService <IOptions <MiscellaneousParameters> >().Value; InitialValues initialValues = testServiceProvider.GetService <IOptions <InitialValues> >().Value; TimeParameters timeParameters = testServiceProvider.GetService <IOptions <TimeParameters> >().Value; MaxOperationsPerBlock maxOperationsPerBlock = testServiceProvider.GetService <IOptions <MaxOperationsPerBlock> >().Value; BeaconChainUtility beaconChainUtility = testServiceProvider.GetService <BeaconChainUtility>(); BeaconStateAccessor beaconStateAccessor = testServiceProvider.GetService <BeaconStateAccessor>(); BeaconStateTransition beaconStateTransition = testServiceProvider.GetService <BeaconStateTransition>(); byte[][] privateKeys = TestKeys.PrivateKeys(timeParameters).ToArray(); BlsPublicKey[] publicKeys; if (signed) { publicKeys = TestKeys.PublicKeys(timeParameters).ToArray(); } else { publicKeys = privateKeys.Select(x => new BlsPublicKey(x)).ToArray(); } List <DepositData> depositDataList = new List <DepositData>(); List <Deposit> genesisDeposits = new List <Deposit>(); Root root = Root.Zero; for (int validatorIndex = 0; validatorIndex < genesisValidatorCount; validatorIndex++) { BlsPublicKey publicKey = publicKeys[validatorIndex]; byte[] privateKey = privateKeys[validatorIndex]; // insecurely use pubkey as withdrawal key if no credentials provided byte[] withdrawalCredentialBytes = TestSecurity.Hash(publicKey.AsSpan()); withdrawalCredentialBytes[0] = initialValues.BlsWithdrawalPrefix; Bytes32 withdrawalCredentials = new Bytes32(withdrawalCredentialBytes); (Deposit deposit, Root depositRoot) = BuildDeposit(testServiceProvider, null, depositDataList, publicKey, privateKey, amount, withdrawalCredentials, signed); root = depositRoot; genesisDeposits.Add(deposit); } return(genesisDeposits, root); }
private void MockDeposit(IServiceProvider testServiceProvider, BeaconState state, int index) { ChainConstants chainConstants = testServiceProvider.GetService <ChainConstants>(); GweiValues gweiValues = testServiceProvider.GetService <IOptions <GweiValues> >().Value; BeaconChainUtility beaconChainUtility = testServiceProvider.GetService <BeaconChainUtility>(); BeaconStateAccessor beaconStateAccessor = testServiceProvider.GetService <BeaconStateAccessor>(); Validator validator = state.Validators[index]; Epoch currentEpoch = beaconStateAccessor.GetCurrentEpoch(state); bool isActive = beaconChainUtility.IsActiveValidator(validator, currentEpoch); isActive.ShouldBeTrue(); validator.SetEligible(chainConstants.FarFutureEpoch); validator.SetActive(chainConstants.FarFutureEpoch); validator.SetEffectiveBalance(gweiValues.MaximumEffectiveBalance); bool isActiveAfter = beaconChainUtility.IsActiveValidator(validator, currentEpoch); isActiveAfter.ShouldBeFalse(); }
private void SlashValidators(IServiceProvider testServiceProvider, BeaconState state, IEnumerable <int> indices, IEnumerable <Epoch> outEpochs) { StateListLengths stateListLengths = testServiceProvider.GetService <IOptions <StateListLengths> >().Value; BeaconStateAccessor beaconStateAccessor = testServiceProvider.GetService <BeaconStateAccessor>(); BeaconStateMutator beaconStateMutator = testServiceProvider.GetService <BeaconStateMutator>(); Gwei totalSlashedBalance = Gwei.Zero; var items = indices.Zip(outEpochs, (index, outEpoch) => new { index, outEpoch }); foreach (var item in items) { Validator validator = state.Validators[item.index]; validator.SetSlashed(); beaconStateMutator.InitiateValidatorExit(state, new ValidatorIndex((ulong)item.index)); validator.SetWithdrawableEpoch(item.outEpoch); totalSlashedBalance += validator.EffectiveBalance; } Epoch currentEpoch = beaconStateAccessor.GetCurrentEpoch(state); Epoch slashingsIndex = (Epoch)(currentEpoch % stateListLengths.EpochsPerSlashingsVector); state.SetSlashings(slashingsIndex, totalSlashedBalance); }
public static BeaconBlock BuildEmptyBlock(IServiceProvider testServiceProvider, BeaconState state, Slot slot, BlsSignature randaoReveal) { //if (slot) is none TimeParameters timeParameters = testServiceProvider.GetService <IOptions <TimeParameters> >().Value; SignatureDomains signatureDomains = testServiceProvider.GetService <IOptions <SignatureDomains> >().Value; ICryptographyService cryptographyService = testServiceProvider.GetService <ICryptographyService>(); BeaconChainUtility beaconChainUtility = testServiceProvider.GetService <BeaconChainUtility>(); BeaconStateAccessor beaconStateAccessor = testServiceProvider.GetService <BeaconStateAccessor>(); BeaconStateTransition beaconStateTransition = testServiceProvider.GetService <BeaconStateTransition>(); Eth1Data eth1Data = new Eth1Data(Root.Zero, state.Eth1DepositIndex, Bytes32.Zero); Root stateRoot = !state.LatestBlockHeader.StateRoot.Equals(Root.Zero) ? state.LatestBlockHeader.StateRoot : cryptographyService.HashTreeRoot(state); BeaconBlockHeader previousBlockHeader = new BeaconBlockHeader(state.LatestBlockHeader.Slot, state.LatestBlockHeader.ParentRoot, stateRoot, state.LatestBlockHeader.BodyRoot); Root previousBlockHashTreeRoot = cryptographyService.HashTreeRoot(previousBlockHeader); if (randaoReveal.Equals(BlsSignature.Zero)) { Epoch blockEpoch = beaconChainUtility.ComputeEpochAtSlot(slot); ValidatorIndex proposerIndex; if (slot == state.Slot) { proposerIndex = beaconStateAccessor.GetBeaconProposerIndex(state); } else { Epoch stateEpoch = beaconChainUtility.ComputeEpochAtSlot(state.Slot); if (blockEpoch > stateEpoch + 1) { Console.WriteLine("WARNING: Block slot (epoch {0}) far away from state (epoch {1}), and no proposer index manually given." + " Signing block is slow due to transition for proposer index calculation.", blockEpoch, stateEpoch); } // use stub state to get proposer index of future slot BeaconState stubState = BeaconState.Clone(state); beaconStateTransition.ProcessSlots(stubState, slot); proposerIndex = beaconStateAccessor.GetBeaconProposerIndex(stubState); } byte[][] privateKeys = TestKeys.PrivateKeys(timeParameters).ToArray(); byte[] privateKey = privateKeys[(int)(ulong)proposerIndex]; Domain randaoDomain = beaconStateAccessor.GetDomain(state, signatureDomains.Randao, blockEpoch); Root epochHashTreeRoot = cryptographyService.HashTreeRoot(blockEpoch); Root randaoSigningRoot = beaconChainUtility.ComputeSigningRoot(epochHashTreeRoot, randaoDomain); randaoReveal = TestSecurity.BlsSign(randaoSigningRoot, privateKey); } BeaconBlock emptyBlock = new BeaconBlock(slot, previousBlockHashTreeRoot, Root.Zero, new BeaconBlockBody( randaoReveal, eth1Data, new Bytes32(), Array.Empty <ProposerSlashing>(), Array.Empty <AttesterSlashing>(), Array.Empty <Attestation>(), Array.Empty <Deposit>(), Array.Empty <SignedVoluntaryExit>() )); return(emptyBlock); }
public async Task GenesisWithEmptyParametersTimeShouldReject() { // Arrange IServiceProvider testServiceProvider = TestSystem.BuildTestServiceProvider(); ChainConstants chainConstants = testServiceProvider.GetService <ChainConstants>(); IOptionsMonitor <MiscellaneousParameters> miscellaneousParameterOptions = testServiceProvider.GetService <IOptionsMonitor <MiscellaneousParameters> >(); IOptionsMonitor <GweiValues> gweiValueOptions = testServiceProvider.GetService <IOptionsMonitor <GweiValues> >(); IOptionsMonitor <InitialValues> initialValueOptions = testServiceProvider.GetService <IOptionsMonitor <InitialValues> >(); IOptionsMonitor <TimeParameters> timeParameterOptions = testServiceProvider.GetService <IOptionsMonitor <TimeParameters> >(); IOptionsMonitor <StateListLengths> stateListLengthOptions = testServiceProvider.GetService <IOptionsMonitor <StateListLengths> >(); IOptionsMonitor <RewardsAndPenalties> rewardsAndPenaltiesOptions = testServiceProvider.GetService <IOptionsMonitor <RewardsAndPenalties> >(); IOptionsMonitor <MaxOperationsPerBlock> maxOperationsPerBlockOptions = testServiceProvider.GetService <IOptionsMonitor <MaxOperationsPerBlock> >(); IOptionsMonitor <ForkChoiceConfiguration> forkChoiceConfigurationOptions = testServiceProvider.GetService <IOptionsMonitor <ForkChoiceConfiguration> >(); IOptionsMonitor <SignatureDomains> signatureDomainOptions = testServiceProvider.GetService <IOptionsMonitor <SignatureDomains> >(); IOptionsMonitor <InMemoryConfiguration> inMemoryConfigurationOptions = testServiceProvider.GetService <IOptionsMonitor <InMemoryConfiguration> >(); miscellaneousParameterOptions.CurrentValue.MinimumGenesisActiveValidatorCount = 2; LoggerFactory loggerFactory = new LoggerFactory(new[] { new ConsoleLoggerProvider(Core2.Configuration.Static.OptionsMonitor <ConsoleLoggerOptions>()) }); ICryptographyService cryptographyService = testServiceProvider.GetService <ICryptographyService>(); IDepositStore depositStore = new DepositStore(cryptographyService, chainConstants); BeaconChainUtility beaconChainUtility = new BeaconChainUtility( loggerFactory.CreateLogger <BeaconChainUtility>(), chainConstants, miscellaneousParameterOptions, initialValueOptions, gweiValueOptions, timeParameterOptions, cryptographyService); BeaconStateAccessor beaconStateAccessor = new BeaconStateAccessor(chainConstants, miscellaneousParameterOptions, timeParameterOptions, stateListLengthOptions, signatureDomainOptions, cryptographyService, beaconChainUtility); BeaconStateMutator beaconStateMutator = new BeaconStateMutator(chainConstants, timeParameterOptions, stateListLengthOptions, rewardsAndPenaltiesOptions, beaconChainUtility, beaconStateAccessor); BeaconStateTransition beaconStateTransition = new BeaconStateTransition( loggerFactory.CreateLogger <BeaconStateTransition>(), chainConstants, gweiValueOptions, timeParameterOptions, stateListLengthOptions, rewardsAndPenaltiesOptions, maxOperationsPerBlockOptions, signatureDomainOptions, cryptographyService, beaconChainUtility, beaconStateAccessor, beaconStateMutator, depositStore); SimpleLatestMessageDrivenGreedyHeaviestObservedSubtree simpleLmdGhost = new SimpleLatestMessageDrivenGreedyHeaviestObservedSubtree( loggerFactory.CreateLogger <SimpleLatestMessageDrivenGreedyHeaviestObservedSubtree>(), chainConstants, beaconChainUtility, beaconStateAccessor); MemoryStore store = new MemoryStore(loggerFactory.CreateLogger <MemoryStore>(), inMemoryConfigurationOptions, new DataDirectory("data"), Substitute.For <IFileSystem>(), simpleLmdGhost, new StoreAccessor()); ForkChoice forkChoice = new ForkChoice(loggerFactory.CreateLogger <ForkChoice>(), chainConstants, miscellaneousParameterOptions, timeParameterOptions, maxOperationsPerBlockOptions, forkChoiceConfigurationOptions, signatureDomainOptions, cryptographyService, beaconChainUtility, beaconStateAccessor, beaconStateTransition); GenesisChainStart genesisChainStart = new GenesisChainStart(loggerFactory.CreateLogger <GenesisChainStart>(), chainConstants, miscellaneousParameterOptions, gweiValueOptions, initialValueOptions, timeParameterOptions, stateListLengthOptions, cryptographyService, store, beaconStateAccessor, beaconStateTransition, forkChoice, depositStore); // Act Bytes32 eth1BlockHash = Bytes32.Zero; ulong eth1Timestamp = 106185600uL; // 1973-05-14 bool success = await genesisChainStart.TryGenesisAsync(eth1BlockHash, eth1Timestamp); // Assert success.ShouldBeFalse(); }
// Run ``process_attester_slashing``, yielding: // - pre-state('pre') // - attester_slashing('attester_slashing') // - post-state('post'). //If ``valid == False``, run expecting ``AssertionError`` private void RunAttesterSlashingProcessing(IServiceProvider testServiceProvider, BeaconState state, AttesterSlashing attesterSlashing, bool expectValid) { ChainConstants chainConstants = testServiceProvider.GetService <ChainConstants>(); StateListLengths stateListLengths = testServiceProvider.GetService <IOptions <StateListLengths> >().Value; RewardsAndPenalties rewardsAndPenalties = testServiceProvider.GetService <IOptions <RewardsAndPenalties> >().Value; BeaconStateAccessor beaconStateAccessor = testServiceProvider.GetService <BeaconStateAccessor>(); BeaconStateTransition beaconStateTransition = testServiceProvider.GetService <BeaconStateTransition>(); if (!expectValid) { Should.Throw <Exception>(() => { beaconStateTransition.ProcessAttesterSlashing(state, attesterSlashing); }); return; } var slashedIndices = attesterSlashing.Attestation1.CustodyBit0Indices .Union(attesterSlashing.Attestation1.CustodyBit1Indices) .ToList(); ValidatorIndex proposerIndex = beaconStateAccessor.GetBeaconProposerIndex(state); Gwei preProposerBalance = TestState.GetBalance(state, proposerIndex); var preSlashings = slashedIndices.ToDictionary(x => x, x => TestState.GetBalance(state, x)); var preWithdrawableEpochs = slashedIndices.ToDictionary(x => x, x => state.Validators[(int)(ulong)x].WithdrawableEpoch); Gwei totalProposerRewards = preSlashings.Values.Aggregate(Gwei.Zero, (sum, x) => sum + x / rewardsAndPenalties.WhistleblowerRewardQuotient); // Process slashing beaconStateTransition.ProcessAttesterSlashing(state, attesterSlashing); foreach (ValidatorIndex slashedIndex in slashedIndices) { Console.WriteLine($"Checking index {slashedIndex}"); Epoch preWithdrawableEpoch = preWithdrawableEpochs[slashedIndex]; Validator slashedValidator = state.Validators[(int)(ulong)slashedIndex]; // Check Slashing slashedValidator.IsSlashed.ShouldBeTrue(); slashedValidator.ExitEpoch.ShouldBeLessThan(chainConstants.FarFutureEpoch); if (preWithdrawableEpoch < chainConstants.FarFutureEpoch) { ulong slashingsEpoch = beaconStateAccessor.GetCurrentEpoch(state) + stateListLengths.EpochsPerSlashingsVector; Epoch expectedWithdrawableEpoch = Epoch.Max(preWithdrawableEpoch, (Epoch)slashingsEpoch); slashedValidator.WithdrawableEpoch.ShouldBe(expectedWithdrawableEpoch); } else { slashedValidator.WithdrawableEpoch.ShouldBeLessThan(chainConstants.FarFutureEpoch); } Gwei preSlashing = preSlashings[slashedIndex]; Gwei slashedBalance = TestState.GetBalance(state, slashedIndex); slashedBalance.ShouldBeLessThan(preSlashing); } if (!slashedIndices.Contains(proposerIndex)) { // gained whistleblower reward Gwei expectedProposerBalance = preProposerBalance + totalProposerRewards; Gwei proposerBalance = TestState.GetBalance(state, proposerIndex); proposerBalance.ShouldBe(expectedProposerBalance); } else { // gained rewards for all slashings, which may include others. And only lost that of themselves. Gwei expectedProposerBalance = preProposerBalance + totalProposerRewards - (preSlashings[proposerIndex] / rewardsAndPenalties.MinimumSlashingPenaltyQuotient); Gwei proposerBalance = TestState.GetBalance(state, proposerIndex); proposerBalance.ShouldBe(expectedProposerBalance); } }