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

State cleanup contract #171

Merged
merged 2 commits into from
Feb 9, 2022
Merged

State cleanup contract #171

merged 2 commits into from
Feb 9, 2022

Conversation

ilblackdragon
Copy link
Member

Allows to cleanup state of an account without deleting it.
Deploy this contract instead of the current one, clean up state and can redeploy new contract or use account in other ways.

}

#[no_mangle]
pub extern "C" fn clean() {
Copy link
Contributor

Choose a reason for hiding this comment

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

There is no guarantee that this can finish within gas limit?

Copy link

Choose a reason for hiding this comment

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

I think there is no guarantee here, it is necessary to first calculate the total size of the deleted data before sending the array of keys

Copy link
Contributor

@austinabell austinabell left a comment

Choose a reason for hiding this comment

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

This might be a good candidate to be a no_std contract. I'll port it over as an alternative when I have free time

let input = env::input().unwrap();
let args: Args = serde_json::from_slice(&input).unwrap();
for key in args.keys.iter() {
env::storage_remove(&base64::decode(key).unwrap());
Copy link
Contributor

Choose a reason for hiding this comment

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

Optional, but can use the Base64VecU8 type instead of String to just handle decoding this during deserialization

Comment on lines +6 to +8
#[allow(dead_code)]
#[near_bindgen]
struct Contract {}
Copy link
Contributor

Choose a reason for hiding this comment

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

Should just be able to delete this? Or am I forgetting about some dependency from v3 or otherwise?

Copy link

@ilyar ilyar Sep 30, 2021

Choose a reason for hiding this comment

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

agree i ran this code on version 3.1.0 and 4.0.0-pre.2

use near_sdk::serde::Deserialize;
use near_sdk::serde_json;
use near_sdk::env;
use near_sdk::json_types::Base64VecU8;

#[derive(Deserialize)]
#[serde(crate = "near_sdk::serde")]
struct Args {
    pub keys: Vec<Base64VecU8>,
}

#[no_mangle]
pub extern "C" fn clean() {
    env::setup_panic_hook();
    let input = env::input().unwrap();
    let args: Args = serde_json::from_slice(&input).unwrap();
    for key in args.keys.iter() {
        env::storage_remove(&key.0);
    }
}

Copy link
Contributor

Choose a reason for hiding this comment

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

FYI, I have no idea why that might be necessary

Copy link

Choose a reason for hiding this comment

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

it helped me to solve the error:

The collection is an inconsistent state. Did previous smart contract execution terminate unexpectedly?

to be more precise it helps to better understand how storage works.
I left a note for myself, it may be useful not only for me https://gist.github.com/ilyar/19bdc04d1aa09ae0fc84eb4297df1a1d#file-clear_state_of_contract_on_near_protocol-md

@MaximusHaximus
Copy link

Howdy folks,
Is there anything pending blocking this contract from being merged and becoming an official core contract? We would like to use it to ensure that when we disable a multisig contract used for 2FA by the NEAR wallet, we first clear any on-chain state that the contract is maintaining. Ref: near/near-api-js#736

@MaximusHaximus
Copy link

Howdy! Just checking in -- is there any reason why this contract can't be used at this point? Can we get this PR merged?

@esaminu
Copy link

esaminu commented Feb 4, 2022

Hello all, we have another report of a user being affected by uncleared state after deleting the multisig contract. Can this be used in the wallet as per @MaximusHaximus's suggestion?

@agileurbanite
Copy link

@austinabell @ilyar @bowenwang1996 @frol any updates on whether the the status of this PR? @frol has already approved but seeing feedback from @austinabell wondering if these are nits that can be addressed in a separate PR or if these are blockers. Thanks!

@austinabell
Copy link
Contributor

but seeing feedback from @austinabell wondering if these are nits that can be addressed in a separate PR or if these are blockers. Thanks!

No blockers! Those are nits

@jberrytech
Copy link

If we're feeling good about this, @austinabell, could you execute the merge?

@austinabell austinabell merged commit 144adb1 into master Feb 9, 2022
@austinabell austinabell deleted the cleanup-contract branch February 9, 2022 22:35
@austinabell
Copy link
Contributor

Opened #203 for how to optimize this in the future

@d3v3us
Copy link

d3v3us commented May 21, 2022

How to get all the keys for contract with large state, it gives an error
Server error: State of contract contract.near is too large to be viewed

@wackazong
Copy link

For other people landing here: you can prevent the error State of contract _____.near is too large to be viewed when calling view-state via the JSON RPC API if you select an alternative RPC provider from this list (also works for testnet if you replace testnet with mainnet in some of the URLs): docs.near.org/api/rpc/providers I was successful with OOMNIA: endpoints.omniatech.io/v1/near/testnet/public

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.