Skip to content
Snippets Groups Projects
Commit 117f185b authored by Hugo Trentesaux's avatar Hugo Trentesaux Committed by Vincent Texier
Browse files

[feat] allow building block from local json

parent 86e38051
No related branches found
No related tags found
2 merge requests!128Release 0.62.0,!124#130: Support reading Duniter's local json blockchain
...@@ -252,6 +252,62 @@ class Block(Document): ...@@ -252,6 +252,62 @@ class Block(Document):
def blockUID(self) -> BlockUID: def blockUID(self) -> BlockUID:
return BlockUID(self.number, self.proof_of_work()) return BlockUID(self.number, self.proof_of_work())
@classmethod
def from_parsed_json(cls: Type[BlockType], parsed_json_block: dict) -> BlockType:
"""return a block from the python structure produced when parsing json"""
b = parsed_json_block # alias for readability
version = b["version"]
currency = b["currency"]
arguments = { # arguments used to create block
"version": b["version"],
"currency": b["currency"],
"number": b["number"],
"powmin": b["powMin"],
"time": b["time"],
"mediantime": b["medianTime"],
"ud": b["dividend"],
"unit_base": b["unitbase"],
"issuer": b["issuer"],
"issuers_frame": b["issuersFrame"],
"issuers_frame_var": b["issuersFrameVar"],
"different_issuers_count": b["issuersCount"],
"prev_hash": b["previousHash"],
"prev_issuer": b["previousIssuer"],
"parameters": b["parameters"],
"members_count": b["membersCount"],
"identities": b["identities"],
"joiners": b["joiners"],
"actives": b["actives"],
"leavers": b["leavers"],
"revokations": b["revoked"],
"excluded": b["excluded"],
"certifications": b["certifications"],
"transactions": b["transactions"],
"inner_hash": b["inner_hash"],
"nonce": b["nonce"],
"signature": b["signature"],
}
# parameters: Optional[Sequence[str]]
if arguments["parameters"]:
arguments["parameters"] = arguments["parameters"].split(":")
# identities: List[Identity]
arguments["identities"] = [Identity.from_inline(version, currency, i+'\n') for i in arguments["identities"]]
# joiners: List[Membership]
arguments["joiners"] = [Membership.from_inline(version, currency, "IN", i+'\n') for i in arguments["joiners"]]
# actives: List[Membership]
arguments["actives"] = [Membership.from_inline(version, currency, "IN", i+'\n') for i in arguments["actives"]]
# leavers: List[Membership]
arguments["leavers"] = [Membership.from_inline(version, currency, "OUT", i+'\n') for i in arguments["leavers"]]
# revokations: List[Revocation]
arguments["revokations"] = [Revocation.from_inline(version, currency, i+'\n') for i in arguments["revokations"]]
# certifications: List[Certification]
arguments["certifications"] = [Certification.from_inline(version, currency, arguments["inner_hash"], i+'\n') for i in arguments["certifications"]]
# transactions: List[Transaction]
arguments["transactions"] = [Transaction.from_bma_history(currency, i) for i in arguments["transactions"]]
# now that the arguments are ready, return the block
return cls(**arguments)
@classmethod @classmethod
def from_signed_raw(cls: Type[BlockType], signed_raw: str) -> BlockType: def from_signed_raw(cls: Type[BlockType], signed_raw: str) -> BlockType:
lines = signed_raw.splitlines(True) lines = signed_raw.splitlines(True)
......
source diff could not be displayed: it is too large. Options to address this: view the blob.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment