Skip to content

Commit f08d34a

Browse files
authored
Merge pull request #72 from ltonetwork/tx-use-none
Default properties as None instead of an empty string or zero.
2 parents 68cff61 + 4af7337 commit f08d34a

25 files changed

+133
-145
lines changed

src/lto/crypto.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,18 @@ def key_type_id(key_type):
8282
raise Exception('Key Type not supported')
8383

8484

85-
8685
def merge_dicts(x, y):
8786
z = x.copy()
88-
z.update(y)
87+
z.update(y)
8988
return z
9089

9190

91+
def clean_dict(x):
92+
return {k:v for (k,v) in x.items() if v is not None}
93+
94+
9295
def compare_data_transaction(data, transaction):
9396
for key in data:
9497
key2 = inflection.underscore(key)
9598
assert data[key] == getattr(transaction, key2)
99+

src/lto/transaction.py

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@
77
class Transaction(ABC):
88

99
def __init__(self):
10-
self.tx_fee = 0
11-
self.timestamp = 0
12-
self.proofs = []
13-
self.sender = ''
14-
self.sender_public_key = ''
15-
self.chain_id = ''
16-
self.sponsor = ''
17-
self.sponsor_public_key = ''
18-
self.sender_key_type = 'ed25519'
19-
self.sponsor_key_type = 'ed25519'
2010
self.id = None
11+
self.tx_fee = None
12+
self.timestamp = None
13+
self.sender = None
14+
self.sender_key_type = None
15+
self.sender_public_key = None
16+
self.sponsor = None
17+
self.sponsor_key_type = None
18+
self.sponsor_public_key = None
19+
self.chain_id = None
20+
self.proofs = []
2121
self.height = None
2222

2323
@abstractmethod
@@ -28,16 +28,17 @@ def is_signed(self):
2828
return len(self.proofs) != 0
2929

3030
def sign_with(self, account):
31-
if self.timestamp == 0:
31+
if self.timestamp is None:
3232
self.timestamp = int(time() * 1000)
3333

34-
if self.sender == '':
34+
if self.sender is None:
3535
self.sender = account.address
36+
self.sender_key_type = account.key_type
3637
self.sender_public_key = account.get_public_key()
37-
38-
self.chain_id = account.get_network()
39-
self.sender_key_type = account.key_type
40-
38+
39+
if self.chain_id is None:
40+
self.chain_id = account.get_network()
41+
4142
self.proofs.append(account.sign(self.to_binary()))
4243

4344
def sponsor_with(self, sponsor_account):
@@ -56,11 +57,3 @@ def broadcast_to(self, node):
5657
def to_json(self):
5758
pass
5859

59-
def _sponsor_json(self):
60-
if self.sponsor:
61-
return {"sponsor": self.sponsor,
62-
"sponsorPublicKey": self.sponsor_public_key,
63-
"sponsorKeyType": self.sponsor_key_type}
64-
else:
65-
return {}
66-

src/lto/transactions/anchor.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,8 @@ def to_binary(self):
5454
raise Exception('Incorrect Version')
5555

5656
def to_json(self):
57-
return (crypto.merge_dicts(
58-
{
59-
"id": self.id if self.id else "",
57+
return crypto.clean_dict({
58+
"id": self.id,
6059
"type": self.TYPE,
6160
"version": self.version,
6261
"sender": self.sender,
@@ -65,10 +64,12 @@ def to_json(self):
6564
"fee": self.tx_fee,
6665
"timestamp": self.timestamp,
6766
"anchors": list(map(lambda anchor: base58.b58encode(crypto.str2bytes(anchor)), self.anchors)),
68-
"proofs": self.proofs,
69-
"height": self.height if self.height else ""
70-
},
71-
self._sponsor_json()))
67+
"sponsor": self.sponsor,
68+
"sponsorKeyType": self.sponsor_key_type,
69+
"sponsorPublicKey": self.sponsor_public_key,
70+
"proofs": self.proofs or None,
71+
"height": self.height
72+
})
7273

7374
@staticmethod
7475
def from_data(data):

src/lto/transactions/association.py

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,15 @@ class Association(Transaction):
1010
TYPE = 16
1111
DEFAULT_VERSION = 3
1212

13-
def __init__(self, recipient, association_type, anchor='', expires=0):
13+
def __init__(self, recipient, association_type, anchor='', expires=None):
1414
super().__init__()
1515
self.recipient = recipient
1616
self.association_type = association_type
1717
self.anchor = anchor
1818
self.tx_fee = self.DEFAULT_FEE
1919
self.version = self.DEFAULT_VERSION
20-
2120
self.expires = expires
22-
current = int(time() * 1000)
23-
if self.expires != 0 and self.expires <= current:
24-
raise Exception('Wring exipration date')
21+
2522

