Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit a534758

Browse files
committed
contracts: add encoded events to ContractResult
1 parent 47200ff commit a534758

File tree

3 files changed

+14
-11
lines changed

3 files changed

+14
-11
lines changed

frame/contracts/primitives/src/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use sp_weights::Weight;
3333
///
3434
/// It contains the execution result together with some auxiliary information.
3535
#[derive(Eq, PartialEq, Encode, Decode, RuntimeDebug, TypeInfo)]
36-
pub struct ContractResult<R, Balance, Event> {
36+
pub struct ContractResult<R, Balance> {
3737
/// How much weight was consumed during execution.
3838
pub gas_consumed: Weight,
3939
/// How much weight is required as gas limit in order to execute this call.
@@ -71,16 +71,16 @@ pub struct ContractResult<R, Balance, Event> {
7171
pub debug_message: Vec<u8>,
7272
/// The execution result of the wasm code.
7373
pub result: R,
74-
pub events: Option<Vec<Event>>,
74+
pub events: Option<Vec<Vec<u8>>>,
7575
}
7676

7777
/// Result type of a `bare_call` call.
78-
pub type ContractExecResult<Balance, Event> =
79-
ContractResult<Result<ExecReturnValue, DispatchError>, Balance, Event>;
78+
pub type ContractExecResult<Balance> =
79+
ContractResult<Result<ExecReturnValue, DispatchError>, Balance>;
8080

8181
/// Result type of a `bare_instantiate` call.
8282
pub type ContractInstantiateResult<AccountId, Balance> =
83-
ContractResult<Result<InstantiateReturnValue<AccountId>, DispatchError>, Balance, ()>;
83+
ContractResult<Result<InstantiateReturnValue<AccountId>, DispatchError>, Balance>;
8484

8585
/// Result type of a `bare_code_upload` call.
8686
pub type CodeUploadResult<CodeHash, Balance> =

frame/contracts/src/lib.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ use frame_support::{
118118
weights::{OldWeight, Weight},
119119
BoundedVec, WeakBoundedVec,
120120
};
121-
use frame_system::{EventRecord, Pallet as System};
121+
use frame_system::Pallet as System;
122122
use pallet_contracts_primitives::{
123123
Code, CodeUploadResult, CodeUploadReturnValue, ContractAccessError, ContractExecResult,
124124
ContractInstantiateResult, ExecReturnValue, GetStorageResult, InstantiateReturnValue,
@@ -1167,7 +1167,7 @@ impl<T: Config> Pallet<T> {
11671167
data: Vec<u8>,
11681168
debug: bool,
11691169
determinism: Determinism,
1170-
) -> ContractExecResult<BalanceOf<T>, EventRecord<<T as pallet::Config>::RuntimeEvent, T>> {
1170+
) -> ContractExecResult<BalanceOf<T>> {
11711171
let mut debug_message = if debug { Some(DebugBufferVec::<T>::default()) } else { None };
11721172
let common = CommonInput {
11731173
origin,
@@ -1178,15 +1178,18 @@ impl<T: Config> Pallet<T> {
11781178
debug_message: debug_message.as_mut(),
11791179
};
11801180
let output = CallInput::<T> { dest, determinism }.run_guarded(common);
1181+
// We are good to call System::events() from the runtime API (i.e offchain).
1182+
// Even though it says it should only be used in tests, it is actually not allowed to be
1183+
// read on-chain cause it will put all the Events emitted in the block so far into the PoV.
1184+
let events: Vec<Vec<u8>> =
1185+
System::<T>::events().iter().map(|e| e.clone().event.encode()).collect(); // todo: should determinism::Relaxed be checked here?
11811186
ContractExecResult {
11821187
result: output.result.map_err(|r| r.error),
11831188
gas_consumed: output.gas_meter.gas_consumed(),
11841189
gas_required: output.gas_meter.gas_required(),
11851190
storage_deposit: output.storage_deposit,
11861191
debug_message: debug_message.unwrap_or_default().to_vec(),
1187-
events: Some(System::events()), /* TODO: is it okay to call System::events() here?
1188-
* The function says it should only be used in tests.
1189-
* But what about offchain calls? */
1192+
events: Some(events),
11901193
}
11911194
}
11921195

frame/contracts/src/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ fn compile_module<T>(fixture_name: &str) -> wat::Result<(Vec<u8>, <T::Hashing as
458458
where
459459
T: frame_system::Config,
460460
{
461-
let fixture_path = ["fixtures/", fixture_name, ".wat"].concat();
461+
let fixture_path = ["frame/contracts/fixtures/", fixture_name, ".wat"].concat();
462462
let wasm_binary = wat::parse_file(fixture_path)?;
463463
let code_hash = T::Hashing::hash(&wasm_binary);
464464
Ok((wasm_binary, code_hash))

0 commit comments

Comments
 (0)