Emit events for inter contract calls (#12136)

* Add topics to contract events

* Add `Call` events

* Fix compilation for no_std

* Added docs
This commit is contained in:
Alexander Theißen
2022-09-03 10:03:36 +02:00
committed by GitHub
parent 5fb97da337
commit 09a52ef882
4 changed files with 265 additions and 83 deletions
+122 -25
View File
@@ -15,6 +15,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
use self::test_utils::hash;
use crate::{
chain_extension::{
ChainExtension, Environment, Ext, InitState, RegisteredChainExtension,
@@ -74,8 +75,9 @@ frame_support::construct_runtime!(
#[macro_use]
pub mod test_utils {
use super::{Balances, Test};
use super::{Balances, Hash, SysConfig, Test};
use crate::{exec::AccountIdOf, storage::Storage, CodeHash, Config, ContractInfoOf, Nonce};
use codec::Encode;
use frame_support::traits::Currency;
pub fn place_contract(address: &AccountIdOf<Test>, code_hash: CodeHash<Test>) {
@@ -95,6 +97,9 @@ pub mod test_utils {
pub fn get_balance(who: &AccountIdOf<Test>) -> u64 {
Balances::free_balance(who)
}
pub fn hash<S: Encode>(s: &S) -> <<Test as SysConfig>::Hashing as Hash>::Output {
<<Test as SysConfig>::Hashing as Hash>::hash_of(s)
}
macro_rules! assert_return_code {
( $x:expr , $y:expr $(,)? ) => {{
assert_eq!(u32::from_le_bytes($x.data[..].try_into().unwrap()), $y as u32);
@@ -571,7 +576,7 @@ fn instantiate_and_call_and_deposit_event() {
deployer: ALICE,
contract: addr.clone()
}),
topics: vec![],
topics: vec![hash(&ALICE), hash(&addr)],
},
]
);
@@ -857,7 +862,7 @@ fn deploy_and_call_other_contract() {
deployer: caller_addr.clone(),
contract: callee_addr.clone(),
}),
topics: vec![],
topics: vec![hash(&caller_addr), hash(&callee_addr)],
},
EventRecord {
phase: Phase::Initialization,
@@ -868,6 +873,22 @@ fn deploy_and_call_other_contract() {
}),
topics: vec![],
},
EventRecord {
phase: Phase::Initialization,
event: Event::Contracts(crate::Event::Called {
caller: caller_addr.clone(),
contract: callee_addr.clone(),
}),
topics: vec![hash(&caller_addr), hash(&callee_addr)],
},
EventRecord {
phase: Phase::Initialization,
event: Event::Contracts(crate::Event::Called {
caller: ALICE,
contract: caller_addr.clone(),
}),
topics: vec![hash(&ALICE), hash(&caller_addr)],
},
]
);
});
@@ -1067,6 +1088,14 @@ fn cannot_self_destruct_by_refund_after_slash() {
}),
topics: vec![],
},
EventRecord {
phase: Phase::Initialization,
event: Event::Contracts(crate::Event::Called {
caller: ALICE,
contract: addr.clone(),
}),
topics: vec![hash(&ALICE), hash(&addr)],
},
EventRecord {
phase: Phase::Initialization,
event: Event::Balances(pallet_balances::Event::ReserveRepatriated {
@@ -1174,7 +1203,15 @@ fn self_destruct_works() {
contract: addr.clone(),
beneficiary: DJANGO
}),
topics: vec![],
topics: vec![hash(&addr), hash(&DJANGO)],
},
EventRecord {
phase: Phase::Initialization,
event: Event::Contracts(crate::Event::Called {
caller: ALICE,
contract: addr.clone(),
}),
topics: vec![hash(&ALICE), hash(&addr)],
},
EventRecord {
phase: Phase::Initialization,
@@ -2621,7 +2658,7 @@ fn upload_code_works() {
EventRecord {
phase: Phase::Initialization,
event: Event::Contracts(crate::Event::CodeStored { code_hash }),
topics: vec![],
topics: vec![code_hash],
},
]
);
@@ -2700,7 +2737,7 @@ fn remove_code_works() {
EventRecord {
phase: Phase::Initialization,
event: Event::Contracts(crate::Event::CodeStored { code_hash }),
topics: vec![],
topics: vec![code_hash],
},
EventRecord {
phase: Phase::Initialization,
@@ -2713,7 +2750,7 @@ fn remove_code_works() {
EventRecord {
phase: Phase::Initialization,
event: Event::Contracts(crate::Event::CodeRemoved { code_hash }),
topics: vec![],
topics: vec![code_hash],
},
]
);
@@ -2755,7 +2792,7 @@ fn remove_code_wrong_origin() {
EventRecord {
phase: Phase::Initialization,
event: Event::Contracts(crate::Event::CodeStored { code_hash }),
topics: vec![],
topics: vec![code_hash],
},
]
);
@@ -2886,7 +2923,7 @@ fn instantiate_with_zero_balance_works() {
EventRecord {
phase: Phase::Initialization,
event: Event::Contracts(crate::Event::CodeStored { code_hash }),
topics: vec![],
topics: vec![code_hash],
},
EventRecord {
phase: Phase::Initialization,
@@ -2894,7 +2931,7 @@ fn instantiate_with_zero_balance_works() {
deployer: ALICE,
contract: addr.clone(),
}),
topics: vec![],
topics: vec![hash(&ALICE), hash(&addr)],
},
]
);
@@ -2986,7 +3023,7 @@ fn instantiate_with_below_existential_deposit_works() {
EventRecord {
phase: Phase::Initialization,
event: Event::Contracts(crate::Event::CodeStored { code_hash }),
topics: vec![],
topics: vec![code_hash],
},
EventRecord {
phase: Phase::Initialization,
@@ -2994,7 +3031,7 @@ fn instantiate_with_below_existential_deposit_works() {
deployer: ALICE,
contract: addr.clone(),
}),
topics: vec![],
topics: vec![hash(&ALICE), hash(&addr)],
},
]
);
@@ -3074,6 +3111,14 @@ fn storage_deposit_works() {
}),
topics: vec![],
},
EventRecord {
phase: Phase::Initialization,
event: Event::Contracts(crate::Event::Called {
caller: ALICE,
contract: addr.clone(),
}),
topics: vec![hash(&ALICE), hash(&addr)],
},
EventRecord {
phase: Phase::Initialization,
event: Event::Balances(pallet_balances::Event::Transfer {
@@ -3091,6 +3136,14 @@ fn storage_deposit_works() {
}),
topics: vec![],
},
EventRecord {
phase: Phase::Initialization,
event: Event::Contracts(crate::Event::Called {
caller: ALICE,
contract: addr.clone(),
}),
topics: vec![hash(&ALICE), hash(&addr)],
},
EventRecord {
phase: Phase::Initialization,
event: Event::Balances(pallet_balances::Event::Transfer {
@@ -3108,6 +3161,14 @@ fn storage_deposit_works() {
}),
topics: vec![],
},
EventRecord {
phase: Phase::Initialization,
event: Event::Contracts(crate::Event::Called {
caller: ALICE,
contract: addr.clone(),
}),
topics: vec![hash(&ALICE), hash(&addr)],
},
EventRecord {
phase: Phase::Initialization,
event: Event::Balances(pallet_balances::Event::ReserveRepatriated {
@@ -3193,11 +3254,11 @@ fn set_code_extrinsic() {
vec![EventRecord {
phase: Phase::Initialization,
event: Event::Contracts(pallet_contracts::Event::ContractCodeUpdated {
contract: addr,
contract: addr.clone(),
new_code_hash,
old_code_hash: code_hash,
}),
topics: vec![],
topics: vec![hash(&addr), new_code_hash, code_hash],
},]
);
});
@@ -3226,7 +3287,7 @@ fn call_after_killed_account_needs_funding() {
// Destroy the account of the contract by slashing.
// Slashing can actually happen if the contract takes part in staking.
// It is a corner case and we except the destruction of the account.
// It is a corner case and we accept the destruction of the account.
let _ = <Test as Config>::Currency::slash(
&addr,
<Test as Config>::Currency::total_balance(&addr),
@@ -3284,6 +3345,14 @@ fn call_after_killed_account_needs_funding() {
}),
topics: vec![],
},
EventRecord {
phase: Phase::Initialization,
event: Event::Contracts(crate::Event::Called {
caller: ALICE,
contract: addr.clone(),
}),
topics: vec![hash(&ALICE), hash(&addr)],
},
EventRecord {
phase: Phase::Initialization,
event: Event::System(frame_system::Event::NewAccount { account: addr.clone() }),
@@ -3306,6 +3375,14 @@ fn call_after_killed_account_needs_funding() {
}),
topics: vec![],
},
EventRecord {
phase: Phase::Initialization,
event: Event::Contracts(crate::Event::Called {
caller: ALICE,
contract: addr.clone(),
}),
topics: vec![hash(&ALICE), hash(&addr)],
},
]
);
});
@@ -3454,6 +3531,8 @@ fn set_code_hash() {
// upload new code
assert_ok!(Contracts::upload_code(Origin::signed(ALICE), new_wasm.clone(), None));
System::reset_events();
// First call sets new code_hash and returns 1
let result = Contracts::bare_call(
ALICE,
@@ -3477,16 +3556,34 @@ fn set_code_hash() {
// Checking for the last event only
assert_eq!(
System::events().pop().unwrap(),
EventRecord {
phase: Phase::Initialization,
event: Event::Contracts(crate::Event::ContractCodeUpdated {
contract: contract_addr.clone(),
new_code_hash,
old_code_hash: code_hash,
}),
topics: vec![],
},
&System::events(),
&[
EventRecord {
phase: Phase::Initialization,
event: Event::Contracts(crate::Event::ContractCodeUpdated {
contract: contract_addr.clone(),
new_code_hash,
old_code_hash: code_hash,
}),
topics: vec![hash(&contract_addr), new_code_hash, code_hash],
},
EventRecord {
phase: Phase::Initialization,
event: Event::Contracts(crate::Event::Called {
caller: ALICE,
contract: contract_addr.clone(),
}),
topics: vec![hash(&ALICE), hash(&contract_addr)],
},
EventRecord {
phase: Phase::Initialization,
event: Event::Contracts(crate::Event::Called {
caller: ALICE,
contract: contract_addr.clone(),
}),
topics: vec![hash(&ALICE), hash(&contract_addr)],
},
],
);
});
}