Repository: rotki/rotki

Issue:
Foreign key error if removing eth account while running PnL report
## Problem Definition

This was found by @nebolax while testing the day of the release.

If a PnL report is running,the user has ethereum addresses and tries to remove one of them then a foreign key error may happen as per screenshot:

![unknown (1)](https://user-images.githubusercontent.com/1658405/170834087-c713af7f-0ebb-4108-894a-5b15ee1e2ddf.png)


## Task

Figure out why it happens and fix it.

Probably something similar to: https://github.com/rotki/rotki/blob/6b85c5c53b8eb0af20c4cdb780fb3fec2c65f68f/rotkehlchen/rotkehlchen.py#L584

needs to happen




Discussion:

Resolving patch:
diff --git a/rotkehlchen/data/importer.py b/rotkehlchen/data/importer.py
--- a/rotkehlchen/data/importer.py
+++ b/rotkehlchen/data/importer.py
@@ -814,7 +814,8 @@ def _consume_blockfi_entry(self, csv_row: Dict[str, Any], **kwargs: Any) -> None
             return
 
         asset = symbol_to_asset_or_token(csv_row['Cryptocurrency'])
-        amount = deserialize_asset_amount_force_positive(csv_row['Amount'])
+        raw_amount = deserialize_asset_amount(csv_row['Amount'])
+        abs_amount = AssetAmount(abs(raw_amount))
         entry_type = csv_row['Transaction Type']
         # BlockFI doesn't provide information about fees
         fee = Fee(ZERO)
@@ -828,7 +829,7 @@ def _consume_blockfi_entry(self, csv_row: Dict[str, Any], **kwargs: Any) -> None
                 transaction_id=None,
                 timestamp=timestamp,
                 asset=asset,
-                amount=amount,
+                amount=abs_amount,
                 fee=fee,
                 fee_asset=fee_asset,
                 link='',
@@ -842,7 +843,7 @@ def _consume_blockfi_entry(self, csv_row: Dict[str, Any], **kwargs: Any) -> None
                 transaction_id=None,
                 timestamp=timestamp,
                 asset=asset,
-                amount=amount,
+                amount=abs_amount,
                 fee=fee,
                 fee_asset=fee_asset,
                 link='',
@@ -854,7 +855,7 @@ def _consume_blockfi_entry(self, csv_row: Dict[str, Any], **kwargs: Any) -> None
                 timestamp=timestamp,
                 action_type=LedgerActionType.EXPENSE,
                 location=Location.BLOCKFI,
-                amount=amount,
+                amount=abs_amount,
                 asset=asset,
                 rate=None,
                 rate_asset=None,
@@ -868,7 +869,7 @@ def _consume_blockfi_entry(self, csv_row: Dict[str, Any], **kwargs: Any) -> None
                 timestamp=timestamp,
                 action_type=LedgerActionType.INCOME,
                 location=Location.BLOCKFI,
-                amount=amount,
+                amount=abs_amount,
                 asset=asset,
                 rate=None,
                 rate_asset=None,
@@ -878,7 +879,7 @@ def _consume_blockfi_entry(self, csv_row: Dict[str, Any], **kwargs: Any) -> None
             self.db_ledger.add_ledger_action(action)
         elif entry_type == 'Crypto Transfer':
             category = (
-                AssetMovementCategory.WITHDRAWAL if FVal(csv_row['Amount']) < ZERO
+                AssetMovementCategory.WITHDRAWAL if raw_amount < ZERO
                 else AssetMovementCategory.DEPOSIT
             )
             asset_movement = AssetMovement(
@@ -888,7 +889,7 @@ def _consume_blockfi_entry(self, csv_row: Dict[str, Any], **kwargs: Any) -> None
                 transaction_id=None,
                 timestamp=timestamp,
                 asset=asset,
-                amount=amount,
+                amount=abs_amount,
                 fee=fee,
                 fee_asset=fee_asset,
                 link='',
diff --git a/rotkehlchen/rotkehlchen.py b/rotkehlchen/rotkehlchen.py
--- a/rotkehlchen/rotkehlchen.py
+++ b/rotkehlchen/rotkehlchen.py
@@ -1,6 +1,7 @@
 #!/usr/bin/env python
 
 import argparse
+import contextlib
 import logging.config
 import os
 import time
@@ -581,7 +582,11 @@ def remove_blockchain_accounts(
             accounts=accounts,
         )
         eth_addresses: List[ChecksumEthAddress] = cast(List[ChecksumEthAddress], accounts) if blockchain == SupportedBlockchain.ETHEREUM else []  # noqa: E501
-        with self.eth_transactions.wait_until_no_query_for(eth_addresses):
+        with contextlib.ExitStack() as stack:
+            if blockchain == SupportedBlockchain.ETHEREUM:
+                stack.enter_context(self.eth_transactions.wait_until_no_query_for(eth_addresses))
+                stack.enter_context(self.eth_transactions.missing_receipts_lock)
+                stack.enter_context(self.evm_tx_decoder.undecoded_tx_query_lock)
             self.data.db.remove_blockchain_accounts(blockchain, accounts)
 
         return balances_update


Test patch: