Пример #1
0
        public void EpochTest_Equality_SameObject()
        {
            Epoch e1 = new Epoch(5, 3);

            this.EqualsTestHelper(e1, e1);
        }
Пример #2
0
 public static void Ize(out UInt256 root, Epoch container)
 {
     Ize(out root, container.Number);
 }
Пример #3
0
 public static DateTime?FromUnixTime(string unixTime)
 {
     return(long.TryParse(unixTime, out long seconds) ? Epoch.AddSeconds(seconds) : default(DateTime?));
 }
Пример #4
0
        public void EpochTest_NullReturnsFalse()
        {
            Epoch e1 = new Epoch(5, 3);

            Assert.IsFalse(e1.Equals(null));
        }
        private static AttestationData BuildAttestationData(IServiceProvider testServiceProvider, BeaconState state, Slot slot, CommitteeIndex index)
        {
            BeaconChainUtility  beaconChainUtility  = testServiceProvider.GetService <BeaconChainUtility>();
            BeaconStateAccessor beaconStateAccessor = testServiceProvider.GetService <BeaconStateAccessor>();

            if (state.Slot > slot)
            {
                throw new ArgumentOutOfRangeException(nameof(slot), slot, $"Slot cannot be greater than state slot {state.Slot}.");
            }

            Root blockRoot;

            if (slot == state.Slot)
            {
                BeaconBlock nextBlock = TestBlock.BuildEmptyBlockForNextSlot(testServiceProvider, state, BlsSignature.Zero);
                blockRoot = nextBlock.ParentRoot;
            }
            else
            {
                blockRoot = beaconStateAccessor.GetBlockRootAtSlot(state, slot);
            }

            Root  epochBoundaryRoot;
            Epoch currentEpoch          = beaconStateAccessor.GetCurrentEpoch(state);
            Slot  currentEpochStartSlot = beaconChainUtility.ComputeStartSlotOfEpoch(currentEpoch);

            if (slot < currentEpochStartSlot)
            {
                Epoch previousEpoch = beaconStateAccessor.GetPreviousEpoch(state);
                epochBoundaryRoot = beaconStateAccessor.GetBlockRoot(state, previousEpoch);
            }
            else if (slot == currentEpochStartSlot)
            {
                epochBoundaryRoot = blockRoot;
            }
            else
            {
                epochBoundaryRoot = beaconStateAccessor.GetBlockRoot(state, currentEpoch);
            }

            Epoch sourceEpoch;
            Root  sourceRoot;

            if (slot < currentEpochStartSlot)
            {
                sourceEpoch = state.PreviousJustifiedCheckpoint.Epoch;
                sourceRoot  = state.PreviousJustifiedCheckpoint.Root;
            }
            else
            {
                sourceEpoch = state.CurrentJustifiedCheckpoint.Epoch;
                sourceRoot  = state.CurrentJustifiedCheckpoint.Root;
            }

            //Crosslink parentCrosslink;
            //if (epochOfSlot == currentEpoch)
            //{
            //    parentCrosslink = state.CurrentCrosslinks[(int)(ulong)shard];
            //}
            //else
            //{
            //    throw new NotImplementedException();
            //}

            Epoch           slotEpoch       = beaconChainUtility.ComputeEpochAtSlot(slot);
            AttestationData attestationData = new AttestationData(
                slot,
                index,
                blockRoot,
                new Checkpoint(sourceEpoch, sourceRoot),
                new Checkpoint(slotEpoch, epochBoundaryRoot));

            return(attestationData);
        }
Пример #6
0
        protected static DateTimeOffset TruncateSubSeconds(DateTimeOffset dto)
        {
            int seconds = dto.ToSecondsSinceEpoch();

            return(Epoch.ToDateTimeOffset(seconds, (int)dto.Offset.TotalMinutes));
        }
        //    Run ``process_attester_slashing``, yielding:
        //  - pre-state('pre')
        //  - attester_slashing('attester_slashing')
        //  - post-state('post').
        //If ``valid == False``, run expecting ``AssertionError``
        private void RunAttesterSlashingProcessing(IServiceProvider testServiceProvider, BeaconState state, AttesterSlashing attesterSlashing, bool expectValid)
        {
            var chainConstants      = testServiceProvider.GetService <ChainConstants>();
            var stateListLengths    = testServiceProvider.GetService <IOptions <StateListLengths> >().Value;
            var rewardsAndPenalties = testServiceProvider.GetService <IOptions <RewardsAndPenalties> >().Value;

            var beaconStateAccessor   = testServiceProvider.GetService <BeaconStateAccessor>();
            var beaconStateTransition = testServiceProvider.GetService <BeaconStateTransition>();

            if (!expectValid)
            {
                Should.Throw <Exception>(() =>
                {
                    beaconStateTransition.ProcessAttesterSlashing(state, attesterSlashing);
                });
                return;
            }

            var slashedIndices = attesterSlashing.Attestation1.CustodyBit0Indices
                                 .Union(attesterSlashing.Attestation1.CustodyBit1Indices)
                                 .ToList();

            var proposerIndex         = beaconStateAccessor.GetBeaconProposerIndex(state);
            var preProposerBalance    = TestState.GetBalance(state, proposerIndex);
            var preSlashings          = slashedIndices.ToDictionary(x => x, x => TestState.GetBalance(state, x));
            var preWithdrawableEpochs = slashedIndices.ToDictionary(x => x, x => state.Validators[(int)(ulong)x].WithdrawableEpoch);

            var totalProposerRewards = preSlashings.Values.Aggregate(Gwei.Zero, (sum, x) => sum + x / rewardsAndPenalties.WhistleblowerRewardQuotient);

            // Process slashing
            beaconStateTransition.ProcessAttesterSlashing(state, attesterSlashing);

            foreach (var slashedIndex in slashedIndices)
            {
                Console.WriteLine($"Checking index {slashedIndex}");
                var preWithdrawableEpoch = preWithdrawableEpochs[slashedIndex];
                var slashedValidator     = state.Validators[(int)(ulong)slashedIndex];

                // Check Slashing
                slashedValidator.IsSlashed.ShouldBeTrue();
                slashedValidator.ExitEpoch.ShouldBeLessThan(chainConstants.FarFutureEpoch);
                if (preWithdrawableEpoch < chainConstants.FarFutureEpoch)
                {
                    var slashingsEpoch            = beaconStateAccessor.GetCurrentEpoch(state) + stateListLengths.EpochsPerSlashingsVector;
                    var expectedWithdrawableEpoch = Epoch.Max(preWithdrawableEpoch, slashingsEpoch);
                    slashedValidator.WithdrawableEpoch.ShouldBe(expectedWithdrawableEpoch);
                }
                else
                {
                    slashedValidator.WithdrawableEpoch.ShouldBeLessThan(chainConstants.FarFutureEpoch);
                }

                var preSlashing    = preSlashings[slashedIndex];
                var slashedBalance = TestState.GetBalance(state, slashedIndex);
                slashedBalance.ShouldBeLessThan(preSlashing);
            }

            if (!slashedIndices.Contains(proposerIndex))
            {
                // gained whistleblower reward
                var expectedProposerBalance = preProposerBalance + totalProposerRewards;
                var proposerBalance         = TestState.GetBalance(state, proposerIndex);
                proposerBalance.ShouldBe(expectedProposerBalance);
            }
            else
            {
                // gained rewards for all slashings, which may include others. And only lost that of themselves.
                var expectedProposerBalance = preProposerBalance + totalProposerRewards
                                              - (preSlashings[proposerIndex] / rewardsAndPenalties.MinimumSlashingPenaltyQuotient);
                var proposerBalance = TestState.GetBalance(state, proposerIndex);
                proposerBalance.ShouldBe(expectedProposerBalance);
            }
        }
