コード例 #1
0
 def add_block(self, block):
     transactions = [
         Transaction(tx['sender'], tx['recipient'], tx['signature'],
                     tx['amount']) for tx in block['transactions']
     ]
     proof_is_valid = Verification.valid_proof(transactions[:-1],
                                               block['previous_hash'],
                                               block['proof'],
                                               GUESS_HASS_PROOF_STRING)
     hashes_match = HashUtil.hash_block(
         self.chain[-1]) == block['previous_hash']
     if not proof_is_valid or not hashes_match:
         return False
     converted_block = Block(block['index'], block['previous_hash'],
                             transactions, block['proof'],
                             block['timestamp'])
     self.__chain.append(converted_block)
     stored_transactions = self.__open_transactions[:]
     for itx in block['transactions']:
         for opentx in stored_transactions:
             if opentx.sender == itx['sender'] and opentx.recipient == itx[
                     'recipient'] and opentx.amount == itx[
                         'amount'] and opentx.signature == itx['signature']:
                 try:
                     self.__open_transactions.remove(opentx)
                 except ValueError:
                     Log.log_error('ValueError', 'Item was already removed',
                                   self.node_id)
     self.save_data()
     return True
コード例 #2
0
def broadcast_transaction():
    values = request.get_json()
    Log.log_message(
        'broadcast-transaction started with values{}'.format(values), port)
    if not values:
        response = {'message': 'No data found.'}
        Log.log_error('400', response, port)
        return jsonify(response), 400
    required = ['sender', 'recipient', 'amount', 'signature']
    if not all(key in values for key in required):
        response = {'message': 'Some data are missing.'}
        Log.log_error('400', response, port)
        return jsonify(response), 400
    Log.log_message('Broadcast-transaction', port)
    success = blockchain.add_transaction(values['recipient'], values['sender'],
                                         values['signature'], values['amount'],
                                         True)
    Log.log_message('Broadcast-transaction Ended', port)
    if success:
        response = {
            'message': 'Succesfully added transaction',
            'transaction': {
                'sender': values['sender'],
                'recipient': values['recipient'],
                'amount': values['amount'],
                'signature': values['signature']
            }
        }
        return jsonify(response), 201
    else:
        response = {'message': 'Creating a transaction failed..!'}
        return jsonify(response), 500
コード例 #3
0
def mine():
    Log.log_message(
        'blockchain.resolve_conflicts={}'.format(blockchain.resolve_conflicts),
        port)
    if blockchain.public_key is None:
        load_keys_return = load_keys()
        if load_keys_return[1] is not 201:
            return load_keys_return
    Log.log_message(
        'blockchain.resolve_conflicts={}'.format(blockchain.resolve_conflicts),
        port)
    if blockchain.resolve_conflicts:
        response = {'message': 'Resolve conflicts first, block not added!'}
        Log.log_error(409, response, port)
        return jsonify(response), 409
    block = blockchain.mine_block()
    if block != None:
        dict_block = block.to_json()
        response = {
            'message': 'Block added succesfully',
            'block': dict_block,
            'funds': blockchain.get_balance()
        }
        Log.log_status(201, response, port)
        return jsonify(response), 201
    else:
        response = {
            'message': 'Adding a block failed.',
            'wallet_set_up': wallet.public_key != None
        }
        Log.log_error(500, response, port)
        return jsonify(response), 500
コード例 #4
0
def add_new_transaction():
    values = request.get_json()
    if not values:
        response = {'message': 'No data found.'}
        return jsonify(response), 400
    required_fields = ['sender', 'signature', 'recipient', 'amount']
    if not all(field in values for field in required_fields):
        response = {'message': 'Required data is missing.'}
        Log.log_error(400, response, port)
        return jsonify(response), 400
    sender = values['sender']
    signature = values['signature']
    recipient = values['recipient']
    amount = float(values['amount'])
    Log.log_message(amount, port)

    success = blockchain.add_transaction(recipient, sender, signature, amount)
    Log.log_message(success, port)
    if success:
        response = {
            'message': 'Succesfully added transaction',
            'transaction': {
                'sender': wallet.public_key,
                'recipient': recipient,
                'amount': amount,
                'signature': signature
            },
            'funds': blockchain.get_balance()
        }
        return jsonify(response), 201
    else:
        response = {'message': 'Creating a transaction failed..!'}
        Log.log_error(500, response, port)
        return jsonify(response), 500
コード例 #5
0
ファイル: misc.py プロジェクト: ngiannios/JACC
    def get_IP(port):
        try:
            host_name = socket.gethostname()
            host_ip = socket.gethostbyname(host_name)
            return (host_ip)

        except:
            Log.log_error('IPE', "Unable to get Hostname and IP", port)
コード例 #6
0
def add_node():
    values = request.get_json()
    if not values:
        response = {'message': 'No data attached.'}
        return jsonify(response), 400
    Log.log_message(values, port)
    if 'node' not in values:
        response = {'message': 'No node data attached.'}
        Log.log_error(401, response, port)
        return jsonify(response), 401
    node = values.get('node')

    blockchain.add_peer_node(node)
    response = {
        'message': 'Node added succesfully.',
        'all_nodes': blockchain.get_peer_nodes()
    }
    return jsonify(response), 201
コード例 #7
0
    def add_transaction(self,
                        recipient,
                        sender,
                        signature,
                        amount=1.0,
                        is_receiving=False):
        """ Append a new value as well as the last blockchain value to the blockchain.

        Arguments:
            :sender: The sender of the coins.
            :recipient: The recipient of the coins.
            :amount: The amount of coins sent with the transaction (default = 1.0)
        """
        # if self.public_key == None:
        #     return False
        transaction = Transaction(sender, recipient, signature, amount)
        if Verification.verify_transaction(transaction, self.get_balance):
            self.__open_transactions.append(transaction)
            self.save_data()
            if not is_receiving:
                for node in self.__peer_nodes:
                    url = '{}/broadcast-transaction'.format(node)
                    try:
                        response = requests.post(url,
                                                 json={
                                                     'sender': sender,
                                                     'recipient': recipient,
                                                     'amount': amount,
                                                     'signature': signature
                                                 })
                        if response.status_code == 400 or response.status_code == 500:
                            Log.log_error(
                                response.status_code,
                                'broadcast-transaction error response from {}:{}'
                                .format(url, response), self.node_id)
                            return False
                    except requests.exceptions.ConnectionError:
                        continue
            return True
        return False
コード例 #8
0
    def add_peer_node(self, new_node):
        # self.__peer_nodes.add(new_node)
        # get all the peer nodes of the added node
        url = '{}/node'.format(new_node)
        try:
            response = requests.get(url)
            peer_nodes = response.json()
            for peer_node in peer_nodes['all_nodes']:
                self.__peer_nodes.add(peer_node)
        except requests.exceptions.ConnectionError:
            Log.log_error(requests.exceptions.ConnectionError,
                          'ConnectionError', self.node_id)

        Log.log_message(Misc.get_Public_IP(self.node_id), self.node_id)
        # if count == 0:
        #     try:
        #         local_http_addr = 'http://{}:{}'.format(Misc.get_Public_IP(self.node_id), self.node_id)
        #         response = requests.post(url, json={'node': local_http_addr, 'count': 1})
        #         Log.log_error(response.status_code, response, self.node_id)
        #     except requests.exceptions.ConnectionError:
        #         None

        self.__peer_nodes.add(new_node)
        self.save_data()