Beispiel #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);
                            }
                        }
                    }
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Method who wait to access the blockchain
        /// </summary>
        /// <param name="guid"></param>
        public static void WaitingForBlockchainAccess(Guid guid)
        {
            var waitingTime = 0;
            var first       = BlockChainWaitingList.FirstOrDefault();

            BlockChainWaitingList.Add(guid);

            bool result;

            try
            {
                result = !BlockChainWaitingList.FirstOrDefault().Equals(guid);
            }
            catch (Exception)
            {
                result = true;
            }

            while (result)
            {
                if (waitingTime > Constants.WAITING_TIME_MAX * 2000)
                {
                    if (first == BlockChainWaitingList.FirstOrDefault())
                    {
                        BlockChainWaitingList.Remove(first);
                    }
                    else
                    {
                        first       = BlockChainWaitingList.FirstOrDefault();
                        waitingTime = 0;
                    }
                }
                waitingTime++;
                Thread.Sleep(5);
                try
                {
                    result = !BlockChainWaitingList.FirstOrDefault().Equals(guid);
                }
                catch (Exception)
                {
                    result = true;
                }
            }
        }