eosjs icon indicating copy to clipboard operation
eosjs copied to clipboard

Endianness of uint128 numbers

Open keisuke-kanao opened this issue 8 years ago • 11 comments

First, I pushed an action using cleos. One of the action parameters is of type uint128 and I specified that like: {"number": 0xb3cb484ada4279ab609ca314e4209e11}. After the transaction, I did "cleos get table ..." and I saw the same number as it is in the contract table. Then, I did the same process using eosjs. This time, I specified this uint128 parameter value like: {number: '238987313771706096047051733442425298449'} since eosjs only accepts decimal format. And found that the contract table stores that value as {number: '0x119e20e414a39c60ab7942da4a48cbb3'}. This is natural considering the implementations of decimalToBinary() and binaryToDecimal() in eojs-numeric.ts. So looks like that it would be easy to modify these implementations to be compatible with cleos. I would like to know what are the intents or reasons for these current implementations.

keisuke-kanao avatar Nov 18 '18 07:11 keisuke-kanao

eosjs and abieos do this to maintain consistency with all the integer types. The abi serializer used by cleos and nodeos treats (u)int128 as a raw bytes type instead of a numeric type to simplify its internal implementation.

tbfleming avatar Nov 18 '18 14:11 tbfleming

How to pass uint128_t number as action parameter?

lowesyang avatar Aug 03 '19 14:08 lowesyang

Update on the original question: we're considering switching nodeos and cleos to decimal for (u)int128.

tbfleming avatar Aug 03 '19 15:08 tbfleming

How to pass uint128_t number as action parameter?

In eosjs, quote it in decimal form. e.g. "485829384751".

tbfleming avatar Aug 03 '19 15:08 tbfleming

example in eosjs:

...,
actions: [{
      account: 'foo',
      name: 'bar',
      authorization: [{
        actor: 'useraaaaaaaa',
        permission: 'active',
      }],
      data: [{
          my_uint128_arg: '4959694834837',
          my_int128_arg:  '-857684798757643',
      }],
}],
...

tbfleming avatar Aug 03 '19 15:08 tbfleming

But when i pass a very big uint128_t number like 131613249979637300000000000000000000000, i got invalid number error. Is there anything wrong?

lowesyang avatar Aug 03 '19 16:08 lowesyang

Did you quote it? "131613249979637300000000000000000000000"

tbfleming avatar Aug 03 '19 16:08 tbfleming

Oh my fault. Quote it and it's ok~ Thanks

lowesyang avatar Aug 03 '19 16:08 lowesyang

But in my table i have a data entry with a uint128_t value 0x01000000000000001052bc9483ac3036 which is little endian encoded from 131613249979637306740937603029919596560. I think hex string should be encoded in big endian order. And when i pass either little endian decimal number or big endian decimal format, i cannot get the target uint128_t value from table.

When i pass hex string which retrieved from the table, it throw invalid number.

Code is below: image

Table: image

image

lowesyang avatar Aug 05 '19 04:08 lowesyang

Like I said, we're considering changing the way nodeos and cleos handle this.

tbfleming avatar Aug 05 '19 07:08 tbfleming

Thank you for the update on this issue.

keisuke-kanao avatar Aug 08 '19 02:08 keisuke-kanao