public async Task Attestation_Deserialize()
        {
            // Arrange
            JsonSerializerOptions options = new JsonSerializerOptions();

            options.ConfigureNethermindCore2();
            string jsonString = "{\"aggregation_bits\":\"0x0100000000010000010100\",\"data\":{" +
                                "\"beacon_block_root\":\"0x1212121212121212121212121212121212121212121212121212121212121212\",\"index\":2,\"slot\":21," +
                                "\"source\":{\"epoch\":1,\"root\":\"0x3434343434343434343434343434343434343434343434343434343434343434\"}," +
                                "\"target\":{\"epoch\":2,\"root\":\"0x5656565656565656565656565656565656565656565656565656565656565656\"}" +
                                "},\"signature\":\"0xefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefef\"}";

            // Act - deserialize from string
            await using MemoryStream memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
            Attestation attestation = await JsonSerializer.DeserializeAsync <Attestation>(memoryStream, options);

            attestation.Data.Slot.ShouldBe(new Slot(21));
            attestation.Data.Target.Epoch.ShouldBe(new Epoch(2));
            attestation.Data.Target.Root.AsSpan()[31].ShouldBe((byte)0x56);
            attestation.Signature.Bytes[95].ShouldBe((byte)0xef);

            attestation.AggregationBits[0].ShouldBeTrue();
            attestation.AggregationBits[5].ShouldBeTrue();
            attestation.AggregationBits.Length.ShouldBe(11);
        }
Example #2
0
        public BeaconNodeProxy(ILogger <BeaconNodeProxy> logger,
                               HttpClient httpClient)
        {
            _logger = logger;

            // Configure (and test) via IHttpClientFactory / AddHttpClient: https://docs.microsoft.com/en-us/dotnet/architecture/microservices/implement-resilient-applications/use-httpclientfactory-to-implement-resilient-http-requests

            _httpClient            = httpClient;
            _jsonSerializerOptions = new JsonSerializerOptions();
            _jsonSerializerOptions.ConfigureNethermindCore2();
        }
Example #3
0
        public async Task SignedBeaconBlock_RoundTripEmpty()
        {
            // Arrange
            JsonSerializerOptions options = new JsonSerializerOptions();

            options.ConfigureNethermindCore2();

            Eth1Data eth1Data = new Eth1Data(
                new Root(Enumerable.Repeat((byte)0x12, 32).ToArray()),
                64,
                new Bytes32(Enumerable.Repeat((byte)0x34, 32).ToArray()));

            BlsSignature randaoReveal = new BlsSignature(Enumerable.Repeat((byte)0xfe, 96).ToArray());

            BeaconBlockBody beaconBlockBody = new BeaconBlockBody(
                randaoReveal,
                eth1Data,
                new Bytes32(new byte[32]),
                new ProposerSlashing[0],
                new AttesterSlashing [0],
                new Attestation[0],
                new Deposit[0],
                new SignedVoluntaryExit[0]
                );

            BeaconBlock beaconBlock = new BeaconBlock(
                new Slot(1),
                new Root(Enumerable.Repeat((byte)0x78, 32).ToArray()),
                new Root(Enumerable.Repeat((byte)0x9a, 32).ToArray()),
                beaconBlockBody);

            SignedBeaconBlock signedBeaconBlock = new SignedBeaconBlock(
                beaconBlock,
                new BlsSignature(Enumerable.Repeat((byte)0x0e, 96).ToArray())
                );

            // Act - round trip to string
            await using MemoryStream outputMemoryStream = new MemoryStream();
            await JsonSerializer.SerializeAsync(outputMemoryStream, signedBeaconBlock, options);

            string jsonString = Encoding.UTF8.GetString(outputMemoryStream.ToArray());

            Console.WriteLine(jsonString);

            // Assert - Round trip

            await using MemoryStream inputMemoryStream = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
            SignedBeaconBlock roundTripSignedBeaconBlock = await JsonSerializer.DeserializeAsync <SignedBeaconBlock>(inputMemoryStream, options);

            roundTripSignedBeaconBlock.Message.Body.Eth1Data.BlockHash.AsSpan()[1].ShouldBe((byte)0x34);
        }
Example #4
0
        public async Task Syncing_DeserializeWithStatus()
        {
            // Arrange
            JsonSerializerOptions options = new JsonSerializerOptions();

            options.ConfigureNethermindCore2();
            string jsonString = "{\"is_syncing\":true,\"sync_status\":{\"current_slot\":2,\"highest_slot\":3,\"starting_slot\":1}}";

            // Act - deserialize from string
            await using MemoryStream memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
            Syncing syncing = await JsonSerializer.DeserializeAsync <Syncing>(memoryStream, options);

            syncing.IsSyncing.ShouldBeTrue();
            syncing.SyncStatus !.CurrentSlot.ShouldBe(new Slot(2));
        }
