Пример #1
0
    def add_block(self, proof):
        prev_block = self._blocks[-1]

        block = Block()  # New block
        block.prev_hash = prev_block.get_hash()
        block.blockNo = prev_block.blockNo + 1
        block.proof = proof
        if self._valid_proof(prev_block.get_proof(), prev_block.get_hash(),
                             proof):

            block._transactions = copy.deepcopy(self._transactions)
            self._blocks.append(block)
            return block
        return None
Пример #2
0
    def chain_from_json(self, chain_data):
        builded_chain = []
        for bd in chain_data:
            try:
                block_dict = json.loads(str(bd))
            except:
                block_dict = bd
            block = Block()
            block.timestamp = block_dict['timestamp']
            block.blockNo = block_dict['blockNo']
            block.prev_hash = block_dict['prev_hash']
            block.proof = block_dict['proof']

            ts = block_dict['_transactions']
            _t = [
                Transaction(t['origin'], t['key'], t['value'], t['timestamp'])
                for t in ts
            ]
            block._transactions = _t
            builded_chain.append(block)

        return builded_chain
Пример #3
0
    def block_from_json(self, block_data):
        for l in block_data:
            block_data = json.loads(l)
        block = Block()  # New block
        block.prev_hash = block_data['prev_hash']
        block.blockNo = block_data['blockNo']
        block.proof = block_data['proof']
        block.timestamp = block_data['timestamp']

        conf_transactions = []
        for t in block_data['_transactions']:
            t1 = Transaction(t['origin'], t['key'], t['value'], t['timestamp'])

            if t1 in self._transactions:
                conf_transactions.append(t1)
            else:
                print("[ERROR] Possible not syncro with other transactions")
                return None

        block._transactions = conf_transactions
        if self.valid_block(block):
            return block
        return None