-
Notifications
You must be signed in to change notification settings - Fork 691
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
Allow iteration over contract storage #119
Comments
Hey @athei, I found if I use mapping, then I cannot use #[ink(storage)]
#[derive(SpreadAllocate, ::scale_info::TypeInfo)]
pub struct DProtocalStack {
/// Stores a single `bool` value on the storage.
value: bool,
account: AccountId,
sim_routers: ink_prelude::vec::Vec<SimNode>,
msg_2_verify: ink_storage::Mapping<ink_prelude::string::String, ink_storage::Mapping<u128, RecvedMessage>>,
} and the #[derive(SpreadLayout, PackedLayout, SpreadAllocate, PackedAllocate, Debug, PartialEq, Eq, scale::Encode, scale::Decode)]
#[cfg_attr(feature = "std", derive(::scale_info::TypeInfo, StorageLayout))]
pub struct SimNode(u16, u32); The error is: I need to iterate So currently, is the only way we can implement things like this is that putting |
Yes. You need to manually keep track of keys you are interested in. Please keep in mind that even with this feature iteration is almost never the right solution for a contract. It is basically unbounded and most probably not what you want. |
Thanks a lot. I get it~ |
If you are using a tuple as a key how do recommend keeping track of all the keys of each tuple? |
Moving into blocked because I am not sure if this is the best way forward or how to design this API. Right now we just have Going forward we want to implement a iterable @xermicus We should design this API along the new |
As a user of the
A note about using a It's also worth noting that the more I would advise against using a linked-list like structure on top of the keys in a tldr; it's almost always best to use a |
Hey @athei , according to the information from @goastler, I found the BTreeMap seems to satisfy everything, which can be a member of the contract itself to store states, easily lookup, get all the keys immediately, and iterate. So what are the drawbacks of the Here's my example code |
That the whole |
|
@goastler For key lookup (2) why don't you use |
The contracts pallet does not allow contracts to iterate over storage. The reason for that is that we expected contracts to store their own metadata in order to be able to iterate. The solution of ink! was to come up with elaborate high level data structures that allowed such iteration without any support from pallet-contracts. However, it turned out that those data structure are complex in terms of code size and computation. The solution to big contract sizes was to come up with a simple
Mapping
data structure that is just a thin wrapper around functionality provided by pallet-contracts. In order to have iteration using this data structure we need some new functionality:API
We just need some API to get the next lexicographic key:
RPC
No RPCs needed. Clients can just query the child trie id of a contract and then call:
Currently, we have
contracts_getStorage
which is redundant. It can just be replaced bychildState_getStorage
.Requirements
We need paritytech/substrate#11029 to make this functionality useful. Otherwise querying the keys wouldn't give us access to the pre image of the key which is what we are interested in most cases.
The text was updated successfully, but these errors were encountered: