Skip to content
Snippets Groups Projects
Commit 4ed36aa5 authored by inso's avatar inso
Browse files

New TX Lifecycle

- First remove the dirty patches to handle blockchain rollback from the refresh
parent 2777208d
No related branches found
No related tags found
No related merge requests found
...@@ -70,11 +70,13 @@ class Transfer(QObject): ...@@ -70,11 +70,13 @@ class Transfer(QObject):
return cls(None, Transfer.TO_SEND, metadata) return cls(None, Transfer.TO_SEND, metadata)
@classmethod @classmethod
def create_from_blockchain(cls, hash, state, metadata): def create_from_blockchain(cls, hash, metadata, block_number, time, nb_validation):
""" """
Create a new transfer sent from another cutecoin instance Create a new transfer sent from another cutecoin instance
""" """
return cls(hash, state, metadata) tx = cls(hash, Transfer.VALIDATING, metadata)
tx.check_registered(block_number, time, nb_validation)
return tx
@classmethod @classmethod
def load(cls, data): def load(cls, data):
...@@ -128,7 +130,7 @@ class Transfer(QObject): ...@@ -128,7 +130,7 @@ class Transfer(QObject):
return result return result
def check_registered(self, txhash, block_number, time, data_validation): def check_registered(self, txhash, block_number, time, nb_validation):
""" """
Check if the transfer was registered in a block. Check if the transfer was registered in a block.
Update the transfer state to VALIDATED if it was registered. Update the transfer state to VALIDATED if it was registered.
...@@ -136,6 +138,7 @@ class Transfer(QObject): ...@@ -136,6 +138,7 @@ class Transfer(QObject):
:param txhash: A transaction ucoinpy object found in the block :param txhash: A transaction ucoinpy object found in the block
:param int block_number: The block number checked :param int block_number: The block number checked
:param int time: The time of the block :param int time: The time of the block
:param int nb_validation: The number of validations needed to become VALIDATED
""" """
if txhash == self.hash: if txhash == self.hash:
if self.state == Transfer.AWAITING: if self.state == Transfer.AWAITING:
...@@ -143,7 +146,7 @@ class Transfer(QObject): ...@@ -143,7 +146,7 @@ class Transfer(QObject):
self._metadata['block'] = block_number self._metadata['block'] = block_number
self._metadata['time'] = time self._metadata['time'] = time
if self.state == Transfer.VALIDATING and \ if self.state == Transfer.VALIDATING and \
self._metadata['block'] - block_number >= data_validation: self._metadata['block'] - block_number >= nb_validation:
self.state = Transfer.VALIDATED self.state = Transfer.VALIDATED
def check_refused(self, time, block_time, mediantime_blocks): def check_refused(self, time, block_time, mediantime_blocks):
......
...@@ -100,9 +100,7 @@ class TxHistory(): ...@@ -100,9 +100,7 @@ class TxHistory():
""" """
receivers = [o.pubkey for o in tx.outputs receivers = [o.pubkey for o in tx.outputs
if o.pubkey != tx.issuers[0]] if o.pubkey != tx.issuers[0]]
nb_validations = community.network.fork_window((yield from community.members_pubkeys()))
state = yield from TxHistory._validation_state(community, block_number, current_block)
if len(receivers) == 0: if len(receivers) == 0:
receivers = [tx.issuers[0]] receivers = [tx.issuers[0]]
...@@ -131,13 +129,13 @@ class TxHistory(): ...@@ -131,13 +129,13 @@ class TxHistory():
if i == self.wallet.pubkey]) > 0 if i == self.wallet.pubkey]) > 0
in_outputs = len([o for o in tx.outputs in_outputs = len([o for o in tx.outputs
if o.pubkey == self.wallet.pubkey]) > 0 if o.pubkey == self.wallet.pubkey]) > 0
awaiting = [t for t in self._transfers watched = [t for t in self._transfers
if t.state in (Transfer.AWAITING, Transfer.VALIDATING)] if t.state in (Transfer.AWAITING, Transfer.VALIDATING)]
# We check if the transaction correspond to one we sent # We check if the transaction correspond to one we sent
# but not from this cutecoin Instance # but not from this cutecoin Instance
tx_hash = hashlib.sha1(tx.signed_raw().encode("ascii")).hexdigest().upper() tx_hash = hashlib.sha1(tx.signed_raw().encode("ascii")).hexdigest().upper()
if tx_hash not in [t.hash for t in awaiting]: if tx_hash not in [t.hash for t in watched]:
# If the wallet pubkey is in the issuers we sent this transaction # If the wallet pubkey is in the issuers we sent this transaction
if in_issuers: if in_issuers:
outputs = [o for o in tx.outputs outputs = [o for o in tx.outputs
...@@ -147,8 +145,9 @@ class TxHistory(): ...@@ -147,8 +145,9 @@ class TxHistory():
amount += o.amount amount += o.amount
metadata['amount'] = amount metadata['amount'] = amount
transfer = Transfer.create_from_blockchain(tx_hash, transfer = Transfer.create_from_blockchain(tx_hash,
state, metadata.copy(),
metadata.copy()) current_block['number'],
mediantime, nb_validations)
return transfer return transfer
# If we are not in the issuers, # If we are not in the issuers,
# maybe it we are in the recipients of this transaction # maybe it we are in the recipients of this transaction
...@@ -159,18 +158,16 @@ class TxHistory(): ...@@ -159,18 +158,16 @@ class TxHistory():
for o in outputs: for o in outputs:
amount += o.amount amount += o.amount
metadata['amount'] = amount metadata['amount'] = amount
transfer = Transfer.create_from_blockchain(tx_hash,
if tx_hash not in [t.hash for t in awaiting]: metadata.copy(),
transfer = Transfer.create_from_blockchain(tx_hash, current_block['number'],
state, mediantime, nb_validations)
metadata.copy()) received_list.append(transfer)
received_list.append(transfer) return transfer
return transfer
else: else:
transfer = [t for t in awaiting if t.hash == tx_hash][0] transfer = [t for t in watched if t.hash == tx_hash][0]
transfer.check_registered(tx_hash, current_block['number'], mediantime, transfer.check_registered(tx_hash, current_block['number'], mediantime, nb_validations)
community.network.fork_window(community.members_pubkeys()))
return None return None
@asyncio.coroutine @asyncio.coroutine
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment