Skip to content

Commit

Permalink
feat(storage): return error when storing already stored object
Browse files Browse the repository at this point in the history
  • Loading branch information
amimart committed Mar 3, 2023
1 parent 803e1b3 commit da89b6f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 16 deletions.
38 changes: 23 additions & 15 deletions contracts/cw-storage/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@ pub mod execute {

let data_path = DATA.key(object.id.clone());
if data_path.has(deps.storage) {
// TODO: maybe throw an error if the owner is different? Or if object already exists?
return Ok(res);
return Err(ContractError::Bucket(BucketError::ObjectAlreadyStored));
}

data_path.save(deps.storage, &data.0)?;
Expand Down Expand Up @@ -316,19 +315,6 @@ mod tests {
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(),
pin: obj1.1,
};
let res = execute(deps.as_mut(), mock_env(), info, msg).unwrap();
assert_eq!(
res.attributes,
vec![
Attribute::new("action", "store_object"),
Attribute::new("id", obj1.2),
]
);
assert_eq!(
objects()
.keys_raw(&deps.storage, None, None, Order::Ascending)
Expand All @@ -343,6 +329,28 @@ mod tests {
);
}

#[test]
fn store_object_already_stored() {
let mut deps = mock_dependencies();
let info = mock_info("creator", &[]);
let msg = InstantiateMsg {
bucket: String::from("test"),
limits: BucketLimits::new(),
};
instantiate(deps.as_mut(), mock_env(), info.clone(), msg).unwrap();

let object = general_purpose::STANDARD.encode("already existing object");
let msg = ExecuteMsg::StoreObject {
data: Binary::from_base64(object.as_str()).unwrap(),
pin: true,
};
execute(deps.as_mut(), mock_env(), info.clone(), msg.clone()).unwrap();
assert_eq!(
execute(deps.as_mut(), mock_env(), info, msg).err(),
Some(ContractError::Bucket(BucketError::ObjectAlreadyStored)),
);
}

#[test]
fn store_object_limits() {
let cases = vec![
Expand Down
3 changes: 3 additions & 0 deletions contracts/cw-storage/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,7 @@ pub enum BucketError {

#[error("Maximum object pins number exceeded")]
MaxObjectPinsLimitExceeded,

#[error("Object is already stored")]
ObjectAlreadyStored,
}
2 changes: 1 addition & 1 deletion contracts/cw-storage/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub enum ExecuteMsg {
/// # StoreObject
/// StoreObject store an object to the bucket and make the sender the owner of the object.
/// The object is referenced by the hash of its content and this value is returned.
/// If the object is already stored, this is a no-op.
/// If the object is already stored, an error is returned.
/// If pin is true, the object is pinned for the sender.
StoreObject { data: Binary, pin: bool },

Expand Down

0 comments on commit da89b6f

Please sign in to comment.