From f2645eec12f080a66228aedbeaecea2c913570ed Mon Sep 17 00:00:00 2001
From: PG Herveou <pgherveou@gmail.com>
Date: Fri, 23 Feb 2024 14:10:06 +0100
Subject: [PATCH] Contracts Add new version for marking new stable API (#3415)

Add a `ApiVersion` constant to the pallet-contracts Config to
communicate with developers the current state of the host functions
exposed by the pallet
---
 .../contracts-rococo/src/contracts.rs          |  1 +
 prdoc/pr_3415.prdoc                            |  9 +++++++++
 substrate/bin/node/runtime/src/lib.rs          |  1 +
 .../src/parachain/contracts_config.rs          |  1 +
 substrate/frame/contracts/src/lib.rs           | 18 ++++++++++++++++++
 substrate/frame/contracts/src/tests.rs         |  1 +
 6 files changed, 31 insertions(+)
 create mode 100644 prdoc/pr_3415.prdoc

diff --git a/cumulus/parachains/runtimes/contracts/contracts-rococo/src/contracts.rs b/cumulus/parachains/runtimes/contracts/contracts-rococo/src/contracts.rs
index 7b89f2df807..681b95ce6a5 100644
--- a/cumulus/parachains/runtimes/contracts/contracts-rococo/src/contracts.rs
+++ b/cumulus/parachains/runtimes/contracts/contracts-rococo/src/contracts.rs
@@ -72,5 +72,6 @@ impl Config for Runtime {
 	type RuntimeHoldReason = RuntimeHoldReason;
 	type Debug = ();
 	type Environment = ();
+	type ApiVersion = ();
 	type Xcm = pallet_xcm::Pallet<Self>;
 }
diff --git a/prdoc/pr_3415.prdoc b/prdoc/pr_3415.prdoc
new file mode 100644
index 00000000000..c56a5d3ffaa
--- /dev/null
+++ b/prdoc/pr_3415.prdoc
@@ -0,0 +1,9 @@
+title: "[pallet-contracts] Add APIVersion to the config."
+
+doc:
+  - audience: Runtime Dev
+    description: |
+      Add `APIVersion` to the config to communicate the state of the Host functions exposed by the pallet.
+
+crates:
+  - name: pallet-contracts
diff --git a/substrate/bin/node/runtime/src/lib.rs b/substrate/bin/node/runtime/src/lib.rs
index 24c8f6f48e9..b34d8c89e56 100644
--- a/substrate/bin/node/runtime/src/lib.rs
+++ b/substrate/bin/node/runtime/src/lib.rs
@@ -1367,6 +1367,7 @@ impl pallet_contracts::Config for Runtime {
 	type CodeHashLockupDepositPercent = CodeHashLockupDepositPercent;
 	type Debug = ();
 	type Environment = ();
+	type ApiVersion = ();
 	type Xcm = ();
 }
 
diff --git a/substrate/frame/contracts/mock-network/src/parachain/contracts_config.rs b/substrate/frame/contracts/mock-network/src/parachain/contracts_config.rs
index dadba394e26..3f26c6f372e 100644
--- a/substrate/frame/contracts/mock-network/src/parachain/contracts_config.rs
+++ b/substrate/frame/contracts/mock-network/src/parachain/contracts_config.rs
@@ -94,5 +94,6 @@ impl pallet_contracts::Config for Runtime {
 	type WeightPrice = Self;
 	type Debug = ();
 	type Environment = ();
+	type ApiVersion = ();
 	type Xcm = pallet_xcm::Pallet<Self>;
 }
diff --git a/substrate/frame/contracts/src/lib.rs b/substrate/frame/contracts/src/lib.rs
index 943bf5f85d4..d8939ee88ba 100644
--- a/substrate/frame/contracts/src/lib.rs
+++ b/substrate/frame/contracts/src/lib.rs
@@ -214,6 +214,18 @@ pub struct Environment<T: Config> {
 	block_number: EnvironmentType<BlockNumberFor<T>>,
 }
 
+/// Defines the current version of the HostFn APIs.
+/// This is used to communicate the available APIs in pallet-contracts.
+///
+/// The version is bumped any time a new HostFn is added or stabilized.
+#[derive(Encode, Decode, TypeInfo)]
+pub struct ApiVersion(u16);
+impl Default for ApiVersion {
+	fn default() -> Self {
+		Self(1)
+	}
+}
+
 #[frame_support::pallet]
 pub mod pallet {
 	use super::*;
@@ -402,6 +414,12 @@ pub mod pallet {
 		#[pallet::constant]
 		type Environment: Get<Environment<Self>>;
 
+		/// The version of the HostFn APIs that are available in the runtime.
+		///
+		/// Only valid value is `()`.
+		#[pallet::constant]
+		type ApiVersion: Get<ApiVersion>;
+
 		/// A type that exposes XCM APIs, allowing contracts to interact with other parachains, and
 		/// execute XCM programs.
 		type Xcm: xcm_builder::Controller<
diff --git a/substrate/frame/contracts/src/tests.rs b/substrate/frame/contracts/src/tests.rs
index b39fc79b62a..2d1c5b315a3 100644
--- a/substrate/frame/contracts/src/tests.rs
+++ b/substrate/frame/contracts/src/tests.rs
@@ -465,6 +465,7 @@ impl Config for Test {
 	type MaxDelegateDependencies = MaxDelegateDependencies;
 	type Debug = TestDebug;
 	type Environment = ();
+	type ApiVersion = ();
 	type Xcm = ();
 }
 
-- 
GitLab