Skip to content

Commit

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

start_position = 0

# 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_dict, s = fast_tx_deserialize(parent_coinbase_tx)
parent_coinbase_tx_dict, start_position = fast_tx_deserialize(parent_coinbase_tx)
auxpow_header['parent_coinbase_tx'] = parent_coinbase_tx

# Next is the parent block hash. According to the Bitcoin.it wiki,
Expand Down
16 changes: 7 additions & 9 deletions lib/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,8 @@ def add_signature_to_txin(cls, txin, signingPos, sig):
txin['scriptSig'] = None # force re-serialization
txin['witness'] = None # force re-serialization

# If expect_trailing_data == True, also returns start position of trailing
# data.
def deserialize(self, force_full_parse=False):
if self.raw is None and self.raw_bytes is None:
return
Expand All @@ -740,10 +742,6 @@ def deserialize(self, force_full_parse=False):
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)
if self.expect_trailing_bytes:
trailing_data = self.raw_bytes[start_position:]
else:
raise Exception("Unimplemented: trailing data in hex")
else:
d = deserialize(self.raw, force_full_parse, raw_bytes=self.raw_bytes)
self._inputs = d['inputs']
Expand All @@ -755,16 +753,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*len(trailing_data))]
self.raw = self.raw[:(2*start_position)]
if self.raw_bytes is not None:
self.raw_bytes = self.raw_bytes[:-len(trailing_data)]
self.raw_bytes = self.raw_bytes[:start_position]
else:
if self.raw is not None:
self.raw = self.raw[:-len(trailing_data)]
self.raw = self.raw[:start_position]
if self.raw_bytes is not None:
self.raw_bytes = self.raw_bytes[:(-len(trailing_data)//2)]
self.raw_bytes = self.raw_bytes[:(start_position//2)]
self.expect_trailing_data = False
return d, trailing_data
return d, start_position
else:
return d

Expand Down

0 comments on commit ae28d43

Please sign in to comment.