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 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 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 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)); }
private BlsSignature GetEpochSignature(IServiceProvider testServiceProvider, byte[] privateKey, ForkVersion forkVersion, Slot slot) { SignatureDomains signatureDomains = testServiceProvider.GetService <IOptions <SignatureDomains> >().Value; BeaconChainUtility beaconChainUtility = testServiceProvider.GetService <BeaconChainUtility>(); var domain = beaconChainUtility.ComputeDomain(signatureDomains.Randao, forkVersion); var epoch = beaconChainUtility.ComputeEpochAtSlot(slot); var epochRoot = epoch.HashTreeRoot(); BLSParameters parameters = new BLSParameters() { PrivateKey = privateKey }; BLS bls = BLS.Create(parameters); var destination = new Span <byte>(new byte[96]); bls.TrySignHash(epochRoot.AsSpan(), destination, out var bytesWritten, domain.AsSpan()); var signature = new BlsSignature(destination.ToArray()); return(signature); }
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 void QuickStartGenesis() { QuickStartParameters quickStartParameters = _quickStartParameterOptions.CurrentValue; _logger.LogWarning(0, "Mocked quick start with genesis time {GenesisTime:n0} and {ValidatorCount} validators.", quickStartParameters.GenesisTime, quickStartParameters.ValidatorCount); GweiValues gweiValues = _gweiValueOptions.CurrentValue; InitialValues initialValues = _initialValueOptions.CurrentValue; SignatureDomains signatureDomains = _signatureDomainOptions.CurrentValue; // Fixed amount Gwei amount = gweiValues.MaximumEffectiveBalance; // Build deposits var depositDataList = new List <DepositData>(); var deposits = new List <Deposit>(); for (ulong validatorIndex = 0uL; validatorIndex < quickStartParameters.ValidatorCount; validatorIndex++) { var privateKey = GeneratePrivateKey(validatorIndex); // Public Key BLSParameters blsParameters = new BLSParameters() { PrivateKey = privateKey }; using BLS bls = BLS.Create(blsParameters); var publicKeyBytes = new byte[BlsPublicKey.Length]; bls.TryExportBLSPublicKey(publicKeyBytes, out int publicKeyBytesWritten); BlsPublicKey publicKey = new BlsPublicKey(publicKeyBytes); // Withdrawal Credentials var withdrawalCredentialBytes = _cryptographyService.Hash(publicKey.AsSpan()).AsSpan().ToArray(); withdrawalCredentialBytes[0] = initialValues.BlsWithdrawalPrefix; Hash32 withdrawalCredentials = new Hash32(withdrawalCredentialBytes); // Build deposit data DepositData depositData = new DepositData(publicKey, withdrawalCredentials, amount); // Sign deposit data Hash32 depositDataSigningRoot = depositData.SigningRoot(); Domain domain = _beaconChainUtility.ComputeDomain(signatureDomains.Deposit); var destination = new byte[96]; bls.TrySignHash(depositDataSigningRoot.AsSpan(), destination, out int bytesWritten, domain.AsSpan()); BlsSignature depositDataSignature = new BlsSignature(destination); depositData.SetSignature(depositDataSignature); // Deposit // TODO: This seems a very inefficient way (copied from tests) as it recalculates the merkle tree each time // (you only need to add one node) // TODO: Add some tests around quick start, then improve int index = depositDataList.Count; depositDataList.Add(depositData); Hash32 root = depositDataList.HashTreeRoot((ulong)1 << _chainConstants.DepositContractTreeDepth); var allLeaves = depositDataList.Select(x => x.HashTreeRoot()); var tree = CalculateMerkleTreeFromLeaves(allLeaves); var merkleProof = GetMerkleProof(tree, index, 32); var proof = new List <Hash32>(merkleProof); var indexBytes = new Span <byte>(new byte[32]); BitConverter.TryWriteBytes(indexBytes, (ulong)index + 1); if (!BitConverter.IsLittleEndian) { indexBytes.Slice(0, 8).Reverse(); } Hash32 indexHash = new Hash32(indexBytes); proof.Add(indexHash); Hash32 leaf = depositData.HashTreeRoot(); _beaconChainUtility.IsValidMerkleBranch(leaf, proof, _chainConstants.DepositContractTreeDepth + 1, (ulong)index, root); Deposit deposit = new Deposit(proof, depositData); if (_logger.IsEnabled(LogLevel.Debug)) { _logger.LogDebug("Quick start adding deposit for mocked validator {ValidatorIndex} with public key {PublicKey}.", validatorIndex, publicKey.ToString().Substring(0, 12)); } deposits.Add(deposit); } BeaconState genesisState = _beaconChain.InitializeBeaconStateFromEth1(quickStartParameters.Eth1BlockHash, quickStartParameters.Eth1Timestamp, deposits); // We use the state directly, and don't test IsValid genesisState.SetGenesisTime(quickStartParameters.GenesisTime); IStore store = _forkChoice.GetGenesisStore(genesisState); _logger.LogDebug("Quick start genesis store created with genesis time {GenesisTime:n0}.", store.GenesisTime); }
private DepositData BuildAndSignDepositData(ulong validatorIndex, Gwei amount, SignatureDomains signatureDomains) { InitialValues initialValues = _initialValueOptions.CurrentValue; byte[] privateKey = GeneratePrivateKey(validatorIndex); // Public Key BLSParameters blsParameters = new BLSParameters { PrivateKey = privateKey }; using BLS bls = BLS.Create(blsParameters); byte[] publicKeyBytes = new byte[BlsPublicKey.Length]; bls.TryExportBlsPublicKey(publicKeyBytes, out int publicKeyBytesWritten); BlsPublicKey publicKey = new BlsPublicKey(publicKeyBytes); // Withdrawal Credentials Bytes32 withdrawalCredentials = _crypto.Hash(publicKey.AsSpan()); withdrawalCredentials.Unwrap()[0] = initialValues.BlsWithdrawalPrefix; // Build deposit data DepositData depositData = new DepositData(publicKey, withdrawalCredentials, amount, BlsSignature.Zero); // Sign deposit data Domain domain = _beaconChainUtility.ComputeDomain(signatureDomains.Deposit); DepositMessage depositMessage = new DepositMessage( depositData.PublicKey, depositData.WithdrawalCredentials, depositData.Amount); Root depositMessageRoot = _crypto.HashTreeRoot(depositMessage); Root depositDataSigningRoot = _beaconChainUtility.ComputeSigningRoot(depositMessageRoot, domain); byte[] signatureBytes = new byte[96]; bls.TrySignData(depositDataSigningRoot.AsSpan(), signatureBytes, out int bytesWritten); BlsSignature depositDataSignature = new BlsSignature(signatureBytes); depositData.SetSignature(depositDataSignature); if (_logger.IsEnabled(LogLevel.Debug)) { LogDebug.QuickStartAddValidator(_logger, validatorIndex, publicKey.ToString().Substring(0, 12), null); } return(depositData); }
public Task <Eth1GenesisData> GetEth1GenesisDataAsync(CancellationToken cancellationToken) { QuickStartParameters quickStartParameters = _quickStartParameterOptions.CurrentValue; if (_logger.IsWarn()) { Log.MockedQuickStart(_logger, quickStartParameters.GenesisTime, quickStartParameters.ValidatorCount, null); } GweiValues gweiValues = _gweiValueOptions.CurrentValue; TimeParameters timeParameters = _timeParameterOptions.CurrentValue; SignatureDomains signatureDomains = _signatureDomainOptions.CurrentValue; // Fixed amount Gwei amount = gweiValues.MaximumEffectiveBalance; for (ulong validatorIndex = 0uL; validatorIndex < quickStartParameters.ValidatorCount; validatorIndex++) { DepositData depositData = BuildAndSignDepositData( validatorIndex, amount, signatureDomains); _depositStore.Place(depositData); } ulong eth1Timestamp = quickStartParameters.Eth1Timestamp; if (eth1Timestamp == 0) { eth1Timestamp = quickStartParameters.GenesisTime - (ulong)(1.5 * timeParameters.MinimumGenesisDelay); } else { ulong minimumEth1TimestampInclusive = quickStartParameters.GenesisTime - 2 * timeParameters.MinimumGenesisDelay; ulong maximumEth1TimestampInclusive = quickStartParameters.GenesisTime - timeParameters.MinimumGenesisDelay - 1; if (eth1Timestamp < minimumEth1TimestampInclusive) { if (_logger.IsEnabled(LogLevel.Warning)) { Log.QuickStartEth1TimestampTooLow(_logger, eth1Timestamp, quickStartParameters.GenesisTime, minimumEth1TimestampInclusive, null); } eth1Timestamp = minimumEth1TimestampInclusive; } else if (eth1Timestamp > maximumEth1TimestampInclusive) { if (_logger.IsEnabled(LogLevel.Warning)) { Log.QuickStartEth1TimestampTooHigh(_logger, eth1Timestamp, quickStartParameters.GenesisTime, maximumEth1TimestampInclusive, null); } eth1Timestamp = maximumEth1TimestampInclusive; } } var eth1GenesisData = new Eth1GenesisData(quickStartParameters.Eth1BlockHash, eth1Timestamp); if (_logger.IsEnabled(LogLevel.Debug)) { LogDebug.QuickStartGenesisDataCreated(_logger, eth1GenesisData.BlockHash, eth1GenesisData.Timestamp, (uint)_depositStore.Deposits.Count, null); } return(Task.FromResult(eth1GenesisData)); }
private static void ValidateConfigShouldHaveValues(MiscellaneousParameters miscellaneousParameters, ForkChoiceConfiguration forkChoiceConfiguration, HonestValidatorConstants honestValidatorConstants, GweiValues gweiValues, InitialValues initialValues, TimeParameters timeParameters, StateListLengths stateListLengths, RewardsAndPenalties rewardsAndPenalties, MaxOperationsPerBlock maxOperationsPerBlock, SignatureDomains signatureDomains) { miscellaneousParameters.ChurnLimitQuotient.ShouldNotBe(0uL); miscellaneousParameters.MaximumCommitteesPerSlot.ShouldNotBe(0uL); miscellaneousParameters.MaximumValidatorsPerCommittee.ShouldNotBe(0uL); miscellaneousParameters.MinimumGenesisActiveValidatorCount.ShouldNotBe(0); miscellaneousParameters.MinimumGenesisTime.ShouldNotBe(0uL); miscellaneousParameters.MinimumPerEpochChurnLimit.ShouldNotBe(0uL); miscellaneousParameters.ShuffleRoundCount.ShouldNotBe(0); miscellaneousParameters.TargetCommitteeSize.ShouldNotBe(0uL); forkChoiceConfiguration.SafeSlotsToUpdateJustified.ShouldNotBe(Slot.Zero); honestValidatorConstants.EpochsPerRandomSubnetSubscription.ShouldNotBe(Epoch.Zero); honestValidatorConstants.Eth1FollowDistance.ShouldNotBe(0uL); honestValidatorConstants.RandomSubnetsPerValidator.ShouldNotBe(0uL); honestValidatorConstants.SecondsPerEth1Block.ShouldNotBe(0uL); honestValidatorConstants.TargetAggregatorsPerCommittee.ShouldNotBe(0uL); gweiValues.EffectiveBalanceIncrement.ShouldNotBe(Gwei.Zero); gweiValues.EjectionBalance.ShouldNotBe(Gwei.Zero); gweiValues.MaximumEffectiveBalance.ShouldNotBe(Gwei.Zero); // actually should be zero initialValues.BlsWithdrawalPrefix.ShouldBe((byte)0); initialValues.GenesisForkVersion.ShouldBe(new ForkVersion(new byte[] { 0x00, 0x00, 0x00, 0x01 })); timeParameters.MaximumSeedLookahead.ShouldNotBe(Epoch.Zero); timeParameters.MinimumAttestationInclusionDelay.ShouldNotBe(Slot.Zero); timeParameters.MinimumGenesisDelay.ShouldNotBe(0u); timeParameters.MinimumEpochsToInactivityPenalty.ShouldNotBe(Epoch.Zero); timeParameters.MinimumSeedLookahead.ShouldNotBe(Epoch.Zero); timeParameters.MinimumValidatorWithdrawabilityDelay.ShouldNotBe(Epoch.Zero); timeParameters.PersistentCommitteePeriod.ShouldNotBe(Epoch.Zero); timeParameters.SecondsPerSlot.ShouldNotBe(0U); timeParameters.SlotsPerEpoch.ShouldNotBe(0U); timeParameters.SlotsPerEth1VotingPeriod.ShouldNotBe(0U); timeParameters.SlotsPerHistoricalRoot.ShouldNotBe(0U); stateListLengths.EpochsPerHistoricalVector.ShouldNotBe(0U); stateListLengths.EpochsPerSlashingsVector.ShouldNotBe(0U); stateListLengths.HistoricalRootsLimit.ShouldNotBe(0uL); stateListLengths.ValidatorRegistryLimit.ShouldNotBe(0uL); rewardsAndPenalties.BaseRewardFactor.ShouldNotBe(0uL); rewardsAndPenalties.InactivityPenaltyQuotient.ShouldNotBe(0uL); rewardsAndPenalties.MinimumSlashingPenaltyQuotient.ShouldNotBe(0uL); rewardsAndPenalties.ProposerRewardQuotient.ShouldNotBe(0uL); rewardsAndPenalties.WhistleblowerRewardQuotient.ShouldNotBe(0uL); maxOperationsPerBlock.MaximumAttestations.ShouldNotBe(0uL); maxOperationsPerBlock.MaximumAttesterSlashings.ShouldNotBe(0uL); maxOperationsPerBlock.MaximumDeposits.ShouldNotBe(0uL); maxOperationsPerBlock.MaximumProposerSlashings.ShouldNotBe(0uL); maxOperationsPerBlock.MaximumVoluntaryExits.ShouldNotBe(0uL); // actually should be zero signatureDomains.BeaconProposer.ShouldBe(default);
public Task <Eth1GenesisData> GetEth1GenesisDataAsync(CancellationToken cancellationToken) { QuickStartParameters quickStartParameters = _quickStartParameterOptions.CurrentValue; if (_logger.IsWarn()) { Log.MockedQuickStart(_logger, quickStartParameters.GenesisTime, quickStartParameters.ValidatorCount, null); } GweiValues gweiValues = _gweiValueOptions.CurrentValue; InitialValues initialValues = _initialValueOptions.CurrentValue; TimeParameters timeParameters = _timeParameterOptions.CurrentValue; SignatureDomains signatureDomains = _signatureDomainOptions.CurrentValue; // Fixed amount Gwei amount = gweiValues.MaximumEffectiveBalance; // Build deposits List <DepositData> depositDataList = new List <DepositData>(); List <Deposit> deposits = new List <Deposit>(); for (ulong validatorIndex = 0uL; validatorIndex < quickStartParameters.ValidatorCount; validatorIndex++) { byte[] privateKey = GeneratePrivateKey(validatorIndex); // Public Key BLSParameters blsParameters = new BLSParameters() { PrivateKey = privateKey }; using BLS bls = BLS.Create(blsParameters); byte[] publicKeyBytes = new byte[BlsPublicKey.Length]; bls.TryExportBlsPublicKey(publicKeyBytes, out int publicKeyBytesWritten); BlsPublicKey publicKey = new BlsPublicKey(publicKeyBytes); // Withdrawal Credentials byte[] withdrawalCredentialBytes = _cryptographyService.Hash(publicKey.AsSpan()).AsSpan().ToArray(); withdrawalCredentialBytes[0] = initialValues.BlsWithdrawalPrefix; Bytes32 withdrawalCredentials = new Bytes32(withdrawalCredentialBytes); // Build deposit data DepositData depositData = new DepositData(publicKey, withdrawalCredentials, amount, BlsSignature.Zero); // Sign deposit data Domain domain = _beaconChainUtility.ComputeDomain(signatureDomains.Deposit); DepositMessage depositMessage = new DepositMessage(depositData.PublicKey, depositData.WithdrawalCredentials, depositData.Amount); Root depositMessageRoot = _cryptographyService.HashTreeRoot(depositMessage); Root depositDataSigningRoot = _beaconChainUtility.ComputeSigningRoot(depositMessageRoot, domain); byte[] destination = new byte[96]; bls.TrySignData(depositDataSigningRoot.AsSpan(), destination, out int bytesWritten); BlsSignature depositDataSignature = new BlsSignature(destination); depositData.SetSignature(depositDataSignature); // Deposit // TODO: This seems a very inefficient way (copied from tests) as it recalculates the merkle tree each time // (you only need to add one node) // TODO: Add some tests around quick start, then improve int index = depositDataList.Count; depositDataList.Add(depositData); //int depositDataLength = (ulong) 1 << _chainConstants.DepositContractTreeDepth; Root root = _cryptographyService.HashTreeRoot(depositDataList); IEnumerable <Bytes32> allLeaves = depositDataList.Select(x => new Bytes32(_cryptographyService.HashTreeRoot((DepositData)x).AsSpan())); IList <IList <Bytes32> > tree = CalculateMerkleTreeFromLeaves(allLeaves); IList <Bytes32> merkleProof = GetMerkleProof(tree, index, 32); List <Bytes32> proof = new List <Bytes32>(merkleProof); byte[] indexBytes = new byte[32]; BinaryPrimitives.WriteInt32LittleEndian(indexBytes, index + 1); Bytes32 indexHash = new Bytes32(indexBytes); proof.Add(indexHash); Bytes32 leaf = new Bytes32(_cryptographyService.HashTreeRoot(depositData).AsSpan()); _beaconChainUtility.IsValidMerkleBranch(leaf, proof, _chainConstants.DepositContractTreeDepth + 1, (ulong)index, root); Deposit deposit = new Deposit(proof, depositData); if (_logger.IsEnabled(LogLevel.Debug)) { LogDebug.QuickStartAddValidator(_logger, validatorIndex, publicKey.ToString().Substring(0, 12), null); } deposits.Add(deposit); } ulong eth1Timestamp = quickStartParameters.Eth1Timestamp; if (eth1Timestamp == 0) { eth1Timestamp = quickStartParameters.GenesisTime - (ulong)(1.5 * timeParameters.MinimumGenesisDelay); } else { ulong minimumEth1TimestampInclusive = quickStartParameters.GenesisTime - 2 * timeParameters.MinimumGenesisDelay; ulong maximumEth1TimestampInclusive = quickStartParameters.GenesisTime - timeParameters.MinimumGenesisDelay - 1; if (eth1Timestamp < minimumEth1TimestampInclusive) { if (_logger.IsEnabled(LogLevel.Warning)) { Log.QuickStartEth1TimestampTooLow(_logger, eth1Timestamp, quickStartParameters.GenesisTime, minimumEth1TimestampInclusive, null); } eth1Timestamp = minimumEth1TimestampInclusive; } else if (eth1Timestamp > maximumEth1TimestampInclusive) { if (_logger.IsEnabled(LogLevel.Warning)) { Log.QuickStartEth1TimestampTooHigh(_logger, eth1Timestamp, quickStartParameters.GenesisTime, maximumEth1TimestampInclusive, null); } eth1Timestamp = maximumEth1TimestampInclusive; } } var eth1GenesisData = new Eth1GenesisData(quickStartParameters.Eth1BlockHash, eth1Timestamp, deposits); if (_logger.IsEnabled(LogLevel.Debug)) { LogDebug.QuickStartGenesisDataCreated(_logger, eth1GenesisData.BlockHash, eth1GenesisData.Timestamp, eth1GenesisData.Deposits.Count, null); } return(Task.FromResult(eth1GenesisData)); }