Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Fix error handling
Browse files Browse the repository at this point in the history
- comment errors: ReturnCodes
- update mock ext implementation
- return Error::CodeNotFound when no code for such hash
  • Loading branch information
yarikbratashchuk committed Jan 20, 2022
1 parent e36d5e5 commit a5a3ec8
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 8 deletions.
1 change: 1 addition & 0 deletions frame/contracts/src/benchmarking/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,7 @@ benchmarks! {
let origin = RawOrigin::Signed(instance.caller.clone());
}: call(origin, instance.addr, 0u32.into(), Weight::MAX, None, vec![])

#[skip_meta]
seal_set_code_hash {
let r in 0 .. API_BENCHMARK_BATCHES;
let code_hashes = (0..r * API_BENCHMARK_BATCH_SIZE)
Expand Down
2 changes: 1 addition & 1 deletion frame/contracts/src/wasm/code_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ pub fn increment_refcount<T: Config>(code_hash: CodeHash<T>) -> Result<(), Dispa
info.refcount = info.refcount.saturating_add(1);
Ok(())
} else {
Err(DispatchError::CannotLookup)
Err(Error::<T>::CodeNotFound.into())
}
})
}
Expand Down
6 changes: 4 additions & 2 deletions frame/contracts/src/wasm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ mod tests {
}

pub struct MockExt {
code_hashes: Vec<CodeHash<Test>>,
storage: HashMap<StorageKey, Vec<u8>>,
instantiates: Vec<InstantiateEntry>,
terminations: Vec<TerminationEntry>,
Expand All @@ -328,6 +329,7 @@ mod tests {
impl Default for MockExt {
fn default() -> Self {
Self {
code_hashes: Default::default(),
storage: Default::default(),
instantiates: Default::default(),
terminations: Default::default(),
Expand Down Expand Up @@ -410,7 +412,7 @@ mod tests {
Ok(result)
}
fn set_code_hash(&mut self, hash: CodeHash<Self::T>) -> Result<(), DispatchError> {
self.storage.insert(StorageKey::from(hash), vec![1, 2, 3]);
self.code_hashes.push(hash);
Ok(())
}
fn caller(&self) -> &AccountIdOf<Self::T> {
Expand Down Expand Up @@ -2225,7 +2227,7 @@ mod tests {
let mut mock_ext = MockExt::default();
execute(CODE, [0u8; 32].encode(), &mut mock_ext).unwrap();

assert_eq!(mock_ext.storage.get(&[17u8; 32]), Some(&vec![1, 2, 3]));
assert_eq!(mock_ext.code_hashes.pop().unwrap(), H256::from_slice(&[17u8; 32]));
}

#[test]
Expand Down
13 changes: 8 additions & 5 deletions frame/contracts/src/wasm/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -826,14 +826,17 @@ define_env!(Env, <E: Ext>,
//
// # Errors
//
// - requested buffer is not within the bounds of the sandbox memory.
// - the buffer contents cannot be decoded as the required type.
// - code for specified hash does not exist
// `ReturnCode::CodeNotFound`
[__unstable__] seal_set_code_hash(ctx, code_hash_ptr: u32) -> ReturnCode => {
ctx.charge_gas(RuntimeCosts::SetCodeHash)?;
let code_hash: CodeHash<<E as Ext>::T> = ctx.read_sandbox_memory_as(code_hash_ptr)?;
ctx.ext.set_code_hash(code_hash)?;
Ok(ReturnCode::Success)
match ctx.ext.set_code_hash(code_hash) {
Err(err) => {
let code = Runtime::<E>::err_into_return_code(err)?;
Ok(code)
},
Ok(()) => Ok(ReturnCode::Success)
}
},

// Set the value at the given key in the contract storage.
Expand Down

0 comments on commit a5a3ec8

Please sign in to comment.