Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix remove contract #1739

Merged
merged 3 commits into from
Jan 17, 2022
Merged

fix remove contract #1739

merged 3 commits into from
Jan 17, 2022

Conversation

ermalkaleci
Copy link
Contributor

@ermalkaleci ermalkaleci commented Dec 30, 2021

current impl is not removing contract_info

xlc
xlc previously approved these changes Dec 30, 2021
modules/evm/src/lib.rs Outdated Show resolved Hide resolved
Copy link
Member

@zjb0807 zjb0807 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Acala/modules/evm/src/lib.rs

Lines 980 to 1011 in e373273

/// Removes an account from Accounts and AccountStorages.
pub fn remove_account(address: &EvmAddress) -> DispatchResult {
// Deref code, and remove it if ref count is zero.
Accounts::<T>::mutate_exists(&address, |maybe_account| {
if let Some(account) = maybe_account {
if let Some(contract_info) = account.contract_info.clone() {
CodeInfos::<T>::mutate_exists(&contract_info.code_hash, |maybe_code_info| {
if let Some(code_info) = maybe_code_info.as_mut() {
code_info.ref_count = code_info.ref_count.saturating_sub(1);
if code_info.ref_count == 0 {
Codes::<T>::remove(&contract_info.code_hash);
account.contract_info = None;
*maybe_code_info = None;
}
}
});
}
}
});
if let Some(AccountInfo {
contract_info: Some(_), ..
}) = Accounts::<T>::take(address)
{
// remove_account can only be called when account is killed. i.e. providers == 0
// but contract_info should maintain a provider
// so this should never happen
debug_assert!(false);
}
Ok(())
}

remove line L1000-L1008 and add debug_assert!(false); in mutate_exists.

modules/evm/src/tests.rs Outdated Show resolved Hide resolved
@ermalkaleci
Copy link
Contributor Author

Acala/modules/evm/src/lib.rs

Lines 980 to 1011 in e373273

/// Removes an account from Accounts and AccountStorages.
pub fn remove_account(address: &EvmAddress) -> DispatchResult {
// Deref code, and remove it if ref count is zero.
Accounts::<T>::mutate_exists(&address, |maybe_account| {
if let Some(account) = maybe_account {
if let Some(contract_info) = account.contract_info.clone() {
CodeInfos::<T>::mutate_exists(&contract_info.code_hash, |maybe_code_info| {
if let Some(code_info) = maybe_code_info.as_mut() {
code_info.ref_count = code_info.ref_count.saturating_sub(1);
if code_info.ref_count == 0 {
Codes::<T>::remove(&contract_info.code_hash);
account.contract_info = None;
*maybe_code_info = None;
}
}
});
}
}
});
if let Some(AccountInfo {
contract_info: Some(_), ..
}) = Accounts::<T>::take(address)
{
// remove_account can only be called when account is killed. i.e. providers == 0
// but contract_info should maintain a provider
// so this should never happen
debug_assert!(false);
}
Ok(())
}

remove line L1000-L1008 and add debug_assert!(false); in mutate_exists.

this will assert if remove_account is called while contract ref > 1

@ermalkaleci ermalkaleci requested review from xlc, zjb0807 and shaunxw January 7, 2022 10:09
@zjb0807
Copy link
Member

zjb0807 commented Jan 10, 2022

this will assert if remove_account is called while contract ref > 1

Accounts::<T>::mutate_exists(&address, |maybe_account| {
    if let Some(account) = maybe_account {
        if let Some(ContractInfo { code_hash, .. }) = account.contract_info {
            CodeInfos::<T>::mutate_exists(&code_hash, |maybe_code_info| {
                if let Some(code_info) = maybe_code_info {
                    code_info.ref_count = code_info.ref_count.saturating_sub(1);
                    if code_info.ref_count == 0 {
                        Codes::<T>::remove(&code_hash);
                        *maybe_code_info = None;
                    }
                }
            });

            // remove_account can only be called when account is killed. i.e. providers == 0
            // but contract_info should maintain a provider
            // so this should never happen
            debug_assert!(false, "removed account while is still linked to contract info");
        } else {
            maybe_account = None;
	}
});

debug_assert is only enabled in non-optimized builds by default. I think this is better.

@xlc xlc merged commit 2a49dbc into master Jan 17, 2022
@xlc xlc deleted the fix-remove-contract branch January 17, 2022 04:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants