From 3e826d0a5d6c0f2a0ec321b0e44f5375f733ac4c Mon Sep 17 00:00:00 2001 From: Jack Robison Date: Fri, 19 Feb 2021 13:15:48 -0500 Subject: [PATCH] named tuples --- lbry/wallet/server/tx.py | 21 ++++++++++++++++----- lbry/wallet/server/util.py | 2 +- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/lbry/wallet/server/tx.py b/lbry/wallet/server/tx.py index 411162155..33cf3da3a 100644 --- a/lbry/wallet/server/tx.py +++ b/lbry/wallet/server/tx.py @@ -26,7 +26,7 @@ # and warranty status of this software. """Transaction-related classes and functions.""" - +import typing from collections import namedtuple from lbry.wallet.server.hash import sha256, double_sha256, hash_to_hex_str @@ -41,11 +41,20 @@ ZERO = bytes(32) MINUS_1 = 4294967295 -class Tx(namedtuple("Tx", "version inputs outputs locktime raw")): - """Class representing a transaction.""" +class Tx(typing.NamedTuple): + version: int + inputs: typing.List['TxInput'] + outputs: typing.List['TxOutput'] + locktime: int + raw: bytes -class TxInput(namedtuple("TxInput", "prev_hash prev_idx script sequence")): +class TxInput(typing.NamedTuple): + prev_hash: bytes + prev_idx: int + script: bytes + sequence: int + """Class representing a transaction input.""" def __str__(self): script = self.script.hex() @@ -65,7 +74,9 @@ class TxInput(namedtuple("TxInput", "prev_hash prev_idx script sequence")): )) -class TxOutput(namedtuple("TxOutput", "value pk_script")): +class TxOutput(typing.NamedTuple): + value: int + pk_script: bytes def serialize(self): return b''.join(( diff --git a/lbry/wallet/server/util.py b/lbry/wallet/server/util.py index bc27f7d51..d78b23bb5 100644 --- a/lbry/wallet/server/util.py +++ b/lbry/wallet/server/util.py @@ -340,7 +340,7 @@ pack_le_int64 = struct_le_q.pack pack_le_uint16 = struct_le_H.pack pack_le_uint32 = struct_le_I.pack pack_be_uint64 = lambda x: x.to_bytes(8, byteorder='big') -pack_be_uint16 = struct_be_H.pack +pack_be_uint16 = lambda x: x.to_bytes(2, byteorder='big') pack_be_uint32 = struct_be_I.pack pack_byte = structB.pack