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

fix: patch message type #278

Merged
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
2 changes: 1 addition & 1 deletion contracts/cosmwasm-vm/cw-integration/tests/test_xcall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ pub fn call_dapp_send_call(
ctx.app.execute_contract(
ctx.sender.clone(),
ctx.get_dapp(),
&cw_xcall_lib::xcall_msg::ExecuteMsg::SendCall {
&cw_mock_dapp_multi::msg::ExecuteMsg::SendMessageAny {
to: cw_xcall_lib::network_address::NetworkAddress::from_str(&to).unwrap(),
envelope,
},
Expand Down
9 changes: 3 additions & 6 deletions contracts/cosmwasm-vm/cw-mock-dapp-multi/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,18 +106,15 @@ impl<'a> CwMockService<'a> {
});

let msg = if message_type == MessageType::CallMessagePersisted as u64 {
AnyMessage::CallMessagePersisted(CallMessagePersisted { data: data.clone() })
AnyMessage::CallMessagePersisted(CallMessagePersisted { data })
} else if message_type == MessageType::CallMessageWithRollback as u64 {
if let Some(rollback) = rollback {
AnyMessage::CallMessageWithRollback(CallMessageWithRollback {
data: data.clone(),
rollback,
})
AnyMessage::CallMessageWithRollback(CallMessageWithRollback { data, rollback })
} else {
return Err(ContractError::InvalidRollbackMessage);
}
} else if message_type == MessageType::CallMessage as u64 {
AnyMessage::CallMessage(CallMessage { data: data.clone() })
AnyMessage::CallMessage(CallMessage { data })
} else {
return Err(ContractError::InvalidMessageType);
};
Expand Down
11 changes: 4 additions & 7 deletions contracts/cosmwasm-vm/cw-mock-dapp/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,11 @@ impl<'a> CwMockService<'a> {
.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(),
})
AnyMessage::CallMessagePersisted(CallMessagePersisted { data })
} else if let Some(rollback) = rollback {
AnyMessage::CallMessageWithRollback(CallMessageWithRollback { data, rollback })
} else {
AnyMessage::CallMessage(CallMessage { data: data.clone() })
AnyMessage::CallMessage(CallMessage { data })
};
let envelope = Envelope::new(msg, vec![], vec![]);

