ethereum / pyethapp

MIT License
1.28k stars 605 forks source link

quantity_decoder fails after a JSON RPC request to geth server #95

Open rethore opened 8 years ago

rethore commented 8 years ago

Hey, I'm a noob at this, so I'm probably doing something wrong. I'm on OSX 10.11.1. geth v1.3.3 & pyethapp 1.0.18/darwin/py2.7.10. I have tried to connect to a geth --rpc command running locally on my machine. The geth command seems to run fine, synchronised well and everything working without issue. I wanted to start playing with the pyethapp library, and decided to check an account balance using the JSONRPCClient.

Running the command:

from pyethapp.rpc_client import JSONRPCClient
c = JSONRPCClient(port=8545)
c.balance('0xb794f5ea0ba39494ce839613fffba74279579268')

... raises the following error:

{
  "jsonrpc": "2.0", 
  "params": [
    "0xb794f5ea0ba39494ce839613fffba74279579268", 
    "pending"
  ], 
  "method": "eth_getBalance", 
  "id": 4
}
{"id":4,"jsonrpc":"2.0","result":"0x06d410721e7c1b1eff0228"}

---------------------------------------------------------------------------
BadRequestError                           Traceback (most recent call last)
<ipython-input-8-33c127b5f25c> in <module>()
----> 1 c.balance('0xb794f5ea0ba39494ce839613fffba74279579268')

/Users/pe/.virtualenvs/eth27/lib/python2.7/site-packages/pyethapp/rpc_client.pyc in balance(self, account)
    160     def balance(self, account):
    161         b = quantity_decoder(
--> 162             self.call('eth_getBalance', address_encoder(account), 'pending'))
    163         return b
    164 

/Users/pe/.virtualenvs/eth27/lib/python2.7/site-packages/pyethapp/jsonrpc.pyc in quantity_decoder(data)
    274             success = False
    275     assert not success
--> 276     raise BadRequestError('Invalid quantity encoding')
    277 
    278 

BadRequestError: Invalid quantity encoding

So after firing up a little debugging session it seems that the error is coming from this method. The data is not passing the test at line 264 because it has a leading 0.

file pyethapp/jsonrpc.py:

    258 def quantity_decoder(data):
    259     """Decode `data` representing a quantity."""
    260     if not is_string(data):
    261         success = False
    262     elif not data.startswith('0x'):
    263         success = False  # must start with 0x prefix
    264     elif len(data) > 3 and data[2] == '0':
    265         success = False  # must not have leading zeros (except `0x0`)
    266     else:
    267         data = data[2:]
    268         # ensure even length
    269         if len(data) % 2 == 1:
    270             data = '0' + data
    271         try:
    272             return int(data, 16)
    273         except ValueError:
    274             success = False
    275     assert not success
    276     raise BadRequestError('Invalid quantity encoding')

If I try to manually decode it using the command int(data, 16) line 272, it seems to run just fine, so I'm wondering if the test line 264 is useful in that context. Maybe this library isn't meant to be talking to a geth rpc server? After a quick look at the JSON RPC API, the example produced there seems to also have a leading 0. So is this a bug?

Cheers, PE

rethore commented 8 years ago

@jnnk I'm not sure if you remember why you added that test line 264. Can I safely remove it? Cheers, PE