Skip to content

Commit

Permalink
New exception MalformedErrorDocument
Browse files Browse the repository at this point in the history
  • Loading branch information
Insoleet committed Jan 30, 2016
1 parent b964c9d commit e27d101
Show file tree
Hide file tree
Showing 10 changed files with 202 additions and 229 deletions.
38 changes: 0 additions & 38 deletions _ucoinpy_test/documents/test_status.py

This file was deleted.

2 changes: 1 addition & 1 deletion ucoinpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
MANAGED_API=["BASIC_MERKLED_API"]

__author__ = 'Caner Candan & inso'
__version__ = '0.14.2'
__version__ = '0.14.3'
__nonsense__ = 'uCoin'

from . import api, documents, key
3 changes: 1 addition & 2 deletions ucoinpy/documents/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@
from .certification import SelfCertification, Certification
from .membership import Membership
from .peer import Endpoint, BMAEndpoint, UnknownEndpoint, Peer
from .status import Status
from .transaction import SimpleTransaction, Transaction
from .document import Document
from .document import Document, MalformedDocumentError
110 changes: 67 additions & 43 deletions ucoinpy/documents/block.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .document import Document
from .document import Document, MalformedDocumentError
from .certification import SelfCertification, Certification
from .membership import Membership
from .transaction import Transaction
Expand Down Expand Up @@ -28,6 +28,8 @@ def from_str(cls, blockid):
:param str blockid: The block id
"""
data = blockid.split("-")
if len(data) != 2:
raise MalformedDocumentError('BlockId')
number = int(data[0])
sha_hash = data[1]
return cls(number, sha_hash)
Expand Down Expand Up @@ -107,6 +109,28 @@ class Block(Document):
re_certifications = re.compile("Certifications:\n")
re_transactions = re.compile("Transactions:\n")

fields_parsers = {**Document.fields_parsers, **{
'Type': re_type,
'Noonce': re_noonce,
'Number': re_number,
'PoWMin': re_powmin,
'Time': re_time,
'MedianTime': re_mediantime,
'UD': re_universaldividend,
'Issuer': re_issuer,
'PreviousIssuer': re_previousissuer,
'PreviousHash': re_previoushash,
'Parameters': re_parameters,
'MembersCount': re_memberscount,
'Identities': re_identities,
'Joiners': re_joiners,
'Actives': re_actives,
'Leavers': re_leavers,
'Certifications': re_certifications,
'Transactions': re_transactions
}
}

Empty_Hash = "DA39A3EE5E6B4B0D3255BFEF95601890AFD80709"

def __init__(self, version, currency, noonce, number, powmin, time,
Expand Down Expand Up @@ -167,54 +191,54 @@ def from_signed_raw(cls, raw):
lines = raw.splitlines(True)
n = 0

version = int(Block.re_version.match(lines[n]).group(1))
n = n + 1
version = int(Block.parse_field("Version", lines[n]))
n += 1

Block.re_type.match(lines[n]).group(1)
n = n + 1
Block.parse_field("Type", lines[n])
n += 1

currency = Block.re_currency.match(lines[n]).group(1)
n = n + 1
currency = Block.parse_field("Currency", lines[n])
n += 1

noonce = int(Block.re_noonce.match(lines[n]).group(1))
n = n + 1
noonce = int(Block.parse_field("Noonce", lines[n]))
n += 1

number = int(Block.re_number.match(lines[n]).group(1))
n = n + 1
number = int(Block.parse_field("Number", lines[n]))
n += 1

powmin = int(Block.re_powmin.match(lines[n]).group(1))
n = n + 1
powmin = int(Block.parse_field("PoWMin", lines[n]))
n += 1

time = int(Block.re_time.match(lines[n]).group(1))
n = n + 1
time = int(Block.parse_field("Time", lines[n]))
n += 1

mediantime = int(Block.re_mediantime.match(lines[n]).group(1))
n = n + 1
mediantime = int(Block.parse_field("MedianTime", lines[n]))
n += 1

ud = Block.re_universaldividend.match(lines[n])
if ud is not None:
ud = int(ud.group(1))
n = n + 1
n += 1

issuer = Block.re_issuer.match(lines[n]).group(1)
n = n + 1
issuer = Block.parse_field("Issuer", lines[n])
n += 1

prev_hash = None
prev_issuer = None
if number > 0:
prev_hash = Block.re_previoushash.match(lines[n]).group(1)
n = n + 1
prev_hash = Block.parse_field("PreviousHash", lines[n])
n += 1

prev_issuer = Block.re_previousissuer.match(lines[n]).group(1)
n = n + 1
prev_issuer = Block.parse_field("PreviousIssuer", lines[n])
n += 1

parameters = None
if number == 0:
parameters = Block.re_parameters.match(lines[n]).groups()
n = n + 1
n += 1

members_count = int(Block.re_memberscount.match(lines[n]).group(1))
n = n + 1
members_count = int(Block.parse_field("MembersCount", lines[n]))
n += 1

identities = []
joiners = []
Expand All @@ -225,50 +249,50 @@ def from_signed_raw(cls, raw):
transactions = []

if Block.re_identities.match(lines[n]) is not None:
n = n + 1
n += 1
while Block.re_joiners.match(lines[n]) is None:
selfcert = SelfCertification.from_inline(version, currency, lines[n])
identities.append(selfcert)
n = n + 1
n += 1

if Block.re_joiners.match(lines[n]):
n = n + 1
n += 1
while Block.re_actives.match(lines[n]) is None:
membership = Membership.from_inline(version, currency, "IN", lines[n])
joiners.append(membership)
n = n + 1
n += 1

if Block.re_actives.match(lines[n]):
n = n + 1
n += 1
while Block.re_leavers.match(lines[n]) is None:
membership = Membership.from_inline(version, currency, "IN", lines[n])
actives.append(membership)
n = n + 1
n += 1

if Block.re_leavers.match(lines[n]):
n = n + 1
n += 1
while Block.re_excluded.match(lines[n]) is None:
membership = Membership.from_inline(version, currency, "OUT", lines[n])
leavers.append(membership)
n = n + 1
n += 1

if Block.re_excluded.match(lines[n]):
n = n + 1
n += 1
while Block.re_certifications.match(lines[n]) is None:
membership = Block.re_exclusion.match(lines[n]).group(1)
excluded.append(membership)
n = n + 1
n += 1

if Block.re_certifications.match(lines[n]):
n = n + 1
n += 1
while Block.re_transactions.match(lines[n]) is None:
certification = Certification.from_inline(version, currency,
prev_hash, lines[n])
certifications.append(certification)
n = n + 1
n += 1

if Block.re_transactions.match(lines[n]):
n = n + 1
n += 1
while not Block.re_signature.match(lines[n]):
tx_lines = ""
header_data = Transaction.re_header.match(lines[n])
Expand All @@ -277,14 +301,14 @@ def from_signed_raw(cls, raw):
inputs_num = int(header_data.group(3))
outputs_num = int(header_data.group(4))
has_comment = int(header_data.group(5))
tx_max = n+issuers_num*2+inputs_num+outputs_num+has_comment+1
tx_max = n + issuers_num * 2 + inputs_num + outputs_num + has_comment + 1
for i in range(n, tx_max):
tx_lines += lines[n]
n = n + 1
n += 1
transaction = Transaction.from_compact(currency, tx_lines)
transactions.append(transaction)

signature = Block.re_signature.match(lines[n]).group(1)
signature = Block.parse_field("Signature", lines[n])

return cls(version, currency, noonce, number, powmin, time,
mediantime, ud, issuer, prev_hash, prev_issuer,
Expand Down
6 changes: 5 additions & 1 deletion ucoinpy/documents/certification.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import base64
import logging

from .document import Document
from .document import Document, MalformedDocumentError


class SelfCertification(Document):
Expand All @@ -26,6 +26,8 @@ def __init__(self, version, currency, pubkey, ts, uid, signature):
@classmethod
def from_inline(cls, version, currency, inline):
selfcert_data = SelfCertification.re_inline.match(inline)
if selfcert_data is None:
raise MalformedDocumentError("Inline self certification")
pubkey = selfcert_data.group(1)
signature = selfcert_data.group(2)
ts = int(selfcert_data.group(3))
Expand Down Expand Up @@ -63,6 +65,8 @@ def __init__(self, version, currency, pubkey_from, pubkey_to,
@classmethod
def from_inline(cls, version, currency, blockhash, inline):
cert_data = Certification.re_inline.match(inline)
if cert_data is None:
raise MalformedDocumentError("Certification")
pubkey_from = cert_data.group(1)
pubkey_to = cert_data.group(2)
blocknumber = int(cert_data.group(3))
Expand Down
29 changes: 28 additions & 1 deletion ucoinpy/documents/document.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,42 @@
import base58
import base64
import re
import logging
import hashlib


class MalformedDocumentError(Exception):
"""
Malformed document exception
"""
def __init__(self, field_name):
super().__init__("Could not parse field {0}".format(field_name))


class Document:
re_version = re.compile("Version: ([0-9]+)\n")
re_currency = re.compile("Currency: ([^\n]+)\n")
re_signature = re.compile("([A-Za-z0-9+/]+(?:=|==)?)\n")

fields_parsers = {
"Version": re_version,
"Currency": re_currency,
"Signature": re_signature
}

@classmethod
def parse_field(cls, field_name, line):
"""
:param field_name:
:param line:
:return:
"""
try:
value = cls.fields_parsers[field_name].match(line).group(1)
except AttributeError:
raise MalformedDocumentError(field_name)
return value

def __init__(self, version, currency, signatures):
self.version = version
self.currency = currency
Expand Down
Loading

0 comments on commit e27d101

Please sign in to comment.