// 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 async Task OnAttestationPreviousEpoch() { // Arrange IServiceProvider testServiceProvider = TestSystem.BuildTestServiceProvider(useStore: true); BeaconState state = TestState.PrepareTestState(testServiceProvider); InitialValues initialValues = testServiceProvider.GetService <IOptions <InitialValues> >().Value; TimeParameters timeParameters = testServiceProvider.GetService <IOptions <TimeParameters> >().Value; ForkChoice forkChoice = testServiceProvider.GetService <ForkChoice>(); // Initialization IStore store = forkChoice.GetGenesisStore(state); ulong time = store.Time + timeParameters.SecondsPerSlot * (ulong)timeParameters.SlotsPerEpoch; await forkChoice.OnTickAsync(store, time); BeaconBlock block = TestBlock.BuildEmptyBlockForNextSlot(testServiceProvider, state, signed: true); TestState.StateTransitionAndSignBlock(testServiceProvider, state, block); // Store block in store await forkChoice.OnBlockAsync(store, block); Attestation attestation = TestAttestation.GetValidAttestation(testServiceProvider, state, block.Slot, CommitteeIndex.None, signed: true); attestation.Data.Target.Epoch.ShouldBe(initialValues.GenesisEpoch); BeaconChainUtility beaconChainUtility = testServiceProvider.GetService <BeaconChainUtility>(); Slot currentSlot = forkChoice.GetCurrentSlot(store); Epoch currentEpoch = beaconChainUtility.ComputeEpochAtSlot(currentSlot); currentEpoch.ShouldBe(initialValues.GenesisEpoch + Epoch.One); await RunOnAttestation(testServiceProvider, state, store, attestation, expectValid : true); }
public void GenesisEpochFullAttestationsNoRewards() { // Arrange IServiceProvider testServiceProvider = TestSystem.BuildTestServiceProvider(); BeaconState state = TestState.PrepareTestState(testServiceProvider); InitialValues initialValues = testServiceProvider.GetService <IOptions <InitialValues> >().Value; TimeParameters timeParameters = testServiceProvider.GetService <IOptions <TimeParameters> >().Value; BeaconChainUtility beaconChainUtility = testServiceProvider.GetService <BeaconChainUtility>(); var attestations = new List <Attestation>(); for (Slot slot = Slot.Zero; slot < timeParameters.SlotsPerEpoch - new Slot(1); slot += new Slot(1)) { // create an attestation for each slot if (slot < timeParameters.SlotsPerEpoch) { Attestation attestation = TestAttestation.GetValidAttestation(testServiceProvider, state, slot, CommitteeIndex.None, signed: true); attestations.Add(attestation); } // fill each created slot in state after inclusion delay if (slot >= timeParameters.MinimumAttestationInclusionDelay) { Slot index = slot - timeParameters.MinimumAttestationInclusionDelay; Attestation includeAttestation = attestations[(int)(ulong)index]; TestAttestation.AddAttestationsToState(testServiceProvider, state, new[] { includeAttestation }, state.Slot); } TestState.NextSlot(testServiceProvider, state); } // ensure has not cross the epoch boundary Epoch stateEpoch = beaconChainUtility.ComputeEpochAtSlot(state.Slot); stateEpoch.ShouldBe(initialValues.GenesisEpoch); BeaconState preState = BeaconState.Clone(state); // Act RunProcessRewardsAndPenalties(testServiceProvider, state); // Assert for (int index = 0; index < preState.Validators.Count; index++) { state.Balances[index].ShouldBe(preState.Balances[index], $"Balance {index}"); } }
private static IList <Attestation> PrepareStateWithFullAttestations(IServiceProvider testServiceProvider, BeaconState state) { TimeParameters timeParameters = testServiceProvider.GetService <IOptions <TimeParameters> >().Value; InitialValues initialValues = testServiceProvider.GetService <IOptions <InitialValues> >().Value; BeaconChainUtility beaconChainUtility = testServiceProvider.GetService <BeaconChainUtility>(); var attestations = new List <Attestation>(); ulong maxSlot = timeParameters.SlotsPerEpoch + timeParameters.MinimumAttestationInclusionDelay; for (Slot slot = Slot.Zero; slot < maxSlot; slot += new Slot(1)) { // create an attestation for each slot in epoch if (slot < timeParameters.SlotsPerEpoch) { Attestation attestation = TestAttestation.GetValidAttestation(testServiceProvider, state, Slot.None, CommitteeIndex.None, signed: true); attestations.Add(attestation); } // fill each created slot in state after inclusion delay if (slot >= timeParameters.MinimumAttestationInclusionDelay) { Slot index = slot - timeParameters.MinimumAttestationInclusionDelay; Attestation includeAttestation = attestations[(int)(ulong)index]; TestAttestation.AddAttestationsToState(testServiceProvider, state, new[] { includeAttestation }, state.Slot); } TestState.NextSlot(testServiceProvider, state); } Epoch stateEpoch = beaconChainUtility.ComputeEpochAtSlot(state.Slot); stateEpoch.ShouldBe(initialValues.GenesisEpoch + Epoch.One); state.PreviousEpochAttestations.Count.ShouldBe((int)timeParameters.SlotsPerEpoch); return(attestations); }
public async Task OnAttestationPastEpoch() { // Arrange IServiceProvider testServiceProvider = TestSystem.BuildTestServiceProvider(useStore: true); BeaconState state = TestState.PrepareTestState(testServiceProvider); ChainConstants chainConstants = testServiceProvider.GetService <ChainConstants>(); InitialValues initialValues = testServiceProvider.GetService <IOptions <InitialValues> >().Value; TimeParameters timeParameters = testServiceProvider.GetService <IOptions <TimeParameters> >().Value; IForkChoice forkChoice = testServiceProvider.GetService <IForkChoice>(); // Initialization IStore store = testServiceProvider.GetService <IStore>(); await forkChoice.InitializeForkChoiceStoreAsync(store, state); // move time forward 2 epochs ulong time = store.Time + 2 * timeParameters.SecondsPerSlot * (ulong)timeParameters.SlotsPerEpoch; await forkChoice.OnTickAsync(store, time); // create and store block from 3 epochs ago BeaconBlock block = TestBlock.BuildEmptyBlockForNextSlot(testServiceProvider, state, BlsSignature.Zero); SignedBeaconBlock signedBlock = TestState.StateTransitionAndSignBlock(testServiceProvider, state, block); await forkChoice.OnBlockAsync(store, signedBlock); // create attestation for past block Attestation attestation = TestAttestation.GetValidAttestation(testServiceProvider, state, state.Slot, CommitteeIndex.None, signed: true); attestation.Data.Target.Epoch.ShouldBe(chainConstants.GenesisEpoch); IBeaconChainUtility beaconChainUtility = testServiceProvider.GetService <IBeaconChainUtility>(); Slot currentSlot = ((ForkChoice)forkChoice).GetCurrentSlot(store); Epoch currentEpoch = beaconChainUtility.ComputeEpochAtSlot(currentSlot); currentEpoch.ShouldBe((Epoch)(chainConstants.GenesisEpoch + 2UL)); await RunOnAttestation(testServiceProvider, state, store, attestation, expectValid : false); }
public void GenesisEpochNoAttestationsNoPenalties() { // Arrange IServiceProvider testServiceProvider = TestSystem.BuildTestServiceProvider(); BeaconState state = TestState.PrepareTestState(testServiceProvider); InitialValues initialValues = testServiceProvider.GetService <IOptions <InitialValues> >().Value; BeaconChainUtility beaconChainUtility = testServiceProvider.GetService <BeaconChainUtility>(); BeaconState preState = BeaconState.Clone(state); Epoch stateEpoch = beaconChainUtility.ComputeEpochAtSlot(state.Slot); stateEpoch.ShouldBe(initialValues.GenesisEpoch); // Act RunProcessRewardsAndPenalties(testServiceProvider, state); // Assert for (int index = 0; index < preState.Validators.Count; index++) { state.Balances[index].ShouldBe(preState.Balances[index], $"Balance {index}"); } }