Skip to content

Commit

Permalink
Namecoin / AuxPoW: avoid some copy operations in auxpow.deserialize_a…
Browse files Browse the repository at this point in the history
…uxpow_header.
  • Loading branch information
JeremyRand committed Jul 2, 2018
1 parent 80578e3 commit 751e1d7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
4 changes: 1 addition & 3 deletions lib/auxpow.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,9 @@ def deserialize_auxpow_header(base_header, s, expect_trailing_data=False, start_
# Chain ID is the top 16 bits of the 32-bit version.
auxpow_header['chain_id'] = get_chain_id(base_header)

s = s[start_position:]

# 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, copy_input=False)
parent_coinbase_tx = Transaction(None, expect_trailing_data=True, raw_bytes=s, expect_trailing_bytes=True, copy_input=False, start_position=start_position)
parent_coinbase_tx_dict, start_position = fast_tx_deserialize(parent_coinbase_tx)
auxpow_header['parent_coinbase_tx'] = parent_coinbase_tx

Expand Down
19 changes: 11 additions & 8 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, copy_input=True) -> dict:
def deserialize(raw: str, force_full_parse=False, expect_trailing_data=False, raw_bytes=None, expect_trailing_bytes=False, copy_input=True, start_position=0) -> dict:
if raw_bytes is None:
raw_bytes = bfh(raw)
d = {}
Expand All @@ -581,6 +581,7 @@ def deserialize(raw: str, force_full_parse=False, expect_trailing_data=False, ra
vds.write(raw_bytes)
else:
vds.input = raw_bytes
vds.read_cursor = start_position
d['version'] = vds.read_int32()
n_vin = vds.read_compact_size()
is_segwit = (n_vin == 0)
Expand Down Expand Up @@ -634,7 +635,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, copy_input=True):
def __init__(self, raw, expect_trailing_data=False, raw_bytes=None, expect_trailing_bytes=False, copy_input=True, start_position=0):
if raw is None:
self.raw = None
self.raw_bytes = raw_bytes
Expand All @@ -657,6 +658,7 @@ def __init__(self, raw, expect_trailing_data=False, raw_bytes=None, expect_trail
self.expect_trailing_data = expect_trailing_data
self.expect_trailing_bytes = expect_trailing_bytes
self.copy_input = copy_input
self.start_position = start_position

def update(self, raw):
self.raw = raw
Expand Down Expand Up @@ -741,9 +743,9 @@ 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, copy_input=self.copy_input)
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, start_position=self.start_position)
else:
d = deserialize(self.raw, force_full_parse, raw_bytes=self.raw_bytes)
d = deserialize(self.raw, force_full_parse, raw_bytes=self.raw_bytes, start_position=self.start_position)
self._inputs = d['inputs']
self._outputs = [(x['type'], x['address'], x['value']) for x in d['outputs']]
self.locktime = d['lockTime']
Expand All @@ -753,15 +755,16 @@ def deserialize(self, force_full_parse=False):
if self.expect_trailing_data:
if self.expect_trailing_bytes:
if self.raw is not None:
self.raw = self.raw[:(2*start_position)]
self.raw = self.raw[(2*self.start_position):(2*start_position)]
if self.raw_bytes is not None:
self.raw_bytes = self.raw_bytes[:start_position]
self.raw_bytes = self.raw_bytes[self.start_position:start_position]
else:
if self.raw is not None:
self.raw = self.raw[:start_position]
self.raw = self.raw[self.start_position:start_position]
if self.raw_bytes is not None:
self.raw_bytes = self.raw_bytes[:(start_position//2)]
self.raw_bytes = self.raw_bytes[(self.start_position//2):(start_position//2)]
self.expect_trailing_data = False
self.start_position = 0
return d, start_position
else:
return d
Expand Down

0 comments on commit 751e1d7

Please sign in to comment.