From 200a716a1abd68c6773687dcba826b175f450af9 Mon Sep 17 00:00:00 2001
From: Svyatoslav Nikolsky <svyatonik@gmail.com>
Date: Fri, 5 Oct 2018 15:17:00 +0300
Subject: [PATCH] Extract search of digest item into Digest::log() function
 (#874)

* Digest::log fn

* change DigestItem::log impl
---
 substrate/core/client/db/src/lib.rs        | 4 +---
 substrate/core/client/src/client.rs        | 4 +---
 substrate/core/client/src/in_mem.rs        | 5 +----
 substrate/core/sr-primitives/src/traits.rs | 7 +++++++
 4 files changed, 10 insertions(+), 10 deletions(-)

diff --git a/substrate/core/client/db/src/lib.rs b/substrate/core/client/db/src/lib.rs
index eb63f60e3ae..2fd965fd6d3 100644
--- a/substrate/core/client/db/src/lib.rs
+++ b/substrate/core/client/db/src/lib.rs
@@ -374,9 +374,7 @@ impl<Block: BlockT> state_machine::ChangesTrieRootsStorage<Blake2Hasher> for DbC
 					.map(Some),
 				None => Ok(None)
 			})?
-			.and_then(|header| header.digest().logs().iter()
-				.find(|log| log.as_changes_trie_root().is_some())
-				.and_then(DigestItem::as_changes_trie_root)
+			.and_then(|header| header.digest().log(DigestItem::as_changes_trie_root)
 				.map(|root| H256::from_slice(root.as_ref()))))
 	}
 }
diff --git a/substrate/core/client/src/client.rs b/substrate/core/client/src/client.rs
index 0e2ca942590..989cfcadc82 100644
--- a/substrate/core/client/src/client.rs
+++ b/substrate/core/client/src/client.rs
@@ -1091,9 +1091,7 @@ pub(crate) mod tests {
 			remote_client.justify_and_import(BlockOrigin::Own, builder.bake().unwrap()).unwrap();
 
 			let header = remote_client.header(&BlockId::Number(i as u64 + 1)).unwrap().unwrap();
-			let trie_root = header.digest().logs().iter()
-				.find(|l| l.as_changes_trie_root().is_some())
-				.and_then(DigestItem::as_changes_trie_root)
+			let trie_root = header.digest().log(DigestItem::as_changes_trie_root)
 				.map(|root| H256::from_slice(root.as_ref()))
 				.unwrap();
 			local_roots.push(trie_root);
diff --git a/substrate/core/client/src/in_mem.rs b/substrate/core/client/src/in_mem.rs
index c938a6dbd35..29bf8afdba0 100644
--- a/substrate/core/client/src/in_mem.rs
+++ b/substrate/core/client/src/in_mem.rs
@@ -474,10 +474,7 @@ where
 
 			self.states.write().insert(hash, operation.new_state.unwrap_or_else(|| old_state.clone()));
 
-			let changes_trie_root = header.digest().logs().iter()
-				.find(|log| log.as_changes_trie_root().is_some())
-				.and_then(DigestItem::as_changes_trie_root)
-				.cloned();
+			let changes_trie_root = header.digest().log(DigestItem::as_changes_trie_root).cloned();
 			if let Some(changes_trie_root) = changes_trie_root {
 				if let Some(changes_trie_update) = operation.changes_trie_update {
 					let changes_trie_root: H::Out = changes_trie_root.into();
diff --git a/substrate/core/sr-primitives/src/traits.rs b/substrate/core/sr-primitives/src/traits.rs
index 75a543850ff..50775921052 100644
--- a/substrate/core/sr-primitives/src/traits.rs
+++ b/substrate/core/sr-primitives/src/traits.rs
@@ -503,6 +503,13 @@ pub trait Digest: Member + Default {
 	fn logs(&self) -> &[Self::Item];
 	/// Push new digest item.
 	fn push(&mut self, item: Self::Item);
+
+	/// Get reference to the first digest item that matches the passed predicate.
+	fn log<T, F: Fn(&Self::Item) -> Option<&T>>(&self, predicate: F) -> Option<&T> {
+		self.logs().iter()
+			.filter_map(predicate)
+			.next()
+	}
 }
 
 /// Single digest item. Could be any type that implements `Member` and provides methods
-- 
GitLab