Ejemplo n.º 1
0
        public async Task QuickStart64DutiesForEpochZeroHaveExactlyOneProposerPerSlot()
        {
            // 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();

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

            IStoreProvider storeProvider = testServiceProvider.GetService <IStoreProvider>();

            storeProvider.TryGetStore(out IStore? store).ShouldBeTrue();
            BeaconState state = await store !.GetBlockStateAsync(store !.FinalizedCheckpoint.Root);

            // Act
            Epoch targetEpoch = new Epoch(0);
            IEnumerable <BlsPublicKey> validatorPublicKeys = state !.Validators.Select(x => x.PublicKey);
            IBeaconNodeApi             beaconNode          = testServiceProvider.GetService <IBeaconNodeApi>();

            beaconNode.ShouldBeOfType(typeof(BeaconNodeFacade));

            int validatorDutyIndex = 0;
            List <ValidatorDuty> validatorDuties = new List <ValidatorDuty>();

            await foreach (ValidatorDuty validatorDuty in beaconNode.ValidatorDutiesAsync(validatorPublicKeys, targetEpoch, CancellationToken.None))
            {
                validatorDuties.Add(validatorDuty);
                Console.WriteLine("Index [{0}], Epoch {1}, Validator {2}, : attestation slot {3}, shard {4}, proposal slot {5}",
                                  validatorDutyIndex, targetEpoch, validatorDuty.ValidatorPublicKey, validatorDuty.AttestationSlot,
                                  (ulong)validatorDuty.AttestationShard, validatorDuty.BlockProposalSlot);
                validatorDutyIndex++;
            }

            // Assert
            Dictionary <Slot, IGrouping <Slot, ValidatorDuty> > groupsByProposalSlot = validatorDuties
                                                                                       .GroupBy(x => x.BlockProposalSlot)
                                                                                       .ToDictionary(x => x.Key, x => x);

            groupsByProposalSlot[new Slot(0)].Count().ShouldBe(1);
            groupsByProposalSlot[new Slot(1)].Count().ShouldBe(1);
            groupsByProposalSlot[new Slot(2)].Count().ShouldBe(1);
            groupsByProposalSlot[new Slot(3)].Count().ShouldBe(1);
            groupsByProposalSlot[new Slot(4)].Count().ShouldBe(1);
            groupsByProposalSlot[new Slot(5)].Count().ShouldBe(1);
            groupsByProposalSlot[new Slot(6)].Count().ShouldBe(1);
            //groupsByProposalSlot[new Slot(7)].Count().ShouldBe(1);
            //groupsByProposalSlot[Slot.None].Count().ShouldBe(numberOfValidators - 8);
        }
Ejemplo n.º 2
0
        public async Task TestValidators80DutiesForEpochZeroHaveExactlyOneProposerPerSlot()
        {
            // 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);

            TimeParameters timeParameters     = testServiceProvider.GetService <IOptions <TimeParameters> >().Value;
            int            numberOfValidators = state.Validators.Count;

            Console.WriteLine("Number of validators: {0}", numberOfValidators);
            BlsPublicKey[] publicKeys  = TestKeys.PublicKeys(timeParameters).ToArray();
            byte[][]       privateKeys = TestKeys.PrivateKeys(timeParameters).ToArray();
            for (int index = 0; index < numberOfValidators; index++)
            {
                Console.WriteLine("[{0}] priv:{1} pub:{2}", index, "0x" + BitConverter.ToString(privateKeys[index]).Replace("-", ""), publicKeys[index]);
            }

            // Act
            Epoch targetEpoch = new Epoch(0);
            IEnumerable <BlsPublicKey> validatorPublicKeys = publicKeys.Take(numberOfValidators);
            IBeaconNodeApi             beaconNode          = testServiceProvider.GetService <IBeaconNodeApi>();

            beaconNode.ShouldBeOfType(typeof(BeaconNodeFacade));

            int validatorDutyIndex = 0;
            List <ValidatorDuty> validatorDuties = new List <ValidatorDuty>();

            await foreach (ValidatorDuty validatorDuty in beaconNode.ValidatorDutiesAsync(validatorPublicKeys, targetEpoch, CancellationToken.None))
            {
                validatorDuties.Add(validatorDuty);
                Console.WriteLine("Index [{0}], Epoch {1}, Validator {2}, : attestation slot {3}, shard {4}, proposal slot {5}",
                                  validatorDutyIndex, targetEpoch, validatorDuty.ValidatorPublicKey, validatorDuty.AttestationSlot,
                                  (ulong)validatorDuty.AttestationShard, validatorDuty.BlockProposalSlot);
                validatorDutyIndex++;
            }

            // Assert
            Dictionary <Slot, IGrouping <Slot, ValidatorDuty> > groupsByProposalSlot = validatorDuties
                                                                                       .GroupBy(x => x.BlockProposalSlot)
                                                                                       .ToDictionary(x => x.Key, x => x);

            groupsByProposalSlot[new Slot(0)].Count().ShouldBe(1);
            groupsByProposalSlot[new Slot(1)].Count().ShouldBe(1);
            groupsByProposalSlot[new Slot(2)].Count().ShouldBe(1);
            groupsByProposalSlot[new Slot(3)].Count().ShouldBe(1);
            groupsByProposalSlot[new Slot(4)].Count().ShouldBe(1);
            groupsByProposalSlot[new Slot(5)].Count().ShouldBe(1);
            //groupsByProposalSlot[new Slot(6)].Count().ShouldBe(1);
            groupsByProposalSlot[new Slot(7)].Count().ShouldBe(1);
            //groupsByProposalSlot[Slot.None].Count().ShouldBe(numberOfValidators - 7);
        }
