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 input to the list of transaction inputs. /// </summary> /// <param name="transactionInput"> /// The transaction input to be added to the list of transaction inputs. /// </param> public void AddInput(TransactionInput transactionInput) { this.transactionInputs.Add(transactionInput); }
/// <summary> /// Parses a Bitcoin transaction input. /// </summary> /// <param name="blockMemoryStreamReader"> /// Provides access to a section of the Bitcoin blockchain file. /// </param> /// <returns> /// The Bitcoin transaction input that was parsed. /// </returns> private static TransactionInput ParseTransactionInput(BlockMemoryStreamReader blockMemoryStreamReader) { TransactionInput transactionInput = new TransactionInput(); transactionInput.SourceTransactionHash = new ByteArray(blockMemoryStreamReader.ReadBytes(32).ReverseByteArray()); transactionInput.SourceTransactionOutputIndex = blockMemoryStreamReader.ReadUInt32(); int scriptLength = (int)blockMemoryStreamReader.ReadVariableLengthInteger(); // Ignore the script portion. transactionInput.InputScript = new ByteArray(blockMemoryStreamReader.ReadBytes(scriptLength)); // Ignore the sequence number. blockMemoryStreamReader.SkipBytes(4); return transactionInput; }