From bab0f2b5a585da12d6e6ddfbc1a34a8cc59965a8 Mon Sep 17 00:00:00 2001 From: Hans Moog Date: Thu, 14 Nov 2024 12:03:20 +0100 Subject: [PATCH] cleaned code --- crates/iota-framework/src/lib.rs | 9 ++-- .../move-binary-format/src/compatibility.rs | 48 ++++++++----------- 2 files changed, 22 insertions(+), 35 deletions(-) diff --git a/crates/iota-framework/src/lib.rs b/crates/iota-framework/src/lib.rs index 0de1b31779a..b7d1122ad3e 100644 --- a/crates/iota-framework/src/lib.rs +++ b/crates/iota-framework/src/lib.rs @@ -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; @@ -252,14 +251,12 @@ pub async fn compare_system_package( 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:?}"); diff --git a/external-crates/move/crates/move-binary-format/src/compatibility.rs b/external-crates/move/crates/move-binary-format/src/compatibility.rs index 26e3764fe03..0df08931e62 100644 --- a/external-crates/move/crates/move-binary-format/src/compatibility.rs +++ b/external-crates/move/crates/move-binary-format/src/compatibility.rs @@ -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; @@ -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 {