Beispiel #1
0
        /// <summary>
        /// Create a new transfer and add it to the pending list
        /// </summary>
        /// <param name="transfer"></param>
        public string CreateTransfer(Transfer transfer)
        {
            if (transfer == null ||
                transfer.Amount <= 0 ||
                transfer.Biscuit < 0)
            {
                return("Error with the transfer. It can't be added (You don't have enough money or you're trying to send negative/zero value)");
            }

            var guid = Guid.NewGuid();

            MainViewModel.WaitingForBlockchainAccess(guid);

            var chain        = MainViewModel.BlockChain.Chain.ToArray();
            var transactions = MainViewModel.BlockChain.PendingTransfers.ToArray();

            MainViewModel.BlockChainWaitingList.Remove(guid);

            if (GetBalance(transfer.FromAddress, chain, transactions) >= transfer.Amount + transfer.Biscuit ||
                new User(Constants.PRIVATE_WORDS_KITTYCHAIN).PublicAddress == transfer.FromAddress)
            {
                PendingTransfers.Add(transfer);
                var receivers = MainViewModel.BlockChainUpdated?.GetInvocationList();
                if (receivers != null)
                {
                    foreach (EventHandler receiver in receivers)
                    {
                        receiver.BeginInvoke(this, EventArgs.Empty, null, null);
                    }
                }
                return(Constants.TRANSFER_ADDED);
            }

            return("Error with the transfer. It can't be added (You don't have enough money or you're trying to send negative/zero value)");
        }
Beispiel #2
0
        /// <summary>
        /// Compare 2 blocks with :
        /// - Chain
        /// - PendingTransfers
        /// - Difficulty
        /// </summary>
        /// <param name="obj">
        /// The compared object, transform in KittyChain object
        /// </param>
        public override bool Equals(object obj)
        {
            if (!(obj is KittyChain other) ||
                !Chain.SequenceEqual(other.Chain) ||
                !PendingTransfers.SequenceEqual(other.PendingTransfers) ||
                !Difficulty.Equals(other.Difficulty))
            {
                return(false);
            }

            return(true);
        }
Beispiel #3
0
        /// <summary>
        /// Add the created block to the blockchain
        /// Create the transfer to give the biscuit to the creator of the block
        /// </summary>
        /// <param name="block"></param>
        public string AddBlock(Block block)
        {
            block.Transfers  = PendingTransfers.ToArray();
            PendingTransfers = new List <Transfer>();
            block.Index      = LastBlock.Index + 1;
            Chain.Add(block);

            if (block.Index % Constants.NUMBER_OF_BLOCKS_TO_CHECK_DIFFICULTY == 0)
            {
                return(CheckDifficulty());
            }

            var receivers = MainViewModel.BlockChainUpdated?.GetInvocationList();

            if (receivers != null)
            {
                foreach (EventHandler receiver in receivers)
                {
                    receiver.BeginInvoke(this, EventArgs.Empty, null, null);
                }
            }

            return("");
        }