Esempio n. 1
0
def get_updated_accounts(*, sender_account_balance, validated_block):
    """Return the updated balances of all accounts involved"""
    existing_accounts = []
    new_accounts = []

    message = validated_block['message']
    txs = message['txs']
    total_amount = sum([tx['amount'] for tx in txs])

    existing_accounts.append({
        'account_number': validated_block['account_number'],
        'balance': sender_account_balance - total_amount,
        'balance_lock': get_message_hash(message=message)
    })

    for tx in txs:
        amount = tx['amount']
        recipient = tx['recipient']
        recipient_account_balance = get_account_balance(account_number=recipient)

        if recipient_account_balance is None:
            new_accounts.append({
                'account_number': recipient,
                'balance': amount
            })
        else:
            existing_accounts.append({
                'account_number': recipient,
                'balance': recipient_account_balance + amount
            })

    return existing_accounts, new_accounts
Esempio n. 2
0
def account_balance_view(_, account_number):
    """
    Return the balance for the given account
    """

    return Response(
        {'balance': get_account_balance(account_number=account_number)})
def is_block_valid(*, block):
    """
    For given block verify:

    - signature
    - account balance exists
    - amount sent does not exceed account balance
    - balance key matches balance lock

    Return boolean indicating validity, senders account balance
    """
    account_number = block.get('account_number')
    message = block.get('message')
    signature = block.get('signature')

    try:
        verify_signature(message=sort_and_encode(message),
                         signature=signature,
                         verify_key=account_number)
    except BadSignatureError:
        return False, None
    except Exception as e:
        capture_exception(e)
        logger.exception(e)
        return False, None

    account_balance = get_account_balance(account_number=account_number)
    account_balance_lock = get_account_balance_lock(
        account_number=account_number)

    if account_balance is None:
        logger.error(f'Account balance for {account_number} not found')
        return False, None

    total_amount_valid, error = is_total_amount_valid(
        block=block, account_balance=account_balance)

    if not total_amount_valid:
        logger.error(error)
        return False, None

    balance_key = message.get('balance_key')

    if balance_key != account_balance_lock:
        logger.error(
            f'Balance key of {balance_key} does not match balance lock of {account_balance_lock}'
        )
        return False, None

    return True, account_balance
Esempio n. 4
0
 def balance(self, request, account_number=None):
     return Response(
         {'balance': get_account_balance(account_number=account_number)})