Skip to content

Commit

Permalink
fix: message type encoding and decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
bishalbikram committed May 8, 2024
1 parent 61225a1 commit 75e0b74
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 24 deletions.
4 changes: 2 additions & 2 deletions contracts/soroban/contracts/mock-dapp-multi/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl MockDapp {
fee: u128,
) -> Result<(), ContractError> {
let network_id = to.nid(&env);
let message = Self::process_message(msg_type, data, rollback)?;
let message = Self::process_message(msg_type as u8, data, rollback)?;
let (sources, destinations) = Self::get_network_connections(&env, network_id)?;

let envelope = Envelope {
Expand Down Expand Up @@ -107,7 +107,7 @@ impl MockDapp {
}

fn process_message(
message_type: u32,
message_type: u8,
data: Bytes,
rollback: Option<Bytes>,
) -> Result<AnyMessage, ContractError> {
Expand Down
2 changes: 1 addition & 1 deletion contracts/soroban/contracts/xcall/src/dapp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl Xcall {
from: &NetworkAddress,
data: &Bytes,
_protocols: Vec<String>,
) -> u32 {
) -> u8 {
let mut protocols: Option<Vec<String>> = None;
if _protocols.len() > 0 {
protocols = Some(_protocols)
Expand Down
8 changes: 6 additions & 2 deletions contracts/soroban/contracts/xcall/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,12 @@ pub(crate) fn call_message(
e.events().publish(("CallMessage",), data);
}

pub(crate) fn call_executed(e: &Env, req_id: u128, code: u32, msg: String) {
let data = CallExecutedEvent { req_id, code, msg };
pub(crate) fn call_executed(e: &Env, req_id: u128, code: u8, msg: String) {
let data = CallExecutedEvent {
req_id,
code: code as u32,
msg,
};
e.events().publish(("CallExecuted",), data)
}

Expand Down
6 changes: 3 additions & 3 deletions contracts/soroban/contracts/xcall/src/types/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub enum MessageType {
CallMessagePersisted = 2,
}

impl From<MessageType> for u32 {
impl From<MessageType> for u8 {
fn from(value: MessageType) -> Self {
match value {
MessageType::CallMessage => 0,
Expand All @@ -19,8 +19,8 @@ impl From<MessageType> for u32 {
}
}

impl From<u32> for MessageType {
fn from(value: u32) -> Self {
impl From<u8> for MessageType {
fn from(value: u8) -> Self {
match value {
0 => MessageType::CallMessage,
1 => MessageType::CallMessageWithRollback,
Expand Down
10 changes: 2 additions & 8 deletions contracts/soroban/contracts/xcall/src/types/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl CSMessageRequest {
list.push_back(encoder::encode_string(&e, self.from.as_string().clone()));
list.push_back(encoder::encode_string(&e, self.to.clone()));
list.push_back(encoder::encode_u128(&e, self.sequence_no));
list.push_back(encoder::encode_u32(&e, self.msg_type.into()));
list.push_back(encoder::encode_u8(&e, self.msg_type.into()));
list.push_back(encoder::encode(&e, self.data.clone()));
list.push_back(encoder::encode_strings(&e, self.protocols.clone()));

Expand All @@ -94,16 +94,10 @@ impl CSMessageRequest {
let from = decoder::decode_string(e, decoded.get(0).unwrap());
let to = decoder::decode_string(&e, decoded.get(1).unwrap());
let sequence_no = decoder::decode_u128(&e, decoded.get(2).unwrap());
let msg_type = decoder::decode_u8(&e, decoded.get(3).unwrap()).into();
let data = decoded.get(4).unwrap();
let protocols = decoder::decode_strings(&e, decoded.get(5).unwrap());

let decoded_msg = decoded.get(3).unwrap();
let msg_type = if decoded_msg.len() > 0 {
decoder::decode_u32(&e, decoded_msg).into()
} else {
MessageType::CallMessage
};

Ok(Self {
from: NetworkAddress::to_network_address(from),
to,
Expand Down
14 changes: 6 additions & 8 deletions contracts/soroban/contracts/xcall/src/types/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub enum CSResponseType {
CSResponseSuccess = 1,
}

impl From<CSResponseType> for u32 {
impl From<CSResponseType> for u8 {
fn from(value: CSResponseType) -> Self {
match value {
CSResponseType::CSResponseFailure => 0,
Expand All @@ -20,8 +20,8 @@ impl From<CSResponseType> for u32 {
}
}

impl From<u32> for CSResponseType {
fn from(value: u32) -> Self {
impl From<u8> for CSResponseType {
fn from(value: u8) -> Self {
match value {
0 => CSResponseType::CSResponseFailure,
1 => CSResponseType::CSResponseSuccess,
Expand Down Expand Up @@ -66,10 +66,8 @@ impl CSMessageResult {
pub fn encode(&self, e: &Env) -> Bytes {
let mut list: Vec<Bytes> = Vec::new(&e);

let code = self.response_code.into();

list.push_back(encoder::encode_u128(&e, self.sequence_no.clone()));
list.push_back(encoder::encode_u32(&e, code));
list.push_back(encoder::encode_u8(&e, self.response_code.into()));
list.push_back(encoder::encode(&e, self.message.clone()));

let encoded = encoder::encode_list(&e, list, false);
Expand All @@ -83,13 +81,13 @@ impl CSMessageResult {
}

let sequence_no = decoder::decode_u128(&e, decoded.get(0).unwrap());
let response_code = decoder::decode_u32(&e, decoded.get(1).unwrap());
let response_code = decoder::decode_u8(&e, decoded.get(1).unwrap()).into();
let message = decoder::decode(&e, decoded.get(2).unwrap_or(Bytes::new(&e)));

Ok(Self {
sequence_no,
message,
response_code: response_code.into(),
response_code,
})
}
}

0 comments on commit 75e0b74

Please sign in to comment.