Example #1
0
        static void Main(string[] args)
        {
            int difficulty = 3;

            BloqChain <string> chain = new BloqChain <string>(difficulty);

            chain.AddBloq("Hello");
            chain.AddBloq("Hello everyone!");

            foreach (var bloq in chain)
            {
                Console.WriteLine($"# Bloq {bloq.Index}");
                Console.WriteLine($"Data: {bloq.Data}");
                Console.WriteLine($"Previous Hash: {bloq.PreviousHash}");
                Console.WriteLine($"Hash: {bloq.Hash}");
                Console.WriteLine();
            }

            Console.WriteLine($"Is valid: {chain.IsValid}");
            Console.WriteLine();

            Console.WriteLine("Changing value in Bloq 1");
            chain[1].Data = "Bye";
            Console.WriteLine($"Is valid: {chain.IsValid}");
            Console.WriteLine();

            Console.WriteLine("Recalculating Hash of Bloq 1");
            chain[1].Mine(difficulty);
            Console.WriteLine($"Is valid: {chain.IsValid}");
            Console.WriteLine();

            Console.WriteLine("Changing PreviousHash of Bloq 2");
            chain[2].PreviousHash = chain[1].Hash;
            Console.WriteLine($"Is valid: {chain.IsValid}");
            Console.WriteLine();

            Console.WriteLine("Recalculating Hash of Bloq 2");
            chain[2].Mine(difficulty);
            Console.WriteLine($"Is valid: {chain.IsValid}");
            Console.WriteLine();

            foreach (var bloq in chain)
            {
                Console.WriteLine($"# Bloq {bloq.Index}");
                Console.WriteLine($"Data: {bloq.Data}");
                Console.WriteLine($"Previous Hash: {bloq.PreviousHash}");
                Console.WriteLine($"Hash: {bloq.Hash}");
                Console.WriteLine();
            }

            Console.ReadKey();
        }
Example #2
0
        public void AddBloq_DefaultData_Throws()
        {
            // Act And Assert
            var chain = new BloqChain <bool>(difficulty: 0);

            Assert.Throws <ArgumentException>(() => chain.AddBloq(default(bool)));
        }
Example #3
0
        public void AddBloq_NullBloq_Throws()
        {
            // Act and Assert
            var chain = new BloqChain <string>(difficulty: 0);

            Assert.Throws <ArgumentException>(() => chain.AddBloq(null));
        }
Example #4
0
        public void Indexer_IndexOutOfRange_Throws()
        {
            // Arrange
            var chain = new BloqChain <string>(difficulty: 0);

            chain.AddBloq("Some data");

            // Act and Assert
            Assert.Throws <ArgumentOutOfRangeException>(() => chain[5]);
        }
Example #5
0
        public void AddBloq_ValidBloq_AddsToChain()
        {
            // Arrange
            string data = "Some data";

            // Act
            var chain = new BloqChain <string>(difficulty: 0);

            chain.AddBloq(data);

            // Assert
            Assert.Equal(2, chain.Length);
        }
Example #6
0
        public void IsValid_UntamperedChainWithBLoqAdded_ReturnsTrue()
        {
            // Arrange
            string data = "Some data";

            var chain = new BloqChain <string>(difficulty: 0);

            chain.AddBloq(data);

            // Act
            bool result = chain.IsValid;

            // Assert
            Assert.True(result);
        }
Example #7
0
        public void IsValid_TamperedHashWithoutRemine_ReturnsFalse()
        {
            // Arrange
            string data = "Some data";

            var chain = new BloqChain <string>(difficulty: 0);

            chain.AddBloq(data);

            // Act
            chain[1].PreviousHash = "I'm changed";
            bool result = chain.IsValid;

            // Assert
            Assert.False(result);
        }
Example #8
0
        public void IsValid_TamperedBloqAddedWithRemineWithDifferentDifficulty_ReturnsFalse()
        {
            // Arrange
            string data = "Some data";

            var chain = new BloqChain <string>(difficulty: 1);

            chain.AddBloq(data);

            // Act
            chain[1].Data = "I'm changed";
            chain[1].Mine(difficulty: 0);

            bool result = chain.IsValid;

            // Assert
            Assert.False(result);
        }
Example #9
0
        public void Indexer_ValidIndex_GetsBloq()
        {
            // Arrange
            string data = "Some data";

            var chain = new BloqChain <string>(difficulty: 0);

            chain.AddBloq(data);

            // Act
            var bloq0 = chain[0];
            var bloq1 = chain[1];

            // Assert
            Assert.NotNull(bloq0);
            Assert.Equal(BloqHashing.GetGenesisHash(), bloq0.PreviousHash);

            Assert.NotNull(bloq1);
            Assert.Equal(data, bloq1.Data);
        }