Пример #1
0
        public async Task ShouldReturnDefaultForkVersion()
        {
            // Arrange
            IServiceCollection testServiceCollection = TestSystem.BuildTestServiceCollection(useStore: true);

            testServiceCollection.AddSingleton <IHostEnvironment>(Substitute.For <IHostEnvironment>());
            testServiceCollection.AddSingleton <IEth1DataProvider>(Substitute.For <IEth1DataProvider>());
            testServiceCollection.AddSingleton <IOperationPool>(Substitute.For <IOperationPool>());
            ServiceProvider testServiceProvider = testServiceCollection.BuildServiceProvider();
            BeaconState     state      = TestState.PrepareTestState(testServiceProvider);
            ForkChoice      forkChoice = testServiceProvider.GetService <ForkChoice>();

            // Get genesis store initialise MemoryStoreProvider with the state
            _ = forkChoice.GetGenesisStore(state);

            // Act
            IBeaconNodeApi beaconNode = testServiceProvider.GetService <IBeaconNodeApi>();

            beaconNode.ShouldBeOfType(typeof(BeaconNodeFacade));

            Core2.Containers.Fork fork = await beaconNode.GetNodeForkAsync(CancellationToken.None);

            // Assert
            fork.Epoch.ShouldBe(Epoch.Zero);
            fork.CurrentVersion.ShouldBe(new ForkVersion());
            fork.PreviousVersion.ShouldBe(new ForkVersion());
        }
Пример #2
0
        public async Task UpdateForkVersionActivityAsync(CancellationToken cancellationToken)
        {
            // TODO: Should we be validating this?  i.e. check against config that it is the chain ID we are expecting?
            // TODO: At least for prior to cutover epoch (allows operation when fork epoch has not yet been reached and only one side is updated)
            // TODO: Once version is different for the current epoch, should disconnect.

            Activity activity = new Activity("update-fork-version");

            activity.Start();
            try
            {
                var forkResponse = await _beaconNodeApi.GetNodeForkAsync(cancellationToken).ConfigureAwait(false);

                if (forkResponse.StatusCode == StatusCode.Success)
                {
                    await _beaconChainInformation.SetForkAsync(forkResponse.Content).ConfigureAwait(false);
                }
                else
                {
                    Log.ErrorGettingForkVersion(_logger, (int)forkResponse.StatusCode, forkResponse.StatusCode, null);
                }
            }
            catch (Exception ex)
            {
                Log.ExceptionGettingForkVersion(_logger, ex.Message, ex);
            }
            finally
            {
                activity.Stop();
            }
        }
Пример #3
0
        /// <summary>Get fork information from running beacon node.</summary>
        /// <returns>Request successful</returns>
        public async Task <Response2> ForkAsync()
        {
            Core2.Containers.Fork fork = await _beaconNode.GetNodeForkAsync();

            Response2 response2 = new Response2();

            // TODO: Not sure what chain ID should be.
            response2.Chain_id              = 0;
            response2.Fork                  = new Fork();
            response2.Fork.Epoch            = fork.Epoch;
            response2.Fork.Current_version  = fork.CurrentVersion.AsSpan().ToArray();
            response2.Fork.Previous_version = fork.PreviousVersion.AsSpan().ToArray();
            return(response2);
        }
Пример #4
0
        public async Task <IActionResult> GetAsync(CancellationToken cancellationToken)
        {
            if (_logger.IsDebug())
            {
                LogDebug.NodeForkRequested(_logger, null);
            }

            ApiResponse <Fork> apiResponse = await _beaconNode.GetNodeForkAsync(cancellationToken).ConfigureAwait(false);

            if (apiResponse.StatusCode == Core2.Api.StatusCode.Success)
            {
                ForkInformation forkInformation = new ForkInformation(0, apiResponse.Content);
                return(Ok(forkInformation));
            }

            return(Problem("Beacon node internal error.", statusCode: (int)apiResponse.StatusCode));
        }
