Esempio n. 1
0
        public void TestBasicBlockchainProtocol()
        {
            BlockChain genesisBlockTemplate = new BlockChain(null, DateTime.Now, 4, "Howdy World!");
            var        minedGenesisBlock    = Miner.Mine(genesisBlockTemplate, 4, CancellationToken.None);

            List <string> transactions = new List <string>()
            {
                "How are you doing?", "I am doing fine.", "Isn't this cool?"
            };

            CancellationTokenSource source   = new CancellationTokenSource(60000);
            BlockChainProtocol      protocol = new BlockChainProtocol(minedGenesisBlock, source.Token);


            CancellationTokenSource cancelMining = new CancellationTokenSource(60000);

            protocol.OnFoundBlock += (o, v) => cancelMining.Cancel();

            foreach (var transaction in transactions)
            {
                cancelMining = new CancellationTokenSource(60000);
                protocol.RecieveTransaction(this, new TransactionRecievedEventArgs(transaction));
                protocol.StartMining(cancelMining.Token);
            }

            var ledger   = new BlockChainValidator().GetLedger(protocol.CurrentBlock.Value);
            var expected = new [] { "Howdy World!", "How are you doing?", "I am doing fine.", "Isn't this cool?" };

            for (int i = 0; i < ledger.Count; i++)
            {
                Assert.AreEqual(expected[i], ledger[i]);
            }

            Assert.True(new BlockChainValidator().Validate(protocol.CurrentBlock.Value));
        }
Esempio n. 2
0
        private void addCompetingBlock(Nonced <BlockChain> block)
        {
            var valid = new BlockChainValidator().Validate(block);

            if (valid)
            {
                if (currentBlock == null)
                {
                    currentBlock = block;
                }

                // descended from block
                // Below the tree
                if (block.Value.GetBlockchainDepth() > currentBlock.Value.GetBlockchainDepth())
                {
                }
            }
        }