From 753907a184078b1bba50615204f7d2db774e1230 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bastian=20K=C3=B6cher?= <bkchr@users.noreply.github.com>
Date: Thu, 4 Aug 2022 11:34:19 +0200
Subject: [PATCH] Timestamp: `set_timestamp` sets `DidUpdate` (#11960)

* Timestamp: `set_timestamp` sets `DidUpdate`

There exists the `set_timestamp` in the Timestamp pallet for setting the current timestamp. The
problem is that it doesn't set `DidUpdate`. This results in `on_finalize` panicking. There is no
real reason why the function doesn't also set `DidUpdate`.

* Update frame/timestamp/src/lib.rs

Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Fix Babe tests

Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
---
 substrate/frame/babe/src/mock.rs       | 2 +-
 substrate/frame/babe/src/tests.rs      | 2 +-
 substrate/frame/timestamp/src/lib.rs   | 2 ++
 substrate/frame/timestamp/src/tests.rs | 5 ++---
 4 files changed, 6 insertions(+), 5 deletions(-)

diff --git a/substrate/frame/babe/src/mock.rs b/substrate/frame/babe/src/mock.rs
index 5677eb7e28e..c2ba3c2be06 100644
--- a/substrate/frame/babe/src/mock.rs
+++ b/substrate/frame/babe/src/mock.rs
@@ -429,7 +429,7 @@ pub fn generate_equivocation_proof(
 		System::reset_events();
 		System::initialize(&current_block, &parent_hash, &pre_digest);
 		System::set_block_number(current_block);
-		Timestamp::set_timestamp(current_block);
+		Timestamp::set_timestamp(*current_slot * Babe::slot_duration());
 		System::finalize()
 	};
 
diff --git a/substrate/frame/babe/src/tests.rs b/substrate/frame/babe/src/tests.rs
index 0859bb7a408..ece08833877 100644
--- a/substrate/frame/babe/src/tests.rs
+++ b/substrate/frame/babe/src/tests.rs
@@ -659,7 +659,7 @@ fn report_equivocation_invalid_equivocation_proof() {
 		equivocation_proof.second_header = equivocation_proof.first_header.clone();
 		assert_invalid_equivocation(equivocation_proof);
 
-		// missing preruntime digest from one header
+		// missing pre-runtime digest from one header
 		let mut equivocation_proof = generate_equivocation_proof(
 			offending_validator_index as u32,
 			&offending_authority_pair,
diff --git a/substrate/frame/timestamp/src/lib.rs b/substrate/frame/timestamp/src/lib.rs
index 81ed67913c2..6a7f849d132 100644
--- a/substrate/frame/timestamp/src/lib.rs
+++ b/substrate/frame/timestamp/src/lib.rs
@@ -282,6 +282,8 @@ impl<T: Config> Pallet<T> {
 	#[cfg(any(feature = "runtime-benchmarks", feature = "std"))]
 	pub fn set_timestamp(now: T::Moment) {
 		Now::<T>::put(now);
+		DidUpdate::<T>::put(true);
+		<T::OnTimestampSet as OnTimestampSet<_>>::on_timestamp_set(now);
 	}
 }
 
diff --git a/substrate/frame/timestamp/src/tests.rs b/substrate/frame/timestamp/src/tests.rs
index f52ba7849c9..ef9fd6e39d4 100644
--- a/substrate/frame/timestamp/src/tests.rs
+++ b/substrate/frame/timestamp/src/tests.rs
@@ -23,7 +23,7 @@ use frame_support::assert_ok;
 #[test]
 fn timestamp_works() {
 	new_test_ext().execute_with(|| {
-		Timestamp::set_timestamp(42);
+		crate::Now::<Test>::put(46);
 		assert_ok!(Timestamp::set(Origin::none(), 69));
 		assert_eq!(Timestamp::now(), 69);
 		assert_eq!(Some(69), get_captured_moment());
@@ -36,7 +36,6 @@ fn double_timestamp_should_fail() {
 	new_test_ext().execute_with(|| {
 		Timestamp::set_timestamp(42);
 		assert_ok!(Timestamp::set(Origin::none(), 69));
-		let _ = Timestamp::set(Origin::none(), 70);
 	});
 }
 
@@ -46,7 +45,7 @@ fn double_timestamp_should_fail() {
 )]
 fn block_period_minimum_enforced() {
 	new_test_ext().execute_with(|| {
-		Timestamp::set_timestamp(42);
+		crate::Now::<Test>::put(44);
 		let _ = Timestamp::set(Origin::none(), 46);
 	});
 }
-- 
GitLab