def should_void_transfer(transfer, wallet_view_context: WalletTransferContext, recipient_view_context: WalletTransferContext, operator_eon_number, is_checkpoint_created): if transfer.eon_number != operator_eon_number and is_checkpoint_created: logger.error('Transfer {} eon mismatch ({}, {})'.format( transfer.id, transfer.eon_number, operator_eon_number)) return True if transfer.amount < 0: logger.error('Transfer {} has negative amount'.format(transfer.id)) return True # Unauthorized transfer if transfer.sender_active_state is None: logger.error('Transfer {} no authorization'.format(transfer.id)) return True # Invalid signature by sender if not transfer.sender_active_state.wallet_signature.is_valid(): logger.error('Transfer {} invalid sender signature.'.format( transfer.id)) return True # Ensure sender log consistency can_append_to_sender_log = wallet_view_context.can_append_transfer() if can_append_to_sender_log is not True: logger.error('Sender: {}'.format(can_append_to_sender_log)) return True # Ensure recipient log consistency can_append_to_recipient_log = recipient_view_context.can_append_transfer() if can_append_to_recipient_log is not True: logger.error('Recipient: {}'.format(can_append_to_recipient_log)) return True # Ensure transfer consistency can_spend, currently_available_funds = wallet_view_context.can_send_transfer( current_eon_number=operator_eon_number, using_only_appended_funds=True) if can_spend is not True: passively_received = wallet_view_context.off_chain_passively_received_amount( eon_number=operator_eon_number, only_appended=True) logger.error(can_spend) logger.info(passively_received) return True last_active_transfer, last_active_transfer_is_outgoing = wallet_view_context.last_appended_active_transfer( operator_eon_number) last_active_state = WalletTransferContext.appropriate_transfer_active_state( transfer=last_active_transfer, is_outgoing=last_active_transfer_is_outgoing) previous_spendings = last_active_state.updated_spendings if last_active_transfer else 0 updated_spendings = transfer.sender_active_state.updated_spendings # Incorrect updated spendings if last_active_transfer: if updated_spendings != previous_spendings + transfer.amount: logger.error( 'Transfer {} invalid updated spendings. Expected {}, found {}.' .format(transfer.id, previous_spendings + transfer.amount, updated_spendings)) return True elif updated_spendings != transfer.amount: logger.error( 'Transfer {} invalid initial spendings. Expected {}, found {}.'. format(transfer.id, transfer.amount, updated_spendings)) return True # Incorrect transfer position last_passively_received = recipient_view_context.last_appended_incoming_passive_transfer( operator_eon_number) if last_passively_received: if transfer.position != last_passively_received.position + last_passively_received.amount: logger.error( 'Transfer {} invalid offset. Expected {}, found {}.'.format( transfer.id, last_passively_received.position + last_passively_received.amount, transfer.position)) return True elif transfer.position != 0: logger.error( 'Transfer {} invalid offset. Expected {}, found {}.'.format( transfer.id, 0, transfer.position)) return True if transfer.sender_balance_marker.amount > currently_available_funds - transfer.amount: logger.error('Transfer {} invalid concise marker balance.'.format( transfer.id)) return True concise_balance_marker = MinimumAvailableBalanceMarker( wallet=transfer.wallet, eon_number=transfer.eon_number, amount=transfer.sender_balance_marker.amount) concise_balance_marker_checksum = hex_value( concise_balance_marker.checksum()) if transfer.sender_balance_marker.signature.checksum != concise_balance_marker_checksum: logger.error( 'Transfer {} invalid concise marker checksum worth: {}'.format( transfer.id, currently_available_funds - transfer.amount)) passively_received = wallet_view_context.off_chain_passively_received_amount( eon_number=operator_eon_number, only_appended=True) logger.info(passively_received) return True return False
def should_void_swap(swap: Transfer, wallet_view_context: WalletTransferContext, recipient_view_context: WalletTransferContext, operator_eon_number: int, is_checkpoint_created: bool): if not settings.SWAPS_ENABLED: logger.error('Swaps disabled. Voiding {}'.format(swap.id)) return True if swap.amount < 1: logger.error('Swap {} has less than 1 amount'.format(swap.id)) return True if swap.amount_swapped < 1: logger.error('Swap {} has less than 1 amount swapped'.format(swap.id)) return True # Unauthorized transfer if swap.sender_active_state is None: logger.error('Swap {} no authorization'.format(swap.id)) return True # Invalid signature by sender if not swap.sender_active_state.wallet_signature.is_valid(): logger.error('Swap {} invalid sender signature.'.format(swap.id)) return True # Unreceived transaction if swap.recipient_active_state is None: logger.error('Swap receipt for {} not provided.'.format(swap.id)) return True # Invalid signature by recipient if not swap.recipient_active_state.wallet_signature.is_valid(): logger.error('Swap {} invalid receipt signature.'.format(swap.id)) return True # Ensure log consistency can_append_to_sender_log = wallet_view_context.can_append_transfer() if can_append_to_sender_log is not True: logger.error('Sender: {}'.format(can_append_to_sender_log)) return True can_append_to_recipient_log = recipient_view_context.can_append_transfer() if can_append_to_recipient_log is not True: logger.error('Recipient: {}'.format(can_append_to_recipient_log)) return True # Skip consistency checks since they were done at least once before. if swap.appended: return False # Overspending sender_funds_remaining = wallet_view_context.loosely_available_funds_at_eon( eon_number=swap.eon_number, current_eon_number=operator_eon_number, is_checkpoint_created=is_checkpoint_created, only_appended=True) # sender remaining funds should be more than remaining amount in order matched_out, matched_in = swap.matched_amounts(all_eons=True) if sender_funds_remaining < swap.amount - matched_out: logger.error('Swap {} overspending.'.format(swap.id)) return True # Prevent future overdrawing # if swap.sender_balance_marker.amount > sender_funds_remaining - swap.amount: if swap.sender_balance_marker.amount != 0: logger.error('Swap {} invalid concise marker balance.'.format(swap.id)) return True concise_balance_marker = MinimumAvailableBalanceMarker( wallet=swap.wallet, eon_number=swap.eon_number, amount=swap.sender_balance_marker.amount) concise_balance_marker_checksum = crypto.hex_value( concise_balance_marker.checksum()) if swap.sender_balance_marker.signature.checksum != concise_balance_marker_checksum: logger.error('Swap {} invalid concise marker checksum for {}.'.format( swap.id, swap.sender_balance_marker.amount)) return True highest_spendings, highest_gains = wallet_view_context.off_chain_actively_sent_received_amounts( eon_number=swap.eon_number, only_appended=True) # if this is a multi eon swap if Transfer.objects.filter(eon_number=swap.eon_number - 1, tx_id=swap.tx_id).exists(): # set balances to initial fixed balances stored in transfer eon state sender_starting_balance = swap.sender_starting_balance recipient_starting_balance = swap.recipient_starting_balance # make sure this eon's starting balance is exactly the initial stored balance # when matched amount is taken into consideration for both sender and receiver if wallet_view_context.starting_balance_in_eon( swap.eon_number) != sender_starting_balance - matched_out: logger.error( 'Swap {} invalid sender starting balance of future state {} != {} - {}.' .format( swap.id, wallet_view_context.starting_balance_in_eon( swap.eon_number), sender_starting_balance, matched_out)) if recipient_view_context.starting_balance_in_eon( swap.eon_number) != recipient_starting_balance + matched_in: logger.error( 'Swap {} invalid recipient starting balance of future state {} != {} + {}.' .format( swap.id, recipient_view_context.starting_balance_in_eon( swap.eon_number), recipient_starting_balance, matched_out)) assert (wallet_view_context.starting_balance_in_eon( swap.eon_number) == sender_starting_balance - matched_out) assert (recipient_view_context.starting_balance_in_eon( swap.eon_number) == recipient_starting_balance + matched_in) else: sender_starting_balance = int( wallet_view_context.starting_balance_in_eon(swap.eon_number)) recipient_starting_balance = int( recipient_view_context.starting_balance_in_eon(swap.eon_number)) # Debit Authorization tx_set_tree = wallet_view_context.optimized_authorized_transfers_tree( only_appended=True, starting_balance=sender_starting_balance) tx_set_hash = crypto.hex_value(tx_set_tree.root_hash()) transfer_index = tx_set_tree.merkle_tree_nonce_map.get(swap.nonce) transfer_proof = tx_set_tree.proof(transfer_index) highest_spendings, highest_gains = wallet_view_context.off_chain_actively_sent_received_amounts( eon_number=swap.eon_number, only_appended=True) debiting_active_state = ActiveState(wallet=swap.wallet, updated_spendings=highest_spendings + swap.amount, updated_gains=highest_gains, tx_set_hash=tx_set_hash, tx_set_proof_hashes=transfer_proof, tx_set_index=transfer_index, eon_number=swap.eon_number) debiting_active_state_checksum = crypto.hex_value( debiting_active_state.checksum()) if swap.sender_active_state.wallet_signature.checksum != debiting_active_state_checksum: logger.error('Swap {} invalid debit active state checksum.'.format( swap.id)) return True # Credit Authorization tx_set_tree = recipient_view_context.optimized_authorized_transfers_tree( only_appended=True, starting_balance=recipient_starting_balance) tx_set_hash = crypto.hex_value(tx_set_tree.root_hash()) transfer_index = tx_set_tree.merkle_tree_nonce_map.get(swap.nonce) transfer_proof = tx_set_tree.proof(transfer_index) highest_spendings, highest_gains = recipient_view_context.off_chain_actively_sent_received_amounts( eon_number=swap.eon_number, only_appended=True) crediting_active_state = ActiveState(wallet=swap.recipient, updated_spendings=highest_spendings, updated_gains=highest_gains, tx_set_hash=tx_set_hash, tx_set_proof_hashes=transfer_proof, tx_set_index=transfer_index, eon_number=swap.eon_number) crediting_active_state_checksum = crypto.hex_value( crediting_active_state.checksum()) if swap.recipient_active_state.wallet_signature.checksum != crediting_active_state_checksum: logger.error('Swap {} invalid credit active state checksum.'.format( swap.id)) return True # Finality Authorization swap.complete = True tx_set_tree = recipient_view_context.optimized_authorized_transfers_tree( only_appended=True, starting_balance=recipient_starting_balance) swap.complete = False tx_set_hash = crypto.hex_value(tx_set_tree.root_hash()) transfer_index = tx_set_tree.merkle_tree_nonce_map.get(swap.nonce) transfer_proof = tx_set_tree.proof(transfer_index) recipient_fulfillment_active_state = ActiveState( wallet=swap.recipient, updated_spendings=highest_spendings, updated_gains=highest_gains + swap.amount_swapped, tx_set_hash=tx_set_hash, tx_set_proof_hashes=transfer_proof, tx_set_index=transfer_index, eon_number=swap.eon_number) recipient_fulfillment_active_state_checksum = crypto.hex_value( recipient_fulfillment_active_state.checksum()) if swap.recipient_fulfillment_active_state.wallet_signature.checksum != recipient_fulfillment_active_state_checksum: logger.error( 'Swap {} invalid finalization active state checksum.'.format( swap.id)) return True return False