Skip to content

Commit

Permalink
feat: mew-message-type added in cosmwasm (#246)
Browse files Browse the repository at this point in the history
* feat: mew-message-type added in cosmwasm

* fix: sendMessageAny method added
  • Loading branch information
gcranju authored Jan 24, 2024
1 parent df65822 commit 16a3a5a
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 6 deletions.
71 changes: 69 additions & 2 deletions contracts/cosmwasm-vm/cw-mock-dapp-multi/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
use std::str::from_utf8;

use cosmwasm_std::SubMsg;
use cw_xcall_lib::{
message::envelope::Envelope, network_address::NetworkAddress, xcall_msg::ExecuteMsg,
use cw_xcall_lib::message::call_message_persisted::CallMessagePersisted;
use cw_xcall_lib::message::msg_type::MessageType;
use cw_xcall_lib::message::AnyMessage;
use cw_xcall_lib::message::{
call_message::CallMessage, call_message_rollback::CallMessageWithRollback, envelope::Envelope,
};
use cw_xcall_lib::{network_address::NetworkAddress, xcall_msg::ExecuteMsg};

use super::*;

Expand Down Expand Up @@ -75,6 +79,69 @@ impl<'a> CwMockService<'a> {
Ok(submessage)
}

pub fn send_new_call_message(
&self,
deps: DepsMut,
info: MessageInfo,
to: NetworkAddress,
data: Vec<u8>,
message_type: u64,
rollback: Option<Vec<u8>>,
) -> Result<Response, ContractError> {
let _sequence = self.increment_sequence(deps.storage)?;
let address = self
.xcall_address()
.load(deps.storage)
.map_err(|_e| ContractError::ModuleAddressNotFound)?;

let network_id = to.nid().to_string();
let connections = self.get_connections(deps.storage, network_id)?;
let (sources, destinations) =
connections
.into_iter()
.fold((Vec::new(), Vec::new()), |mut acc, x| {
acc.0.push(x.src_endpoint);
acc.1.push(x.dest_endpoint);
acc
});

let msg = if message_type == MessageType::CallMessagePersisted as u64 {
AnyMessage::CallMessagePersisted(CallMessagePersisted { data: data.clone() })
} else if message_type == MessageType::CallMessageWithRollback as u64 {
if let Some(rollback) = rollback {
AnyMessage::CallMessageWithRollback(CallMessageWithRollback {
data: data.clone(),
rollback,
})
} else {
return Err(ContractError::InvalidRollbackMessage);
}
} else if message_type == MessageType::CallMessage as u64 {
AnyMessage::CallMessage(CallMessage { data: data.clone() })
} else {
return Err(ContractError::InvalidMessageType);
};
let envelope = Envelope::new(msg, sources, destinations);

let msg = ExecuteMsg::SendCall { envelope, to };
let message: CosmosMsg<Empty> = CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: address,
msg: to_binary(&msg).unwrap(),
funds: info.funds,
});

let submessage = SubMsg {
id: 1,
msg: message,
gas_limit: None,
reply_on: cosmwasm_std::ReplyOn::Never,
};

Ok(Response::new()
.add_attribute("Action", "SendNewMessage")
.add_submessage(submessage))
}

