referrerpolicy=no-referrer-when-downgrade

pallet_revive/
storage.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18//! This module contains routines for accessing and altering a contract related state.
19
20pub mod meter;
21
22use crate::{
23	address::AddressMapper,
24	exec::{AccountIdOf, Key},
25	storage::meter::Diff,
26	tracing::if_tracing,
27	weights::WeightInfo,
28	AccountInfoOf, BalanceOf, BalanceWithDust, Config, DeletionQueue, DeletionQueueCounter, Error,
29	TrieId, SENTINEL,
30};
31use alloc::vec::Vec;
32use codec::{Decode, Encode, MaxEncodedLen};
33use core::marker::PhantomData;
34use frame_support::{
35	storage::child::{self, ChildInfo},
36	weights::{Weight, WeightMeter},
37	CloneNoBound, DebugNoBound, DefaultNoBound,
38};
39use scale_info::TypeInfo;
40use sp_core::{Get, H160};
41use sp_io::KillStorageResult;
42use sp_runtime::{
43	traits::{Hash, Saturating, Zero},
44	DispatchError, RuntimeDebug,
45};
46
47pub enum AccountIdOrAddress<T: Config> {
48	/// An account that is a contract.
49	AccountId(AccountIdOf<T>),
50	/// An externally owned account (EOA).
51	Address(H160),
52}
53
54/// Represents the account information for a contract or an externally owned account (EOA).
55#[derive(
56	DefaultNoBound,
57	Encode,
58	Decode,
59	CloneNoBound,
60	PartialEq,
61	Eq,
62	RuntimeDebug,
63	TypeInfo,
64	MaxEncodedLen,
65)]
66#[scale_info(skip_type_params(T))]
67pub struct AccountInfo<T: Config> {
68	/// The type of the account.
69	pub account_type: AccountType<T>,
70
71	// The  amount that was transferred to this account that is less than the
72	// NativeToEthRatio, and can be represented in the native currency
73	pub dust: u32,
74}
75
76/// The account type is used to distinguish between contracts and externally owned accounts.
77#[derive(
78	DefaultNoBound,
79	Encode,
80	Decode,
81	CloneNoBound,
82	PartialEq,
83	Eq,
84	RuntimeDebug,
85	TypeInfo,
86	MaxEncodedLen,
87)]
88#[scale_info(skip_type_params(T))]
89pub enum AccountType<T: Config> {
90	/// An account that is a contract.
91	Contract(ContractInfo<T>),
92
93	/// An account that is an externally owned account (EOA).
94	#[default]
95	EOA,
96}
97
98/// Information for managing an account and its sub trie abstraction.
99/// This is the required info to cache for an account.
100#[derive(Encode, Decode, CloneNoBound, PartialEq, Eq, DebugNoBound, TypeInfo, MaxEncodedLen)]
101#[scale_info(skip_type_params(T))]
102pub struct ContractInfo<T: Config> {
103	/// Unique ID for the subtree encoded as a bytes vector.
104	pub trie_id: TrieId,
105	/// The code associated with a given account.
106	pub code_hash: sp_core::H256,
107	/// How many bytes of storage are accumulated in this contract's child trie.
108	storage_bytes: u32,
109	/// How many items of storage are accumulated in this contract's child trie.
110	storage_items: u32,
111	/// This records to how much deposit the accumulated `storage_bytes` amount to.
112	pub storage_byte_deposit: BalanceOf<T>,
113	/// This records to how much deposit the accumulated `storage_items` amount to.
114	storage_item_deposit: BalanceOf<T>,
115	/// This records how much deposit is put down in order to pay for the contract itself.
116	///
117	/// We need to store this information separately so it is not used when calculating any refunds
118	/// since the base deposit can only ever be refunded on contract termination.
119	storage_base_deposit: BalanceOf<T>,
120	/// The size of the immutable data of this contract.
121	immutable_data_len: u32,
122}
123
124impl<T: Config> From<H160> for AccountIdOrAddress<T> {
125	fn from(address: H160) -> Self {
126		AccountIdOrAddress::Address(address)
127	}
128}
129
130impl<T: Config> AccountIdOrAddress<T> {
131	pub fn address(&self) -> H160 {
132		match self {
133			AccountIdOrAddress::AccountId(id) =>
134				<T::AddressMapper as AddressMapper<T>>::to_address(id),
135			AccountIdOrAddress::Address(address) => *address,
136		}
137	}
138
139	pub fn account_id(&self) -> AccountIdOf<T> {
140		match self {
141			AccountIdOrAddress::AccountId(id) => id.clone(),
142			AccountIdOrAddress::Address(address) => T::AddressMapper::to_account_id(address),
143		}
144	}
145}
146
147impl<T: Config> From<ContractInfo<T>> for AccountType<T> {
148	fn from(contract_info: ContractInfo<T>) -> Self {
149		AccountType::Contract(contract_info)
150	}
151}
152
153impl<T: Config> AccountInfo<T> {
154	/// Returns true if the account is a contract.
155	fn is_contract(address: &H160) -> bool {
156		let Some(info) = <AccountInfoOf<T>>::get(address) else { return false };
157		matches!(info.account_type, AccountType::Contract(_))
158	}
159
160	/// Returns the balance of the account at the given address.
161	pub fn balance(account: AccountIdOrAddress<T>) -> BalanceWithDust<BalanceOf<T>> {
162		use frame_support::traits::{
163			fungible::Inspect,
164			tokens::{Fortitude::Polite, Preservation::Preserve},
165		};
166
167		let value = T::Currency::reducible_balance(&account.account_id(), Preserve, Polite);
168		let dust = <AccountInfoOf<T>>::get(account.address()).map(|a| a.dust).unwrap_or_default();
169		BalanceWithDust::new_unchecked::<T>(value, dust)
170	}
171
172	/// Loads the contract information for a given address.
173	pub fn load_contract(address: &H160) -> Option<ContractInfo<T>> {
174		let Some(info) = <AccountInfoOf<T>>::get(address) else { return None };
175		let AccountType::Contract(contract_info) = info.account_type else { return None };
176		Some(contract_info)
177	}
178
179	/// Insert a contract, existing dust if any will be unchanged.
180	pub fn insert_contract(address: &H160, contract: ContractInfo<T>) {
181		AccountInfoOf::<T>::mutate(address, |account| {
182			if let Some(account) = account {
183				account.account_type = contract.clone().into();
184			} else {
185				*account = Some(AccountInfo { account_type: contract.clone().into(), dust: 0 });
186			}
187		});
188	}
189}
190
191impl<T: Config> ContractInfo<T> {
192	/// Constructs a new contract info **without** writing it to storage.
193	///
194	/// This returns an `Err` if an contract with the supplied `account` already exists
195	/// in storage.
196	pub fn new(
197		address: &H160,
198		nonce: T::Nonce,
199		code_hash: sp_core::H256,
200	) -> Result<Self, DispatchError> {
201		if <AccountInfo<T>>::is_contract(address) {
202			return Err(Error::<T>::DuplicateContract.into());
203		}
204
205		let trie_id = {
206			let buf = ("bcontract_trie_v1", address, nonce).using_encoded(T::Hashing::hash);
207			buf.as_ref()
208				.to_vec()
209				.try_into()
210				.expect("Runtime uses a reasonable hash size. Hence sizeof(T::Hash) <= 128; qed")
211		};
212
213		let contract = Self {
214			trie_id,
215			code_hash,
216			storage_bytes: 0,
217			storage_items: 0,
218			storage_byte_deposit: Zero::zero(),
219			storage_item_deposit: Zero::zero(),
220			storage_base_deposit: Zero::zero(),
221			immutable_data_len: 0,
222		};
223
224		Ok(contract)
225	}
226
227	/// Associated child trie unique id is built from the hash part of the trie id.
228	pub fn child_trie_info(&self) -> ChildInfo {
229		ChildInfo::new_default(self.trie_id.as_ref())
230	}
231
232	/// The deposit paying for the accumulated storage generated within the contract's child trie.
233	pub fn extra_deposit(&self) -> BalanceOf<T> {
234		self.storage_byte_deposit.saturating_add(self.storage_item_deposit)
235	}
236
237	/// Same as [`Self::extra_deposit`] but including the base deposit.
238	pub fn total_deposit(&self) -> BalanceOf<T> {
239		self.extra_deposit().saturating_add(self.storage_base_deposit)
240	}
241
242	/// Returns the storage base deposit of the contract.
243	pub fn storage_base_deposit(&self) -> BalanceOf<T> {
244		self.storage_base_deposit
245	}
246
247	/// Reads a storage kv pair of a contract.
248	///
249	/// The read is performed from the `trie_id` only. The `address` is not necessary. If the
250	/// contract doesn't store under the given `key` `None` is returned.
251	pub fn read(&self, key: &Key) -> Option<Vec<u8>> {
252		let value = child::get_raw(&self.child_trie_info(), key.hash().as_slice());
253		if_tracing(|t| {
254			t.storage_read(key, value.as_deref());
255		});
256		return value
257	}
258
259	/// Returns `Some(len)` (in bytes) if a storage item exists at `key`.
260	///
261	/// Returns `None` if the `key` wasn't previously set by `set_storage` or
262	/// was deleted.
263	pub fn size(&self, key: &Key) -> Option<u32> {
264		child::len(&self.child_trie_info(), key.hash().as_slice())
265	}
266
267	/// Update a storage entry into a contract's kv storage.
268	///
269	/// If the `new_value` is `None` then the kv pair is removed. If `take` is true
270	/// a [`WriteOutcome::Taken`] is returned instead of a [`WriteOutcome::Overwritten`].
271	///
272	/// This function also records how much storage was created or removed if a `storage_meter`
273	/// is supplied. It should only be absent for testing or benchmarking code.
274	pub fn write(
275		&self,
276		key: &Key,
277		new_value: Option<Vec<u8>>,
278		storage_meter: Option<&mut meter::NestedMeter<T>>,
279		take: bool,
280	) -> Result<WriteOutcome, DispatchError> {
281		let hashed_key = key.hash();
282		if_tracing(|t| {
283			let old = child::get_raw(&self.child_trie_info(), hashed_key.as_slice());
284			t.storage_write(key, old, new_value.as_deref());
285		});
286
287		self.write_raw(&hashed_key, new_value.as_deref(), storage_meter, take)
288	}
289
290	/// Update a storage entry into a contract's kv storage.
291	/// Function used in benchmarks, which can simulate prefix collision in keys.
292	#[cfg(feature = "runtime-benchmarks")]
293	pub fn bench_write_raw(
294		&self,
295		key: &[u8],
296		new_value: Option<Vec<u8>>,
297		take: bool,
298	) -> Result<WriteOutcome, DispatchError> {
299		self.write_raw(key, new_value.as_deref(), None, take)
300	}
301
302	fn write_raw(
303		&self,
304		key: &[u8],
305		new_value: Option<&[u8]>,
306		storage_meter: Option<&mut meter::NestedMeter<T>>,
307		take: bool,
308	) -> Result<WriteOutcome, DispatchError> {
309		let child_trie_info = &self.child_trie_info();
310		let (old_len, old_value) = if take {
311			let val = child::get_raw(child_trie_info, key);
312			(val.as_ref().map(|v| v.len() as u32), val)
313		} else {
314			(child::len(child_trie_info, key), None)
315		};
316
317		if let Some(storage_meter) = storage_meter {
318			let mut diff = meter::Diff::default();
319			let key_len = key.len() as u32;
320			match (old_len, new_value.as_ref().map(|v| v.len() as u32)) {
321				(Some(old_len), Some(new_len)) =>
322					if new_len > old_len {
323						diff.bytes_added = new_len - old_len;
324					} else {
325						diff.bytes_removed = old_len - new_len;
326					},
327				(None, Some(new_len)) => {
328					diff.bytes_added = new_len.saturating_add(key_len);
329					diff.items_added = 1;
330				},
331				(Some(old_len), None) => {
332					diff.bytes_removed = old_len.saturating_add(key_len);
333					diff.items_removed = 1;
334				},
335				(None, None) => (),
336			}
337			storage_meter.charge(&diff);
338		}
339
340		match &new_value {
341			Some(new_value) => child::put_raw(child_trie_info, key, new_value),
342			None => child::kill(child_trie_info, key),
343		}
344
345		Ok(match (old_len, old_value) {
346			(None, _) => WriteOutcome::New,
347			(Some(old_len), None) => WriteOutcome::Overwritten(old_len),
348			(Some(_), Some(old_value)) => WriteOutcome::Taken(old_value),
349		})
350	}
351
352	/// Sets and returns the contract base deposit.
353	///
354	/// The base deposit is updated when the `code_hash` of the contract changes, as it depends on
355	/// the deposit paid to upload the contract's code. It also depends on the size of immutable
356	/// storage which is also changed when the code hash of a contract is changed.
357	pub fn update_base_deposit(&mut self, code_deposit: BalanceOf<T>) -> BalanceOf<T> {
358		let contract_deposit = Diff {
359			bytes_added: (self.encoded_size() as u32).saturating_add(self.immutable_data_len),
360			items_added: if self.immutable_data_len == 0 { 1 } else { 2 },
361			..Default::default()
362		}
363		.update_contract::<T>(None)
364		.charge_or_zero();
365
366		// Instantiating the contract prevents its code to be deleted, therefore the base deposit
367		// includes a fraction (`T::CodeHashLockupDepositPercent`) of the original storage deposit
368		// to prevent abuse.
369		let code_deposit = T::CodeHashLockupDepositPercent::get().mul_ceil(code_deposit);
370
371		let deposit = contract_deposit.saturating_add(code_deposit);
372		self.storage_base_deposit = deposit;
373		deposit
374	}
375
376	/// Push a contract's trie to the deletion queue for lazy removal.
377	///
378	/// You must make sure that the contract is also removed when queuing the trie for deletion.
379	pub fn queue_trie_for_deletion(&self) {
380		DeletionQueueManager::<T>::load().insert(self.trie_id.clone());
381	}
382
383	/// Calculates the weight that is necessary to remove one key from the trie and how many
384	/// of those keys can be deleted from the deletion queue given the supplied weight limit.
385	pub fn deletion_budget(meter: &WeightMeter) -> (Weight, u32) {
386		let base_weight = T::WeightInfo::on_process_deletion_queue_batch();
387		let weight_per_key = T::WeightInfo::on_initialize_per_trie_key(1) -
388			T::WeightInfo::on_initialize_per_trie_key(0);
389
390		// `weight_per_key` being zero makes no sense and would constitute a failure to
391		// benchmark properly. We opt for not removing any keys at all in this case.
392		let key_budget = meter
393			.limit()
394			.saturating_sub(base_weight)
395			.checked_div_per_component(&weight_per_key)
396			.unwrap_or(0) as u32;
397
398		(weight_per_key, key_budget)
399	}
400
401	/// Delete as many items from the deletion queue possible within the supplied weight limit.
402	pub fn process_deletion_queue_batch(meter: &mut WeightMeter) {
403		if meter.try_consume(T::WeightInfo::on_process_deletion_queue_batch()).is_err() {
404			return
405		};
406
407		let mut queue = <DeletionQueueManager<T>>::load();
408		if queue.is_empty() {
409			return;
410		}
411
412		let (weight_per_key, budget) = Self::deletion_budget(&meter);
413		let mut remaining_key_budget = budget;
414		while remaining_key_budget > 0 {
415			let Some(entry) = queue.next() else { break };
416
417			#[allow(deprecated)]
418			let outcome = child::kill_storage(
419				&ChildInfo::new_default(&entry.trie_id),
420				Some(remaining_key_budget),
421			);
422
423			match outcome {
424				// This happens when our budget wasn't large enough to remove all keys.
425				KillStorageResult::SomeRemaining(keys_removed) => {
426					remaining_key_budget.saturating_reduce(keys_removed);
427					break
428				},
429				KillStorageResult::AllRemoved(keys_removed) => {
430					entry.remove();
431					// charge at least one key even if none were removed.
432					remaining_key_budget = remaining_key_budget.saturating_sub(keys_removed.max(1));
433				},
434			};
435		}
436
437		meter.consume(weight_per_key.saturating_mul(u64::from(budget - remaining_key_budget)))
438	}
439
440	/// Returns the code hash of the contract specified by `account` ID.
441	pub fn load_code_hash(account: &AccountIdOf<T>) -> Option<sp_core::H256> {
442		<AccountInfo<T>>::load_contract(&T::AddressMapper::to_address(account)).map(|i| i.code_hash)
443	}
444
445	/// Returns the amount of immutable bytes of this contract.
446	pub fn immutable_data_len(&self) -> u32 {
447		self.immutable_data_len
448	}
449
450	/// Set the number of immutable bytes of this contract.
451	pub fn set_immutable_data_len(&mut self, immutable_data_len: u32) {
452		self.immutable_data_len = immutable_data_len;
453	}
454}
455
456/// Information about what happened to the pre-existing value when calling [`ContractInfo::write`].
457#[cfg_attr(any(test, feature = "runtime-benchmarks"), derive(Debug, PartialEq))]
458pub enum WriteOutcome {
459	/// No value existed at the specified key.
460	New,
461	/// A value of the returned length was overwritten.
462	Overwritten(u32),
463	/// The returned value was taken out of storage before being overwritten.
464	///
465	/// This is only returned when specifically requested because it causes additional work
466	/// depending on the size of the pre-existing value. When not requested [`Self::Overwritten`]
467	/// is returned instead.
468	Taken(Vec<u8>),
469}
470
471impl WriteOutcome {
472	/// Extracts the size of the overwritten value or `0` if there
473	/// was no value in storage.
474	pub fn old_len(&self) -> u32 {
475		match self {
476			Self::New => 0,
477			Self::Overwritten(len) => *len,
478			Self::Taken(value) => value.len() as u32,
479		}
480	}
481
482	/// Extracts the size of the overwritten value or `SENTINEL` if there
483	/// was no value in storage.
484	///
485	/// # Note
486	///
487	/// We cannot use `0` as sentinel value because there could be a zero sized
488	/// storage entry which is different from a non existing one.
489	pub fn old_len_with_sentinel(&self) -> u32 {
490		match self {
491			Self::New => SENTINEL,
492			Self::Overwritten(len) => *len,
493			Self::Taken(value) => value.len() as u32,
494		}
495	}
496}
497
498/// Manage the removal of contracts storage that are marked for deletion.
499///
500/// When a contract is deleted by calling `seal_terminate` it becomes inaccessible
501/// immediately, but the deletion of the storage items it has accumulated is performed
502/// later by pulling the contract from the queue in the `on_idle` hook.
503#[derive(Encode, Decode, TypeInfo, MaxEncodedLen, DefaultNoBound, Clone)]
504#[scale_info(skip_type_params(T))]
505pub struct DeletionQueueManager<T: Config> {
506	/// Counter used as a key for inserting a new deleted contract in the queue.
507	/// The counter is incremented after each insertion.
508	insert_counter: u32,
509	/// The index used to read the next element to be deleted in the queue.
510	/// The counter is incremented after each deletion.
511	delete_counter: u32,
512
513	_phantom: PhantomData<T>,
514}
515
516/// View on a contract that is marked for deletion.
517struct DeletionQueueEntry<'a, T: Config> {
518	/// the trie id of the contract to delete.
519	trie_id: TrieId,
520
521	/// A mutable reference on the queue so that the contract can be removed, and none can be added
522	/// or read in the meantime.
523	queue: &'a mut DeletionQueueManager<T>,
524}
525
526impl<'a, T: Config> DeletionQueueEntry<'a, T> {
527	/// Remove the contract from the deletion queue.
528	fn remove(self) {
529		<DeletionQueue<T>>::remove(self.queue.delete_counter);
530		self.queue.delete_counter = self.queue.delete_counter.wrapping_add(1);
531		<DeletionQueueCounter<T>>::set(self.queue.clone());
532	}
533}
534
535impl<T: Config> DeletionQueueManager<T> {
536	/// Load the `DeletionQueueCounter`, so we can perform read or write operations on the
537	/// DeletionQueue storage.
538	fn load() -> Self {
539		<DeletionQueueCounter<T>>::get()
540	}
541
542	/// Returns `true` if the queue contains no elements.
543	fn is_empty(&self) -> bool {
544		self.insert_counter.wrapping_sub(self.delete_counter) == 0
545	}
546
547	/// Insert a contract in the deletion queue.
548	fn insert(&mut self, trie_id: TrieId) {
549		<DeletionQueue<T>>::insert(self.insert_counter, trie_id);
550		self.insert_counter = self.insert_counter.wrapping_add(1);
551		<DeletionQueueCounter<T>>::set(self.clone());
552	}
553
554	/// Fetch the next contract to be deleted.
555	///
556	/// Note:
557	/// we use the delete counter to get the next value to read from the queue and thus don't pay
558	/// the cost of an extra call to `sp_io::storage::next_key` to lookup the next entry in the map
559	fn next(&mut self) -> Option<DeletionQueueEntry<T>> {
560		if self.is_empty() {
561			return None
562		}
563
564		let entry = <DeletionQueue<T>>::get(self.delete_counter);
565		entry.map(|trie_id| DeletionQueueEntry { trie_id, queue: self })
566	}
567}
568
569#[cfg(test)]
570impl<T: Config> DeletionQueueManager<T> {
571	pub fn from_test_values(insert_counter: u32, delete_counter: u32) -> Self {
572		Self { insert_counter, delete_counter, _phantom: Default::default() }
573	}
574	pub fn as_test_tuple(&self) -> (u32, u32) {
575		(self.insert_counter, self.delete_counter)
576	}
577}