Example #1
0
        public void Beacon_state_there_and_back()
        {
            Eth1Data eth1Data = new Eth1Data(
                Sha256.RootOfAnEmptyString,
                1,
                Sha256.Bytes32OfAnEmptyString);

            BeaconBlockHeader beaconBlockHeader = new BeaconBlockHeader(
                new Slot(14),
                Sha256.RootOfAnEmptyString,
                Sha256.RootOfAnEmptyString,
                Sha256.RootOfAnEmptyString);

            Deposit         zeroDeposit     = new Deposit(Enumerable.Repeat(Bytes32.Zero, Ssz.DepositContractTreeDepth + 1), DepositData.Zero);
            BeaconBlockBody beaconBlockBody = new BeaconBlockBody(
                TestSig1,
                eth1Data,
                new Bytes32(new byte[32]),
                Enumerable.Repeat(ProposerSlashing.Zero, 2).ToArray(),
                Enumerable.Repeat(AttesterSlashing.Zero, 3).ToArray(),
                Enumerable.Repeat(Attestation.Zero, 4).ToArray(),
                Enumerable.Repeat(zeroDeposit, 5).ToArray(),
                Enumerable.Repeat(SignedVoluntaryExit.Zero, 6).ToArray()
                );

            BeaconBlock beaconBlock = new BeaconBlock(
                new Slot(1),
                Sha256.RootOfAnEmptyString,
                Sha256.RootOfAnEmptyString,
                beaconBlockBody);

            BeaconState container = new BeaconState(
                123,
                new Slot(1),
                new Fork(new ForkVersion(new byte[] { 0x05, 0x00, 0x00, 0x00 }),
                         new ForkVersion(new byte[] { 0x07, 0x00, 0x00, 0x00 }), new Epoch(3)),
                beaconBlockHeader,
                Enumerable.Repeat(Root.Zero, Ssz.SlotsPerHistoricalRoot).ToArray(),
                Enumerable.Repeat(Root.Zero, Ssz.SlotsPerHistoricalRoot).ToArray(),
                Enumerable.Repeat(Root.Zero, 13).ToArray(),
                eth1Data,
                Enumerable.Repeat(Eth1Data.Zero, 2).ToArray(),
                1234,
                Enumerable.Repeat(Validator.Zero, 7).ToArray(),
                new Gwei[3],
                Enumerable.Repeat(Bytes32.Zero, Ssz.EpochsPerHistoricalVector).ToArray(),
                new Gwei[Ssz.EpochsPerSlashingsVector],
                Enumerable.Repeat(PendingAttestation.Zero, 1).ToArray(),
                Enumerable.Repeat(PendingAttestation.Zero, 11).ToArray(),
                new BitArray(new byte[] { 0x09 }),
                new Checkpoint(new Epoch(3), Sha256.RootOfAnEmptyString),
                new Checkpoint(new Epoch(5), Sha256.RootOfAnEmptyString),
                new Checkpoint(new Epoch(7), Sha256.RootOfAnEmptyString)
                );

            JsonSerializerOptions options = new JsonSerializerOptions {
                WriteIndented = true
            };

            options.ConfigureNethermindCore2();
            TestContext.WriteLine("Original state: {0}", JsonSerializer.Serialize(container, options));

            int encodedLength = Ssz.BeaconStateLength(container);

            TestContext.WriteLine("Encoded length: {0}", encodedLength);
            Span <byte> encoded = new byte[encodedLength];

            Ssz.Encode(encoded, container);
            BeaconState decoded = Ssz.DecodeBeaconState(encoded);

            TestContext.WriteLine("Decoded state: {0}", JsonSerializer.Serialize(decoded, options));

            AssertBeaconStateEqual(container, decoded);

            Span <byte> encodedAgain = new byte[Ssz.BeaconStateLength(decoded)];

            Ssz.Encode(encodedAgain, decoded);

            byte[] encodedArray      = encoded.ToArray();
            byte[] encodedAgainArray = encodedAgain.ToArray();

            encodedAgainArray.Length.ShouldBe(encodedArray.Length);
            //encodedAgainArray.ShouldBe(encodedArray);
            //Assert.True(Bytes.AreEqual(encodedAgain, encoded));

            Merkle.Ize(out UInt256 root, container);
        }
Example #2
0
        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 void Beacon_state_there_and_back()
        {
            Eth1Data eth1Data = new Eth1Data(
                Sha256.OfAnEmptyString,
                1,
                Sha256.OfAnEmptyString);

            BeaconBlockHeader beaconBlockHeader = new BeaconBlockHeader(
                new Slot(14),
                Sha256.OfAnEmptyString,
                Sha256.OfAnEmptyString,
                Sha256.OfAnEmptyString,
                SszTest.TestSig1);

            BeaconBlockBody beaconBlockBody = new BeaconBlockBody(
                SszTest.TestSig1,
                eth1Data,
                new Bytes32(new byte[32]),
                new ProposerSlashing[2],
                new AttesterSlashing[3],
                new Attestation[4],
                new Deposit[5],
                new VoluntaryExit[6]
                );

            BeaconBlock beaconBlock = new BeaconBlock(
                new Slot(1),
                Sha256.OfAnEmptyString,
                Sha256.OfAnEmptyString,
                beaconBlockBody,
                SszTest.TestSig1);

            BeaconState container = new BeaconState(
                123,
                new Slot(1),
                new Fork(new ForkVersion(new byte[] { 0x05, 0x00, 0x00, 0x00 }),
                         new ForkVersion(new byte[] { 0x07, 0x00, 0x00, 0x00 }), new Epoch(3)),
                beaconBlockHeader,
                new Hash32[Ssz.SlotsPerHistoricalRoot],
                new Hash32[Ssz.SlotsPerHistoricalRoot],
                new Hash32[13],
                eth1Data,
                new Eth1Data[2],
                1234,
                new Validator[7],
                new Gwei[3],
                new Hash32[Ssz.EpochsPerHistoricalVector],
                new Gwei[Ssz.EpochsPerSlashingsVector],
                new PendingAttestation[1],
                new PendingAttestation[11],
                new BitArray(new byte[] { 0x09 }),
                new Checkpoint(new Epoch(3), Sha256.OfAnEmptyString),
                new Checkpoint(new Epoch(5), Sha256.OfAnEmptyString),
                new Checkpoint(new Epoch(7), Sha256.OfAnEmptyString)
                );

            JsonSerializerOptions options = new JsonSerializerOptions {
                WriteIndented = true
            };

            options.ConfigureNethermindCore2();
            TestContext.WriteLine("Original state: {0}", JsonSerializer.Serialize(container, options));

            int encodedLength = Ssz.BeaconStateLength(container);

            TestContext.WriteLine("Encoded length: {0}", encodedLength);
            Span <byte> encoded = new byte[encodedLength];

            Ssz.Encode(encoded, container);
            BeaconState decoded = Ssz.DecodeBeaconState(encoded);

            TestContext.WriteLine("Decoded state: {0}", JsonSerializer.Serialize(decoded, options));

            AssertBeaconStateEqual(container, decoded);

            Span <byte> encodedAgain = new byte[Ssz.BeaconStateLength(decoded)];

            Ssz.Encode(encodedAgain, decoded);

            byte[] encodedArray      = encoded.ToArray();
            byte[] encodedAgainArray = encodedAgain.ToArray();

            encodedAgainArray.Length.ShouldBe(encodedArray.Length);
            //encodedAgainArray.ShouldBe(encodedArray);
            //Assert.True(Bytes.AreEqual(encodedAgain, encoded));

            Merkle.Ize(out UInt256 root, container);
        }