public static int BeaconBlockBodyLength(BeaconBlockBody?container)
        {
            if (container is null)
            {
                return(0);
            }

            int result = BeaconBlockBodyDynamicOffset;

            result += Ssz.ProposerSlashingLength * container.ProposerSlashings.Count;
            result += Ssz.DepositLength() * container.Deposits.Count;
            result += Ssz.VoluntaryExitLength * container.VoluntaryExits.Count;

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

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

            return(result);
        }
Beispiel #2
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 #3
0
        private static void Encode(Span <byte> span, Deposit[]?containers, ref int offset, ref int dynamicOffset)
        {
            int length = (containers?.Length ?? 0) * Ssz.DepositLength();

            Encode(span.Slice(offset, VarOffsetSize), dynamicOffset);
            Encode(span.Slice(dynamicOffset, length), containers);
            dynamicOffset += length;
            offset        += VarOffsetSize;
        }
Beispiel #4
0
 public static void Encode(Span <byte> span, Deposit container)
 {
     if (span.Length != Ssz.DepositLength())
     {
         ThrowTargetLength <Deposit>(span.Length, Ssz.DepositLength());
     }
     Encode(span.Slice(0, Ssz.DepositLengthOfProof()), container.Proof);
     Encode(span.Slice(Ssz.DepositLengthOfProof()), container.Data.Item);
 }
        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, BeaconBlock container)
        {
            if (span.Length != Ssz.BeaconBlockLength(container))
            {
                ThrowTargetLength <BeaconBlock>(span.Length, Ssz.BeaconBlockLength(container));
            }
            int offset = 0;

            Encode(span, container, ref offset);
        }
        public static int AttesterSlashingLength(AttesterSlashing?container)
        {
            if (container is null)
            {
                return(0);
            }

            return(2 * sizeof(uint) +
                   Ssz.IndexedAttestationLength(container.Attestation1) +
                   Ssz.IndexedAttestationLength(container.Attestation2));
        }
Beispiel #8
0
        public static Deposit DecodeDeposit(ReadOnlySpan <byte> span)
        {
            if (span.Length != Ssz.DepositLength())
            {
                ThrowSourceLength <Deposit>(span.Length, Ssz.DepositLength());
            }
            Bytes32[]   proof   = DecodeBytes32s(span.Slice(0, Ssz.DepositLengthOfProof()));
            DepositData data    = DecodeDepositData(span.Slice(Ssz.DepositLengthOfProof()));
            Deposit     deposit = new Deposit(proof, data.OrRoot);

            return(deposit);
        }
Beispiel #9
0
 public static void Encode(Span <byte> span, Deposit?container)
 {
     if (span.Length != Ssz.DepositLength())
     {
         ThrowTargetLength <Deposit>(span.Length, Ssz.DepositLength());
     }
     if (container == null)
     {
         return;
     }
     Encode(span.Slice(0, Ssz.DepositLengthOfProof()), container.Proof);
     Encode(span.Slice(Ssz.DepositLengthOfProof()), container.Data);
 }
Beispiel #10
0
        public static void Encode(Span <byte> span, Attestation container)
        {
            if (span.Length != Ssz.AttestationLength(container))
            {
                ThrowTargetLength <Attestation>(span.Length, Ssz.AttestationLength(container));
            }
            int offset        = 0;
            int dynamicOffset = Ssz.AttestationDynamicOffset;

            Encode(span, container.AggregationBits, ref offset, ref dynamicOffset);
            Encode(span, container.Data, ref offset);
            Encode(span, container.Signature, ref offset);
        }
        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);
        }
Beispiel #12
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);
        }
Beispiel #13
0
        public static void Encode(Span <byte> span, Attestation[] containers)
        {
            int offset        = 0;
            int dynamicOffset = containers.Length * VarOffsetSize;

            for (int i = 0; i < containers.Length; i++)
            {
                int currentLength = Ssz.AttestationLength(containers[i]);
                Encode(span.Slice(offset, VarOffsetSize), dynamicOffset);
                Encode(span.Slice(dynamicOffset, currentLength), containers[i]);
                offset        += VarOffsetSize;
                dynamicOffset += currentLength;
            }
        }
Beispiel #14
0
        private static void Encode(Span <byte> span, AttesterSlashing[] containers, ref int offset, ref int dynamicOffset)
        {
            int length = containers.Length * VarOffsetSize;

            for (int i = 0; i < containers.Length; i++)
            {
                length += Ssz.AttesterSlashingLength(containers[i]);
            }

            Encode(span.Slice(offset, VarOffsetSize), dynamicOffset);
            Encode(span.Slice(dynamicOffset, length), containers);
            dynamicOffset += length;
            offset        += VarOffsetSize;
        }
