Skip to content
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

Use single byte top keys for storage [DON'T MERGE; BREAKING] #268

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion contracts/nois-gateway/src/state/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -21,4 +23,4 @@ pub struct Config {
pub sink: Addr,
}

pub const CONFIG: Item<Config> = Item::new("config");
pub const CONFIG: Item<Config> = Item::new(TopKey::Config.as_str());
4 changes: 3 additions & 1 deletion contracts/nois-gateway/src/state/customers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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());
6 changes: 4 additions & 2 deletions contracts/nois-gateway/src/state/drand_jobs/drand_jobs2.rs
Original file line number Diff line number Diff line change
@@ -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<u32, u16> = Map::new("djids");
const JOBS: Map<(u32, u16), Job> = Map::new(TopKey::Jobs.as_str());
const LAST_JOB_ID: Map<u32, u16> = Map::new(TopKey::JobsLastId.as_str());

/// Add an element to the unprocessed drand jobs queue of this round
pub fn unprocessed_drand_jobs_enqueue(
Expand Down
21 changes: 21 additions & 0 deletions contracts/nois-gateway/src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
}
}
}
5 changes: 4 additions & 1 deletion contracts/nois-gateway/src/state/stats.rs
Original file line number Diff line number Diff line change
@@ -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<u64, u32> = Map::new("drand_jobs_pc");
const PROCESSED_DRAND_JOBS_COUNT: Map<u64, u32> =
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<u32> {
Expand Down