Skip to content

Commit a90d324

Browse files
authored
Contracts: Remove topics for internal events (#4510)
1 parent 2e36f57 commit a90d324

File tree

6 files changed

+107
-118
lines changed

6 files changed

+107
-118
lines changed

prdoc/pr_4510.prdoc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
title: "[Contracts] Remove internal topic index"
2+
3+
doc:
4+
- audience: Runtime Dev
5+
description: |
6+
This PR removes topics from internal events emitted by pallet_contracts. It does not touch the `deposit_event` host function used by
7+
smart contracts that can still include topics.
8+
Event topics incurs significant Storage costs, and are only used by light clients to index events and avoid downloading the entire block.
9+
They are not used by Dapp or Indexers that download the whole block anyway.
10+
11+
crates:
12+
- name: pallet-contracts
13+
bump: patch

substrate/frame/contracts/src/exec.rs

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ use sp_core::{
4646
};
4747
use sp_io::{crypto::secp256k1_ecdsa_recover_compressed, hashing::blake2_256};
4848
use sp_runtime::{
49-
traits::{Convert, Dispatchable, Hash, Zero},
49+
traits::{Convert, Dispatchable, Zero},
5050
DispatchError,
5151
};
5252
use sp_std::{fmt::Debug, marker::PhantomData, mem, prelude::*, vec::Vec};
@@ -983,16 +983,16 @@ where
983983
let caller = self.caller().account_id()?.clone();
984984

985985
// Deposit an instantiation event.
986-
Contracts::<T>::deposit_event(
987-
vec![T::Hashing::hash_of(&caller), T::Hashing::hash_of(account_id)],
988-
Event::Instantiated { deployer: caller, contract: account_id.clone() },
989-
);
986+
Contracts::<T>::deposit_event(Event::Instantiated {
987+
deployer: caller,
988+
contract: account_id.clone(),
989+
});
990990
},
991991
(ExportedFunction::Call, Some(code_hash)) => {
992-
Contracts::<T>::deposit_event(
993-
vec![T::Hashing::hash_of(account_id), T::Hashing::hash_of(&code_hash)],
994-
Event::DelegateCalled { contract: account_id.clone(), code_hash },
995-
);
992+
Contracts::<T>::deposit_event(Event::DelegateCalled {
993+
contract: account_id.clone(),
994+
code_hash,
995+
});
996996
},
997997
(ExportedFunction::Call, None) => {
998998
// If a special limit was set for the sub-call, we enforce it here.
@@ -1002,10 +1002,10 @@ where
10021002
frame.nested_storage.enforce_subcall_limit(contract)?;
10031003

10041004
let caller = self.caller();
1005-
Contracts::<T>::deposit_event(
1006-
vec![T::Hashing::hash_of(&caller), T::Hashing::hash_of(&account_id)],
1007-
Event::Called { caller: caller.clone(), contract: account_id.clone() },
1008-
);
1005+
Contracts::<T>::deposit_event(Event::Called {
1006+
caller: caller.clone(),
1007+
contract: account_id.clone(),
1008+
});
10091009
},
10101010
}
10111011

@@ -1324,13 +1324,10 @@ where
13241324
.charge_deposit(frame.account_id.clone(), StorageDeposit::Refund(*deposit));
13251325
}
13261326

1327-
Contracts::<T>::deposit_event(
1328-
vec![T::Hashing::hash_of(&frame.account_id), T::Hashing::hash_of(&beneficiary)],
1329-
Event::Terminated {
1330-
contract: frame.account_id.clone(),
1331-
beneficiary: beneficiary.clone(),
1332-
},
1333-
);
1327+
Contracts::<T>::deposit_event(Event::Terminated {
1328+
contract: frame.account_id.clone(),
1329+
beneficiary: beneficiary.clone(),
1330+
});
13341331
Ok(())
13351332
}
13361333

@@ -1422,7 +1419,7 @@ where
14221419
}
14231420

14241421
fn deposit_event(&mut self, topics: Vec<T::Hash>, data: Vec<u8>) {
1425-
Contracts::<Self::T>::deposit_event(
1422+
Contracts::<Self::T>::deposit_indexed_event(
14261423
topics,
14271424
Event::ContractEmitted { contract: self.top_frame().account_id.clone(), data },
14281425
);
@@ -1527,14 +1524,11 @@ where
15271524

15281525
Self::increment_refcount(hash)?;
15291526
Self::decrement_refcount(prev_hash);
1530-
Contracts::<Self::T>::deposit_event(
1531-
vec![T::Hashing::hash_of(&frame.account_id), hash, prev_hash],
1532-
Event::ContractCodeUpdated {
1533-
contract: frame.account_id.clone(),
1534-
new_code_hash: hash,
1535-
old_code_hash: prev_hash,
1536-
},
1537-
);
1527+
Contracts::<Self::T>::deposit_event(Event::ContractCodeUpdated {
1528+
contract: frame.account_id.clone(),
1529+
new_code_hash: hash,
1530+
old_code_hash: prev_hash,
1531+
});
15381532
Ok(())
15391533
}
15401534

@@ -1639,7 +1633,7 @@ mod tests {
16391633
exec::ExportedFunction::*,
16401634
gas::GasMeter,
16411635
tests::{
1642-
test_utils::{get_balance, hash, place_contract, set_balance},
1636+
test_utils::{get_balance, place_contract, set_balance},
16431637
ExtBuilder, RuntimeCall, RuntimeEvent as MetaEvent, Test, TestFilter, ALICE, BOB,
16441638
CHARLIE, GAS_LIMIT,
16451639
},
@@ -3164,7 +3158,7 @@ mod tests {
31643158
caller: Origin::from_account_id(ALICE),
31653159
contract: BOB,
31663160
}),
3167-
topics: vec![hash(&Origin::<Test>::from_account_id(ALICE)), hash(&BOB)],
3161+
topics: vec![],
31683162
},
31693163
]
31703164
);
@@ -3264,7 +3258,7 @@ mod tests {
32643258
caller: Origin::from_account_id(ALICE),
32653259
contract: BOB,
32663260
}),
3267-
topics: vec![hash(&Origin::<Test>::from_account_id(ALICE)), hash(&BOB)],
3261+
topics: vec![],
32683262
},
32693263
]
32703264
);

