subql icon indicating copy to clipboard operation
subql copied to clipboard

Support state_traceBlock for substrate chains

Open ianhe8x opened this issue 1 year ago • 0 comments

Prerequisites

  • [ ] Are you running the latest version(s)?
  • [ ] Have you searched the documentation for your issue?
  • [ ] Did you search for an existing issue or pull request?

Description

We can offer a way to index storage changes without knowing what extrinsic or event cause it. It is extremely useful to trace balance changes. The downside is this handler has requirement to the chain endpoint.

Details

It will look like

dataSources:
  - kind: substrate/Runtime
    startBlock: 1
    mapping:
      file: ./dist/index.js
      handlers:
        - handler: handleStateTrace
          kind: substrate/StateTraceHandler
          filter:
            keyPrefix: 0x....
            # or with both module & method
            module: balances
            method: account

Underlying

  • it uses the unsafe rpc state_traceBlock
    • to avoid abuse of the rpc and also reduce the response size, we should by mandatory set topic to state, methods to Put,Kill,Append,ChildKill,ChildPut,ClearPrefix,ChildClearPrefix, and a non-empty key prefix.
  • this handler should supports skipBlock & skipTransaction options
  • to get key prefix, use balance as example, we can get from api.query.system.account.keyPrefix(), note that 0x need to be removed before feeding it to state_traceBlock
  • An example state_traceBlock looks like (for polkadot)
curl -s -X POST -H 'Content-type: application/json' --data '{"jsonrpc":"2.0","method":"state_traceBlock","params":["0x93a74e08015ff32837f98e1e441cb92810600d2305daf71c20e04cb55eead827", "state", "26aa394eea5630e07c48ae0c9558cef7b99d880ec681799c0cf30e8886371da9", "Put,Kill,Append,ChildKill,ChildPut,ClearPrefix,ChildClearPrefix"],"id":1}' <chain api>
  • the response looks like
{"jsonrpc":"2.0","result":{"blockTrace":{"blockHash":"93a74e08015ff32837f98e1e441cb92810600d2305daf71c20e04cb55eead827","parentHash":"77ad695260deff06e5bd0e1aa6433372d9811a9f326554a9013c03523ea79881","tracingTargets":"state","storageKeys":"26aa394eea5630e07c48ae0c9558cef7b99d880ec681799c0cf30e8886371da9","methods":"Put,Kill,Append,ChildKill,ChildPut,ClearPrefix,ChildClearPrefix","spans":[],"events":[{"target":"state","data":{"stringValues":{"key":"26aa394eea5630e07c48ae0c9558cef7b99d880ec681799c0cf30e8886371da9e0a490d758ac5f00362b6b8f66730d9052f8cd6f528b1976a2dba9c840db7ad7166fbd51560e627973cbfe2637df691c","value":"Some(01000000000000000100000000000000b03fb04d290000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000)","value_encoded":"0101000000000000000100000000000000b03fb04d290000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","ext_id":"7052","method":"Put"}},"parentId":null},{"target":"state","data":{"stringValues":{"key":"26aa394eea5630e07c48ae0c9558cef7b99d880ec681799c0cf30e8886371da9e0a490d758ac5f00362b6b8f66730d9052f8cd6f528b1976a2dba9c840db7ad7166fbd51560e627973cbfe2637df691c","value":"Some(0100000000000000010000000000000096364a44290000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000)","value_encoded":"010100000000000000010000000000000096364a44290000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","ext_id":"7052","method":"Put"}},"parentId":null},{"target":"state","data":{"stringValues":{"key":"26aa394eea5630e07c48ae0c9558cef7b99d880ec681799c0cf30e8886371da9e0a490d758ac5f00362b6b8f66730d9052f8cd6f528b1976a2dba9c840db7ad7166fbd51560e627973cbfe2637df691c","value":"Some(0100000000000000010000000000000096545cb6040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000)","value_encoded":"010100000000000000010000000000000096545cb6040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","ext_id":"7052","method":"Put"}},"parentId":null},{"target":"state","data":{"stringValues":{"key":"26aa394eea5630e07c48ae0c9558cef7b99d880ec681799c0cf30e8886371da933db265af8c45c38bb83431934748076b48df30eeaa840a4469f64de38950bcb2abc8c839689fc6b6f7dc85e3ed274e1","value":"Some(01000000000000000100000000000000dfe7240c280000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000)","value_encoded":"0101000000000000000100000000000000dfe7240c280000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","ext_id":"7052","method":"Put"}},"parentId":null},{"target":"state","data":{"stringValues":{"key":"26aa394eea5630e07c48ae0c9558cef7b99d880ec681799c0cf30e8886371da95ecffd7b6c0f78751baa9d281e0bfa3a6d6f646c70792f74727372790000000000000000000000000000000000000000","value":"Some(00000000000000000100000000000000207bba410fb319060000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000)","value_encoded":"0100000000000000000100000000000000207bba410fb319060000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","ext_id":"7052","method":"Put"}},"parentId":null},{"target":"state","data":{"stringValues":{"key":"26aa394eea5630e07c48ae0c9558cef7b99d880ec681799c0cf30e8886371da921cd43758107884f7de2d987389969115606fcd617bb7328f828ebc07396ab27f038eb5f036125a2ab5b60ab9218f242","value":"Some(130000000300000001000000000000004a58c0b3c70000000000000000000000000000000000000000000000000000006f97f3902e00000000000000000000006f97f3902e0000000000000000000000)","value_encoded":"01130000000300000001000000000000004a58c0b3c70000000000000000000000000000000000000000000000000000006f97f3902e00000000000000000000006f97f3902e0000000000000000000000","ext_id":"7052","method":"Put"}},"parentId":null}]}},"id":1}

Local Environment: [You can get this information from executing subql version.] Query Version: [What is the version of the query service?] Indexer Version: [What is the version of the indexer service?] Network Details:

  • [Network]
  • [Block height, a block height where the issue is happening]
  • [Dictionary endpoint, if used]

Steps to Reproduce

  1. [First Step]
  2. [Second Step]
  3. [and so on...]

Example project: [A link to a minimal example that can reproduce the issue]

Expected behavior: [What you expected to happen]

Actual behavior: [What actually happened]

Any other information

Is there any other information you would like to add?

ianhe8x avatar Jul 18 '23 04:07 ianhe8x