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

Commit 661ba2e

Browse files
committed
Avoid unused creation of child trie.
1 parent d8c58c6 commit 661ba2e

File tree

3 files changed

+25
-16
lines changed

3 files changed

+25
-16
lines changed

core/primitives/src/child_trie.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ impl ChildTrie {
216216
pub fn parent_key_slice(p: &ParentTrie) -> &[u8] {
217217
&p[CHILD_STORAGE_KEY_PREFIX.len()..]
218218
}
219+
219220
/// Method for fetching or initiating a new child trie.
220221
///
221222
/// Note that call back could do nothing, which will allow unspecified behavior,
@@ -297,6 +298,11 @@ impl ChildTrie {
297298
pub fn keyspace(&self) -> &KeySpace {
298299
&self.keyspace
299300
}
301+
/// Getter function for extension content of child trie.
302+
pub fn extension(&self) -> &[u8] {
303+
&self.extension[..]
304+
}
305+
300306
/// Encoder for the child trie, with a new root value.
301307
/// The child trie current root value is not updated (if
302308
/// content is commited the child trie will need to be fetch

core/state-machine/src/backend.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ impl<H: Hasher> Backend<H> for InMemory<H> {
407407

408408
let is_default = root == default_child_trie_root::<H>();
409409

410-
(root, is_default, full_transaction)
410+
(root, is_default && child_trie.extension().is_empty(), full_transaction)
411411
}
412412

413413
fn pairs(&self) -> Vec<(Vec<u8>, Vec<u8>)> {

srml/contracts/src/rent.rs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
use crate::{BalanceOf, ContractInfo, ContractInfoOf, TombstoneContractInfo,
1818
Trait, prefixed_trie_id, AliveContractInfo};
1919
use runtime_primitives::traits::{Bounded, CheckedDiv, CheckedMul, Saturating, Zero,
20-
SaturatedConversion};
20+
SaturatedConversion, Hash as HashT};
2121
use srml_support::traits::{Currency, ExistenceRequirement, Get, WithdrawReason};
2222
use srml_support::{storage::child, StorageMap};
2323

@@ -101,11 +101,10 @@ fn try_evict_or_and_pay_rent<T: Trait>(
101101
// The contract cannot afford to leave a tombstone, so remove the contract info altogether.
102102
<ContractInfoOf<T>>::remove(account);
103103
let p_key = prefixed_trie_id(&contract.trie_id);
104-
let child_trie = child::fetch_or_new(
105-
p_key.as_ref(),
106-
&current_block_number,
107-
);
108-
runtime_io::kill_child_storage(&child_trie);
104+
105+
if let Some(child_trie) = child::child_trie(p_key.as_ref()) {
106+
runtime_io::kill_child_storage(&child_trie);
107+
}
109108
return (RentOutcome::Evicted, None);
110109
}
111110

@@ -150,21 +149,25 @@ fn try_evict_or_and_pay_rent<T: Trait>(
150149
// threshold, so it leaves a tombstone.
151150

152151
let p_key = prefixed_trie_id(&contract.trie_id);
153-
let child_trie = child::fetch_or_new(
154-
p_key.as_ref(),
155-
&current_block_number,
156-
);
157-
158-
// Note: this operation is heavy.
159-
let child_storage_root = runtime_io::child_storage_root(&child_trie);
160-
152+
let (o_ct, child_storage_root) = if let Some(child_trie) = child::child_trie(p_key.as_ref()) {
153+
// Note: this operation is heavy.
154+
let child_storage_root = runtime_io::child_storage_root(&child_trie);
155+
(Some(child_trie), child_storage_root)
156+
} else {
157+
// Note that this will fail as soon as we support multiple type
158+
// of hashing for child trie.
159+
let child_storage_root =
160+
<<T as system::Trait>::Hashing as HashT>::enumerated_trie_root(&[]);
161+
let child_storage_root_ref: &[u8] = child_storage_root.as_ref();
162+
(None, child_storage_root_ref.to_vec())
163+
};
161164
let tombstone = <TombstoneContractInfo<T>>::new(
162165
&child_storage_root[..],
163166
contract.code_hash,
164167
);
165168
let tombstone_info = ContractInfo::Tombstone(tombstone);
166169
<ContractInfoOf<T>>::insert(account, &tombstone_info);
167-
runtime_io::kill_child_storage(&child_trie);
170+
o_ct.map(|child_trie| runtime_io::kill_child_storage(&child_trie));
168171

169172
return (RentOutcome::Evicted, Some(tombstone_info));
170173
}

0 commit comments

Comments
 (0)