Exemple #1
0
        public void Should_Verify_Incorrect_Hash()
        {
            // arrange
            var genesis     = Block.Genesis();
            var secondBlock = MakeBlock(2, new Challenge().Solve(genesis.Proof), Sha256Hash.Of("invalid"), new List <Transaction>());
            var thirdBlock  = MakeBlock(3, secondBlock);
            var chain       = new List <Block>
            {
                genesis, secondBlock, thirdBlock
            };

            // act
            var actual = Blockchain.Validate(chain);

            // assert
            actual.Should().BeFalse();
        }
Exemple #2
0
        public void Should_Create_Genesis_Block()
        {
            // arrange
            var expected = Block.Genesis();

            // act
            var blockchain = new Blockchain();
            var actual     = blockchain.Chain.First();

            // assert
            blockchain.Chain.Should().HaveCount(1);
            actual.Should().NotBeNull();
            actual.Index.Should().Be(1);
            actual.Proof.Should().Be(new ProofOfWork(1));
            actual.PreviousHash.Should().Be(Sha256Hash.Of("Genesis"));
            actual.Transactions.Should().BeEmpty();
        }
Exemple #3
0
        public void Should_Discard_Invalid_Blockchain()
        {
            // arrange
            var blockchain   = new Blockchain();
            var genesis      = Block.Genesis();
            var secondBlock  = MakeBlock(2, new Challenge().Solve(genesis.Proof), Sha256Hash.Of("invalid"), new List <Transaction>());
            var thirdBlock   = MakeBlock(3, secondBlock);
            var invalidChain = new List <Block>
            {
                genesis, secondBlock, thirdBlock
            };

            // act
            var actual = blockchain.ResolveConflicts(new List <IList <Block> > {
                invalidChain.ToList()
            });

            // assert
            actual.Should().BeFalse();
            blockchain.Chain.Should().NotBeEmpty();
            blockchain.Chain.Should().NotEqual(invalidChain);
        }