Skip to content

Commit

Permalink
feat(objectarium)!: avoid failing on already stored object
Browse files Browse the repository at this point in the history
  • Loading branch information
amimart committed Apr 6, 2024
1 parent 4b9ac2f commit 7e56d22
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 45 deletions.
97 changes: 59 additions & 38 deletions contracts/okp4-objectarium/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,50 +118,49 @@ pub mod execute {
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));
}
let compressed_data = compression.compress(&data.0)?;

data_path.save(deps.storage, &compressed_data)?;

// store object
let compressed_size = (compressed_data.len() as u128).into();
let object = &Object {
id,
owner: info.sender.clone(),
size,
pin_count: if pin { Uint128::one() } else { Uint128::zero() },
compression,
compressed_size,
};
if !data_path.has(deps.storage) {
let compressed_data = compression.compress(&data.0)?;

data_path.save(deps.storage, &compressed_data)?;

// store object
let compressed_size = (compressed_data.len() as u128).into();
let object = &Object {
id: id.clone(),
owner: info.sender.clone(),
size,
pin_count: if pin { Uint128::one() } else { Uint128::zero() },
compression,
compressed_size,
};

objects().save(deps.storage, object.id.clone(), object)?;
objects().save(deps.storage, object.id.clone(), object)?;

// save bucket stats
BUCKET.update(deps.storage, |mut bucket| -> Result<_, ContractError> {
let stat = &mut bucket.stat;
stat.size += size;
stat.object_count += Uint128::one();
stat.compressed_size += compressed_size;
Ok(bucket)
})?;
// save bucket stats
BUCKET.update(deps.storage, |mut bucket| -> Result<_, ContractError> {
let stat = &mut bucket.stat;
stat.size += size;
stat.object_count += Uint128::one();
stat.compressed_size += compressed_size;
Ok(bucket)
})?;
}

// save pin
if pin {
pins().save(
deps.storage,
(object.id.clone(), info.sender.clone()),
(id.clone(), info.sender.clone()),
&Pin {
id: object.id.clone(),
id: id.clone(),
address: info.sender,
},
)?;
}

Ok(Response::new()
.add_attribute("action", "store_object")
.add_attribute("id", object.id.to_string()))
.add_attribute("id", id.to_string()))
}

pub fn pin_object(
Expand Down Expand Up @@ -849,16 +848,38 @@ mod tests {
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,
compression_algorithm: Some(CompressionAlgorithm::Passthrough),
};
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)),
execute(
deps.as_mut(),
mock_env(),
info.clone(),
ExecuteMsg::StoreObject {
data: Binary::from_base64(object.as_str()).unwrap(),
pin: false,
compression_algorithm: Some(CompressionAlgorithm::Passthrough),
},
)
.unwrap();

let res = execute(
deps.as_mut(),
mock_env(),
info.clone(),
ExecuteMsg::StoreObject {
data: Binary::from_base64(object.as_str()).unwrap(),
pin: true,
compression_algorithm: Some(CompressionAlgorithm::Passthrough),
},
);

assert!(res.is_ok());
assert!(pins().has(
&deps.storage,
(
decode_hex("46c4b2f687df251a98cc83cc35437e9893c16861899c2f9d183e1de57d3a2c0e")
.into(),
info.sender
),
))
}

#[test]
Expand Down
7 changes: 0 additions & 7 deletions contracts/okp4-objectarium/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ pub enum BucketError {
#[error("Maximum object pins number exceeded: {0} / {1}")]
MaxObjectPinsLimitExceeded(Uint128, Uint128),

#[error("Object is already stored")]
ObjectAlreadyStored,

#[error("Compression algorithm is not accepted: {0:?} (accepted: \"{1:?}\")")]
CompressionAlgorithmNotAccepted(CompressionAlgorithm, Vec<CompressionAlgorithm>),
}
Expand Down Expand Up @@ -82,10 +79,6 @@ fn test_bucket_error_messages() {
ContractError::Bucket(BucketError::MaxObjectPinsLimitExceeded(5u8.into(), 2u8.into())),
"Maximum object pins number exceeded: 5 / 2",
),
(
ContractError::Bucket(BucketError::ObjectAlreadyStored),
"Object is already stored",
),
(
ContractError::Bucket(BucketError::CompressionAlgorithmNotAccepted(
CompressionAlgorithm::Snappy,
Expand Down

0 comments on commit 7e56d22

Please sign in to comment.