Skip to content

Commit

Permalink
feat(*): add custum errors define
Browse files Browse the repository at this point in the history
  • Loading branch information
wd30130 committed Aug 20, 2024
1 parent 4763e69 commit ecb94f8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
22 changes: 11 additions & 11 deletions frame/hybrid-vm/src/interoperate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,22 +82,22 @@ impl<T: Config> InterCall<T> {
target_gas: Weight,
) -> Result<(Vec<u8>, Weight)> {
if !T::EnableCallWasmVM::get() {
return Err(DispatchError::from("EnableCallWasmVM is false, can't call wasm VM."));
return Err(DispatchError::from(Error::<T>::DisableCallWasmVM));
}

let input: Vec<u8>;
let target: Vec<u8>;

match vm_codec::wasm_encode(&data[32..].iter().cloned().collect()) {
Ok(r) => (input, target) = r,
Err(e) => return Err(DispatchError::from(str2s(e.to_string()))),
Err(_) => return Err(DispatchError::from(Error::<T>::WasmEncodeError)),
}

let gas_limit: Weight = target_gas;

let origin = ensure_signed(origin)?;
let target = <T as frame_system::Config>::AccountId::decode(&mut target.as_slice())
.map_err(|e| DispatchError::from(str2s(e.to_string())))?;
.map_err(|_| DispatchError::from(Error::<T>::AccountIdDecodeError))?;

let info = pallet_contracts::Pallet::<T>::bare_call(
origin,
Expand All @@ -123,23 +123,23 @@ impl<T: Config> InterCall<T> {
"",
);
} else {
return Err(DispatchError::from("Call wasm contract failed(REVERT)"));
return Err(DispatchError::from(Error::<T>::WasmContractRevert));
}
},
Err(e) => return Err(e),
}

match output {
Ok(r) => return Ok((r, info.gas_consumed)),
Err(e) => return Err(DispatchError::from(str2s(e.to_string()))),
Err(_) => return Err(DispatchError::from(Error::<T>::WasmDecodeError)),
}
}
}

impl<C: Config> InterCall<C> {
pub fn call_evm<E: Ext<T = C>>(mut env: Environment<E, InitState>) -> Result<RetVal> {
if !C::EnableCallEVM::get() {
return Err(DispatchError::from("EnableCallEVM is false, can't call evm."));
return Err(DispatchError::from(Error::<C>::DisableCallEvm));
}

let gas_meter = env.ext().gas_meter();
Expand All @@ -156,8 +156,8 @@ impl<C: Config> InterCall<C> {

match vm_codec::evm_encode(&input0) {
Ok(r) => (input, target) = r,
Err(e) => {
return Err(DispatchError::from(str2s(e.to_string())));
Err(_) => {
return Err(DispatchError::from(Error::<C>::EvmEncodeError));
},
}

Expand Down Expand Up @@ -187,7 +187,7 @@ impl<C: Config> InterCall<C> {
if success.is_succeed() {
output = vm_codec::evm_decode(&input0, &v1, true, "");
} else {
return Err(DispatchError::from("Call EVM failed "));
return Err(DispatchError::from(Error::<C>::EVMExecuteFailed));
}
},
};
Expand All @@ -201,13 +201,13 @@ impl<C: Config> InterCall<C> {
Ok(r) => {
let output = envbuf
.write(&r, false, None)
.map_err(|_| DispatchError::from("ChainExtension failed to write result"));
.map_err(|_| DispatchError::from(Error::<C>::ChainExtensionWriteError));
match output {
Ok(_) => return Ok(RetVal::Converging(0)),
Err(e) => return Err(e),
}
},
Err(e) => return Err(DispatchError::from(str2s(e.to_string()))),
Err(_) => return Err(DispatchError::from(Error::<C>::EvmDecodeError)),
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions frame/hybrid-vm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,14 @@ pub mod pallet {
EvmABIDecodeError,
EvmABIError,
WasmContractRevert,
DisableCallWasmVM,
DisableCallEvm,
AccountIdDecodeError,
WasmEncodeError,
WasmDecodeError,
EvmEncodeError,
EvmDecodeError,
ChainExtensionWriteError,
}

#[pallet::hooks]
Expand Down

0 comments on commit ecb94f8

Please sign in to comment.