paritytech / substrate-api-sidecar

REST service that makes it easy to interact with blockchain nodes built using Substrate's FRAME framework.
https://paritytech.github.io/substrate-api-sidecar/dist/
GNU General Public License v3.0
245 stars 151 forks source link

v12.4.0 unable to decode older blocks on Moonbeam networks #992

Closed hyd628 closed 2 years ago

hyd628 commented 2 years ago

Description

The latest version of SAS is not able to decode older blocks on Moonbeam networks. This problem is not present on older versions of SAS (such as v11.3.9).

Steps to Reproduce

  1. install v12.4.0 of SAS
  2. export SAS_SUBSTRATE_WS_URL=wss://wss.api.moonbase.moonbeam.network
  3. start Sidecar instance locally: node_modules/.bin/substrate-api-sidecar
  4. curl -s http://127.0.0.1:8080/blocks/1075626

Expected vs. Actual Behavior

Expected:

The request should return the decoded block object

Actual:

Throws errors and unable to return the block object

Server side error:

2022-07-25 22:16:20 error: VEC: Unable to decode on index 2 createType(ExtrinsicV4):: createType(Call):: Call: failed decoding authorInherent.setAuthor:: Struct: failed on args: :: decodeU8a: failed at 0xaab5f4cfd28161e04c0e470743950009… on author (index 0/1):: DoNotConstruct: Cannot construct unknown type AuthorId 2022-07-25 22:16:20 error: RPC-CORE: getBlock(hash?: BlockHash): SignedBlock:: createType(SignedBlock):: Struct: failed on block: {"header":"Header","extrinsics":"Vec"}:: Struct: failed on extrinsics: Vec:: createType(ExtrinsicV4):: createType(Call):: Call: failed decoding authorInherent.setAuthor:: Struct: failed on args: :: decodeU8a: failed at 0xaab5f4cfd28161e04c0e470743950009… on author (index 0/1):: DoNotConstruct: Cannot construct unknown type AuthorId 2022-07-25 22:16:20 error: createType(SignedBlock):: Struct: failed on block: {"header":"Header","extrinsics":"Vec"}:: Struct: failed on extrinsics: Vec:: createType(ExtrinsicV4):: createType(Call):: Call: failed decoding authorInherent.setAuthor:: Struct: failed on args: :: decodeU8a: failed at 0xaab5f4cfd28161e04c0e470743950009… on author (index 0/1):: DoNotConstruct: Cannot construct unknown type AuthorId Error: createType(SignedBlock):: Struct: failed on block: {"header":"Header","extrinsics":"Vec"}:: Struct: failed on extrinsics: Vec:: createType(ExtrinsicV4):: createType(Call):: Call: failed decoding authorInherent.setAuthor:: Struct: failed on args: :: decodeU8a: failed at 0xaab5f4cfd28161e04c0e470743950009… on author (index 0/1):: DoNotConstruct: Cannot construct unknown type AuthorId at createTypeUnsafe (/Users/henryduong/Documents/workspace/sidecar2/node_modules/@polkadot/types-create/cjs/create/type.js:73:18) at TypeRegistry.createTypeUnsafe (/Users/henryduong/Documents/workspace/sidecar2/node_modules/@polkadot/types/cjs/create/registry.js:353:46) at RpcCore._formatOutput (/Users/henryduong/Documents/workspace/sidecar2/node_modules/@polkadot/rpc-core/cjs/bundle.js:427:21) at RpcCore._formatResult (/Users/henryduong/Documents/workspace/sidecar2/node_modules/@polkadot/rpc-core/cjs/bundle.js:232:27) at callWithRegistry (/Users/henryduong/Documents/workspace/sidecar2/node_modules/@polkadot/rpc-core/cjs/bundle.js:260:19) at processTicksAndRejections (node:internal/process/task_queues:96:5) 2022-07-25 22:16:20 error: GET /blocks/1075626 500 202ms 2022-07-25 22:16:20 error: RPC-CORE: getStorage(key: StorageKey, at?: BlockHash): StorageData:: Unable to decode storage system.events:: createType(Vec):: Vec:: Decoded input doesn't match input, received 0x590100000000000000585f8f09000000000200000001000000060199c6220000…7a9dc69b0edcc18742432e000000001b0000000000c88dc22d00000000000000 (4602 bytes), created 0x590100000000000000585f8f09000000000200000001000000060199c6220000…0000000000c88dc22d0000000000000000000000000000000000000000000000 (4618 bytes)

Client side error:

