protected void AdjustLength(int newArraySize, int adjustment) { if (Length == UnknownLength) { return; } // Our own length is now unknown if we have an unknown length adjustment. if (adjustment == UnknownLength) { Length = UnknownLength; return; } Length += adjustment; // Check if we will need more bytes to encode the length prefix. if (newArraySize == 1) { Length++; } // The assumption here is we never call adjustLength with the same arraySize as before. else if (newArraySize != 0) { Length += VarInt.SizeInBytesOf((ulong)newArraySize) - VarInt.SizeInBytesOf((ulong)(newArraySize - 1)); } }
/** * Used only in creation of the genesis block. */ TransactionInput(NetworkParameters params, Transaction parentTransaction, byte[] scriptBytes) { super(params); this.scriptBytes = scriptBytes; this.outpoint = new TransactionOutPoint(params, NO_SEQUENCE, (Transaction)null); this.sequence = NO_SEQUENCE; this.parentTransaction = parentTransaction; length = 40 + (scriptBytes == null ? 1 : VarInt.SizeInBytesOf(scriptBytes.Length) + scriptBytes.length); }
/// <summary> /// Provides a reasonable guess at the byte length of the transactions part of the block. /// The returned value will be accurate in 99% of cases and in those cases where not will probably slightly oversize. /// This is used to preallocate the underlying byte array for a Stream. /// If the size is under the real value the only penalty is resizing of the underlying byte array. /// </summary> /// <returns></returns> private int guessTransactionsLength() { if (transactionBytesValid) { return(Bytes.Length - HeaderSize); } if (Transactions == null) { return(0); } int len = VarInt.SizeInBytesOf((ulong)Transactions.Count); foreach (Transaction tx in Transactions) { len += tx.MessageSize == UnknownLength?255:tx.MessageSize; } // 255 is just a guess at an average tx length return(len); }
/// <summary> /// <exception cref="ProtocolException"/> /// </summary> private void parseTransactions() { if (transactionsParsed) { return; } Cursor = Offset + HeaderSize; optimalEncodingMessageSize = HeaderSize; if (Bytes.Length == Cursor) { // This message is just a header, it has no transactions. transactionsParsed = true; transactionBytesValid = false; return; } ulong numTransactions = ReadVarInt().Value; optimalEncodingMessageSize += VarInt.SizeInBytesOf(numTransactions); Transactions = new List <Transaction>((int)numTransactions); for (ulong i = 0; i < numTransactions; i++) { Transaction tx = new Transaction(Parameters, Bytes, Cursor, this, ParseLazy, ParseRetain, UnknownLength); Transactions.Add(tx); Cursor += tx.MessageSize; optimalEncodingMessageSize += tx.OptimalEncodingMessageSize; } // No need to set length here. If length was not provided then it should be set at the end of parseLight(). // If this is a genuine lazy parse then length must have been provided to the constructor. transactionsParsed = true; transactionBytesValid = ParseRetain; }