diff --git a/contracts/nois-gateway/src/state/config.rs b/contracts/nois-gateway/src/state/config.rs index 4a93e85f..89fb9ca3 100644 --- a/contracts/nois-gateway/src/state/config.rs +++ b/contracts/nois-gateway/src/state/config.rs @@ -2,6 +2,8 @@ use cosmwasm_schema::cw_serde; use cosmwasm_std::{Addr, Coin}; use cw_storage_plus::Item; +use super::TopKey; + #[cw_serde] pub struct Config { /// The address of the drand contract. @@ -21,4 +23,4 @@ pub struct Config { pub sink: Addr, } -pub const CONFIG: Item = Item::new("config"); +pub const CONFIG: Item = Item::new(TopKey::Config.as_str()); diff --git a/contracts/nois-gateway/src/state/customers.rs b/contracts/nois-gateway/src/state/customers.rs index 74816c15..9f8f4f1a 100644 --- a/contracts/nois-gateway/src/state/customers.rs +++ b/contracts/nois-gateway/src/state/customers.rs @@ -2,6 +2,8 @@ use cosmwasm_schema::cw_serde; use cosmwasm_std::Addr; use cw_storage_plus::Map; +use super::TopKey; + #[cw_serde] pub struct Customer { /// The payment contract address @@ -11,4 +13,4 @@ pub struct Customer { } /// A map from channel ID to customer information -pub const CUSTOMERS: Map<&str, Customer> = Map::new("customers"); +pub const CUSTOMERS: Map<&str, Customer> = Map::new(TopKey::Customers.as_str()); diff --git a/contracts/nois-gateway/src/state/drand_jobs/drand_jobs2.rs b/contracts/nois-gateway/src/state/drand_jobs/drand_jobs2.rs index 7514a794..cfcb4fda 100644 --- a/contracts/nois-gateway/src/state/drand_jobs/drand_jobs2.rs +++ b/contracts/nois-gateway/src/state/drand_jobs/drand_jobs2.rs @@ -1,11 +1,13 @@ use cosmwasm_std::{Order, StdResult, Storage}; use cw_storage_plus::Map; +use crate::state::TopKey; + use super::Job; /// A map from (round, job ID) here job ID is a round specific auto incrementing ID -const JOBS: Map<(u32, u16), Job> = Map::new("djobs"); -const LAST_JOB_ID: Map = Map::new("djids"); +const JOBS: Map<(u32, u16), Job> = Map::new(TopKey::Jobs.as_str()); +const LAST_JOB_ID: Map = Map::new(TopKey::JobsLastId.as_str()); /// Add an element to the unprocessed drand jobs queue of this round pub fn unprocessed_drand_jobs_enqueue( diff --git a/contracts/nois-gateway/src/state/mod.rs b/contracts/nois-gateway/src/state/mod.rs index d7e3b29f..d11a6eed 100644 --- a/contracts/nois-gateway/src/state/mod.rs +++ b/contracts/nois-gateway/src/state/mod.rs @@ -12,3 +12,24 @@ pub use drand_jobs::{ }; pub use requests_log::{requests_log_add, requests_log_asc, requests_log_desc, RequestLogEntry}; pub use stats::{get_processed_drand_jobs, increment_processed_drand_jobs}; + +/// Top level storage key. Values must not conflict. +/// Each key is only one byte long to ensure we use the smallest possible storage keys. +#[repr(u8)] +pub enum TopKey { + Config = b'c', + Customers = b'C', + Jobs = b'j', + JobsLastId = b'J', + ProcessedDrandJobsCount = b'p', +} + +impl TopKey { + const fn as_str(&self) -> &str { + let array_ref = unsafe { std::mem::transmute::<_, &[u8; 1]>(self) }; + match core::str::from_utf8(array_ref) { + Ok(a) => a, + Err(_) => panic!("Non-utf8 enum value found. Use a-z, A-Z and 0-9"), + } + } +} diff --git a/contracts/nois-gateway/src/state/stats.rs b/contracts/nois-gateway/src/state/stats.rs index 4c9410d8..08c2b641 100644 --- a/contracts/nois-gateway/src/state/stats.rs +++ b/contracts/nois-gateway/src/state/stats.rs @@ -1,9 +1,12 @@ use cosmwasm_std::{StdResult, Storage}; use cw_storage_plus::Map; +use super::TopKey; + /// A map from drand rounds to number of jobs. /// "pc" is short for processed count. -const PROCESSED_DRAND_JOBS_COUNT: Map = Map::new("drand_jobs_pc"); +const PROCESSED_DRAND_JOBS_COUNT: Map = + Map::new(TopKey::ProcessedDrandJobsCount.as_str()); /// Add an element to the processed drand jobs queue of this round pub fn get_processed_drand_jobs(storage: &dyn Storage, round: u64) -> StdResult {