Пример #8
0
 public DateTime ToDateTime()
 {
     return(Epoch.AddSeconds(_seconds));
 }
Пример #9
0
        /// <summary>
        /// Run ``on_attestation`` upon receiving a new ``attestation`` from either within a block or directly on the wire.
        /// An ``attestation`` that is asserted as invalid may be valid at a later time,
        /// consider scheduling it for later processing in such case.
        /// </summary>
        public async Task OnAttestationAsync(IStore store, Attestation attestation)
        {
            if (_logger.IsInfo())
            {
                Log.OnAttestation(_logger, attestation, null);
            }

            InitialValues  initialValues  = _initialValueOptions.CurrentValue;
            TimeParameters timeParameters = _timeParameterOptions.CurrentValue;

            Checkpoint target = attestation.Data.Target;

            // Attestations must be from the current or previous epoch
            Slot  currentSlot  = GetCurrentSlot(store);
            Epoch currentEpoch = _beaconChainUtility.ComputeEpochAtSlot(currentSlot);

            // Use GENESIS_EPOCH for previous when genesis to avoid underflow
            Epoch previousEpoch = currentEpoch > initialValues.GenesisEpoch
                ? currentEpoch - new Epoch(1)
                : initialValues.GenesisEpoch;

            if (target.Epoch != currentEpoch && target.Epoch != previousEpoch)
            {
                throw new ArgumentOutOfRangeException("attestation.Target.Epoch", target.Epoch, $"Attestation target epoch must be either the current epoch {currentEpoch} or previous epoch {previousEpoch}.");
            }
            // Cannot calculate the current shuffling if have not seen the target
            BeaconBlock targetBlock = await store.GetBlockAsync(target.Root).ConfigureAwait(false);

            // Attestations target be for a known block. If target block is unknown, delay consideration until the block is found
            BeaconState targetStoredState = await store.GetBlockStateAsync(target.Root).ConfigureAwait(false);

            // Attestations cannot be from future epochs. If they are, delay consideration until the epoch arrives
            BeaconState baseState                = BeaconState.Clone(targetStoredState);
            Slot        targetEpochStartSlot     = _beaconChainUtility.ComputeStartSlotOfEpoch(target.Epoch);
            ulong       targetEpochStartSlotTime = baseState.GenesisTime + (ulong)targetEpochStartSlot * timeParameters.SecondsPerSlot;

            if (store.Time < targetEpochStartSlotTime)
            {
                throw new Exception($"Ättestation target state time {targetEpochStartSlotTime} should not be larger than the store time {store.Time}).");
            }

            // Attestations must be for a known block. If block is unknown, delay consideration until the block is found
            BeaconBlock attestationBlock = await store.GetBlockAsync(attestation.Data.BeaconBlockRoot).ConfigureAwait(false);

            // Attestations must not be for blocks in the future. If not, the attestation should not be considered
            if (attestationBlock.Slot > attestation.Data.Slot)
            {
                throw new Exception($"Attestation data root slot {attestationBlock.Slot} should not be larger than the attestation data slot {attestation.Data.Slot}).");
            }

            // Store target checkpoint state if not yet seen

            BeaconState?targetState = await store.GetCheckpointStateAsync(target, false).ConfigureAwait(false);

            if (targetState == null)
            {
                _beaconStateTransition.ProcessSlots(baseState, targetEpochStartSlot);
                await store.SetCheckpointStateAsync(target, baseState).ConfigureAwait(false);

                targetState = baseState;
            }

            // Attestations can only affect the fork choice of subsequent slots.
            // Delay consideration in the fork choice until their slot is in the past.
            ulong attestationDataSlotTime = targetState !.GenesisTime + ((ulong)attestation.Data.Slot + 1) * timeParameters.SecondsPerSlot;

            if (store.Time < attestationDataSlotTime)
            {
                throw new Exception($"Attestation data time {attestationDataSlotTime} should not be larger than the store time {store.Time}).");
            }

            // Get state at the `target` to validate attestation and calculate the committees
            IndexedAttestation indexedAttestation = _beaconStateAccessor.GetIndexedAttestation(targetState, attestation);
            Domain             domain             = _beaconStateAccessor.GetDomain(targetState, _signatureDomainOptions.CurrentValue.BeaconAttester, indexedAttestation.Data.Target.Epoch);
            bool isValid = _beaconChainUtility.IsValidIndexedAttestation(targetState, indexedAttestation, domain);

            if (!isValid)
            {
                throw new Exception($"Indexed attestation {indexedAttestation} is not valid.");
            }

            // Update latest messages
            IEnumerable <ValidatorIndex> attestingIndices = _beaconStateAccessor.GetAttestingIndices(targetState, attestation.Data, attestation.AggregationBits);

            foreach (ValidatorIndex index in attestingIndices)
            {
                LatestMessage?latestMessage = await store.GetLatestMessageAsync(index, false).ConfigureAwait(false);

                if (latestMessage == null || target.Epoch > latestMessage !.Epoch)
                {
                    latestMessage = new LatestMessage(target.Epoch, attestation.Data.BeaconBlockRoot);
                    await store.SetLatestMessageAsync(index, latestMessage).ConfigureAwait(false);
                }
            }
        }
