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); }
public BlockChainMessageObject AddTransaction(Transaction transaction) { BlockChainMessageObject message = new BlockChainMessageObject(); if (transaction == null) { message.Message = "Invalid Transaction!"; message.IsValid = false; return(message); } if (PreviousHash != _genesisBlockPreviousHash) { if (transaction.ProcessTransaction().IsValid != true) { message.Message = "Transaction failed to process. Discarded!"; message.IsValid = false; return(message); } } Transactions.Add(transaction); message.Message = "Transaction successfully added to block"; message.IsValid = true; return(message); }