From 8ef20a55343c179d832e1e5da264bce7c9939ae2 Mon Sep 17 00:00:00 2001
From: Fabio Tudone <circlespainter@users.noreply.github.com>
Date: Wed, 16 Oct 2019 11:02:12 +0300
Subject: [PATCH] Fix #1536: do not require to construct a block for encoding
 it (#3813)

* Fix #1536: do not require to construct a block for encoding it

* Bump `impl_version`

* Improve `Block::encode_from` signature and rustdoc (from review by @bkchr)
---
 substrate/core/client/src/client.rs               | 8 ++++----
 substrate/core/sr-primitives/src/generic/block.rs | 3 +++
 substrate/core/sr-primitives/src/testing.rs       | 3 +++
 substrate/core/sr-primitives/src/traits.rs        | 2 ++
 substrate/node/runtime/src/lib.rs                 | 2 +-
 5 files changed, 13 insertions(+), 5 deletions(-)

diff --git a/substrate/core/client/src/client.rs b/substrate/core/client/src/client.rs
index 34334b1388e..796520cb7f0 100644
--- a/substrate/core/client/src/client.rs
+++ b/substrate/core/client/src/client.rs
@@ -1059,10 +1059,10 @@ impl<B, E, Block, RA> Client<B, E, Block, RA> where
 					}
 				};
 
-				let encoded_block = <Block as BlockT>::new(
-					import_headers.pre().clone(),
-					body.unwrap_or_default(),
-				).encode();
+				let encoded_block = <Block as BlockT>::encode_from(
+					import_headers.pre(),
+					&body.unwrap_or_default()
+				);
 
 				let (_, storage_update, changes_update) = self.executor
 					.call_at_state::<_, _, NeverNativeValue, fn() -> _>(
diff --git a/substrate/core/sr-primitives/src/generic/block.rs b/substrate/core/sr-primitives/src/generic/block.rs
index 736ad0cbbb6..29fcaab5729 100644
--- a/substrate/core/sr-primitives/src/generic/block.rs
+++ b/substrate/core/sr-primitives/src/generic/block.rs
@@ -93,6 +93,9 @@ where
 	fn new(header: Self::Header, extrinsics: Vec<Self::Extrinsic>) -> Self {
 		Block { header, extrinsics }
 	}
+	fn encode_from(header: &Self::Header, extrinsics: &[Self::Extrinsic]) -> Vec<u8> {
+		(header, extrinsics).encode()
+	}
 }
 
 /// Abstraction over a substrate block and justification.
diff --git a/substrate/core/sr-primitives/src/testing.rs b/substrate/core/sr-primitives/src/testing.rs
index 5391735576a..b7903471188 100644
--- a/substrate/core/sr-primitives/src/testing.rs
+++ b/substrate/core/sr-primitives/src/testing.rs
@@ -259,6 +259,9 @@ impl<Xt: 'static + Codec + Sized + Send + Sync + Serialize + Clone + Eq + Debug
 	fn new(header: Self::Header, extrinsics: Vec<Self::Extrinsic>) -> Self {
 		Block { header, extrinsics }
 	}
+	fn encode_from(header: &Self::Header, extrinsics: &[Self::Extrinsic]) -> Vec<u8> {
+		(header, extrinsics).encode()
+	}
 }
 
 impl<'a, Xt> Deserialize<'a> for Block<Xt> where Block<Xt>: Decode {
diff --git a/substrate/core/sr-primitives/src/traits.rs b/substrate/core/sr-primitives/src/traits.rs
index f94a71d38a6..a589f5f389d 100644
--- a/substrate/core/sr-primitives/src/traits.rs
+++ b/substrate/core/sr-primitives/src/traits.rs
@@ -687,6 +687,8 @@ pub trait Block: Clone + Send + Sync + Codec + Eq + MaybeSerializeDebugButNotDes
 	fn hash(&self) -> Self::Hash {
 		<<Self::Header as Header>::Hashing as Hash>::hash_of(self.header())
 	}
+	/// Create an encoded block from the given `header` and `extrinsics` without requiring to create an instance.
+	fn encode_from(header: &Self::Header, extrinsics: &[Self::Extrinsic]) -> Vec<u8>;
 }
 
 /// Something that acts like an `Extrinsic`.
diff --git a/substrate/node/runtime/src/lib.rs b/substrate/node/runtime/src/lib.rs
index 70b1b8f16a7..d8fc9a1a578 100644
--- a/substrate/node/runtime/src/lib.rs
+++ b/substrate/node/runtime/src/lib.rs
@@ -85,7 +85,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
 	// implementation changes and behavior does not, then leave spec_version as
 	// is and increment impl_version.
 	spec_version: 175,
-	impl_version: 175,
+	impl_version: 176,
 	apis: RUNTIME_API_VERSIONS,
 };
 
-- 
GitLab