Beispiel #15
0
        public static void Encode(Span <byte> span, BeaconBlock container)
        {
            if (span.Length != Ssz.BeaconBlockLength(container))
            {
                ThrowTargetLength <BeaconBlock>(span.Length, Ssz.BeaconBlockLength(container));
            }
            int offset = 0;

            Encode(span, container.Slot, ref offset);
            Encode(span, container.ParentRoot, ref offset);
            Encode(span, container.StateRoot, ref offset);
            Encode(span, Ssz.BeaconBlockDynamicOffset, ref offset);
            Encode(span, container.Signature, ref offset);
            Encode(span.Slice(offset), container.Body);
        }
Beispiel #16
0
        public static void Encode(Span <byte> span, Deposit[]?containers)
        {
            if (span.Length != Ssz.DepositLength() * (containers?.Length ?? 0))
            {
                ThrowTargetLength <Deposit>(span.Length, Ssz.DepositLength());
            }

            if (!(containers is null))
            {
                for (int i = 0; i < containers.Length; i++)
                {
                    Encode(span.Slice(i * Ssz.DepositLength(), Ssz.DepositLength()), containers[i]);
                }
            }
        }
        private static void Encode(Span <byte> span, BeaconBlockBody container, ref int offset)
        {
            // Semantics of Encode = write container into span at offset, then increase offset by the bytes written

            // Static
            int dynamicOffset = Ssz.BeaconBlockBodyDynamicOffset;

            Encode(span, container.RandaoReveal, ref offset);
            Encode(span, container.Eth1Data, ref offset);
            Encode(span, container.Graffiti.AsSpan().ToArray(), ref offset);

            Encode(span, dynamicOffset, ref offset);
            int proposerSlashingsLength = container.ProposerSlashings.Count * Ssz.ProposerSlashingLength;

            dynamicOffset += proposerSlashingsLength;

            Encode(span, dynamicOffset, ref offset);
            int attesterSlashingsLength = container.AttesterSlashings.Sum(x => Ssz.AttesterSlashingLength(x) + VarOffsetSize);

            dynamicOffset += attesterSlashingsLength;

            Encode(span, dynamicOffset, ref offset);
            dynamicOffset += container.Attestations.Sum(x => Ssz.AttestationLength(x) + VarOffsetSize);

            Encode(span, dynamicOffset, ref offset);
            int depositsLength = container.Deposits.Count * Ssz.DepositLength();

            dynamicOffset += depositsLength;

            Encode(span, dynamicOffset, ref offset);
            int voluntaryExitsLength = container.VoluntaryExits.Count * Ssz.VoluntaryExitLength;

            dynamicOffset += voluntaryExitsLength;

            // Dynamic
            Encode(span.Slice(offset, proposerSlashingsLength), container.ProposerSlashings.ToArray());
            offset += proposerSlashingsLength;

            Encode(span.Slice(offset, attesterSlashingsLength), container.AttesterSlashings.ToArray());
            offset += attesterSlashingsLength;

            Encode(span, container.Attestations, ref offset);

            Encode(span.Slice(offset, depositsLength), container.Deposits.ToArray());
            offset += depositsLength;

            EncodeList(span, container.VoluntaryExits, ref offset);
        }
Beispiel #18
0
        public static Deposit?DecodeDeposit(Span <byte> span)
        {
            if (span.Length != Ssz.DepositLength())
            {
                ThrowSourceLength <Deposit>(span.Length, Ssz.DepositLength());
            }
            if (span.SequenceEqual(NullDeposit()))
            {
                return(null);
            }
            Hash32[]    proof   = DecodeHashes(span.Slice(0, Ssz.DepositLengthOfProof()));
            DepositData data    = DecodeDepositData(span.Slice(Ssz.DepositLengthOfProof()));
            Deposit     deposit = new Deposit(proof, data);

            return(deposit);
        }
Beispiel #19
0
        private static void Encode(Span <byte> span, Attestation?[]?attestations, ref int offset, ref int dynamicOffset)
        {
            int length = (attestations?.Length ?? 0) * VarOffsetSize;

            if (!(attestations is null))
            {
                for (int i = 0; i < attestations.Length; i++)
                {
                    length += Ssz.AttestationLength(attestations[i]);
                }
            }

            Encode(span.Slice(offset, VarOffsetSize), dynamicOffset);
            Encode(span.Slice(dynamicOffset, length), attestations);
            dynamicOffset += length;
            offset        += VarOffsetSize;
        }
