libethc icon indicating copy to clipboard operation
libethc copied to clipboard

Combined encoding of address[] and uint8[] and other types is giving corrupted(incorrect result) result

Open DerXanRam opened this issue 6 months ago • 13 comments

the function calling is

getPoolInfoBatchAsString(address[],uint8[])

the encoded data are

pool addresses: 0xe6b4903642b4a1637d7b411d009af5c91617860c,0x12d59b7849989f677a2d67fcf21b3d2e28291da6,0x2c7fe80e727408751b086d5e2f257ab20cd97f69,0x68f940fb79e216faafbc96a6701b11b44f5255c2,0x8da0d9418e74c8523cc189be07589a788a1c49e7,0xe3b3aa98faa70bf098ea5e75e11193de95ab7cda,0x9950c1f3754fb8a3ebbaf24b8573cafc7474c00f,0xafecdd2fc04f0939d7b6835529677608470c063d,0xc4081a370585681e265ec9efb84143549469e77e


pool versions: 1,0,0,1,0,0,1,1,0

the correct call data is

000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000001800000000000000000000000000000000000000000000000000000000000000009000000000000000000000000e6b4903642b4a1637d7b411d009af5c91617860c00000000000000000000000012d59b7849989f677a2d67fcf21b3d2e28291da60000000000000000000000002c7fe80e727408751b086d5e2f257ab20cd97f6900000000000000000000000068f940fb79e216faafbc96a6701b11b44f5255c20000000000000000000000008da0d9418e74c8523cc189be07589a788a1c49e7000000000000000000000000e3b3aa98faa70bf098ea5e75e11193de95ab7cda0000000000000000000000009950c1f3754fb8a3ebbaf24b8573cafc7474c00f000000000000000000000000afecdd2fc04f0939d7b6835529677608470c063d000000000000000000000000c4081a370585681e265ec9efb84143549469e77e0000000000000000000000000000000000000000000000000000000000000009000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000

libethc generating is

000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000001600000000000000000000000000000000000000000000000000000000000000009000000000000000000000000e6b4903642b4a1637d7b411d009af5c91617860c00000000000000000000000012d59b7849989f677a2d67fcf21b3d2e28291da60000000000000000000000002c7fe80e727408751b086d5e2f257ab20cd97f6900000000000000000000000068f940fb79e216faafbc96a6701b11b44f5255c20000000000000000000000008da0d9418e74c8523cc189be07589a788a1c49e7000000000000000000000000e3b3aa98faa70bf098ea5e75e11193de95ab7cda0000000000000000000000009950c1f3754fb8a3ebbaf24b8573cafc7474c00f000000000000000000000000afecdd2fc04f0939d7b6835529677608470c063d000000000000000000000000c4081a370585681e265ec9efb84143549469e77e0000000000000000000000000000000000000000000000000000000000000009000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000

the code

struct eth_abi abi = {};
char *fn = strdup(function.c_str());
size_t hexlen;
eth_abi_init(&abi, ETH_ABI_ENCODE);
eth_abi_call(&abi, &fn, NULL);

eth_abi_array(&abi, NULL); // array beign
     eth_abi_address(&abi, "0xe6b4903642b4a1637d7b411d009af5c91617860c");
     eth_abi_address(&abi, "0x12d59b7849989f677a2d67fcf21b3d2e28291da6");
     eth_abi_address(&abi, "0x2c7fe80e727408751b086d5e2f257ab20cd97f69");
     eth_abi_address(&abi, "0x68f940fb79e216faafbc96a6701b11b44f5255c2");
     eth_abi_address(&abi, "0x8da0d9418e74c8523cc189be07589a788a1c49e7");
     eth_abi_address(&abi, "0xe3b3aa98faa70bf098ea5e75e11193de95ab7cda");
     eth_abi_address(&abi, "0x9950c1f3754fb8a3ebbaf24b8573cafc7474c00f");
     eth_abi_address(&abi, "0xafecdd2fc04f0939d7b6835529677608470c063d");
     eth_abi_address(&abi, "0xc4081a370585681e265ec9efb84143549469e77e");
eth_abi_array_end(&abi); // array end

