|
1 | 1 | use std::collections::HashMap; |
2 | 2 |
|
3 | | -use near_vm_runner::ContractRuntimeCache; |
4 | | - |
5 | 3 | use crate::modules::blocks::BlocksInfoByFinality; |
6 | 4 | use code_storage::CodeStorage; |
| 5 | +use near_vm_runner::ContractRuntimeCache; |
7 | 6 |
|
8 | 7 | mod code_storage; |
9 | 8 |
|
@@ -137,20 +136,39 @@ pub async fn run_contract( |
137 | 136 | block_hash: block.block_hash, |
138 | 137 | } |
139 | 138 | })?; |
| 139 | + println!("Contract code len {}", code.data.len()); |
140 | 140 | contract_code_cache.put(code_hash, code.data.clone()).await; |
141 | 141 | Contract::new(Some(code.data), code_hash) |
142 | 142 | } |
143 | 143 | } |
144 | 144 | }; |
145 | 145 |
|
| 146 | + // We need to calculate the state size of the contract to determine if we should prefetch the state or not. |
| 147 | + // The state size is the storage usage minus the code size. |
| 148 | + // If the state size is less than the prefetch_state_size_limit, we prefetch the state. |
| 149 | + let code_len = if let Some(contract_code) = &contract_code.contract_code { |
| 150 | + contract_code.code().len() |
| 151 | + } else if let Some(code) = contract_code_cache.get(&code_hash).await { |
| 152 | + code.len() |
| 153 | + } else { |
| 154 | + db_manager |
| 155 | + .get_contract_code(account_id, block.block_height, "query_call_function") |
| 156 | + .await |
| 157 | + .map(|code| code.data.len()) |
| 158 | + .unwrap_or_default() |
| 159 | + }; |
| 160 | + let state_size = contract |
| 161 | + .data |
| 162 | + .storage_usage() |
| 163 | + .saturating_sub(code_len as u64); |
146 | 164 | // Init an external database interface for the Runtime logic |
147 | 165 | let code_storage = CodeStorage::init( |
148 | 166 | db_manager.clone(), |
149 | 167 | account_id.clone(), |
150 | 168 | block.block_height, |
151 | 169 | validators, |
152 | 170 | optimistic_data, |
153 | | - contract.data.storage_usage() <= prefetch_state_size_limit, |
| 171 | + state_size <= prefetch_state_size_limit, |
154 | 172 | ) |
155 | 173 | .await; |
156 | 174 |
|
|
0 commit comments