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

Commit f098b1a

Browse files
committed
Created Nonce type, Defaulted legacy_tag to tag unless it is set
1 parent 552bf7b commit f098b1a

File tree

4 files changed

+51
-29
lines changed

4 files changed

+51
-29
lines changed

iota/transaction/base.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from iota.crypto import Curl, HASH_LENGTH
1111
from iota.json import JsonSerializable
1212
from iota.transaction.types import BundleHash, Fragment, TransactionHash, \
13-
TransactionTrytes
13+
TransactionTrytes, Nonce
1414
from iota.types import Address, Hash, Tag, TryteString, TrytesCompatible, \
1515
int_from_trits, trits_from_int
1616

@@ -64,7 +64,7 @@ def from_tryte_string(cls, trytes, hash_=None):
6464
attachment_timestamp = int_from_trits(tryte_string[2619:2628].as_trits()),
6565
attachment_timestamp_lower_bound = int_from_trits(tryte_string[2628:2637].as_trits()),
6666
attachment_timestamp_upper_bound = int_from_trits(tryte_string[2637:2646].as_trits()),
67-
nonce = TryteString(tryte_string[2646:2673]),
67+
nonce = Nonce(tryte_string[2646:2673]),
6868
)
6969

7070
def __init__(
@@ -73,7 +73,6 @@ def __init__(
7373
signature_message_fragment,
7474
address,
7575
value,
76-
legacy_tag,
7776
timestamp,
7877
current_index,
7978
last_index,
@@ -85,8 +84,9 @@ def __init__(
8584
attachment_timestamp_lower_bound,
8685
attachment_timestamp_upper_bound,
8786
nonce,
87+
legacy_tag = None
8888
):
89-
# type: (Optional[TransactionHash], Optional[Fragment], Address, int, Optional[Tag], int, Optional[int], Optional[int], Optional[BundleHash], Optional[TransactionHash], Optional[TransactionHash], Optional[Tag], Optional[int], Optional[int], Optional[int] Optional[Hash]) -> None
89+
# type: (Optional[TransactionHash], Optional[Fragment], Address, int, int, Optional[int], Optional[int], Optional[BundleHash], Optional[TransactionHash], Optional[TransactionHash], Optional[Tag], Optional[int], Optional[int], Optional[int] Optional[Hash]) -> None
9090
self.hash = hash_ # type: Optional[TransactionHash]
9191
"""
9292
Transaction ID, generated by taking a hash of the transaction
@@ -112,12 +112,12 @@ def __init__(
112112
Can be negative (i.e., for spending inputs).
113113
"""
114114

115-
self.legacy_tag = legacy_tag # type: Optional[Tag]
115+
self._legacy_tag = legacy_tag # type: Optional[Tag]
116116
"""
117117
Optional classification legacy_tag applied to this transaction.
118118
"""
119119

120-
self.nonce = nonce # type: Optional[Hash]
120+
self.nonce = nonce # type: Optional[Nonce]
121121
"""
122122
Unique value used to increase security of the transaction hash.
123123
"""
@@ -341,6 +341,15 @@ def get_signature_validation_trytes(self):
341341
+ self.current_index_as_trytes
342342
+ self.last_index_as_trytes
343343
)
344+
345+
@property
346+
def legacy_tag(self):
347+
# type: () -> Tag
348+
"""
349+
Return the legacy tag of the transaction.
350+
If no legacy tag was set, returns the tag instead.
351+
"""
352+
return self._legacy_tag or self.tag
344353

345354

346355
class Bundle(JsonSerializable, Sequence[Transaction]):

iota/transaction/creation.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from iota.crypto.types import PrivateKey
1414
from iota.exceptions import with_context
1515
from iota.transaction.base import Bundle, Transaction
16-
from iota.transaction.types import BundleHash, Fragment, TransactionHash
16+
from iota.transaction.types import BundleHash, Fragment, TransactionHash, Nonce
1717
from iota.transaction.utils import get_current_timestamp
1818
from iota.types import Address, Hash, Tag, TryteString
1919

@@ -37,7 +37,6 @@ def __init__(self, address, value, tag=None, message=None, timestamp=None):
3737

3838
super(ProposedTransaction, self).__init__(
3939
address = address,
40-
legacy_tag = Tag(b'') if tag is None else tag,
4140
tag = Tag(b'') if tag is None else tag,
4241
timestamp = timestamp,
4342
value = value,
@@ -55,7 +54,7 @@ def __init__(self, address, value, tag=None, message=None, timestamp=None):
5554
# These values start out empty; they will be populated when the
5655
# node does PoW.
5756
branch_transaction_hash = TransactionHash(b''),
58-
nonce = TryteString(b''),
57+
nonce = Nonce(b''),
5958
trunk_transaction_hash = TransactionHash(b''),
6059
)
6160

iota/transaction/types.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
'Fragment',
1212
'TransactionHash',
1313
'TransactionTrytes',
14+
'Nonce'
1415
]
1516

1617

@@ -72,3 +73,25 @@ def __init__(self, trytes):
7273
'trytes': trytes,
7374
},
7475
)
76+
77+
class Nonce(TryteString):
78+
"""
79+
A TryteString that acts as a transaction nonce.
80+
"""
81+
LEN = 27
82+
83+
def __init__(self, trytes):
84+
# type: (TrytesCompatible) -> None
85+
super(Nonce, self).__init__(trytes, pad=self.LEN)
86+
87+
if len(self._trytes) > self.LEN:
88+
raise with_context(
89+
exc = ValueError('{cls} values must be {len} trytes long.'.format(
90+
cls = type(self).__name__,
91+
len = self.LEN
92+
)),
93+
94+
context = {
95+
'trytes': trytes,
96+
},
97+
)

