Skip to content

Commit 8cadd7b

Browse files
authored
[sui-verifier] Move helper functions into more foundational crates (#11135)
## Description `sui-verifier` contained a number of helper functions that were being used by other crates (like `sui-json`). These helpers don't need to be versioned as part of the execution layer, but `sui-verifier` in general does. By moving helpers into `sui-types` and `move-bytecode-utils` (which are not versioned), we avoid `sui-json` having to deal with the versioned API. ## Test Plan ``` $ cargo simtest $ env SUI_SKIP_SIMTESTS=1 cargo nextest run ```
1 parent 6f0699d commit 8cadd7b

File tree

1 file changed

+76
-2
lines changed
  • tools/move-bytecode-utils/src

1 file changed

+76
-2
lines changed

tools/move-bytecode-utils/src/lib.rs

+76-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,14 @@ pub mod layout;
77
pub mod module_cache;
88

99
use crate::dependency_graph::DependencyGraph;
10-
use move_binary_format::{access::ModuleAccess, file_format::CompiledModule};
11-
use move_core_types::language_storage::ModuleId;
10+
use move_binary_format::{
11+
access::ModuleAccess,
12+
binary_views::BinaryIndexedView,
13+
file_format::{CompiledModule, SignatureToken, StructHandleIndex},
14+
};
15+
use move_core_types::{
16+
account_address::AccountAddress, identifier::IdentStr, language_storage::ModuleId,
17+
};
1218

1319
use anyhow::{anyhow, Result};
1420
use std::collections::BTreeMap;
@@ -94,3 +100,71 @@ impl<'a> Modules<'a> {
94100
Ok(all_deps)
95101
}
96102
}
103+
104+
pub fn resolve_struct<'a>(
105+
view: &'a BinaryIndexedView,
106+
sidx: StructHandleIndex,
107+
) -> (&'a AccountAddress, &'a IdentStr, &'a IdentStr) {
108+
let shandle = view.struct_handle_at(sidx);
109+
let mhandle = view.module_handle_at(shandle.module);
110+
let address = view.address_identifier_at(mhandle.address);
111+
let module_name = view.identifier_at(mhandle.name);
112+
let struct_name = view.identifier_at(shandle.name);
113+
(address, module_name, struct_name)
114+
}
115+
116+
pub fn format_signature_token(view: &BinaryIndexedView, t: &SignatureToken) -> String {
117+
match t {
118+
SignatureToken::Bool => "bool".to_string(),
119+
SignatureToken::U8 => "u8".to_string(),
120+
SignatureToken::U16 => "u16".to_string(),
121+
SignatureToken::U32 => "u32".to_string(),
122+
SignatureToken::U64 => "u64".to_string(),
123+
SignatureToken::U128 => "u128".to_string(),
124+
SignatureToken::U256 => "u256".to_string(),
125+
SignatureToken::Address => "address".to_string(),
126+
SignatureToken::Signer => "signer".to_string(),
127+
SignatureToken::Vector(inner) => {
128+
format!("vector<{}>", format_signature_token(view, inner))
129+
}
130+
SignatureToken::Reference(inner) => format!("&{}", format_signature_token(view, inner)),
131+
SignatureToken::MutableReference(inner) => {
132+
format!("&mut {}", format_signature_token(view, inner))
133+
}
134+
SignatureToken::TypeParameter(i) => format!("T{}", i),
135+
136+
SignatureToken::Struct(idx) => format_signature_token_struct(view, *idx, &[]),
137+
SignatureToken::StructInstantiation(idx, ty_args) => {
138+
format_signature_token_struct(view, *idx, ty_args)
139+
}
140+
}
141+
}
142+
143+
pub fn format_signature_token_struct(
144+
view: &BinaryIndexedView,
145+
sidx: StructHandleIndex,
146+
ty_args: &[SignatureToken],
147+
) -> String {
148+
let (address, module_name, struct_name) = resolve_struct(view, sidx);
149+
let s;
150+
let ty_args_string = if ty_args.is_empty() {
151+
""
152+
} else {
153+
s = format!(
154+
"<{}>",
155+
ty_args
156+
.iter()
157+
.map(|t| format_signature_token(view, t))
158+
.collect::<Vec<_>>()
159+
.join(", ")
160+
);
161+
&s
162+
};
163+
format!(
164+
"0x{}::{}::{}{}",
165+
address.short_str_lossless(),
166+
module_name,
167+
struct_name,
168+
ty_args_string
169+
)
170+
}

0 commit comments

Comments
 (0)