Expand Down
4 changes: 2 additions & 2 deletions contracts/cosmwasm-vm/cw-xcall-lib/src/message/envelope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ mod tests {
let encoded_data = rlp::encode(&envelope).to_vec();

assert_eq!(
"e50185c483010203d087736f757263653187736f7572636532cc856465737431856465737432",
"e50085c483010203d087736f757263653187736f7572636532cc856465737431856465737432",
hex::encode(&encoded_data)
);
let decoded: Envelope = rlp::decode(&encoded_data).unwrap();
Expand All @@ -113,7 +113,7 @@ mod tests {
let encoded_data = rlp::encode(&envelope).to_vec();

assert_eq!(
"e90289c88301020383010203d087736f757263653187736f7572636532cc856465737431856465737432",
"e90189c88301020383010203d087736f757263653187736f7572636532cc856465737431856465737432",
hex::encode(&encoded_data)
);
let decoded: Envelope = rlp::decode(&encoded_data).unwrap();
Expand Down
18 changes: 9 additions & 9 deletions contracts/cosmwasm-vm/cw-xcall-lib/src/message/msg_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,27 @@

#[derive(Clone, Debug, Serialize, serde::Deserialize, PartialEq, Eq)]
pub enum MessageType {
CallMessage = 1,
CallMessageWithRollback = 2,
CallMessagePersisted = 3,
CallMessage = 0,
CallMessageWithRollback = 1,
CallMessagePersisted = 2,
}

impl From<MessageType> for u8 {
fn from(val: MessageType) -> Self {
match val {
MessageType::CallMessage => 1,
MessageType::CallMessageWithRollback => 2,
MessageType::CallMessagePersisted => 3,
MessageType::CallMessage => 0,
MessageType::CallMessageWithRollback => 1,
MessageType::CallMessagePersisted => 2,

Check warning on line 15 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#L15

Added line #L15 was not covered by tests
}
}
}

impl From<u8> for MessageType {
fn from(value: u8) -> Self {
match value {
1 => MessageType::CallMessage,
2 => MessageType::CallMessageWithRollback,
3 => MessageType::CallMessagePersisted,
0 => MessageType::CallMessage,
1 => MessageType::CallMessageWithRollback,
2 => MessageType::CallMessagePersisted,

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#L23-L25

Added lines #L23 - L25 were not covered by tests
_ => panic!("unsupported message type"),
}
}
Expand Down
6 changes: 3 additions & 3 deletions contracts/cosmwasm-vm/cw-xcall/src/types/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ mod tests {

let encoded = rlp::encode(&msg);

assert_eq!("f83f8b3078312e4554482f307861aa63783030303030303030303030303030303030303030303030303030303030303030303030303031303215018474657374c0",hex::encode(encoded));
assert_eq!("f83f8b3078312e4554482f307861aa63783030303030303030303030303030303030303030303030303030303030303030303030303031303215008474657374c0",hex::encode(encoded));

let msg = CSMessageRequest::new(
NetworkAddress::from_str("0x1.ETH/0xa").unwrap(),
Expand All @@ -204,7 +204,7 @@ mod tests {

let encoded = rlp::encode(&msg);

assert_eq!("f84b8b3078312e4554482f307861aa63783030303030303030303030303030303030303030303030303030303030303030303030303031303215018474657374cc836162638363646583656667",hex::encode(encoded));
assert_eq!("f84b8b3078312e4554482f307861aa63783030303030303030303030303030303030303030303030303030303030303030303030303031303215008474657374cc836162638363646583656667",hex::encode(encoded));

let msg = CSMessageRequest::new(
NetworkAddress::from_str("0x1.ETH/0xa").unwrap(),
Expand All @@ -217,6 +217,6 @@ mod tests {

let encoded = rlp::encode(&msg);

assert_eq!("f84b8b3078312e4554482f307861aa63783030303030303030303030303030303030303030303030303030303030303030303030303031303215028474657374cc836162638363646583656667",hex::encode(encoded));
assert_eq!("f84b8b3078312e4554482f307861aa63783030303030303030303030303030303030303030303030303030303030303030303030303031303215018474657374cc836162638363646583656667",hex::encode(encoded));
}
}
6 changes: 3 additions & 3 deletions contracts/evm/library/utils/Types.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ library Types {

int constant CS_RESULT = 2;

int constant CALL_MESSAGE_TYPE = 1;
int constant CALL_MESSAGE_ROLLBACK_TYPE = 2;
int constant PERSISTENT_MESSAGE_TYPE = 3;
int constant CALL_MESSAGE_TYPE = 0;
int constant CALL_MESSAGE_ROLLBACK_TYPE = 1;
int constant PERSISTENT_MESSAGE_TYPE = 2;

/**
* Legacy Code, CallRequest replaced with RollbackData
Expand Down
12 changes: 6 additions & 6 deletions contracts/evm/test/xcall/CallService.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ contract CallServiceTest is Test {
vm.expectEmit();
emit CallMessageSent(address(dapp), iconDapp, 1);

Types.CSMessageRequestV2 memory request = Types.CSMessageRequestV2(ethDappAddress, dstAccount, 1, 1, data, _baseDestination);
Types.CSMessageRequestV2 memory request = Types.CSMessageRequestV2(ethDappAddress, dstAccount, 1, 0, data, _baseDestination);
Types.CSMessage memory message = Types.CSMessage(Types.CS_REQUEST, request.encodeCSMessageRequestV2());

vm.expectCall(address(baseConnection), abi.encodeCall(baseConnection.sendMessage, (iconNid, Types.NAME, 0, message.encodeCSMessage())));
Expand Down Expand Up @@ -204,7 +204,7 @@ contract CallServiceTest is Test {
vm.expectEmit();
emit CallMessageSent(address(dapp), iconDapp, 1);

Types.CSMessageRequestV2 memory request = Types.CSMessageRequestV2(ethDappAddress, dstAccount, 1, 1, data, destinations);
Types.CSMessageRequestV2 memory request = Types.CSMessageRequestV2(ethDappAddress, dstAccount, 1, 0, data, destinations);
Types.CSMessage memory message = Types.CSMessage(Types.CS_REQUEST,request.encodeCSMessageRequestV2());

vm.expectCall(address(connection1), abi.encodeCall(connection1.sendMessage, (iconNid, Types.NAME, 0, message.encodeCSMessage())));
Expand Down Expand Up @@ -268,7 +268,7 @@ contract CallServiceTest is Test {
vm.expectEmit();
emit CallMessageSent(address(dapp), iconDapp, 1);

Types.CSMessageRequestV2 memory request = Types.CSMessageRequestV2(ethDappAddress, dstAccount, 1, 2, data, new string[](0));
Types.CSMessageRequestV2 memory request = Types.CSMessageRequestV2(ethDappAddress, dstAccount, 1, 1, data, new string[](0));
Types.CSMessage memory message = Types.CSMessage(Types.CS_REQUEST,request.encodeCSMessageRequestV2());
vm.expectCall(address(baseConnection), abi.encodeCall(baseConnection.sendMessage, (iconNid, Types.NAME, 1, message.encodeCSMessage())));

Expand Down Expand Up @@ -877,7 +877,7 @@ contract CallServiceTest is Test {
vm.mockCall(address(connection1), abi.encodeWithSelector(connection1.getFee.selector), abi.encode(0));
vm.mockCall(address(connection2), abi.encodeWithSelector(connection2.getFee.selector), abi.encode(0));

Types.CSMessageRequestV2 memory request = Types.CSMessageRequestV2(iconDapp, ParseAddress.toString(address(receiver)), 1, 2, data, connections);
Types.CSMessageRequestV2 memory request = Types.CSMessageRequestV2(iconDapp, ParseAddress.toString(address(receiver)), 1, 1, data, connections);
Types.CSMessage memory message = Types.CSMessage(Types.CS_REQUEST,request.encodeCSMessageRequestV2());

vm.prank(address(connection1));
Expand Down Expand Up @@ -907,7 +907,7 @@ contract CallServiceTest is Test {
defaultServiceReceiver = IDefaultCallServiceReceiver(address(0x5678));
callService.setDefaultConnection(netTo, address(baseConnection));

Types.CSMessageRequestV2 memory request = Types.CSMessageRequestV2(iconDapp, ParseAddress.toString(address(defaultServiceReceiver)), 1, 2, data, _baseSource);
Types.CSMessageRequestV2 memory request = Types.CSMessageRequestV2(iconDapp, ParseAddress.toString(address(defaultServiceReceiver)), 1, 1, data, _baseSource);
Types.CSMessage memory message = Types.CSMessage(Types.CS_REQUEST,request.encodeCSMessageRequestV2());

vm.prank(address(baseConnection));
Expand All @@ -929,7 +929,7 @@ contract CallServiceTest is Test {
function testExecuteCallFailedExecution() public {
bytes memory data = bytes("test");

Types.CSMessageRequestV2 memory request = Types.CSMessageRequestV2(iconDapp, ParseAddress.toString(address(receiver)), 1, 2, data, _baseSource);
Types.CSMessageRequestV2 memory request = Types.CSMessageRequestV2(iconDapp, ParseAddress.toString(address(receiver)), 1, 1, data, _baseSource);
Types.CSMessage memory message = Types.CSMessage(Types.CS_REQUEST,request.encodeCSMessageRequestV2());

vm.prank(address(baseConnection));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
package foundation.icon.xcall.messages;

public class CallMessage extends Message {
public static final int TYPE = 1;
public static final int TYPE = 0;
private byte[] data;

public CallMessage(byte[] data) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import score.ObjectWriter;

public class CallMessageWithRollback extends Message {
public static final int TYPE = 2;
public static final int TYPE = 1;
private byte[] data;
private byte[] rollback;
public CallMessageWithRollback(byte[] data, byte[] rollback) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
package foundation.icon.xcall.messages;

public class PersistentMessage extends Message {
public static final int TYPE = 3;
public static final int TYPE = 2;
private byte[] data;

public PersistentMessage(byte[] data) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public void sendMessage_singleProtocol() {
xcall.invoke(dapp.account, "sendCallMessage", ethDapp.toString(), data, null, baseSource, baseDestination);

// Assert
CSMessageRequest request = new CSMessageRequest(iconDappAddress.toString(), ethDapp.account.toString(), BigInteger.ONE, 1, data, baseDestination);
CSMessageRequest request = new CSMessageRequest(iconDappAddress.toString(), ethDapp.account.toString(), BigInteger.ONE, 0, data, baseDestination);

CSMessage msg = new CSMessage(CSMessage.REQUEST, request.toBytes());
verify(baseConnection.mock).sendMessage(eq(ethNid), eq(CallService.NAME), eq(BigInteger.ZERO), aryEq(msg.toBytes()));
Expand All @@ -107,7 +107,7 @@ public void sendMessage_defaultProtocol() {
xcall.invoke(dapp.account, "sendCallMessage", ethDapp.toString(), data);

// Assert
CSMessageRequest request = new CSMessageRequest(iconDappAddress.toString(), ethDapp.account.toString(), BigInteger.ONE, 1, data, null);
CSMessageRequest request = new CSMessageRequest(iconDappAddress.toString(), ethDapp.account.toString(), BigInteger.ONE, 0, data, null);
CSMessage msg = new CSMessage(CSMessage.REQUEST, request.toBytes());
verify(baseConnection.mock).sendMessage(eq(ethNid), eq(CallService.NAME), eq(BigInteger.ZERO), aryEq(msg.toBytes()));
verify(xcallSpy).CallMessageSent(dapp.getAddress(), ethDapp.toString(), BigInteger.ONE);
Expand Down Expand Up @@ -139,7 +139,7 @@ public void sendMessage_multiProtocol() throws Exception {
xcall.invoke(dapp.account, "sendCallMessage", ethDapp.toString(), data, null, sources, destinations);

// Assert
CSMessageRequest request = new CSMessageRequest(iconDappAddress.toString(), ethDapp.account.toString(), BigInteger.ONE, 1, data, destinations);
CSMessageRequest request = new CSMessageRequest(iconDappAddress.toString(), ethDapp.account.toString(), BigInteger.ONE, 0, data, destinations);
CSMessage msg = new CSMessage(CSMessage.REQUEST, request.toBytes());
verify(connection1.mock).sendMessage(eq(ethNid), eq(CallService.NAME), eq(BigInteger.ZERO), aryEq(msg.toBytes()));
verify(connection2.mock).sendMessage(eq(ethNid), eq(CallService.NAME), eq(BigInteger.ZERO), aryEq(msg.toBytes()));
Expand Down Expand Up @@ -453,7 +453,7 @@ public void executeCall_multiProtocol_rollback() throws Exception {
MockContract<Connection> connection2 = new MockContract<>(ConnectionScoreInterface.class, Connection.class, sm, owner);
String[] connections = {connection1.getAddress().toString(), connection2.getAddress().toString()};

CSMessageRequest request = new CSMessageRequest(ethDapp.toString(), dapp.getAddress().toString(), BigInteger.ONE, 2, data, connections);
CSMessageRequest request = new CSMessageRequest(ethDapp.toString(), dapp.getAddress().toString(), BigInteger.ONE, 1, data, connections);
CSMessage msg = new CSMessage(CSMessage.REQUEST, request.toBytes());
xcall.invoke(connection1.account, "handleBTPMessage", ethNid, CallService.NAME, BigInteger.ZERO, msg.toBytes());
xcall.invoke(connection2.account, "handleBTPMessage", ethNid, CallService.NAME, BigInteger.ZERO, msg.toBytes());
Expand Down
Loading