Skip to content

Commit

Permalink
chore: Update
Browse files Browse the repository at this point in the history
  • Loading branch information
kulikthebird committed Dec 5, 2024
1 parent cfe5648 commit 04a9126
Showing 1 changed file with 69 additions and 79 deletions.
148 changes: 69 additions & 79 deletions contracts/replier/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use cosmwasm_schema::{cw_serde, QueryResponses};
use cosmwasm_std::{
entry_point, to_json_binary, Binary, Deps, DepsMut, Env, MessageInfo, QueryResponse, Reply,
ReplyOn, Response, StdResult, SubMsg, WasmMsg, to_json_vec, from_json
entry_point, from_json, to_json_binary, to_json_vec, Binary, Deps, DepsMut, Env, MessageInfo, QueryResponse, Reply, ReplyOn, Response, StdError, StdResult, SubMsg, WasmMsg
};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
Expand All @@ -10,19 +9,19 @@ use serde::{Deserialize, Serialize};
pub struct InstantiateMsg {}

#[cw_serde]
pub enum ExecuteMsg {
Call { counter: u8, msg_id: u8 },
pub struct ExecuteMsg {
msg_id: u8,
set_data_resp: bool,
return_order_resp: bool,
exec_error: bool,
reply_error: bool,
messages: Vec<ExecuteMsg>
}

#[cw_serde]
#[derive(QueryResponses)]
pub enum QueryMsg {}

#[cw_serde]
pub struct VerifierResponse {
pub verifier: String,
}

pub const CONFIG_KEY: &[u8] = b"config";
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
pub struct State {
Expand All @@ -42,7 +41,7 @@ pub fn instantiate(
order: vec![],
})?,
);
Ok(Response::new().add_attribute("Let the", "hacking begin"))
Ok(Response::new())
}

#[entry_point]
Expand All @@ -52,59 +51,48 @@ pub fn execute(
_info: MessageInfo,
msg: ExecuteMsg,
) -> StdResult<Response> {
match msg {
ExecuteMsg::Call { counter, msg_id } => {

let data = deps
.storage
.get(CONFIG_KEY)
.unwrap();
let mut config: State = from_json(data)?;
config.order.extend([0xEE, msg_id]);
deps.storage.set(CONFIG_KEY, &to_json_vec(&config)?);
let data = deps
.storage
.get(CONFIG_KEY)
.unwrap();
let mut config: State = from_json(data)?;
config.order.extend([0xEE, msg.msg_id]);
deps.storage.set(CONFIG_KEY, &to_json_vec(&config)?);

let mut resp = Response::new().set_data(Binary::new(vec![0xEE, msg.msg_id]));

if counter > 0 {
let msg1 = WasmMsg::Execute {
contract_addr: env.contract.address.to_string(),
msg: to_json_binary(&ExecuteMsg::Call {
counter: counter - 1,
msg_id: msg_id + msg_id + 1,
})
.unwrap(),
funds: vec![],
};
let submsg1 = SubMsg {
id: msg_id.into(),
payload: Binary::default(),
msg: msg1.into(),
gas_limit: None,
reply_on: ReplyOn::Always,
};
let msg2 = WasmMsg::Execute {
contract_addr: env.contract.address.to_string(),
msg: to_json_binary(&ExecuteMsg::Call {
counter: counter - 1,
msg_id: msg_id + msg_id + 2,
})
.unwrap(),
funds: vec![],
};
let submsg2 = SubMsg {
id: msg_id.into(),
payload: Binary::default(),
msg: msg2.into(),
gas_limit: None,
reply_on: ReplyOn::Always,
};
Ok(Response::new()
.set_data(Binary::new(vec![0xEE, msg_id]))
.add_submessage(submsg1)
.add_submessage(submsg2))
} else {
Ok(Response::new().set_data(Binary::new(vec![0xEE, msg_id])))
}
if msg.exec_error {
return Err(StdError::generic_err(format!("Err in exec msg_id: {}", msg.msg_id)))
}

for next_msg in msg.messages {
let wasm_msg = WasmMsg::Execute {
contract_addr: env.contract.address.to_string(),
msg: to_json_binary(&next_msg)
.unwrap(),
funds: vec![],
};
let mut msg_id: u64 = msg.msg_id.into();
if msg.reply_error {
msg_id = msg_id | 0x100;
}
if msg.set_data_resp {
msg_id = msg_id | 0x200;
}
if msg.return_order_resp {
msg_id = msg_id | 0x400;
}

let submsg = SubMsg {
id: msg_id,
payload: Binary::default(),
msg: wasm_msg.into(),
gas_limit: None,
reply_on: ReplyOn::Always,
};
resp = resp.add_submessage(submsg);
}
Ok(resp)
}

#[entry_point]
Expand All @@ -114,33 +102,35 @@ pub fn query(_deps: Deps, _env: Env, _msg: QueryMsg) -> StdResult<QueryResponse>

#[entry_point]
pub fn reply(deps: DepsMut, _env: Env, msg: Reply) -> StdResult<Response> {
let msg_id = msg.id & 0xFF;
let should_return_err = msg.id & 0x100 != 0;
let should_set_data = msg.id & 0x200 != 0;
let should_set_order = msg.id & 0x400 != 0;
let result = msg.result.unwrap();

if should_return_err {
return Err(StdError::generic_err(format!("Err in reply msg_id: {}", msg_id)))
}

let data = deps
.storage
.get(CONFIG_KEY)
.unwrap();
let mut config: State = from_json(data)?;
config.order.extend([0xBB, msg.id as u8]);
config.order.extend([0xBB, msg_id as u8]);
deps.storage.set(CONFIG_KEY, &to_json_vec(&config)?);

let data = result
.msg_responses
.into_iter()
.map(|resp| resp.value.as_slice().to_vec())
.flatten()
.chain([0xBB, msg.id as u8].into_iter());

let data = if msg.id == 0 {
let mut data = data.chain([0xAA, 0xAA, 0xAA].into_iter()).collect::<Vec<_>>();
data.extend(config.order);
data
if should_set_order {
Ok(Response::new().set_data(Binary::new(config.order)))
} else if should_set_data {
Ok(Response::new().set_data(Binary::new(result
.msg_responses
.into_iter()
.map(|resp| resp.value.as_slice().to_vec())
.flatten()
.chain([0xBB, msg_id as u8].into_iter())
.collect())))
} else {
data.collect::<Vec<_>>()
};

Ok(Response::new().set_data(Binary::new(
data
)))
// Ok(Response::default())
Ok(Response::new())
}
}

0 comments on commit 04a9126

Please sign in to comment.