Skip to content

Commit

Permalink
cleaned code
Browse files Browse the repository at this point in the history
  • Loading branch information
hmoog committed Nov 14, 2024
1 parent 7ab9e5e commit bab0f2b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 35 deletions.
9 changes: 3 additions & 6 deletions crates/iota-framework/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ use move_binary_format::{
CompiledModule,
binary_config::BinaryConfig,
compatibility::Compatibility,
errors::{PartialVMError, PartialVMResult},
file_format::{Ability, AbilitySet},
};
use move_core_types::{gas_algebra::InternalGas, vm_status::StatusCode};
use move_core_types::gas_algebra::InternalGas;
use once_cell::sync::Lazy;
use serde::{Deserialize, Serialize};
use tracing::error;
Expand Down Expand Up @@ -252,14 +251,12 @@ pub async fn compare_system_package<S: ObjectStore>(
return Err(anyhow!("Could not normalize existing package: {e:?}"));
}
};
let mut new_normalized = new_pkg
.normalize(binary_config)
.map_err(|e| PartialVMError::new(StatusCode::UNKNOWN_STATUS).with_message(e.to_string()))?;
let mut new_normalized = new_pkg.normalize(binary_config)?;

for (name, cur_module) in cur_normalized {
let new_module = new_normalized
.remove(&name)
.ok_or(PartialVMError::new(StatusCode::UNKNOWN_STATUS))?;
.ok_or_else(|| anyhow!("removed"))?;

if let Err(e) = compatibility.check(&cur_module, &new_module) {
error!("Compatibility check failed, for new version of {id}::{name}: {e:?}");
Expand Down
48 changes: 19 additions & 29 deletions external-crates/move/crates/move-binary-format/src/compatibility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// Modifications Copyright (c) 2024 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use anyhow::{Result, anyhow};

use std::collections::BTreeSet;

use move_core_types::vm_status::StatusCode;
Expand Down Expand Up @@ -119,35 +121,23 @@ impl Compatibility {

/// Check compatibility for `new_module` relative to old module
/// `old_module`.
pub fn check(&self, old_module: &Module, new_module: &Module) -> PartialVMResult<()> {
// add macro to simplify error handling with a formatted error reason
macro_rules! return_if {
($check:ident, $($arg:tt)*) => {
if self.$check {
return Err(
PartialVMError::new(StatusCode::BACKWARD_INCOMPATIBLE_MODULE_UPDATE)
.with_message(format!(
"error in module {}: {}", old_module.module_id(), format!($($arg)*),
))
);
}
};
}
macro_rules! datatype_and_function_linking {
($($arg:tt)*) => { return_if!(check_datatype_and_pub_function_linking, $($arg)*) }
}
macro_rules! datatype_layout {
($($arg:tt)*) => { return_if!(check_datatype_layout, $($arg)*) }
}
macro_rules! friend_linking {
($($arg:tt)*) => { return_if!(check_friend_linking, $($arg)*) }
}
macro_rules! entry_linking {
($($arg:tt)*) => { return_if!(check_private_entry_linking, $($arg)*) }
}
macro_rules! no_new_variants {
($($arg:tt)*) => { return_if!(disallow_new_variants, $($arg)*) }
}
pub fn check(&self, old_module: &Module, new_module: &Module) -> Result<()> {
// add macro to simplify error handling
macro_rules! datatype_and_function_linking { ($($arg:tt)*) => {
if self.check_datatype_and_pub_function_linking { return Err(anyhow!($($arg)*)); }
}}
macro_rules! datatype_layout { ($($arg:tt)*) => {
if self.check_datatype_layout { return Err(anyhow!($($arg)*)); }
}}
macro_rules! friend_linking { ($($arg:tt)*) => {
if self.check_friend_linking { return Err(anyhow!($($arg)*)); }
}}
macro_rules! entry_linking { ($($arg:tt)*) => {
if self.check_private_entry_linking { return Err(anyhow!($($arg)*)); }
}}
macro_rules! no_new_variants { ($($arg:tt)*) => {
if self.disallow_new_variants { return Err(anyhow!($($arg)*)); }
}}

// module's name and address are unchanged
if old_module.address != new_module.address || old_module.name != new_module.name {
Expand Down

0 comments on commit bab0f2b

Please sign in to comment.