Repository: rotki/rotki

Issue:
Sign In Failed - TypeError
Good evening!

I'm here on Linux, I've just tried to log in to my Rotki database created with 1.0.4 (using 1.0.5 now). After I type in my password and log in, I get the message

> **Sign In Failed**
> TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'

Now when I attempt to go back to 1.0.4 I get
> **Sign In Failed**
> DBUpgradeError: Your database version is newer than the version expected by the executable. Did you perhaps try to revert to an older rotkehlchen version?Please only use the latest version of the software.

No big worries, I'm still evaluating the software to see if it can do what I need so there's not a ton of data in there. Just thought you should know. I'll be happy to help with debugging if I can. But kids, so... I'll do my best!


Discussion:
Hey @Aurifier! Thank you for trying out Rotki.

That sounds strange.Can you provide me with the following?

The logs of the run? There should probably be a stacktrace in there. For linux it is `rotkehlchen.log` either in the same directory as the executable or more probably in `~/.config/rotkehlchen.log`?

The reason for the 2nd message is that from 1.0.4 -> 1.0.5 there is a DB upgrade to account for the [SAI->DAI changes](https://github.com/rotki/rotki/issues/547).

I just made a test account with some DAI in it on a 1.0.4 rotki, then run it with 1.0.5 and did not see your problem. So I will definitely need to see the logs.
I'm also having this issue but I can't find the log file on Mac, where is it?

Also is there any way to downgrade the DB so I can get up and running?
Hey @hairychris!

> I'm also having this issue but I can't find the log file on Mac, where is it?

Did you also use v1.0.4 in the past and this happened with v1.0.5 as described also by @Aurifier?

For OSX the logs should be here: `~/Library/Logs/rotkehlchen`. Please post any logs you see there. If you feel more comfortable having them private you can also email them at: support@rotki.com

> Also is there any way to downgrade the DB so I can get up and running?

No I am afraid there is not but if I understand the problem I can make a hotfix release.
I have not had this working since last January, I was using version v0.6.0 and jumped straight to v1.0.5

```
~/Library/Logs/rotki/rotkehlchen.log
```

```                                                                                                            
30/12/2019 19:41:19 GMT -- INFO:rotkehlchen.rotkehlchen:Unlocking user user=chris, create_new=False, sync_approval=unknown
30/12/2019 19:41:19 GMT -- DEBUG:rotkehlchen.premium.sync:can sync data from server -- start
```

That's all, can I turn up the verbosity for you somehow? To be clear I do not have a premium subscription and I have not entered an API key.
Hey @hairychris.

There is also  `~/Library/Logs/rotkehlchenrotki_electron.log` . Apparently its placed in the wrong location (#588). Can you post its contents after your failed run?

> I was using version v0.6.0 and jumped straight to v1.0.5

This is most probably the problem. Since v0.6.0 there has been a major version upgrade and DB upgrades were not taken into account before that so a lot of things can have gone wrong.

Also it seems that whatever goes wrong happens at the user unlocking. An exception is probably thrown but nothing is logged. I just made an issue to fix this in future versions (#589).

But we can figure it out still! There are two ways:

### Easy but trusted way

For this you will have to send me the DB via an email at support@rotki.com. Also its password. I will try to unlock it using v1.0.5 and see whats wrong. But you will have to trust me that I will delete it right afterwards and that I will not look at anything inside the DB other than what is needed to debug the issue.

### Harder but no trust required

I assume you have v1.0.5. installed via a .dgm in OSX.

You will need to know how to use the python package manager (pip). [Here](https://pip.pypa.io/en/stable/installing/#installing-with-get-pip-py) is a guide on how to install it.

Once you have it installed, open two terminal windows. In one of them type:

`/Applications/rotki.app/Contents/Resources/app/rotkehlchen_py_dist/rotkehlchen-1.0.5-macos --zerorpc-port 4242 --logfile /Users/XXX/Library/Logs/rotkehlchen/rotkehlchen.log`

Replace `XXX` with your username. Now press `ENTER`.
You should see something like `start running on tcp://127.0.0.1:4242`

The above is essentially running the v1.0.5 rotki backend. This is the core of the application that is running under the frontend UI.

In the other terminal window (and assuming pip is installed fine) type:

`pip install zerorpc`

This should install a zerorpc client.

And now type the following to unlock your account in the same terminal window.

`zerorpc --timeout 1980 tcp://localhost:4242 unlock_user USERNAME PASSWORD False unknown '' ''`

Replace `USERNAME` with your username and `PASSWORD` with your password.

Once you do so and press `ENTER` you should see the same error you see via the front-end but also an entire stacktrace showing what exactly the problem is. Copy it all here.
Thanks for taking the time to debug with me, stacktrace below.

```
connecting to "tcp://localhost:4242"
Traceback (most recent call last):
  File "/Users/chris/.local/share/virtualenvs/rotbot-iNyCipiq/bin/zerorpc", line 8, in <module>
    sys.exit(main())
  File "/Users/chris/.local/share/virtualenvs/rotbot-iNyCipiq/lib/python3.7/site-packages/zerorpc/cli.py", line 310, in main
    return run_client(args)
  File "/Users/chris/.local/share/virtualenvs/rotbot-iNyCipiq/lib/python3.7/site-packages/zerorpc/cli.py", line 269, in run_client
    results = client(args.command, *call_args)
  File "/Users/chris/.local/share/virtualenvs/rotbot-iNyCipiq/lib/python3.7/site-packages/zerorpc/core.py", line 270, in __call__
    return self._process_response(request_event, bufchan, timeout)
  File "/Users/chris/.local/share/virtualenvs/rotbot-iNyCipiq/lib/python3.7/site-packages/zerorpc/core.py", line 238, in _process_response
    reply_event, self._handle_remote_error)
  File "/Users/chris/.local/share/virtualenvs/rotbot-iNyCipiq/lib/python3.7/site-packages/zerorpc/patterns.py", line 45, in process_answer
    raise exception
zerorpc.exceptions.RemoteError: Traceback (most recent call last):
  File "site-packages/zerorpc/core.py", line 153, in _async_task
  File "site-packages/zerorpc/patterns.py", line 30, in process_call
  File "site-packages/zerorpc/decorators.py", line 44, in __call__
  File "rotkehlchen/server.py", line 510, in unlock_user
  File "rotkehlchen/rotkehlchen.py", line 154, in unlock_user
  File "rotkehlchen/db/dbhandler.py", line 401, in get_settings
  File "rotkehlchen/db/settings.py", line 71, in db_settings_from_dict
TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'
```
All right we are very close to the answer! Some setting in your old DB has an unexpected value for Rotki v1.0.5.

Can you use `homebrew`? If yes that would be great since you can install `sqlcipher`, the library needed to read the encrypted rotki database. (I guess we can somehow access it from inside the .dmg but I would have to research about it and homebrew is faster)

Command should be `brew update && brew install sqlcipher`
Press `ENTER` to install it.

Now we can open the DB.

In a terminal type: `sqlcipher ~/.rotkehlchen/USERNAME/rotkehlchen.db`

Replace `USERNAME` with your username and press `ENTER`.

This should give you a prompt like below:

```
SQLCipher version 3.28.0 2019-04-16 19:49:53
Enter ".help" for usage hints.
sqlite> 
```

Inside this prompt type:

`sqlite> PRAGMA key="PASSWORD";`

Replace `PASSWORD` with your password and press `ENTER`.

Now your database is unlocked for this session.
It's all `sqlite` queries now.

Type: `SELECT * FROM settings ;` and press ENTER.

Paste the result here. None of the settings data should be sensitive in any way so it's totally safe.
```
taxfree_after_period|
ui_floating_precision|2
historical_data_start|01/12/2014
main_currency|GBP
balance_save_frequency|24
anonymized_logs|False
last_write_ts|1547309966
eth_rpc_endpoint|http://localhost:8545
version|8
```
@hairychris All right there is the problem. And this is a problem with v1.0.5 when `taxfree_after_period` is disabled. It's not a DB problem but it's an actual bug.

It has already been fixed in `develop` but from the `develop` branch the next release is v1.1.0 with a ton of changes coming out some time in January 2020.

To have a fix for this earlier probably a hotfix from master should be made.

The problem is that in v1.0.5 if the taxfree period setting is disabled there is no proper code to handle it:

https://github.com/rotki/rotki/blob/74a9d119c50210027426026c1b681f1f87d5e735/rotkehlchen/db/settings.py#L70-L71

Funnily enough it worked fine in v1.0.4:

https://github.com/rotki/rotki/blob/b13002ecf115da7c3cde137554428d4e278ee40a/rotkehlchen/db/dbhandler.py#L424-L425

And also is properly handled in develop (upcoming v1.1.0):
https://github.com/rotki/rotki/blob/c9508472bdc8ba025d5efc18d37f3aaee79adda0/rotkehlchen/db/settings.py#L105-L117

We will probably make a patch release for this.

And I suspect that @Aurifier has the same exact problem as it should have worked fine in v1.0.4.

@hairychris You don't need to wait for the patch release. You can also edit the DB and put a very big number of seconds for the time after taxfree period. This will effectively disable the setting. Once the patch release is out you can switch to properly disabling the setting.

The way to edit the DB is to get into the sqlcipher prompt as before but type the following:

`INSERT OR REPLACE INTO settings(name, value) VALUES("taxfree_after_period", 9999999999999);`

After that start Rotki. It should work again.

Resolving patch:
diff --git a/rotkehlchen/db/settings.py b/rotkehlchen/db/settings.py
--- a/rotkehlchen/db/settings.py
+++ b/rotkehlchen/db/settings.py
@@ -68,7 +68,23 @@ def db_settings_from_dict(
         elif key == 'include_crypto2crypto':
             specified_args[key] = read_boolean(value)
         elif key == 'taxfree_after_period':
-            specified_args[key] = int(value)
+            # taxfree_after_period can also be None, to signify disabled setting
+            if value is None:
+                specified_args[key] = value
+            else:
+                int_value = int(value)
+                if int_value <= 0:
+                    value = None
+                    msg_aggregator.add_warning(
+                        f'A negative or zero value ({int_value}) for taxfree_after_period '
+                        f'ended up in the DB. Setting it to None. Please open an issue in '
+                        f'Github: https://github.com/rotki/rotki/issues/new/choose',
+                    )
+
+                else:
+                    value = int_value
+
+                specified_args[key] = value
         elif key == 'balance_save_frequency':
             specified_args[key] = int(value)
         elif key == 'main_currency':


Test patch:
diff --git a/rotkehlchen/tests/db/test_db.py b/rotkehlchen/tests/db/test_db.py
--- a/rotkehlchen/tests/db/test_db.py
+++ b/rotkehlchen/tests/db/test_db.py
@@ -1038,3 +1038,21 @@ def test_add_ethereum_transactions(data_dir, username):
     assert len(warnings) == 0
     returned_transactions = data.db.get_ethereum_transactions()
     assert returned_transactions == [tx1, tx2, tx3]
+
+
+def test_can_unlock_db_with_disabled_taxfree_after_period(data_dir, username):
+    """Test that with taxfree_after_period being empty the DB can be opened
+
+    Regression test for https://github.com/rotki/rotki/issues/587
+    """
+    msg_aggregator = MessagesAggregator()
+    data = DataHandler(data_dir, msg_aggregator)
+    data.unlock(username, '123', create_new=True)
+    data.db.set_settings({'taxfree_after_period': None})
+
+    # now relogin and check that no exception is thrown
+    del data
+    data = DataHandler(data_dir, msg_aggregator)
+    data.unlock(username, '123', create_new=False)
+    settings = data.db.get_settings()
+    assert settings.taxfree_after_period is None