Beispiel #1
0
    def final_addr_balance(self, addr):
        """ This function scans the complete blockchain and returns
        the total balance of an Address """
        chain_length = self.redis_client.llen('chain')
        total = 0
        used_transactions = set()
        while chain_length > 0:
            block = Block.from_json(
                json.loads(
                    self.redis_client.lindex('chain', chain_length -
                                             1).decode('utf-8')))
            for tx in block.transactions:
                for inp in tx.inputs:
                    if inp.address == addr:
                        used_transactions.add((inp.previous_tx, inp.index))
                for out_index, out in enumerate(tx.outputs):
                    if tx.hash not in used_transactions:
                        if out.address == addr:
                            total = total + out.value
                    else:
                        if out.address == addr and (
                                tx.hash, out_index) not in used_transactions:
                            total = total + out.value
            chain_length -= 1

        return total
Beispiel #2
0
    def get_block_by_hash(self, hash: str) -> Block:
        chain_length = self.redis_client.llen('chain')
        while chain_length > 0:
            block = Block.from_json(
                json.loads(
                    self.redis_client.lindex('chain', chain_length -
                                             1).decode('utf-8')))
            if block.hash == hash:
                return block
            chain_length -= 1

        return
Beispiel #3
0
    def get_tx_by_hash(self, hash: str):
        chain_length = self.redis_client.llen('chain')
        while chain_length > 0:
            block = Block.from_json(
                json.loads(
                    self.redis_client.lindex('chain', chain_length -
                                             1).decode('utf-8')))
            for tx in block.transactions:
                if tx.hash == hash:
                    return tx
            chain_length -= 1

        return
Beispiel #4
0
 def full_chain_verify(self):
     verify_message = "unverified"
     if self.chain_length == 0:
         return "Empty Chain! Verification failed!"
     for i in range(0, self.chain_length):
         raw = json.loads(self.redis_client.lindex('chain', i).decode('utf-8'))
         block = Block.from_json(raw)
         block.calculate_merkle_root()
         v_merkl_root = block.merkle_root
         if v_merkl_root != raw["merkle_root"]: 
             verify_message = "failed"
             self.del_from_faultblock(i)
             return verify_message
     verify_message = "verified"
     return verify_message
Beispiel #5
0
    def sendblock(self, request=None, response=None):
        """ Send block """

        if request is None and response is None:
            # If no data is being passed on by command handler
            pass
        if request is not None:
            # Processing requests
            UDPHandler.broadcastmessage(
                json.dumps({
                    "prev_command": "sendblock",
                    "body": request
                }))
        if response is not None:
            # Processing reaponse
            blkc = BlockChain()
            blkc.add_block(Block.from_json(response["body"]))
            blkc.close()
Beispiel #6
0
    def get_txs_by_addr(self, addr) -> List[Transaction]:
        txs = []
        chain_length = self.redis_client.llen('chain')
        while chain_length > 0:
            block = Block.from_json(
                json.loads(
                    self.redis_client.lindex('chain', chain_length -
                                             1).decode('utf-8')))
            print(len(block.transactions))
            for tx in block.transactions:
                inp_adresses = [inp.address for inp in tx.inputs]
                if addr in inp_adresses:
                    txs.append(tx)
                    continue
                out_adresses = [out.address for out in tx.outputs]
                if addr in out_adresses:
                    txs.append(tx)
                    continue
            chain_length -= 1

        return txs
Beispiel #7
0
    def get_utxos_by_addr(self, addr) -> List[Tuple[str, int, float]]:
        utxos = []
        chain_length = self.redis_client.llen('chain')
        used_transactions = set()
        while chain_length > 0:
            block = Block.from_json(
                json.loads(
                    self.redis_client.lindex('chain', chain_length -
                                             1).decode('utf-8')))
            for tx in block.transactions:
                for inp in tx.inputs:
                    if inp.address == addr:
                        used_transactions.add((inp.previous_tx, inp.index))
                for out_index, out in enumerate(tx.outputs):
                    if tx.hash not in used_transactions:
                        if out.address == addr:
                            utxos.append((tx.hash, out_index, out.value))
                    else:
                        if out.address == addr and (
                                tx.hash, out_index) not in used_transactions:
                            utxos.append((tx.hash, out_index, out.value))
            chain_length -= 1

        return utxos
Beispiel #8
0
 def get_block(self, index: int) -> Block:
     raw = self.redis_client.lindex("chain", index)
     if raw is not None:
         return Block.from_json(json.loads(raw.decode("utf-8")))
     else:
         return