Skip to content

Commit

Permalink
Emit ContractCodeUpdated when setting new code_hash
Browse files Browse the repository at this point in the history
  • Loading branch information
yarikbratashchuk committed Jan 20, 2022
1 parent a5a3ec8 commit 01987ea
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
4 changes: 4 additions & 0 deletions frame/contracts/src/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1026,6 +1026,10 @@ where
top_frame.contract_info().code_hash = hash;
increment_refcount::<Self::T>(hash)?;
decrement_refcount::<Self::T>(prev_hash)?;
Contracts::<Self::T>::deposit_event(Event::ContractCodeUpdated {
contract: top_frame.account_id.clone(),
code_hash: hash,
});
Ok(())
}

Expand Down
8 changes: 8 additions & 0 deletions frame/contracts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
13 changes: 13 additions & 0 deletions frame/contracts/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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![],
},
);
});
}

Expand Down

0 comments on commit 01987ea

Please sign in to comment.