pub fn send_call(
&self,
deps: DepsMut,
Expand Down
4 changes: 4 additions & 0 deletions contracts/cosmwasm-vm/cw-mock-dapp-multi/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,8 @@ pub enum ContractError {
ConnectionNotFound { network_id: String },
#[error("Invalid Address {address}")]
InvalidAddress { address: String },
#[error("Invalid Rollback Message")]
InvalidRollbackMessage,
#[error("Invalid Message Type")]
InvalidMessageType,
}
10 changes: 9 additions & 1 deletion contracts/cosmwasm-vm/cw-mock-dapp-multi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,15 @@ pub fn execute(
.add_submessage(submsg)
.add_attribute("Action", "SendMessage"))
}
ExecuteMsg::SendCall { to, envelope } => call_service.send_call(deps, info, to, envelope),
ExecuteMsg::SendNewCallMessage {
to,
data,
message_type,
rollback,
} => call_service.send_new_call_message(deps, info, to, data, message_type, rollback),
ExecuteMsg::SendMessageAny { to, envelope } => {
call_service.send_call(deps, info, to, envelope)
}
ExecuteMsg::HandleCallMessage {
from,
data,
Expand Down
8 changes: 7 additions & 1 deletion contracts/cosmwasm-vm/cw-mock-dapp-multi/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@ pub enum ExecuteMsg {
data: Vec<u8>,
rollback: Option<Vec<u8>>,
},
SendCall {
SendMessageAny {
to: NetworkAddress,
envelope: Envelope,
},
SendNewCallMessage {
to: NetworkAddress,
data: Vec<u8>,
message_type: u64,
rollback: Option<Vec<u8>>,
},
HandleCallMessage {
from: NetworkAddress,
data: Vec<u8>,
Expand Down
49 changes: 47 additions & 2 deletions contracts/cosmwasm-vm/cw-mock-dapp/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
use std::str::from_utf8;

use cw_xcall_lib::{network_address::NetworkAddress, xcall_msg::ExecuteMsg};

use super::*;
use cw_xcall_lib::message::call_message_persisted::CallMessagePersisted;
use cw_xcall_lib::message::AnyMessage;
use cw_xcall_lib::message::{
call_message::CallMessage, call_message_rollback::CallMessageWithRollback, envelope::Envelope,
};
use cw_xcall_lib::{network_address::NetworkAddress, xcall_msg::ExecuteMsg};

// version info for migration info
const CONTRACT_NAME: &str = "crates.io:cw-mock-dapp";
Expand Down Expand Up @@ -58,6 +62,47 @@ impl<'a> CwMockService<'a> {
.add_message(message))
}

pub fn send_new_call_message(
&self,
deps: DepsMut,
info: MessageInfo,
to: NetworkAddress,
data: Vec<u8>,
rollback: Option<Vec<u8>>,
is_persistent: bool,
) -> Result<Response, ContractError> {
let _sequence = self.increment_sequence(deps.storage)?;
let address = self
.xcall_address()
.load(deps.storage)
.map_err(|_e| ContractError::ModuleAddressNotFound)?;

let msg = if is_persistent {
AnyMessage::CallMessagePersisted(CallMessagePersisted { data: data.clone() })
} else if let Some(rollback) = rollback.clone() {
AnyMessage::CallMessageWithRollback(CallMessageWithRollback {
data: data.clone(),
rollback: rollback.clone(),
})
} else {
AnyMessage::CallMessage(CallMessage { data: data.clone() })
};
let envelope = Envelope::new(msg, vec![], vec![]);

let msg = ExecuteMsg::SendCall { envelope, to };
let message: CosmosMsg<Empty> = CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: address,
msg: to_binary(&msg).unwrap(),
funds: info.funds,
});

println!("{:?}", message);

Ok(Response::new()
.add_attribute("Action", "SendNewMessage")
.add_message(message))
}

pub fn handle_call_message(
&self,
deps: DepsMut,
Expand Down
6 changes: 6 additions & 0 deletions contracts/cosmwasm-vm/cw-mock-dapp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ pub fn execute(
ExecuteMsg::SendCallMessage { to, data, rollback } => {
call_service.send_call_message(deps, info, to, data, rollback)
}
ExecuteMsg::SendNewCallMessage {
to,
data,
rollback,
is_persistent,
} => call_service.send_new_call_message(deps, info, to, data, rollback, is_persistent),
ExecuteMsg::HandleCallMessage { from, data } => {
call_service.handle_call_message(deps, info, from, data)
}
Expand Down
6 changes: 6 additions & 0 deletions contracts/cosmwasm-vm/cw-mock-dapp/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ pub enum ExecuteMsg {
data: Vec<u8>,
rollback: Option<Vec<u8>>,
},
SendNewCallMessage {
to: NetworkAddress,
data: Vec<u8>,
rollback: Option<Vec<u8>>,
is_persistent: bool,
},
HandleCallMessage {
from: NetworkAddress,
data: Vec<u8>,
Expand Down

0 comments on commit 16a3a5a

Please sign in to comment.