2623
def __to_binary_v1(self):
2724
if self.anchor:
@@ -58,7 +55,7 @@ def __to_binary_v3(self):
5855
struct.pack(">Q", self.tx_fee) +
5956
base58.b58decode(self.recipient) +
6057
struct.pack(">i", self.association_type) +
61-
struct.pack(">Q", self.expires) +
58+
struct.pack(">Q", self.expires or 0) +
6259
struct.pack(">H", len(crypto.str2bytes(self.anchor))) +
6360
crypto.str2bytes(self.anchor))
6461

@@ -73,8 +70,8 @@ def to_binary(self):
7370

7471

7572
def to_json(self):
76-
tx = {
77-
"id": self.id if self.id else "",
73+
return crypto.clean_dict({
74+
"id": self.id,
7875
"type": self.TYPE,
7976
"version": self.version,
8077
"sender": self.sender,
@@ -86,12 +83,12 @@ def to_json(self):
8683
"timestamp": self.timestamp,
8784
"expires": self.expires if self.version != 1 else None,
8885
"fee": self.tx_fee,
89-
"proofs": self.proofs,
90-
"height": self.height if self.height else ""
91-
}
92-
if self.version == 1:
93-
tx.pop('expires')
94-
return crypto.merge_dicts(tx, self._sponsor_json())
86+
"sponsor": self.sponsor,
87+
"sponsorKeyType": self.sponsor_key_type,
88+
"sponsorPublicKey": self.sponsor_public_key,
89+
"proofs": self.proofs or None,
90+
"height": self.height
91+
})
9592

9693

9794
@staticmethod

src/lto/transactions/burn.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,22 @@ def to_binary(self):
3434
raise Exception('Incorrect Version')
3535

3636
def to_json(self):
37-
return (crypto.merge_dicts({
37+
return crypto.clean_dict({
38+
"id": self.id,
3839
"type": self.TYPE,
3940
"version": self.version,
40-
"id": self.id if self.id else "",
4141
"sender": self.sender,
4242
"senderKeyType": self.sender_key_type,
4343
"senderPublicKey": self.sender_public_key,
4444
"fee": self.tx_fee,
4545
"timestamp": self.timestamp,
4646
"amount": self.amount,
47-
"proofs": self.proofs,
48-
"height": self.height if self.height else ""
49-
}, self._sponsor_json()))
47+
"sponsor": self.sponsor,
48+
"sponsorKeyType": self.sponsor_key_type,
49+
"sponsorPublicKey": self.sponsor_public_key,
50+
"proofs": self.proofs or None,
51+
"height": self.height
52+
})
5053

5154
@staticmethod
5255
def from_data(data):

src/lto/transactions/cancel_lease.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,22 @@ def to_binary(self):
4444
raise Exception('Incorrect Version')
4545

4646
def to_json(self):
47-
return(crypto.merge_dicts({
48-
"id": self.id if self.id else "",
47+
return crypto.clean_dict({
48+
"id": self.id,
4949
"type": self.TYPE,
5050
"version": self.version,
5151
"sender": self.sender,
5252
"senderKeyType": self.sender_key_type,
5353
"senderPublicKey": self.sender_public_key,
5454
"fee": self.tx_fee,
5555
"timestamp": self.timestamp,
56-
"proofs": self.proofs,
56+
"sponsor": self.sponsor,
57+
"sponsorKeyType": self.sponsor_key_type,
58+
"sponsorPublicKey": self.sponsor_public_key,
59+
"proofs": self.proofs or None,
5760
"leaseId": self.lease_id,
58-
"height": self.height if self.height else ""
59-
},
60-
self._sponsor_json()))
61+
"height": self.height
62+
})
6163

6264
@staticmethod
6365
def from_data(data):

src/lto/transactions/cancel_sponsorship.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ def to_binary(self):
4444
raise Exception('Incorrect Version')
4545

4646
def to_json(self):
47-
return (crypto.merge_dicts({
48-
"id": self.id if self.id else "",
47+
return crypto.clean_dict({
48+
"id": self.id,
4949
"type": self.TYPE,
5050
"version": self.version,
5151
"senderKeyType": self.sender_key_type,
@@ -54,9 +54,12 @@ def to_json(self):
5454
"senderPublicKey": self.sender_public_key,
5555
"timestamp": self.timestamp,
5656
"fee": self.tx_fee,
57-
"proofs": self.proofs,
58-
"height": self.height if self.height else ""
59-
}, self._sponsor_json()))
57+
"sponsor": self.sponsor,
58+
"sponsorKeyType": self.sponsor_key_type,
59+
"sponsorPublicKey": self.sponsor_public_key,
60+
"proofs": self.proofs or None,
61+
"height": self.height
62+
})
6063

6164
@staticmethod
6265
def from_data(data):

src/lto/transactions/data.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ def to_binary(self):
5252
raise Exception('Incorrect Version')
5353

