public void Beacon_state_there_and_back() { Eth1Data eth1Data = new Eth1Data(); eth1Data.BlockHash = Sha256.OfAnEmptyString; eth1Data.DepositCount = 1; eth1Data.DepositRoot = Sha256.OfAnEmptyString; BeaconBlockHeader beaconBlockHeader = new BeaconBlockHeader(); beaconBlockHeader.Signature = BlsSignature.TestSig1; beaconBlockHeader.Slot = new Slot(14); beaconBlockHeader.BodyRoot = Sha256.OfAnEmptyString; beaconBlockHeader.ParentRoot = Sha256.OfAnEmptyString; beaconBlockHeader.StateRoot = Sha256.OfAnEmptyString; BeaconBlockBody beaconBlockBody = new BeaconBlockBody(); beaconBlockBody.RandaoReversal = BlsSignature.TestSig1; beaconBlockBody.Eth1Data = eth1Data; beaconBlockBody.Graffiti = new byte[32]; beaconBlockBody.ProposerSlashings = new ProposerSlashing[2]; beaconBlockBody.AttesterSlashings = new AttesterSlashing[3]; beaconBlockBody.Attestations = new Attestation[4]; beaconBlockBody.Deposits = new Deposit[5]; beaconBlockBody.VoluntaryExits = new VoluntaryExit[6]; BeaconBlock beaconBlock = new BeaconBlock(); beaconBlock.Body = beaconBlockBody; beaconBlock.Signature = BlsSignature.TestSig1; beaconBlock.Slot = new Slot(1); beaconBlock.ParentRoot = Sha256.OfAnEmptyString; beaconBlock.StateRoot = Sha256.OfAnEmptyString; BeaconState container = new BeaconState(); container.Balances = new Gwei[3]; container.Fork = new Fork(new ForkVersion(new byte[] { 0x05, 0x00, 0x00, 0x00 }), new ForkVersion(new byte[] { 0x07, 0x00, 0x00, 0x00 }), new Epoch(3)); container.Slashings = new Gwei[Time.EpochsPerSlashingsVector]; container.Slot = new Slot(1); container.Validators = new Validator[7]; container.BlockRoots = new Hash32[Time.SlotsPerHistoricalRoot]; container.StateRoots = new Hash32[Time.SlotsPerHistoricalRoot]; container.Eth1Data = eth1Data; container.Eth1DataVotes = new Eth1Data[2]; container.PreviousJustifiedCheckpoint = new Checkpoint(new Epoch(3), Sha256.OfAnEmptyString); container.CurrentJustifiedCheckpoint = new Checkpoint(new Epoch(5), Sha256.OfAnEmptyString); container.FinalizedCheckpoint = new Checkpoint(new Epoch(7), Sha256.OfAnEmptyString); container.GenesisTime = 123; container.HistoricalRoots = new Hash32[13]; container.JustificationBits = 9; container.RandaoMixes = new Hash32[Time.EpochsPerHistoricalVector]; container.PreviousEpochAttestations = new PendingAttestation[1]; container.CurrentEpochAttestations = new PendingAttestation[11]; container.Eth1DepositIndex = 1234; container.LatestBlockHeader = beaconBlockHeader; Span <byte> encoded = new byte[BeaconState.SszLength(container)]; Ssz.Encode(encoded, container); BeaconState decoded = Ssz.DecodeBeaconState(encoded); Assert.AreEqual(container, decoded); Span <byte> encodedAgain = new byte[BeaconState.SszLength(decoded)]; Ssz.Encode(encodedAgain, decoded); Assert.True(Bytes.AreEqual(encodedAgain, encoded)); Merkle.Ize(out UInt256 root, container); }
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); }