Exemplo n.º 1
0
        public async Task OnTickAsync(BeaconChain beaconChain, ulong time, CancellationToken cancellationToken)
        {
            // update time
            Slot previousSlot = GetCurrentSlot(beaconChain);
            await beaconChain.SetTimeAsync(time).ConfigureAwait(false);

            Slot currentSlot = GetCurrentSlot(beaconChain);

            // TODO: attestation is done 1/3 way through slot

            // Not a new slot, return
            bool isNewSlot = currentSlot > previousSlot;

            if (!isNewSlot)
            {
                return;
            }

            await UpdateForkVersion(cancellationToken).ConfigureAwait(false);

            Epoch currentEpoch = ComputeEpochAtSlot(currentSlot);

            await UpdateDutiesAsync(currentEpoch, cancellationToken).ConfigureAwait(false);

            await ProcessProposalDutiesAsync(currentSlot, cancellationToken).ConfigureAwait(false);

            // If upcoming attester, join (or change) topics
            // Subscribe to topics

            // Attest 1/3 way through slot
        }
Exemplo n.º 2
0
 public BeaconNodeApiAdapter(ILogger <BeaconNodeApiAdapter> logger,
                             BeaconChain beaconChain,
                             BeaconNodeConfiguration beaconNodeConfiguration,
                             BlockProducer blockProducer)
 {
     _logger                  = logger;
     _beaconChain             = beaconChain;
     _beaconNodeConfiguration = beaconNodeConfiguration;
     _blockProducer           = blockProducer;
 }
Exemplo n.º 3
0
        public async Task GenesisWithEmptyParametersTimeShouldReject()
        {
            // Arrange
            var testServiceProvider = TestSystem.BuildTestServiceProvider();

            var chainConstants = testServiceProvider.GetService <ChainConstants>();
            var miscellaneousParameterOptions = testServiceProvider.GetService <IOptionsMonitor <MiscellaneousParameters> >();
            var gweiValueOptions             = testServiceProvider.GetService <IOptionsMonitor <GweiValues> >();
            var initialValueOptions          = testServiceProvider.GetService <IOptionsMonitor <InitialValues> >();
            var timeParameterOptions         = testServiceProvider.GetService <IOptionsMonitor <TimeParameters> >();
            var stateListLengthOptions       = testServiceProvider.GetService <IOptionsMonitor <StateListLengths> >();
            var rewardsAndPenaltiesOptions   = testServiceProvider.GetService <IOptionsMonitor <RewardsAndPenalties> >();
            var maxOperationsPerBlockOptions = testServiceProvider.GetService <IOptionsMonitor <MaxOperationsPerBlock> >();
            var signatureDomainOptions       = testServiceProvider.GetService <IOptionsMonitor <SignatureDomains> >();

            miscellaneousParameterOptions.CurrentValue.MinimumGenesisActiveValidatorCount = 2;

            var loggerFactory = new LoggerFactory(new[] {
                new ConsoleLoggerProvider(TestOptionsMonitor.Create(new ConsoleLoggerOptions()))
            });

            var cryptographyService = new CryptographyService();
            var beaconChainUtility  = new BeaconChainUtility(loggerFactory.CreateLogger <BeaconChainUtility>(),
                                                             miscellaneousParameterOptions, gweiValueOptions, timeParameterOptions,
                                                             cryptographyService);
            var beaconStateAccessor = new BeaconStateAccessor(miscellaneousParameterOptions, initialValueOptions, timeParameterOptions, stateListLengthOptions, signatureDomainOptions,
                                                              cryptographyService, beaconChainUtility);
            var beaconStateMutator = new BeaconStateMutator(chainConstants, timeParameterOptions, stateListLengthOptions, rewardsAndPenaltiesOptions,
                                                            beaconChainUtility, beaconStateAccessor);
            var beaconStateTransition = new BeaconStateTransition(loggerFactory.CreateLogger <BeaconStateTransition>(),
                                                                  chainConstants, miscellaneousParameterOptions, gweiValueOptions, initialValueOptions, timeParameterOptions, stateListLengthOptions, rewardsAndPenaltiesOptions, maxOperationsPerBlockOptions, signatureDomainOptions,
                                                                  cryptographyService, beaconChainUtility, beaconStateAccessor, beaconStateMutator);
            var beaconChain = new BeaconChain(loggerFactory.CreateLogger <BeaconChain>(),
                                              chainConstants, miscellaneousParameterOptions,
                                              gweiValueOptions, initialValueOptions, timeParameterOptions, stateListLengthOptions, maxOperationsPerBlockOptions,
                                              cryptographyService, beaconChainUtility, beaconStateAccessor, beaconStateMutator, beaconStateTransition);

            // Act
            var eth1BlockHash = Hash32.Zero;
            var eth1Timestamp = (ulong)106185600; // 1973-05-14
            var deposits      = Array.Empty <Deposit>();
            var success       = await beaconChain.TryGenesisAsync(eth1BlockHash, eth1Timestamp, deposits);

            // Assert
            success.ShouldBeFalse();
            beaconChain.State.ShouldBeNull();
        }
