How would I go about measuring the size of the state that is being used for storage of the current contract? I have the idea to iterate through the state tree using the host functions, but what is the root prefix of the current contract? Is it 0?
You can get the entire state of the contract using GetInstanceState
GRPC V2 endpoint.
See https://github.com/Concordium/concordium-rust-sdk/blob/main/examples/v2_get_instance_state.rs for an example, which also outputs the size of the state.
The state root is currently at the empty key &[]
Is there a way to get the state size on-chain in a smart contract? I would need this for testing. Does not have to be efficient. But would make testing much easier to have the information on-chain.
We have an example of how to do this using the Rust SDK
I saw that but AFAICS this relies on get_instance_state
of the client to get the state as a byte stream. How can I get the same byte stream inside the contract? Can I use lookup_entry
and just get the size of the root entry at the empty key &[]
or do I need to do some form of tree-walking?
You would need to traverse the entire tree.
If you just get a value at the root then you’ll just get the size of the top-level node. Which typically is not the entire state (if you use StateMap or StateSet or Box).
Ist that a straightforward thing to do? I would rather have the size in the smart contract if that is not too much work. As I said, gas fees/performance is not important. Could you provide me with some pointers on how to do that? Thanks!
There is a fair amount of abstraction involved in concordium-std
so it’s not simple to get this size inside the contract.
We don’t really have any helper functions for that in concordium-std, and with the way that this is currently designed it would be a lot of effort to add I think.
Ok, understood. Thanks.