Beispiel #1
0
        public static int BeaconStateLength(BeaconState?container)
        {
            if (container is null)
            {
                return(0);
            }

            int result = BeaconStateDynamicOffset();

            result += Ssz.Hash32Length * (container.HistoricalRoots?.Count ?? 0);
            result += Ssz.ValidatorLength * (container.Validators?.Count ?? 0);
            result += Ssz.GweiLength * (container.Balances?.Count ?? 0);
            result += Ssz.Eth1DataLength * (container.Eth1DataVotes?.Count ?? 0);

            result += (container.PreviousEpochAttestations?.Count ?? 0) * sizeof(uint);
            if (!(container.PreviousEpochAttestations is null))
            {
                for (int i = 0; i < container.PreviousEpochAttestations.Count; i++)
                {
                    result += Ssz.PendingAttestationLength(container.PreviousEpochAttestations[i]);
                }
            }

            result += (container.CurrentEpochAttestations?.Count ?? 0) * sizeof(uint);
            if (!(container.CurrentEpochAttestations is null))
            {
                for (int i = 0; i < container.CurrentEpochAttestations.Count; i++)
                {
                    result += Ssz.PendingAttestationLength(container.CurrentEpochAttestations[i]);
                }
            }

            return(result);
        }
Beispiel #2
0
        public static void Encode(Span <byte> span, PendingAttestation[] containers)
        {
            int offset        = 0;
            int dynamicOffset = containers.Length * VarOffsetSize;

            for (int i = 0; i < containers.Length; i++)
            {
                int currentLength = Ssz.PendingAttestationLength(containers[i]);
                Encode(span.Slice(offset, VarOffsetSize), dynamicOffset);
                Encode(span.Slice(dynamicOffset, currentLength), containers[i]);
                offset        += VarOffsetSize;
                dynamicOffset += currentLength;
            }
        }
