Root ICryptographyService.HashTreeRoot(HistoricalBatch historicalBatch) { Merkle.Ize(out UInt256 root, historicalBatch); Span <byte> bytes = MemoryMarshal.Cast <UInt256, byte>(MemoryMarshal.CreateSpan(ref root, 1)); return(new Root(bytes)); }
public void ProcessFinalUpdates(BeaconState state) { _logger.LogInformation(Event.ProcessFinalUpdates, "Process epoch final updates state {BeaconState}", state); var timeParameters = _timeParameterOptions.CurrentValue; var gweiValues = _gweiValueOptions.CurrentValue; var stateListLengths = _stateListLengthOptions.CurrentValue; var currentEpoch = _beaconStateAccessor.GetCurrentEpoch(state); var nextEpoch = currentEpoch + new Epoch(1); // Reset eth1 data votes var nextSlot = state.Slot + new Slot(1); if (nextSlot % timeParameters.SlotsPerEth1VotingPeriod == Slot.Zero) { state.ClearEth1DataVotes(); } // Update effective balances with hysteresis var halfIncrement = gweiValues.EffectiveBalanceIncrement / 2; for (var index = 0; index < state.Validators.Count; index++) { var validator = state.Validators[index]; var balance = state.Balances[index]; if (balance < validator.EffectiveBalance || (validator.EffectiveBalance + (halfIncrement * 3)) < balance) { var roundedBalance = balance - (balance % gweiValues.EffectiveBalanceIncrement); var effectiveBalance = Gwei.Min(roundedBalance, gweiValues.MaximumEffectiveBalance); validator.SetEffectiveBalance(effectiveBalance); } } // Reset slashings var slashingsIndex = nextEpoch % stateListLengths.EpochsPerSlashingsVector; state.SetSlashings(slashingsIndex, Gwei.Zero); // Set randao mix var randaoIndex = nextEpoch % stateListLengths.EpochsPerHistoricalVector; var randaoMix = _beaconStateAccessor.GetRandaoMix(state, currentEpoch); state.SetRandaoMix(randaoIndex, randaoMix); // Set historical root accumulator var divisor = timeParameters.SlotsPerHistoricalRoot / timeParameters.SlotsPerEpoch; if ((ulong)nextEpoch % divisor == 0) { var historicalBatch = new HistoricalBatch(state.BlockRoots.ToArray(), state.StateRoots.ToArray()); var historicalRoot = historicalBatch.HashTreeRoot(); state.AddHistoricalRoot(historicalRoot); } // Rotate current/previous epoch attestations state.SetPreviousEpochAttestations(state.CurrentEpochAttestations); state.SetCurrentEpochAttestations(new PendingAttestation[0]); }
public static void Ize(out UInt256 root, HistoricalBatch container) { Merkleizer merkleizer = new Merkleizer(1); merkleizer.Feed(container.BlockRoots); merkleizer.Feed(container.StateRoots); merkleizer.CalculateRoot(out root); }
public static void Encode(Span <byte> span, HistoricalBatch container) { if (span.Length != Ssz.HistoricalBatchLength()) { ThrowTargetLength <HistoricalBatch>(span.Length, Ssz.HistoricalBatchLength()); } Encode(span.Slice(0, Ssz.HistoricalBatchLength() / 2), container.BlockRoots); Encode(span.Slice(Ssz.HistoricalBatchLength() / 2), container.StateRoots); }
public static void Encode(Span <byte> span, HistoricalBatch container) { if (span.Length != HistoricalBatch.SszLength) { ThrowTargetLength <HistoricalBatch>(span.Length, HistoricalBatch.SszLength); } Encode(span.Slice(0, HistoricalBatch.SszLength / 2), container.BlockRoots); Encode(span.Slice(HistoricalBatch.SszLength / 2), container.StateRoots); }
private static void TestHistoricBatchSsz(byte[] serialized, UInt256 expectedMerkleRoot, string testCaseDir) { HistoricalBatch container = Nethermind.Ssz.Ssz.DecodeHistoricalBatch(serialized); byte[] again = new byte[serialized.Length]; Nethermind.Ssz.Ssz.Encode(again, container); Assert.AreEqual(serialized.ToHexString(), again.ToHexString(), testCaseDir); Merkle.Ize(out UInt256 root, container); Assert.AreEqual(expectedMerkleRoot, root); }
public static HistoricalBatch?DecodeHistoricalBatch(Span <byte> span) { if (span.Length != Ssz.HistoricalBatchLength()) { ThrowSourceLength <HistoricalBatch>(span.Length, Ssz.HistoricalBatchLength()); } Root[] blockRoots = DecodeRoots(span.Slice(0, Ssz.HistoricalBatchLength() / 2)); Root[] stateRoots = DecodeRoots(span.Slice(Ssz.HistoricalBatchLength() / 2)); HistoricalBatch container = new HistoricalBatch(blockRoots, stateRoots); return(container); }
public static HistoricalBatch DecodeHistoricalBatch(Span <byte> span) { if (span.Length != HistoricalBatch.SszLength) { ThrowSourceLength <HistoricalBatch>(span.Length, HistoricalBatch.SszLength); } HistoricalBatch container = new HistoricalBatch(); container.BlockRoots = DecodeHashes(span.Slice(0, HistoricalBatch.SszLength / 2)); container.StateRoots = DecodeHashes(span.Slice(HistoricalBatch.SszLength / 2)); return(container); }
public void Historical_batch_there_and_back() { HistoricalBatch container = new HistoricalBatch(); container.BlockRoots[3] = Sha256.OfAnEmptyString; container.StateRoots[7] = Sha256.OfAnEmptyString; Span <byte> encoded = new byte[HistoricalBatch.SszLength]; Ssz.Encode(encoded, container); HistoricalBatch?decoded = Ssz.DecodeHistoricalBatch(encoded); Assert.AreEqual(container, decoded); Merkle.Ize(out UInt256 root, container); }
public void Historical_batch_there_and_back() { Root[] blockRoots = Enumerable.Repeat(Root.Zero, Ssz.SlotsPerHistoricalRoot).ToArray(); Root[] stateRoots = Enumerable.Repeat(Root.Zero, Ssz.SlotsPerHistoricalRoot).ToArray(); blockRoots[3] = Sha256.RootOfAnEmptyString; stateRoots[7] = Sha256.RootOfAnEmptyString; HistoricalBatch container = new HistoricalBatch(blockRoots, stateRoots); Span <byte> encoded = new byte[Ssz.HistoricalBatchLength()]; Ssz.Encode(encoded, container); HistoricalBatch?decoded = Ssz.DecodeHistoricalBatch(encoded); Assert.AreEqual(container, decoded); Merkle.Ize(out UInt256 root, container); }
public Root HashTreeRoot(HistoricalBatch historicalBatch) { throw new NotImplementedException(); }
public static Hash32 HashTreeRoot(this HistoricalBatch item) { var tree = new SszTree(item.ToSszContainer()); return(new Hash32(tree.HashTreeRoot())); }
private static IEnumerable <SszElement> GetValues(HistoricalBatch item) { yield return(item.BlockRoots.ToSszVector()); yield return(item.StateRoots.ToSszVector()); }
public static SszContainer ToSszContainer(this HistoricalBatch item) { return(new SszContainer(GetValues(item))); }
public Hash32 HashTreeRoot(HistoricalBatch historicalBatch) { Merkle.Ize(out UInt256 root, historicalBatch); Span<byte> bytes = MemoryMarshal.Cast<UInt256, byte>(MemoryMarshal.CreateSpan(ref root, 1)); return new Hash32(bytes); }
public Root HashTreeRoot(HistoricalBatch historicalBatch) { return(historicalBatch.HashTreeRoot()); }