From 4a39965fb0555398b817dbdac947acde19cfadb5 Mon Sep 17 00:00:00 2001 From: Benjamin DENEUX Date: Wed, 19 Jun 2024 14:56:43 +0200 Subject: [PATCH 1/3] feat(objectarium): include compressed_size, size and pinned attribute when store object --- contracts/axone-objectarium/src/contract.rs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/contracts/axone-objectarium/src/contract.rs b/contracts/axone-objectarium/src/contract.rs index 3cbb6540..026f52f1 100644 --- a/contracts/axone-objectarium/src/contract.rs +++ b/contracts/axone-objectarium/src/contract.rs @@ -120,6 +120,10 @@ pub mod execute { // store object data let id = crypto::hash(&bucket.config.hash_algorithm.into(), &data.0); + let mut res = Response::new() + .add_attribute("action", "store_object") + .add_attribute("id", id.to_string()); + let data_path = DATA.key(id.clone()); let (old_obj, mut new_obj) = if !data_path.has(deps.storage) { @@ -137,6 +141,9 @@ pub mod execute { Ok(bucket) })?; + res = res.add_attribute("size", size) + .add_attribute("compressed_size", compressed_size); + ( None, Object { @@ -153,15 +160,15 @@ pub mod execute { (Some(old.clone()), old) }; + let mut pinned = false; if pin { - may_pin_object(deps.storage, info.sender, &mut new_obj)?; + pinned = may_pin_object(deps.storage, info.sender, &mut new_obj)?; } + objects().replace(deps.storage, id.clone(), Some(&new_obj), old_obj.as_ref())?; - Ok(Response::new() - .add_attribute("action", "store_object") - .add_attribute("id", id.to_string())) + Ok(res.add_attribute("pinned", pinned.to_string())) } pub fn pin_object( From 82418ccdb62636abcc31874a077e6d6d0910c59d Mon Sep 17 00:00:00 2001 From: Benjamin DENEUX Date: Wed, 19 Jun 2024 14:57:17 +0200 Subject: [PATCH 2/3] test(objectarium): test if event attribute is correclty set on store object --- contracts/axone-objectarium/src/contract.rs | 87 +++++++++++++++++++-- 1 file changed, 82 insertions(+), 5 deletions(-) diff --git a/contracts/axone-objectarium/src/contract.rs b/contracts/axone-objectarium/src/contract.rs index 026f52f1..38979bcf 100644 --- a/contracts/axone-objectarium/src/contract.rs +++ b/contracts/axone-objectarium/src/contract.rs @@ -855,6 +855,14 @@ mod tests { "5d41402abc4b2a76b9719d911017c592" .to_string(), 5, + vec![ + Attribute::new("action", "store_object"), + Attribute::new("id", "5d41402abc4b2a76b9719d911017c592" + .to_string()), + Attribute::new("size", "5"), + Attribute::new("compressed_size", "5"), + Attribute::new("pinned", "true"), + ] ), ( obj2_content, @@ -862,6 +870,14 @@ mod tests { "33f41d49353ad1a876e36918f64eac4d" .to_string(), 4, + vec![ + Attribute::new("action", "store_object"), + Attribute::new("id", "33f41d49353ad1a876e36918f64eac4d" + .to_string()), + Attribute::new("size", "4"), + Attribute::new("compressed_size", "4"), + Attribute::new("pinned", "false"), + ] ), ], ), @@ -874,6 +890,14 @@ mod tests { "ea09ae9cc6768c50fcee903ed054556e5bfc8347907f12598aa24193" .to_string(), 5, + vec![ + Attribute::new("action", "store_object"), + Attribute::new("id", "ea09ae9cc6768c50fcee903ed054556e5bfc8347907f12598aa24193" + .to_string()), + Attribute::new("size", "5"), + Attribute::new("compressed_size", "5"), + Attribute::new("pinned", "true"), + ] ), ( obj2_content, @@ -881,6 +905,14 @@ mod tests { "fe798aa30e560c57d69c46982b2bb1320dc86813730bb7c6406ce84b" .to_string(), 4, + vec![ + Attribute::new("action", "store_object"), + Attribute::new("id", "fe798aa30e560c57d69c46982b2bb1320dc86813730bb7c6406ce84b" + .to_string()), + Attribute::new("size", "4"), + Attribute::new("compressed_size", "4"), + Attribute::new("pinned", "false"), + ] ), ], ), @@ -893,6 +925,14 @@ mod tests { "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824" .to_string(), 5, + vec![ + Attribute::new("action", "store_object"), + Attribute::new("id", "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824" + .to_string()), + Attribute::new("size", "5"), + Attribute::new("compressed_size", "5"), + Attribute::new("pinned", "true"), + ] ), ( obj2_content, @@ -900,6 +940,14 @@ mod tests { "315d0d9ab12c5f8884100055f79de50b72db4bd2c9bfd3df049d89640fed1fa6" .to_string(), 4, + vec![ + Attribute::new("action", "store_object"), + Attribute::new("id", "315d0d9ab12c5f8884100055f79de50b72db4bd2c9bfd3df049d89640fed1fa6" + .to_string()), + Attribute::new("size", "4"), + Attribute::new("compressed_size", "4"), + Attribute::new("pinned", "false"), + ] ), ], ), @@ -912,6 +960,14 @@ mod tests { "59e1748777448c69de6b800d7a33bbfb9ff1b463e44354c3553bcdb9c666fa90125a3c79f90397bdf5f6a13de828684f" .to_string(), 5, + vec![ + Attribute::new("action", "store_object"), + Attribute::new("id", "59e1748777448c69de6b800d7a33bbfb9ff1b463e44354c3553bcdb9c666fa90125a3c79f90397bdf5f6a13de828684f" + .to_string()), + Attribute::new("size", "5"), + Attribute::new("compressed_size", "5"), + Attribute::new("pinned", "true"), + ] ), ( obj2_content, @@ -919,6 +975,14 @@ mod tests { "e700b122a81f64ce34ab67c6a815987536a05b0590bbeb32cf5e88963edd8c6e69c9e43b0f957f242d984f09f91bcaf2" .to_string(), 4, + vec![ + Attribute::new("action", "store_object"), + Attribute::new("id", "e700b122a81f64ce34ab67c6a815987536a05b0590bbeb32cf5e88963edd8c6e69c9e43b0f957f242d984f09f91bcaf2" + .to_string()), + Attribute::new("size", "4"), + Attribute::new("compressed_size", "4"), + Attribute::new("pinned", "false"), + ] ), ], ), @@ -931,6 +995,14 @@ mod tests { "9b71d224bd62f3785d96d46ad3ea3d73319bfbc2890caadae2dff72519673ca72323c3d99ba5c11d7c7acc6e14b8c5da0c4663475c2e5c3adef46f73bcdec043" .to_string(), 5, + vec![ + Attribute::new("action", "store_object"), + Attribute::new("id", "9b71d224bd62f3785d96d46ad3ea3d73319bfbc2890caadae2dff72519673ca72323c3d99ba5c11d7c7acc6e14b8c5da0c4663475c2e5c3adef46f73bcdec043" + .to_string()), + Attribute::new("size", "5"), + Attribute::new("compressed_size", "5"), + Attribute::new("pinned", "true"), + ] ), ( obj2_content, @@ -938,6 +1010,14 @@ mod tests { "e4f4025e1e28abb473c89bcae03ded972e91b4427e8970be87f645cc34b9b203d633c12760e32c97011439640cba159f60992e10aac8023fa2577cadc1be3b55" .to_string(), 4, + vec![ + Attribute::new("action", "store_object"), + Attribute::new("id", "e4f4025e1e28abb473c89bcae03ded972e91b4427e8970be87f645cc34b9b203d633c12760e32c97011439640cba159f60992e10aac8023fa2577cadc1be3b55" + .to_string()), + Attribute::new("size", "4"), + Attribute::new("compressed_size", "4"), + Attribute::new("pinned", "false"), + ] ), ], ), @@ -963,7 +1043,7 @@ mod tests { ) .unwrap(); - for (content, pin, expected_hash, expected_size) in &objs { + for (content, pin, expected_hash, expected_size, expected_attr) in &objs { let msg = ExecuteMsg::StoreObject { data: Binary::from_base64(content).unwrap(), pin: *pin, @@ -972,10 +1052,7 @@ mod tests { let res = execute(deps.as_mut(), mock_env(), info.clone(), msg).unwrap(); assert_eq!( res.attributes, - vec![ - Attribute::new("action", "store_object"), - Attribute::new("id", expected_hash.clone()), - ] + *expected_attr ); assert_eq!( From 11aea99e7a25ae4dbc4c8634db6e5647651907e4 Mon Sep 17 00:00:00 2001 From: Benjamin DENEUX Date: Wed, 19 Jun 2024 15:59:34 +0200 Subject: [PATCH 3/3] test(objectarium): add specific test for event attribute --- contracts/axone-objectarium/src/contract.rs | 170 +++++++++++++++++++- 1 file changed, 163 insertions(+), 7 deletions(-) diff --git a/contracts/axone-objectarium/src/contract.rs b/contracts/axone-objectarium/src/contract.rs index 38979bcf..ae385586 100644 --- a/contracts/axone-objectarium/src/contract.rs +++ b/contracts/axone-objectarium/src/contract.rs @@ -141,7 +141,8 @@ pub mod execute { Ok(bucket) })?; - res = res.add_attribute("size", size) + res = res + .add_attribute("size", size) .add_attribute("compressed_size", compressed_size); ( @@ -165,8 +166,7 @@ pub mod execute { pinned = may_pin_object(deps.storage, info.sender, &mut new_obj)?; } - - objects().replace(deps.storage, id.clone(), Some(&new_obj), old_obj.as_ref())?; + objects().replace(deps.storage, id, Some(&new_obj), old_obj.as_ref())?; Ok(res.add_attribute("pinned", pinned.to_string())) } @@ -448,6 +448,7 @@ mod tests { use cosmwasm_std::{coins, from_json, Addr, Attribute, Order, StdError, Uint128}; use cw_utils::PaymentError; + use crate::msg::CompressionAlgorithm::{Passthrough, Snappy}; use std::any::type_name; fn decode_hex(hex: &str) -> Vec { @@ -1050,10 +1051,7 @@ mod tests { compression_algorithm: Some(CompressionAlgorithm::Passthrough), }; let res = execute(deps.as_mut(), mock_env(), info.clone(), msg).unwrap(); - assert_eq!( - res.attributes, - *expected_attr - ); + assert_eq!(res.attributes, *expected_attr); assert_eq!( Binary::from_base64(content).unwrap(), @@ -1413,6 +1411,164 @@ mod tests { } } + #[test] + fn store_object_check_attributes() { + let obj_content = &general_purpose::STANDARD.encode("hello"); + let obj_exist_content = &general_purpose::STANDARD.encode("axone"); + let obj_exist_pinned_content = &general_purpose::STANDARD.encode("protocol"); + let obj_large_content = &general_purpose::STANDARD.encode("In a world of interconnected systems, there were countless realms, one of which was known as AXONE. Within this realm, AI researchers harnessed the power to create collaborative AI models. As the realm expanded, the researchers used their expertise to power collaborative systems, bringing innovation and advancement to all who sought their knowledge. And so, the legend of the AI researchers and their collaborative AI models lived on, inspiring future generations to unlock the potential of the digital realm with AI."); + + let test_cases = vec![ + ( + obj_content, + true, + Passthrough, + vec![ + Attribute::new("action", "store_object"), + Attribute::new( + "id", + "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824" + .to_string(), + ), + Attribute::new("size", "5"), + Attribute::new("compressed_size", "5"), + Attribute::new("pinned", "true"), + ], + ), + ( + obj_content, + false, + Passthrough, + vec![ + Attribute::new("action", "store_object"), + Attribute::new( + "id", + "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824" + .to_string(), + ), + Attribute::new("size", "5"), + Attribute::new("compressed_size", "5"), + Attribute::new("pinned", "false"), + ], + ), + ( + obj_large_content, + true, + Snappy, + vec![ + Attribute::new("action", "store_object"), + Attribute::new( + "id", + "afb9c7804a3515714a3ec2313c990df31d54000b890ae677dcaaa1060b437660" + .to_string(), + ), + Attribute::new("size", "519"), + Attribute::new("compressed_size", "453"), + Attribute::new("pinned", "true"), + ], + ), + ( + obj_exist_content, + true, + Passthrough, + vec![ + Attribute::new("action", "store_object"), + Attribute::new( + "id", + "45a8243ff863a08531c666569ce9997b63df94c2e2aeedaed3d32656ee1ae622" + .to_string(), + ), + Attribute::new("pinned", "true"), + ], + ), + ( + obj_exist_content, + false, + Passthrough, + vec![ + Attribute::new("action", "store_object"), + Attribute::new( + "id", + "45a8243ff863a08531c666569ce9997b63df94c2e2aeedaed3d32656ee1ae622" + .to_string(), + ), + Attribute::new("pinned", "false"), + ], + ), + ( + obj_exist_pinned_content, + false, + Passthrough, + vec![ + Attribute::new("action", "store_object"), + Attribute::new( + "id", + "2ea88c7a30351b12a4dcfc06cdce2af6eab18416176466c2500cb6ef74f745bf" + .to_string(), + ), + Attribute::new("pinned", "false"), + ], + ), + ( + obj_exist_pinned_content, + true, + Passthrough, + vec![ + Attribute::new("action", "store_object"), + Attribute::new( + "id", + "2ea88c7a30351b12a4dcfc06cdce2af6eab18416176466c2500cb6ef74f745bf" + .to_string(), + ), + Attribute::new("pinned", "false"), + ], + ), + ]; + + for (content, pin, compression_algorithm, expected_attr) in &test_cases { + let mut deps = mock_dependencies(); + let info = mock_info("creator", &[]); + let msg = InstantiateMsg { + bucket: String::from("test"), + config: Default::default(), + limits: Default::default(), + pagination: Default::default(), + }; + instantiate(deps.as_mut(), mock_env(), info.clone(), msg).unwrap(); + + _ = execute( + deps.as_mut(), + mock_env(), + info.clone(), + ExecuteMsg::StoreObject { + data: Binary::from_base64(obj_exist_content).unwrap(), + pin: false, + compression_algorithm: Some(Passthrough), + }, + ); + + _ = execute( + deps.as_mut(), + mock_env(), + info.clone(), + ExecuteMsg::StoreObject { + data: Binary::from_base64(obj_exist_pinned_content).unwrap(), + pin: true, + compression_algorithm: Some(Passthrough), + }, + ); + + let msg = ExecuteMsg::StoreObject { + data: Binary::from_base64(content).unwrap(), + pin: *pin, + compression_algorithm: Some(*compression_algorithm), + }; + + let res = execute(deps.as_mut(), mock_env(), info.clone(), msg).unwrap(); + assert_eq!(res.attributes, *expected_attr); + } + } + #[test] fn object() { let mut deps = mock_dependencies();