diff --git a/contracts/okp4-objectarium/src/contract.rs b/contracts/okp4-objectarium/src/contract.rs index 9fb81ae4b..18944a83f 100644 --- a/contracts/okp4-objectarium/src/contract.rs +++ b/contracts/okp4-objectarium/src/contract.rs @@ -61,8 +61,7 @@ pub mod execute { use crate::msg; use crate::state::BucketLimits; use crate::ContractError::ObjectPinned; - use cosmwasm_std::{Order, StdError, Uint128}; - use std::any::type_name; + use cosmwasm_std::{Order, Uint128}; pub fn store_object( deps: DepsMut<'_>, @@ -118,6 +117,7 @@ pub mod execute { // store object data let id = crypto::hash(&bucket.config.hash_algorithm.into(), &data.0); let data_path = DATA.key(id.clone()); + if data_path.has(deps.storage) { return Err(ContractError::Bucket(BucketError::ObjectAlreadyStored)); } @@ -416,7 +416,6 @@ impl From for crypto::HashAlgorithm { #[cfg(test)] mod tests { use super::*; - use crate::crypto::Hash; use crate::error::BucketError; use crate::msg::{ BucketConfig, BucketLimitsBuilder, BucketResponse, CompressionAlgorithm, HashAlgorithm, @@ -428,6 +427,24 @@ mod tests { use cosmwasm_std::{from_binary, Attribute, Order, StdError, Uint128}; use std::any::type_name; + fn decode_hex(hex: &str) -> Vec { + base16ct::lower::decode_vec(hex).unwrap() + } + + fn with_namespace(key: &[u8]) -> Vec { + let namespace = decode_hex("00064f424a454354"); + let mut v = Vec::with_capacity(namespace.len() + key.len()); + v.extend(namespace); + v.extend_from_slice(key); + v + } + + fn not_found_object_info(hex: &str) -> String { + let type_name = type_name::(); + let key = with_namespace(&decode_hex(hex)); + format!("type: {type_name}; key: {:02X?}", key) + } + #[test] fn proper_initialization() { let mut deps = mock_dependencies(); @@ -620,10 +637,6 @@ mod tests { assert_eq!("foobar", value.name); } - fn decode_hash(hash: String) -> Hash { - base16ct::lower::decode_vec(hash).unwrap().into() - } - #[test] fn store_object_without_limits() { let obj1_content = &general_purpose::STANDARD.encode("hello"); @@ -765,15 +778,15 @@ mod tests { assert_eq!( Binary::from_base64(content).unwrap(), Binary::from( - DATA.load(&deps.storage, decode_hash(expected_hash.clone())) + DATA.load(&deps.storage, decode_hex(expected_hash.as_str()).into()) .unwrap() ), ); let created = objects() - .load(&deps.storage, decode_hash(expected_hash.clone())) + .load(&deps.storage, decode_hex(expected_hash.as_str()).into()) .unwrap(); - assert_eq!(created.id, decode_hash(expected_hash.to_string())); + assert_eq!(created.id, decode_hex(expected_hash.as_str()).into()); assert_eq!(created.owner, info.sender.clone()); assert_eq!(created.size.u128(), *expected_size); assert_eq!( @@ -788,7 +801,10 @@ mod tests { assert_eq!( pins().has( &deps.storage, - (decode_hash(expected_hash.to_string()), info.clone().sender), + ( + decode_hex(expected_hash.as_str()).into(), + info.clone().sender + ), ), *pin, ); @@ -1425,7 +1441,9 @@ mod tests { senders: vec![mock_info("bob", &[])], expected_count: 0, expected_error: Some(ContractError::Std(StdError::not_found( - type_name::(), + not_found_object_info::( + "abafa4428bdc8c34dae28bbc17303a62175f274edf59757b3e9898215a428a56", + ), ))), expected_object_pin_count: vec![( ObjectId::from( @@ -1523,7 +1541,7 @@ mod tests { for (object_id, count) in case.expected_object_pin_count { assert_eq!( objects() - .load(&deps.storage, decode_hash(object_id)) + .load(&deps.storage, decode_hex(&object_id).into()) .unwrap() .pin_count, count @@ -1692,7 +1710,9 @@ mod tests { unpin_senders: vec![mock_info("martin", &[])], expected_count: 1, expected_error: Some(ContractError::Std(StdError::not_found( - type_name::(), + not_found_object_info::( + "abafa4428bdc8c34dae28bbc17303a62175f274edf59757b3e9898215a428a56", + ), ))), expected_object_pin_count: vec![( ObjectId::from( @@ -1804,7 +1824,7 @@ mod tests { for (object_id, count) in case.expected_object_pin_count { assert_eq!( objects() - .load(&deps.storage, decode_hash(object_id)) + .load(&deps.storage, decode_hex(&object_id).into()) .unwrap() .pin_count, count @@ -2076,7 +2096,9 @@ mod tests { after: None, first: None, }, - ContractError::Std(StdError::not_found(type_name::())), + ContractError::Std(StdError::not_found(not_found_object_info::( + "abafa4428bdc8c34dae28bbc17303a62175f274edf59757b3e9898215a428a56", + ))), ), ( QueryMsg::ObjectPins { @@ -2198,7 +2220,9 @@ mod tests { expected_count: 3, expected_total_size: Uint128::new(13), expected_error: Some(ContractError::Std(StdError::not_found( - type_name::(), + not_found_object_info::( + "abafa4428bdc8c34dae28bbc17303a62175f274edf59757b3e9898215a428a56", + ), ))), }, TC { @@ -2299,9 +2323,9 @@ mod tests { for object_id in case.forget_objects { assert_eq!( objects() - .load(&deps.storage, decode_hash(object_id)) + .load(&deps.storage, decode_hex(object_id.as_str()).into()) .unwrap_err(), - StdError::not_found(type_name::()) + StdError::not_found(not_found_object_info::(&object_id)) ); } }