Пример #1
0
        public BlockChainMessageObject ProcessTransaction()
        {
            BlockChainMessageObject message = new BlockChainMessageObject();

            if (!VerifySignature())
            {
                message.Message = "#Transaction signature failed to verify";
                message.IsValid = false;
                return(message);
            }
            foreach (var transactionInput in Inputs)
            {
                transactionInput.UTXO = BlockChain.GetUnspentTransactionOutputById(transactionInput.TransactionOutputId);
            }

            var totalInputValue = GetInputsValue();

            if (totalInputValue < BlockChain.MinimumTransaction)
            {
                message.Message = $"#Transaction Input is too small {totalInputValue}";
                message.IsValid = false;
                return(message);
            }

            double remainingBalance = totalInputValue - Value;

            TransactionId = CalculateHash();
            Outputs.Add(new TransactionOutput(RecipientPublicKey, Value, TransactionId));
            Outputs.Add(new TransactionOutput(SenderPublicKey, remainingBalance, TransactionId));

            foreach (var transactionOutput in Outputs)
            {
                BlockChain.AddUnspentTransactionOutput(transactionOutput.Id, transactionOutput);
            }

            foreach (var transactionInput in Inputs)
            {
                if (transactionInput.UTXO == null)
                {
                    continue;
                }
                BlockChain.RemoveUnspentTransactionOutput(transactionInput.UTXO.Id);
            }
            message.Message = "Transaction Processed!";
            message.IsValid = true;
            return(message);
        }