def send_swap(test_case: RPCTestCase, eon_number, account, token, token_swapped, amount, amount_swapped, nonce, expected_status=status.HTTP_201_CREATED, eon_count=1, sell_order=True): # Sender account sender_wallet = Wallet.objects.get(address=remove_0x_prefix( account.get('address')), token=token) # Recipient account recipient_wallet = Wallet.objects.get(address=remove_0x_prefix( account.get('address')), token=token_swapped) sender_wallet_context = WalletTransferContext(wallet=sender_wallet, transfer=None) recipient_wallet_context = WalletTransferContext(wallet=recipient_wallet, transfer=None) initial_sender_balance = sender_wallet_context.available_funds_at_eon( eon_number=eon_number, only_appended=False) initial_recipient_balance = recipient_wallet_context.available_funds_at_eon( eon_number=eon_number, only_appended=False) if initial_sender_balance < amount: send_transaction(test_case=test_case, eon_number=eon_number, sender=testrpc_accounts.accounts[0], recipient=account, amount=amount - initial_sender_balance, nonce=random.randint(1, 999999), token=token) if initial_sender_balance > amount: # clear sender account send_transaction(test_case=test_case, eon_number=eon_number, sender=account, recipient=testrpc_accounts.accounts[0], amount=initial_sender_balance - amount, nonce=random.randint(1, 999999), token=token) if initial_recipient_balance > 0: # clear recipient account send_transaction(test_case=test_case, eon_number=eon_number, sender=account, recipient=testrpc_accounts.accounts[0], amount=initial_recipient_balance, nonce=random.randint(1, 999999), token=token_swapped) sender_balance = sender_wallet_context.available_funds_at_eon( eon_number=eon_number, only_appended=False) recipient_balance = recipient_wallet_context.available_funds_at_eon( eon_number=eon_number, only_appended=False) test_case.assertEqual(sender_balance, amount) test_case.assertEqual(recipient_balance, 0) debit_balance_signatures = [] debit_signatures = [] credit_balance_signatures = [] credit_signatures = [] fulfillment_signatures = [] for i in range(eon_count): swap = Transfer(wallet=sender_wallet, amount=amount, eon_number=eon_number + i, recipient=recipient_wallet, amount_swapped=amount_swapped, nonce=nonce, processed=False, complete=False, swap=True) sender_wallet_context = WalletTransferContext(wallet=sender_wallet, transfer=swap) recipient_wallet_context = WalletTransferContext( wallet=recipient_wallet, transfer=swap) sender_highest_spent, sender_highest_gained = sender_wallet_context.off_chain_actively_sent_received_amounts( eon_number=eon_number + i, only_appended=False) if i == 0: tx_set_tree = sender_wallet_context.authorized_transfers_tree( only_appended=False, force_append=True) else: tx_set_tree = WalletTransferContext.authorized_transfers_tree_from_list( [ swap.shorthand(sender_wallet_context, is_last_transfer=True, starting_balance=sender_balance) ]) tx_set_hash = hex_value(tx_set_tree.root_hash()) debiting_active_state = ActiveState( wallet=sender_wallet, updated_spendings=sender_highest_spent + amount, updated_gains=sender_highest_gained, eon_number=eon_number + i, tx_set_hash=tx_set_hash) debiting_active_state_authorization = sign_message( debiting_active_state.checksum(), account.get('pk')) debiting_active_state_signature = Signature( wallet=sender_wallet, checksum=hex_value(debiting_active_state.checksum()), value=encode_signature(debiting_active_state_authorization)) test_case.assertTrue(debiting_active_state_signature.is_valid()) debit_concise_balance_marker = MinimumAvailableBalanceMarker( wallet=sender_wallet, eon_number=eon_number + i, amount=0) debit_concise_balance_marker_authorization = sign_message( debit_concise_balance_marker.checksum(), account.get('pk')) debit_concise_balance_marker_signature = Signature( wallet=sender_wallet, checksum=hex_value(debit_concise_balance_marker.checksum()), value=encode_signature(debit_concise_balance_marker_authorization)) test_case.assertTrue(debit_concise_balance_marker_signature.is_valid()) recipient_highest_spent, recipient_highest_gained = recipient_wallet_context.off_chain_actively_sent_received_amounts( eon_number=eon_number + i, only_appended=False) if i == 0: tx_set_tree = recipient_wallet_context.authorized_transfers_tree( only_appended=False, force_append=True) else: tx_set_tree = WalletTransferContext.authorized_transfers_tree_from_list( [ swap.shorthand(recipient_wallet_context, is_last_transfer=True, starting_balance=recipient_balance) ]) tx_set_hash = hex_value(tx_set_tree.root_hash()) crediting_active_state = ActiveState( wallet=recipient_wallet, updated_spendings=recipient_highest_spent, updated_gains=recipient_highest_gained, eon_number=eon_number + i, tx_set_hash=tx_set_hash) crediting_active_state_authorization = sign_message( crediting_active_state.checksum(), account.get('pk')) crediting_active_state_signature = Signature( wallet=recipient_wallet, checksum=hex_value(crediting_active_state.checksum()), value=encode_signature(crediting_active_state_authorization)) test_case.assertTrue(crediting_active_state_signature.is_valid()) credit_concise_balance_marker = MinimumAvailableBalanceMarker( wallet=recipient_wallet, eon_number=eon_number + i, amount=0) credit_concise_balance_marker_authorization = sign_message( credit_concise_balance_marker.checksum(), account.get('pk')) credit_concise_balance_marker_signature = Signature( wallet=recipient_wallet, checksum=hex_value(credit_concise_balance_marker.checksum()), value=encode_signature( credit_concise_balance_marker_authorization)) test_case.assertTrue( credit_concise_balance_marker_signature.is_valid()) swap.processed, swap.complete = True, True if i == 0: tx_set_tree = recipient_wallet_context.authorized_transfers_tree( only_appended=False, force_append=True) else: tx_set_tree = WalletTransferContext.authorized_transfers_tree_from_list( [ swap.shorthand(recipient_wallet_context, is_last_transfer=True, starting_balance=0) ]) tx_set_hash = hex_value(tx_set_tree.root_hash()) recipient_fulfillment_active_state = ActiveState( wallet=recipient_wallet, updated_spendings=recipient_highest_spent, updated_gains=recipient_highest_gained + amount_swapped, eon_number=eon_number + i, tx_set_hash=tx_set_hash) recipient_fulfillment_active_state_authorization = sign_message( recipient_fulfillment_active_state.checksum(), account.get('pk')) swap.processed, swap.complete = False, False recipient_fulfillment_active_state_signature = Signature( wallet=recipient_wallet, checksum=hex_value(recipient_fulfillment_active_state.checksum()), value=encode_signature( recipient_fulfillment_active_state_authorization)) test_case.assertTrue( recipient_fulfillment_active_state_signature.is_valid()) debit_balance_signatures.append({ 'value': encode_signature(debit_concise_balance_marker_authorization) }) debit_signatures.append( {'value': encode_signature(debiting_active_state_authorization)}) credit_balance_signatures.append({ 'value': encode_signature(credit_concise_balance_marker_authorization) }) credit_signatures.append( {'value': encode_signature(crediting_active_state_authorization)}) fulfillment_signatures.append({ 'value': encode_signature(recipient_fulfillment_active_state_authorization) }) # Make API Request url = reverse('swap-endpoint') data = { 'debit_signature': debit_signatures, 'debit_balance_signature': debit_balance_signatures, 'credit_signature': credit_signatures, 'credit_balance_signature': credit_balance_signatures, 'credit_fulfillment_signature': fulfillment_signatures, 'eon_number': eon_number, 'amount': amount, 'amount_swapped': amount_swapped, 'nonce': nonce, 'wallet': { 'address': sender_wallet.address, 'token': sender_wallet.token.address, }, 'recipient': { 'address': recipient_wallet.address, 'token': recipient_wallet.token.address, }, 'sell_order': sell_order } # Send tx to server x = datetime.now() response = test_case.client.post(url, data, format='json') y = datetime.now() delta = y - x # Ensure the transaction was recorded test_case.assertEqual(response.status_code, expected_status, response.content) print('SWAP Time: {}s for {}/{}'.format(delta, amount, amount_swapped)) # assert that swap created for current eon is confirmed tx = json.loads(response.content) swap = Transfer.objects.get(id=tx['id']) test_case.assertEqual(swap.eon_number, eon_number) test_case.assertTrue(swap.is_signed_by_operator()) # Log time delta return delta
def process_passive_transfer(transfer, operator_eon_number, checkpoint_created): if transfer.wallet == transfer.recipient: logger.info('Voiding self transfer.') transfer.close(voided=True) return with transfer.lock(auto_renewal=True), transfer.wallet.lock( auto_renewal=True), transfer.recipient.lock(auto_renewal=True): wallet_view_context = WalletTransferContext(wallet=transfer.wallet, transfer=transfer) recipient_view_context = WalletTransferContext( wallet=transfer.recipient, transfer=transfer) if should_void_transfer(transfer, wallet_view_context, recipient_view_context, operator_eon_number, checkpoint_created): logger.info('Voiding transfer.') transfer.close(voided=True) return # Invalid active state update tx_set_list = wallet_view_context.authorized_transfers_list_shorthand( only_appended=True, force_append=True, last_transfer_is_finalized=False) tx_set_tree = WalletTransferContext.authorized_transfers_tree_from_list( tx_set_list) tx_set_hash = hex_value(tx_set_tree.root_hash()) highest_spendings, highest_gains = wallet_view_context.off_chain_actively_sent_received_amounts( eon_number=transfer.eon_number, only_appended=True) active_state = ActiveState( wallet=transfer.wallet, updated_spendings=transfer.sender_active_state.updated_spendings, updated_gains=highest_gains, tx_set_hash=tx_set_hash, eon_number=transfer.eon_number) raw_checksum = active_state.checksum() encoded_checksum = hex_value(raw_checksum) wallet_active_state = transfer.sender_active_state if wallet_active_state.wallet_signature.checksum != encoded_checksum: logger.error( 'Transfer {} invalid sender active state checksum for {}'. format(transfer.id, transfer.wallet.address)) transfer.close(voided=True) return try: wallet_active_state.operator_signature = wallet_active_state.sign_active_state( settings.HUB_OWNER_ACCOUNT_ADDRESS, settings.HUB_OWNER_ACCOUNT_KEY) except LookupError as e: logger.error(e) return transfer.sender_active_state.save() transfer.close(complete=True, appended=True) operator_celery.send_task('auditor.tasks.on_transfer_confirmation', args=[transfer.id]) logger.info('Transfer {} processed.'.format(transfer.id))