{"code":500,"message":"createType(SignedBlock):: Struct: failed on block: {\"header\":\"Header\",\"extrinsics\":\"Vec\"}:: Struct: failed on extrinsics: Vec:: createType(ExtrinsicV4):: createType(Call):: Call: failed decoding authorInherent.setAuthor:: Struct: failed on args: :: decodeU8a: failed at 0xaab5f4cfd28161e04c0e470743950009… on author (index 0/1):: DoNotConstruct: Cannot construct unknown type AuthorId","stack":"Error: createType(SignedBlock):: Struct: failed on block: {\"header\":\"Header\",\"extrinsics\":\"Vec\"}:: Struct: failed on extrinsics: Vec:: createType(ExtrinsicV4):: createType(Call):: Call: failed decoding authorInherent.setAuthor:: Struct: failed on args: :: decodeU8a: failed at 0xaab5f4cfd28161e04c0e470743950009… on author (index 0/1):: DoNotConstruct: Cannot construct unknown type AuthorId\n at createTypeUnsafe (/Users/henryduong/Documents/workspace/sidecar2/node_modules/@polkadot/types-create/cjs/create/type.js:73:18)\n at TypeRegistry.createTypeUnsafe (/Users/henryduong/Documents/workspace/sidecar2/node_modules/@polkadot/types/cjs/create/registry.js:353:46)\n at RpcCore._formatOutput (/Users/henryduong/Documents/workspace/sidecar2/node_modules/@polkadot/rpc-core/cjs/bundle.js:427:21)\n at RpcCore._formatResult (/Users/henryduong/Documents/workspace/sidecar2/node_modules/@polkadot/rpc-core/cjs/bundle.js:232:27)\n at callWithRegistry (/Users/henryduong/Documents/workspace/sidecar2/node_modules/@polkadot/rpc-core/cjs/bundle.js:260:19)\n at processTicksAndRejections (node:internal/process/task_queues:96:5)","level":"error"}%

TarikGul commented 2 years ago

Seems to be a type decoding problem.

Check v12.0.0 release. We no longer support pulling in custom types from apps-config, and expect users to connect their own type bundles to sidecar. We supply all the bridging necessary, but you will need to get the custom types required for moonbeam in order to decode those blocks properly.

In this case it's having a problem identifying a type that is not native to polkadot-js.

hyd628 commented 2 years ago

Thanks, I see.

It looks like apps-config was pulling Moonbeam types from here:

https://github.com/polkadot-js/apps/blob/51835328db5f0eb90a9efcc7bf5510704a7ab279/packages/apps-config/src/api/spec/moonbeam.ts

https://www.npmjs.com/package/moonbeam-types-bundle

How do I import this into Sidecar manually in the latest version? I tried generating a json file and setting "SAS_SUBSTRATE_TYPES" variable, but that did not work.

TarikGul commented 2 years ago

I believe you will have to assign it to the SAS_SUBSTRATE_TYPES_BUNDLE, if its the types bundle you are retrieving. I will do a run through as well for you to ensure it assigns the types from moonbeam.

I have also realized it might be worth having a tool that generates these types into a JSON file for users. It could be really helpful and make things a little more straight-forward.

hyd628 commented 2 years ago

Awesome, that'd be great.

(Im still not able to get the older blocks to decode setting the SAS_SUBSTRATE_TYPES_BUNDLE to the json file generated from moonbeam-types-bundle).

TarikGul commented 2 years ago

Hey @hyd628 Sorry for the delayed response, I was running into my own issues as it's been a second since I have dealt with the OverrideBundleType structure.

So the JSON file you want to pass to SAS_SUBSTRATE_TYPES_BUNDLE should look like:

{
    spec: {
        moonbeam: {
            types: {...}
        }
    }
}

Inside of the moonbeam object, you can also have things such as rpc, aliases, etc. I am currently writing a tool that will generate those files for you :), But yea I can confirm locally that it worked.

hyd628 commented 2 years ago

OK great, thank you. Look forward to the generation tool.

TarikGul commented 2 years ago

So the only thing that is odd is, via 12.4.1 there is an issue with moonbeam decoding that block given the types:

"createType(SignedBlock):: Struct: failed on block: {\"header\":\"Header\",\"extrinsics\":\"Vec<Extrinsic>\"}:: Struct: failed on extrinsics: Vec<Extrinsic>:: createType(ExtrinsicV4):: createType(Call):: Call: failed decoding ethereum.transact:: Struct: failed on args: {\"transaction\":\"Lookup83\"}:: decodeU8a: failed at 0xc8010000000000000000000000000000… on transaction (index 0/1): {\"hash\":\"H256\",\"nonce\":\"u256\",\"blockHash\":\"Option<H256>\",\"blockNumber\":\"Option<U256>\",\"transactionIndex\":\"Option<U256>\",\"from\":\"H160\",\"to\":\"Option<H160>\",\"value\":\"u256\",\"gasPrice\":\"Option<U256>\",\"maxFeePerGas\":\"Option<U256>\",\"maxPriorityFeePerGas\":\"Option<U256>\",\"gas\":\"u256\",\"input\":\"Bytes\",\"creates\":\"Option<H160>\",\"raw\":\"Bytes\",\"publicKey\":\"Option<H512>\",\"chainId\":\"Option<U64>\",\"standardV\":\"u256\",\"v\":\"u256\",\"r\":\"u256\",\"s\":\"u256\",\"accessList\":\"Option<Vec<EthAccessListItem>>\",\"transactionType\":\"Option<U256>\"}:: decodeU8a: failed at 0x09ee02315e10eacc7d1d8118d9f53a73… on input (index 12/23): Bytes:: Bytes: required length less than remainder, expected at least 15236, found 52",

