public Block GenerateBlock( ByteArray blockHash, ByteArray transactionHash, InputInfo[] inputs, OutputInfo[] outputs) { BlockHeader blockHeader = new BlockHeader() { BlockHash = blockHash, BlockNonce = 0, BlockTargetDifficulty = 0, BlockTimestamp = this.GetCurrentBlockTimeStamp(), BlockTimestampUnix = 0, BlockVersion = 1, MerkleRootHash = ByteArray.Empty, PreviousBlockHash = this.previousBlockHash, }; this.blockTimeStamp = this.blockTimeStamp.AddDays(1); string expectedFileName = string.Format(CultureInfo.InvariantCulture, "blk{0:00000}.dat", this.GetCurentBlockchainFileIndex()); Block block = new Block(expectedFileName, blockHeader); Transaction transaction = new Transaction() { TransactionHash = transactionHash, TransactionLockTime = 0, TransactionVersion = 1, }; foreach (InputInfo inputInfo in inputs) { TransactionInput transactionInput = new TransactionInput() { InputScript = ByteArray.Empty, SourceTransactionHash = inputInfo.SourceTransactionHash, SourceTransactionOutputIndex = inputInfo.SourceTransactionOutputIndex, }; transaction.AddInput(transactionInput); } foreach (OutputInfo outputInfo in outputs) { TransactionOutput transactionOutput = new TransactionOutput() { OutputScript = ByteArray.Empty, OutputValueSatoshi = (ulong)(outputInfo.OutputValueSatoshi * DatabaseGenerator.BtcToSatoshi), }; transaction.AddOutput(transactionOutput); } block.AddTransaction(transaction); this.previousBlockHash = block.BlockHeader.BlockHash; return block; }
/// <summary> /// Adds a new transaction to the list of transactions. /// </summary> /// <param name="transaction"> /// The Bitcoin transaction to be added to the list of transactions. /// </param> public void AddTransaction(Transaction transaction) { this.transactions.Add(transaction); }
/// <summary> /// Parses a Bitcoin transaction. /// </summary> /// <param name="blockMemoryStreamReader"> /// Provides access to a section of the Bitcoin blockchain file. /// </param> /// <returns> /// The Bitcoin transaction that was parsed. /// </returns> private static Transaction ParseTransaction(BlockMemoryStreamReader blockMemoryStreamReader) { Transaction transaction = new Transaction(); int positionInBaseStreamAtTransactionStart = (int)blockMemoryStreamReader.BaseStream.Position; transaction.TransactionVersion = blockMemoryStreamReader.ReadUInt32(); int inputsCount = (int)blockMemoryStreamReader.ReadVariableLengthInteger(); for (int inputIndex = 0; inputIndex < inputsCount; inputIndex++) { TransactionInput transactionInput = BlockchainParser.ParseTransactionInput(blockMemoryStreamReader); transaction.AddInput(transactionInput); } int outputsCount = (int)blockMemoryStreamReader.ReadVariableLengthInteger(); for (int outputIndex = 0; outputIndex < outputsCount; outputIndex++) { TransactionOutput transactionOutput = BlockchainParser.ParseTransactionOutput(blockMemoryStreamReader); transaction.AddOutput(transactionOutput); } // TODO: Need to find out more details about the semantic of TransactionLockTime. transaction.TransactionLockTime = blockMemoryStreamReader.ReadUInt32(); int positionInBaseStreamAfterTransactionEnd = (int)blockMemoryStreamReader.BaseStream.Position; using (SHA256Managed sha256 = new SHA256Managed()) { //// We need to calculate the double SHA256 hash of this transaction. //// We need to access the buffer that contains the transaction that we jut read through. //// Here we take advantage of the fact that the entire block was loaded as an in-memory buffer. //// The base stream of blockMemoryStreamReader is that in-memory buffer. byte[] baseBuffer = blockMemoryStreamReader.GetBuffer(); int transactionBufferSize = positionInBaseStreamAfterTransactionEnd - positionInBaseStreamAtTransactionStart; byte[] hash1 = sha256.ComputeHash(baseBuffer, positionInBaseStreamAtTransactionStart, transactionBufferSize); transaction.TransactionHash = new ByteArray(sha256.ComputeHash(hash1).ReverseByteArray()); } return transaction; }