Exemplo n.º 1
0
class BlockWithProof:
    def __init__(self, block):
        self._block = block
        self._proof = []

    def add_proof(self, commit):
        self._proof.append(commit)

    def to_json(self):
        proof_json = []
        for commit in self._proof:
            proof_json.append(commit.to_json())
        block_with_proof_json = {
            'block': self._block.to_json(),
            'proof': proof_json
        }
        return block_with_proof_json

    def from_json(self, proof_json):
        self._block = Block()
        self._block.from_json(proof_json['block'])
        for commit_json in proof_json['proof']:
            commit = Commit()
            commit.from_json(commit_json)
            self._proof.append(commit)
        return
Exemplo n.º 2
0
class PrepareRequest:
    def __init__(self, node_id, view_id, block_json=None, signature=None):
        self.infoId = 'PrepareRequest'
        self.nodeId = node_id
        self.viewId = view_id
        self.block = Block()
        if block_json is not None:
            self.block.from_json(block_json)
        self.signature = signature

    def sign(self, signature):
        self.signature = signature

    def get_signature(self):
        return self.signature

    def to_json(self):
        request_json = {
            'infoId': self.infoId,
            'nodeId': self.nodeId,
            'viewId': self.viewId,
            'block': self.block.to_json(),
            'signature': self.signature
        }
        return request_json

    def from_json(self, request_json):
        self.infoId = request_json['infoId']
        self.nodeId = request_json['nodeId']
        self.viewId = request_json['viewId']
        self.block.from_json(request_json['block'])
        self.signature = request_json['signature']
        return

    def calc_hash(self):
        return hash_message(str(self))

    def check_signature(self):
        return verify_signature(PK_list[self.nodeId], self.calc_hash(),
                                self.signature)

    def __str__(self):
        return str(self.infoId) + str(self.nodeId) + str(self.viewId) + str(
            self.block.hash)
Exemplo n.º 3
0
    def from_json(self, chain_json):
        required = ['height', 'blockchain']
        if not all(k in chain_json for k in required):
            logging.warning(f'value missing in {required}')
            return False

        if not isinstance(chain_json['height'], int):
            logging.warning("height should be type<int>")
            return False
        if not isinstance(chain_json['blockchain'], list):
            logging.warning("blockchain should be type<list>")
            return False

        chain = []
        for block_json in chain_json['blockchain']:
            new_block = Block()
            if not new_block.from_json(block_json):
                return False
            chain.append(new_block)
        self._blockchain = chain
        return True