use correct segwit deserializer

This commit is contained in:
Lex Berezhny 2019-11-24 22:37:11 -05:00 committed by Victor Shyba
parent fb4d60f02f
commit 3f6493dc5c
3 changed files with 13 additions and 34 deletions

View file

@ -5,6 +5,7 @@ from torba.server.script import ScriptPubKey, OpCodes
from torba.server.util import cachedproperty from torba.server.util import cachedproperty
from torba.server.hash import hash_to_hex_str, HASHX_LEN from torba.server.hash import hash_to_hex_str, HASHX_LEN
from torba.server.coins import Coin, CoinError from torba.server.coins import Coin, CoinError
from torba.server.tx import DeserializerSegWit
from lbry.wallet.script import OutputScript from lbry.wallet.script import OutputScript
from .session import LBRYElectrumX, LBRYSessionManager from .session import LBRYElectrumX, LBRYSessionManager
@ -18,6 +19,7 @@ class LBC(Coin):
SESSIONCLS = LBRYElectrumX SESSIONCLS = LBRYElectrumX
BLOCK_PROCESSOR = LBRYBlockProcessor BLOCK_PROCESSOR = LBRYBlockProcessor
SESSION_MANAGER = LBRYSessionManager SESSION_MANAGER = LBRYSessionManager
DESERIALIZER = DeserializerSegWit
DB = LBRYDB DB = LBRYDB
NAME = "LBRY" NAME = "LBRY"
SHORTNAME = "LBC" SHORTNAME = "LBC"

View file

@ -649,7 +649,7 @@ class SQLDB:
body_timer = timer.add_timer('body') body_timer = timer.add_timer('body')
for position, (etx, txid) in enumerate(all_txs): for position, (etx, txid) in enumerate(all_txs):
tx = timer.run( tx = timer.run(
Transaction, etx.serialize(), height=height, position=position Transaction, etx.raw, height=height, position=position
) )
# Inputs # Inputs
spent_claims, spent_supports, spent_others = timer.run( spent_claims, spent_supports, spent_others = timer.run(

View file

@ -41,19 +41,9 @@ ZERO = bytes(32)
MINUS_1 = 4294967295 MINUS_1 = 4294967295
class Tx(namedtuple("Tx", "version inputs outputs locktime")): class Tx(namedtuple("Tx", "version inputs outputs locktime raw")):
"""Class representing a transaction.""" """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 TxInput(namedtuple("TxInput", "prev_hash prev_idx script sequence")):
"""Class representing a transaction input.""" """Class representing a transaction input."""
@ -105,22 +95,13 @@ class Deserializer:
def read_tx(self): def read_tx(self):
"""Return a deserialized transaction.""" """Return a deserialized transaction."""
version = self._read_le_int32() start = self.cursor
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()
return Tx( return Tx(
version, self._read_le_int32(), # version
inputs, self._read_inputs(), # inputs
outputs, self._read_outputs(), # outputs
locktime self._read_le_uint32(), # locktime
self.binary[start:self.cursor],
) )
def read_tx_and_hash(self): def read_tx_and_hash(self):
@ -144,11 +125,7 @@ class Deserializer:
def _read_inputs(self): def _read_inputs(self):
read_input = self._read_input read_input = self._read_input
num_inputs = self._read_varint() return [read_input() for i in range(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)]
def _read_input(self): def _read_input(self):
return TxInput( return TxInput(
@ -220,7 +197,7 @@ class Deserializer:
class TxSegWit(namedtuple("Tx", "version marker flag inputs outputs " class TxSegWit(namedtuple("Tx", "version marker flag inputs outputs "
"witness locktime")): "witness locktime raw")):
"""Class representing a SegWit transaction.""" """Class representing a SegWit transaction."""
@ -266,7 +243,7 @@ class DeserializerSegWit(Deserializer):
vsize = (3 * base_size + self.binary_length) // 4 vsize = (3 * base_size + self.binary_length) // 4
return TxSegWit(version, marker, flag, inputs, outputs, witness, 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): def read_tx(self):
return self._read_tx_parts()[0] return self._read_tx_parts()[0]