I am aware of this example that shows how to create a mock chain extension and test that. However, I do not know how to test the chain extension that is used by the runtime and calling functions from pallets. How do I ensure the chain extension is interacting correctly with pallets?

  • Do I understand you correctly that you're asking how to write unit tests for the chain extension? So unit tests that sit in the Substrate runtime?
    – cmichi
    Mar 30, 2022 at 8:22
  • Yes, I am asking how it is recommended to test that the chain extension is implemented correctly. Mar 30, 2022 at 14:21

2 Answers 2


I'm not sure if this is the simplest way, but I came up with a solution.

  1. Write a basic smart contract that calls each chain extension function
  2. Create a mock runtime using the chain extension
  3. Write a test that builds the contract, uploads it with pallet_contracts::bare_upload_code and pallet_contracts::bare_instantiate. Then call the contract's functions and verify the state of the appropriate pallets.
  • 1
    Sounds good. Do you have example of all this steps?
    – IronMan
    Apr 20, 2022 at 15:49

There doesn't seem to be a clear way to test that e.g. a given call function in a chain extension is set up to behave the way that is expected(because it's difficult to instantiate the Environment struct externally).

Instead, you might break some parts out of call and test them individually. For example, the matching on the func_id is a large portion of the chain extension's work https://github.com/paritytech/ink/blob/6f105c2a0288b9a160385e1795375e5ad0350e34/examples/rand-extension/runtime/chain-extension-example.rs#L29-L48. You might split this out into a separate function that takes the function id and returns the expected call, if you can figure out a clean way to separate it from the env.write(). Then, you can test this new function, for example.

You can also look at the existing ink! tests in the chain extension example that test chain extension setup more implicitly. In this case they are checking the state after using a chain extension that calls an extrinsic. https://github.com/paritytech/ink/blob/6f105c2a0288b9a160385e1795375e5ad0350e34/examples/rand-extension/lib.rs#L124-L155

  • Writing the internals of the chain extension in a separate function could be useful. You could probably have reasonable confidence that it is working with that approach, but you would not be 100% sure. For the second part, that is a mock chain extension, not the actual chain extension. That is just testing the smart contract itself, not the chain extension. Apr 4, 2022 at 16:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.