Пример #1
0
        /// <summary>
        /// Method to try hash for a new block
        /// </summary>
        /// <see cref="Block.TryHash"/>
        public void Mining()
        {
            CurrentMineBlock = new Block(0, ActualUser.PublicAddress, BlockChain.Chain.Last().Hash, BlockChain.PendingTransfers, BlockChain.Difficulty);

            while (true)
            {
                var guid = Guid.NewGuid();
                WaitingForBlockchainAccess(guid);

                var difficulty   = BlockChain.Difficulty;
                var previousHash = BlockChain.LastBlock.Hash;
                var transfers    = BlockChain.PendingTransfers.ToArray();

                BlockChainWaitingList.Remove(guid);

                if (CurrentMineBlock.TryHash(difficulty, previousHash, transfers))
                {
                    guid = Guid.NewGuid();
                    WaitingForBlockchainAccess(guid);

                    var dif = BlockChain.LastBlock.CreationDate - DateTime.UtcNow;
                    Console = BlockChain.AddBlock(CurrentMineBlock);

                    BlockChainWaitingList.Remove(guid);

                    if (!BlockChain.IsValid())
                    {
                        Client.NeedBlockchain();
                    }
                    else
                    {
                        var biscuit = CurrentMineBlock.GetBiscuit(BlockChain.Biscuit);

                        Console = $"You have mined one block ! You successfull win {biscuit} coins.";
                        Console = $"The last block was mined {dif:hh}h {dif:mm}m {dif:ss}s ago.";

                        Client.NewBlock(CurrentMineBlock);

                        var transfer = new Transfer(new User(Constants.PRIVATE_WORDS_KITTYCHAIN), ActualUser.PublicAddress, biscuit, 0);

                        BlockChain.CreateTransfer(transfer);
                        Client.NewTransfer(transfer);
                        CurrentMineBlock = new Block(0, ActualUser.PublicAddress, BlockChain.Chain.Last().Hash, BlockChain.PendingTransfers, BlockChain.Difficulty);

                        var receivers = BlockChainUpdated?.GetInvocationList();
                        if (receivers != null)
                        {
                            foreach (EventHandler receiver in receivers)
                            {
                                receiver.BeginInvoke(this, EventArgs.Empty, null, null);
                            }
                        }
                    }
                }
            }
        }
Пример #2
0
        public void CreateKittyChainTest()
        {
            var blockchain = new KittyChain();

            blockchain.InitializeChain();

            Assert.Single(blockchain.Chain);

            var blockchain2 = new KittyChain(GetBlocks(), new List <Transfer>());

            Assert.True(blockchain2.IsValid());
        }