-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathledger.py
More file actions
232 lines (176 loc) · 6.88 KB
/
ledger.py
File metadata and controls
232 lines (176 loc) · 6.88 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# for serializing / deserializing data
import pickle
# for signing
from Crypto.Signature import pkcs1_15
# hasher for signatures
from Crypto.Hash import SHA384
# hasher for blocks
from Crypto.Hash import SHA256
# for loading / exporting public and secret keys
from Crypto.PublicKey import RSA
# has functions for calculating balances of accounts
import accounting
# variable used globally for accessing ledger data
ledger = None
class _Post_Payload:
'''
Class for containing post data
'''
def __init__(self, block_id, poster_public_register, payee_public_register, amount, data):
self.block_id = block_id
self.poster_public_register = poster_public_register
self.transaction = {'payee': payee_public_register,'amount': amount} # payee will be a public wallet object
self.data = data
class _Post:
'''
Class for posts which contain a _Post_Payload object and a valid signature
'''
def __init__(self, post_payload, signature):
self.post_payload = post_payload
self.signature = signature
class _Work_Proof:
'''
Class for containing the proof of work done by a miner
work_proof is a SHA256 hash of the _Block_Payload which begins with 5 zero bytes, which is the definition of "work"
'''
def __init__(self, miner_id, miner_pk, proof):
self.miker_id = miner_id
self.miner_pk = miner_pk
self.work_proof = proof # signed hash of Block_Payload
class _Block:
'''
Class which represents a block on the chain.
The miner will create this object and keep updating the work string until the SHA256 hash produces 5 leading zeroes
A list of Blocks is the blockchain, which is maintained in the ledger file
'''
def __init__(self, block_id, previous_block_hash, posts, miner_id, miner_pk):
# block IDs are simply the integers in order
self.block_id = block_id
# the previous block hash is included for verifying that this block payload is truly the next in sequence
self.previous_block_hash = previous_block_hash
self.posts = posts
self.miner_id = miner_id
self.miner_pk = miner_pk
self.work = ''
def update_work(self, work_str):
self.work = work_str
class InsufficientFunds(Exception):
'Error which occurs if someone is trying to pay but cannot afford it. I.E. the poor and destitute'
def retrieve_ledger():
# open the ledger file
# read in the contents
# unpickle the ledger
with open('ledger', 'rb') as f:
l = pickle.load(f)
return l
def check_ledger():
# read through the blocks one at a time
# check the previous block hashes to this block's hash (except first block)
# check the work has 5 zeroes and hashes
pass
def save_ledger():
# pickle the ledger
# save it to file
with open('ledger', 'wb') as f:
pickle.dump(ledger, f)
def get_current_block_id(ledger):
# return the id of the the newest block + 1
return len(ledger['blocks']) + 1
def generate_post_payload(poster_public_register, payee_public_register, amount, data):
# block_id is always added so signatures are tied uniquely to this post, otherwise a signature could be re-used by duplicating an identical post
block_id = get_current_block_id(ledger)
post_payload = _Post_Payload(block_id, poster_public_register, payee_public_register, amount, data)
return post_payload
def add_post_to_pending_posts(new_post):
global ledger
# add the new post to the list of pending posts, which will be collected and added to the next mined block
ledger['pending-posts'].append(new_post)
save_ledger()
def get_pending_posts():
return ledger['pending-posts']
def get_prev_block_hash():
# get the previous block from the ledger
prev_block = ledger['blocks'][-1]
# convert it into bytes for hashing
block_bytes = pickle.dumps(prev_block)
# create new SHA256 hasher
hasher = SHA256.new()
# process the block byes
hasher.update(block_bytes)
# retrieve the hash
block_hash = hasher.digest()
return block_hash
def create_block():
# get the active block id number
block_id = get_current_block_id()
# get the hash of the previous block
prev_block_hash = get_prev_block_hash()
# get all pending posts
posts = get_pending_posts()
# create the _Block object
block_payload = _Block_Payload(block_id, previous_block_hash, posts)
return block_payload
def validate_post_payload(post_payload, signature):
# make sure the signature works
# make sure the payor has sufficient funds
payer = post_payload.poster_public_register['display-name']
amount = post_payload.transaction['amount']
accounting.load_accounts(get_blocks())
if not accounting.available_balance(payer, amount, get_pending_posts()):
raise InsufficientFunds
def submit_post(post_payload, signature):
# validate the post_payload data
validate_post_payload(post_payload, signature)
# create the bytes representation of the data
post_payload_bytes = pickle.dumps(post_payload)
# create a sha384 hasher
hasher = SHA384.new()
# process the post_payload data
hasher.update(post_payload_bytes)
# retrieve the public key from the signer
poster_pk = post_payload.poster_public_register['pk']
poster_pk = RSA.import_key(poster_pk)
# create a signer object
validator = pkcs1_15.new(poster_pk)
# call verify with hasher and signature to validate. No errors = valid
validator.verify(hasher, signature)
# create a new _Post object
new_post = _Post(post_payload, signature)
# add the post to pending posts
add_post_to_pending_posts(new_post)
def clear_pending_posts():
global ledger
ledger['pending-posts'] = []
def add_block_to_chain(solved_block):
# verify that the block is correct
# delete old pending posts
# note that this creates race conditions where new posts added after the miner began will be lost
# to mitigate the race condition, later on a lock on the pending posts list can be implemented
clear_pending_posts()
# append the block to the chain
ledger['blocks'].append(solved_block)
# save the updated ledger
save_ledger()
def create_block(miner_id, miner_pk):
global ledger
block_id = get_current_block_id(ledger)
previous_block_hash = get_prev_block_hash()
posts = ledger['pending-posts']
pk_exp = miner_pk.export_key()
new_block = _Block(block_id, previous_block_hash, posts, miner_id, pk_exp)
return new_block
def first_block():
global ledger
bid = 0
pbh = b''
posts = []
mid = b''
mpk = b''
return _Block(bid, pbh, posts, mid, mpk)
def get_blocks():
global ledger
return ledger['blocks']
# load ledger data to ledger global
ledger = retrieve_ledger()
# set up the accounts for later verification of sufficient funds when posts are submitted
accounting.load_accounts(get_blocks())