Skip to content

Commit

Permalink
fix(storage): use Uint128 instead of primitive in state
Browse files Browse the repository at this point in the history
  • Loading branch information
amimart committed Mar 3, 2023
1 parent 11267fa commit 803e1b3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
23 changes: 12 additions & 11 deletions contracts/cw-storage/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,38 +47,37 @@ pub fn execute(
pub mod execute {
use super::*;
use crate::state::Limits;
use cosmwasm_std::Uint128;

pub fn store_object(
deps: DepsMut,
info: MessageInfo,
data: Binary,
pin: bool,
) -> Result<Response, ContractError> {
let size = data.len() as u128;
let size = (data.len() as u128).into();
BUCKET.update(deps.storage, |mut bucket| -> Result<_, ContractError> {
bucket.size += size;
bucket.object_count += 1;
bucket.object_count += Uint128::one();
match bucket.limits {
Limits {
max_object_size: Some(max),
..
} if size > max.u128() => Err(BucketError::MaxObjectSizeLimitExceeded.into()),
} if size > max => Err(BucketError::MaxObjectSizeLimitExceeded.into()),
Limits {
max_objects: Some(max),
..
} if bucket.object_count > max.u128() => {
Err(BucketError::MaxObjectsLimitExceeded.into())
}
} if bucket.object_count > max => Err(BucketError::MaxObjectsLimitExceeded.into()),
Limits {
max_object_pins: Some(max),
..
} if pin && max.u128() < 1u128 => {
} if pin && max < Uint128::one() => {
Err(BucketError::MaxObjectPinsLimitExceeded.into())
}
Limits {
max_total_size: Some(max),
..
} if bucket.size > max.u128() => Err(BucketError::MaxTotalSizeLimitExceeded.into()),
} if bucket.size > max => Err(BucketError::MaxTotalSizeLimitExceeded.into()),
_ => Ok(bucket),
}
})?;
Expand Down Expand Up @@ -144,7 +143,7 @@ pub mod query {
.load(deps.storage, id)
.map(|object| ObjectResponse {
id: object.id.clone(),
size: object.size.into(),
size: object.size,
owner: object.owner.into(),
is_pinned: pins()
.idx
Expand Down Expand Up @@ -306,15 +305,17 @@ mod tests {
let created = objects().load(&deps.storage, String::from(obj.2)).unwrap();
assert_eq!(created.id, obj.2);
assert_eq!(created.owner, info.clone().sender);
assert_eq!(created.size, obj.3);
assert_eq!(created.size.u128(), obj.3);

assert_eq!(
pins().has(&deps.storage, (String::from(obj.2), info.clone().sender)),
obj.1,
);
}

assert_eq!(BUCKET.load(&deps.storage).unwrap().size, obj1.3 + obj2.3);
let bucket = BUCKET.load(&deps.storage).unwrap();
assert_eq!(bucket.size.u128(), obj1.3 + obj2.3);
assert_eq!(bucket.object_count.u128(), 2);

let msg = ExecuteMsg::StoreObject {
data: Binary::from_base64(obj1.0.as_str()).unwrap(),
Expand Down
10 changes: 5 additions & 5 deletions contracts/cw-storage/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ pub struct Bucket {
/// The limits of the bucket.
pub limits: Limits,
/// The total size of the objects contained in the bucket.
pub size: u128,
pub size: Uint128,
/// The number of objects in the bucket.
pub object_count: u128,
pub object_count: Uint128,
}

impl Bucket {
Expand All @@ -30,8 +30,8 @@ impl Bucket {
Ok(Self {
name: n,
limits,
size: 0u128,
object_count: 0u128,
size: Uint128::zero(),
object_count: Uint128::zero(),
})
}
}
Expand Down Expand Up @@ -71,7 +71,7 @@ pub struct Object {
/// The owner of the object.
pub owner: Addr,
/// The size of the object.
pub size: u128,
pub size: Uint128,
}

pub struct ObjectIndexes<'a> {
Expand Down

0 comments on commit 803e1b3

Please sign in to comment.