Пример #5
0
        public async Task BasicNewBlock()
        {
            // Arrange
            int numberOfValidators = 64;
            int genesisTime        = 1578009600;
            IServiceCollection testServiceCollection = TestSystem.BuildTestServiceCollection(useStore: true);
            IConfigurationRoot configuration         = new ConfigurationBuilder()
                                                       .AddInMemoryCollection(new Dictionary <string, string>
            {
                ["QuickStart:ValidatorCount"] = $"{numberOfValidators}",
                ["QuickStart:GenesisTime"]    = $"{genesisTime}"
            })
                                                       .Build();

            testServiceCollection.AddBeaconNodeQuickStart(configuration);
            testServiceCollection.AddBeaconNodeEth1Bridge(configuration);
            testServiceCollection.AddSingleton <IHostEnvironment>(Substitute.For <IHostEnvironment>());
            ServiceProvider testServiceProvider = testServiceCollection.BuildServiceProvider();

            Eth1BridgeWorker eth1BridgeWorker =
                testServiceProvider.GetServices <IHostedService>().OfType <Eth1BridgeWorker>().First();
            await eth1BridgeWorker.ExecuteEth1GenesisAsync(CancellationToken.None);

            IBeaconNodeApi     beaconNode   = testServiceProvider.GetService <IBeaconNodeApi>();
            ApiResponse <Fork> forkResponse = await beaconNode.GetNodeForkAsync(CancellationToken.None);

            Fork fork = forkResponse.Content;

            fork.CurrentVersion.ShouldBe(new ForkVersion(new byte[4] {
                0x00, 0x00, 0x00, 0x01
            }));

            // Act
            BlockProducer blockProducer = testServiceProvider.GetService <BlockProducer>();
            Slot          targetSlot    = new Slot(1);

            // With QuickStart64, proposer for Slot 1 is validator index 29, 0xa98ed496...
            QuickStartMockEth1GenesisProvider quickStartMockEth1GenesisProvider = (QuickStartMockEth1GenesisProvider)testServiceProvider.GetService <IEth1GenesisProvider>();

            byte[]       privateKey   = quickStartMockEth1GenesisProvider.GeneratePrivateKey(29);
            BlsSignature randaoReveal = GetEpochSignature(testServiceProvider, privateKey, fork.CurrentVersion, targetSlot);

            // value for quickstart 20/64, fork 0, slot 1
            randaoReveal.ToString().StartsWith("0x932f8730");
            BeaconBlock newBlock = await blockProducer.NewBlockAsync(targetSlot, randaoReveal, CancellationToken.None);

            // Assert
            newBlock.Slot.ShouldBe(targetSlot);
            newBlock.Body.RandaoReveal.ShouldBe(randaoReveal);

            newBlock.ParentRoot.ToString().ShouldStartWith("0x4d4e9a16");

            newBlock.Body.Eth1Data.DepositCount.ShouldBe((ulong)numberOfValidators);

            newBlock.Body.Eth1Data.DepositRoot.ToString().ShouldStartWith("0x66687aad");

            newBlock.StateRoot.ToString().ShouldStartWith("0x0138b69f");

            newBlock.Body.Attestations.Count.ShouldBe(0);
            newBlock.Body.Deposits.Count.ShouldBe(0);
        }
Пример #6
0
        public async Task BasicNewBlock()
        {
            // Arrange
            int numberOfValidators = 64;
            int genesisTime        = 1578009600;
            IServiceCollection testServiceCollection = TestSystem.BuildTestServiceCollection(useStore: true);
            IConfigurationRoot configuration         = new ConfigurationBuilder()
                                                       .AddInMemoryCollection(new Dictionary <string, string>
            {
                ["QuickStart:ValidatorCount"] = $"{numberOfValidators}",
                ["QuickStart:GenesisTime"]    = $"{genesisTime}"
            })
                                                       .Build();

            testServiceCollection.AddBeaconNodeQuickStart(configuration);
            testServiceCollection.AddSingleton <IHostEnvironment>(Substitute.For <IHostEnvironment>());
            ServiceProvider testServiceProvider = testServiceCollection.BuildServiceProvider();

            QuickStart quickStart = (QuickStart)testServiceProvider.GetService <INodeStart>();
            await quickStart.InitializeNodeAsync();

            IBeaconNodeApi beaconNode = testServiceProvider.GetService <IBeaconNodeApi>();

            Core2.Containers.Fork fork = await beaconNode.GetNodeForkAsync(CancellationToken.None);

            fork.CurrentVersion.ShouldBe(new ForkVersion());

            // Act
            BlockProducer blockProducer = testServiceProvider.GetService <BlockProducer>();
            Slot          targetSlot    = new Slot(1);

            // With QuickStart64, proposer for Slot 1 is validator index 20, 0xa1c76af1...
            byte[]       privateKey   = quickStart.GeneratePrivateKey(20);
            BlsSignature randaoReveal = GetEpochSignature(testServiceProvider, privateKey, fork.CurrentVersion, targetSlot);

            // value for quickstart 20/64, fork 0, slot 1
            randaoReveal.ToString().ShouldBe("0xa3426b6391a29c88f2280428d5fdae9e20f4c75a8d38d0714e3aa5b9e55594dbd555c4bc685191e83d39158c3be9744d06adc34b21d2885998a206e3b3fd435eab424cf1c01b8fd562deb411348a601e83d7332d8774d1fd3bf8b88d7a33c67c");
            BeaconBlock newBlock = await blockProducer.NewBlockAsync(targetSlot, randaoReveal);

            // Assert
            newBlock.Slot.ShouldBe(targetSlot);
            newBlock.Body.RandaoReveal.ShouldBe(randaoReveal);

            Hash32 expectedParentRoot = new Hash32("0x91b06cbcd6dc97b89dc8b95e0b01a497932683b182e6c722ddfa10cd005b2180");

            newBlock.ParentRoot.ShouldBe(expectedParentRoot);

            newBlock.Body.Eth1Data.DepositCount.ShouldBe((ulong)numberOfValidators);

            Hash32 expectedEth1DataDepositRoot = new Hash32("0x66687aadf862bd776c8fc18b8e9f8e20089714856ee233b3902a591d0d5f2925");

            newBlock.Body.Eth1Data.DepositRoot.ShouldBe(expectedEth1DataDepositRoot);

            Hash32 expectedStateRoot = new Hash32("0xe05ccf6347cac0b0dab0ad0d5c941fe7c7e2ed4c69550ed9a628bc9d62914242");

            newBlock.StateRoot.ShouldBe(expectedStateRoot);

            newBlock.Signature.ShouldBe(new BlsSignature(new byte[96])); // signature should be empty

            newBlock.Body.Attestations.Count.ShouldBe(0);
            newBlock.Body.Deposits.Count.ShouldBe(0);
        }
