Skip to content

Using pubsub query with big values results in an int64 overflow  #726

Closed

Description

@freak12techno

Bug Report

Setup

CometBFT version (use cometbft version or git rev-parse --verify HEAD if installed from source): should be latest

Have you tried the latest version: no

ABCI app (name for built-in, URL for self-written if it's publicly available): Rebus

Environment:

  • OS (e.g. from /etc/os-release): Ubuntu
  • Install tools:
  • Others:

node command runtime flags:

Config

What happened?

I am building a tool to subscribe to transactions and messages I need. Internally I use Query /libs/pubsub/query/query.go to filter out messages I do not need. One of my goals is to use values comparison (for example, as in this filter: delegate.amount > 10000000, to filter out MsgDelegate messages with amount less than 10m native tokens). It works okay with small values, but on chains with a different denom exponent (namely, Rebus, with exponent 18, and probably Evmos (haven't tested it with it, but it should result in a similar error)), when trying to subscribe to that filter, it would result in the following error:

7:52PM ERR Error querying for events err="failed to match against query message.action = '/cosmos.staking.v1beta1.MsgDelegate' AND delegate.amount > 10000000000000000000: got strconv.ParseInt: parsing \"10000000000000000000\": value out of range while trying to parse 10000000000000000000 as int64 (should never happen if the grammar is correct)" module=pubsub server=node

And if you do the same in app (try to create a filter from the following string and use query.Matches() on it, it would result in the error in this app.

What did you expect to happen?

Conversion is done without any errors.

How to reproduce it

  • Start a Rebus node (or any other node that has denom exponent 18 or bigger)
  • Subscribe via Tendermint Websockets to the updates on the following filter: message.action = '/cosmos.staking.v1beta1.MsgDelegate' AND delegate.amount > 10000000000000000000
  • Check logs and see errors if such transactions would occur, get a message like the one above per each.

Logs

See above

dump_consensus_state output

Anything else we need to know

This is a copy of tendermint/tendermint#9978 which was closed as stale but apparently never fixed.

Activity

  1. added
    bug
    needs-triage
    on Apr 17, 2023
  2. added and removed
    needs-triage
    on Apr 18, 2023
  3. thanethomson commented on Apr 24, 2023

    @thanethomson
    Contributor

    @freak12techno could you please clarify which version(s) of CometBFT you're using? This will help in deciding the version(s) of CometBFT to target with a fix.

  4. freak12techno commented on Apr 24, 2023

    @freak12techno
    Author

    @thanethomson I haven't tested this with CometBFT but tested it with Tendermint with a Rebus node, its Tendermint version is apparently v0.34.22.
    Additionally I asked CometBFT devs in Discord and they confirmed this is indeed still an issue with the latest versions.

  5. andynog commented on Apr 24, 2023

    @andynog
    Contributor

    Just checking the file libs/pubsub/query/query.go. Looks like this file was completely revamped last year (around Sep/2022)

    The main branch doesn't have the logic that is causing this error. I was looking for this text (should never happen if the grammar is correct) in the code and couldn't find it in the main branch.
    https://github.com/cometbft/cometbft/blob/main/libs/pubsub/query/query.go

    But looking at CometBFT v0.37 release, it seems the code it's still there, and this line seems to be the one causing it.

    value, err := strconv.ParseInt(number, 10, 64)

    The problem is that this amount is greater than Int64 so it will error. You can see an example here: https://go.dev/play/p/Bm7JVDAfSQD

    This might even cause issues because if you look at the example above, the strconv.ParseInt function returns a value, err and if the code doesn't check for err the value is assigned the int64 max value which can lead to unintended consequences (but the code handles err so it's fine here).

    The way the current code on main branch handles it by using a strconv.ParseFloat that will output a 10000000000000000000.000000 value and it will not error out.

    Another solution to handle these large denoms (10^18) would be to use a library that can handle Big Integers (BigInt). The cosmos-sdk uses a library that can handle these integers with 256 bits https://pkg.go.dev/cosmossdk.io/math#pkg-overview, Maybe use something like https://pkg.go.dev/cosmossdk.io/math#NewIntFromString that returns an Int (which is a wrapped BigInt). But this is probably needed if you are doing some arithmetic with these numbers, and the library would help with that. In this case just for parsing the number to be used in a query might not make much sense using this library.

  6. freak12techno commented on Apr 25, 2023

    @freak12techno
    Author

    To add my two cents here: having values that exceed 2^64 is quite a common usecase, as we have quite a few chains with denom exponent as 18 (few examples I know: Rebus, Evmos).
    As cosmos-sdk uses BigInt internally, I think the best way to solve it is to use BigInt in CometBFT as well, what do you think?

    But this is probably needed if you are doing some arithmetic with these numbers, and the library would help with that.

    There's also an issue when CometBFT itself isn't able to determine whether the event matches the filter someone subscribed to. I also had it in my own app, where I used Query internally, but it happens also on the node itself when checking if event matches the filter, it doesn't do any conversions there to work with numbers except for determining if it's matching the filter.

  7. self-assigned this
    on Apr 27, 2023
  8. added this to the 2023-Q2 milestone on Apr 27, 2023
  9. jmalicevic commented on Apr 27, 2023

    @jmalicevic
    Collaborator

    @freak12techno , can you confirm which version of CometBFT you are using?

  10. freak12techno commented on Apr 27, 2023

    @freak12techno
    Author

    @thanethomson see my answer here #726 (comment)

  11. jmalicevic commented on Apr 27, 2023

    @jmalicevic
    Collaborator

    Oh sorry, don't know how I missed that. Yes that is in line with what we are expecting, 0.37.x and earlier versions all parse non-float numbers as int64. Thanks for the quick feedback :)

  12. linked a pull request that will close this issuev0.37 pubsub/kvindexer: Handle big ints #771on Apr 27, 2023
  13. moved this from Todo to In Progress in CometBFT 2023on Apr 28, 2023
  14. jmalicevic commented on May 11, 2023

    @jmalicevic
    Collaborator

    @freak12techno this should be fixed now with #798 for 0.34.x and #771 for v0.37.x and should be out in the next release of these versions.

  15. moved this from In Progress to Done in CometBFT 2023on May 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugrpc

Type

No type

Projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions