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

Fixing cache not managing multiple currencies

parent 9b26ea0f
No related branches found
No related tags found
No related merge requests found
...@@ -92,9 +92,9 @@ class Application(object): ...@@ -92,9 +92,9 @@ class Application(object):
if os.path.exists(wallet_path): if os.path.exists(wallet_path):
json_data = open(wallet_path, 'r') json_data = open(wallet_path, 'r')
data = json.load(json_data) data = json.load(json_data)
wallet.cache.load_from_json(data) wallet.load_caches(data)
for community in account.communities: for community in account.communities:
wallet.cache.refresh(community) wallet.refresh_cache(community)
def save(self, account): def save(self, account):
with open(config.parameters['data'], 'w') as outfile: with open(config.parameters['data'], 'w') as outfile:
...@@ -114,7 +114,7 @@ class Application(object): ...@@ -114,7 +114,7 @@ class Application(object):
wallet_path = os.path.join(config.parameters['home'], wallet_path = os.path.join(config.parameters['home'],
account.name, '__cache__', wallet.pubkey) account.name, '__cache__', wallet.pubkey)
with open(wallet_path, 'w') as outfile: with open(wallet_path, 'w') as outfile:
json.dump(wallet.cache.jsonify(), outfile, indent=4, sort_keys=True) json.dump(wallet.jsonify_caches(), outfile, indent=4, sort_keys=True)
def import_account(self, file, name): def import_account(self, file, name):
with tarfile.open(file, "r") as tar: with tarfile.open(file, "r") as tar:
......
...@@ -17,6 +17,7 @@ class Cache(): ...@@ -17,6 +17,7 @@ class Cache():
def __init__(self, wallet): def __init__(self, wallet):
self.latest_block = 0 self.latest_block = 0
self.wallet = wallet self.wallet = wallet
self.tx_sent = [] self.tx_sent = []
self.awaiting_tx = [] self.awaiting_tx = []
self.tx_received = [] self.tx_received = []
...@@ -131,7 +132,7 @@ class Wallet(object): ...@@ -131,7 +132,7 @@ class Wallet(object):
self.walletid = walletid self.walletid = walletid
self.pubkey = pubkey self.pubkey = pubkey
self.name = name self.name = name
self.cache = Cache(self) self.caches = {}
@classmethod @classmethod
def create(cls, walletid, salt, password, name): def create(cls, walletid, salt, password, name):
...@@ -151,6 +152,20 @@ class Wallet(object): ...@@ -151,6 +152,20 @@ class Wallet(object):
def __eq__(self, other): def __eq__(self, other):
return (self.keyid == other.keyid) return (self.keyid == other.keyid)
def load_caches(self, json_data):
for currency in json_data:
self.caches[currency] = Cache(self)
self.caches[currency].load_from_json(json_data[currency])
def jsonify_caches(self):
for currency in self.caches:
return {currency: self.caches[currency].jsonify()}
def refresh_cache(self, community):
if community.currency not in self.caches:
self.caches[community.currency] = Cache(self)
self.caches[community.currency].refresh(community)
def check_password(self, salt, password): def check_password(self, salt, password):
key = None key = None
if self.walletid == 0: if self.walletid == 0:
...@@ -174,16 +189,17 @@ class Wallet(object): ...@@ -174,16 +189,17 @@ class Wallet(object):
def tx_inputs(self, amount, community): def tx_inputs(self, amount, community):
value = 0 value = 0
inputs = [] inputs = []
cache = self.caches[community.currency]
logging.debug("Available inputs : {0}".format(self.cache.available_sources)) logging.debug("Available inputs : {0}".format(cache.available_sources))
buf_inputs = list(self.cache.available_sources) buf_inputs = list(cache.available_sources)
for s in self.cache.available_sources: for s in cache.available_sources:
value += s.amount value += s.amount
s.index = 0 s.index = 0
inputs.append(s) inputs.append(s)
buf_inputs.remove(s) buf_inputs.remove(s)
if value >= amount: if value >= amount:
self.cache.available_sources = buf_inputs cache.available_sources = buf_inputs
return inputs return inputs
raise NotEnoughMoneyError(value, community.currency, raise NotEnoughMoneyError(value, community.currency,
...@@ -226,7 +242,7 @@ class Wallet(object): ...@@ -226,7 +242,7 @@ class Wallet(object):
try: try:
community.broadcast(bma.tx.Process, community.broadcast(bma.tx.Process,
post_args={'transaction': tx.signed_raw()}) post_args={'transaction': tx.signed_raw()})
self.cache.awaiting_tx.append(tx) self.caches[community.currency].awaiting_tx.append(tx)
except: except:
raise raise
...@@ -239,13 +255,13 @@ class Wallet(object): ...@@ -239,13 +255,13 @@ class Wallet(object):
return tx return tx
def transactions_awaiting(self, community): def transactions_awaiting(self, community):
return self.cache.awaiting(community) return self.caches[community.currency].awaiting(community)
def transactions_sent(self, community): def transactions_sent(self, community):
return self.cache.latest_sent(community) return self.caches[community.currency].latest_sent(community)
def transactions_received(self, community): def transactions_received(self, community):
return self.cache.latest_received(community) return self.caches[community.currency].latest_received(community)
def get_text(self, community): def get_text(self, community):
return "%s : \n \ return "%s : \n \
......
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