2024 01 26 rm lazy bytes (#5373)

* Remove lazy evaluation of bytes in {Transaction, TransactionInput, TransactionOutput}

* use ByteVector.concat in BytesUtil.writeCmpctSizeUInt()

* mv cmpct.bytes into ByteVector.concat()

* Remove uncessary lazy byteSize
This commit is contained in:
Chris Stewart 2024-01-26 10:38:53 -06:00 committed by GitHub
parent 5bd5cc51dc
commit 5885f4e99e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 5 additions and 12 deletions

View file

@ -12,8 +12,6 @@ import scodec.bits.ByteVector
*/
sealed abstract class Transaction extends NetworkElement {
override lazy val byteSize = bytes.length
/** The `sha256(sha256(tx))` of this transaction,
* Note that this is the little endian encoding of the hash, NOT the big endian encoding shown in block
* explorers. See
@ -139,7 +137,7 @@ object Transaction extends Factory[Transaction] {
sealed abstract class NonWitnessTransaction extends Transaction {
override def weight: Long = byteSize * 4
override lazy val bytes: ByteVector = {
override val bytes: ByteVector = {
val versionBytes = version.bytes.reverse
val inputBytes = BytesUtil.writeCmpctSizeUInt(inputs)
val outputBytes = BytesUtil.writeCmpctSizeUInt(outputs)
@ -226,7 +224,7 @@ case class WitnessTransaction(
* Functionality inside of Bitcoin Core:
* [[https://github.com/bitcoin/bitcoin/blob/e8cfe1ee2d01c493b758a67ad14707dca15792ea/src/primitives/transaction.h#L282-L287s]]
*/
override lazy val bytes: ByteVector = {
override val bytes: ByteVector = {
val versionBytes = version.bytes.reverse
val inputBytes = BytesUtil.writeCmpctSizeUInt(inputs)
val outputBytes = BytesUtil.writeCmpctSizeUInt(outputs)

View file

@ -15,7 +15,7 @@ sealed abstract class TransactionInput extends NetworkElement {
def scriptSignature: ScriptSignature
def sequence: UInt32
override lazy val bytes: ByteVector = RawTransactionInputParser.write(this)
override val bytes: ByteVector = RawTransactionInputParser.write(this)
}
case object EmptyTransactionInput extends TransactionInput {

View file

@ -8,11 +8,7 @@ import scodec.bits.ByteVector
case class TransactionOutput(value: CurrencyUnit, scriptPubKey: ScriptPubKey)
extends NetworkElement {
//https://bitcoin.org/en/developer-reference#txout
override lazy val byteSize: Long = scriptPubKey.byteSize + 8
override lazy val bytes: ByteVector = RawTransactionOutputParser.write(this)
override val bytes: ByteVector = RawTransactionOutputParser.write(this)
}
final object EmptyTransactionOutput

View file

@ -10,9 +10,8 @@ import scala.annotation.tailrec
trait BytesUtil extends CryptoBytesUtil {
def writeCmpctSizeUInt[T <: NetworkElement](ts: Seq[T]): ByteVector = {
val serialized = ts.map(_.bytes).foldLeft(ByteVector.empty)(_ ++ _)
val cmpct = CompactSizeUInt(UInt64(ts.size))
cmpct.bytes ++ serialized
ByteVector.concat(cmpct.bytes +: ts.map(_.bytes))
}
/** Used parse a byte sequence to a Seq[TransactionInput], Seq[TransactionOutput], etc