Пример #10
0
 /// <summary>
 /// Converts <see cref="BroTime"/> to a .NET <see cref="DateTime"/>.
 /// </summary>
 /// <returns>This <see cref="BroTime"/> as a .NET <see cref="DateTime"/>.</returns>
 public DateTime ToDateTime()
 {
     return(Epoch.AddSeconds(m_value));
 }
Пример #11
0
 public static string ToDisplayString(this Epoch epoch)
 {
     return($"{epoch.DataLossNumber}.{epoch.ConfigurationNumber}");
 }
Пример #12
0
    // Generation End
    private void GenerationEnd()
    {
        // Update Values
        UpdateChildrenCount();
        UpdateBreedingAmount();

        _Generation++;

        // Order all epochs based on score
        _Epoch_All = ReorderCurrentEpochsByScore();

        // Take Best Epoch
        if (_Epoch_All[_MaxEpochs - 1].GetScore() > _Best.GetScore())
        {
            _Best.CopyData(_Epoch_All[_MaxEpochs - 1]);
            _Best.SetScore(_Epoch_All[_MaxEpochs - 1].GetScore());
        }

        // New Children
        Epoch[] NewChildren = new Epoch[NewChildrenCount];

        // Create Children Values
        for (int i = 0; i < NewChildrenCount; i++)
        {
            // Acquire Random Amount of Epochs
            List <Epoch> Randos = AcquireRandomEpochs(BreedingAmount);
            // Find best 2 epochs
            Epoch Best_1 = FindBestEpochByScore(Randos);
            Epoch Best_2 = FindBestEpochByScore(Randos, Best_1);
            // Child Value
            NewChildren[i] = Epoch.CrossOver(Best_1, Best_2);
            //  Debug.Log("Best1 Bias: " + Best_1.GetOutputLayers()[0].GetBias());
            //  Debug.Log("Best2 Bias: " + Best_2.GetOutputLayers()[0].GetBias());
            //  Debug.Log("Child Bias: " + NewChildren[i].GetOutputLayers()[0].GetBias());
            //  Debug.Log("~~~");

            // Chance that child mutates
            if (CalculateMutationChance())
            {
                NewChildren[i].MutateValues(GameplayManager.GetInstance().MutationAmount);
            }
        }

        // Replace weakest ones with children
        for (int i = 0; i < NewChildrenCount; i++)
        {
            _Epoch_All[i].CopyData(NewChildren[i]);
        }

        // Add best epoch back in the list
        _Epoch_All[NewChildrenCount].CopyData(_Best);

        // Weakest one right after children getss jittered a lot for fun
        if (GameplayManager.GetInstance().MutateOneWeakestByALot)
        {
            _Epoch_All[NewChildrenCount].MutateValues(GameplayManager.GetInstance().MutationAmount * 2.0f);
        }

        // Reset all scores
        ResetScores();

        // Start Next Epoch
        NextEpoch();


        //  // Find best 2 Epochs
        //  Epoch Winner_1st = FindBestEpochByScore();
        //  Epoch Winner_2nd = FindBestEpochByScore(Winner_1st);
        //  Epoch Child = Epoch.CrossOver(Winner_1st, Winner_2nd);
        //
        //  // Error Check
        //  if(Child == null)
        //  {
        //      Debug.LogError("CrossoverChild Error");
        //      NextEpoch();
        //      return;
        //  }
        //
        //  // Set first two epochs as winner and child
        //  _Epoch_All[0] = Winner_1st;
        //  _Epoch_All[1] = Child;
        //
        //  // Rest of epochs are just clones of child with jitter
        //  for (int i = 2; i < _MaxEpochs; i++)
        //  {
        //      _Epoch_All[i].CopyLayers(Child);
        //      _Epoch_All[i].JitterLayers(GameplayManager.GetInstance().MutationAmount);
        //  }
    }
Пример #13
0
 /// <summary>
 /// Initializes a new instance of the <cref name="BackupVersion"/>
 /// </summary>
 /// <param name="epoch">The <cref name="Epoch"/> at which the backup was taken.</param>
 /// <param name="lsn">The last committed logical sequence number included in the backup.</param>
 public BackupVersion(Epoch epoch, long lsn)
 {
     this._epoch = epoch;
     this._lsn   = lsn;
 }
        public async Task UpdateEpochAsync(
            Epoch epoch,
            long previousEpochLastSequenceNumber,
            CancellationToken cancellationToken)
        {
            var update = new ProgressIndicator(epoch, previousEpochLastSequenceNumber);
            this.logger.Log($"UpdateEpochAsync: {update}");

            this.progressVector.Update(update);
            await this.PersistEpochUpdate(update);

            this.logger.Log($"ProgressVector: {this.progressVector}");
        }
    private void CharacterLoad_Mail(Player player)
    {
#if _SERVER
#if _MYSQL
        var table = ExecuteReaderMySql("SELECT * FROM mail WHERE messageTo=@character AND deleted=0 AND expires > @expires ORDER BY sent", new MySqlParameter("@character", player.name), new MySqlParameter("@expires", Epoch.Current()));
#elif _SQLITE
        var table = connection.Query <mail>("SELECT * FROM mail WHERE messageTo=? AND deleted=0 AND expires > " + Epoch.Current() + " ORDER BY sent", player.name);
#endif
        foreach (var row in table)
        {
            MailMessage message = Mail_BuildMessageFromDBRow(row);
            player.mailMessages.Add(message);
        }
#endif
    }
