forked from hclivess/nado
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaccount_ops.py
More file actions
105 lines (78 loc) · 3.11 KB
/
account_ops.py
File metadata and controls
105 lines (78 loc) · 3.11 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import os
import msgpack
from data_ops import check_traversal, get_home
def get_account(address, create_on_error=True):
"""return all account information if account exists else create it"""
check_traversal(address)
account_path = f"{get_home()}/accounts/{address}/balance.dat"
if os.path.exists(account_path):
with open(account_path, "rb") as account_file:
account = msgpack.unpack(account_file)
return account
elif create_on_error:
return create_account(address)
else:
return None
def reflect_transaction(transaction, revert=False): #todo hardening
sender = transaction["sender"]
recipient = transaction["recipient"]
amount = transaction["amount"]
is_burn = False
if recipient == "burn":
is_burn = True
if revert:
change_balance(address=sender, amount=amount, is_burn=is_burn)
change_balance(address=recipient, amount=-amount)
else:
change_balance(address=sender, amount=-amount, is_burn=is_burn)
change_balance(address=recipient, amount=amount)
def change_balance(address: str, amount: int, is_burn=False):
check_traversal(address)
while True:
try:
account_message = get_account(address)
account_message["account_balance"] += amount
assert (account_message["account_balance"] >= 0), "Cannot change balance into negative"
if is_burn:
account_message["account_burned"] -= amount
assert (account_message["account_burned"] >= 0), "Cannot change burn into negative"
with open(f"{get_home()}/accounts/{address}/balance.dat", "wb") as account_file:
msgpack.pack(account_message, account_file)
except Exception as e:
raise ValueError(f"Failed setting balance for {address}: {e}")
break
return True
def increase_produced_count(address, amount, revert=False):
check_traversal(address)
account_path = f"{get_home()}/accounts/{address}/balance.dat"
account = get_account(address)
produced = account["account_produced"]
if revert:
account.update(account_produced=produced - amount)
else:
account.update(account_produced=produced + amount)
with open(account_path, "wb") as outfile:
msgpack.pack(account, outfile)
return produced
def create_account(address, balance=0, burned=0, produced=0):
"""create account if it does not exist"""
check_traversal(address)
account_path = f"{get_home()}/accounts/{address}/balance.dat"
if not os.path.exists(account_path):
os.makedirs(f"{get_home()}/accounts/{address}")
account = {
"account_balance": balance,
"account_burned": burned,
"account_address": address,
"account_produced": produced,
}
with open(account_path, "wb") as outfile:
msgpack.pack(account, outfile)
return account
else:
return get_account(address)
def get_account_value(address, key):
check_traversal(address)
account = get_account(address)
value = account[key]
return value