test/transaction/base_test.py

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from unittest import TestCase
66

77
from iota import Address, Bundle, BundleHash, Fragment, Hash, \
8-
Tag, Transaction, TransactionHash, TransactionTrytes, TryteString
8+
Tag, Transaction, TransactionHash, TransactionTrytes, TryteString, Nonce
99

1010

1111
class BundleTestCase(TestCase):
@@ -32,8 +32,7 @@ def setUp(self):
3232
branch_transaction_hash = TransactionHash(b''),
3333
bundle_hash = BundleHash(b''),
3434
hash_ = TransactionHash(b''),
35-
nonce = TryteString(b''),
36-
legacy_tag = Tag(b''),
35+
nonce = Nonce(b''),
3736
timestamp = 1485020456,
3837
trunk_transaction_hash = TransactionHash(b''),
3938
tag = Tag(b''),
@@ -62,8 +61,7 @@ def setUp(self):
6261
branch_transaction_hash = TransactionHash(b''),
6362
bundle_hash = BundleHash(b''),
6463
hash_ = TransactionHash(b''),
65-
nonce = TryteString(b''),
66-
legacy_tag = Tag(b''),
64+
nonce = Nonce(b''),
6765
timestamp = 1485020456,
6866
trunk_transaction_hash = TransactionHash(b''),
6967
tag = Tag(b''),
@@ -92,8 +90,7 @@ def setUp(self):
9290
branch_transaction_hash = TransactionHash(b''),
9391
bundle_hash = BundleHash(b''),
9492
hash_ = TransactionHash(b''),
95-
nonce = TryteString(b''),
96-
legacy_tag = Tag(b''),
93+
nonce = Nonce(b''),
9794
timestamp = 1485020456,
9895
trunk_transaction_hash = TransactionHash(b''),
9996
tag = Tag(b''),
@@ -157,8 +154,7 @@ def setUp(self):
157154
branch_transaction_hash = TransactionHash(b''),
158155
bundle_hash = BundleHash(b''),
159156
hash_ = TransactionHash(b''),
160-
nonce = TryteString(b''),
161-
legacy_tag = Tag(b''),
157+
nonce = Nonce(b''),
162158
timestamp = 1485020456,
163159
trunk_transaction_hash = TransactionHash(b''),
164160
tag = Tag(b''),
@@ -197,8 +193,7 @@ def setUp(self):
197193
branch_transaction_hash = TransactionHash(b''),
198194
bundle_hash = BundleHash(b''),
199195
hash_ = TransactionHash(b''),
200-
nonce = TryteString(b''),
201-
legacy_tag = Tag(b''),
196+
nonce = Nonce(b''),
202197
timestamp = 1485020456,
203198
trunk_transaction_hash = TransactionHash(b''),
204199
tag = Tag(b''),
@@ -228,8 +223,7 @@ def setUp(self):
228223
branch_transaction_hash = TransactionHash(b''),
229224
bundle_hash = BundleHash(b''),
230225
hash_ = TransactionHash(b''),
231-
nonce = TryteString(b''),
232-
legacy_tag = Tag(b''),
226+
nonce = Nonce(b''),
233227
timestamp = 1485020456,
234228
trunk_transaction_hash = TransactionHash(b''),
235229
tag = Tag(b''),
@@ -259,8 +253,7 @@ def setUp(self):
259253
branch_transaction_hash = TransactionHash(b''),
260254
bundle_hash = BundleHash(b''),
261255
hash_ = TransactionHash(b''),
262-
nonce = TryteString(b''),
263-
legacy_tag = Tag(b''),
256+
nonce = Nonce(b''),
264257
timestamp = 1485020456,
265258
trunk_transaction_hash = TransactionHash(b''),
266259
tag = Tag(b''),
@@ -290,8 +283,7 @@ def setUp(self):
290283
branch_transaction_hash = TransactionHash(b''),
291284
bundle_hash = BundleHash(b''),
292285
hash_ = TransactionHash(b''),
293-
nonce = TryteString(b''),
294-
legacy_tag = Tag(b''),
286+
nonce = Nonce(b''),
295287
timestamp = 1485020456,
296288
trunk_transaction_hash = TransactionHash(b''),
297289
tag = Tag(b''),
@@ -551,7 +543,7 @@ def test_from_tryte_string(self):
551543
self.assertEqual(
552544
transaction.nonce,
553545

554-
TryteString(
546+
Nonce(
555547
b'999999999999999999999999999'
556548
),
557549
)
@@ -629,7 +621,6 @@ def test_as_tryte_string(self):
629621
),
630622

631623
value = 0,
632-
legacy_tag = Tag(b'999999999999999999999999999'),
633624
timestamp = 1480690413,
634625
current_index = 1,
635626
last_index = 1,
@@ -659,7 +650,7 @@ def test_as_tryte_string(self):
659650

660651

661652
nonce =
662-
TryteString(
653+
Nonce(
663654
b'999999999999999999999999999'
664655
),
665656
)

0 commit comments

Comments
 (0)