Пример #1
0
        public async Task GenesisHead()
        {
            // Arrange
            IServiceProvider testServiceProvider = TestSystem.BuildTestServiceProvider(useStore: true);
            BeaconState      state = TestState.PrepareTestState(testServiceProvider);

            MiscellaneousParameters miscellaneousParameters = testServiceProvider.GetService <IOptions <MiscellaneousParameters> >().Value;
            TimeParameters          timeParameters          = testServiceProvider.GetService <IOptions <TimeParameters> >().Value;
            StateListLengths        stateListLengths        = testServiceProvider.GetService <IOptions <StateListLengths> >().Value;
            MaxOperationsPerBlock   maxOperationsPerBlock   = testServiceProvider.GetService <IOptions <MaxOperationsPerBlock> >().Value;

            JsonSerializerOptions options = new System.Text.Json.JsonSerializerOptions {
                WriteIndented = true
            };

            options.AddCortexContainerConverters();
            string debugState = System.Text.Json.JsonSerializer.Serialize(state, options);

            // Initialization
            ForkChoice forkChoice = testServiceProvider.GetService <ForkChoice>();
            IStore     store      = forkChoice.GetGenesisStore(state);

            // Act
            Hash32 headRoot = await forkChoice.GetHeadAsync(store);

            // Assert
            Hash32      stateRoot    = state.HashTreeRoot(miscellaneousParameters, timeParameters, stateListLengths, maxOperationsPerBlock);
            BeaconBlock genesisBlock = new BeaconBlock(stateRoot);
            Hash32      expectedRoot = genesisBlock.SigningRoot(miscellaneousParameters, maxOperationsPerBlock);

            headRoot.ShouldBe(expectedRoot);
        }
        public Hash32 HashTreeRoot(BeaconState beaconState)
        {
            MiscellaneousParameters miscellaneousParameters = _miscellaneousParameterOptions.CurrentValue;
            TimeParameters          timeParameters          = _timeParameterOptions.CurrentValue;
            StateListLengths        stateListLengths        = _stateListLengthOptions.CurrentValue;
            MaxOperationsPerBlock   maxOperationsPerBlock   = _maxOperationsPerBlockOptions.CurrentValue;
            ulong maximumAttestationsPerEpoch = maxOperationsPerBlock.MaximumAttestations * (ulong)timeParameters.SlotsPerEpoch;

            return(beaconState.HashTreeRoot(stateListLengths.HistoricalRootsLimit,
                                            timeParameters.SlotsPerEth1VotingPeriod, stateListLengths.ValidatorRegistryLimit,
                                            maximumAttestationsPerEpoch, miscellaneousParameters.MaximumValidatorsPerCommittee));
        }
Пример #3
0
        public Hash32 HashTreeRoot(BeaconState beaconState)
        {
            MiscellaneousParameters miscellaneousParameters = _miscellaneousParameterOptions.CurrentValue;
            TimeParameters timeParameters =  _timeParameterOptions.CurrentValue;
            StateListLengths stateListLengths =  _stateListLengthOptions.CurrentValue;
            MaxOperationsPerBlock maxOperationsPerBlock = _maxOperationsPerBlockOptions.CurrentValue;
            ulong maximumAttestationsPerEpoch = maxOperationsPerBlock.MaximumAttestations * (ulong) timeParameters.SlotsPerEpoch;
            return beaconState.HashTreeRoot(stateListLengths.HistoricalRootsLimit,
                timeParameters.SlotsPerEth1VotingPeriod, stateListLengths.ValidatorRegistryLimit,
                maximumAttestationsPerEpoch, miscellaneousParameters.MaximumValidatorsPerCommittee);

//            Merkle.Ize(out UInt256 root, beaconState);
//            Span<byte> bytes = MemoryMarshal.Cast<UInt256, byte>(MemoryMarshal.CreateSpan(ref root, 1));
//            return new Hash32(bytes);
        }
Пример #4
0
        /// <summary>
        /// State transition via the provided ``block``
        /// then package the block with the state root and signature.
        /// </summary>
        public static void StateTransitionAndSignBlock(IServiceProvider testServiceProvider, BeaconState state, BeaconBlock block)
        {
            MiscellaneousParameters miscellaneousParameters = testServiceProvider.GetService <IOptions <MiscellaneousParameters> >().Value;
            TimeParameters          timeParameters          = testServiceProvider.GetService <IOptions <TimeParameters> >().Value;
            StateListLengths        stateListLengths        = testServiceProvider.GetService <IOptions <StateListLengths> >().Value;
            MaxOperationsPerBlock   maxOperationsPerBlock   = testServiceProvider.GetService <IOptions <MaxOperationsPerBlock> >().Value;

            BeaconStateTransition beaconStateTransition = testServiceProvider.GetService <BeaconStateTransition>();

            beaconStateTransition.StateTransition(state, block, validateStateRoot: false);
            Hash32 stateRoot = state.HashTreeRoot(miscellaneousParameters, timeParameters, stateListLengths, maxOperationsPerBlock);

            block.SetStateRoot(stateRoot);
            TestBlock.SignBlock(testServiceProvider, state, block, ValidatorIndex.None);
        }