substrate/frame/contracts/src/lib.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ use frame_system::{
135135
use scale_info::TypeInfo;
136136
use smallvec::Array;
137137
use sp_runtime::{
138-
traits::{Convert, Dispatchable, Hash, Saturating, StaticLookup, Zero},
138+
traits::{Convert, Dispatchable, Saturating, StaticLookup, Zero},
139139
DispatchError, RuntimeDebug,
140140
};
141141
use sp_std::{fmt::Debug, prelude::*};
@@ -833,14 +833,11 @@ pub mod pallet {
833833
};
834834
<ExecStack<T, WasmBlob<T>>>::increment_refcount(code_hash)?;
835835
<ExecStack<T, WasmBlob<T>>>::decrement_refcount(contract.code_hash);
836-
Self::deposit_event(
837-
vec![T::Hashing::hash_of(&dest), code_hash, contract.code_hash],
838-
Event::ContractCodeUpdated {
839-
contract: dest.clone(),
840-
new_code_hash: code_hash,
841-
old_code_hash: contract.code_hash,
842-
},
843-
);
836+
Self::deposit_event(Event::ContractCodeUpdated {
837+
contract: dest.clone(),
838+
new_code_hash: code_hash,
839+
old_code_hash: contract.code_hash,
840+
});
844841
contract.code_hash = code_hash;
845842
Ok(())
846843
})
@@ -1827,8 +1824,13 @@ impl<T: Config> Pallet<T> {
18271824
Ok(())
18281825
}
18291826

1830-
/// Deposit a pallet contracts event. Handles the conversion to the overarching event type.
1831-
fn deposit_event(topics: Vec<T::Hash>, event: Event<T>) {
1827+
/// Deposit a pallet contracts event.
1828+
fn deposit_event(event: Event<T>) {
1829+
<frame_system::Pallet<T>>::deposit_event(<T as Config>::RuntimeEvent::from(event))
1830+
}
1831+
1832+
/// Deposit a pallet contracts indexed event.
1833+
fn deposit_indexed_event(topics: Vec<T::Hash>, event: Event<T>) {
18321834
<frame_system::Pallet<T>>::deposit_event_indexed(
18331835
&topics,
18341836
<T as Config>::RuntimeEvent::from(event).into(),

substrate/frame/contracts/src/storage/meter.rs

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ use frame_support::{
3434
DefaultNoBound, RuntimeDebugNoBound,
3535
};
3636
use sp_runtime::{
37-
traits::{Hash as HashT, Saturating, Zero},
37+
traits::{Saturating, Zero},
3838
DispatchError, FixedPointNumber, FixedU128,
3939
};
40-
use sp_std::{fmt::Debug, marker::PhantomData, vec, vec::Vec};
40+
use sp_std::{fmt::Debug, marker::PhantomData, vec::Vec};
4141

4242
/// Deposit that uses the native fungible's balance type.
4343
pub type DepositOf<T> = Deposit<BalanceOf<T>>;
@@ -551,14 +551,11 @@ impl<T: Config> Ext<T> for ReservingExt {
551551
Fortitude::Polite,
552552
)?;
553553

554-
Pallet::<T>::deposit_event(
555-
vec![T::Hashing::hash_of(&origin), T::Hashing::hash_of(&contract)],
556-
Event::StorageDepositTransferredAndHeld {
557-
from: origin.clone(),
558-
to: contract.clone(),
559-
amount: *amount,
560-
},
561-
);
554+
Pallet::<T>::deposit_event(Event::StorageDepositTransferredAndHeld {
555+
from: origin.clone(),
556+
to: contract.clone(),
557+
amount: *amount,
558+
});
562559
},
563560
Deposit::Refund(amount) => {
564561
let transferred = T::Currency::transfer_on_hold(
@@ -571,14 +568,11 @@ impl<T: Config> Ext<T> for ReservingExt {
571568
Fortitude::Polite,
572569
)?;
573570

574-
Pallet::<T>::deposit_event(
575-
vec![T::Hashing::hash_of(&contract), T::Hashing::hash_of(&origin)],
576-
Event::StorageDepositTransferredAndReleased {
577-
from: contract.clone(),
578-
to: origin.clone(),
579-
amount: transferred,
580-
},
581-
);
571+
Pallet::<T>::deposit_event(Event::StorageDepositTransferredAndReleased {
572+
from: contract.clone(),
573+
to: origin.clone(),
574+
amount: transferred,
575+
});
582576

583577
if transferred < *amount {
584578
// This should never happen, if it does it means that there is a bug in the

0 commit comments

Comments
 (0)