示例#1
0
        public void Pending_attestation_there_and_back()
        {
            AttestationData data = new AttestationData();

            data.Slot            = new Slot(1);
            data.CommitteeIndex  = new CommitteeIndex(2);
            data.BeaconBlockRoot = Sha256.OfAnEmptyString;
            data.Source          = new Checkpoint(new Epoch(1), Sha256.OfAnEmptyString);
            data.Target          = new Checkpoint(new Epoch(2), Sha256.OfAnEmptyString);

            PendingAttestation container = new PendingAttestation();

            container.AggregationBits = new byte[3];
            container.Data            = data;
            container.InclusionDelay  = new Slot(7);
            container.ProposerIndex   = new ValidatorIndex(13);

            Span <byte> encoded = new byte[PendingAttestation.SszLength(container)];

            Ssz.Encode(encoded, container);
            PendingAttestation?decoded = Ssz.DecodePendingAttestation(encoded);

            Assert.AreEqual(container, decoded);

            Merkle.Ize(out UInt256 root, container);
        }
示例#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 = PendingAttestation.SszLength(containers[i]);
                Encode(span.Slice(offset, VarOffsetSize), dynamicOffset);
                Encode(span.Slice(dynamicOffset, currentLength), containers[i]);
                offset        += VarOffsetSize;
                dynamicOffset += currentLength;
            }
        }
示例#3
0
        public static void Encode(Span <byte> span, PendingAttestation container)
        {
            if (span.Length != PendingAttestation.SszLength(container))
            {
                ThrowTargetLength <PendingAttestation>(span.Length, PendingAttestation.SszLength(container));
            }
            if (container == null)
            {
                return;
            }
            int offset        = 0;
            int dynamicOffset = PendingAttestation.SszDynamicOffset;

            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);
        }
示例#4
0
        public static void Encode(Span <byte> span, BeaconState container)
        {
            if (span.Length != BeaconState.SszLength(container))
            {
                ThrowTargetLength <BeaconState>(span.Length, BeaconState.SszLength(container));
            }

            int offset        = 0;
            int dynamicOffset = BeaconState.SszDynamicOffset;

            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, BeaconBlockHeader.SszLength), container.LatestBlockHeader);
            offset += BeaconBlockHeader.SszLength;
            Encode(span.Slice(offset, Time.SlotsPerHistoricalRoot * Sha256.SszLength), container.BlockRoots);
            offset += Time.SlotsPerHistoricalRoot * Sha256.SszLength;
            Encode(span.Slice(offset, Time.SlotsPerHistoricalRoot * Sha256.SszLength), container.StateRoots);
            offset += Time.SlotsPerHistoricalRoot * Sha256.SszLength;
            int length1 = container.HistoricalRoots.Length * Sha256.SszLength;

            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.Length * Eth1Data.SszLength;

            Encode(span.Slice(offset, VarOffsetSize), dynamicOffset);
            Encode(span.Slice(dynamicOffset, length2), container.Eth1DataVotes);
            dynamicOffset += length2;
            offset        += VarOffsetSize;
            Encode(span.Slice(offset, sizeof(ulong)), container.Eth1DepositIndex);
            offset += sizeof(ulong);
            int length3 = container.Validators.Length * Validator.SszLength;

            Encode(span.Slice(offset, VarOffsetSize), dynamicOffset);
            Encode(span.Slice(dynamicOffset, length3), container.Validators);
            dynamicOffset += length3;
            offset        += VarOffsetSize;
            int length4 = container.Balances.Length * Gwei.SszLength;

            Encode(span.Slice(offset, VarOffsetSize), dynamicOffset);
            Encode(span.Slice(dynamicOffset, length4), container.Balances);
            dynamicOffset += length4;
            offset        += VarOffsetSize;
            Encode(span.Slice(offset, Time.EpochsPerHistoricalVector * Sha256.SszLength), container.RandaoMixes);
            offset += Time.EpochsPerHistoricalVector * Sha256.SszLength;
            Encode(span.Slice(offset, Time.EpochsPerSlashingsVector * Gwei.SszLength), container.Slashings);
            offset += Time.EpochsPerSlashingsVector * Gwei.SszLength;

            int length5 = container.PreviousEpochAttestations.Length * VarOffsetSize;

            for (int i = 0; i < container.PreviousEpochAttestations.Length; i++)
            {
                length5 += PendingAttestation.SszLength(container.PreviousEpochAttestations[i]);
            }

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

            int length6 = container.CurrentEpochAttestations.Length * VarOffsetSize;

            for (int i = 0; i < container.CurrentEpochAttestations.Length; i++)
            {
                length6 += PendingAttestation.SszLength(container.CurrentEpochAttestations[i]);
            }

            Encode(span.Slice(offset, VarOffsetSize), dynamicOffset);
            Encode(span.Slice(dynamicOffset, length6), container.CurrentEpochAttestations);
            dynamicOffset += length6;
            offset        += VarOffsetSize;

            Encode(span.Slice(offset, 1), container.JustificationBits);
            offset += 1;
            Encode(span, container.PreviousJustifiedCheckpoint, ref offset);
            Encode(span, container.CurrentJustifiedCheckpoint, ref offset);
            Encode(span, container.FinalizedCheckpoint, ref offset);
        }