Skip to content
This repository was archived by the owner on Jan 13, 2023. It is now read-only.

Commit c782574

Browse files
committed
Refactored common code, fixed head_to_tail semantics.
1 parent 5d16af1 commit c782574

File tree

2 files changed

+14
-25
lines changed

2 files changed

+14
-25
lines changed

iota/transaction/base.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ def get_messages(self, errors='drop'):
427427

428428
return messages
429429

430-
def as_tryte_strings(self, head_to_tail=True):
430+
def as_tryte_strings(self, head_to_tail=False):
431431
# type: (bool) -> List[TransactionTrytes]
432432
"""
433433
Returns TryteString representations of the transactions in this
@@ -436,10 +436,13 @@ def as_tryte_strings(self, head_to_tail=True):
436436
:param head_to_tail:
437437
Determines the order of the transactions:
438438
439-
- ``True`` (default): head txn first, tail txn last.
440-
- ``False``: tail txn first, head txn last.
439+
- ``True``: head txn first, tail txn last.
440+
- ``False`` (default): tail txn first, head txn last.
441+
442+
Note that the order is reversed by default, as this is the way
443+
bundles are typically broadcast to the Tangle.
441444
"""
442-
transactions = reversed(self) if head_to_tail else self
445+
transactions = self if head_to_tail else reversed(self)
443446
return [t.as_tryte_string() for t in transactions]
444447

445448
def as_json_compatible(self):

iota/transaction/creation.py

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@
1111
from iota.crypto.signing import KeyGenerator
1212
from iota.crypto.types import PrivateKey
1313
from iota.exceptions import with_context
14-
from iota.json import JsonSerializable
15-
from iota.transaction.base import Transaction
16-
from iota.transaction.types import Fragment, TransactionHash
14+
from iota.transaction.base import Bundle, Transaction
15+
from iota.transaction.types import BundleHash, Fragment, TransactionHash
1716
from iota.transaction.utils import get_current_timestamp
1817
from iota.types import Address, Hash, Tag, TryteString
1918

@@ -80,7 +79,7 @@ def as_tryte_string(self):
8079
return super(ProposedTransaction, self).as_tryte_string()
8180

8281

83-
class ProposedBundle(JsonSerializable, Sequence[ProposedTransaction]):
82+
class ProposedBundle(Bundle, Sequence[ProposedTransaction]):
8483
"""
8584
A collection of proposed transactions, to be treated as an atomic
8685
unit when attached to the Tangle.
@@ -89,8 +88,6 @@ def __init__(self, transactions=None, inputs=None, change_address=None):
8988
# type: (Optional[Iterable[ProposedTransaction]], Optional[Iterable[Address]], Optional[Address]) -> None
9089
super(ProposedBundle, self).__init__()
9190

92-
self.hash = None # type: Optional[Hash]
93-
9491
self._transactions = [] # type: List[ProposedTransaction]
9592

9693
if transactions:
@@ -177,17 +174,6 @@ def as_json_compatible(self):
177174
"""
178175
return [txn.as_json_compatible() for txn in self]
179176

180-
def as_tryte_strings(self):
181-
# type: () -> List[TryteString]
182-
"""
183-
Returns the bundle as a list of TryteStrings, suitable as inputs
184-
for :py:meth:`iota.api.Iota.send_trytes`.
185-
"""
186-
# Return the transaction trytes in reverse order, so that the tail
187-
# transaction is last. This will allow the node to link the
188-
# transactions properly when it performs PoW.
189-
return [t.as_tryte_string() for t in reversed(self)]
190-
191177
def add_transaction(self, transaction):
192178
# type: (ProposedTransaction) -> None
193179
"""
@@ -333,13 +319,13 @@ def finalize(self):
333319

334320
sponge.absorb(txn.get_signature_validation_trytes().as_trits())
335321

336-
bundle_hash = [0] * HASH_LENGTH # type: MutableSequence[int]
337-
sponge.squeeze(bundle_hash)
338-
self.hash = Hash.from_trits(bundle_hash)
322+
bundle_hash_trits = [0] * HASH_LENGTH # type: MutableSequence[int]
323+
sponge.squeeze(bundle_hash_trits)
339324

340325
# Copy bundle hash to individual transactions.
326+
bundle_hash = BundleHash.from_trits(bundle_hash_trits)
341327
for txn in self:
342-
txn.bundle_hash = self.hash
328+
txn.bundle_hash = bundle_hash
343329

344330
# Initialize signature/message fragment.
345331
txn.signature_message_fragment = Fragment(txn.message or b'')

0 commit comments

Comments
 (0)