Skip to content

Commit

Permalink
Merge pull request #584 from axone-protocol/feat/objectarium-event-at…
Browse files Browse the repository at this point in the history
…tribute

✉️ Improve event attributes for `store_object`
  • Loading branch information
bdeneux authored Jun 20, 2024
2 parents c6c21bd + 11aea99 commit 2864de5
Showing 1 changed file with 253 additions and 13 deletions.
266 changes: 253 additions & 13 deletions contracts/axone-objectarium/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -137,6 +141,10 @@ pub mod execute {
Ok(bucket)
})?;

res = res
.add_attribute("size", size)
.add_attribute("compressed_size", compressed_size);

(
None,
Object {
Expand All @@ -153,15 +161,14 @@ 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())?;
objects().replace(deps.storage, id, 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(
Expand Down Expand Up @@ -441,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<u8> {
Expand Down Expand Up @@ -848,13 +856,29 @@ 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,
false,
"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"),
]
),
],
),
Expand All @@ -867,13 +891,29 @@ 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,
false,
"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"),
]
),
],
),
Expand All @@ -886,13 +926,29 @@ 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,
false,
"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"),
]
),
],
),
Expand All @@ -905,13 +961,29 @@ 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,
false,
"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"),
]
),
],
),
Expand All @@ -924,13 +996,29 @@ 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,
false,
"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"),
]
),
],
),
Expand All @@ -956,20 +1044,14 @@ 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,
compression_algorithm: Some(CompressionAlgorithm::Passthrough),
};
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()),
]
);
assert_eq!(res.attributes, *expected_attr);

assert_eq!(
Binary::from_base64(content).unwrap(),
Expand Down Expand Up @@ -1329,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();
Expand Down

0 comments on commit 2864de5

Please sign in to comment.