Repository: MystenLabs/sui
File: crates/sui-move/src/unit_test.rs (Rust)

Code before:
    fn vm_config(&self) -> VMConfig {
        sui_adapter::adapter::vm_config(&self.protocol_config)
    }

    fn native_function_table(&self) -> move_vm_runtime::native_functions::NativeFunctionTable {
        self.native_function_table.clone()
    }

    fn new_extensions(&self) -> InMemoryTestStore {
        InMemoryTestStore(RefCell::new(InMemoryStorage::default()))
    }

    fn new_native_context_extensions<'ext>(
        &self,
        store: &'ext InMemoryTestStore,
    ) -> NativeContextExtensions<'ext> {
        let mut ext = NativeContextExtensions::default();
        // Use a throwaway metrics registry for testing.
        let registry = prometheus::Registry::new();
        let metrics = Arc::new(LimitsMetrics::new(&registry));
        // let store = InMemoryTestStore(RefCell::new(InMemoryStorage::default()));
        let protocol_config = ProtocolConfig::get_for_max_version_UNSAFE();

        ext.add(ObjectRuntime::new(
            store,
            BTreeMap::new(),
            false,
            Box::leak(Box::new(ProtocolConfig::get_for_max_version_UNSAFE())), // leak for testing
            metrics,
            0, // epoch id
        ));
        ext.add(NativesCostTable::from_protocol_config(&protocol_config));
        let tx_context = TxContext::new_from_components(
            &SuiAddress::ZERO,
            &TransactionDigest::default(),
            &0,
            0,
            0,
            0,
            0,
            None,
            &protocol_config,
        );
        ext.add(TransactionContext::new_for_testing(Rc::new(RefCell::new(
            tx_context,
        ))));
        ext.add(store);
        ext
    }
}


Review comment (suggestion):
nit
```suggestion
            Box::leak(Box::new(self.protocol_config.clone())), // leak for testing
```

Code after:
    fn vm_config(&self) -> VMConfig {
        sui_adapter::adapter::vm_config(&self.protocol_config)
    }

    fn native_function_table(&self) -> move_vm_runtime::native_functions::NativeFunctionTable {
        self.native_function_table.clone()
    }

    fn new_extensions_builder(&self) -> InMemoryTestStore {
        InMemoryTestStore(RefCell::new(InMemoryStorage::default()))
    }

    fn new_native_context_extensions<'ext>(
        &self,
        store: &'ext InMemoryTestStore,
    ) -> NativeContextExtensions<'ext> {
        let mut ext = NativeContextExtensions::default();
        // Use a throwaway metrics registry for testing.
        let registry = prometheus::Registry::new();
        let metrics = Arc::new(LimitsMetrics::new(&registry));

        ext.add(ObjectRuntime::new(
            store,
            BTreeMap::new(),
            false,
            Box::leak(Box::new(ProtocolConfig::get_for_max_version_UNSAFE())), // leak for testing
            metrics,
            0, // epoch id
        ));
        ext.add(NativesCostTable::from_protocol_config(
            &self.protocol_config,
        ));
        let tx_context = TxContext::new_from_components(
            &SuiAddress::ZERO,
            &TransactionDigest::default(),
            &0,
            0,
            0,
            0,
            0,
            None,
            &self.protocol_config,
        );
        ext.add(TransactionContext::new_for_testing(Rc::new(RefCell::new(
            tx_context,
        ))));
        ext.add(store);
        ext
    }
}