Beispiel #3
0
        public static void Encode(Span <byte> span, PendingAttestation container)
        {
            if (span.Length != Ssz.PendingAttestationLength(container))
            {
                ThrowTargetLength <PendingAttestation>(span.Length, Ssz.PendingAttestationLength(container));
            }
            int offset        = 0;
            int dynamicOffset = Ssz.PendingAttestationDynamicOffset;

            Encode(span, container.AggregationBits, ref offset, ref dynamicOffset);
            Encode(span, container.Data, ref offset);
            Encode(span, container.InclusionDelay, ref offset);
            Encode(span, container.ProposerIndex, ref offset);
        }
        public static int BeaconStateLength(BeaconState container)
        {
            int result = BeaconStateDynamicOffset();

            result += Ssz.RootLength * (container.HistoricalRoots.Count);
            result += Ssz.ValidatorLength * (container.Validators.Count);
            result += Ssz.GweiLength * (container.Balances.Count);
            result += Ssz.Eth1DataLength * (container.Eth1DataVotes.Count);

            result += container.PreviousEpochAttestations.Count * sizeof(uint);
            for (int i = 0; i < container.PreviousEpochAttestations.Count; i++)
            {
                result += Ssz.PendingAttestationLength(container.PreviousEpochAttestations[i]);
            }

            result += container.CurrentEpochAttestations.Count * sizeof(uint);
            for (int i = 0; i < container.CurrentEpochAttestations.Count; i++)
            {
                result += Ssz.PendingAttestationLength(container.CurrentEpochAttestations[i]);
            }

            return(result);
        }
        public static void Encode(Span <byte> span, BeaconState container)
        {
            if (span.Length != Ssz.BeaconStateLength(container))
            {
                ThrowTargetLength <BeaconState>(span.Length, Ssz.BeaconStateLength(container));
            }

            int offset        = 0;
            int dynamicOffset = Ssz.BeaconStateDynamicOffset();

            Encode(span.Slice(offset, sizeof(ulong)), container.GenesisTime);
            offset += sizeof(ulong);
            Encode(span, container.Slot, ref offset);
            Encode(span, container.Fork, ref offset);
            Encode(span.Slice(offset, Ssz.BeaconBlockHeaderLength), container.LatestBlockHeader);
            offset += Ssz.BeaconBlockHeaderLength;
            Encode(span.Slice(offset, Ssz.SlotsPerHistoricalRoot * Ssz.RootLength), container.BlockRoots);
            offset += Ssz.SlotsPerHistoricalRoot * Ssz.RootLength;
            Encode(span.Slice(offset, Ssz.SlotsPerHistoricalRoot * Ssz.RootLength), container.StateRoots);
            offset += Ssz.SlotsPerHistoricalRoot * Ssz.RootLength;
            int length1 = container.HistoricalRoots.Count * Ssz.RootLength;

            Encode(span.Slice(offset, VarOffsetSize), dynamicOffset);
            Encode(span.Slice(dynamicOffset, length1), container.HistoricalRoots);
            dynamicOffset += length1;
            offset        += VarOffsetSize;
            Encode(span, container.Eth1Data, ref offset);
            int length2 = container.Eth1DataVotes.Count * Ssz.Eth1DataLength;

            Encode(span.Slice(offset, VarOffsetSize), dynamicOffset);
            Encode(span.Slice(dynamicOffset, length2), container.Eth1DataVotes.ToArray());
            dynamicOffset += length2;
            offset        += VarOffsetSize;
            Encode(span.Slice(offset, sizeof(ulong)), container.Eth1DepositIndex);
            offset += sizeof(ulong);
            int length3 = container.Validators.Count * Ssz.ValidatorLength;

            Encode(span.Slice(offset, VarOffsetSize), dynamicOffset);
            Encode(span.Slice(dynamicOffset, length3), container.Validators.ToArray());
            dynamicOffset += length3;
            offset        += VarOffsetSize;
            int length4 = container.Balances.Count * Ssz.GweiLength;

            Encode(span.Slice(offset, VarOffsetSize), dynamicOffset);
            Encode(span.Slice(dynamicOffset, length4), container.Balances.ToArray());
            dynamicOffset += length4;
            offset        += VarOffsetSize;
            Encode(span.Slice(offset, Ssz.EpochsPerHistoricalVector * Ssz.Bytes32Length), container.RandaoMixes);
            offset += Ssz.EpochsPerHistoricalVector * Ssz.Bytes32Length;
            Encode(span.Slice(offset, Ssz.EpochsPerSlashingsVector * Ssz.GweiLength), container.Slashings.ToArray());
            offset += Ssz.EpochsPerSlashingsVector * Ssz.GweiLength;

            int length5 = container.PreviousEpochAttestations.Count * VarOffsetSize;

            for (int i = 0; i < container.PreviousEpochAttestations.Count; i++)
            {
                length5 += Ssz.PendingAttestationLength(container.PreviousEpochAttestations[i]);
            }

            Encode(span.Slice(offset, VarOffsetSize), dynamicOffset);
            Encode(span.Slice(dynamicOffset, length5), container.PreviousEpochAttestations.ToArray());
            dynamicOffset += length5;
            offset        += VarOffsetSize;

            int length6 = container.CurrentEpochAttestations.Count * VarOffsetSize;

            for (int i = 0; i < container.CurrentEpochAttestations.Count; i++)
            {
                length6 += Ssz.PendingAttestationLength(container.CurrentEpochAttestations[i]);
            }

            Encode(span.Slice(offset, VarOffsetSize), dynamicOffset);
            Encode(span.Slice(dynamicOffset, length6), container.CurrentEpochAttestations.ToArray());
            dynamicOffset += length6;
            offset        += VarOffsetSize;
            Encode(span, container.JustificationBits, ref offset);
            Encode(span, container.PreviousJustifiedCheckpoint, ref offset);
            Encode(span, container.CurrentJustifiedCheckpoint, ref offset);
            Encode(span, container.FinalizedCheckpoint, ref offset);
        }