|
17 | 17 | use crate::{BalanceOf, ContractInfo, ContractInfoOf, TombstoneContractInfo, |
18 | 18 | Trait, prefixed_trie_id, AliveContractInfo}; |
19 | 19 | use runtime_primitives::traits::{Bounded, CheckedDiv, CheckedMul, Saturating, Zero, |
20 | | - SaturatedConversion}; |
| 20 | + SaturatedConversion, Hash as HashT}; |
21 | 21 | use srml_support::traits::{Currency, ExistenceRequirement, Get, WithdrawReason}; |
22 | 22 | use srml_support::{storage::child, StorageMap}; |
23 | 23 |
|
@@ -101,11 +101,10 @@ fn try_evict_or_and_pay_rent<T: Trait>( |
101 | 101 | // The contract cannot afford to leave a tombstone, so remove the contract info altogether. |
102 | 102 | <ContractInfoOf<T>>::remove(account); |
103 | 103 | let p_key = prefixed_trie_id(&contract.trie_id); |
104 | | - let child_trie = child::fetch_or_new( |
105 | | - p_key.as_ref(), |
106 | | - ¤t_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 | + } |
109 | 108 | return (RentOutcome::Evicted, None); |
110 | 109 | } |
111 | 110 |
|
@@ -150,21 +149,25 @@ fn try_evict_or_and_pay_rent<T: Trait>( |
150 | 149 | // threshold, so it leaves a tombstone. |
151 | 150 |
|
152 | 151 | let p_key = prefixed_trie_id(&contract.trie_id); |
153 | | - let child_trie = child::fetch_or_new( |
154 | | - p_key.as_ref(), |
155 | | - ¤t_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 | + }; |
161 | 164 | let tombstone = <TombstoneContractInfo<T>>::new( |
162 | 165 | &child_storage_root[..], |
163 | 166 | contract.code_hash, |
164 | 167 | ); |
165 | 168 | let tombstone_info = ContractInfo::Tombstone(tombstone); |
166 | 169 | <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)); |
168 | 171 |
|
169 | 172 | return (RentOutcome::Evicted, Some(tombstone_info)); |
170 | 173 | } |
|
0 commit comments