Пример #16
0
 public void SetSlashings(Epoch slashingsIndex, Gwei amount) => _slashings[slashingsIndex] += amount;
    // -----------------------------------------------------------------------------------
    //
    // -----------------------------------------------------------------------------------
    public List <MailMessage> Mail_CheckForNewMessages(long maxID)
    {
        List <MailMessage> result = new List <MailMessage>();

#if _SERVER
#if _MYSQL
        var table = ExecuteReaderMySql("SELECT * FROM mail WHERE id > @maxid AND deleted=0 AND expires > @expires ORDER BY sent", new MySqlParameter("@maxid", maxID), new MySqlParameter("@expires", Epoch.Current()));
#elif _SQLITE
        var table = connection.Query <mail>("SELECT * FROM mail WHERE id > " + maxID + " AND deleted=0 AND expires > " + Epoch.Current() + " ORDER BY sent");
#endif
        foreach (var row in table)
        {
            MailMessage message = Mail_BuildMessageFromDBRow(row);
            result.Add(message);
        }
#endif
        return(result);
    }
Пример #18
0
        /// <summary>
        /// Return the block root at the start of a recent ``epoch``.
        /// </summary>
        public Hash32 GetBlockRoot(BeaconState state, Epoch epoch)
        {
            Slot startSlot = _beaconChainUtility.ComputeStartSlotOfEpoch(epoch);

            return(GetBlockRootAtSlot(state, startSlot));
        }
Пример #19
0
        public void RenderedStimulus(
            [Values(10000, 20000, 50000)] double sampleRate,
            [Values(2)] int nEpochs
            )
        {
            Logging.ConfigureConsole();

            Converters.Clear();
            Converters.Register("V", "V",
                                // just an identity conversion for now, to pass Validate()
                                (IMeasurement m) => m);
            HekaDAQInputStream.RegisterConverters();
            HekaDAQOutputStream.RegisterConverters();

            Assert.That(HekaDAQController.AvailableControllers().Count(), Is.GreaterThan(0));
            foreach (var daq in HekaDAQController.AvailableControllers())
            {
                const double epochDuration = 5; //s

                daq.InitHardware();
                try
                {
                    daq.SampleRate = new Measurement((decimal)sampleRate, "Hz");

                    var controller = new Controller {
                        Clock = daq, DAQController = daq
                    };

                    var dev0 = new UnitConvertingExternalDevice("Device0", "Manufacturer", controller, new Measurement(0, "V"))
                    {
                        MeasurementConversionTarget = "V",
                        Clock = daq
                    };
                    dev0.BindStream((IDAQOutputStream)daq.GetStreams("ANALOG_OUT.0").First());
                    dev0.BindStream((IDAQInputStream)daq.GetStreams("ANALOG_IN.0").First());

                    var dev1 = new UnitConvertingExternalDevice("Device1", "Manufacturer", controller, new Measurement(0, "V"))
                    {
                        MeasurementConversionTarget = "V",
                        Clock = daq
                    };
                    dev1.BindStream((IDAQOutputStream)daq.GetStreams("ANALOG_OUT.1").First());
                    dev1.BindStream((IDAQInputStream)daq.GetStreams("ANALOG_IN.1").First());

                    for (int j = 0; j < nEpochs; j++)
                    {
                        // Setup Epoch
                        var e = new Epoch("HekaIntegration");

                        var nSamples = (int)TimeSpan.FromSeconds(epochDuration).Samples(daq.SampleRate);
                        IList <IMeasurement> stimData = (IList <IMeasurement>)Enumerable.Range(0, nSamples)
                                                        .Select(i => new Measurement((decimal)(8 * Math.Sin(((double)i) / (nSamples / 10.0))), "V") as IMeasurement)
                                                        .ToList();

                        e.Stimuli[dev0] = new RenderedStimulus((string)"RenderedStimulus", (IDictionary <string, object>) new Dictionary <string, object>(),
                                                               (IOutputData) new OutputData(stimData, daq.SampleRate));
                        e.Responses[dev0]  = new Response();
                        e.Background[dev0] = new Epoch.EpochBackground(new Measurement(0, "V"), daq.SampleRate);

                        e.Stimuli[dev1] = new RenderedStimulus((string)"RenderedStimulus", (IDictionary <string, object>) new Dictionary <string, object>(),
                                                               (IOutputData) new OutputData(Enumerable.Range(0, nSamples)
                                                                                            .Select(i => new Measurement((decimal)(8 * Math.Sin(((double)i) / (nSamples / 10.0))), "V"))
                                                                                            .ToList(),
                                                                                            daq.SampleRate));
                        e.Responses[dev1]  = new Response();
                        e.Background[dev1] = new Epoch.EpochBackground(new Measurement(0, "V"), daq.SampleRate);


                        //Run single epoch
                        var fakeEpochPersistor = new FakeEpochPersistor();

                        controller.RunEpoch(e, fakeEpochPersistor);



                        Assert.That((bool)e.StartTime, Is.True);
                        Assert.That((DateTimeOffset)e.StartTime, Is.LessThanOrEqualTo(controller.Clock.Now));
                        Assert.That(e.Responses[dev0].Duration, Is.EqualTo(((TimeSpan)e.Duration))
                                    .Within(TimeSpanExtensions.FromSamples(1,
                                                                           daq.
                                                                           SampleRate)));
                        Assert.That(e.Responses[dev1].Duration, Is.EqualTo(((TimeSpan)e.Duration))
                                    .Within(TimeSpanExtensions.FromSamples(1,
                                                                           daq.
                                                                           SampleRate)));
                        Assert.That(fakeEpochPersistor.PersistedEpochs, Contains.Item(e));

                        var failures0 =
                            e.Responses[dev0].Data.Select(
                                (t, i) => new { index = i, diff = t.QuantityInBaseUnit - stimData[i].QuantityInBaseUnit })
                            .Where(dif => Math.Abs(dif.diff) > (decimal)MAX_VOLTAGE_DIFF);

                        foreach (var failure in failures0.Take(10))
                        {
                            Console.WriteLine("{0}: {1}", failure.index, failure.diff);
                        }


                        /*
                         * According to Telly @ Heka, a patch cable may introduce 3-4 offset points
                         */
                        Assert.That(failures0.Count(), Is.LessThanOrEqualTo(4));


                        /*
                         * //Since we only have one patch cable on the test rig,
                         * //we're not checking second device response values
                         *
                         * var failures1 =
                         *  e.Responses[dev1].Data.Data.Select(
                         *      (t, i) => new { index = i, diff = t.QuantityInBaseUnit - stimData[i].QuantityInBaseUnit })
                         *      .Where(dif => Math.Abs(dif.diff) > MAX_VOLTAGE_DIFF);
                         *
                         * foreach (var failure in failures1.Take(10))
                         *  Console.WriteLine("{0}: {1}", failure.index, failure.diff);
                         *
                         * Assert.That(failures1.Count(), Is.EqualTo(0));
                         */
                    }
                }
                finally
                {
                    if (daq.HardwareReady)
                    {
                        daq.CloseHardware();
                    }
                }
            }
        }