Пример #5
0
        public void ProcessSlot(BeaconState state)
        {
            _logger.LogInformation(Event.ProcessSlot, "Process current slot {Slot} for state {BeaconState}", state.Slot, state);
            // Cache state root
            var previousStateRoot = state.HashTreeRoot(_miscellaneousParameterOptions.CurrentValue, _timeParameterOptions.CurrentValue,
                                                       _stateListLengthOptions.CurrentValue, _maxOperationsPerBlockOptions.CurrentValue);
            var previousRootIndex = state.Slot % _timeParameterOptions.CurrentValue.SlotsPerHistoricalRoot;

            state.SetStateRoot(previousRootIndex, previousStateRoot);
            // Cache latest block header state root
            if (state.LatestBlockHeader.StateRoot == Hash32.Zero)
            {
                state.LatestBlockHeader.SetStateRoot(previousStateRoot);
            }
            // Cache block root
            var previousBlockRoot = state.LatestBlockHeader.SigningRoot();

            state.SetBlockRoot(previousRootIndex, previousBlockRoot);
        }
Пример #6
0
        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(
                                                 new BlsSignature(),
                                                 eth1Data,
                                                 new Bytes32(),
                                                 Array.Empty <ProposerSlashing>(),
                                                 Array.Empty <AttesterSlashing>(),
                                                 Array.Empty <Attestation>(),
                                                 Array.Empty <Deposit>(),
                                                 Array.Empty <VoluntaryExit>()
                                                 ),
                                             new BlsSignature());

            if (signed)
            {
                SignBlock(testServiceProvider, state, emptyBlock, ValidatorIndex.None);
            }

            return(emptyBlock);
        }
Пример #7
0
        public IStore GetGenesisStore(BeaconState genesisState)
        {
            var miscellaneousParameters = _miscellaneousParameterOptions.CurrentValue;
            var maxOperationsPerBlock   = _maxOperationsPerBlockOptions.CurrentValue;

            var stateRoot           = genesisState.HashTreeRoot(miscellaneousParameters, _timeParameterOptions.CurrentValue, _stateListLengthOptions.CurrentValue, maxOperationsPerBlock);
            var genesisBlock        = new BeaconBlock(stateRoot);
            var root                = genesisBlock.SigningRoot(miscellaneousParameters, maxOperationsPerBlock);
            var justifiedCheckpoint = new Checkpoint(_initialValueOptions.CurrentValue.GenesisEpoch, root);
            var finalizedCheckpoint = new Checkpoint(_initialValueOptions.CurrentValue.GenesisEpoch, root);

            _logger.LogInformation(Event.CreateGenesisStore, "Creating genesis store with block {BeaconBlock} for state {BeaconState}, with checkpoint {JustifiedCheckpoint}, with signing root {SigningRoot}",
                                   genesisBlock, genesisState, justifiedCheckpoint, root);

            var blocks = new Dictionary <Hash32, BeaconBlock>
            {
                [root] = genesisBlock
            };
            var blockStates = new Dictionary <Hash32, BeaconState>
            {
                [root] = BeaconState.Clone(genesisState)
            };
            var checkpointStates = new Dictionary <Checkpoint, BeaconState>
            {
                [justifiedCheckpoint] = BeaconState.Clone(genesisState)
            };

            var store = _storeProvider.CreateStore(
                genesisState.GenesisTime,
                genesisState.GenesisTime,
                justifiedCheckpoint,
                finalizedCheckpoint,
                justifiedCheckpoint,
                blocks,
                blockStates,
                checkpointStates,
                new Dictionary <ValidatorIndex, LatestMessage>()
                );

            return(store);
        }
Пример #8
0
        public BeaconState StateTransition(BeaconState state, BeaconBlock block, bool validateStateRoot)
        {
            _logger.LogInformation(Event.ProcessSlots, "State transition for state {BeaconState} with block {BeaconBlock}; validating {ValidateStateRoot}.",
                                   state, block, validateStateRoot);

            // Process slots (including those with no blocks) since block
            ProcessSlots(state, block.Slot);
            // Process block
            ProcessBlock(state, block);
            // Validate state root (True in production)
            if (validateStateRoot)
            {
                //var options = new System.Text.Json.JsonSerializerOptions { WriteIndented = true };
                //options.AddCortexContainerConverters();
                //var debugState = System.Text.Json.JsonSerializer.Serialize(state, options);

                var checkStateRoot = state.HashTreeRoot(_miscellaneousParameterOptions.CurrentValue, _timeParameterOptions.CurrentValue, _stateListLengthOptions.CurrentValue, _maxOperationsPerBlockOptions.CurrentValue);
                if (block.StateRoot != checkStateRoot)
                {
                    throw new Exception($"Mismatch between calculated state root {checkStateRoot} and block state root {block.StateRoot}.");
                }
            }
            return(state);
        }