예제 #1
0
        public void TryToUpdateBestBlock__New_Best_Block_Provided()
        {
            var state = BlockchainIndexationState.Restore(new[]
            {
                new BlocksIntervalIndexationState(0, 5, true)
            });

            state.TryToUpdateBestBlock(7)
            .Should().BeTrue();

            state.Last().To
            .Should().Be(7);
        }
예제 #2
0
        public void Restore__No_Genesis_Block_Passed__ArgumentException_Thrown()
        {
            Action restore = () =>
            {
                BlockchainIndexationState.Restore(new []
                {
                    new BlocksIntervalIndexationState(1, 2, false),
                });
            };

            restore
            .Should().Throw <ArgumentException>()
            .WithMessage("Indexation state of the genesis block has not been found.");
        }
예제 #3
0
        public void Serialize_And_Deserialize_With_MessagePack()
        {
            var state = BlockchainIndexationState.Restore(new[]
            {
                new BlocksIntervalIndexationState(0, 2, false),
                new BlocksIntervalIndexationState(3, 6, true),
                new BlocksIntervalIndexationState(7, 9, false)
            });

            MessagePackSerializer
            .Deserialize <IEnumerable <BlocksIntervalIndexationState> >
                (MessagePackSerializer.Serialize((IEnumerable <BlocksIntervalIndexationState>)state))
            .Should().BeEquivalentTo(state);
        }
예제 #4
0
        public void Restore()
        {
            var intervals = new[]
            {
                new BlocksIntervalIndexationState(0, 5, true),
                new BlocksIntervalIndexationState(6, 9, false)
            };

            var state = BlockchainIndexationState.Restore(intervals);

            // ReSharper disable once CoVariantArrayConversion
            state
            .Should().BeEquivalentTo(intervals);
        }
예제 #5
0
        public void Restore__Non_Sequential_Intervals_Passed__ArgumentException_Thrown()
        {
            Action restore = () =>
            {
                BlockchainIndexationState.Restore(new []
                {
                    new BlocksIntervalIndexationState(0, 4, false),
                    new BlocksIntervalIndexationState(6, 9, true)
                });
            };

            restore
            .Should().Throw <ArgumentException>()
            .WithMessage("Specified intervals [0..4 and 6..9] are not sequential or overlap.");
        }
예제 #6
0
        public void Restore__Non_Merged_Intervals_Passed__ArgumentException_Thrown()
        {
            Action restore = () =>
            {
                BlockchainIndexationState.Restore(new []
                {
                    new BlocksIntervalIndexationState(0, 5, true),
                    new BlocksIntervalIndexationState(6, 9, true)
                });
            };

            restore
            .Should().Throw <ArgumentException>()
            .WithMessage("Specified intervals [0..5 and 6..9] have same indexation state [true].");
        }
        public async Task <BlockchainIndexationState> GetOrCreateAsync()
        {
            if (await _blobStorage.HasBlobAsync(Container, DataKey))
            {
                var stateJson = await _blobStorage.GetAsTextAsync(Container, DataKey);

                return(BlockchainIndexationState.Restore
                       (
                           JsonConvert.DeserializeObject <IEnumerable <BlockIntervalIndexationState> >(stateJson)
                       ));
            }
            else
            {
                return(BlockchainIndexationState.Create());
            }
        }
예제 #8
0
        private static async Task Write(BlockchainIndexationStateRepository repository)
        {
            Console.WriteLine("Reading the state from the 'state.json' file...");

            var json = await File.ReadAllTextAsync("state.json");

            Console.WriteLine("Deserializing the state from json...");

            var intervals = JsonConvert.DeserializeObject <IEnumerable <BlocksIntervalIndexationState> >(json);
            var state     = BlockchainIndexationState.Restore(intervals);

            var x = state.GetNonIndexedBlockNumbers();

            Console.WriteLine("Saving the state to BLOB...");

            await repository.UpdateAsync(state);
        }
 public async Task <BlockchainIndexationState> GetOrCreateAsync()
 {
     if (await _blobStorage.HasBlobAsync(Container, DataKey))
     {
         using (var stream = await _blobStorage.GetAsync(Container, DataKey))
         {
             return(BlockchainIndexationState.Restore
                    (
                        await MessagePackSerializer.DeserializeAsync <IEnumerable <BlocksIntervalIndexationState> >(stream)
                    ));
         }
     }
     else
     {
         return(BlockchainIndexationState.Create());
     }
 }
예제 #10
0
        private static async Task ReadFile(string filePath)
        {
            Console.WriteLine($"Reading the state from the file {filePath}...");

            using var stream = File.OpenRead(filePath);

            var state = BlockchainIndexationState.Restore
                        (
                await MessagePackSerializer.DeserializeAsync <IEnumerable <BlocksIntervalIndexationState> >(stream)
                        );

            Console.WriteLine("Serializing the state to json...");

            var json = JsonConvert.SerializeObject(state.AsEnumerable(), Formatting.Indented);

            Console.WriteLine("Serializing the state json to the 'state.json' file...");

            await File.WriteAllTextAsync("state.json", json);

            stream.Close();
        }
예제 #11
0
        private static async Task WriteFile(string destinationFile)
        {
            Console.WriteLine("Reading the state from the 'state.json' file...");

            var json = await File.ReadAllTextAsync("state.json");

            Console.WriteLine("Deserializing the state from json...");

            var intervals = JsonConvert.DeserializeObject <IEnumerable <BlocksIntervalIndexationState> >(json);
            var state     = BlockchainIndexationState.Restore(intervals);

            Console.WriteLine($"Saving the state to the file {destinationFile}...");

            using var serializationStream = new MemoryStream();
            await MessagePackSerializer.SerializeAsync(serializationStream, (IEnumerable <BlocksIntervalIndexationState>) state);

            serializationStream.Position = 0;

            using var writeStream = File.OpenWrite(destinationFile);

            await serializationStream.CopyToAsync(writeStream);

            writeStream.Close();
        }