Plugging in the types bundle works for everything pre v12.4.1 Not completely sure why the ethereum.transact type is having issues decode. But that being said everything should work for v12.4.0 for moonbeam

hyd628 commented 2 years ago

""createType(SignedBlock):: Struct: failed on block: {\"header\":\"Header\",\"extrinsics\":\"Vec\"}:: Struct: failed on extrinsics: Vec:: createType(ExtrinsicV4):: createType(Call):: Call: failed decoding ethereum.transact:: Struct: failed on args: {\"transaction\":\"Lookup83\"}:: decodeU8a: failed at 0xc8010000000000000000000000000000… on transaction (index 0/1): {\"hash\":\"H256\",\"nonce\":\"u256\",\"blockHash\":\"Option\",\"blockNumber\":\"Option\",\"transactionIndex\":\"Option\",\"from\":\"H160\",\"to\":\"Option\",\"value\":\"u256\",\"gasPrice\":\"Option\",\"maxFeePerGas\":\"Option\",\"maxPriorityFeePerGas\":\"Option\",\"gas\":\"u256\",\"input\":\"Bytes\",\"creates\":\"Option\",\"raw\":\"Bytes\",\"publicKey\":\"Option\",\"chainId\":\"Option\",\"standardV\":\"u256\",\"v\":\"u256\",\"r\":\"u256\",\"s\":\"u256\",\"accessList\":\"Option<Vec>\",\"transactionType\":\"Option\"}:: decodeU8a: failed at 0x09ee02315e10eacc7d1d8118d9f53a73… on input (index 12/23): Bytes:: Bytes: required length less than remainder, expected at least 15236, found 52"

This seems to be an error on the "input" field of an ethereum transaction which should not have any length checks (other than a maximum allowed maybe), because it could range from null for a simple transfer, to the encoded parameters for a function call, to the contract's bytecode in the case of a contract creation transaction.

The error messages looks like it might be related to SCALE types encoding:

https://github.com/paritytech/subport/issues/82 https://github.com/paritytech/polkadot-launch/pull/45

I'm not very familiar with how or where SCALE types are configured, but it would need to be changed to a type that accepts arbitrary length bytes with no minimum length checking.

hyd628 commented 2 years ago

Hey @TarikGul , were you able to finish the tool for generating the types file?

edit: oh wait I see the readme file has a link to it, let me try it out.

TarikGul commented 2 years ago

Yea I thought I had linked it in one of the other issues (Not totally sure though).

Heres the link to the repo. https://github.com/paritytech/generate-type-bundle

TarikGul commented 2 years ago

Closing this for now as it is fixed. Let me know if you have any issues moving forward with the types bundle package.

Thanks again for raising this issue.

hyd628 commented 2 years ago

Hey @TarikGul , thanks for making the types generator tool.

However, I tried generating and importing the types bundle as described and still not able to decode the same block in the issue.

Screen Shot 2022-08-14 at 4 56 00 PM Screen Shot 2022-08-14 at 4 56 14 PM

Am I missing something?

hyd628 commented 2 years ago

Here is a link to how I'm starting the sidecar instance:

https://asciinema.org/a/XhCvB1anBEKd44lcTvNfqr29N

TarikGul commented 2 years ago

Hey @TarikGul , thanks for making the types generator tool.

However, I tried generating and importing the types bundle as described and still not able to decode the same block in the issue.

Screen Shot 2022-08-14 at 4 56 00 PM Screen Shot 2022-08-14 at 4 56 14 PM

Am I missing something?

What was the command you used to make a typesBundle file? I noticed for moonbeam if I do the general typesBundle with no specific chain it works perfectly (typesBundle should have all chains in it). But if I specify just moonbeam if will have types issues. My guess being there is some secondary type information that it needs.

hyd628 commented 2 years ago

OK, I tried it again with the types generated for "moonbase" and it can decode most of the older blocks now, but still not the specific one mentioned in this issue, because of the problem you mentioned here with decoding the ethereum.transact type https://github.com/paritytech/substrate-api-sidecar/issues/992#issuecomment-1199607117

I'll investigate that issue a little bit more from the Moonbeam types bundle side.

Thanks for the help with creating the types generation tool.