Пример #20
0
 /// <summary>
 /// Reads the value as a <see cref="DateTime"/>
 /// </summary>
 /// <returns></returns>
 public DateTime ReadDateTime() => Epoch.AddSeconds(ReadValue());
Пример #21
0
        public void MaxBandwidth(
            [Values(20000)] decimal sampleRate,
            [Values(1)] int nEpochs,
            [Values(4)] int nOut,
            [Values(8)] int nIn
            )
        {
            Logging.ConfigureConsole();

            Converters.Clear();
            HekaDAQInputStream.RegisterConverters();
            HekaDAQOutputStream.RegisterConverters();

            Assert.That(HekaDAQController.AvailableControllers().Count(), Is.GreaterThan(0));

            foreach (var daq in HekaDAQController.AvailableControllers())
            {
                const double epochDuration = 10; //s

                daq.InitHardware();
                try
                {
                    daq.SampleRate = new Measurement(sampleRate, "Hz");

                    var controller = new Controller {
                        Clock = daq, DAQController = daq
                    };

                    HekaDAQController daq1 = daq;
                    var outDevices         = Enumerable.Range(0, nOut)
                                             .Select(i =>
                    {
                        var dev0 = new UnitConvertingExternalDevice("Device_OUT_" + i, "Manufacturer", controller,
                                                                    new Measurement(0, "V"))
                        {
                            MeasurementConversionTarget = "V",
                            Clock = daq1
                        };
                        dev0.BindStream((IDAQOutputStream)daq1.GetStreams("ANALOG_OUT." + i).First());

                        return(dev0);
                    })
                                             .ToList();


                    var inDevices = Enumerable.Range(0, nIn)
                                    .Select(i =>
                    {
                        var dev0 = new UnitConvertingExternalDevice("Device_IN_" + i, "Manufacturer", controller,
                                                                    new Measurement(0, "V"))
                        {
                            MeasurementConversionTarget = "V",
                            Clock = daq1
                        };
                        dev0.BindStream((IDAQInputStream)daq1.GetStreams("ANALOG_IN." + i).First());

                        return(dev0);
                    })
                                    .ToList();

                    for (int j = 0; j < nEpochs; j++)
                    {
                        // Setup Epoch
                        var e = new Epoch("HekaIntegration");

                        var nSamples = (int)TimeSpan.FromSeconds(epochDuration).Samples(daq.SampleRate);
                        IList <IMeasurement> stimData = Enumerable.Range(0, nSamples)
                                                        .Select(i => new Measurement((decimal)(8 * Math.Sin(((double)i) / (nSamples / 10.0))), "V") as IMeasurement)
                                                        .ToList();

                        foreach (var outDev in outDevices)
                        {
                            e.Stimuli[outDev] = new RenderedStimulus((string)"RenderedStimulus", (IDictionary <string, object>) new Dictionary <string, object>(),
                                                                     (IOutputData) new OutputData(stimData, daq.SampleRate));

                            e.Background[outDev] = new Epoch.EpochBackground(new Measurement(0, "V"), daq.SampleRate);
                        }

                        foreach (var inDev in inDevices)
                        {
                            e.Responses[inDev] = new Response();
                        }


                        //Run single epoch
                        var fakeEpochPersistor = new FakeEpochPersistor();

                        controller.RunEpoch(e, fakeEpochPersistor);



                        Assert.That((bool)e.StartTime, Is.True);
                        Assert.That((DateTimeOffset)e.StartTime, Is.LessThanOrEqualTo(controller.Clock.Now));
                        Assert.That(fakeEpochPersistor.PersistedEpochs, Contains.Item(e));
                    }
                }
                finally
                {
                    if (daq.HardwareReady)
                    {
                        daq.CloseHardware();
                    }
                }
            }
        }
