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

Remove v1 drand jobs storage layout #280

Merged
merged 3 commits into from
Sep 1, 2023
Merged
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: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ and this project adheres to
- nois-drand: Rounds that are not divisible by 10 are not considered invalid
anymore. The incentivisation however requires `round%10==0` as before and jobs
only use those. This allows bots to submit other rounds for no reason.
- gateway: Remove `ExecuteMsg::Cleanup` to cleanup outdated jobs state ([#280])
- gateway: Remove code for accessing drand jobs storage layout v1 ([#280])

[#280]: https://github.com/noislabs/nois-contracts/pull/280

### Fixed

Expand Down
41 changes: 1 addition & 40 deletions contracts/nois-gateway/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use crate::request_router::{NewDrand, RequestRouter, RoutingReceipt};
use crate::state::{
all_unprocessed_drand_jobs, get_processed_drand_jobs, requests_log_add, requests_log_asc,
requests_log_desc, unprocessed_drand_jobs_len, Config, Customer, RequestLogEntry, CONFIG,
CUSTOMERS, DRAND_JOBS_V1_END, DRAND_JOBS_V1_START,
CUSTOMERS,
};

#[entry_point]
Expand Down Expand Up @@ -93,7 +93,6 @@ pub fn execute(
drand_addr,
payment_initial_funds,
),
ExecuteMsg::Cleanup { limit } => execute_cleanup(deps, env, info, limit),
}
}

Expand Down Expand Up @@ -506,44 +505,6 @@ fn execute_add_verified_round(
.add_attributes(attributes))
}

fn execute_cleanup(
deps: DepsMut,
_env: Env,
_info: MessageInfo,
limit: Option<u32>,
) -> Result<Response, ContractError> {
// the number of elements we can still take (decreasing over time)
let mut limit = limit.unwrap_or(20) as usize;

let mut deleted = 0;
const PER_SCAN: usize = 20;
loop {
let take_this_scan = std::cmp::min(PER_SCAN, limit);
let keys: Vec<_> = deps
.storage
.range_keys(
Some(DRAND_JOBS_V1_START),
Some(DRAND_JOBS_V1_END),
Order::Ascending,
)
.take(take_this_scan)
.collect();
let deleted_this_scan = keys.len();
for k in keys {
deps.storage.remove(&k);
}
deleted += deleted_this_scan;
limit -= deleted_this_scan;
if limit == 0 || deleted_this_scan < take_this_scan {
break;
}
}

Ok(Response::new()
.add_attribute("action", "execute_cleanup")
.add_attribute("deleted_entries", deleted.to_string()))
}

/// In order not to fall in the chicken egg problem where you need
/// to instantiate two or more contracts that need to be aware of each other
/// in a context where the contract addresses generration is not known
Expand Down
7 changes: 0 additions & 7 deletions contracts/nois-gateway/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,6 @@ pub enum ExecuteMsg {
/// To deactivate it later on, send Some(Coin::new(0, "unois")) here.
payment_initial_funds: Option<Coin>,
},
/// Cleans up the given number of elements of obsolete data.
/// Call this multiple times to increamentally clean up state.
Cleanup {
/// The number of state elements to delete.
/// Using None will fall back to a default value that is subject to change (currently 20).
limit: Option<u32>,
},
}

#[cw_serde]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
use cosmwasm_std::{Order, StdResult, Storage};
use cosmwasm_schema::cw_serde;
use cosmwasm_std::{Binary, Order, StdResult, Storage};
use cw_storage_plus::Map;

use super::Job;
#[cw_serde]
pub struct Job {
/// A RNG specific randomness source identifier, e.g. `drand:<network id>:<round>`
pub source_id: String,
// The channel the job came from and we have to send the response to
pub channel: String,
pub origin: Binary,
}

/// 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");
Expand Down Expand Up @@ -64,10 +72,82 @@ pub fn all_unprocessed_drand_jobs(

#[cfg(test)]
mod tests {
use cosmwasm_std::{testing::MockStorage, Binary};
use cosmwasm_std::{testing::MockStorage, Binary, Order};

use super::*;

fn make_job(id: u32) -> Job {
Job {
channel: "chan-123".to_string(),
source_id: "drannd:foo:bar".to_string(),
origin: Binary::from(id.to_be_bytes()),
}
}

#[test]
fn unprocessed_drand_jobs_dequeue_works() {
let mut storage = MockStorage::default();

let round = 3;

let job = unprocessed_drand_jobs_dequeue(&mut storage, round).unwrap();
assert_eq!(job, None);

let job1 = make_job(1);
let job2 = make_job(2);
let job3 = make_job(3);
let job4 = make_job(4);

unprocessed_drand_jobs_enqueue(&mut storage, round, &job1).unwrap();
unprocessed_drand_jobs_enqueue(&mut storage, round, &job2).unwrap();
unprocessed_drand_jobs_enqueue(&mut storage, round, &job3).unwrap();
unprocessed_drand_jobs_enqueue(&mut storage, round, &job4).unwrap();

let job = unprocessed_drand_jobs_dequeue(&mut storage, round).unwrap();
assert_eq!(job, Some(job1));
let job = unprocessed_drand_jobs_dequeue(&mut storage, round).unwrap();
assert_eq!(job, Some(job2));
let job = unprocessed_drand_jobs_dequeue(&mut storage, round).unwrap();
assert_eq!(job, Some(job3));
let job = unprocessed_drand_jobs_dequeue(&mut storage, round).unwrap();
assert_eq!(job, Some(job4));
let job = unprocessed_drand_jobs_dequeue(&mut storage, round).unwrap();
assert_eq!(job, None);
}

#[test]
fn unprocessed_drand_jobs_len_works() {
let mut storage = MockStorage::default();

let round = 3;

let job1 = make_job(1);
let job2 = make_job(2);
let job3 = make_job(3);
let job4 = make_job(4);

assert_eq!(unprocessed_drand_jobs_len(&storage, round).unwrap(), 0);
unprocessed_drand_jobs_enqueue(&mut storage, round, &job1).unwrap();
assert_eq!(unprocessed_drand_jobs_len(&storage, round).unwrap(), 1);
unprocessed_drand_jobs_enqueue(&mut storage, round, &job2).unwrap();
assert_eq!(unprocessed_drand_jobs_len(&storage, round).unwrap(), 2);
unprocessed_drand_jobs_enqueue(&mut storage, round, &job3).unwrap();
assert_eq!(unprocessed_drand_jobs_len(&storage, round).unwrap(), 3);
unprocessed_drand_jobs_enqueue(&mut storage, round, &job4).unwrap();
assert_eq!(unprocessed_drand_jobs_len(&storage, round).unwrap(), 4);

let _ = unprocessed_drand_jobs_dequeue(&mut storage, round).unwrap();
assert_eq!(unprocessed_drand_jobs_len(&storage, round).unwrap(), 3);
let _ = unprocessed_drand_jobs_dequeue(&mut storage, round).unwrap();
assert_eq!(unprocessed_drand_jobs_len(&storage, round).unwrap(), 2);
let _ = unprocessed_drand_jobs_dequeue(&mut storage, round).unwrap();
assert_eq!(unprocessed_drand_jobs_len(&storage, round).unwrap(), 1);
let _ = unprocessed_drand_jobs_dequeue(&mut storage, round).unwrap();
assert_eq!(unprocessed_drand_jobs_len(&storage, round).unwrap(), 0);
let _ = unprocessed_drand_jobs_dequeue(&mut storage, round).unwrap();
assert_eq!(unprocessed_drand_jobs_len(&storage, round).unwrap(), 0);
}

#[test]
fn all_unprocessed_drand_jobs_works() {
let mut storage = MockStorage::default();
Expand Down
160 changes: 0 additions & 160 deletions contracts/nois-gateway/src/state/drand_jobs/drand_jobs1.rs

This file was deleted.

Loading