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

Implementing coin algorithms

parent 1b68ee60
No related branches found
No related tags found
No related merge requests found
......@@ -82,9 +82,12 @@ class MainWindow(QMainWindow, Ui_MainWindow):
self.label_account_name.setText(
"Current account : " +
self.core.current_account.name)
self.list_wallets.setModel(
WalletsListModel(
self.core.current_account))
for wallet in self.core.current_account.wallets:
wallet.refresh_coins()
wallets_list_model = WalletsListModel(self.core.current_account)
self.list_wallets.setModel(wallets_list_model)
self.tabs_communities.clear()
for community in self.core.current_account.communities:
......
......@@ -185,6 +185,7 @@ class ProcessConfigureCommunity(QDialog, Ui_CommunityConfigurationDialog):
menu.exec_(self.mapToGlobal(point))
def accept(self):
#TODO: Push wht only if changed
for wallet in self.account.wallets:
wallet.push_wht()
......
......@@ -13,7 +13,6 @@ from cutecoin.models.account.communities import Communities
from cutecoin.models.community import Community
from cutecoin.models.transaction import Transaction
from cutecoin.models.person import Person
from cutecoin.tools.exceptions import CommunityNotFoundError
class Account(object):
......@@ -113,11 +112,8 @@ class Account(object):
return sent
def quality(self, community):
if community in self.communities:
wallets = self.wallets.community_wallets(community.currency)
return community.person_quality(wallets, self.fingerprint())
else:
raise CommunityNotFoundError(self.keyid, community.amendment_id())
wallets = self.wallets.community_wallets(community.currency)
return community.person_quality(wallets, self.fingerprint())
def jsonify_contacts(self):
data = []
......
......@@ -7,6 +7,7 @@ Created on 2 févr. 2014
import re
import math
import logging
import ucoin
class Coin(object):
......@@ -15,34 +16,30 @@ class Coin(object):
A coin parsing a regex to read its value
'''
def __init__(self, issuer, number, base, power, origin):
def __init__(self, issuer, am_number, coin_number):
self.issuer = issuer
self.number = number
self.base = base
self.power = power
self.origin = origin
self.am_number = am_number
self.coin_number = coin_number
@classmethod
def from_id(cls, coin_id):
# Regex to parse the coin id
regex = "^([A-Z\d]{40})-(\d+)-(\d)-(\d+)-((A|F|D)-\d+)$"
regex = "^([A-Z\d]{40})-(\d+)-(\d+)$"
m = re.search(regex, coin_id)
issuer = m.group(1)
number = int(m.group(2))
base = int(m.group(3))
power = int(m.group(4))
origin = m.group(5)
return cls(issuer, number, base, power, origin)
am_number = int(m.group(2))
power = int(m.group(3))
return cls(issuer, am_number, power)
def __eq__(self, other):
return self.get_id() == other.get_id()
def value(self):
return self.base * math.pow(10, self.power)
def value(self, wallet):
amendment_request = ucoin.hdc.amendments.view.Self(self.am_number)
amendment = wallet.request(amendment_request)
return wallet.coin_algo(amendment, self.coin_number)
def get_id(self):
return self.issuer + "-" \
+ str(self.number) + "-" \
+ str(self.base) + "-" \
+ str(self.power) + "-" \
+ self.origin
+ str(self.am_number) + "-" \
+ str(self.coin_number)
'''
Created on 21 mai 2014
@author: inso
'''
import math
class Algorithm(object):
'''
classdocs
'''
def __init__(self, parameters):
'''
Constructor
'''
self.parameters = parameters
def coin_value(self, amendement, coin_number):
pass
class Base2Draft(Algorithm):
def __init__(self, parameters):
super.__init__(parameters)
def coin_value(self, amendment, coin_number):
coin_base = amendment['CoinBase']
coins_list = amendment['CoinsList']
i = 0
while coin_number > coins_list[i]:
coin_number -= coins_list[i]
i += 1
return math.pow(2, coin_base + i)
......@@ -9,10 +9,12 @@ import logging
import gnupg
import json
import time
import importlib
from cutecoin.models.coin import Coin
from cutecoin.models.coin import algorithms
from cutecoin.models.node import Node
from cutecoin.models.transaction import Transaction
from cutecoin.tools.exceptions import CommunityNotFoundError
from cutecoin.tools.exceptions import AlgorithmNotImplemented
class Wallet(object):
......@@ -33,6 +35,7 @@ class Wallet(object):
self.name = name
self.nodes = nodes
self.required_trusts = required_trusts
self.coin_algo = None
@classmethod
def create(cls, keyid, currency, node, required_trusts, name):
......@@ -62,9 +65,28 @@ class Wallet(object):
def value(self):
value = 0
for coin in self.coins:
value += coin.value()
value += coin.value(self.coin_algo)
return value
def refresh_coins(self):
coins_list_request = ucoin.hdc.coins.List(self.fingerprint())
data = self.request(coins_list_request)
for coin_data in data['coins']:
coin = Coin.from_id(coin_data)
self.coins.append(coin)
node_parameters = self.request(ucoin.registry.Parameters())
if 'CoinAlgo' in node_parameters:
coin_algo_name = node_parameters['CoinAlgo']
else:
coin_algo_name = 'Base2Draft'
try:
module = importlib.import_module("cutecoin.models.coin.algorithm")
self.coin_algo = module.getattr(module, coin_algo_name)
except AttributeError:
raise AlgorithmNotImplemented(coin_algo_name)
def transactions_received(self):
received = []
transactions_data = self.request(
......@@ -208,7 +230,7 @@ Hosters:
def jsonify(self):
return {'coins': self.jsonify_coins_list(),
'fingerprint': self.fingerprint(),
'keyid': self.keyid,
'name': self.name,
'currency': self.currency,
'nodes': self.jsonify_nodes_list()}
......@@ -49,19 +49,18 @@ class PersonNotFoundError(Error):
" not found ")
class CommunityNotFoundError(Error):
class AlgorithmNotImplemented(Error):
'''
Exception raised when looking for community in an account list
Exception raised when a coin uses an algorithm not known
'''
def __init__(self, keyid, amendmentid):
def __init__(self, algo_name):
'''
Constructor
'''
super(CommunityNotFoundError, self) \
.__init("Community with amendment " + amendmentid
+ " not found in account " + keyid)
super(AlgorithmNotImplemented, self) \
.__init("Algorithm " + algo_name + " not implemented.")
class KeyAlreadyUsed(Error):
......
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