Ejemplo n.º 3
0
        public async Task NodeVersionInitialFailTryAgain()
        {
            // Arrange
            IServiceCollection testServiceCollection = TestSystem.BuildTestServiceCollection();

            IBeaconNodeOApiClient beaconNodeOApiClient1 = Substitute.For <IBeaconNodeOApiClient>();

            beaconNodeOApiClient1.VersionAsync(Arg.Any <CancellationToken>()).Throws(new HttpRequestException("TESTEXCEPTION"));
            IBeaconNodeOApiClient beaconNodeOApiClient2 = Substitute.For <IBeaconNodeOApiClient>();

            beaconNodeOApiClient2.VersionAsync(Arg.Any <CancellationToken>()).Returns("TESTVERSION");
            IBeaconNodeOApiClientFactory beaconNodeOApiClientFactory = Substitute.For <IBeaconNodeOApiClientFactory>();

            beaconNodeOApiClientFactory.CreateClient(Arg.Any <string>()).Returns(beaconNodeOApiClient1, beaconNodeOApiClient2);
            testServiceCollection.AddSingleton <IBeaconNodeOApiClientFactory>(beaconNodeOApiClientFactory);

            ServiceProvider testServiceProvider = testServiceCollection.BuildServiceProvider();

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

            beaconNodeProxy.ShouldBeOfType(typeof(BeaconNodeProxy));
            string version1 = await beaconNodeProxy.GetNodeVersionAsync(CancellationToken.None);

            // Assert
            version1.ShouldBe("TESTVERSION");
            beaconNodeOApiClientFactory.CreateClient(Arg.Any <string>()).Received(2);
        }
Ejemplo n.º 4
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());
        }
Ejemplo n.º 5
0
        public async Task NodeVersionTwiceShouldUseSameClient()
        {
            // Arrange
            IServiceCollection testServiceCollection = TestSystem.BuildTestServiceCollection();

            IBeaconNodeOApiClient beaconNodeOApiClient = Substitute.For <IBeaconNodeOApiClient>();

            beaconNodeOApiClient.VersionAsync(Arg.Any <CancellationToken>()).Returns("TESTVERSION");
            IBeaconNodeOApiClientFactory beaconNodeOApiClientFactory = Substitute.For <IBeaconNodeOApiClientFactory>();

            beaconNodeOApiClientFactory.CreateClient(Arg.Any <string>()).Returns(beaconNodeOApiClient);
            testServiceCollection.AddSingleton <IBeaconNodeOApiClientFactory>(beaconNodeOApiClientFactory);

            ServiceProvider testServiceProvider = testServiceCollection.BuildServiceProvider();

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

            beaconNodeProxy.ShouldBeOfType(typeof(BeaconNodeProxy));
            string version1 = await beaconNodeProxy.GetNodeVersionAsync(CancellationToken.None);

            string version2 = await beaconNodeProxy.GetNodeVersionAsync(CancellationToken.None);

            // Assert
            version1.ShouldBe("TESTVERSION");
            version2.ShouldBe("TESTVERSION");
            beaconNodeOApiClientFactory.CreateClient(Arg.Any <string>()).Received(1);
        }
Ejemplo n.º 6
0
        public async Task TestValidators80DutiesForEpochOneHaveExactlyOneProposerPerSlot()
        {
            // 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);

            TimeParameters timeParameters     = testServiceProvider.GetService <IOptions <TimeParameters> >().Value;
            int            numberOfValidators = state.Validators.Count;

            Console.WriteLine("Number of validators: {0}", numberOfValidators);
            BlsPublicKey[] publicKeys = TestKeys.PublicKeys(timeParameters).ToArray();

            // Act
            Epoch          targetEpoch         = new Epoch(1);
            var            validatorPublicKeys = publicKeys.Take(numberOfValidators);
            IBeaconNodeApi beaconNode          = testServiceProvider.GetService <IBeaconNodeApi>();

            beaconNode.ShouldBeOfType(typeof(BeaconNodeFacade));
            var validatorDuties = await beaconNode.ValidatorDutiesAsync(validatorPublicKeys, targetEpoch);

            for (var index = 0; index < validatorDuties.Count; index++)
            {
                ValidatorDuty validatorDuty = validatorDuties[index];
                Console.WriteLine("Index [{0}], Epoch {1}, Validator {2}, : attestation slot {3}, shard {4}, proposal slot {5}",
                                  index, targetEpoch, validatorDuty.ValidatorPublicKey, validatorDuty.AttestationSlot,
                                  (ulong)validatorDuty.AttestationShard, validatorDuty.BlockProposalSlot);
            }

            // Assert
            Dictionary <Slot, IGrouping <Slot, ValidatorDuty> > groupsByProposalSlot = validatorDuties
                                                                                       .GroupBy(x => x.BlockProposalSlot)
                                                                                       .ToDictionary(x => x.Key, x => x);

            groupsByProposalSlot[new Slot(8)].Count().ShouldBe(1);
            groupsByProposalSlot[new Slot(9)].Count().ShouldBe(1);
            groupsByProposalSlot[new Slot(10)].Count().ShouldBe(1);
            groupsByProposalSlot[new Slot(11)].Count().ShouldBe(1);
            //groupsByProposalSlot[new Slot(12)].Count().ShouldBe(1);
            groupsByProposalSlot[new Slot(13)].Count().ShouldBe(1);
            groupsByProposalSlot[new Slot(14)].Count().ShouldBe(1);
            groupsByProposalSlot[new Slot(15)].Count().ShouldBe(1);
            //groupsByProposalSlot[Slot.None].Count().ShouldBe(numberOfValidators - 8);
        }
        public async Task ShouldGetStatusWhenSyncComplete()
        {
            // Arrange
            Slot            starting           = Slot.One;
            Slot            current            = new Slot(11);
            Slot            highest            = new Slot(11);
            INetworkPeering mockNetworkPeering = Substitute.For <INetworkPeering>();

            mockNetworkPeering.HighestPeerSlot.Returns(highest);
            mockNetworkPeering.SyncStartingSlot.Returns(starting);
            IStore            mockStore   = Substitute.For <IStore>();
            Root              root        = new Root(Enumerable.Repeat((byte)0x12, 32).ToArray());
            Checkpoint        checkpoint  = new Checkpoint(Epoch.Zero, root);
            SignedBeaconBlock signedBlock =
                new SignedBeaconBlock(new BeaconBlock(current, Root.Zero, Root.Zero, BeaconBlockBody.Zero),
                                      BlsSignature.Zero);
            BeaconState state = TestState.Create(slot: current, finalizedCheckpoint: checkpoint);

            mockStore.GetHeadAsync().Returns(root);
            mockStore.GetSignedBlockAsync(root).Returns(signedBlock);
            mockStore.GetBlockStateAsync(root).Returns(state);
            mockStore.IsInitialized.Returns(true);
            mockStore.JustifiedCheckpoint.Returns(checkpoint);

            IServiceCollection testServiceCollection = TestSystem.BuildTestServiceCollection(useStore: true);

            testServiceCollection.AddSingleton(mockNetworkPeering);
            testServiceCollection.AddSingleton(mockStore);
            testServiceCollection.AddSingleton <IHostEnvironment>(Substitute.For <IHostEnvironment>());
            testServiceCollection.AddSingleton <IEth1DataProvider>(Substitute.For <IEth1DataProvider>());
            testServiceCollection.AddSingleton <IOperationPool>(Substitute.For <IOperationPool>());
            ServiceProvider testServiceProvider = testServiceCollection.BuildServiceProvider();

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

            beaconNode.ShouldBeOfType(typeof(BeaconNodeFacade));

            ApiResponse <Syncing> syncingResponse = await beaconNode.GetSyncingAsync(CancellationToken.None);

            Syncing syncing = syncingResponse.Content;

            // Assert
            syncing.IsSyncing.ShouldBeFalse();
            syncing.SyncStatus !.StartingSlot.ShouldBe(Slot.One);
            syncing.SyncStatus.CurrentSlot.ShouldBe(new Slot(11));
            syncing.SyncStatus.HighestSlot.ShouldBe(new Slot(11));
        }
Ejemplo n.º 8
0
 public HonestValidatorWorker(ILogger <HonestValidatorWorker> logger,
                              IClock clock,
                              IHostEnvironment environment,
                              DataDirectory dataDirectory,
                              IBeaconNodeApi beaconNodeApi,
                              BeaconChainInformation beaconChainInformation,
                              ValidatorClient validatorClient,
                              IClientVersion clientVersion)
 {
     _logger                 = logger;
     _clock                  = clock;
     _environment            = environment;
     _dataDirectory          = dataDirectory;
     _beaconNodeApi          = beaconNodeApi;
     _beaconChainInformation = beaconChainInformation;
     _validatorClient        = validatorClient;
     _clientVersion          = clientVersion;
 }
Ejemplo n.º 9
0
 public HonestValidatorWorker(ILogger <HonestValidatorWorker> logger,
                              IClock clock,
                              IHostEnvironment environment,
                              IConfiguration configuration,
                              IBeaconNodeApi beaconNodeApi,
                              BeaconChain beaconChain,
                              ValidatorClient validatorClient,
                              IClientVersion clientVersion)
 {
     _logger          = logger;
     _clock           = clock;
     _environment     = environment;
     _configuration   = configuration;
     _beaconNodeApi   = beaconNodeApi;
     _beaconChain     = beaconChain;
     _validatorClient = validatorClient;
     _clientVersion   = clientVersion;
 }
Ejemplo n.º 10
0
        public async Task NodeVersionInitialFailAfterSuccessTryAgain()
        {
            // Arrange
            IServiceCollection testServiceCollection = TestSystem.BuildTestServiceCollection();

            IBeaconNodeOApiClient beaconNodeOApiClient1 = Substitute.For <IBeaconNodeOApiClient>();

            beaconNodeOApiClient1.VersionAsync(Arg.Any <CancellationToken>()).Returns(
                callInfo => "TESTVERSION1",
                callInfo => throw new HttpRequestException("TESTEXCEPTION")
                );
            beaconNodeOApiClient1.BaseUrl.Returns("CLIENT1");
            IBeaconNodeOApiClient beaconNodeOApiClient2 = Substitute.For <IBeaconNodeOApiClient>();

            beaconNodeOApiClient2.VersionAsync(Arg.Any <CancellationToken>()).Returns("TESTVERSION2");
            IBeaconNodeOApiClientFactory beaconNodeOApiClientFactory = Substitute.For <IBeaconNodeOApiClientFactory>();

            beaconNodeOApiClientFactory.CreateClient(Arg.Any <string>()).Returns(beaconNodeOApiClient1, beaconNodeOApiClient2);
            testServiceCollection.AddSingleton <IBeaconNodeOApiClientFactory>(beaconNodeOApiClientFactory);

            ServiceProvider testServiceProvider = testServiceCollection.BuildServiceProvider();

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

            beaconNodeProxy.ShouldBeOfType(typeof(BeaconNodeProxy));
            string version1 = await beaconNodeProxy.GetNodeVersionAsync(CancellationToken.None);

            string version2 = await beaconNodeProxy.GetNodeVersionAsync(CancellationToken.None);

            // Assert
            version1.ShouldBe("TESTVERSION1");
            version2.ShouldBe("TESTVERSION2");
            beaconNodeOApiClientFactory.CreateClient(Arg.Any <string>()).Received(2);

            List <ICall> client1Received = beaconNodeOApiClient1.ReceivedCalls().ToList();

            client1Received.Count(x => x.GetMethodInfo().Name == nameof(beaconNodeOApiClient1.VersionAsync)).ShouldBe(2);

            List <ICall> client2Received = beaconNodeOApiClient2.ReceivedCalls().ToList();

            client2Received.Count(x => x.GetMethodInfo().Name == nameof(beaconNodeOApiClient1.VersionAsync)).ShouldBe(1);
        }
