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

feat: multi message rust #171

Merged
Show file tree
Hide file tree
Changes from 11 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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion contracts/cosmwasm-vm/cw-xcall-lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ schemars = {workspace=true}
serde = { workspace=true}
thiserror = { workspace=true}
debug_print={workspace=true}

common = { git = "https://github.com/icon-project/IBC-Integration.git",branch="main" }


[dev-dependencies]
Expand Down
1 change: 1 addition & 0 deletions contracts/cosmwasm-vm/cw-xcall-lib/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod dapp_msg;
pub mod dapp_multi_msg;
pub mod message;
pub mod network_address;
pub mod xcall_connection_msg;
pub mod xcall_msg;
58 changes: 58 additions & 0 deletions contracts/cosmwasm-vm/cw-xcall-lib/src/message/call_message.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use common::rlp::{self, Decodable, DecoderError, Encodable, RlpStream};

use super::{msg_trait::IMessage, msg_type::MessageType};

#[derive(Clone, Debug, PartialEq)]

Check warning on line 5 in contracts/cosmwasm-vm/cw-xcall-lib/src/message/call_message.rs

View check run for this annotation

Codecov / codecov/patch

contracts/cosmwasm-vm/cw-xcall-lib/src/message/call_message.rs#L5

Added line #L5 was not covered by tests
pub struct CallMessage {
pub msg_type: MessageType,
pub data: Vec<u8>,
}

impl Encodable for CallMessage {
fn rlp_append(&self, stream: &mut RlpStream) {
stream
.begin_list(2)
.append(&Into::<u8>::into(self.msg_type.clone()))
.append(&self.data);
}

Check warning on line 17 in contracts/cosmwasm-vm/cw-xcall-lib/src/message/call_message.rs

View check run for this annotation

Codecov / codecov/patch

contracts/cosmwasm-vm/cw-xcall-lib/src/message/call_message.rs#L12-L17

Added lines #L12 - L17 were not covered by tests
ibrizsabin marked this conversation as resolved.
Show resolved Hide resolved
}

impl Decodable for CallMessage {
fn decode(rlp: &rlp::Rlp) -> Result<Self, rlp::DecoderError> {
let msg_type: u8 = rlp.val_at(0)?;

Check warning on line 22 in contracts/cosmwasm-vm/cw-xcall-lib/src/message/call_message.rs

View check run for this annotation

Codecov / codecov/patch

contracts/cosmwasm-vm/cw-xcall-lib/src/message/call_message.rs#L21-L22

Added lines #L21 - L22 were not covered by tests

Ok(Self {
msg_type: MessageType::from(msg_type),
data: rlp.val_at(1)?,

Check warning on line 26 in contracts/cosmwasm-vm/cw-xcall-lib/src/message/call_message.rs

View check run for this annotation

Codecov / codecov/patch

contracts/cosmwasm-vm/cw-xcall-lib/src/message/call_message.rs#L25-L26

Added lines #L25 - L26 were not covered by tests
})
}

Check warning on line 28 in contracts/cosmwasm-vm/cw-xcall-lib/src/message/call_message.rs

View check run for this annotation

Codecov / codecov/patch

contracts/cosmwasm-vm/cw-xcall-lib/src/message/call_message.rs#L28

Added line #L28 was not covered by tests
}

