subsquid / squid-sdk

The main repo of the squid SDK
Apache License 2.0
1.23k stars 152 forks source link

Incorrect typed enum variant and BoundedVec decoding #335

Closed khssnv closed 3 months ago

khssnv commented 3 months ago

Describe the bug

Decoding of BoundedVec<u8, _> and decoding of an enum with AccountId32 type variant return a part of the SCALE-encoded value instead of the properly decoded SS58-string for the address and decoded ASCII string for the vec of u8. For instance for a BoundedVec<u8, 255> containing ASCII-encoded string 38.242.236.247 it returns 0x3137382e3235312e3232382e323336. At the same time at https://polkadot.js.org/apps/#/chainstate all values correctly decoded and displayed as expected.

To Reproduce

  1. Setup the project.

    git clone --branch debug/khssnv-squid-sdk-decoding-bug-report https://github.com/Cerebellum-Network/cere-squid-indexer.git
    cd cere-squid-indexer
    npm i
  2. Launch processor.

    docker compose up -d db
    sqd process
  3. See badly decoded values in logs.

    ...
    DdcNodesProcessor.process.v48017
       Event NodeCreated (struct), field nodePubKey (enum): 0x9ef98ad9c3626ba725e78d76cfcfc4b4d07e84f0388465bc7eb992e3e117234a
    DdcNodesProcessor.processDdcNodesEvents.v48013
       Storage item StorageNode (struct), field pubKey (enum): 0x9ef98ad9c3626ba725e78d76cfcfc4b4d07e84f0388465bc7eb992e3e117234a
       Storage item StorageNode (struct), field host (BoundedVec): 0x33382e3234322e3233362e323437
       Storage item StorageNode (struct), field domain (BoundedVec): 0x73746f726167652d312e6465766e65742e636572652e6e6574776f726b
    ...

Expected behavior

For the log lines above expected values are:

A full SCALE-encoded value is 0x9ef98ad9c3626ba725e78d76cfcfc4b4d07e84f0388465bc7eb992e3e117234ac2c72f9ce9f1ca879c6a098e2cf729a538b0d341933d949cea755abffee4780a017f82864e4f097e63d04cc279e4d8d2eb45a42ffa3833382e3234322e3233362e3234377473746f726167652d312e6465766e65742e636572652e6e6574776f726b01901f82236e2302

Screenshots

Correctly decoded values displayed at https://polkadot.js.org/apps/#/chainstate. Fields pubKey, host, and domain are shown as expected. image

Environment (please complete the following information):

Applicable to decoding issues:

Additional context

Affected values type definitions:

I would also like to apologize for the flood of issues in recent days 😬. Thank you for swift responses!

belopash commented 3 months ago

Actually, this is intented behavior. Polkadot.js encodes those addresses under the hood, but our sdk don't. You can use https://github.com/subsquid/squid-sdk/tree/master/substrate/ss58 to get the address in ss58 format

khssnv commented 3 months ago

Indeed. The fact that the address is a variant of an enum does not affect the value returned from the SDK storage getter.

What about BoundedVec? Hex-string with a SCALE encoded value does not contain the length and it is not available from the SDK typegen. In my case on chain the length is a paramter type used for a stored struct. Is it intended to keep track of updates for the length manually?

khssnv commented 3 months ago

A code snippet for someone who have the same problem with ASCII decoding from BoundedVec.

import { TypeRegistry, VecFixed } from '@polkadot/types'

const scaleEncodedAsciiString = '0x33382e3234322e3233362e323436' // SCALE encoded ASCII numbers for string "38.242.236.246"
const boundedVecCapacity = 255
const vecFixedAsciiString = new VecFixed(new TypeRegistry(), 'u8', boundedVecCapacity, scaleEncodedAsciiString)
const endOfAsciiString= vecFixedAsciiString.toU8a().indexOf(0)
const decodedValue = String.fromCharCode(...vecFixedAsciiString.toU8a().slice(0, endOfAsciiString))
console.log(`Decoded ${decodedValue.length} characters: "${decodedValue}".`)
eldargab commented 3 months ago

Bounded vector is encoded exactly like a regular vector and exported to the metadata exactly like a regular vector. Hence, the SDK can't distinguish between the two and does not need to.

khssnv commented 3 months ago

The SDK functions return primitive types like booleans and numbers decoded from SCALE and it made me to expect the vector to come decoded too. Alright, thanks! Please close the issue.