Skip to content

Commit

Permalink
Merge pull request #72 from ltonetwork/tx-use-none
Browse files Browse the repository at this point in the history
Default properties as None instead of an empty string or zero.
  • Loading branch information
jasny authored Jul 5, 2022
2 parents 68cff61 + 4af7337 commit f08d34a
Show file tree
Hide file tree
Showing 25 changed files with 133 additions and 145 deletions.
8 changes: 6 additions & 2 deletions src/lto/crypto.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,18 @@ def key_type_id(key_type):
raise Exception('Key Type not supported')



def merge_dicts(x, y):
z = x.copy()
z.update(y)
z.update(y)
return z


def clean_dict(x):
return {k:v for (k,v) in x.items() if v is not None}


def compare_data_transaction(data, transaction):
for key in data:
key2 = inflection.underscore(key)
assert data[key] == getattr(transaction, key2)

41 changes: 17 additions & 24 deletions src/lto/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@
class Transaction(ABC):

def __init__(self):
self.tx_fee = 0
self.timestamp = 0
self.proofs = []
self.sender = ''
self.sender_public_key = ''
self.chain_id = ''
self.sponsor = ''
self.sponsor_public_key = ''
self.sender_key_type = 'ed25519'
self.sponsor_key_type = 'ed25519'
self.id = None
self.tx_fee = None
self.timestamp = None
self.sender = None
self.sender_key_type = None
self.sender_public_key = None
self.sponsor = None
self.sponsor_key_type = None
self.sponsor_public_key = None
self.chain_id = None
self.proofs = []
self.height = None

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

def sign_with(self, account):
if self.timestamp == 0:
if self.timestamp is None:
self.timestamp = int(time() * 1000)

if self.sender == '':
if self.sender is None:
self.sender = account.address
self.sender_key_type = account.key_type
self.sender_public_key = account.get_public_key()

self.chain_id = account.get_network()
self.sender_key_type = account.key_type

if self.chain_id is None:
self.chain_id = account.get_network()
self.proofs.append(account.sign(self.to_binary()))

def sponsor_with(self, sponsor_account):
Expand All @@ -56,11 +57,3 @@ def broadcast_to(self, node):
def to_json(self):
pass

def _sponsor_json(self):
if self.sponsor:
return {"sponsor": self.sponsor,
"sponsorPublicKey": self.sponsor_public_key,
"sponsorKeyType": self.sponsor_key_type}
else:
return {}

15 changes: 8 additions & 7 deletions src/lto/transactions/anchor.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,8 @@ def to_binary(self):
raise Exception('Incorrect Version')

def to_json(self):
return (crypto.merge_dicts(
{
"id": self.id if self.id else "",
return crypto.clean_dict({
"id": self.id,
"type": self.TYPE,
"version": self.version,
"sender": self.sender,
Expand All @@ -65,10 +64,12 @@ def to_json(self):
"fee": self.tx_fee,
"timestamp": self.timestamp,
"anchors": list(map(lambda anchor: base58.b58encode(crypto.str2bytes(anchor)), self.anchors)),
"proofs": self.proofs,
"height": self.height if self.height else ""
},
self._sponsor_json()))
"sponsor": self.sponsor,
"sponsorKeyType": self.sponsor_key_type,
"sponsorPublicKey": self.sponsor_public_key,
"proofs": self.proofs or None,
"height": self.height
})

@staticmethod
def from_data(data):
Expand Down
25 changes: 11 additions & 14 deletions src/lto/transactions/association.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,15 @@ class Association(Transaction):
TYPE = 16
DEFAULT_VERSION = 3

def __init__(self, recipient, association_type, anchor='', expires=0):
def __init__(self, recipient, association_type, anchor='', expires=None):
super().__init__()
self.recipient = recipient
self.association_type = association_type
self.anchor = anchor
self.tx_fee = self.DEFAULT_FEE
self.version = self.DEFAULT_VERSION

self.expires = expires
current = int(time() * 1000)
if self.expires != 0 and self.expires <= current:
raise Exception('Wring exipration date')