impl IMessage for CallMessage {
fn rollback(&self) -> Option<Vec<u8>> {
None
}

Check warning on line 34 in contracts/cosmwasm-vm/cw-xcall-lib/src/message/call_message.rs

View check run for this annotation

Codecov / codecov/patch

contracts/cosmwasm-vm/cw-xcall-lib/src/message/call_message.rs#L32-L34

Added lines #L32 - L34 were not covered by tests

fn data(&self) -> Vec<u8> {
self.data.clone()
}

Check warning on line 38 in contracts/cosmwasm-vm/cw-xcall-lib/src/message/call_message.rs

View check run for this annotation

Codecov / codecov/patch

contracts/cosmwasm-vm/cw-xcall-lib/src/message/call_message.rs#L36-L38

Added lines #L36 - L38 were not covered by tests

fn msg_type(&self) -> &MessageType {
&self.msg_type
}

Check warning on line 42 in contracts/cosmwasm-vm/cw-xcall-lib/src/message/call_message.rs

View check run for this annotation

Codecov / codecov/patch

contracts/cosmwasm-vm/cw-xcall-lib/src/message/call_message.rs#L40-L42

Added lines #L40 - L42 were not covered by tests

fn should_persist(&self) -> bool {
false
}

Check warning on line 46 in contracts/cosmwasm-vm/cw-xcall-lib/src/message/call_message.rs

View check run for this annotation

Codecov / codecov/patch

contracts/cosmwasm-vm/cw-xcall-lib/src/message/call_message.rs#L44-L46

Added lines #L44 - L46 were not covered by tests

fn to_bytes(&self) -> Result<Vec<u8>, DecoderError> {
Ok(rlp::encode(self).to_vec())
}

Check warning on line 50 in contracts/cosmwasm-vm/cw-xcall-lib/src/message/call_message.rs

View check run for this annotation

Codecov / codecov/patch

contracts/cosmwasm-vm/cw-xcall-lib/src/message/call_message.rs#L48-L50

Added lines #L48 - L50 were not covered by tests

// fn from_bytes(bytes: Vec<u8>) -> Result<Self, rlp::DecoderError> {
// let rlp = rlp::Rlp::new(&bytes);
// let msg = Self::decode(&rlp)?;

// Ok(msg)
// }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use common::rlp::{self, Decodable, DecoderError, Encodable, RlpStream};

use super::{msg_trait::IMessage, msg_type::MessageType};

#[derive(Clone, Debug, PartialEq)]

Check warning on line 5 in contracts/cosmwasm-vm/cw-xcall-lib/src/message/call_message_rollback.rs

View check run for this annotation

Codecov / codecov/patch

contracts/cosmwasm-vm/cw-xcall-lib/src/message/call_message_rollback.rs#L5

Added line #L5 was not covered by tests
pub struct CallMessageWithRollback {
pub msg_type: MessageType,
pub data: Vec<u8>,
pub rollback: Vec<u8>,
}

impl Encodable for CallMessageWithRollback {
fn rlp_append(&self, stream: &mut RlpStream) {
stream
.begin_list(2)
.append(&Into::<u8>::into(self.msg_type.clone()))
ibrizsabin marked this conversation as resolved.
Show resolved Hide resolved
.append(&self.data)
.append(&self.rollback);
}

Check warning on line 19 in contracts/cosmwasm-vm/cw-xcall-lib/src/message/call_message_rollback.rs

View check run for this annotation

Codecov / codecov/patch

contracts/cosmwasm-vm/cw-xcall-lib/src/message/call_message_rollback.rs#L13-L19

Added lines #L13 - L19 were not covered by tests
}

impl Decodable for CallMessageWithRollback {
fn decode(rlp: &rlp::Rlp) -> Result<Self, rlp::DecoderError> {
let msg_type: u8 = rlp.val_at(0)?;

Check warning on line 24 in contracts/cosmwasm-vm/cw-xcall-lib/src/message/call_message_rollback.rs

View check run for this annotation

Codecov / codecov/patch

contracts/cosmwasm-vm/cw-xcall-lib/src/message/call_message_rollback.rs#L23-L24

Added lines #L23 - L24 were not covered by tests

Ok(Self {
msg_type: MessageType::from(msg_type),
data: rlp.val_at(1)?,
rollback: rlp.val_at(2)?,

Check warning on line 29 in contracts/cosmwasm-vm/cw-xcall-lib/src/message/call_message_rollback.rs

View check run for this annotation

Codecov / codecov/patch

contracts/cosmwasm-vm/cw-xcall-lib/src/message/call_message_rollback.rs#L27-L29

Added lines #L27 - L29 were not covered by tests
})
}

Check warning on line 31 in contracts/cosmwasm-vm/cw-xcall-lib/src/message/call_message_rollback.rs

View check run for this annotation

Codecov / codecov/patch

contracts/cosmwasm-vm/cw-xcall-lib/src/message/call_message_rollback.rs#L31

Added line #L31 was not covered by tests
}

impl IMessage for CallMessageWithRollback {
fn rollback(&self) -> Option<Vec<u8>> {
Some(self.rollback.clone())
}

fn data(&self) -> Vec<u8> {
self.data.clone()
}

fn msg_type(&self) -> &MessageType {
&self.msg_type
}

fn should_persist(&self) -> bool {
true
}
fn to_bytes(&self) -> Result<Vec<u8>, DecoderError> {
Ok(rlp::encode(self).to_vec())
}

Check warning on line 52 in contracts/cosmwasm-vm/cw-xcall-lib/src/message/call_message_rollback.rs

View check run for this annotation

Codecov / codecov/patch

contracts/cosmwasm-vm/cw-xcall-lib/src/message/call_message_rollback.rs#L50-L52

Added lines #L50 - L52 were not covered by tests

// fn from_bytes(bytes: Vec<u8>) -> Result<Self, rlp::DecoderError> {
// let rlp = rlp::Rlp::new(&bytes);
// let msg = Self::decode(&rlp)?;

// Ok(msg)
// }
}
116 changes: 116 additions & 0 deletions contracts/cosmwasm-vm/cw-xcall-lib/src/message/envelope.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
use common::rlp::{self, Decodable, DecoderError, Encodable};

use super::{
call_message::CallMessage, call_message_rollback::CallMessageWithRollback, msg_trait::IMessage,
msg_type::MessageType, AnyMessage,
};
#[derive(Clone, Debug, PartialEq)]

Check warning on line 7 in contracts/cosmwasm-vm/cw-xcall-lib/src/message/envelope.rs

View check run for this annotation

Codecov / codecov/patch

contracts/cosmwasm-vm/cw-xcall-lib/src/message/envelope.rs#L7

Added line #L7 was not covered by tests
pub struct Envelope {
pub message: AnyMessage,
pub sources: Vec<String>,
pub destinations: Vec<String>,
}

impl Envelope {
pub fn new(msg: AnyMessage, sources: Vec<String>, destinations: Vec<String>) -> Self {
Self {
message: msg,
sources,
destinations,
}
}
}

impl Encodable for Envelope {
fn rlp_append(&self, stream: &mut common::rlp::RlpStream) {
stream.begin_list(4);
stream.append(&Into::<u8>::into(self.message.msg_type().clone()));
stream.append(&self.message.to_bytes().unwrap());
stream.begin_list(self.sources.len());
for source in self.sources.iter() {
stream.append(source);
}
stream.begin_list(self.destinations.len());
for dest in self.destinations.iter() {
stream.append(dest);
}
}

Check warning on line 37 in contracts/cosmwasm-vm/cw-xcall-lib/src/message/envelope.rs

View check run for this annotation

Codecov / codecov/patch

contracts/cosmwasm-vm/cw-xcall-lib/src/message/envelope.rs#L25-L37

Added lines #L25 - L37 were not covered by tests
}

impl Decodable for Envelope {
fn decode(rlp: &rlp::Rlp) -> Result<Self, rlp::DecoderError> {
let msg_int: u8 = rlp.val_at(0)?;
let msg_type = MessageType::from(msg_int);
let message_bytes: Vec<u8> = rlp.val_at(1)?;
let message = decode_message(msg_type, message_bytes)?;

Check warning on line 45 in contracts/cosmwasm-vm/cw-xcall-lib/src/message/envelope.rs

View check run for this annotation

Codecov / codecov/patch

contracts/cosmwasm-vm/cw-xcall-lib/src/message/envelope.rs#L41-L45

Added lines #L41 - L45 were not covered by tests

let sources = rlp.at(2)?;
let sources: Vec<String> = sources.as_list()?;
let destinations = rlp.at(3)?;
let destinations: Vec<String> = destinations.as_list()?;

Check warning on line 50 in contracts/cosmwasm-vm/cw-xcall-lib/src/message/envelope.rs

View check run for this annotation

Codecov / codecov/patch

contracts/cosmwasm-vm/cw-xcall-lib/src/message/envelope.rs#L47-L50

Added lines #L47 - L50 were not covered by tests

Ok(Envelope {
message,
sources,
destinations,
})
}

Check warning on line 57 in contracts/cosmwasm-vm/cw-xcall-lib/src/message/envelope.rs

View check run for this annotation

Codecov / codecov/patch

contracts/cosmwasm-vm/cw-xcall-lib/src/message/envelope.rs#L52-L57

Added lines #L52 - L57 were not covered by tests
}

pub fn decode_message(msg_type: MessageType, bytes: Vec<u8>) -> Result<AnyMessage, DecoderError> {
match msg_type {

Check warning on line 61 in contracts/cosmwasm-vm/cw-xcall-lib/src/message/envelope.rs

View check run for this annotation

Codecov / codecov/patch

contracts/cosmwasm-vm/cw-xcall-lib/src/message/envelope.rs#L60-L61

Added lines #L60 - L61 were not covered by tests
MessageType::BasicMessage => {
let msg: CallMessage = rlp::decode(&bytes)?;
Ok(AnyMessage::CallMessage(msg))

Check warning on line 64 in contracts/cosmwasm-vm/cw-xcall-lib/src/message/envelope.rs

View check run for this annotation

Codecov / codecov/patch

contracts/cosmwasm-vm/cw-xcall-lib/src/message/envelope.rs#L63-L64

Added lines #L63 - L64 were not covered by tests
}
MessageType::MessageWithRollback => {
let msg: CallMessageWithRollback = rlp::decode(&bytes)?;
Ok(AnyMessage::CallMessageWithRollback(msg))

Check warning on line 68 in contracts/cosmwasm-vm/cw-xcall-lib/src/message/envelope.rs

View check run for this annotation

Codecov / codecov/patch

contracts/cosmwasm-vm/cw-xcall-lib/src/message/envelope.rs#L67-L68

Added lines #L67 - L68 were not covered by tests
}
}
}

Check warning on line 71 in contracts/cosmwasm-vm/cw-xcall-lib/src/message/envelope.rs

View check run for this annotation

Codecov / codecov/patch

contracts/cosmwasm-vm/cw-xcall-lib/src/message/envelope.rs#L71

Added line #L71 was not covered by tests

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_encoding_decoding_envelope_call_message() {
// Create a sample Envelope
let message = AnyMessage::CallMessage(CallMessage {
msg_type: MessageType::BasicMessage,
ibrizsabin marked this conversation as resolved.
Show resolved Hide resolved
data: vec![1, 2, 3],
});
let sources = vec!["source1".to_string(), "source2".to_string()];
let destinations = vec!["dest1".to_string(), "dest2".to_string()];
let envelope = Envelope::new(message, sources, destinations);
let encoded_data = rlp::encode(&envelope).to_vec();

assert_eq!(
"e60186c50183010203d087736f757263653187736f7572636532cc856465737431856465737432",
hex::encode(&encoded_data)
);
let decoded: Envelope = rlp::decode(&encoded_data).unwrap();

assert_eq!(envelope, decoded);
}

#[test]
fn test_encoding_decoding_envelope_call_message_rollback() {
// Create a sample Envelope
let message = AnyMessage::CallMessageWithRollback(CallMessageWithRollback {
msg_type: MessageType::MessageWithRollback,
data: vec![1, 2, 3],
rollback: vec![1, 2, 3],
});
let sources = vec!["source1".to_string(), "source2".to_string()];
let destinations = vec!["dest1".to_string(), "dest2".to_string()];
let envelope = Envelope::new(message, sources, destinations);
let encoded_data = rlp::encode(&envelope).to_vec();

assert_eq!("ea028ac5028301020383010203d087736f757263653187736f7572636532cc856465737431856465737432",hex::encode(&encoded_data));
let decoded: Envelope = rlp::decode(&encoded_data).unwrap();

assert_eq!(envelope, decoded);
}
}
77 changes: 77 additions & 0 deletions contracts/cosmwasm-vm/cw-xcall-lib/src/message/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
use common::rlp::DecoderError;

