paritytech / substrate

Substrate: The platform for blockchain innovators
Apache License 2.0
8.39k stars 2.65k forks source link

contracts: Expose environment types for offchain tooling #14750

Closed athei closed 1 year ago

athei commented 1 year ago

In order to implement https://github.com/paritytech/cargo-contract/issues/1167 we need to expose all types passed between pallet-contracts and the deployed contracts. The idea is to bundle all relevant types in an Environment struct mirroring the ink! Environment type. In order to expose it we add it to the config trait as Get<Environment>. The types are obviously only mentioned as PhantomData because we don't ever want to construct them. I did just this in this PR.

I think this would be an elegant solution if it wouldn't be for https://github.com/paritytech/scale-info/pull/111. @ascjones Can we maybe add an attribute to scale-info that allows to opt-in to PhantomData types?

cc @SkymanOne

cumulus companion: https://github.com/paritytech/cumulus/pull/3036

athei commented 1 year ago

Hmm yes that could work. This whole solution seems a bit hacky. Even without the wrapper type. But I guess our only way to get this in without introducing a whole new way of just expressing types into the metadata.

athei commented 1 year ago

Implemented your idea. Seems to be working. This is the output. I think we can work with that:

{
  "id": 576,
  "type": {
    "path": [
      "pallet_contracts",
      "Environment"
    ],
    "params": [
      {
        "name": "T",
        "type": null
      }
    ],
    "def": {
      "composite": {
        "fields": [
          {
            "name": "account_id",
            "type": 577,
            "typeName": "EnvironmentType<AccountIdOf<T>>"
          },
          {
            "name": "balance",
            "type": 578,
            "typeName": "EnvironmentType<BalanceOf<T>>"
          },
          {
            "name": "hash",
            "type": 579,
            "typeName": "EnvironmentType<<T as frame_system::Config>::Hash>"
          },
          {
            "name": "hasher",
            "type": 580,
            "typeName": "EnvironmentType<<T as frame_system::Config>::Hashing>"
          },
          {
            "name": "timestamp",
            "type": 582,
            "typeName": "EnvironmentType<MomentOf<T>>"
          },
          {
            "name": "block_number",
            "type": 583,
            "typeName": "EnvironmentType<BlockNumberFor<T>>"
          }
        ]
      }
    }
  }
},
{
  "id": 577,
  "type": {
    "path": [
      "pallet_contracts",
      "EnvironmentType"
    ],
    "params": [
      {
        "name": "T",
        "type": 0
      }
    ],
    "def": {
      "composite": {}
    }
  }
},
{
  "id": 578,
  "type": {
    "path": [
      "pallet_contracts",
      "EnvironmentType"
    ],
    "params": [
      {
        "name": "T",
        "type": 6
      }
    ],
    "def": {
      "composite": {}
    }
  }
},
{
  "id": 579,
  "type": {
    "path": [
      "pallet_contracts",
      "EnvironmentType"
    ],
    "params": [
      {
        "name": "T",
        "type": 12
      }
    ],
    "def": {
      "composite": {}
    }
  }
},
{
  "id": 580,
  "type": {
    "path": [
      "pallet_contracts",
      "EnvironmentType"
    ],
    "params": [
      {
        "name": "T",
        "type": 581
      }
    ],
    "def": {
      "composite": {}
    }
  }
},
ascjones commented 1 year ago

Hmm yes that could work. This whole solution seems a bit hacky. Even without the wrapper type. But I guess our only way to get this in without introducing a whole new way of just expressing types into the metadata.

A little bit hacky, perhaps. But a very neat way of getting the information we need into the metadata without adding any additional configuration schema. Of course the downstream tooling will need to do some explicit name lookups to pull out the types, but I think it might be worth it to keep things simple on the node side.

athei commented 1 year ago

bot rebase

paritytech-processbot[bot] commented 1 year ago

Rebased

athei commented 1 year ago

Please also approve the companion.

athei commented 1 year ago

bot rebase

paritytech-processbot[bot] commented 1 year ago

Rebased

athei commented 1 year ago

bot merge