예제 #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);
        }
예제 #2
0
        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);
        }