use self::{
call_message::CallMessage, call_message_rollback::CallMessageWithRollback, msg_trait::IMessage,
msg_type::MessageType,
};

pub mod call_message;
pub mod call_message_rollback;
pub mod envelope;
pub mod msg_trait;
pub mod msg_type;
#[derive(Clone, Debug, PartialEq)]

Check warning on line 13 in contracts/cosmwasm-vm/cw-xcall-lib/src/message/mod.rs

View check run for this annotation

Codecov / codecov/patch

contracts/cosmwasm-vm/cw-xcall-lib/src/message/mod.rs#L13

Added line #L13 was not covered by tests
pub enum AnyMessage {
CallMessage(CallMessage),
CallMessageWithRollback(CallMessageWithRollback),
}

impl IMessage for AnyMessage {
fn rollback(&self) -> Option<Vec<u8>> {
match self {
AnyMessage::CallMessage(m) => m.rollback(),

Check warning on line 22 in contracts/cosmwasm-vm/cw-xcall-lib/src/message/mod.rs

View check run for this annotation

Codecov / codecov/patch

contracts/cosmwasm-vm/cw-xcall-lib/src/message/mod.rs#L22

Added line #L22 was not covered by tests
AnyMessage::CallMessageWithRollback(m) => m.rollback(),
}
}

fn data(&self) -> Vec<u8> {
match self {
AnyMessage::CallMessage(m) => m.data(),

Check warning on line 29 in contracts/cosmwasm-vm/cw-xcall-lib/src/message/mod.rs

View check run for this annotation

Codecov / codecov/patch

contracts/cosmwasm-vm/cw-xcall-lib/src/message/mod.rs#L29

Added line #L29 was not covered by tests
AnyMessage::CallMessageWithRollback(m) => m.data(),
}
}

fn msg_type(&self) -> &MessageType {
match self {
AnyMessage::CallMessage(m) => m.msg_type(),

Check warning on line 36 in contracts/cosmwasm-vm/cw-xcall-lib/src/message/mod.rs

View check run for this annotation

Codecov / codecov/patch

contracts/cosmwasm-vm/cw-xcall-lib/src/message/mod.rs#L36

Added line #L36 was not covered by tests
AnyMessage::CallMessageWithRollback(m) => m.msg_type(),
}
}

fn should_persist(&self) -> bool {
match self {
AnyMessage::CallMessage(m) => m.should_persist(),

Check warning on line 43 in contracts/cosmwasm-vm/cw-xcall-lib/src/message/mod.rs

View check run for this annotation

Codecov / codecov/patch

contracts/cosmwasm-vm/cw-xcall-lib/src/message/mod.rs#L43

Added line #L43 was not covered by tests
AnyMessage::CallMessageWithRollback(m) => m.should_persist(),
}
}

fn to_bytes(&self) -> Result<Vec<u8>, DecoderError> {
match self {
AnyMessage::CallMessage(m) => m.to_bytes(),
AnyMessage::CallMessageWithRollback(m) => m.to_bytes(),

Check warning on line 51 in contracts/cosmwasm-vm/cw-xcall-lib/src/message/mod.rs

View check run for this annotation

Codecov / codecov/patch

contracts/cosmwasm-vm/cw-xcall-lib/src/message/mod.rs#L48-L51

Added lines #L48 - L51 were not covered by tests
}
}

Check warning on line 53 in contracts/cosmwasm-vm/cw-xcall-lib/src/message/mod.rs

View check run for this annotation

Codecov / codecov/patch

contracts/cosmwasm-vm/cw-xcall-lib/src/message/mod.rs#L53

Added line #L53 was not covered by tests

// fn from_bytes(bytes:Vec<u8>)->Result<Self,DecoderError> {
// todo!()
// }
}