Ejemplo n.º 11
0
        public ValidatorClient(ILogger <ValidatorClient> logger,
                               IOptionsMonitor <InitialValues> initialValueOptions,
                               IOptionsMonitor <TimeParameters> timeParameterOptions,
                               IOptionsMonitor <SignatureDomains> signatureDomainOptions,
                               ICryptographyService cryptographyService,
                               IBeaconNodeApi beaconNodeApi,
                               IValidatorKeyProvider validatorKeyProvider,
                               BeaconChainInformation beaconChainInformation)
        {
            _logger = logger;
            _initialValueOptions    = initialValueOptions;
            _timeParameterOptions   = timeParameterOptions;
            _signatureDomainOptions = signatureDomainOptions;
            _cryptographyService    = cryptographyService;
            _beaconNodeApi          = beaconNodeApi;
            _validatorKeyProvider   = validatorKeyProvider;
            _beaconChainInformation = beaconChainInformation;

            _validatorState = new ValidatorState();
            _cache          = new MemoryCache(new MemoryCacheOptions());
        }
Ejemplo n.º 12
0
        public ValidatorClient(ILogger <ValidatorClient> logger,
                               IOptionsMonitor <MiscellaneousParameters> miscellaneousParameterOptions,
                               IOptionsMonitor <TimeParameters> timeParameterOptions,
                               IOptionsMonitor <MaxOperationsPerBlock> maxOperationsPerBlockOptions,
                               IOptionsMonitor <SignatureDomains> signatureDomainOptions,
                               ICryptographyService cryptographyService,
                               IBeaconNodeApi beaconNodeApi,
                               IValidatorKeyProvider validatorKeyProvider,
                               BeaconChain beaconChain)
        {
            _logger = logger;
            _miscellaneousParameterOptions = miscellaneousParameterOptions;
            _timeParameterOptions          = timeParameterOptions;
            _maxOperationsPerBlockOptions  = maxOperationsPerBlockOptions;
            _signatureDomainOptions        = signatureDomainOptions;
            _cryptographyService           = cryptographyService;
            _beaconNodeApi        = beaconNodeApi;
            _validatorKeyProvider = validatorKeyProvider;
            _beaconChain          = beaconChain;

            _validatorState = new ValidatorState();
        }
Ejemplo n.º 13
0
        public async Task BasicGensisTime()
        {
            // Arrange
            IServiceCollection testServiceCollection = TestSystem.BuildTestServiceCollection();

            IBeaconNodeOApiClient beaconNodeOApiClient = Substitute.For <IBeaconNodeOApiClient>();

            beaconNodeOApiClient.TimeAsync(Arg.Any <CancellationToken>()).Returns(1_578_009_600uL);
            IBeaconNodeOApiClientFactory beaconNodeOApiClientFactory = Substitute.For <IBeaconNodeOApiClientFactory>();

            beaconNodeOApiClientFactory.CreateClient(Arg.Any <string>()).Returns(beaconNodeOApiClient);
            testServiceCollection.AddSingleton <IBeaconNodeOApiClientFactory>(beaconNodeOApiClientFactory);

            ServiceProvider testServiceProvider = testServiceCollection.BuildServiceProvider();

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

            beaconNodeProxy.ShouldBeOfType(typeof(BeaconNodeProxy));
            ulong genesisTime = await beaconNodeProxy.GetGenesisTimeAsync(CancellationToken.None);

            // Assert
            genesisTime.ShouldBe(1578009600uL);
        }
Ejemplo n.º 14
0
 public NodeForkController(ILogger <NodeForkController> logger, IBeaconNodeApi beaconNode)
 {
     _logger     = logger;
     _beaconNode = beaconNode;
 }
