Skip to content

[pallet-revive] Add Pallet::get_storage_keys#9625

Closed
AndreiEres wants to merge 4 commits intomasterfrom
AndreiEres/revive-get-storage-keys
Closed

[pallet-revive] Add Pallet::get_storage_keys#9625
AndreiEres wants to merge 4 commits intomasterfrom
AndreiEres/revive-get-storage-keys

Conversation

@AndreiEres
Copy link
Copy Markdown
Contributor

Part of #9553
See paritytech/foundry-polkadot#274

Returns all the storage keys associated with the contract.

Integration

Should not affect downstream projects.

@AndreiEres AndreiEres added the T7-smart_contracts This PR/Issue is related to smart contracts. label Sep 2, 2025
@AndreiEres
Copy link
Copy Markdown
Contributor Author

/cmd prdoc --audience runtime_user --bump minor

///
/// Returns `Ok(Vec<Vec<u8>>)` if the contract exists, containing all storage keys
/// associated with the contract. Otherwise returns `Err(ContractAccessError)`
fn get_storage_keys(address: H160) -> Result<Vec<Vec<u8>>, ContractAccessError>;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Feels weird that both this function and the one from here https://github.com/paritytech/polkadot-sdk/pull/9617/files, needs to be added to the public APIs just for forge test,

Would it make more sense to actually have something like ReviveAPIExtension, ReviveAPITestExtension trait and have all of them there ?

Copy link
Copy Markdown
Contributor Author

@AndreiEres AndreiEres Sep 2, 2025

Choose a reason for hiding this comment

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

I think that's a good idea
Seems like the guys don't like it
#9617 (comment)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nit I would just use an Option here

Maybe we should use a BoundedVec just to be safe, so that this can not be exploited 🤔

Comment on lines +4907 to +4929
let _ = <Test as Config>::Currency::set_balance(&ALICE, 1_000_000);

let keys = Pallet::<Test>::get_storage_keys(ALICE_ADDR);
assert_err!(keys, crate::ContractAccessError::DoesntExist);

let Contract { addr, .. } =
builder::bare_instantiate(Code::Upload(code)).build_and_unwrap_contract();

let keys = Pallet::<Test>::get_storage_keys(addr).unwrap();
assert!(keys.is_empty());

let contract = AccountInfo::<Test>::load_contract(&addr).unwrap();
let child_info = contract.child_trie_info();
let storage_key = child_info.storage_key();
let key1 = b"key_1";
let key2 = b"key_2";
sp_io::default_child_storage::set(storage_key, key1, &[]);
sp_io::default_child_storage::set(storage_key, key2, &[]);

let keys = Pallet::<Test>::get_storage_keys(addr).unwrap();
assert_eq!(keys.len(), 2);
assert!(keys.contains(&key1.to_vec()));
assert!(keys.contains(&key2.to_vec()));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

this is not the correct way to write the test,

  • create a fixture contract similar to fixtures/contracts/store_call.rs where you set a bunch of keys (maybe in the deploy function)
  • then check for these keys

note thatyou will get the hashed keys in your Vec

Copy link
Copy Markdown
Contributor

@pgherveou pgherveou left a comment

Choose a reason for hiding this comment

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

This works but this will give you the hashed key

Comment on lines +1557 to +1572
let child_info = contract_info.child_trie_info();
let mut keys = Vec::new();
let mut previous_key = Vec::new();

loop {
let next_key =
sp_io::default_child_storage::next_key(child_info.storage_key(), &previous_key);

let Some(key) = next_key else {
break;
};
keys.push(key.clone());
previous_key = key;
}

Some(keys)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

a bit of a shame that there is not an Iterator for this

$crate::Pallet::<Self>::get_storage(address, key)
}

fn get_storage_keys(address: $crate::H160) -> Option<Vec<Vec<u8>>> {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

maybe call it get_storage_hashed_keys, since that's what you are getting

@alexggh
Copy link
Copy Markdown
Contributor

alexggh commented Sep 23, 2025

Not needed anymore see rationale in paritytech/foundry-polkadot#274.

@alexggh alexggh closed this Sep 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

T7-smart_contracts This PR/Issue is related to smart contracts.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants