Skip to content

Commit

Permalink
[move-bytecode-template] Emit better error on identifier failure (#16587
Browse files Browse the repository at this point in the history
)

## Description 

Explicit error on incorrect Identifier passed.

## Test Plan 

Tested.

### Type of Change (Check all that apply)

- [ ] protocol change
- [ ] user-visible impact
- [ ] breaking change for a client SDKs
- [ ] breaking change for FNs (FN binary must upgrade)
- [ ] breaking change for validators or node operators (must upgrade
binaries)
- [ ] breaking change for on-chain data layout
- [ ] necessitate either a data wipe or data migration

### Release notes
  • Loading branch information
damirka authored Mar 11, 2024
1 parent 8cf578a commit 42fce1d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
16 changes: 13 additions & 3 deletions sdk/move-bytecode-template/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,24 @@ pub fn deserialize(binary: &[u8]) -> Result<JsValue, JsErr> {
/// });
/// ```
pub fn update_identifiers(binary: &[u8], map: JsValue) -> Result<Box<[u8]>, JsErr> {
let updates: HashMap<String, String> = serde_wasm_bindgen::from_value(map)?;
let mut updates: HashMap<String, String> = serde_wasm_bindgen::from_value(map)?;
let mut compiled_module = CompiledModule::deserialize_with_defaults(binary)?;

// First update the identifiers.
for ident in compiled_module.identifiers.iter_mut() {
let old = ident.to_string();
if let Some(new) = updates.get(&old) {
*ident = Identifier::new(new.clone()).map_err(|err| JsErr {
if updates.contains_key(&old) {
let new = updates.remove(&old).unwrap();

// Check if the new identifier is valid. Return a proper error if not.
if !Identifier::is_valid(&new) {
return Err(JsErr {
display: format!("Invalid identifier: {}", new),
message: "Invalid identifier".to_string(),
});
}

*ident = Identifier::new(new).map_err(|err| JsErr {
display: format!("{}", err),
message: err.to_string(),
})?;
Expand Down
14 changes: 14 additions & 0 deletions sdk/move-bytecode-template/tests/universal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,20 @@ describe('move-binary-template', () => {
.find((c) => c.value_bcs == bcs.string().serialize('MCN').toBytes())
);
});

it('should fail on incorrect identifier', () => {
expect(() => {
template.update_identifiers(pokemonBytes(), { Stats: '123123PokeStats' });
}).toThrow();

expect(() => {
template.update_identifiers(pokemonBytes(), { Stats: '\\aaa' });
}).toThrow();

expect(() => {
template.update_identifiers(pokemonBytes(), { Stats: '+say_hello' });
}).toThrow();
});
});

function pokemonBytes() {
Expand Down

0 comments on commit 42fce1d

Please sign in to comment.