CosmWasm / cosmwasm

Framework for building smart contracts in Wasm for the Cosmos SDK
https://www.cosmwasm.com/
Apache License 2.0
1.06k stars 330 forks source link

What is the maximum length of `vec` in `cosmwasm`? #2226

Closed 99Kies closed 1 month ago

99Kies commented 1 month ago

What is the maximum length of vec in cosmwasm? I might set up a maximum of 15625 whitelists, but I'm concerned that this would exceed the vec length limit. Also the max block limit for our chain is 22MB.

this is my code:

#[cw_serde]
pub struct WhitelistConfig {
    pub addr: String,
}

#[cw_serde]
pub struct Whitelist {
    pub users: Vec<WhitelistConfig>,
}

impl Whitelist {
    pub fn is_whitelist(&self, addr: impl AsRef<str>) -> bool {
        let addr = addr.as_ref();
        self.users.iter().any(|a| a.addr == addr)
    }
}
chipshort commented 1 month ago

There is no explicit limit for vec length, but the limit you will most likely run into is the length limit for storage values.

So you need to make sure that when you serialize your value and save it in storage, the serialized value is smaller than that limit. I would recommand to keep a little buffer in-between, in case we ever need to lower the limit in the future.

Also keep in mind that reading and writing storage takes more gas depending on the size of the value.