// impl Encodable for AnyMessage {
// fn rlp_append(&self, s: &mut common::rlp::RlpStream) {
// match self{
// AnyMessage::CallMessage(m)=>{
// s.append(&rlp::encode(m));
// },
// AnyMessage::CallMessageWithRollback(m)=>{
// s.append(&rlp::encode(m));
// }
// }
// }
// }

// impl Decodable for AnyMessage {
// fn decode(rlp: &rlp::Rlp) -> Result<Self, rlp::DecoderError> {

// }
// }
22 changes: 22 additions & 0 deletions contracts/cosmwasm-vm/cw-xcall-lib/src/message/msg_trait.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use common::rlp::DecoderError;

use super::msg_type::MessageType;

pub trait IMessage: Clone {
fn rollback(&self) -> Option<Vec<u8>>;
fn data(&self) -> Vec<u8>;
fn msg_type(&self) -> &MessageType;
// fn from_bytes(bytes: Vec<u8>) -> Result<Self, rlp::DecoderError> {
// let rlp = rlp::Rlp::new(&bytes);
// let msg = Self::decode(&rlp)?;

// Ok(msg)
// }

// fn to_bytes(&self) -> Vec<u8> {
// rlp::encode(self).to_vec()
// }
fn should_persist(&self) -> bool;
fn to_bytes(&self) -> Result<Vec<u8>, DecoderError>;
//fn from_bytes(bytes:Vec<u8>)->Result<Self,DecoderError>;
}
35 changes: 35 additions & 0 deletions contracts/cosmwasm-vm/cw-xcall-lib/src/message/msg_type.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use serde::Serialize;

