Fix the bug: decode panic form a not exist contract address
@@ -105,7 +105,6 @@ impl PermissionManagement {
}
/// Resources array
- /// TODO Result. Check []
pub fn resources(executor: &Executor, address: &Address) -> Vec<Resource> {
let output = executor.call_contract_method(address, &*RESOURCES_HASH.as_slice());
trace!("Resources output: {:?}", output);
@@ -114,7 +113,8 @@ impl PermissionManagement {
}
fn to_address_vec(output: &[u8]) -> Vec<Address> {
- let mut decoded = decode(&[ParamType::Array(Box::new(ParamType::Address))], &output).unwrap();
+ match decode(&[ParamType::Array(Box::new(ParamType::Address))], &output) {
+ Ok(mut decoded) => {
let addresses: Vec<Token> = decoded.remove(0).to_array().unwrap();
let addresses: Vec<Address> = addresses
.into_iter()
@@ -123,16 +123,20 @@ impl PermissionManagement {
debug!("Decoded addresses: {:?}", addresses);
addresses
}
+ Err(_) => Vec::new(),
+ }
+ }
fn to_resource_vec(output: &[u8]) -> Vec<Resource> {
// Decode the address[] and bytes4[]
- let mut decoded = decode(
+ match decode(
&[
ParamType::Array(Box::new(ParamType::Address)),
ParamType::Array(Box::new(ParamType::FixedBytes(4))),
],
&output,
- ).unwrap();
+ ) {
+ Ok(mut decoded) => {
trace!("Resource decode: {:?}", decoded);
let cont_mapiter = decoded
.remove(0)
@@ -153,6 +157,9 @@ impl PermissionManagement {
.map(|(cont, func)| Resource::new(cont, func))
.collect()
}
+ Err(_) => Vec::new(),
+ }
+ }
}
/// Check the account contains the resource
@@ -278,6 +285,14 @@ mod tests {
);
}
+ #[test]
+ fn test_resources_from_not_exist_permission() {
+ let executor = init_executor();
+ let permission = Address::from(0x13);
+ let resources: Vec<Resource> = PermissionManagement::resources(&executor, &permission);
+ assert_eq!(resources, vec![]);
+ }
+
#[test]
fn test_load_account_permissions() {
let executor = init_executor();
