From a0341cefde3ba78bdb35128baa2d1290c3085bea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rain=20=E2=81=A3?= Date: Mon, 17 Jun 2019 17:09:32 -0700 Subject: [PATCH] [vm_runtime] remove duplicate code to convert CompiledScript to CompiledModule This code already exists in `vm`. --- language/vm/src/file_format.rs | 3 +++ .../vm_runtime/src/code_cache/module_cache.rs | 26 ++----------------- .../vm_runtime/src/code_cache/script_cache.rs | 13 ++++------ language/vm/vm_runtime/src/txn_executor.rs | 6 ++--- .../src/unit_tests/module_cache_tests.rs | 17 +++++------- .../src/unit_tests/runtime_tests.rs | 12 ++++----- 6 files changed, 26 insertions(+), 51 deletions(-) diff --git a/language/vm/src/file_format.rs b/language/vm/src/file_format.rs index 3db3965eeea1..039ed8c91bc0 100644 --- a/language/vm/src/file_format.rs +++ b/language/vm/src/file_format.rs @@ -1054,6 +1054,9 @@ pub struct CompiledScript { } impl CompiledScript { + /// Returns the index of `main` in case a script is converted to a module. + pub const MAIN_INDEX: FunctionDefinitionIndex = FunctionDefinitionIndex(0); + /// Converts a `CompiledScript` to a `CompiledModule` for code that wants a uniform view of /// both. pub fn into_module(self) -> CompiledModule { diff --git a/language/vm/vm_runtime/src/code_cache/module_cache.rs b/language/vm/vm_runtime/src/code_cache/module_cache.rs index 1eef17f83e5a..eb632c54efb8 100644 --- a/language/vm/vm_runtime/src/code_cache/module_cache.rs +++ b/language/vm/vm_runtime/src/code_cache/module_cache.rs @@ -18,8 +18,8 @@ use vm::{ access::{BaseAccess, ModuleAccess}, errors::*, file_format::{ - CompiledModule, CompiledScript, FunctionDefinitionIndex, FunctionHandleIndex, - SignatureToken, StructDefinitionIndex, StructHandleIndex, + CompiledModule, FunctionHandleIndex, SignatureToken, StructDefinitionIndex, + StructHandleIndex, }, views::{FunctionHandleView, StructHandleView}, }; @@ -120,28 +120,6 @@ pub struct VMModuleCache<'alloc> { map: CacheRefMap<'alloc, CodeKey, LoadedModule>, } -/// Convert a CompiledScript into a CompiledModule. -pub fn create_fake_module(script: CompiledScript) -> (CompiledModule, FunctionDefinitionIndex) { - ( - CompiledModule { - module_handles: script.module_handles, - struct_handles: script.struct_handles, - function_handles: script.function_handles, - - struct_defs: vec![], - field_defs: vec![], - function_defs: vec![script.main], - type_signatures: script.type_signatures, - function_signatures: script.function_signatures, - locals_signatures: script.locals_signatures, - string_pool: script.string_pool, - byte_array_pool: script.byte_array_pool, - address_pool: script.address_pool, - }, - FunctionDefinitionIndex::new(0), - ) -} - impl<'alloc> VMModuleCache<'alloc> { /// In order /// to get a cleaner lifetime, the loaded program trait will take an input parameter of Arena diff --git a/language/vm/vm_runtime/src/code_cache/script_cache.rs b/language/vm/vm_runtime/src/code_cache/script_cache.rs index 82ed5fa2e276..5bfc27970858 100644 --- a/language/vm/vm_runtime/src/code_cache/script_cache.rs +++ b/language/vm/vm_runtime/src/code_cache/script_cache.rs @@ -2,12 +2,9 @@ // SPDX-License-Identifier: Apache-2.0 //! Cache for commonly executed scripts -use crate::{ - code_cache::module_cache::create_fake_module, - loaded_data::{ - function::{FunctionRef, FunctionReference}, - loaded_module::LoadedModule, - }, +use crate::loaded_data::{ + function::{FunctionRef, FunctionReference}, + loaded_module::LoadedModule, }; use logger::prelude::*; use tiny_keccak::Keccak; @@ -46,13 +43,13 @@ impl<'alloc> ScriptCache<'alloc> { Ok(Ok(f)) } else { trace!("[VM] Script cache miss"); - let (fake_module, idx) = create_fake_module(script); + let fake_module = script.into_module(); let loaded_module = LoadedModule::new(fake_module)?; self.map .or_insert_with_try_transform( hash, move || loaded_module, - |module_ref| FunctionRef::new(module_ref, idx), + |module_ref| FunctionRef::new(module_ref, CompiledScript::MAIN_INDEX), ) .map(Ok) } diff --git a/language/vm/vm_runtime/src/txn_executor.rs b/language/vm/vm_runtime/src/txn_executor.rs index 30342d8c2d9c..1a77c9b9a2d8 100644 --- a/language/vm/vm_runtime/src/txn_executor.rs +++ b/language/vm/vm_runtime/src/txn_executor.rs @@ -3,7 +3,7 @@ //! Processor for a single transaction. use crate::{ - code_cache::module_cache::{create_fake_module, ModuleCache, VMModuleCache}, + code_cache::module_cache::{ModuleCache, VMModuleCache}, data_cache::{RemoteCache, TransactionDataCache}, execution_stack::ExecutionStack, gas_meter::GasMeter, @@ -818,9 +818,9 @@ pub fn execute_function( ) -> VMResult<()> { let allocator = Arena::new(); let module_cache = VMModuleCache::new(&allocator); - let (main_module, entry_idx) = create_fake_module(caller_script); + let main_module = caller_script.into_module(); let loaded_main = LoadedModule::new(main_module)?; - let entry_func = FunctionRef::new(&loaded_main, entry_idx)?; + let entry_func = FunctionRef::new(&loaded_main, CompiledScript::MAIN_INDEX)?; for m in modules { module_cache.cache_module(m)?; } diff --git a/language/vm/vm_runtime/src/unit_tests/module_cache_tests.rs b/language/vm/vm_runtime/src/unit_tests/module_cache_tests.rs index 495e7e700624..18689ae581cb 100644 --- a/language/vm/vm_runtime/src/unit_tests/module_cache_tests.rs +++ b/language/vm/vm_runtime/src/unit_tests/module_cache_tests.rs @@ -3,10 +3,7 @@ use super::*; use crate::{ - code_cache::{ - module_adapter::FakeFetcher, - module_cache::{create_fake_module, ModuleCache}, - }, + code_cache::{module_adapter::FakeFetcher, module_cache::ModuleCache}, loaded_data::function::{FunctionRef, FunctionReference}, }; use ::compiler::{compiler, parser::parse_program}; @@ -178,9 +175,9 @@ fn test_loader_cross_modules() { let loaded_program = VMModuleCache::new(&allocator); loaded_program.cache_module(module).unwrap(); - let (owned_entry_module, entry_idx) = create_fake_module(script); + let owned_entry_module = script.into_module(); let loaded_main = LoadedModule::new(owned_entry_module).unwrap(); - let entry_func = FunctionRef::new(&loaded_main, entry_idx).unwrap(); + let entry_func = FunctionRef::new(&loaded_main, CompiledScript::MAIN_INDEX).unwrap(); let entry_module = entry_func.module(); let func1 = loaded_program .resolve_function_ref(entry_module, FunctionHandleIndex::new(0)) @@ -209,9 +206,9 @@ fn test_loader_cross_modules() { fn test_cache_with_storage() { let allocator = Arena::new(); - let (owned_entry_module, entry_idx) = create_fake_module(test_script()); + let owned_entry_module = test_script().into_module(); let loaded_main = LoadedModule::new(owned_entry_module).unwrap(); - let entry_func = FunctionRef::new(&loaded_main, entry_idx).unwrap(); + let entry_func = FunctionRef::new(&loaded_main, CompiledScript::MAIN_INDEX).unwrap(); let entry_module = entry_func.module(); let vm_cache = VMModuleCache::new(&allocator); @@ -375,9 +372,9 @@ fn test_multi_level_cache_write_back() { address_pool: vec![AccountAddress::default()], }; - let (owned_entry_module, entry_idx) = create_fake_module(script); + let owned_entry_module = script.into_module(); let loaded_main = LoadedModule::new(owned_entry_module).unwrap(); - let entry_func = FunctionRef::new(&loaded_main, entry_idx).unwrap(); + let entry_func = FunctionRef::new(&loaded_main, CompiledScript::MAIN_INDEX).unwrap(); let entry_module = entry_func.module(); { diff --git a/language/vm/vm_runtime/src/unit_tests/runtime_tests.rs b/language/vm/vm_runtime/src/unit_tests/runtime_tests.rs index bc04ae44ceb4..29c5217dcc47 100644 --- a/language/vm/vm_runtime/src/unit_tests/runtime_tests.rs +++ b/language/vm/vm_runtime/src/unit_tests/runtime_tests.rs @@ -153,9 +153,9 @@ fn test_binop_instruction_overflow<'alloc, 'txn>( fn test_simple_instruction_transition() { let allocator = Arena::new(); let module_cache = VMModuleCache::new(&allocator); - let (main_module, entry_idx) = create_fake_module(fake_script()); + let main_module = fake_script().into_module(); let loaded_main = LoadedModule::new(main_module).unwrap(); - let entry_func = FunctionRef::new(&loaded_main, entry_idx).unwrap(); + let entry_func = FunctionRef::new(&loaded_main, CompiledScript::MAIN_INDEX).unwrap(); let data_cache = FakeDataCache::new(); let mut vm = TransactionExecutor::new(module_cache, &data_cache, TransactionMetadata::default()); @@ -343,9 +343,9 @@ fn test_simple_instruction_transition() { fn test_arith_instructions() { let allocator = Arena::new(); let module_cache = VMModuleCache::new(&allocator); - let (main_module, entry_idx) = create_fake_module(fake_script()); + let main_module = fake_script().into_module(); let loaded_main = LoadedModule::new(main_module).unwrap(); - let entry_func = FunctionRef::new(&loaded_main, entry_idx).unwrap(); + let entry_func = FunctionRef::new(&loaded_main, CompiledScript::MAIN_INDEX).unwrap(); let data_cache = FakeDataCache::new(); let mut vm = @@ -683,9 +683,9 @@ fn test_call() { fn test_transaction_info() { let allocator = Arena::new(); let module_cache = VMModuleCache::new(&allocator); - let (main_module, entry_idx) = create_fake_module(fake_script()); + let main_module = fake_script().into_module(); let loaded_main = LoadedModule::new(main_module).unwrap(); - let entry_func = FunctionRef::new(&loaded_main, entry_idx).unwrap(); + let entry_func = FunctionRef::new(&loaded_main, CompiledScript::MAIN_INDEX).unwrap(); let txn_info = { let (_, public_key) = crypto::signing::generate_genesis_keypair();