#[derive(Clone, Debug, Serialize, serde::Deserialize, PartialEq, Eq)]
pub enum MessageType {
BasicMessage = 1,
MessageWithRollback = 2,
}

impl From<MessageType> for u8 {
fn from(val: MessageType) -> Self {
match val {
MessageType::BasicMessage => 1,
MessageType::MessageWithRollback => 2,
}
}
}

impl From<u8> for MessageType {
fn from(value: u8) -> Self {
match value {
1 => MessageType::BasicMessage,
2 => MessageType::MessageWithRollback,
_ => panic!("unsupported message type"),

Check warning on line 23 in contracts/cosmwasm-vm/cw-xcall-lib/src/message/msg_type.rs

View check run for this annotation

Codecov / codecov/patch

contracts/cosmwasm-vm/cw-xcall-lib/src/message/msg_type.rs#L19-L23

Added lines #L19 - L23 were not covered by tests
}
}

Check warning on line 25 in contracts/cosmwasm-vm/cw-xcall-lib/src/message/msg_type.rs

View check run for this annotation

Codecov / codecov/patch

contracts/cosmwasm-vm/cw-xcall-lib/src/message/msg_type.rs#L25

Added line #L25 was not covered by tests
}

impl MessageType {
pub fn as_int(&self) -> u8 {
self.clone().into()
}
pub fn from_int(val: u8) -> Self {
MessageType::from(val)
}

Check warning on line 34 in contracts/cosmwasm-vm/cw-xcall-lib/src/message/msg_type.rs

View check run for this annotation

Codecov / codecov/patch

contracts/cosmwasm-vm/cw-xcall-lib/src/message/msg_type.rs#L32-L34

Added lines #L32 - L34 were not covered by tests
}
Loading