Beispiel #20
0
        public static Deposit[] DecodeDeposits(ReadOnlySpan <byte> span)
        {
            if (span.Length % Ssz.DepositLength() != 0)
            {
                ThrowInvalidSourceArrayLength <Deposit>(span.Length, Ssz.DepositLength());
            }

            int count = span.Length / Ssz.DepositLength();

            Deposit[] containers = new Deposit[count];
            for (int i = 0; i < count; i++)
            {
                containers[i] = DecodeDeposit(span.Slice(i * Ssz.DepositLength(), Ssz.DepositLength()));
            }

            return(containers);
        }
Beispiel #21
0
        private static void Encode(Span <byte> span, IReadOnlyList <Attestation> attestations, ref int offset)
        {
            // Semantics of Encode = write container into span at offset, then increase offset by the bytes written

            // Static
            int staticOffset  = offset;
            int dynamicOffset = attestations.Count * VarOffsetSize;

            offset += dynamicOffset;
            foreach (Attestation attestation in attestations)
            {
                int length = Ssz.AttestationLength(attestation);
                Encode(span, dynamicOffset, ref staticOffset);
                dynamicOffset += length;
                Encode(span.Slice(offset, length), attestation);
                offset += length;
            }
        }
        public static void Encode(Span <byte> span, AttesterSlashing?[]?containers)
        {
            if (containers is null)
            {
                return;
            }

            int offset        = 0;
            int dynamicOffset = containers.Length * VarOffsetSize;

            for (int i = 0; i < containers.Length; i++)
            {
                int currentLength = Ssz.AttesterSlashingLength(containers[i]);
                Encode(span.Slice(offset, VarOffsetSize), dynamicOffset);
                Encode(span.Slice(dynamicOffset, currentLength), containers[i]);
                offset        += VarOffsetSize;
                dynamicOffset += currentLength;
            }
        }
Beispiel #23
0
        public static void Encode(Span <byte> span, IndexedAttestation?container)
        {
            if (container is null)
            {
                return;
            }

            if (span.Length != Ssz.IndexedAttestationLength(container))
            {
                ThrowTargetLength <IndexedAttestation>(span.Length, Ssz.IndexedAttestationLength(container));
            }

            int offset        = 0;
            int dynamicOffset = Ssz.IndexedAttestationDynamicOffset;

            Encode(span, container.AttestingIndices.ToArray(), ref offset, ref dynamicOffset);
            Encode(span, container.Data, ref offset);
            Encode(span, container.Signature, ref offset);
        }
Beispiel #24
0
        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, AttesterSlashing?container)
        {
            if (span.Length != Ssz.AttesterSlashingLength(container))
            {
                ThrowTargetLength <AttesterSlashing>(span.Length, Ssz.AttesterSlashingLength(container));
            }

            if (container == null)
            {
                return;
            }

            int dynamicOffset = 2 * VarOffsetSize;
            int length1       = Ssz.IndexedAttestationLength(container.Attestation1);

            Encode(span.Slice(0, VarOffsetSize), dynamicOffset);
            Encode(span.Slice(dynamicOffset, length1), container.Attestation1);

            dynamicOffset += Ssz.IndexedAttestationLength(container.Attestation1);
            int length2 = Ssz.IndexedAttestationLength(container.Attestation2);

            Encode(span.Slice(VarOffsetSize, VarOffsetSize), dynamicOffset);
            Encode(span.Slice(dynamicOffset, length2), container.Attestation2);
        }
Beispiel #26
0
 public static int SignedBeaconBlockLength(SignedBeaconBlock container)
 {
     return(SignedBeaconBlockDynamicOffset + Ssz.BeaconBlockLength(container.Message));
 }
Beispiel #27
0
 public static void Ize(Span <byte> root, UInt128 value)
 {
     Ssz.Encode(root.Slice(0, 16), value);
 }
Beispiel #28
0
 public static void Ize(Span <byte> root, UInt256 value)
 {
     Ssz.Encode(root, value);
 }
Beispiel #29
0
        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);
        }
Beispiel #30
0
 private static byte[] NullDeposit()
 {
     return(new byte[Ssz.DepositLength()]);
 }