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

Refactored wallet refreshing

parent 78602c70
Branches
Tags
No related merge requests found
...@@ -47,8 +47,6 @@ class Cache(): ...@@ -47,8 +47,6 @@ class Cache():
:return: The cache as a dict in json format :return: The cache as a dict in json format
''' '''
#TODO: Change the requests caching because hashed keys are different
#from a cutecoin run to another
data = {k: self.data[k] for k in self.data.keys() data = {k: self.data[k] for k in self.data.keys()
if k[0] in Cache._saved_requests} if k[0] in Cache._saved_requests}
entries = [] entries = []
......
...@@ -60,47 +60,23 @@ class Cache(): ...@@ -60,47 +60,23 @@ class Cache():
def transfers(self): def transfers(self):
return [t for t in self._transfers if t.state != Transfer.DROPPED] return [t for t in self._transfers if t.state != Transfer.DROPPED]
#TODO: Refactor to reduce this method size and split it to more methods def _parse_transaction(self, community, block_number, mediantime, tx):
def refresh(self, community):
current_block = 0
try:
block_data = community.current_blockid()
current_block = block_data['number']
# Lets look if transactions took too long to be validated
awaiting = [t for t in self._transfers
if t.state == Transfer.AWAITING]
with_tx = community.request(bma.blockchain.TX)
# We parse only blocks with transactions receivers = [o.pubkey for o in tx.outputs
parsed_blocks = reversed(range(self.latest_block + 1, if o.pubkey != tx.issuers[0]]
current_block + 1))
logging.debug("Refresh from {0} to {1}".format(self.latest_block + 1,
current_block + 1))
parsed_blocks = [n for n in parsed_blocks
if n in with_tx['result']['blocks']]
self.wallet.refresh_progressed.emit(self.latest_block, current_block)
for block_number in parsed_blocks:
block = community.request(bma.blockchain.Block,
req_args={'number': block_number})
signed_raw = "{0}{1}\n".format(block['raw'],
block['signature'])
try:
block_doc = Block.from_signed_raw(signed_raw)
except:
logging.debug("Error in {0}".format(block_number))
raise
for tx in block_doc.transactions:
metadata = {'block': block_number, metadata = {'block': block_number,
'time': block_doc.mediantime, 'time': mediantime,
'comment': tx.comment, 'comment': tx.comment,
'issuer': tx.issuers[0]} 'issuer': tx.issuers[0],
receivers = [o.pubkey for o in tx.outputs 'receiver': receivers[0]}
if o.pubkey != metadata['issuer']]
metadata['receiver'] = receivers[0]
in_issuers = len([i for i in tx.issuers in_issuers = len([i for i in tx.issuers
if i == self.wallet.pubkey]) > 0 if i == self.wallet.pubkey]) > 0
in_outputs = len([o for o in tx.outputs
if o.pubkey == self.wallet.pubkey]) > 0
# 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
if o.pubkey != self.wallet.pubkey] if o.pubkey != self.wallet.pubkey]
...@@ -111,31 +87,75 @@ class Cache(): ...@@ -111,31 +87,75 @@ class Cache():
awaiting = [t for t in self._transfers awaiting = [t for t in self._transfers
if t.state == Transfer.AWAITING] if t.state == Transfer.AWAITING]
awaiting_docs = [t.txdoc.signed_raw() for t in awaiting] # We check if the transaction correspond to one we sent
if tx.signed_raw() not in awaiting_docs: if tx.signed_raw() not in [t.txdoc.signed_raw() for t in awaiting]:
transfer = Transfer.create_validated(tx, transfer = Transfer.create_validated(tx,
metadata.copy()) metadata.copy())
self._transfers.append(transfer) self._transfers.append(transfer)
else: # If we are not in the issuers,
for transfer in awaiting: # maybe it we are in the recipients of this transaction
transfer.check_registered(tx, block_number, elif in_outputs:
block_doc.mediantime)
else:
outputs = [o for o in tx.outputs outputs = [o for o in tx.outputs
if o.pubkey == self.wallet.pubkey] if o.pubkey == self.wallet.pubkey]
if len(outputs) > 0:
amount = 0 amount = 0
for o in outputs: for o in outputs:
amount += o.amount amount += o.amount
metadata['amount'] = amount metadata['amount'] = amount
self._transfers.append(Received(tx, self._transfers.append(Received(tx,
metadata.copy())) metadata.copy()))
logging.debug("Receivers : {0}".format(self.wallet.receivers(self.wallet.refresh_progressed)))
def _parse_block(self, community, block_number):
block = community.request(bma.blockchain.Block,
req_args={'number': block_number})
signed_raw = "{0}{1}\n".format(block['raw'],
block['signature'])
try:
block_doc = Block.from_signed_raw(signed_raw)
except:
logging.debug("Error in {0}".format(block_number))
raise
for tx in block_doc.transactions:
self._parse_transaction(community, tx, block_number, block_doc.mediantime)
awaiting = [t for t in self._transfers
if t.state == Transfer.AWAITING]
# After we checked all transactions, we check if
# sent transactions still waiting for validation
# have to be considered refused
for transfer in awaiting:
transfer.check_registered(tx, block_number,
block_doc.mediantime)
#TODO: Refactor to reduce this method size and split it to more methods
def refresh(self, community):
current_block = 0
try:
block_data = community.current_blockid()
current_block = block_data['number']
# Lets look if transactions took too long to be validated
awaiting = [t for t in self._transfers
if t.state == Transfer.AWAITING]
with_tx = community.request(bma.blockchain.TX)
# We parse only blocks with transactions
parsed_blocks = reversed(range(self.latest_block + 1,
current_block + 1))
logging.debug("Refresh from {0} to {1}".format(self.latest_block + 1,
current_block + 1))
parsed_blocks = [n for n in parsed_blocks
if n in with_tx['result']['blocks']]
self.wallet.refresh_progressed.emit(self.latest_block, current_block)
for block_number in parsed_blocks:
self._parse_block(community, block_number)
self.wallet.refresh_progressed.emit(current_block - block_number, self.wallet.refresh_progressed.emit(current_block - block_number,
current_block - self.latest_block) current_block - self.latest_block)
if current_block > self.latest_block: if current_block > self.latest_block:
self.available_sources = self.wallet.sources(community) self.available_sources = self.wallet.sources(community)
for transfer in awaiting: for transfer in awaiting:
transfer.check_refused(current_block) transfer.check_refused(current_block)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment