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

Fixed bugs when the blockchain is empty

parent 4734cc71
No related branches found
No related tags found
No related merge requests found
......@@ -199,10 +199,15 @@ class Account(object):
self_ = Person.lookup(self.pubkey, community)
selfcert = self_.selfcert(community)
block = community.get_block()
block_hash = hashlib.sha1(block.signed_raw().encode("ascii")).hexdigest().upper()
try:
block = community.get_block()
block_hash = hashlib.sha1(block.signed_raw().encode("ascii")).hexdigest().upper()
block_number = block['number']
except ValueError as e:
block_number = 0
block_hash = "DA39A3EE5E6B4B0D3255BFEF95601890AFD80709"
membership = Membership(PROTOCOL_VERSION, community.currency,
selfcert.pubkey, block.number,
selfcert.pubkey, block_number,
block_hash, type, selfcert.uid,
selfcert.timestamp, None)
key = SigningKey(self.salt, password)
......
......@@ -69,10 +69,13 @@ class Community(object):
def dividend(self):
ud = self.request(bma.blockchain.UD)
block_number = ud['result']['blocks'][-1]
block = self.request(bma.blockchain.Block,
req_args={'number': block_number})
return block['dividend']
if len(ud['result']['blocks']) > 0:
block_number = ud['result']['blocks'][-1]
block = self.request(bma.blockchain.Block,
req_args={'number': block_number})
return block['dividend']
else:
return 1
def peering(self):
peers = []
......
......@@ -81,13 +81,22 @@ class Cache():
return self.tx_received
def refresh(self, community):
current_block = community.request(bma.blockchain.Current)
current_block = 0
try:
block_data = community.request(bma.blockchain.Current)
current_block = block_data['number']
except ValueError as e:
if '404' in str(e):
current_block = 0
else:
raise
with_tx = community.request(bma.blockchain.TX)
# We parse only blocks with transactions
parsed_blocks = reversed(range(self.latest_block + 1,
current_block['number'] + 1))
current_block + 1))
logging.debug("Refresh from {0} to {1}".format(self.latest_block + 1,
current_block['number'] + 1))
current_block + 1))
parsed_blocks = [n for n in parsed_blocks
if n in with_tx['result']['blocks']]
......@@ -110,13 +119,13 @@ class Cache():
if awaiting.compact() != tx.compact()]
self.tx_sent.append(tx)
if current_block['number'] > self.latest_block:
if current_block > self.latest_block:
self.available_sources = self.wallet.sources(community)
self.tx_sent = self.tx_sent[:50]
self.tx_received = self.tx_received[:50]
self.latest_block = current_block['number']
self.latest_block = current_block
class Wallet(object):
......
......@@ -25,21 +25,23 @@ class BlockchainWatcher(QObject):
self.account = account
self.community = community
self.exiting = False
self.last_block = self.community.request(bma.blockchain.Current)['number']
peering = self.community.request(bma.network.Peering)
self.last_block = peering['block'].split('-')[0]
@pyqtSlot()
def watch(self):
while not self.exiting:
time.sleep(10)
current_block = self.community.request(bma.blockchain.Current)
if self.last_block != current_block['number']:
peering = self.community.request(bma.network.Peering)
block_number = peering['block'].split('-')[0]
if self.last_block != block_number:
for w in self.account.wallets:
w.cache.refresh(self.community)
logging.debug("New block, {0} mined in {1}".format(current_block['number'],
logging.debug("New block, {0} mined in {1}".format(block_number,
self.community.currency))
self.new_block_mined.emit(current_block['number'])
self.last_block = current_block['number']
self.new_block_mined.emit(block_number)
self.last_block = block_number
new_block_mined = pyqtSignal(int)
......@@ -90,7 +92,8 @@ class CurrencyTabWidget(QWidget, Ui_CurrencyTabWidget):
self.tabs_account.addTab(self.tab_community,
QIcon(':/icons/community_icon'),
"Community")
block_number = self.community.request(bma.blockchain.Current)['number']
peering = self.community.request(bma.network.Peering)
block_number = peering['block'].split('-')[0]
self.status_label.setText("Connected : Block {0}"
.format(block_number))
......@@ -167,7 +170,8 @@ class CurrencyTabWidget(QWidget, Ui_CurrencyTabWidget):
self.app.save(self.app.current_account)
def showEvent(self, event):
block_number = self.community.request(bma.blockchain.Current)['number']
peering = self.community.request(bma.network.Peering)
block_number = peering['block'].split('-')[0]
self.status_label.setText("Connected : Block {0}"
.format(block_number))
......
......@@ -50,6 +50,7 @@ class StepPageInit(Step):
self.config_dialog.list_communities.setModel(model)
nb_wallets = len(self.config_dialog.account.wallets)
self.config_dialog.spinbox_wallets.setValue(nb_wallets)
self.config_dialog.password_asker = PasswordAskerDialog(self.config_dialog.account)
self.config_dialog.button_previous.setEnabled(False)
self.config_dialog.button_next.setEnabled(False)
......
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