Пример #22
0
    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.P) || Input.GetKeyDown(KeyCode.Escape))
        {
            // Can only unpause if not saving
            if (SavingThread.GetInstance().checkThreadRunning() == false)
            {
                ToggleActive();
            }
        }

        if (_Active)
        {
            // Saving Effect
            bool CheckSaving = SavingThread.GetInstance().checkThreadRunning();
            PauseMenu.GetInstance()._LoadingText.enabled = CheckSaving;
            if (CheckSaving)
            {
                PauseMenu.GetInstance()._LoadingText.text = "Loading... (" +
                                                            SavingThread.m_Saving_Layer.ToString() + "-" +
                                                            SavingThread.m_Saving_Perceptron.ToString() + "-" +
                                                            SavingThread.m_Saving_Weight.ToString() + ")";
            }
            //

            _Parent.SetActive(true);

            Vector3 Position = _FancyBar.GetComponent <RectTransform>().position;
            Position.y = Mathf.Lerp(Position.y, getItem(_MenuIndex)._Text.GetComponent <RectTransform>().position.y, 0.3f);
            _FancyBar.GetComponent <RectTransform>().position = Position;

            if (Input.GetKeyDown(KeyCode.DownArrow) || Input.GetKeyDown(KeyCode.S))
            {
                _MenuIndex++;
                if (_MenuIndex > _Items.Length - 1)
                {
                    _MenuIndex = 0;
                }
            }
            if (Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.W))
            {
                _MenuIndex--;
                if (_MenuIndex < 0)
                {
                    _MenuIndex = _Items.Length - 1;
                }
            }

            for (int i = 0; i < _Items.Length; i++)
            {
                if (i != _MenuIndex)
                {
                    _Items[i]._Text.color = Color.Lerp(_Items[i]._Text.color, _UnselectedColor, 0.25f);
                }
            }
            _Items[_MenuIndex]._Text.color = Color.Lerp(_Items[_MenuIndex]._Text.color, _SelectedColor, 0.4f);

            // API for File Browser
            // https://github.com/gkngkc/UnityStandaloneFileBrowser
            //
            if (Input.GetKeyDown(KeyCode.Space) || Input.GetKeyDown(KeyCode.KeypadEnter) || Input.GetKeyDown(KeyCode.Return))
            {
                switch (_Items[_MenuIndex]._type)
                {
                case PauseItem.Type.RESUME:
                {
                    // Can only unpause if not saving
                    if (SavingThread.GetInstance().checkThreadRunning() == false)
                    {
                        ToggleActive();
                    }
                }
                break;

                case PauseItem.Type.SAVE_EPOCH:
                {
                    // Get Current Epoch
                    Epoch epoch = TetrisPerceptronManager.GetEpochManager().GetCurrentEpoch();
                    if (epoch == null)
                    {
                        Debug.LogError("No Current Epoch");
                        return;
                    }

                    string Directory = SimpleFileDialog.SelectFile();
                    if (Directory == "")
                    {
                        Debug.LogError("Directory Not Selected");
                        return;
                    }

                    SavingThread.GetInstance().SaveEpoch(epoch, Directory);
                }
                break;

                case PauseItem.Type.SAVE_BEST_EPOCH:
                {
                    // Get Best Epoch
                    Epoch epoch = TetrisPerceptronManager.GetEpochManager().GetBestEpochAcrossGeneration();
                    if (epoch == null)
                    {
                        Debug.LogError("No Current Epoch");
                        return;
                    }

                    string Directory = SimpleFileDialog.SelectFile();
                    if (Directory == "")
                    {
                        Debug.LogError("Directory Not Selected");
                        return;
                    }

                    SavingThread.GetInstance().SaveEpoch(epoch, Directory);
                }
                break;

                case PauseItem.Type.LOAD_EPOCH:
                {
                    // Read Data File as String
                    string Data = SimpleFileDialog.OpenFile();
                    if (Data.Length == 0)
                    {
                        return;
                    }

                    // Read File
                    Epoch Result = EpochParser.LoadFile(Data);
                    if (Result == null)
                    {
                        return;
                    }

                    // Set To Current Epoch
                    TetrisPerceptronManager.GetEpochManager().OverrideCurrentEpoch(Result);

                    // Reset Game
                    Tetris.GetInstance().ResetGame();
                }
                break;
                }
            }
        }
        else
        {
            _Parent.SetActive(false);
        }
    }
Пример #23
0
        public void ShouldSetStreamBackgroundOnStop(
            [Values(10000, 20000)] double sampleRate
            )
        {
            Converters.Clear();
            Converters.Register("V", "V",
                                // just an identity conversion for now, to pass Validate()
                                (IMeasurement m) => m);
            HekaDAQInputStream.RegisterConverters();
            HekaDAQOutputStream.RegisterConverters();

            Assert.That(HekaDAQController.AvailableControllers().Count(), Is.GreaterThan(0));

            foreach (var daq in HekaDAQController.AvailableControllers())
            {
                const double epochDuration = 1; //s
                //Configure DAQ
                daq.InitHardware();
                try
                {
                    daq.SampleRate = new Measurement((decimal)sampleRate, "Hz");

                    var controller = new Controller();
                    controller.Clock         = daq;
                    controller.DAQController = daq;


                    const decimal expectedBackgroundVoltage = -3.2m;
                    var           expectedBackground        = new Measurement(expectedBackgroundVoltage, "V");
                    var           dev0 = new UnitConvertingExternalDevice("Device0", "Manufacturer", controller, expectedBackground)
                    {
                        MeasurementConversionTarget = "V"
                    };
                    dev0.BindStream(daq.GetStreams("ANALOG_OUT.0").First() as IDAQOutputStream);
                    dev0.BindStream(daq.GetStreams("ANALOG_IN.0").First() as IDAQInputStream);
                    dev0.Clock = daq;

                    controller.DiscardedEpoch += (c, args) => Console.WriteLine("Discarded epoch: " + args.Epoch);

                    // Setup Epoch
                    var e = new Epoch("HekaIntegration");

                    var nSamples = (int)TimeSpanExtensions.Samples(TimeSpan.FromSeconds(epochDuration), daq.SampleRate);
                    IList <IMeasurement> stimData = (IList <IMeasurement>)Enumerable.Range(0, nSamples)
                                                    .Select(i => new Measurement((decimal)(8 * Math.Sin(((double)i) / (nSamples / 10.0))), "V") as IMeasurement)
                                                    .ToList();

                    var stim = new RenderedStimulus((string)"RenderedStimulus", (IDictionary <string, object>) new Dictionary <string, object>(),
                                                    (IOutputData) new OutputData(stimData, daq.SampleRate, false));
                    e.Stimuli[dev0]    = stim;
                    e.Responses[dev0]  = new Response();
                    e.Background[dev0] = new Epoch.EpochBackground(expectedBackground, daq.SampleRate);


                    //Run single epoch
                    var fakeEpochPersistor = new FakeEpochPersistor();

                    controller.RunEpoch(e, fakeEpochPersistor);

                    Thread.Sleep(TimeSpan.FromMilliseconds(100)); //allow DAC to settle


                    var actual = ((HekaDAQController)controller.DAQController).ReadStreamAsyncIO(
                        daq.GetStreams("ANALOG_IN.0").First() as IDAQInputStream);

                    //Should be within +/- 0.025 volts
                    Assert.That(actual.Data.First().QuantityInBaseUnit, Is.InRange(expectedBackground.QuantityInBaseUnit - (decimal)0.025,
                                                                                   expectedBackground.QuantityInBaseUnit + (decimal)0.025));
                }
                finally
                {
                    if (daq.HardwareReady)
                    {
                        daq.CloseHardware();
                    }
                }
            }
        }
