Skip to content

Commit

Permalink
Namecoin / AuxPoW: Avoid some copy operations in transaction.deserial…
Browse files Browse the repository at this point in the history
…ize.
  • Loading branch information
JeremyRand committed Jul 2, 2018
1 parent 3b15937 commit 271fcb4
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/auxpow.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def deserialize_auxpow_header(base_header, s, expect_trailing_data=False):

# The parent coinbase transaction is first.
# Deserialize it and save the trailing data.
parent_coinbase_tx = Transaction(None, expect_trailing_data=True, raw_bytes=s, expect_trailing_bytes=True)
parent_coinbase_tx = Transaction(None, expect_trailing_data=True, raw_bytes=s, expect_trailing_bytes=True, copy_input=False)
parent_coinbase_tx_dict, s = fast_tx_deserialize(parent_coinbase_tx)
auxpow_header['parent_coinbase_tx'] = parent_coinbase_tx

Expand Down
12 changes: 8 additions & 4 deletions lib/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ def parse_output(vds, i):

# if expect_trailing_data, returns (deserialized transaction, start position of
# trailing data)
def deserialize(raw: str, force_full_parse=False, expect_trailing_data=False, raw_bytes=None, expect_trailing_bytes=False) -> dict:
def deserialize(raw: str, force_full_parse=False, expect_trailing_data=False, raw_bytes=None, expect_trailing_bytes=False, copy_input=True) -> dict:
if raw_bytes is None:
raw_bytes = bfh(raw)
d = {}
Expand All @@ -577,7 +577,10 @@ def deserialize(raw: str, force_full_parse=False, expect_trailing_data=False, ra
d['partial'] = is_partial = False
full_parse = force_full_parse or is_partial
vds = BCDataStream()
vds.write(raw_bytes)
if copy_input:
vds.write(raw_bytes)
else:
vds.input = raw_bytes
d['version'] = vds.read_int32()
n_vin = vds.read_compact_size()
is_segwit = (n_vin == 0)
Expand Down Expand Up @@ -631,7 +634,7 @@ def __str__(self):
self.raw = self.serialize()
return self.raw

def __init__(self, raw, expect_trailing_data=False, raw_bytes=None, expect_trailing_bytes=False):
def __init__(self, raw, expect_trailing_data=False, raw_bytes=None, expect_trailing_bytes=False, copy_input=True):
if raw is None:
self.raw = None
self.raw_bytes = raw_bytes
Expand All @@ -653,6 +656,7 @@ def __init__(self, raw, expect_trailing_data=False, raw_bytes=None, expect_trail
self._segwit_ser = None # None means "don't know"
self.expect_trailing_data = expect_trailing_data
self.expect_trailing_bytes = expect_trailing_bytes
self.copy_input = copy_input

def update(self, raw):
self.raw = raw
Expand Down Expand Up @@ -735,7 +739,7 @@ def deserialize(self, force_full_parse=False):
if self._inputs is not None:
return
if self.expect_trailing_data:
d, start_position = deserialize(self.raw, force_full_parse, expect_trailing_data=self.expect_trailing_data, raw_bytes=self.raw_bytes, expect_trailing_bytes=self.expect_trailing_bytes)
d, start_position = deserialize(self.raw, force_full_parse, expect_trailing_data=self.expect_trailing_data, raw_bytes=self.raw_bytes, expect_trailing_bytes=self.expect_trailing_bytes, copy_input=self.copy_input)
if self.expect_trailing_bytes:
trailing_data = self.raw_bytes[start_position:]
else:
Expand Down

0 comments on commit 271fcb4

Please sign in to comment.