public static void SignVoluntaryExit(IServiceProvider testServiceProvider, BeaconState state, VoluntaryExit voluntaryExit, byte[] privateKey) { var signatureDomains = testServiceProvider.GetService <IOptions <SignatureDomains> >().Value; var beaconStateAccessor = testServiceProvider.GetService <BeaconStateAccessor>(); var domain = beaconStateAccessor.GetDomain(state, signatureDomains.VoluntaryExit, voluntaryExit.Epoch); var signature = TestSecurity.BlsSign(voluntaryExit.SigningRoot(), privateKey, domain); voluntaryExit.Signature = signature; }
public static void SignBlock(IServiceProvider testServiceProvider, BeaconState state, BeaconBlock block, ValidatorIndex proposerIndex) { var miscellaneousParameters = testServiceProvider.GetService <IOptions <MiscellaneousParameters> >().Value; var timeParameters = testServiceProvider.GetService <IOptions <TimeParameters> >().Value; var maxOperationsPerBlock = testServiceProvider.GetService <IOptions <MaxOperationsPerBlock> >().Value; var signatureDomains = testServiceProvider.GetService <IOptions <SignatureDomains> >().Value; var beaconChainUtility = testServiceProvider.GetService <BeaconChainUtility>(); var beaconStateAccessor = testServiceProvider.GetService <BeaconStateAccessor>(); var 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}"); } var blockEpoch = beaconChainUtility.ComputeEpochAtSlot(block.Slot); if (proposerIndex == ValidatorIndex.None) { if (block.Slot == state.Slot) { proposerIndex = beaconStateAccessor.GetBeaconProposerIndex(state); } else { var 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 var stubState = BeaconState.Clone(state); beaconStateTransition.ProcessSlots(stubState, block.Slot); proposerIndex = beaconStateAccessor.GetBeaconProposerIndex(stubState); } } var privateKeys = TestKeys.PrivateKeys(timeParameters).ToArray(); var privateKey = privateKeys[(int)(ulong)proposerIndex]; var randaoDomain = beaconStateAccessor.GetDomain(state, signatureDomains.Randao, blockEpoch); var randaoRevealHash = blockEpoch.HashTreeRoot(); var randaoReveal = TestSecurity.BlsSign(randaoRevealHash, privateKey, randaoDomain); block.Body.SetRandaoReveal(randaoReveal); var signatureDomain = beaconStateAccessor.GetDomain(state, signatureDomains.BeaconProposer, blockEpoch); var signingRoot = block.SigningRoot(miscellaneousParameters, maxOperationsPerBlock); var signature = TestSecurity.BlsSign(signingRoot, privateKey, signatureDomain); block.SetSignature(signature); }
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]; var privateKey = TestKeys.PublicKeyToPrivateKey(validator.PublicKey, timeParameters); VoluntaryExit voluntaryExit = TestVoluntaryExit.BuildVoluntaryExit(testServiceProvider, state, currentEpoch, validatorIndex, privateKey, signed: false); RunVoluntaryExitProcessing(testServiceProvider, state, voluntaryExit, expectValid: false); }
// 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 static VoluntaryExit BuildVoluntaryExit(IServiceProvider testServiceProvider, BeaconState state, Epoch epoch, ValidatorIndex validatorIndex, byte[] privateKey, bool signed) { var voluntaryExit = new VoluntaryExit(epoch, validatorIndex, BlsSignature.Empty); if (signed) { SignVoluntaryExit(testServiceProvider, state, voluntaryExit, privateKey); } return(voluntaryExit); }
public async Task <Fork> GetNodeForkAsync() { BeaconState state = await GetHeadStateAsync(); return(state.Fork); }
public async Task <ulong> GetGenesisTimeAsync() { BeaconState state = await GetHeadStateAsync(); return(state.GenesisTime); }
public static BeaconBlock BuildEmptyBlockForNextSlot(IServiceProvider testServiceProvider, BeaconState state, bool signed) { return(BuildEmptyBlock(testServiceProvider, state, state.Slot + new Slot(1), signed)); }
public static BeaconBlock BuildEmptyBlock(IServiceProvider testServiceProvider, BeaconState state, Slot slot, bool signed) { //if (slot) is none var miscellaneousParameters = testServiceProvider.GetService <IOptions <MiscellaneousParameters> >().Value; var timeParameters = testServiceProvider.GetService <IOptions <TimeParameters> >().Value; var stateListLengths = testServiceProvider.GetService <IOptions <StateListLengths> >().Value; var maxOperationsPerBlock = testServiceProvider.GetService <IOptions <MaxOperationsPerBlock> >().Value; var eth1Data = new Eth1Data(state.Eth1DepositIndex, Hash32.Zero); var previousBlockHeader = BeaconBlockHeader.Clone(state.LatestBlockHeader); if (previousBlockHeader.StateRoot == Hash32.Zero) { var stateRoot = state.HashTreeRoot(miscellaneousParameters, timeParameters, stateListLengths, maxOperationsPerBlock); previousBlockHeader.SetStateRoot(stateRoot); } var previousBlockSigningRoot = previousBlockHeader.SigningRoot(); var emptyBlock = new BeaconBlock(slot, previousBlockSigningRoot, Hash32.Zero, new BeaconBlockBody( BlsSignature.Empty, eth1Data, new Bytes32(), Array.Empty <ProposerSlashing>(), Array.Empty <AttesterSlashing>(), Array.Empty <Attestation>(), Array.Empty <Deposit>(), Array.Empty <VoluntaryExit>() ), BlsSignature.Empty); if (signed) { SignBlock(testServiceProvider, state, emptyBlock, ValidatorIndex.None); } return(emptyBlock); }