Пример #7
0
        public async Task UpdateForkVersion(CancellationToken cancellationToken)
        {
            Fork fork = await _beaconNodeApi.GetNodeForkAsync(cancellationToken).ConfigureAwait(false);

            await _beaconChain.SetForkAsync(fork).ConfigureAwait(false);
        }
Пример #8
0
        public async Task BasicNewBlock()
        {
            // Arrange
            int numberOfValidators = 64;
            int genesisTime        = 1578009600;
            IServiceCollection testServiceCollection = TestSystem.BuildTestServiceCollection(useStore: true);
            IConfigurationRoot configuration         = new ConfigurationBuilder()
                                                       .AddInMemoryCollection(new Dictionary <string, string>
            {
                ["QuickStart:ValidatorCount"] = $"{numberOfValidators}",
                ["QuickStart:GenesisTime"]    = $"{genesisTime}"
            })
                                                       .Build();

            testServiceCollection.AddQuickStart(configuration);
            testServiceCollection.AddSingleton <IHostEnvironment>(Substitute.For <IHostEnvironment>());
            ServiceProvider testServiceProvider = testServiceCollection.BuildServiceProvider();

            QuickStart quickStart = (QuickStart)testServiceProvider.GetService <INodeStart>();
            await quickStart.InitializeNodeAsync();

            IBeaconNodeApi beaconNode = testServiceProvider.GetService <IBeaconNodeApi>();

            Core2.Containers.Fork fork = await beaconNode.GetNodeForkAsync();

            fork.CurrentVersion.ShouldBe(new ForkVersion());

            // Act
            BlockProducer blockProducer = testServiceProvider.GetService <BlockProducer>();
            Slot          targetSlot    = new Slot(1);

            // With QuickStart64, proposer for Slot 1 is validator index 20, 0xa1c76af1...
            byte[]       privateKey   = quickStart.GeneratePrivateKey(20);
            BlsSignature randaoReveal = GetEpochSignature(testServiceProvider, privateKey, fork.CurrentVersion, targetSlot);

            // value for quickstart 20/64, fork 0, slot 1
            randaoReveal.ToString().ShouldBe("0xa3426b6391a29c88f2280428d5fdae9e20f4c75a8d38d0714e3aa5b9e55594dbd555c4bc685191e83d39158c3be9744d06adc34b21d2885998a206e3b3fd435eab424cf1c01b8fd562deb411348a601e83d7332d8774d1fd3bf8b88d7a33c67c");
            BeaconBlock newBlock = await blockProducer.NewBlockAsync(targetSlot, randaoReveal);

            // Assert
            newBlock.Slot.ShouldBe(targetSlot);
            newBlock.Body.RandaoReveal.ShouldBe(randaoReveal);

            Hash32 expectedParentRoot = new Hash32(Bytes.FromHexString("0x3111350140726cc0501223143ae5c7baad7f5a06764fcc7d444a657016e7d616"));

            newBlock.ParentRoot.ShouldBe(expectedParentRoot);

            newBlock.Body.Eth1Data.DepositCount.ShouldBe((ulong)numberOfValidators);

            Hash32 expectedEth1DataDepositRoot = new Hash32(Bytes.FromHexString("0x66687aadf862bd776c8fc18b8e9f8e20089714856ee233b3902a591d0d5f2925"));

            newBlock.Body.Eth1Data.DepositRoot.ShouldBe(expectedEth1DataDepositRoot);

            Hash32 expectedStateRoot = new Hash32(Bytes.FromHexString("0x9c7d3e5180f95175691511fd56f8a610299f0b5a682b6fe178230493d74f6d13"));

            newBlock.StateRoot.ShouldBe(expectedStateRoot);

            newBlock.Signature.ShouldBe(new BlsSignature(new byte[96])); // signature should be empty

            newBlock.Body.Attestations.Count.ShouldBe(0);
            newBlock.Body.Deposits.Count.ShouldBe(0);
        }