eth_abi_array(&abi, NULL); // array beign
     eth_abi_uint8(&abi, 1);
     eth_abi_uint8(&abi, 0);
     eth_abi_uint8(&abi, 0);
     eth_abi_uint8(&abi, 1);
     eth_abi_uint8(&abi, 0);
     eth_abi_uint8(&abi, 0);
     eth_abi_uint8(&abi, 1);
     eth_abi_uint8(&abi, 1);
     eth_abi_uint8(&abi, 0);
eth_abi_array_end(&abi); // array end
eth_abi_call_end(&abi);
eth_abi_to_hex(&abi, &hex, &hexlen)
string data(hex);

if you can please check your change by doing regression test to check the new change doesn't affect other functionalities. I know you are very busy and and took you the whole week to response. I check my email every single day, all day for your response and i don't say anything to not nag you. 🙏

DerXanRam avatar Aug 07 '25 21:08 DerXanRam

not only address[] and uint8[] combination, it is for all combination. here is an example for getPoolInfoBatchAsString(address[],uint8[],address[],address,uint256,uint256)

Real 790b6b3c00000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000000014000000000000000000000000000000000000000000000000000000000000001c000000000000000000000000068f940fb79e216faafbc96a6701b11b44f5255c2000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000150000000000000000000000000000000000000000000000000000000000000003000000000000000000000000e6b4903642b4a1637d7b411d009af5c91617860c00000000000000000000000012d59b7849989f677a2d67fcf21b3d2e28291da60000000000000000000000002c7fe80e727408751b086d5e2f257ab20cd97f6900000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003000000000000000000000000e6b4903642b4a1637d7b411d009af5c91617860c00000000000000000000000012d59b7849989f677a2d67fcf21b3d2e28291da60000000000000000000000002c7fe80e727408751b086d5e2f257ab20cd97f69

libethc result

790b6b3c00000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000000012000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000068f940fb79e216faafbc96a6701b11b44f5255c2000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000150000000000000000000000000000000000000000000000000000000000000003000000000000000000000000e6b4903642b4a1637d7b411d009af5c91617860c00000000000000000000000012d59b7849989f677a2d67fcf21b3d2e28291da60000000000000000000000002c7fe80e727408751b086d5e2f257ab20cd97f6900000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003000000000000000000000000e6b4903642b4a1637d7b411d009af5c91617860c00000000000000000000000012d59b7849989f677a2d67fcf21b3d2e28291da60000000000000000000000002c7fe80e727408751b086d5e2f257ab20cd97f69

DerXanRam avatar Aug 07 '25 21:08 DerXanRam

Hi @DerXanRam thanks for opening the issue. I'm on vacation right now, that's why it's taking a bit longer to respond & react to the problems. Trying my best to cover all the issues. Thanks for understanding.

mhw0 avatar Aug 09 '25 11:08 mhw0

Ok tnx bro👍

DerXanRam avatar Aug 09 '25 11:08 DerXanRam

Hi @DerXanRam thanks for opening the issue. I'm on vacation right now, that's why it's taking a bit longer to respond & react to the problems. Trying my best to cover all the issues. Thanks for understanding.

Any progress bro?

DerXanRam avatar Aug 17 '25 05:08 DerXanRam

Hi @DerXanRam Fixed the issue. Refactoring right now. Pushing the patch soon.

mhw0 avatar Aug 17 '25 17:08 mhw0

Hello @DerXanRam Pushed the changes. Can you please check if the issue is fixed?

Added your case in this test: https://github.com/mhw0/libethc/blob/8110d2ae35cd8bac13a1c74c7e625dfa42d80fdb/test/test-eth-abi.c#L626-L648

Also made some changes to the API. eth_abi_address used to assign allocated memory to the input as output. Now, you have to pass preallocated memory into it. I think this makes more sense as the size of address is always fixed.

mhw0 avatar Aug 19 '25 15:08 mhw0

Hello @DerXanRam Pushed the changes. Can you please check if the issue is fixed?

Added your case in this test:

libethc/test/test-eth-abi.c

Lines 626 to 648 in 8110d2a