Example #5
0
        public async Task Syncing_DeserializeAlternativeOrderNullStatus()
        {
            // Arrange
            JsonSerializerOptions options = new JsonSerializerOptions();

            options.ConfigureNethermindCore2();
            string jsonString = "{\"sync_status\":null,\"is_syncing\":true}";

            // Act - deserialize from string
            await using MemoryStream memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
            Syncing syncing = await JsonSerializer.DeserializeAsync <Syncing>(memoryStream, options);

            syncing.IsSyncing.ShouldBeTrue();
            syncing.SyncStatus.ShouldBeNull();
        }
Example #6
0
        public async Task SyncingStatus_DeserializeAlternativeOrder()
        {
            // Arrange
            JsonSerializerOptions options = new JsonSerializerOptions();

            options.ConfigureNethermindCore2();
            string jsonString = "{\"starting_slot\":1,\"current_slot\":2,\"highest_slot\":3}";

            // Act - deserialize from string
            await using MemoryStream memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
            SyncingStatus syncingStatus = await JsonSerializer.DeserializeAsync <SyncingStatus>(memoryStream, options);

            syncingStatus.StartingSlot.ShouldBe(Slot.One);
            syncingStatus.CurrentSlot.ShouldBe(new Slot(2));
            syncingStatus.HighestSlot.ShouldBe(new Slot(3));
        }
Example #7
0
        public async Task SyncingStatus_Serialize()
        {
            // Arrange
            JsonSerializerOptions options = new JsonSerializerOptions();

            options.ConfigureNethermindCore2();
            SyncingStatus syncingStatus = new SyncingStatus(new Slot(1), new Slot(2), new Slot(3));

            // Act - serialize to string
            await using MemoryStream memoryStream = new MemoryStream();
            await JsonSerializer.SerializeAsync(memoryStream, syncingStatus, options);

            string jsonString = Encoding.UTF8.GetString(memoryStream.ToArray());

            // Assert
            jsonString.ShouldBe("{\"current_slot\":2,\"highest_slot\":3,\"starting_slot\":1}");
        }
Example #8
0
        public async Task Syncing_SerializeNullStatus()
        {
            // Arrange
            JsonSerializerOptions options = new JsonSerializerOptions();

            options.ConfigureNethermindCore2();
            Syncing syncing = new Syncing(true, null);

            // Act - serialize to string
            await using MemoryStream memoryStream = new MemoryStream();
            await JsonSerializer.SerializeAsync(memoryStream, syncing, options);

            string jsonString = Encoding.UTF8.GetString(memoryStream.ToArray());

            // Assert
            jsonString.ShouldBe("{\"is_syncing\":true,\"sync_status\":null}");
        }
        public async Task Attestation_Serialize()
        {
            // Arrange
            JsonSerializerOptions options = new JsonSerializerOptions();

            options.ConfigureNethermindCore2();
            Attestation attestation = new Attestation(
                new BitArray(new[] {
                true, false, false, false, false, true, false, false,
                true, true, false
            }),
                new AttestationData(
                    new Slot(2 * 8 + 5),
                    new CommitteeIndex(2),
                    new Root(Enumerable.Repeat((byte)0x12, 32).ToArray()),
                    new Checkpoint(
                        new Epoch(1),
                        new Root(Enumerable.Repeat((byte)0x34, 32).ToArray())
                        ),
                    new Checkpoint(
                        new Epoch(2),
                        new Root(Enumerable.Repeat((byte)0x56, 32).ToArray())
                        )
                    ),
                new BlsSignature(Enumerable.Repeat((byte)0xef, 96).ToArray()));

            // Act - serialize to string
            await using MemoryStream memoryStream = new MemoryStream();
            await JsonSerializer.SerializeAsync(memoryStream, attestation, options);

            string jsonString = Encoding.UTF8.GetString(memoryStream.ToArray());

            // Assert

            // Spec 0.10.1 has json to hex values, of format byte
            // If converted bits to bytes, e.g. little ending, then would not communicate length.
            // Convert bit 0 => 0x00, 1 => 0x01, and byte sequence of 0x00 and 0x01.

            jsonString.ShouldBe("{\"aggregation_bits\":\"0x0100000000010000010100\",\"data\":{" +
                                "\"beacon_block_root\":\"0x1212121212121212121212121212121212121212121212121212121212121212\",\"index\":2,\"slot\":21," +
                                "\"source\":{\"epoch\":1,\"root\":\"0x3434343434343434343434343434343434343434343434343434343434343434\"}," +
                                "\"target\":{\"epoch\":2,\"root\":\"0x5656565656565656565656565656565656565656565656565656565656565656\"}" +
                                "},\"signature\":\"0xefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefef\"}");
        }
Example #10
0
 public MemoryStore(ILogger <MemoryStore> logger,
                    IOptionsMonitor <InMemoryConfiguration> inMemoryConfigurationOptions,
                    DataDirectory dataDirectory,
                    IFileSystem fileSystem,
                    IHeadSelectionStrategy headSelectionStrategy,
                    StoreAccessor storeAccessor)
 {
     _logger = logger;
     _inMemoryConfigurationOptions = inMemoryConfigurationOptions;
     _dataDirectory         = dataDirectory;
     _fileSystem            = fileSystem;
     _headSelectionStrategy = headSelectionStrategy;
     _storeAccessor         = storeAccessor;
     _jsonSerializerOptions = new JsonSerializerOptions {
         WriteIndented = true
     };
     _jsonSerializerOptions.ConfigureNethermindCore2();
     if (_inMemoryConfigurationOptions.CurrentValue.LogBlockJson ||
         inMemoryConfigurationOptions.CurrentValue.LogBlockStateJson)
     {
         _ = GetLogDirectory();
     }
 }
Example #11
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 #12
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);
        }
        public async Task SignedBeaconBlock_RoundTripWithDeposit()
        {
            // Arrange
            JsonSerializerOptions options = new JsonSerializerOptions();

            options.ConfigureNethermindCore2();

            Eth1Data eth1Data = new Eth1Data(
                new Root(Enumerable.Repeat((byte)0x12, 32).ToArray()),
                64,
                new Bytes32(Enumerable.Repeat((byte)0x34, 32).ToArray()));

            int     depositContractTreeDepth = 32;
            Deposit deposit = new Deposit(
                Enumerable.Repeat(new Bytes32(Enumerable.Repeat((byte)0x11, 32).ToArray()), depositContractTreeDepth + 1),
                new Ref <DepositData>(new DepositData(
                                          new BlsPublicKey(Enumerable.Repeat((byte)0x22, 48).ToArray()),
                                          new Bytes32(Enumerable.Repeat((byte)0x33, 32).ToArray()),
                                          new Gwei(32_000_000),
                                          new BlsSignature(Enumerable.Repeat((byte)0x44, 96).ToArray())
                                          )));

            BlsSignature randaoReveal = new BlsSignature(Enumerable.Repeat((byte)0xfe, 96).ToArray());

            BeaconBlockBody beaconBlockBody = new BeaconBlockBody(
                randaoReveal,
                eth1Data,
                new Bytes32(new byte[32]),
                new ProposerSlashing[0],
                new AttesterSlashing [0],
                new Attestation[0],
                new Deposit[] { deposit },
                new SignedVoluntaryExit[0]
                );

            BeaconBlock beaconBlock = new BeaconBlock(
                new Slot(1),
                new Root(Enumerable.Repeat((byte)0x78, 32).ToArray()),
                new Root(Enumerable.Repeat((byte)0x9a, 32).ToArray()),
                beaconBlockBody);

            SignedBeaconBlock signedBeaconBlock = new SignedBeaconBlock(
                beaconBlock,
                new BlsSignature(Enumerable.Repeat((byte)0x0e, 96).ToArray())
                );

            // Act - round trip to string
            await using MemoryStream outputMemoryStream = new MemoryStream();
            await JsonSerializer.SerializeAsync(outputMemoryStream, signedBeaconBlock, options);

            string jsonString = Encoding.UTF8.GetString(outputMemoryStream.ToArray());

            Console.WriteLine(jsonString);

            // Assert - Round trip

            await using MemoryStream inputMemoryStream = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
            SignedBeaconBlock roundTripSignedBeaconBlock = await JsonSerializer.DeserializeAsync <SignedBeaconBlock>(inputMemoryStream, options);

            roundTripSignedBeaconBlock.Message.Body.Eth1Data.BlockHash.AsSpan()[31].ShouldBe((byte)0x34);
            roundTripSignedBeaconBlock.Message.Body.Deposits[0].Data.Item.Signature.AsSpan()[95].ShouldBe((byte)0x44);
            roundTripSignedBeaconBlock.Message.Body.Deposits[0].Proof[32].AsSpan()[31].ShouldBe((byte)0x11);
        }