ethereum / pyethapp

MIT License
1.28k stars 605 forks source link

add py3 compatibility to pyethapp #261

Closed changwu-tw closed 6 years ago

changwu-tw commented 7 years ago

Make pyethapp work both on py2/py3.

hwwhww commented 6 years ago

In Python3, if we restart the daemon, the type of network_id in the DB is Python2 str so it raises an exception.

Traceback (most recent call last):
  File "/usr/local/bin/pyethapp", line 11, in <module>
    load_entry_point('pyethapp==1.5.0', 'console_scripts', 'pyethapp')()
  File "/usr/local/lib/python3.6/site-packages/click-6.7-py3.6.egg/click/core.py", line 722, in __call__
    return self.main(*args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/click-6.7-py3.6.egg/click/core.py", line 697, in main
    rv = self.invoke(ctx)
  File "/usr/local/lib/python3.6/site-packages/click-6.7-py3.6.egg/click/core.py", line 1066, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/usr/local/lib/python3.6/site-packages/click-6.7-py3.6.egg/click/core.py", line 895, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/usr/local/lib/python3.6/site-packages/click-6.7-py3.6.egg/click/core.py", line 535, in invoke
    return callback(*args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/click-6.7-py3.6.egg/click/decorators.py", line 17, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/pyethapp-1.5.0-py3.6.egg/pyethapp/app.py", line 232, in run
    service.register_with_app(app)
  File "/usr/local/lib/python3.6/site-packages/devp2p-0.9.2-py3.6.egg/devp2p/service.py", line 47, in register_with_app
    s = klass(app)
  File "/usr/local/lib/python3.6/site-packages/pyethapp-1.5.0-py3.6.egg/pyethapp/eth_service.py", line 150, in __init__
    self.config['data_dir'], db_network_id, sce['network_id']
RuntimeError: The database in '/root/.config/pyethapp' was initialized with network id bytearray(b'1') and can not be used when connecting to network id 1. Please choose a different data directory.

I think we can use ethereum.utils.to_string to fix it.

--- a/pyethapp/eth_service.py
+++ b/pyethapp/eth_service.py
@@ -34,7 +34,10 @@ from ethereum.slogging import get_logger
 from ethereum.exceptions import InvalidTransaction, InvalidNonce, \
     InsufficientBalance, InsufficientStartGas, VerificationFailed
 from ethereum.transactions import Transaction
-from ethereum.utils import encode_hex
+from ethereum.utils import (
+    encode_hex,
+    to_string,
+)

 from .synchronizer import Synchronizer
 from . import eth_protocol
@@ -142,8 +145,8 @@ class ChainService(WiredService):
             self.db.put("I am not pruning", "1")

         if 'network_id' in self.db:
-            db_network_id = self.db.get('network_id')
-            if db_network_id != str(sce['network_id']):
+            db_network_id = self.db.get(b'network_id')
+            if db_network_id != to_string(sce['network_id']):
                 raise RuntimeError(
                     "The database in '{}' was initialized with network id {} and can not be used "
                     "when connecting to network id {}. Please choose a different data directory.".format(
@@ -152,7 +155,7 @@ class ChainService(WiredService):
                 )

         else:
-            self.db.put('network_id', str(sce['network_id']))
+            self.db.put(b'network_id', to_string(sce['network_id']))
             self.db.commit()