//b.执行铸币交易 public void execute_CoinbaseTransaction(Transaction transaction) { uint indexOfOutput = 0; foreach (TxOut transactionOutput in transaction.Outputs) { string txhashAndIndex = transaction.GetHash().ToString() + "-" + indexOfOutput; string txhash = transaction.GetHash().ToString(); ulong value = transactionOutput.Value; string script = new ByteArray(transactionOutput.ScriptPubKey.ToBytes()).ToString(); if (value == 0) { if (transactionOutput.ScriptPubKey.ToBytes()[0] == 0x6a || transactionOutput.ScriptPubKey.ToBytes()[1] == 0x6a) { opreturnOutputItem_Class opreturnOutputItem = new opreturnOutputItem_Class(txhash, indexOfOutput, value, script); opreturnOutputLinkedList.AddLast(opreturnOutputItem); } else { UTXOItem_Class unSpentTxOutItem = new UTXOItem_Class(txhash, indexOfOutput, value, script); if (!utxoDictionary.ContainsKey(txhashAndIndex)) { utxoDictionary.Add(txhashAndIndex, unSpentTxOutItem); } else { utxoDictionary[txhashAndIndex].utxoItemAmount++; sameTransactionCount++; } } } else { UTXOItem_Class unSpentTxOutItem = new UTXOItem_Class(txhash, indexOfOutput, value, script); if (!utxoDictionary.ContainsKey(txhashAndIndex)) { utxoDictionary.Add(txhashAndIndex, unSpentTxOutItem); } else { utxoDictionary[txhashAndIndex].utxoItemAmount++; sameTransactionCount++; } } indexOfOutput++; } }
//c.执行常规交易 public void execute_RegularTransaction(Transaction transaction) { foreach (TxIn transactionInput in transaction.Inputs) { string sourceTxhashAndIndex = transactionInput.PrevOut.ToString(); if (utxoDictionary.ContainsKey(sourceTxhashAndIndex)) { if (utxoDictionary[sourceTxhashAndIndex].utxoItemAmount > 1) { utxoDictionary[sourceTxhashAndIndex].utxoItemAmount--; } else { utxoDictionary.Remove(sourceTxhashAndIndex); } } else { Console.WriteLine("当前交易中的输入不存在:" + sourceTxhashAndIndex); return; } } uint indexOfOutput = 0; foreach (TxOut transactionOutput in transaction.Outputs) { string txhashAndIndex = transaction.GetHash().ToString() + "-" + indexOfOutput; string txhash = transaction.GetHash().ToString(); ulong value = transactionOutput.Value; string script = new ByteArray(transactionOutput.ScriptPubKey.ToBytes()).ToString(); if (value == 0) { if (transactionOutput.ScriptPubKey.ToBytes()[0] == 0x6a || transactionOutput.ScriptPubKey.ToBytes()[1] == 0x6a) { opreturnOutputItem_Class opreturnOutputItem = new opreturnOutputItem_Class(txhash, indexOfOutput, value, script); opreturnOutputLinkedList.AddLast(opreturnOutputItem); } else { UTXOItem_Class unSpentTxOutItem = new UTXOItem_Class(txhash, indexOfOutput, value, script); if (!utxoDictionary.ContainsKey(txhashAndIndex)) { utxoDictionary.Add(txhashAndIndex, unSpentTxOutItem); } else { utxoDictionary[txhashAndIndex].utxoItemAmount++; sameTransactionCount++; } } } else { UTXOItem_Class unSpentTxOutItem = new UTXOItem_Class(txhash, indexOfOutput, value, script); if (!utxoDictionary.ContainsKey(txhashAndIndex)) { utxoDictionary.Add(txhashAndIndex, unSpentTxOutItem); } else { utxoDictionary[txhashAndIndex].utxoItemAmount++; sameTransactionCount++; } } indexOfOutput++; } }