def __to_binary_v1(self):
if self.anchor:
Expand Down Expand Up @@ -58,7 +55,7 @@ def __to_binary_v3(self):
struct.pack(">Q", self.tx_fee) +
base58.b58decode(self.recipient) +
struct.pack(">i", self.association_type) +
struct.pack(">Q", self.expires) +
struct.pack(">Q", self.expires or 0) +
struct.pack(">H", len(crypto.str2bytes(self.anchor))) +
crypto.str2bytes(self.anchor))

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


def to_json(self):
tx = {
"id": self.id if self.id else "",
return crypto.clean_dict({
"id": self.id,
"type": self.TYPE,
"version": self.version,
"sender": self.sender,
Expand All @@ -86,12 +83,12 @@ def to_json(self):
"timestamp": self.timestamp,
"expires": self.expires if self.version != 1 else None,
"fee": self.tx_fee,
"proofs": self.proofs,
"height": self.height if self.height else ""
}
if self.version == 1:
tx.pop('expires')
return crypto.merge_dicts(tx, self._sponsor_json())
"sponsor": self.sponsor,
"sponsorKeyType": self.sponsor_key_type,
"sponsorPublicKey": self.sponsor_public_key,
"proofs": self.proofs or None,
"height": self.height
})


@staticmethod
Expand Down
13 changes: 8 additions & 5 deletions src/lto/transactions/burn.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,22 @@ def to_binary(self):
raise Exception('Incorrect Version')

def to_json(self):
return (crypto.merge_dicts({
return crypto.clean_dict({
"id": self.id,
"type": self.TYPE,
"version": self.version,
"id": self.id if self.id else "",
"sender": self.sender,
"senderKeyType": self.sender_key_type,
"senderPublicKey": self.sender_public_key,
"fee": self.tx_fee,
"timestamp": self.timestamp,
"amount": self.amount,
"proofs": self.proofs,
"height": self.height if self.height else ""
}, self._sponsor_json()))
"sponsor": self.sponsor,
"sponsorKeyType": self.sponsor_key_type,
"sponsorPublicKey": self.sponsor_public_key,
"proofs": self.proofs or None,
"height": self.height
})

@staticmethod
def from_data(data):
Expand Down
14 changes: 8 additions & 6 deletions src/lto/transactions/cancel_lease.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,22 @@ def to_binary(self):
raise Exception('Incorrect Version')

def to_json(self):
return(crypto.merge_dicts({
"id": self.id if self.id else "",
return crypto.clean_dict({
"id": self.id,
"type": self.TYPE,
"version": self.version,
"sender": self.sender,
"senderKeyType": self.sender_key_type,
"senderPublicKey": self.sender_public_key,
"fee": self.tx_fee,
"timestamp": self.timestamp,
"proofs": self.proofs,
"sponsor": self.sponsor,
"sponsorKeyType": self.sponsor_key_type,
"sponsorPublicKey": self.sponsor_public_key,
"proofs": self.proofs or None,
"leaseId": self.lease_id,
"height": self.height if self.height else ""
},
self._sponsor_json()))
"height": self.height
})

@staticmethod
def from_data(data):
Expand Down
13 changes: 8 additions & 5 deletions src/lto/transactions/cancel_sponsorship.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ def to_binary(self):
raise Exception('Incorrect Version')

def to_json(self):
return (crypto.merge_dicts({
"id": self.id if self.id else "",
return crypto.clean_dict({
"id": self.id,
"type": self.TYPE,
"version": self.version,
"senderKeyType": self.sender_key_type,
Expand All @@ -54,9 +54,12 @@ def to_json(self):
"senderPublicKey": self.sender_public_key,
"timestamp": self.timestamp,
"fee": self.tx_fee,
"proofs": self.proofs,
"height": self.height if self.height else ""
}, self._sponsor_json()))
"sponsor": self.sponsor,
"sponsorKeyType": self.sponsor_key_type,
"sponsorPublicKey": self.sponsor_public_key,
"proofs": self.proofs or None,
"height": self.height
})

@staticmethod
def from_data(data):
Expand Down
14 changes: 8 additions & 6 deletions src/lto/transactions/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ def to_binary(self):
raise Exception('Incorrect Version')

