-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaccounting.py
More file actions
76 lines (57 loc) · 2.1 KB
/
accounting.py
File metadata and controls
76 lines (57 loc) · 2.1 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
'''
Accounting simply crawls through the blocks and sums up mining fees and transactions.
It assumes that all blocks are valid. Checking validity is the responsibility of ledger
'''
import pickle
acts = None
class NonExistentAccountError(Exception):
'If an account does not exist'
def _update_act_balance(disp, amt):
global acts
for act in acts:
if disp in act.values():
act['balance'] += amt
def available_balance(account, amount, pending_posts):
global acts
avail = -1.0
# get available balance from blocks
for act in acts:
if account in act.values():
avail = act['balance']
# subtract pending transactions from available funds so you can't overdraft in a single block
for post in pending_posts:
p = post.post_payload
payer = p.poster_public_register['display-name']
if account == payer:
avail -= p.transaction['amount']
# the only way avail can be None is if no account name was found
if avail is None:
raise NonExistentAccountError
return avail >= amount
def load_accounts(blocks):
global acts
pub_reg = 'public_register'
with open(pub_reg, 'rb') as f:
public_register = pickle.load(f)
# ignore the first block, which is there only to bootstrap the rest
blocks = blocks[1:]
acts = public_register['accounts']
for block in blocks:
# miner gets awarded 1 as a reward
miner = block.miner_id
_update_act_balance(miner, 1.0)
for post in block.posts:
payload = post.post_payload
try:
payee = payload.transaction['payee']['display-name']
# not all posts contain transactions, which is fine
except TypeError:
payee = ''
if not payee == '':
pending_amt = payload.transaction['amount']
_update_act_balance(payee, pending_amt)
payor = payload.poster_public_register['display-name']
_update_act_balance(payor, -1.0 * pending_amt)
def get_accounts():
global acts
return acts