예제 #1
0
        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);
        }
예제 #2
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);
 }
예제 #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;
        }
예제 #4
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);
        }
예제 #5
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);
 }
예제 #6
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]);
                }
            }
        }
예제 #7
0
        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);
        }
예제 #8
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);
        }
예제 #9
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);
        }
예제 #10
0
 private static byte[] NullDeposit()
 {
     return(new byte[Ssz.DepositLength()]);
 }