Пример #24
0
        private long GetValidLogPosition(long logPosition, Epoch[] epochs, IPEndPoint replicaEndPoint, Guid subscriptionId)
        {
            if (epochs.Length == 0)
            {
                if (logPosition > 0)
                {
                    // slave has some data, but doesn't have any epoch
                    // for now we'll just report error and close connection
                    var msg = string.Format("Replica [{0},S:{1},{2}] has positive LogPosition {3} (0x{3:X}), but does not have epochs.",
                                            replicaEndPoint, subscriptionId,
                                            string.Join(", ", epochs.Select(x => x.AsString())), logPosition);
                    Log.Info(msg);
                    throw new Exception(msg);
                }
                return(0);
            }

            var   masterCheckpoint = _db.Config.WriterCheckpoint.Read();
            Epoch afterCommonEpoch = null;
            Epoch commonEpoch      = null;

            for (int i = 0; i < epochs.Length; ++i)
            {
                var epoch = epochs[i];
                if (_epochManager.IsCorrectEpochAt(epoch.EpochPosition, epoch.EpochNumber, epoch.EpochId))
                {
                    commonEpoch      = epoch;
                    afterCommonEpoch = i > 0 ? epochs[i - 1] : null;
                    break;
                }
            }
            if (commonEpoch == null)
            {
                Log.Error("No common epoch found for replica [{0},S{1},{2}(0x{2:X}),{3}]. Subscribing at 0. Master LogPosition: {4} (0x{4:X}), known epochs: {5}.",
                          replicaEndPoint, subscriptionId, logPosition,
                          string.Join(", ", epochs.Select(x => x.AsString())),
                          masterCheckpoint, string.Join(", ", _epochManager.GetLastEpochs(int.MaxValue).Select(x => x.AsString())));
                return(0);
            }

            // if afterCommonEpoch is present, logPosition > afterCommonEpoch.EpochPosition,
            // so safe position is definitely the start of afterCommonEpoch
            var replicaPosition = afterCommonEpoch == null ? logPosition : afterCommonEpoch.EpochPosition;

            if (commonEpoch.EpochNumber == _epochManager.LastEpochNumber)
            {
                return(Math.Min(replicaPosition, masterCheckpoint));
            }

            var nextEpoch = _epochManager.GetEpoch(commonEpoch.EpochNumber + 1, throwIfNotFound: false);

            if (nextEpoch == null)
            {
                nextEpoch = _epochManager.GetEpochWithAllEpochs(commonEpoch.EpochNumber + 1, throwIfNotFound: false);
            }
            if (nextEpoch == null)
            {
                var msg = string.Format("Replica [{0},S:{1},{2}(0x{2:X}),epochs:\n{3}]\n provided epochs which are not in "
                                        + "EpochManager (possibly too old, known epochs:\n{4}).\nMaster LogPosition: {5} (0x{5:X}). "
                                        + "We do not support this case as of now.\n"
                                        + "CommonEpoch: {6}, AfterCommonEpoch: {7}",
                                        replicaEndPoint, subscriptionId, logPosition,
                                        string.Join("\n", epochs.Select(x => x.AsString())),
                                        string.Join("\n", _epochManager.GetLastEpochs(int.MaxValue).Select(x => x.AsString())), masterCheckpoint,
                                        commonEpoch.AsString(), afterCommonEpoch == null ? "<none>" : afterCommonEpoch.AsString());
                Log.Error(msg);
                throw new Exception(msg);
            }

            return(Math.Min(replicaPosition, nextEpoch.EpochPosition));
        }
Пример #25
0
        public void SealLeak(
            [Values(10000, 20000, 50000)] double sampleRate
            )
        {
            Converters.Clear();
            Converters.Register("V", "V",
                                // just an identity conversion for now, to pass Validate()
                                (IMeasurement m) => m);
            HekaDAQInputStream.RegisterConverters();
            HekaDAQOutputStream.RegisterConverters();

            Assert.That(HekaDAQController.AvailableControllers().Count(), Is.GreaterThan(0));

            foreach (var daq in HekaDAQController.AvailableControllers())
            {
                const double epochDuration = 10; //s

                //Configure DAQ
                daq.InitHardware();
                try
                {
                    daq.SampleRate = new Measurement((decimal)sampleRate, "Hz");

                    var controller = new Controller();
                    controller.Clock         = daq;
                    controller.DAQController = daq;


                    const decimal expectedBackgroundVoltage = 3.2m;
                    var           expectedBackground        = new Measurement(expectedBackgroundVoltage, "V");
                    var           dev0 = new UnitConvertingExternalDevice("Device0", "Manufacturer", controller, expectedBackground)
                    {
                        MeasurementConversionTarget = "V",
                        Clock = daq
                    };
                    dev0.BindStream(daq.GetStreams("ANALOG_OUT.0").First() as IDAQOutputStream);
                    dev0.BindStream(daq.GetStreams("ANALOG_IN.0").First() as IDAQInputStream);

                    controller.DiscardedEpoch += (c, args) => Console.WriteLine("Discarded epoch: " + args.Epoch);

                    // Setup Epoch
                    var e = new Epoch("HekaIntegration");

                    HekaDAQController cDAQ = daq;
                    var stim = new DelegatedStimulus("TEST_ID", "V", new Dictionary <string, object>(),
                                                     (parameters, duration) =>
                                                     DataForDuration(duration, cDAQ.SampleRate),
                                                     parameters => Option <TimeSpan> .None()
                                                     );

                    e.Stimuli[dev0]    = stim;
                    e.Background[dev0] = new Epoch.EpochBackground(expectedBackground, daq.SampleRate);


                    //Run single epoch
                    var fakeEpochPersistor = new FakeEpochPersistor();

                    new TaskFactory().StartNew(() =>
                    {
                        Thread.Sleep(TimeSpan.FromSeconds(epochDuration));
                        controller.CancelEpoch();
                    },
                                               TaskCreationOptions.LongRunning
                                               );

                    controller.RunEpoch(e, fakeEpochPersistor);
                }
                finally
                {
                    if (daq.HardwareReady)
                    {
                        daq.CloseHardware();
                    }
                }
            }
        }
