sui_adapter_latest/data_store/legacy/
sui_data_store.rsuse crate::data_store::{PackageStore, legacy::linkage_view::LinkageView};
use move_binary_format::errors::{Location, PartialVMError, PartialVMResult, VMResult};
use move_core_types::{
account_address::AccountAddress, identifier::IdentStr, language_storage::ModuleId,
resolver::ModuleResolver, vm_status::StatusCode,
};
use move_vm_types::data_store::DataStore;
use std::rc::Rc;
use sui_types::{base_types::ObjectID, error::SuiResult, move_package::MovePackage};
pub(crate) struct SuiDataStore<'state, 'a> {
linkage_view: &'a LinkageView<'state>,
new_packages: &'a [MovePackage],
}
impl<'state, 'a> SuiDataStore<'state, 'a> {
pub(crate) fn new(
linkage_view: &'a LinkageView<'state>,
new_packages: &'a [MovePackage],
) -> Self {
Self {
linkage_view,
new_packages,
}
}
fn get_module(&self, module_id: &ModuleId) -> Option<&Vec<u8>> {
for package in self.new_packages {
let module = package.get_module(module_id);
if module.is_some() {
return module;
}
}
None
}
}
impl DataStore for SuiDataStore<'_, '_> {
fn link_context(&self) -> PartialVMResult<AccountAddress> {
self.linkage_view.link_context().map_err(|e| {
PartialVMError::new(StatusCode::UNKNOWN_INVARIANT_VIOLATION_ERROR)
.with_message(e.to_string())
})
}
fn relocate(&self, module_id: &ModuleId) -> PartialVMResult<ModuleId> {
self.linkage_view.relocate(module_id).map_err(|err| {
PartialVMError::new(StatusCode::LINKER_ERROR)
.with_message(format!("Error relocating {module_id}: {err:?}"))
})
}
fn defining_module(
&self,
runtime_id: &ModuleId,
struct_: &IdentStr,
) -> PartialVMResult<ModuleId> {
self.linkage_view
.defining_module(runtime_id, struct_)
.map_err(|err| {
PartialVMError::new(StatusCode::LINKER_ERROR).with_message(format!(
"Error finding defining module for {runtime_id}::{struct_}: {err:?}"
))
})
}
fn load_module(&self, module_id: &ModuleId) -> VMResult<Vec<u8>> {
if let Some(bytes) = self.get_module(module_id) {
return Ok(bytes.clone());
}
match self.linkage_view.get_module(module_id) {
Ok(Some(bytes)) => Ok(bytes),
Ok(None) => Err(PartialVMError::new(StatusCode::LINKER_ERROR)
.with_message(format!("Cannot find {:?} in data cache", module_id))
.finish(Location::Undefined)),
Err(err) => {
let msg = format!("Unexpected storage error: {:?}", err);
Err(
PartialVMError::new(StatusCode::UNKNOWN_INVARIANT_VIOLATION_ERROR)
.with_message(msg)
.finish(Location::Undefined),
)
}
}
}
fn publish_module(&mut self, _module_id: &ModuleId, _blob: Vec<u8>) -> VMResult<()> {
Ok(())
}
}
impl PackageStore for SuiDataStore<'_, '_> {
fn get_package(&self, id: &ObjectID) -> SuiResult<Option<Rc<MovePackage>>> {
for package in self.new_packages {
if package.id() == *id {
return Ok(Some(Rc::new(package.clone())));
}
}
self.linkage_view.get_package(id)
}
fn resolve_type_to_defining_id(
&self,
_module_address: ObjectID,
_module_name: &IdentStr,
_type_name: &IdentStr,
) -> SuiResult<Option<ObjectID>> {
unimplemented!(
"resolve_type_to_defining_id is not implemented for legacy::SuiDataStore and should never be called"
)
}
}