Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[framework] Refactor the error_mapping generator and binary include #1701

Merged
merged 3 commits into from
May 18, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions crates/rooch/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,7 @@ rooch-rpc-server = { workspace = true }
rooch-rpc-client = { workspace = true }
rooch-integration-test-runner = { workspace = true }

framework-release = { workspace = true }

[features]
dashboard = []
27 changes: 14 additions & 13 deletions crates/rooch/src/commands/move_cli/commands/explain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// SPDX-License-Identifier: Apache-2.0

use crate::cli_types::WalletContextOptions;
use bcs_ext;
use clap::*;
use move_core_types::errmap::{ErrorDescription, ErrorMapping};
use move_core_types::language_storage::ModuleId;
Expand Down Expand Up @@ -31,13 +30,16 @@ impl Explain {
let context = self.context_options.build()?;
let address_mapping = context.address_mapping();
let module_id = self.location.into_module_id(&address_mapping)?;
let error_descriptions = framework_types::error_descriptions::ERROR_DESCRIPTIONS.clone();
let error_description_bytes = error_descriptions.get(module_id.address());
let error_descriptions = &framework_release::error_descriptions::ERROR_DESCRIPTIONS;
let error_mapping = error_descriptions.get(module_id.address());

match error_description_bytes {
Some(bytes) => {
let explain_result =
explain_move_abort(AbortLocation::Module(module_id), self.abort_code, bytes);
match error_mapping {
Some(error_mapping) => {
let explain_result = explain_move_abort(
AbortLocation::Module(module_id),
self.abort_code,
error_mapping,
);
println!("{}", explain_result)
}
None => {
Expand All @@ -54,11 +56,10 @@ impl Explain {
pub fn get_explanation(
module_id: &ModuleId,
abort_code: u64,
data: &[u8],
error_mapping: &ErrorMapping,
) -> Option<ErrorDescription> {
let error_descriptions: ErrorMapping =
bcs_ext::from_bytes(data).expect("Decode err map failed");
error_descriptions.get_explanation(module_id.to_string().as_str(), abort_code)
let module_name = module_id.short_str_lossless();
error_mapping.get_explanation(&module_name, abort_code)
}

#[derive(Debug, Clone, Serialize, Deserialize, Hash, Eq, PartialEq)]
Expand Down Expand Up @@ -90,10 +91,10 @@ impl std::fmt::Display for MoveAbortExplain {
pub fn explain_move_abort(
abort_location: AbortLocation,
abort_code: u64,
data: &[u8],
error_mapping: &ErrorMapping,
) -> MoveAbortExplain {
let err_description = match abort_location {
AbortLocation::Module(module_id) => get_explanation(&module_id, abort_code, data),
AbortLocation::Module(module_id) => get_explanation(&module_id, abort_code, error_mapping),
AbortLocation::Script => None,
};
match err_description {
Expand Down
11 changes: 0 additions & 11 deletions examples/wasm_execution/sources/wasm.move

Large diffs are not rendered by default.

19 changes: 13 additions & 6 deletions frameworks/framework-builder/src/stdlib_configs.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) RoochNetwork
// SPDX-License-Identifier: Apache-2.0

use crate::{path_in_crate, Stdlib, StdlibBuildConfig};
use crate::{path_in_crate, release_dir, Stdlib, StdlibBuildConfig};
use anyhow::Result;
use move_package::BuildConfig;
use once_cell::sync::Lazy;
Expand All @@ -25,11 +25,14 @@ static STDLIB_BUILD_CONFIGS: Lazy<Vec<StdlibBuildConfig>> = Lazy::new(|| {
.canonicalize()
.expect("canonicalize path failed");

let latest_error_descriptions = release_dir().join("latest").join("error_descriptions");

vec![
StdlibBuildConfig {
path: move_stdlib_path.clone(),
error_prefix: "E".to_string(),
error_code_map_output_file: move_stdlib_path.join("error_description.errmap"),
error_code_map_output_file: latest_error_descriptions
.join("move_stdlib_error_description.errmap"),
document_template: move_stdlib_path.join("doc_template/README.md"),
document_output_directory: move_stdlib_path.join("doc"),
build_config: BuildConfig::default(),
Expand All @@ -38,7 +41,8 @@ static STDLIB_BUILD_CONFIGS: Lazy<Vec<StdlibBuildConfig>> = Lazy::new(|| {
StdlibBuildConfig {
path: moveos_stdlib_path.clone(),
error_prefix: "Error".to_string(),
error_code_map_output_file: moveos_stdlib_path.join("error_description.errmap"),
error_code_map_output_file: latest_error_descriptions
.join("moveos_stdlib_error_description.errmap"),
document_template: moveos_stdlib_path.join("doc_template/README.md"),
document_output_directory: moveos_stdlib_path.join("doc"),
build_config: BuildConfig::default(),
Expand All @@ -47,7 +51,8 @@ static STDLIB_BUILD_CONFIGS: Lazy<Vec<StdlibBuildConfig>> = Lazy::new(|| {
StdlibBuildConfig {
path: rooch_framework_path.clone(),
error_prefix: "Error".to_string(),
error_code_map_output_file: rooch_framework_path.join("error_description.errmap"),
error_code_map_output_file: latest_error_descriptions
.join("rooch_framework_error_description.errmap"),
document_template: rooch_framework_path.join("doc_template/README.md"),
document_output_directory: rooch_framework_path.join("doc"),
build_config: BuildConfig::default(),
Expand All @@ -56,7 +61,8 @@ static STDLIB_BUILD_CONFIGS: Lazy<Vec<StdlibBuildConfig>> = Lazy::new(|| {
StdlibBuildConfig {
path: bitcoin_move_path.clone(),
error_prefix: "Error".to_string(),
error_code_map_output_file: bitcoin_move_path.join("error_description.errmap"),
error_code_map_output_file: latest_error_descriptions
.join("bitcoin_move_error_description.errmap"),
document_template: bitcoin_move_path.join("doc_template/README.md"),
document_output_directory: bitcoin_move_path.join("doc"),
build_config: BuildConfig::default(),
Expand All @@ -65,7 +71,8 @@ static STDLIB_BUILD_CONFIGS: Lazy<Vec<StdlibBuildConfig>> = Lazy::new(|| {
StdlibBuildConfig {
path: rooch_nursery_path.clone(),
error_prefix: "Error".to_string(),
error_code_map_output_file: rooch_nursery_path.join("error_description.errmap"),
error_code_map_output_file: latest_error_descriptions
.join("rooch_nursery_error_description.errmap"),
document_template: rooch_nursery_path.join("doc_template/README.md"),
document_output_directory: rooch_nursery_path.join("doc"),
build_config: BuildConfig::default(),
Expand Down
2 changes: 2 additions & 0 deletions frameworks/framework-release/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ serde = { workspace = true }
tracing = { workspace = true }
tracing-subscriber = { workspace = true }
include_dir = { workspace = true }
bcs = { workspace = true }

move-core-types = { workspace = true }
move-binary-format = { workspace = true }
Expand All @@ -29,6 +30,7 @@ move-package = { workspace = true }
moveos-types = { workspace = true }
moveos-stdlib = { workspace = true }

framework-types = { workspace = true }
framework-builder = { workspace = true }

[build-dependencies]
Expand Down
80 changes: 80 additions & 0 deletions frameworks/framework-release/src/error_descriptions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright (c) RoochNetwork
// SPDX-License-Identifier: Apache-2.0

use crate::STATIC_FRAMEWORK_DIR;
use framework_types::addresses::*;
use move_core_types::{account_address::AccountAddress, errmap::ErrorMapping};
use once_cell::sync::Lazy;
use std::collections::BTreeMap;

pub static ERROR_DESCRIPTIONS: Lazy<BTreeMap<AccountAddress, ErrorMapping>> = Lazy::new(|| {
let mut error_descriptions = BTreeMap::new();

let move_stdlib_err: ErrorMapping = bcs::from_bytes(
STATIC_FRAMEWORK_DIR
.get_file("latest/error_descriptions/move_stdlib_error_description.errmap")
.unwrap()
.contents(),
)
.unwrap();
error_descriptions.insert(MOVE_STD_ADDRESS, move_stdlib_err);

let moveos_std_err: ErrorMapping = bcs::from_bytes(
STATIC_FRAMEWORK_DIR
.get_file("latest/error_descriptions/moveos_stdlib_error_description.errmap")
.unwrap()
.contents(),
)
.unwrap();
error_descriptions.insert(MOVEOS_STD_ADDRESS, moveos_std_err);

let rooch_framework_err: ErrorMapping = bcs::from_bytes(
STATIC_FRAMEWORK_DIR
.get_file("latest/error_descriptions/rooch_framework_error_description.errmap")
.unwrap()
.contents(),
)
.unwrap();

error_descriptions.insert(ROOCH_FRAMEWORK_ADDRESS, rooch_framework_err);

let bitcoin_move_err: ErrorMapping = bcs::from_bytes(
STATIC_FRAMEWORK_DIR
.get_file("latest/error_descriptions/bitcoin_move_error_description.errmap")
.unwrap()
.contents(),
)
.unwrap();

error_descriptions.insert(BITCOIN_MOVE_ADDRESS, bitcoin_move_err);

let rooch_nursery_err: ErrorMapping = bcs::from_bytes(
STATIC_FRAMEWORK_DIR
.get_file("latest/error_descriptions/rooch_nursery_error_description.errmap")
.unwrap()
.contents(),
)
.unwrap();

error_descriptions.insert(ROOCH_NURSERY_ADDRESS, rooch_nursery_err);

error_descriptions
});

#[cfg(test)]
mod tests {

use super::*;

#[test]
fn test_error_descriptions() {
let error_descriptions = ERROR_DESCRIPTIONS.clone();
let error_mapping = error_descriptions.get(&MOVEOS_STD_ADDRESS).unwrap();
//println!("{:?}",error_mapping.module_error_maps);
let description = error_mapping.get_explanation("0x2::object", 1);
//println!("{:?}",description);
assert!(description.is_some());
let description = description.unwrap();
assert_eq!(description.code_name.as_str(), "ErrorAlreadyExists");
}
}
2 changes: 2 additions & 0 deletions frameworks/framework-release/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use anyhow::{anyhow, Result};
use framework_builder::{stdlib_version::StdlibVersion, Stdlib};
use include_dir::{include_dir, Dir};

pub mod error_descriptions;

pub(crate) const STATIC_FRAMEWORK_DIR: Dir = include_dir!("released");

pub fn load_stdlib(version: StdlibVersion) -> Result<Stdlib> {
Expand Down
55 changes: 0 additions & 55 deletions frameworks/framework-types/src/error_descriptions.rs

This file was deleted.

1 change: 0 additions & 1 deletion frameworks/framework-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@
// SPDX-License-Identifier: Apache-2.0

pub mod addresses;
pub mod error_descriptions;
Binary file removed frameworks/move-stdlib/error_description.errmap
Binary file not shown.
Binary file removed frameworks/moveos-stdlib/error_description.errmap
Binary file not shown.
Binary file not shown.
Loading