Пример #26
0
        public void EpochTest_InvalidTypeReturnsFalse()
        {
            Epoch e1 = new Epoch(5, 3);

            Assert.IsFalse(e1.Equals(new object()));
        }
Пример #27
0
        public void LongEpochPersistence(
            [Values(5, 60)] double epochDuration, //seconds
            [Values(2)] int nEpochs
            )
        {
            const decimal sampleRate = 10000m;

            const string h5Path = "..\\..\\..\\LongEpochPersistence.h5";

            if (File.Exists(h5Path))
            {
                File.Delete(h5Path);
            }

            Logging.ConfigureConsole();

            Converters.Clear();
            Converters.Register("V", "V",
                                // just an identity conversion for now, to pass Validate()
                                (IMeasurement m) => m);
            HekaDAQInputStream.RegisterConverters();
            HekaDAQOutputStream.RegisterConverters();

            Assert.That(HekaDAQController.AvailableControllers().Count(), Is.GreaterThan(0));

            foreach (var daq in HekaDAQController.AvailableControllers())
            {
                try
                {
                    daq.InitHardware();
                    daq.SampleRate = new Measurement(sampleRate, "Hz");

                    var controller = new Controller {
                        Clock = daq, DAQController = daq
                    };

                    var dev0 = new UnitConvertingExternalDevice("Device0", "Manufacturer", controller,
                                                                new Measurement(0, "V"))
                    {
                        MeasurementConversionTarget = "V",
                        Clock = daq
                    };
                    dev0.BindStream((IDAQOutputStream)daq.GetStreams("ANALOG_OUT.0").First());
                    dev0.BindStream((IDAQInputStream)daq.GetStreams("ANALOG_IN.0").First());


                    for (int j = 0; j < nEpochs; j++)
                    {
                        // Setup Epoch
                        var e = new Epoch("HekaIntegration");

                        var nSamples = (int)TimeSpan.FromSeconds(epochDuration).Samples(daq.SampleRate);
                        IList <IMeasurement> stimData = (IList <IMeasurement>)Enumerable.Range(0, nSamples)
                                                        .Select(
                            i =>
                            new Measurement(
                                (decimal)
                                (8 *
                                 Math.Sin(((double)i) /
                                          (nSamples / 10.0))),
                                "V") as IMeasurement)
                                                        .ToList();

                        e.Stimuli[dev0] = new RenderedStimulus((string)"RenderedStimulus", (IDictionary <string, object>) new Dictionary <string, object>(),
                                                               (IOutputData) new OutputData(stimData, daq.SampleRate));
                        e.Responses[dev0]  = new Response();
                        e.Background[dev0] = new Epoch.EpochBackground(new Measurement(0, "V"), daq.SampleRate);



                        //Run single epoch
                        using (var persistor = new EpochHDF5Persistor(h5Path, null, 9))
                        {
                            persistor.BeginEpochGroup("label", "source", new string[0], new Dictionary <string, object>(),
                                                      Guid.NewGuid(), DateTimeOffset.Now);

                            controller.RunEpoch(e, persistor);

                            persistor.EndEpochGroup();
                        }


                        Assert.That((bool)e.StartTime, Is.True);
                        Assert.That((DateTimeOffset)e.StartTime, Is.LessThanOrEqualTo(controller.Clock.Now));
                        Assert.That(e.Responses[dev0].Duration, Is.EqualTo(((TimeSpan)e.Duration))
                                    .Within(TimeSpanExtensions.FromSamples(1,
                                                                           daq.
                                                                           SampleRate)));
                        //Assert.That(e.Responses[dev1].Duration, Is.EqualTo(((TimeSpan) e.Duration))
                        //                                            .Within(TimeSpanExtensions.FromSamples(1,
                        //                                                                                   daq.
                        //                                                                                       SampleRate)));
                    }
                }
                finally
                {
                    if (File.Exists(h5Path))
                    {
                        File.Delete(h5Path);
                    }

                    if (daq.HardwareReady)
                    {
                        daq.CloseHardware();
                    }
                }
            }
        }
Пример #28
0
 internal static string ToString(Epoch epoch)
 {
     return(string.Format("{0}:{1:x}", epoch.DataLossNumber, epoch.ConfigurationNumber));
 }
Пример #29
0
        public async Task <IList <ValidatorDuty> > ValidatorDutiesAsync(IEnumerable <BlsPublicKey> validatorPublicKeys, Epoch epoch)
        {
            // TODO: Rather than check one by one (each of which loops through potentially all slots for the epoch), optimise by either checking multiple, or better possibly caching or pre-calculating
            List <ValidatorDuty> validatorDuties = new List <ValidatorDuty>();

            foreach (BlsPublicKey validatorPublicKey in validatorPublicKeys)
            {
                ValidatorDuty validatorDuty = await _validatorAssignments.GetValidatorDutyAsync(validatorPublicKey, epoch);

                validatorDuties.Add(validatorDuty);
            }
            return(validatorDuties);
        }
Пример #30
0
 public static DateTime FromUnixTime(double unixTime) => Epoch.AddSeconds(unixTime);
Пример #31
0
 public Hash32 HashTreeRoot(Epoch epoch)
 {
     throw new NotImplementedException();
 }
Пример #32
0
            public SubscribeReplica(long logPosition, byte[] chunkId, Epoch[] lastEpochs, byte[] ip, int port,
                                    byte[] masterId, byte[] subscriptionId, bool isPromotable)
            {
                LogPosition = logPosition;
                ChunkId = chunkId;
                LastEpochs = lastEpochs;

                Ip = ip;
                Port = port;
                MasterId = masterId;
                SubscriptionId = subscriptionId;
                IsPromotable = isPromotable;
            }
 public RecordVersion(Epoch epoch, long logSequenceNumber)
 {
     this.Epoch = epoch;
     this.LogSequenceNumber = logSequenceNumber;
 }