Ejemplo n.º 15
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);
        }
        public async Task TestValidators80DutiesForEpochOneHaveExactlyOneProposerPerSlot()
        {
            // 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);
            IForkChoice     forkChoice = testServiceProvider.GetService <IForkChoice>();
            // Get genesis store initialise MemoryStoreProvider with the state
            IStore store = testServiceProvider.GetService <IStore>();
            await forkChoice.InitializeForkChoiceStoreAsync(store, state);

            TimeParameters timeParameters     = testServiceProvider.GetService <IOptions <TimeParameters> >().Value;
            int            numberOfValidators = state.Validators.Count;

            Console.WriteLine("Number of validators: {0}", numberOfValidators);
            BlsPublicKey[] publicKeys = TestKeys.PublicKeys(timeParameters).ToArray();

            // Act
            Epoch          targetEpoch         = new Epoch(1);
            var            validatorPublicKeys = publicKeys.Take(numberOfValidators).ToList();
            IBeaconNodeApi beaconNode          = testServiceProvider.GetService <IBeaconNodeApi>();

            beaconNode.ShouldBeOfType(typeof(BeaconNodeFacade));

            int validatorDutyIndex = 0;
            List <ValidatorDuty> validatorDuties = new List <ValidatorDuty>();
            var validatorDutiesResponse          =
                await beaconNode.ValidatorDutiesAsync(validatorPublicKeys, targetEpoch, CancellationToken.None);

            foreach (ValidatorDuty validatorDuty in validatorDutiesResponse.Content)
            {
                validatorDuties.Add(validatorDuty);
                Console.WriteLine("Index [{0}], Epoch {1}, Validator {2}, : attestation slot {3}, shard {4}, proposal slot {5}",
                                  validatorDutyIndex, targetEpoch, validatorDuty.ValidatorPublicKey, validatorDuty.AttestationSlot,
                                  (ulong)validatorDuty.AttestationShard, validatorDuty.BlockProposalSlot);
                validatorDutyIndex++;
            }

            Console.WriteLine();
            Console.WriteLine("** ValidatorDuty summary");
            foreach (ValidatorDuty validatorDuty in validatorDuties)
            {
                Console.WriteLine("Index [{0}], Epoch {1}, Validator {2}, : attestation slot {3}, shard {4}, proposal slot {5}",
                                  validatorDutyIndex, targetEpoch, validatorDuty.ValidatorPublicKey, validatorDuty.AttestationSlot,
                                  (ulong)validatorDuty.AttestationShard, validatorDuty.BlockProposalSlot);
            }

            // Assert
            Dictionary <Slot, IGrouping <Slot, ValidatorDuty> > groupsByProposalSlot = validatorDuties
                                                                                       .Where(x => x.BlockProposalSlot.HasValue)
                                                                                       .GroupBy(x => x.BlockProposalSlot !.Value)
                                                                                       .ToDictionary(x => x.Key, x => x);

            groupsByProposalSlot[new Slot(8)].Count().ShouldBe(1);
            groupsByProposalSlot[new Slot(9)].Count().ShouldBe(1);
            groupsByProposalSlot[new Slot(10)].Count().ShouldBe(1);
            groupsByProposalSlot[new Slot(11)].Count().ShouldBe(1);
            groupsByProposalSlot[new Slot(12)].Count().ShouldBe(1);
            groupsByProposalSlot[new Slot(13)].Count().ShouldBe(1);
            groupsByProposalSlot[new Slot(14)].Count().ShouldBe(1);
            //groupsByProposalSlot[new Slot(15)].Count().ShouldBe(1);
            validatorDuties.Count(x => !x.BlockProposalSlot.HasValue).ShouldBe(numberOfValidators - 7);
        }
Ejemplo n.º 17
0
 public BeaconNodeOApiAdapter(ILogger <BeaconNodeOApiAdapter> logger,
                              IBeaconNodeApi beaconNode)
 {
     _logger     = logger;
     _beaconNode = beaconNode;
 }
Ejemplo n.º 18
0
 public ValidatorBlockController(ILogger <ValidatorBlockController> logger, IBeaconNodeApi beaconNode)
 {
     _logger     = logger;
     _beaconNode = beaconNode;
 }
Ejemplo n.º 19
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);
        }
Ejemplo n.º 20
0
 public NodeGenesisTimeController(ILogger <NodeGenesisTimeController> logger, IBeaconNodeApi beaconNode)
 {
     _logger     = logger;
     _beaconNode = beaconNode;
 }
Ejemplo n.º 21
0
 public ValidatorAttestationController(ILogger <ValidatorAttestationController> logger, IBeaconNodeApi beaconNode)
 {
     _logger     = logger;
     _beaconNode = beaconNode;
 }
Ejemplo n.º 22
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);
        }