Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Law-stone] Remove instantiate context state and use reply payload #607

Merged
merged 3 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 43 additions & 46 deletions contracts/axone-law-stone/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use cw_utils::nonpayable;

use crate::error::ContractError;
use crate::msg::{ExecuteMsg, InstantiateMsg, QueryMsg};
use crate::state::INSTANTIATE_CONTEXT;
use axone_objectarium_client::ObjectRef;

// version info for migration info
Expand Down Expand Up @@ -44,12 +43,10 @@ pub fn instantiate(
funds: vec![],
};

INSTANTIATE_CONTEXT.save(deps.storage, &msg.storage_address)?;

Ok(Response::new().add_submessage(SubMsg::reply_on_success(
store_program_msg,
STORE_PROGRAM_REPLY_ID,
)))
Ok(Response::new().add_submessage(
SubMsg::reply_on_success(store_program_msg, STORE_PROGRAM_REPLY_ID)
.with_payload(Binary::from(msg.storage_address.as_bytes())),
))
}

#[cfg_attr(not(feature = "library"), entry_point)]
Expand Down Expand Up @@ -210,8 +207,6 @@ pub mod reply {
_env: Env,
msg: Reply,
) -> Result<Response, ContractError> {
let context = INSTANTIATE_CONTEXT.load(deps.storage)?;

msg.result
.into_result()
.map_err(ParseReplyError::SubMsgFailure)
Expand All @@ -224,21 +219,25 @@ pub mod reply {
.into()
})
})
.map(|obj_id| LawStone {
broken: false,
law: ObjectRef {
object_id: obj_id,
storage_address: context.clone(),
},
.and_then(|obj_id| {
Ok(LawStone {
broken: false,
law: ObjectRef {
object_id: obj_id,
storage_address: String::from_utf8(msg.payload.to_vec()).map_err(|e| {
ParseReplyError::SubMsgFailure(format!(
"could not convert reply payload into string address: {}",
e
))
})?,
},
})
})
.and_then(|stone| -> Result<Vec<SubMsg>, ContractError> {
PROGRAM
.save(deps.storage, &stone)
.map_err(ContractError::from)?;

// Clean instantiate context
INSTANTIATE_CONTEXT.remove(deps.storage);

let req = build_source_files_query(stone.law.clone())?.into();
let res = deps.querier.query(&req).map_err(ContractError::from)?;

Expand Down Expand Up @@ -358,10 +357,15 @@ mod tests {

let res = instantiate(deps.as_mut(), mock_env(), info, msg).unwrap();

// Check if a message is send to the axone-objectarium to store the logic program.
// Check if a message is sent to the axone-objectarium to store the logic program.
assert_eq!(1, res.messages.len());
let sub_msg = res.messages.first().unwrap();
assert_eq!(STORE_PROGRAM_REPLY_ID, sub_msg.id);
assert_eq!(
"axone1ffzp0xmjhwkltuxcvccl0z9tyfuu7txp5ke0tpkcjpzuq9fcj3pq85yqlv",
String::from_utf8(sub_msg.payload.to_vec()).unwrap()
);

match &sub_msg.msg {
CosmosMsg::Wasm(wasm_msg) => match wasm_msg {
WasmMsg::Execute { msg, .. } => {
Expand All @@ -383,10 +387,6 @@ mod tests {
},
_ => panic!("cosmos sub message should be a Wasm message execute"),
}
assert_eq!(
"axone1ffzp0xmjhwkltuxcvccl0z9tyfuu7txp5ke0tpkcjpzuq9fcj3pq85yqlv".to_string(),
INSTANTIATE_CONTEXT.load(&deps.storage).unwrap()
);
}

#[test]
Expand Down Expand Up @@ -695,7 +695,9 @@ mod tests {

let reply = Reply {
id: STORE_PROGRAM_REPLY_ID,
payload: Binary::default(),
payload: Binary::from(
"axone1dclchlcttf2uektxyryg0c6yau63eml5q9uq03myg44ml8cxpxnqen9apd".as_bytes(),
),
gas_used: 0,
result: SubMsgResult::Ok(SubMsgResponse {
events: vec![Event::new("e".to_string())
Expand All @@ -705,14 +707,6 @@ mod tests {
}),
};

// Configure the instantiate context
INSTANTIATE_CONTEXT
.save(
deps.as_mut().storage,
&"axone1dclchlcttf2uektxyryg0c6yau63eml5q9uq03myg44ml8cxpxnqen9apd".to_string(),
)
.unwrap();

let response = reply::store_program_reply(deps.as_mut(), mock_env(), reply);
let res = response.unwrap();

Expand Down Expand Up @@ -779,11 +773,6 @@ mod tests {
"each dependencies should be pinned by a PinObject message"
)
}

assert!(
INSTANTIATE_CONTEXT.load(&deps.storage).is_err(),
"the instantiate context should be cleaned at the end"
)
}
}

Expand All @@ -794,7 +783,7 @@ mod tests {
(
Reply {
id: 404,
payload: Binary::default(),
payload: Binary::from("axone1dclchlcttf2uektxyryg0c6yau63eml5q9uq03myg44ml8cxpxnqen9apd".as_bytes()),
gas_used: 0,
result: SubMsgResult::Ok(SubMsgResponse {
events: vec![Event::new("e".to_string())
Expand All @@ -808,7 +797,7 @@ mod tests {
(
Reply {
id: 1,
payload: Binary::default(),
payload: Binary::from("axone1dclchlcttf2uektxyryg0c6yau63eml5q9uq03myg44ml8cxpxnqen9apd".as_bytes()),
gas_used: 0,
result: SubMsgResult::Ok(SubMsgResponse {
events: vec![Event::new("e".to_string())],
Expand All @@ -820,6 +809,21 @@ mod tests {
"reply event doesn't contains object id".to_string(),
))),
),
(
Reply {
id: 1,
payload: Binary::from(vec![0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89]),
gas_used: 0,
result: SubMsgResult::Ok(SubMsgResponse {
events: vec![Event::new("e".to_string()).add_attribute("id".to_string(), "some_object_id".to_string())],
data: None,
msg_responses: vec![],
}),
},
Err(ContractError::ParseReplyError(SubMsgFailure(
"could not convert reply payload into string address: invalid utf-8 sequence of 1 bytes from index 0".to_string(),
))),
),
];

for case in cases {
Expand All @@ -830,13 +834,6 @@ mod tests {
custom_query_type: PhantomData,
};

INSTANTIATE_CONTEXT
.save(
deps.as_mut().storage,
&"axone1dclchlcttf2uektxyryg0c6yau63eml5q9uq03myg44ml8cxpxnqen9apd".to_string(),
)
.unwrap();

let response = reply(deps.as_mut(), mock_env(), case.0);

assert_eq!(response, case.1);
Expand Down
3 changes: 0 additions & 3 deletions contracts/axone-law-stone/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ use crate::msg::ProgramResponse;
use axone_objectarium_client::ObjectRef;
use cw_storage_plus::{Item, Map};

/// State to store context during contract instantiation
pub const INSTANTIATE_CONTEXT: Item<String> = Item::new("instantiate");

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
pub struct LawStone {
pub broken: bool,
Expand Down
Loading