1- from .document import Document
1+ from .document import Document , MalformedDocumentError
22from .certification import SelfCertification , Certification
33from .membership import Membership
44from .transaction import Transaction
@@ -28,6 +28,8 @@ def from_str(cls, blockid):
2828 :param str blockid: The block id
2929 """
3030 data = blockid .split ("-" )
31+ if len (data ) != 2 :
32+ raise MalformedDocumentError ('BlockId' )
3133 number = int (data [0 ])
3234 sha_hash = data [1 ]
3335 return cls (number , sha_hash )
@@ -107,6 +109,28 @@ class Block(Document):
107109 re_certifications = re .compile ("Certifications:\n " )
108110 re_transactions = re .compile ("Transactions:\n " )
109111
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+
110134 Empty_Hash = "DA39A3EE5E6B4B0D3255BFEF95601890AFD80709"
111135
112136 def __init__ (self , version , currency , noonce , number , powmin , time ,
@@ -167,54 +191,54 @@ def from_signed_raw(cls, raw):
167191 lines = raw .splitlines (True )
168192 n = 0
169193
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
172196
173- Block .re_type . match ( lines [n ]). group ( 1 )
174- n = n + 1
197+ Block .parse_field ( "Type" , lines [n ])
198+ n += 1
175199
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
178202
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
181205
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
184208
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
187211
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
190214
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
193217
194218 ud = Block .re_universaldividend .match (lines [n ])
195219 if ud is not None :
196220 ud = int (ud .group (1 ))
197- n = n + 1
221+ n += 1
198222
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
201225
202226 prev_hash = None
203227 prev_issuer = None
204228 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
207231
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
210234
211235 parameters = None
212236 if number == 0 :
213237 parameters = Block .re_parameters .match (lines [n ]).groups ()
214- n = n + 1
238+ n += 1
215239
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
218242
219243 identities = []
220244 joiners = []
@@ -225,50 +249,50 @@ def from_signed_raw(cls, raw):
225249 transactions = []
226250
227251 if Block .re_identities .match (lines [n ]) is not None :
228- n = n + 1
252+ n += 1
229253 while Block .re_joiners .match (lines [n ]) is None :
230254 selfcert = SelfCertification .from_inline (version , currency , lines [n ])
231255 identities .append (selfcert )
232- n = n + 1
256+ n += 1
233257
234258 if Block .re_joiners .match (lines [n ]):
235- n = n + 1
259+ n += 1
236260 while Block .re_actives .match (lines [n ]) is None :
237261 membership = Membership .from_inline (version , currency , "IN" , lines [n ])
238262 joiners .append (membership )
239- n = n + 1
263+ n += 1
240264
241265 if Block .re_actives .match (lines [n ]):
242- n = n + 1
266+ n += 1
243267 while Block .re_leavers .match (lines [n ]) is None :
244268 membership = Membership .from_inline (version , currency , "IN" , lines [n ])
245269 actives .append (membership )
246- n = n + 1
270+ n += 1
247271
248272 if Block .re_leavers .match (lines [n ]):
249- n = n + 1
273+ n += 1
250274 while Block .re_excluded .match (lines [n ]) is None :
251275 membership = Membership .from_inline (version , currency , "OUT" , lines [n ])
252276 leavers .append (membership )
253- n = n + 1
277+ n += 1
254278
255279 if Block .re_excluded .match (lines [n ]):
256- n = n + 1
280+ n += 1
257281 while Block .re_certifications .match (lines [n ]) is None :
258282 membership = Block .re_exclusion .match (lines [n ]).group (1 )
259283 excluded .append (membership )
260- n = n + 1
284+ n += 1
261285
262286 if Block .re_certifications .match (lines [n ]):
263- n = n + 1
287+ n += 1
264288 while Block .re_transactions .match (lines [n ]) is None :
265289 certification = Certification .from_inline (version , currency ,
266290 prev_hash , lines [n ])
267291 certifications .append (certification )
268- n = n + 1
292+ n += 1
269293
270294 if Block .re_transactions .match (lines [n ]):
271- n = n + 1
295+ n += 1
272296 while not Block .re_signature .match (lines [n ]):
273297 tx_lines = ""
274298 header_data = Transaction .re_header .match (lines [n ])
@@ -277,14 +301,14 @@ def from_signed_raw(cls, raw):
277301 inputs_num = int (header_data .group (3 ))
278302 outputs_num = int (header_data .group (4 ))
279303 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
281305 for i in range (n , tx_max ):
282306 tx_lines += lines [n ]
283- n = n + 1
307+ n += 1
284308 transaction = Transaction .from_compact (currency , tx_lines )
285309 transactions .append (transaction )
286310
287- signature = Block .re_signature . match ( lines [n ]). group ( 1 )
311+ signature = Block .parse_field ( "Signature" , lines [n ])
288312
289313 return cls (version , currency , noonce , number , powmin , time ,
290314 mediantime , ud , issuer , prev_hash , prev_issuer ,
0 commit comments