Skip to content

Commit

Permalink
refactor(objectarium): organize code for multiple hash algo
Browse files Browse the repository at this point in the history
  • Loading branch information
ccamel committed Apr 9, 2023
1 parent e417ce2 commit 8df8fdf
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 27 deletions.
49 changes: 33 additions & 16 deletions contracts/okp4-objectarium/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub fn instantiate(
let bucket = Bucket::try_new(
info.sender,
msg.bucket,
msg.config.into(),
msg.limits.into(),
msg.pagination.try_into()?,
)?;
Expand Down Expand Up @@ -50,7 +51,7 @@ pub fn execute(

pub mod execute {
use super::*;
use crate::state::Limits;
use crate::state::BucketLimits;
use crate::ContractError::ObjectAlreadyPinned;
use cosmwasm_std::{Order, StdError, Uint128};
use std::any::type_name;
Expand All @@ -66,23 +67,23 @@ pub mod execute {
bucket.stat.size += size;
bucket.stat.object_count += Uint128::one();
match bucket.limits {
Limits {
BucketLimits {
max_object_size: Some(max),
..
} if size > max => Err(BucketError::MaxObjectSizeLimitExceeded(size, max).into()),
Limits {
BucketLimits {
max_objects: Some(max),
..
} if bucket.stat.object_count > max => {
Err(BucketError::MaxObjectsLimitExceeded(bucket.stat.object_count, max).into())
}
Limits {
BucketLimits {
max_object_pins: Some(max),
..
} if pin && max < Uint128::one() => {
Err(BucketError::MaxObjectPinsLimitExceeded(Uint128::one(), max).into())
}
Limits {
BucketLimits {
max_total_size: Some(max),
..
} if bucket.stat.size > max => {
Expand Down Expand Up @@ -152,7 +153,7 @@ pub mod execute {
let bucket = BUCKET.load(deps.storage)?;

match bucket.limits {
Limits {
BucketLimits {
max_object_pins: Some(max),
..
} if max < o.pin_count => {
Expand Down Expand Up @@ -260,6 +261,7 @@ pub mod query {

Ok(BucketResponse {
name: bucket.name,
config: bucket.config.into(),
limits: bucket.limits.into(),
pagination: bucket.pagination.into(),
})
Expand Down Expand Up @@ -356,10 +358,7 @@ pub mod query {
mod tests {
use super::*;
use crate::error::BucketError;
use crate::msg::{
BucketLimits, BucketLimitsBuilder, BucketResponse, ObjectPinsResponse, ObjectResponse,
ObjectsResponse, PageInfo, PaginationConfig, PaginationConfigBuilder,
};
use crate::msg::{BucketConfig, BucketConfigBuilder, BucketLimits, BucketLimitsBuilder, BucketResponse, HashAlgorithm, ObjectPinsResponse, ObjectResponse, ObjectsResponse, PageInfo, PaginationConfig, PaginationConfigBuilder};
use base64::{engine::general_purpose, Engine as _};
use cosmwasm_std::testing::{mock_dependencies, mock_env, mock_info};
use cosmwasm_std::StdError::NotFound;
Expand All @@ -372,6 +371,7 @@ mod tests {

let msg = InstantiateMsg {
bucket: "foo".to_string(),
config: BucketConfig::default(),
limits: BucketLimits::default(),
pagination: PaginationConfig::default(),
};
Expand Down Expand Up @@ -402,12 +402,14 @@ mod tests {

let msg = InstantiateMsg {
bucket: "bar".to_string(),
limits: BucketLimits {
max_total_size: Some(Uint128::new(20000)),
max_objects: Some(Uint128::new(10)),
max_object_size: Some(Uint128::new(2000)),
max_object_pins: Some(Uint128::new(1)),
},
config: BucketConfigBuilder::default()
.hash_algorithm(HashAlgorithm::Sha256)
.build().unwrap(),
limits: BucketLimitsBuilder::default()
.max_total_size(Uint128::new(20000))
.max_objects(Uint128::new(10))
.max_object_size(Uint128::new(2000))
.max_object_pins(Uint128::new(1)).build().unwrap(),
pagination: PaginationConfigBuilder::default()
.max_page_size(50)
.default_page_size(30)
Expand Down Expand Up @@ -435,6 +437,7 @@ mod tests {
let mut deps = mock_dependencies();
let msg = InstantiateMsg {
bucket: "bar".to_string(),
config: BucketConfig::default(),
limits: BucketLimits::default(),
pagination: PaginationConfigBuilder::default()
.max_page_size(50)
Expand Down Expand Up @@ -472,6 +475,7 @@ mod tests {
let mut deps = mock_dependencies();
let msg = InstantiateMsg {
bucket: "bar".to_string(),
config: BucketConfig::default(),
limits: BucketLimits::default(),
pagination: case.0,
};
Expand All @@ -488,6 +492,7 @@ mod tests {

let msg = InstantiateMsg {
bucket: "".to_string(),
config: BucketConfig::default(),
limits: BucketLimits::default(),
pagination: PaginationConfig::default(),
};
Expand All @@ -504,6 +509,7 @@ mod tests {

let msg = InstantiateMsg {
bucket: "foo bar".to_string(),
config: BucketConfig::default(),
limits: BucketLimits::default(),
pagination: PaginationConfig::default(),
};
Expand All @@ -527,6 +533,7 @@ mod tests {
info.clone(),
InstantiateMsg {
bucket: "test".to_string(),
config: BucketConfig::default(),
limits: BucketLimits::default(),
pagination: PaginationConfig::default(),
},
Expand Down Expand Up @@ -607,6 +614,7 @@ mod tests {
let info = mock_info("creator", &[]);
let msg = InstantiateMsg {
bucket: String::from("test"),
config: BucketConfig::default(),
limits: BucketLimits::default(),
pagination: PaginationConfig::default(),
};
Expand Down Expand Up @@ -702,6 +710,7 @@ mod tests {
let info = mock_info("creator", &[]);
let msg = InstantiateMsg {
bucket: String::from("test"),
config: BucketConfig::default(),
limits: case.0, // case.0(&mut BucketLimitsBuilder::default()).unwrap(),
pagination: PaginationConfig::default(),
};
Expand Down Expand Up @@ -729,6 +738,7 @@ mod tests {

let msg = InstantiateMsg {
bucket: String::from("test"),
config: BucketConfig::default(),
limits: BucketLimits::default(),
pagination: PaginationConfig::default(),
};
Expand Down Expand Up @@ -796,6 +806,7 @@ mod tests {

let msg = InstantiateMsg {
bucket: String::from("test"),
config: BucketConfig::default(),
limits: BucketLimits::default(),
pagination: PaginationConfig::default(),
};
Expand Down Expand Up @@ -1054,6 +1065,7 @@ mod tests {
info.clone(),
InstantiateMsg {
bucket: "test".to_string(),
config: BucketConfig::default(),
limits: BucketLimitsBuilder::default()
.max_object_pins(Uint128::new(2))
.build()
Expand Down Expand Up @@ -1296,6 +1308,7 @@ mod tests {
info.clone(),
InstantiateMsg {
bucket: "test".to_string(),
config: BucketConfig::default(),
limits: BucketLimits::default(),
pagination: PaginationConfig::default(),
},
Expand Down Expand Up @@ -1379,6 +1392,7 @@ mod tests {

let msg = InstantiateMsg {
bucket: String::from("test"),
config: BucketConfig::default(),
limits: BucketLimits::default(),
pagination: PaginationConfig::default(),
};
Expand Down Expand Up @@ -1461,6 +1475,7 @@ mod tests {

let msg = InstantiateMsg {
bucket: String::from("test"),
config: BucketConfig::default(),
limits: BucketLimits::default(),
pagination: PaginationConfig::default(),
};
Expand Down Expand Up @@ -1554,6 +1569,7 @@ mod tests {

let msg = InstantiateMsg {
bucket: String::from("test"),
config: BucketConfig::default(),
limits: BucketLimits::default(),
pagination: PaginationConfig::default(),
};
Expand Down Expand Up @@ -1672,6 +1688,7 @@ mod tests {
info.clone(),
InstantiateMsg {
bucket: "test".to_string(),
config: BucketConfig::default(),
limits: BucketLimits::default(),
pagination: PaginationConfig::default(),
},
Expand Down
42 changes: 42 additions & 0 deletions contracts/okp4-objectarium/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ pub struct InstantiateMsg {
/// The name could not be empty or contains whitespaces.
/// If name contains whitespace, they will be removed.
pub bucket: String,
/// The configuration of the bucket.
pub config: BucketConfig,
/// The limits of the bucket.
pub limits: BucketLimits,
/// The configuration for paginated query.
Expand Down Expand Up @@ -118,12 +120,52 @@ pub struct PageInfo {
pub struct BucketResponse {
/// The name of the bucket.
pub name: String,
/// The configuration of the bucket.
pub config: BucketConfig,
/// The limits of the bucket.
pub limits: BucketLimits,
/// The configuration for paginated query.
pub pagination: PaginationConfig,
}

/// HashAlgorithm is an enumeration that defines the different hash algorithms
/// supported for hashing the content of objects.
#[cw_serde]
#[derive(Copy)]
pub enum HashAlgorithm {
/// Represents the SHA-256 algorithm.
Sha256,
/// Represents the SHA-512 algorithm.
Sha512,
}

impl Default for HashAlgorithm {
fn default() -> Self {
HashAlgorithm::Sha256
}
}

/// BucketConfig is the type of the configuration of a bucket.
///
/// The configuration is set at the instantiation of the bucket, and is immutable and cannot be changed.
/// The configuration is optional and if not set, the default configuration is used.
#[cw_serde]
#[derive(Default, Builder)]
#[builder(default, setter(into, strip_option))]
pub struct BucketConfig {
/// The algorithm used to hash the content of the objects to generate the id of the objects.
/// The algorithm is optional and if not set, the default algorithm is used.
///
/// The default algorithm is Sha256 .
pub hash_algorithm: Option<HashAlgorithm>,
}

impl BucketConfig {
pub fn hash_algorithm_or_default(&self) -> HashAlgorithm {
self.hash_algorithm.as_ref().copied().unwrap_or_default()
}
}

/// BucketLimits is the type of the limits of a bucket.
///
/// The limits are optional and if not set, there is no limit.
Expand Down
Loading

0 comments on commit 8df8fdf

Please sign in to comment.