Exemplo n.º 4
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;
 }
Exemplo n.º 5
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();
        }
Exemplo n.º 6
0
        public Slot GetCurrentSlot(BeaconChain beaconChain)
        {
            ulong slotValue = (beaconChain.Time - beaconChain.GenesisTime) / _timeParameterOptions.CurrentValue.SecondsPerSlot;

            return(new Slot(slotValue));
        }
Exemplo n.º 7
0
        public void Part1(string[] lines)
        {
            var scanners     = ParseInput(lines);
            var beaconChains = new List <BeaconChain>();

            for (int i = 0; i < scanners.Count; i++)
            {
                for (int j = i + 1; j < scanners.Count; j++)
                {
                    scanners[i].CheckForAssociatedScanners(scanners[j]);
                }
            }

            beaconChains = new List <BeaconChain>();
            var uniqueBecons    = new HashSet <ScannerBeacon>();
            var connectedBecons = new HashSet <ScannerBeacon>();

            for (int i = 0; i < scanners.Count; i++)
            {
                foreach (var beacon in scanners[i].Beacons)
                {
                    if (beacon.ConnectedBeacons.Count == 0)
                    {
                        uniqueBecons.Add(beacon);
                        continue;
                    }

                    var chains = beaconChains.Where(x => x.Becons.Contains(beacon)).ToList();
                    if (chains.Any())
                    {
                        foreach (var oldChain in chains)
                        {
                            foreach (var connectedBeacon in oldChain.Becons.Where(x => x != beacon))
                            {
                                connectedBeacon.ConnectedBeacons.Add(beacon);
                                beacon.ConnectedBeacons.Add(connectedBeacon);
                            }

                            beaconChains.Remove(oldChain);
                        }
                    }

                    var chain = new BeaconChain();
                    chain.AddBeacon(beacon);
                    foreach (var connectedBeacon in beacon.ConnectedBeacons)
                    {
                        chain.AddBeacon(connectedBeacon);
                    }
                    beaconChains.Add(chain);
                    connectedBecons.Add(beacon);
                }
            }

            foreach (var beacon in connectedBecons)
            {
                var chains = beaconChains.Where(x => x.Becons.Contains(beacon)).ToList();
                if (chains.Count > 1)
                {
                    throw new Exception("Becon in multiple chains.");
                }
                else if (chains.Count == 0)
                {
                    throw new Exception("Becon missing from chain.");
                }

                if (uniqueBecons.Any(x => x.ConnectedBeacons.Any()))
                {
                    throw new Exception("Unque beacon has connection.");
                }
            }

            var sum = uniqueBecons.Count + beaconChains.Count;

            Console.WriteLine($"There are a total of {uniqueBecons.Count} unique becons and {beaconChains.Count} becons that are shared. Total becons is: {sum}");
        }