1
- from .document import Document
1
+ from .document import Document , MalformedDocumentError
2
2
from .certification import SelfCertification , Certification
3
3
from .membership import Membership
4
4
from .transaction import Transaction
@@ -28,6 +28,8 @@ def from_str(cls, blockid):
28
28
:param str blockid: The block id
29
29
"""
30
30
data = blockid .split ("-" )
31
+ if len (data ) != 2 :
32
+ raise MalformedDocumentError ('BlockId' )
31
33
number = int (data [0 ])
32
34
sha_hash = data [1 ]
33
35
return cls (number , sha_hash )
@@ -107,6 +109,28 @@ class Block(Document):
107
109
re_certifications = re .compile ("Certifications:\n " )
108
110
re_transactions = re .compile ("Transactions:\n " )
109
111
112
+ fields_parsers = {** Document .fields_parsers , ** {
113
+ 'Type' : re_type ,
114
+ 'Noonce' : re_noonce ,
115
+ 'Number' : re_number ,
116
+ 'PoWMin' : re_powmin ,
117
+ 'Time' : re_time ,
118
+ 'MedianTime' : re_mediantime ,
119
+ 'UD' : re_universaldividend ,
120
+ 'Issuer' : re_issuer ,
121
+ 'PreviousIssuer' : re_previousissuer ,
122
+ 'PreviousHash' : re_previoushash ,
123
+ 'Parameters' : re_parameters ,
124
+ 'MembersCount' : re_memberscount ,
125
+ 'Identities' : re_identities ,
126
+ 'Joiners' : re_joiners ,
127
+ 'Actives' : re_actives ,
128
+ 'Leavers' : re_leavers ,
129
+ 'Certifications' : re_certifications ,
130
+ 'Transactions' : re_transactions
131
+ }
132
+ }
133
+
110
134
Empty_Hash = "DA39A3EE5E6B4B0D3255BFEF95601890AFD80709"
111
135
112
136
def __init__ (self , version , currency , noonce , number , powmin , time ,
@@ -167,54 +191,54 @@ def from_signed_raw(cls, raw):
167
191
lines = raw .splitlines (True )
168
192
n = 0
169
193
170
- version = int (Block .re_version . match ( lines [n ]). group ( 1 ))
171
- n = n + 1
194
+ version = int (Block .parse_field ( "Version" , lines [n ]))
195
+ n += 1
172
196
173
- Block .re_type . match ( lines [n ]). group ( 1 )
174
- n = n + 1
197
+ Block .parse_field ( "Type" , lines [n ])
198
+ n += 1
175
199
176
- currency = Block .re_currency . match ( lines [n ]). group ( 1 )
177
- n = n + 1
200
+ currency = Block .parse_field ( "Currency" , lines [n ])
201
+ n += 1
178
202
179
- noonce = int (Block .re_noonce . match ( lines [n ]). group ( 1 ))
180
- n = n + 1
203
+ noonce = int (Block .parse_field ( "Noonce" , lines [n ]))
204
+ n += 1
181
205
182
- number = int (Block .re_number . match ( lines [n ]). group ( 1 ))
183
- n = n + 1
206
+ number = int (Block .parse_field ( "Number" , lines [n ]))
207
+ n += 1
184
208
185
- powmin = int (Block .re_powmin . match ( lines [n ]). group ( 1 ))
186
- n = n + 1
209
+ powmin = int (Block .parse_field ( "PoWMin" , lines [n ]))
210
+ n += 1
187
211
188
- time = int (Block .re_time . match ( lines [n ]). group ( 1 ))
189
- n = n + 1
212
+ time = int (Block .parse_field ( "Time" , lines [n ]))
213
+ n += 1
190
214
191
- mediantime = int (Block .re_mediantime . match ( lines [n ]). group ( 1 ))
192
- n = n + 1
215
+ mediantime = int (Block .parse_field ( "MedianTime" , lines [n ]))
216
+ n += 1
193
217
194
218
ud = Block .re_universaldividend .match (lines [n ])
195
219
if ud is not None :
196
220
ud = int (ud .group (1 ))
197
- n = n + 1
221
+ n += 1
198
222
199
- issuer = Block .re_issuer . match ( lines [n ]). group ( 1 )
200
- n = n + 1
223
+ issuer = Block .parse_field ( "Issuer" , lines [n ])
224
+ n += 1
201
225
202
226
prev_hash = None
203
227
prev_issuer = None
204
228
if number > 0 :
205
- prev_hash = Block .re_previoushash . match ( lines [n ]). group ( 1 )
206
- n = n + 1
229
+ prev_hash = Block .parse_field ( "PreviousHash" , lines [n ])
230
+ n += 1
207
231
208
- prev_issuer = Block .re_previousissuer . match ( lines [n ]). group ( 1 )
209
- n = n + 1
232
+ prev_issuer = Block .parse_field ( "PreviousIssuer" , lines [n ])
233
+ n += 1
210
234
211
235
parameters = None
212
236
if number == 0 :
213
237
parameters = Block .re_parameters .match (lines [n ]).groups ()
214
- n = n + 1
238
+ n += 1
215
239
216
- members_count = int (Block .re_memberscount . match ( lines [n ]). group ( 1 ))
217
- n = n + 1
240
+ members_count = int (Block .parse_field ( "MembersCount" , lines [n ]))
241
+ n += 1
218
242
219
243
identities = []
220
244
joiners = []
@@ -225,50 +249,50 @@ def from_signed_raw(cls, raw):
225
249
transactions = []
226
250
227
251
if Block .re_identities .match (lines [n ]) is not None :
228
- n = n + 1
252
+ n += 1
229
253
while Block .re_joiners .match (lines [n ]) is None :
230
254
selfcert = SelfCertification .from_inline (version , currency , lines [n ])
231
255
identities .append (selfcert )
232
- n = n + 1
256
+ n += 1
233
257
234
258
if Block .re_joiners .match (lines [n ]):
235
- n = n + 1
259
+ n += 1
236
260
while Block .re_actives .match (lines [n ]) is None :
237
261
membership = Membership .from_inline (version , currency , "IN" , lines [n ])
238
262
joiners .append (membership )
239
- n = n + 1
263
+ n += 1
240
264
241
265
if Block .re_actives .match (lines [n ]):
242
- n = n + 1
266
+ n += 1
243
267
while Block .re_leavers .match (lines [n ]) is None :
244
268
membership = Membership .from_inline (version , currency , "IN" , lines [n ])
245
269
actives .append (membership )
246
- n = n + 1
270
+ n += 1
247
271
248
272
if Block .re_leavers .match (lines [n ]):
249
- n = n + 1
273
+ n += 1
250
274
while Block .re_excluded .match (lines [n ]) is None :
251
275
membership = Membership .from_inline (version , currency , "OUT" , lines [n ])
252
276
leavers .append (membership )
253
- n = n + 1
277
+ n += 1
254
278
255
279
if Block .re_excluded .match (lines [n ]):
256
- n = n + 1
280
+ n += 1
257
281
while Block .re_certifications .match (lines [n ]) is None :
258
282
membership = Block .re_exclusion .match (lines [n ]).group (1 )
259
283
excluded .append (membership )
260
- n = n + 1
284
+ n += 1
261
285
262
286
if Block .re_certifications .match (lines [n ]):
263
- n = n + 1
287
+ n += 1
264
288
while Block .re_transactions .match (lines [n ]) is None :
265
289
certification = Certification .from_inline (version , currency ,
266
290
prev_hash , lines [n ])
267
291
certifications .append (certification )
268
- n = n + 1
292
+ n += 1
269
293
270
294
if Block .re_transactions .match (lines [n ]):
271
- n = n + 1
295
+ n += 1
272
296
while not Block .re_signature .match (lines [n ]):
273
297
tx_lines = ""
274
298
header_data = Transaction .re_header .match (lines [n ])
@@ -277,14 +301,14 @@ def from_signed_raw(cls, raw):
277
301
inputs_num = int (header_data .group (3 ))
278
302
outputs_num = int (header_data .group (4 ))
279
303
has_comment = int (header_data .group (5 ))
280
- tx_max = n + issuers_num * 2 + inputs_num + outputs_num + has_comment + 1
304
+ tx_max = n + issuers_num * 2 + inputs_num + outputs_num + has_comment + 1
281
305
for i in range (n , tx_max ):
282
306
tx_lines += lines [n ]
283
- n = n + 1
307
+ n += 1
284
308
transaction = Transaction .from_compact (currency , tx_lines )
285
309
transactions .append (transaction )
286
310
287
- signature = Block .re_signature . match ( lines [n ]). group ( 1 )
311
+ signature = Block .parse_field ( "Signature" , lines [n ])
288
312
289
313
return cls (version , currency , noonce , number , powmin , time ,
290
314
mediantime , ud , issuer , prev_hash , prev_issuer ,
0 commit comments