5454
def to_json(self):
55-
return (crypto.merge_dicts({
56-
"id": self.id if self.id else "",
55+
return crypto.clean_dict({
56+
"id": self.id,
5757
"type": self.TYPE,
5858
"version": self.version,
5959
"sender": self.sender,
@@ -62,10 +62,12 @@ def to_json(self):
6262
"fee": self.tx_fee,
6363
"timestamp": self.timestamp,
6464
"data": list(map(lambda entry: entry.to_json(), self.data)),
65-
"proofs": self.proofs,
66-
"height": self.height if self.height else ""
67-
},
68-
self._sponsor_json()))
65+
"sponsor": self.sponsor,
66+
"sponsorKeyType": self.sponsor_key_type,
67+
"sponsorPublicKey": self.sponsor_public_key,
68+
"proofs": self.proofs or None,
69+
"height": self.height
70+
})
6971

7072
def data_as_dict(self):
7173
dictionary = {}

src/lto/transactions/lease.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ def to_binary(self):
4848
raise Exception('Incorrect Version')
4949

5050
def to_json(self):
51-
return (crypto.merge_dicts({
52-
"id": self.id if self.id else "",
51+
return crypto.clean_dict({
52+
"id": self.id,
5353
"type": self.TYPE,
5454
"version": self.version,
5555
"sender": self.sender,
@@ -59,10 +59,12 @@ def to_json(self):
5959
"amount": self.amount,
6060
"fee": self.tx_fee,
6161
"timestamp": self.timestamp,
62-
"proofs": self.proofs,
63-
"height": self.height if self.height else ""
64-
},
65-
self._sponsor_json()))
62+
"sponsor": self.sponsor,
63+
"sponsorKeyType": self.sponsor_key_type,
64+
"sponsorPublicKey": self.sponsor_public_key,
65+
"proofs": self.proofs or None,
66+
"height": self.height
67+
})
6668

6769
@staticmethod
6870
def from_data(data):

src/lto/transactions/mass_transfer.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,20 +64,23 @@ def to_binary(self):
6464

6565

6666
def to_json(self):
67-
return (crypto.merge_dicts({
68-
"id": self.id if self.id else "",
67+
return crypto.clean_dict({
68+
"id": self.id,
6969
"type": self.TYPE,
7070
"version": self.version,
7171
"sender": self.sender,
7272
"senderKeyType": self.sender_key_type,
7373
"senderPublicKey": self.sender_public_key,
7474
"fee": self.tx_fee,
7575
"timestamp": self.timestamp,
76-
"proofs": self.proofs,
7776
"attachment": base58.b58encode(crypto.str2bytes(self.attachment)),
7877
"transfers": self.transfers,
79-
"height": self.height if self.height else ""
80-
}, self._sponsor_json()))
78+
"sponsor": self.sponsor,
79+
"sponsorKeyType": self.sponsor_key_type,
80+
"sponsorPublicKey": self.sponsor_public_key,
81+
"proofs": self.proofs or None,
82+
"height": self.height
83+
})
8184

8285
@staticmethod
8386
def from_data(data):

src/lto/transactions/register.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ def __account_to_json(account):
6060
return account
6161

6262
def to_json(self):
63-
return (crypto.merge_dicts({
64-
"id": self.id if self.id else "",
63+
return crypto.clean_dict({
64+
"id": self.id,
6565
"type": self.TYPE,
6666
"version": self.version,
6767
"sender": self.sender,
@@ -70,10 +70,12 @@ def to_json(self):
7070
"fee": self.tx_fee,
7171
"timestamp": self.timestamp,
7272
"accounts": list(map(self.__account_to_json, self.accounts)),
73-
"proofs": self.proofs,
74-
"height": self.height if self.height else ""
75-
},
76-
self._sponsor_json()))
73+
"sponsor": self.sponsor,
74+
"sponsorKeyType": self.sponsor_key_type,
75+
"sponsorPublicKey": self.sponsor_public_key,
76+
"proofs": self.proofs or None,
77+
"height": self.height
78+
})
7779

7880
@staticmethod
7981
def __account_from_data(data):

src/lto/transactions/revoke_association.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ def to_binary(self):
6464
raise Exception('Incorrect Version')
6565

6666
def to_json(self):
67-
return (crypto.merge_dicts({
68-
"id": self.id if self.id else "",
67+
return crypto.clean_dict({
68+
"id": self.id,
6969
"type": self.TYPE,
7070
"version": self.version,
7171
"sender": self.sender,
@@ -76,9 +76,12 @@ def to_json(self):
7676
"hash": base58.b58encode(crypto.str2bytes(self.anchor)),
7777
"timestamp": self.timestamp,
7878
"fee": self.tx_fee,
79-
"proofs": self.proofs,
80-
"height": self.height if self.height else ""
81-
}, self._sponsor_json()))
79+
"sponsor": self.sponsor,
80+
"sponsorKeyType": self.sponsor_key_type,
81+
"sponsorPublicKey": self.sponsor_public_key,
82+
"proofs": self.proofs or None,
83+
"height": self.height
84+
})
8285

8386
@staticmethod
8487
def from_data(data):

0 commit comments

Comments
 (0)