def to_json(self):
return (crypto.merge_dicts({
"id": self.id if self.id else "",
return crypto.clean_dict({
"id": self.id,
"type": self.TYPE,
"version": self.version,
"sender": self.sender,
Expand All @@ -62,10 +62,12 @@ def to_json(self):
"fee": self.tx_fee,
"timestamp": self.timestamp,
"data": list(map(lambda entry: entry.to_json(), self.data)),
"proofs": self.proofs,
"height": self.height if self.height else ""
},
self._sponsor_json()))
"sponsor": self.sponsor,
"sponsorKeyType": self.sponsor_key_type,
"sponsorPublicKey": self.sponsor_public_key,
"proofs": self.proofs or None,
"height": self.height
})

def data_as_dict(self):
dictionary = {}
Expand Down
14 changes: 8 additions & 6 deletions src/lto/transactions/lease.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ def to_binary(self):
raise Exception('Incorrect Version')

def to_json(self):
return (crypto.merge_dicts({
"id": self.id if self.id else "",
return crypto.clean_dict({
"id": self.id,
"type": self.TYPE,
"version": self.version,
"sender": self.sender,
Expand All @@ -59,10 +59,12 @@ def to_json(self):
"amount": self.amount,
"fee": self.tx_fee,
"timestamp": self.timestamp,
"proofs": self.proofs,
"height": self.height if self.height else ""
},
self._sponsor_json()))
"sponsor": self.sponsor,
"sponsorKeyType": self.sponsor_key_type,
"sponsorPublicKey": self.sponsor_public_key,
"proofs": self.proofs or None,
"height": self.height
})

@staticmethod
def from_data(data):
Expand Down
13 changes: 8 additions & 5 deletions src/lto/transactions/mass_transfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,20 +64,23 @@ def to_binary(self):


def to_json(self):
return (crypto.merge_dicts({
"id": self.id if self.id else "",
return crypto.clean_dict({
"id": self.id,
"type": self.TYPE,
"version": self.version,
"sender": self.sender,
"senderKeyType": self.sender_key_type,
"senderPublicKey": self.sender_public_key,
"fee": self.tx_fee,
"timestamp": self.timestamp,
"proofs": self.proofs,
"attachment": base58.b58encode(crypto.str2bytes(self.attachment)),
"transfers": self.transfers,
"height": self.height if self.height else ""
}, self._sponsor_json()))
"sponsor": self.sponsor,
"sponsorKeyType": self.sponsor_key_type,
"sponsorPublicKey": self.sponsor_public_key,
"proofs": self.proofs or None,
"height": self.height
})

@staticmethod
def from_data(data):
Expand Down
14 changes: 8 additions & 6 deletions src/lto/transactions/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ def __account_to_json(account):
return account

def to_json(self):
return (crypto.merge_dicts({
"id": self.id if self.id else "",
return crypto.clean_dict({
"id": self.id,
"type": self.TYPE,
"version": self.version,
"sender": self.sender,
Expand All @@ -70,10 +70,12 @@ def to_json(self):
"fee": self.tx_fee,
"timestamp": self.timestamp,
"accounts": list(map(self.__account_to_json, self.accounts)),
"proofs": self.proofs,
"height": self.height if self.height else ""
},
self._sponsor_json()))
"sponsor": self.sponsor,
"sponsorKeyType": self.sponsor_key_type,
"sponsorPublicKey": self.sponsor_public_key,
"proofs": self.proofs or None,
"height": self.height
})

@staticmethod
def __account_from_data(data):
Expand Down
13 changes: 8 additions & 5 deletions src/lto/transactions/revoke_association.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ def to_binary(self):
raise Exception('Incorrect Version')

def to_json(self):
return (crypto.merge_dicts({
"id": self.id if self.id else "",
return crypto.clean_dict({
"id": self.id,
"type": self.TYPE,
"version": self.version,
"sender": self.sender,
Expand All @@ -76,9 +76,12 @@ def to_json(self):
"hash": base58.b58encode(crypto.str2bytes(self.anchor)),
"timestamp": self.timestamp,
"fee": self.tx_fee,
"proofs": self.proofs,
"height": self.height if self.height else ""
}, self._sponsor_json()))
"sponsor": self.sponsor,
"sponsorKeyType": self.sponsor_key_type,
"sponsorPublicKey": self.sponsor_public_key,
"proofs": self.proofs or None,
"height": self.height
})

@staticmethod
def from_data(data):
Expand Down
Loading

0 comments on commit f08d34a

Please sign in to comment.