andelf / tronpy

TRON Python Client Library.
MIT License
212 stars 96 forks source link

tron.get_block returns error response #5

Closed MioYvo closed 4 years ago

MioYvo commented 4 years ago

Python: 3.7 tronpy: 0.1.5 error:

>> tronpy.Tron().get_block(24223718)
{'Error': 'class com.alibaba.fastjson.JSONException : unclosed string : _'}

I found only block 24223718 have problems. I've reported this to Tron offical telegram group, no reply yet.

But:

tron.py:

    def get_block(self, id_or_num: Union[None, str, int] = None) -> dict:
        """Get block from a block id or block number."""

        if isinstance(id_or_num, (int,)):
            block = self.provider.make_request("wallet/getblockbynum", {"num": id_or_num, "visible": True})
        elif isinstance(id_or_num, (str,)):
            block = self.provider.make_request("wallet/getblockbyid", {"value": id_or_num, "visible": True})
        elif id_or_num is None:
            block = self.provider.make_request("wallet/getnowblock", {"visible": True})
        else:
            raise TypeError("can not infer type of {}".format(id_or_num))

        if block:
            return block
        else:
            raise BlockNotFound

"wallet/getblockbynum", "wallet/getblockbyid", "wallet/getnowblock" do not have the "visible" parameter now (maybe exists before) ref: https://developers.tron.network/reference#walletgetblockbynum

andelf commented 4 years ago

Sorry, as an HTTP client library, tronpy can't fix the json encoding/decoding bug in java-tron.

This is an upstream bug.

visible is a parameter passed to JsonFormat, every HTTP API has a visible parameter. Doc sucks. Code speaks.

Ref: https://github.com/tronprotocol/java-tron/blob/develop/framework/src/main/java/org/tron/core/services/http/GetBlockByIdServlet.java#L38

      PostParams params = PostParams.getPostParams(request);
      BytesMessage.Builder build = BytesMessage.newBuilder();
      JsonFormat.merge(params.getParams(), build, params.isVisible());
      fillResponse(params.isVisible(), build.getValue(), response);

Thanks.

And for convenience, tronpy won't switch to non-visible request.

MioYvo commented 4 years ago

A "visible" param to Tron.get_block ? Users can process the address format (to_base58check_address) outside without getting an error. :|

andelf commented 4 years ago

Good point, will fix this now.

andelf commented 4 years ago

😁Published as v0.1.6. You can use pip install -U tronpy to test the new API.