From 01987ea0ab76e1d3a8ec5331e83452355d60ea97 Mon Sep 17 00:00:00 2001 From: yarikbratashchuk Date: Thu, 20 Jan 2022 16:44:00 +0200 Subject: [PATCH] Emit ContractCodeUpdated when setting new code_hash --- frame/contracts/src/exec.rs | 4 ++++ frame/contracts/src/lib.rs | 8 ++++++++ frame/contracts/src/tests.rs | 13 +++++++++++++ 3 files changed, 25 insertions(+) diff --git a/frame/contracts/src/exec.rs b/frame/contracts/src/exec.rs index 235ca63442b45..5733d86b42cac 100644 --- a/frame/contracts/src/exec.rs +++ b/frame/contracts/src/exec.rs @@ -1026,6 +1026,10 @@ where top_frame.contract_info().code_hash = hash; increment_refcount::(hash)?; decrement_refcount::(prev_hash)?; + Contracts::::deposit_event(Event::ContractCodeUpdated { + contract: top_frame.account_id.clone(), + code_hash: hash, + }); Ok(()) } diff --git a/frame/contracts/src/lib.rs b/frame/contracts/src/lib.rs index e4a54362c90bc..77c3227179667 100644 --- a/frame/contracts/src/lib.rs +++ b/frame/contracts/src/lib.rs @@ -559,6 +559,14 @@ pub mod pallet { /// A code with the specified hash was removed. CodeRemoved { code_hash: T::Hash }, + + /// Contract code has been updated to one specified with code_hash + ContractCodeUpdated { + /// The contract that has been updated. + contract: T::AccountId, + /// New code hash that was set for the contract + code_hash: T::Hash, + }, } #[pallet::error] diff --git a/frame/contracts/src/tests.rs b/frame/contracts/src/tests.rs index 0c844fb55828e..c3ced4885b6b5 100644 --- a/frame/contracts/src/tests.rs +++ b/frame/contracts/src/tests.rs @@ -735,6 +735,19 @@ fn set_code_hash() { .result .unwrap(); assert_return_code!(result, 2); + + // Checking for the last event only + assert_eq!( + System::events()[13], + EventRecord { + phase: Phase::Initialization, + event: Event::Contracts(crate::Event::ContractCodeUpdated { + contract: contract_addr.clone(), + code_hash: new_code_hash.clone(), + }), + topics: vec![], + }, + ); }); }