assert(eth_abi_array(&abi0, NULL) == ETH_OK); eth_abi_address(&abi0, addr0); eth_abi_address(&abi0, addr1); eth_abi_address(&abi0, addr2); eth_abi_address(&abi0, addr3); eth_abi_address(&abi0, addr4); eth_abi_address(&abi0, addr5); eth_abi_address(&abi0, addr6); eth_abi_address(&abi0, addr7); eth_abi_address(&abi0, addr8); assert(eth_abi_array_end(&abi0) == ETH_OK);

assert(eth_abi_array(&abi0, NULL) == ETH_OK); eth_abi_uint8(&abi0, &int0); eth_abi_uint8(&abi0, &int1); eth_abi_uint8(&abi0, &int2); eth_abi_uint8(&abi0, &int3); eth_abi_uint8(&abi0, &int4); eth_abi_uint8(&abi0, &int5); eth_abi_uint8(&abi0, &int6); eth_abi_uint8(&abi0, &int7); eth_abi_uint8(&abi0, &int8); assert(eth_abi_array_end(&abi0) == ETH_OK); Also made some changes to the API. eth_abi_address used to assign allocated memory to the input as output. Now, you have to pass preallocated memory into it. I think this makes more sense as the size of address is always fixed.

sorry for late response...testing

DerXanRam avatar Aug 23 '25 19:08 DerXanRam

Sorry for late, accident happened.

when uint8 comes first, still it encodes incorrectly.

DerXanRam avatar Sep 06 '25 07:09 DerXanRam

Hi @DerXanRam can you give example so I can reproduce the issue?

mhw0 avatar Sep 10 '25 09:09 mhw0

truct eth_abi abi = {};
    std::string function = "getPoolInfoBatchAsString(uint8[],address[])";
    char *fn = strdup(function.c_str());
    size_t hexlen;
    eth_abi_init(&abi, ETH_ABI_ENCODE);
    eth_abi_call(&abi, &fn, NULL);

    char *add1 = "0xe6b4903642b4a1637d7b411d009af5c91617860c";
    char *add2 = "0x12d59b7849989f677a2d67fcf21b3d2e28291da6";
    char *add3 = "0x2c7fe80e727408751b086d5e2f257ab20cd97f69";


    uint8_t n1 = 1;
    uint8_t n2 = 0;
    uint8_t n3 = 0;
    eth_abi_array(&abi, NULL); // array beign
    eth_abi_uint8(&abi, &n1);
    eth_abi_uint8(&abi, &n2);
    eth_abi_uint8(&abi, &n3);

    eth_abi_array_end(&abi); // array end
    
    eth_abi_array(&abi, NULL); // array beign
    eth_abi_address(&abi, &add1);
    eth_abi_address(&abi, &add2);
    eth_abi_address(&abi, &add3);
    eth_abi_array_end(&abi); // array end
    
    eth_abi_call_end(&abi);
    char *hex;
    eth_abi_to_hex(&abi, &hex, &hexlen);
    std::string data(hex);
    std::cout<<"encoded "<<data<<std::endl;

expected result

2b4276ed000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003000000000000000000000000e6b4903642b4a1637d7b411d009af5c91617860c00000000000000000000000012d59b7849989f677a2d67fcf21b3d2e28291da60000000000000000000000002c7fe80e727408751b086d5e2f257ab20cd97f69

libethc result

2b4276ed000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003000000000000000000000000e6b4903642b4a1637d7b411d009af5c91617860c00000000000000000000000012d59b7849989f677a2d67fcf21b3d2e28291da60000000000000000000000002c7fe80e727408751b086d5e2f257ab20cd97f69

DerXanRam avatar Sep 10 '25 12:09 DerXanRam

hello

DerXanRam avatar Sep 23 '25 09:09 DerXanRam

Hi buddy. Very very sorry for the big delay. Have to finish some things. I’ll be back starting next month

mhw0 avatar Sep 23 '25 15:09 mhw0

Hi @DerXanRam I tested your code and getting the expected result. Could you confirm that you got that result from libethc?

mhw0 avatar Oct 04 '25 06:10 mhw0