From 3f6493dc5c3da593165728129df86af30f4e071f Mon Sep 17 00:00:00 2001 From: Lex Berezhny Date: Sun, 24 Nov 2019 22:37:11 -0500 Subject: [PATCH] use correct segwit deserializer --- lbry/lbry/wallet/server/coin.py | 2 ++ lbry/lbry/wallet/server/db/writer.py | 2 +- torba/torba/server/tx.py | 43 +++++++--------------------- 3 files changed, 13 insertions(+), 34 deletions(-) diff --git a/lbry/lbry/wallet/server/coin.py b/lbry/lbry/wallet/server/coin.py index 96588f3ad..eb136dc03 100644 --- a/lbry/lbry/wallet/server/coin.py +++ b/lbry/lbry/wallet/server/coin.py @@ -5,6 +5,7 @@ from torba.server.script import ScriptPubKey, OpCodes from torba.server.util import cachedproperty from torba.server.hash import hash_to_hex_str, HASHX_LEN from torba.server.coins import Coin, CoinError +from torba.server.tx import DeserializerSegWit from lbry.wallet.script import OutputScript from .session import LBRYElectrumX, LBRYSessionManager @@ -18,6 +19,7 @@ class LBC(Coin): SESSIONCLS = LBRYElectrumX BLOCK_PROCESSOR = LBRYBlockProcessor SESSION_MANAGER = LBRYSessionManager + DESERIALIZER = DeserializerSegWit DB = LBRYDB NAME = "LBRY" SHORTNAME = "LBC" diff --git a/lbry/lbry/wallet/server/db/writer.py b/lbry/lbry/wallet/server/db/writer.py index 01ec33a57..96e43e079 100644 --- a/lbry/lbry/wallet/server/db/writer.py +++ b/lbry/lbry/wallet/server/db/writer.py @@ -649,7 +649,7 @@ class SQLDB: body_timer = timer.add_timer('body') for position, (etx, txid) in enumerate(all_txs): tx = timer.run( - Transaction, etx.serialize(), height=height, position=position + Transaction, etx.raw, height=height, position=position ) # Inputs spent_claims, spent_supports, spent_others = timer.run( diff --git a/torba/torba/server/tx.py b/torba/torba/server/tx.py index e240bef36..13aee32ef 100644 --- a/torba/torba/server/tx.py +++ b/torba/torba/server/tx.py @@ -41,19 +41,9 @@ ZERO = bytes(32) MINUS_1 = 4294967295 -class Tx(namedtuple("Tx", "version inputs outputs locktime")): +class Tx(namedtuple("Tx", "version inputs outputs locktime raw")): """Class representing a transaction.""" - def serialize(self): - return b''.join(( - pack_le_int32(self.version), - pack_varint(len(self.inputs)), - b''.join(tx_in.serialize() for tx_in in self.inputs), - pack_varint(len(self.outputs)), - b''.join(tx_out.serialize() for tx_out in self.outputs), - pack_le_uint32(self.locktime) - )) - class TxInput(namedtuple("TxInput", "prev_hash prev_idx script sequence")): """Class representing a transaction input.""" @@ -105,22 +95,13 @@ class Deserializer: def read_tx(self): """Return a deserialized transaction.""" - version = self._read_le_int32() - inputs = self._read_inputs() - outputs = self._read_outputs() - if self.flags == 1: - # drain witness portion of transaction - # too many witnesses for no crime - for i in range(len(inputs)): - for v in range(self._read_varint()): - self._read_varbytes() - self.flags = 0 - locktime = self._read_le_uint32() + start = self.cursor return Tx( - version, - inputs, - outputs, - locktime + self._read_le_int32(), # version + self._read_inputs(), # inputs + self._read_outputs(), # outputs + self._read_le_uint32(), # locktime + self.binary[start:self.cursor], ) def read_tx_and_hash(self): @@ -144,11 +125,7 @@ class Deserializer: def _read_inputs(self): read_input = self._read_input - num_inputs = self._read_varint() - if num_inputs == 0: - self.flags = self._read_byte() - num_inputs = self._read_varint() - return [read_input() for i in range(num_inputs)] + return [read_input() for i in range(self._read_varint())] def _read_input(self): return TxInput( @@ -220,7 +197,7 @@ class Deserializer: class TxSegWit(namedtuple("Tx", "version marker flag inputs outputs " - "witness locktime")): + "witness locktime raw")): """Class representing a SegWit transaction.""" @@ -266,7 +243,7 @@ class DeserializerSegWit(Deserializer): vsize = (3 * base_size + self.binary_length) // 4 return TxSegWit(version, marker, flag, inputs, outputs, witness, - locktime), self.TX_HASH_FN(orig_ser), vsize + locktime, orig_ser), self.TX_HASH_FN(orig_ser), vsize def read_tx(self): return self._read_tx_parts()[0]