-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.py
35 lines (28 loc) · 1.14 KB
/
data.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from collections import defaultdict
from pysyncobj import SyncObj, SyncObjConf, replicated_sync
class Data(SyncObj):
def __init__(self, self_node, other_nodes, journal=None):
if journal is None:
self_node_norm = self_node.replace(':', '_')
journal = f'.journals/journal_{self_node_norm}.journal'
cfg = SyncObjConf(dynamicMembershipChange=True, journalFile=journal)
super().__init__(self_node, other_nodes, cfg)
self._balances = defaultdict(int)
@replicated_sync
def withdraw(self, account, amount):
if self._balances[account] >= amount:
self._balances[account] -= amount
return True
return False
@replicated_sync
def deposit(self, account, amount):
self._balances[account] += amount
@replicated_sync
def transfer(self, from_account, to_account, amount):
if self._balances[from_account] >= amount:
self._balances[from_account] -= amount
self._balances[to_account] += amount
return True
return False
def get_balance(self, account):
return self._balances[account]